0025418: Debug output to be limited to OCC development environment
[occt.git] / src / TNaming / TNaming_Naming.cxx
CommitLineData
b311480e 1// Created on: 1997-09-03
2// Created by: Yves FRICAUD
3// Copyright (c) 1997-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17#include <TNaming_Naming.ixx>
18
19#include <TNaming_Builder.hxx>
20#include <TNaming_Tool.hxx>
21#include <TNaming_ListIteratorOfListOfNamedShape.hxx>
22#include <TNaming_Iterator.hxx>
7fd59977 23#include <TNaming_Scope.hxx>
24#include <TNaming_NamingTool.hxx>
25#include <TNaming_Name.hxx>
26#include <TNaming_Identifier.hxx>
27#include <TNaming_Localizer.hxx>
28#include <TNaming_NewShapeIterator.hxx>
29#include <TNaming_OldShapeIterator.hxx>
30#include <TNaming_Selector.hxx>
31#include <TNaming.hxx>
7fd59977 32#include <TDF_ChildIterator.hxx>
33#include <TDF_LabelList.hxx>
34#include <TDF_Data.hxx>
35#include <TDF_LabelMap.hxx>
36#include <TDF_TagSource.hxx>
37#include <TopoDS_Iterator.hxx>
38#include <TopTools_MapOfShape.hxx>
39#include <TopExp.hxx>
40#include <TopExp_Explorer.hxx>
41#include <TopTools_MapIteratorOfMapOfShape.hxx>
42#include <Standard_DomainError.hxx>
43#include <TopTools_ListOfShape.hxx>
44#include <TopTools_ListIteratorOfListOfShape.hxx>
45#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46#include <TColStd_MapOfInteger.hxx>
47#include <TColStd_MapIteratorOfMapOfInteger.hxx>
48#include <TopTools_MapIteratorOfMapOfOrientedShape.hxx>
49#include <TopTools_HArray1OfShape.hxx>
1ec8a59e 50#include <BRepTools.hxx>
7fd59977 51#include <BRep_Tool.hxx>
52#include <TopoDS.hxx>
1ec8a59e 53#include <TopoDS_Wire.hxx>
54#include <TopoDS_Face.hxx>
55#include <TopoDS_Shell.hxx>
56#include <TopoDS_Solid.hxx>
7fd59977 57// #include <TNaming_NCollections.hxx>
58#include <NCollection_Map.hxx>
59#include <NCollection_DataMap.hxx>
60typedef NCollection_Map<TopoDS_Shape> TNaming_MapOfShape;
61typedef TNaming_MapOfShape::Iterator TNaming_MapIteratorOfMapOfShape;
62typedef NCollection_DataMap<TopoDS_Shape, TNaming_MapOfShape> TNaming_DataMapOfShapeMapOfShape;
63typedef TNaming_DataMapOfShapeMapOfShape::Iterator TNaming_DataMapIteratorOfDataMapOfShapeMapOfShape;
64// The bug concerns access to a null object in the method Filter():
65#define BUC60847
66
67#define OCC273
68#define OCC350
69#define OCC352
70#define ALLOW_CHILD_NBS
71//#define MDTV_DEB_CC
72//#define MDTV_DEB_OR
73//#define MDTV_DEB_MOD
74//#define MDTV_OR
75//#define MDTV_DEB_INNS
76//#define MDTV_DEB_NBS
77//#define MDTV_DEB_71
78//#define MDTV_DEB_WIN
0797d9d3 79#ifdef OCCT_DEBUG
7fd59977 80#include <TDF_MapIteratorOfLabelMap.hxx>
81#include <TCollection_AsciiString.hxx>
82#include <TDF_Tool.hxx>
83#include <BRepTools.hxx>
84#include <TNaming_Tool.hxx>
7fd59977 85#include <TDF_Tool.hxx>
86#include <TDF_MapIteratorOfLabelMap.hxx>
87
88#include <TCollection_AsciiString.hxx>
89#include <BRepTools.hxx>
90void Print_Entry(const TDF_Label& label)
91{
92 TCollection_AsciiString entry;
93 TDF_Tool::Entry(label, entry);
94 cout << "LabelEntry = "<< entry << endl;
95}
96static void Write(const TopoDS_Shape& shape,
97 const Standard_CString filename)
98{
99 char buf[256];
100 if(strlen(filename) > 256) return;
101 strcpy (buf, filename);
102 char* p = buf;
103 while (*p) {
104 if(*p == ':')
105 *p = '-';
106 p++;
107 }
108 ofstream save (buf);
109 if(!save)
110 cout << "File " << buf << " was not created: rdstate = " << save.rdstate() << endl;
111 save << "DBRep_DrawableShape" << endl << endl;
112 if(!shape.IsNull()) BRepTools::Write(shape, save);
113 save.close();
114}
115void WriteNSOnLabel(const Handle(TNaming_NamedShape) & NS, const TCollection_AsciiString& Nam)
116{
117 if (!NS.IsNull()) {
118 TCollection_AsciiString entry;
119 TDF_Tool::Entry(NS->Label(), entry);
120 TopoDS_Shape Sh = TNaming_Tool::GetShape (NS);
121 if(!Sh.IsNull()) {
122 TCollection_AsciiString Entry = Nam + entry + ".brep";
123 Write(Sh, Entry.ToCString());
124 }
125 else
126 cout << "WriteNSOnLabel>>> TopoDS_Shape IS NULL on Entry = "<< entry << endl;
127 }
128 else
129 cout << "WriteNSOnLabel >>> NamedShape IS NULL" << endl;
130}
7fd59977 131#endif
132
133//==========================================================================================
134static Handle(TNaming_NamedShape) BuildName (const TDF_Label& F,
135 TNaming_Scope& MDF,
136 const TopoDS_Shape& S,
137 const TopoDS_Shape& Context,
138 const Handle(TNaming_NamedShape)& Stop,
139 const Standard_Boolean Geometry);
140
141//=======================================================================
142static Standard_Integer RepeatabilityInContext(const TopoDS_Shape& Selection,
143 const TopoDS_Shape& Context);
144//=======================================================================
145extern Standard_Boolean IsEqual (const TopoDS_Shape& S1, const TopoDS_Shape& S2);
146
147//=======================================================================
148//function : Solve
149//purpose : voir avec YFR comment retrouver le bon resulat et le mettre
150// : dans le NamedShape de L
151//=======================================================================
152Standard_Boolean TNaming_Naming::Solve (TDF_LabelMap& Valid)
153{
154 Handle(TNaming_Naming) subname;
155 for (TDF_ChildIterator it (Label(),Standard_False); it.More(); it.Next()) {
0797d9d3 156#ifdef OCCT_DEBUG_NBS
7fd59977 157 TCollection_AsciiString anEntry;
158 TDF_Tool::Entry(it.Value(), anEntry);
159 cout << "TNaming_Naming::Solve: Label to be solved = " << anEntry << endl;
160#endif
161 if (it.Value().FindAttribute(TNaming_Naming::GetID(),subname)) {
162 if (!subname->Solve (Valid)) {
163 return Standard_False; // not necessary to continue
164 }
165 }
166 }
0797d9d3 167#ifdef OCCT_DEBUG_CC
7fd59977 168 TDF_MapIteratorOfLabelMap anItr(Valid);
169 cout << "TNaming_Naming::Solve:: Valid label Map" << endl;
170 for (; anItr.More(); anItr.Next()) {
171 const TDF_Label& aLabel = anItr.Key();
172 TCollection_AsciiString anEntry;
173 TDF_Tool::Entry(aLabel, anEntry);
174 cout << "Label = " << anEntry << endl;
175 }
176#endif
177 if (Regenerate(Valid)) {
178 if (!Valid.IsEmpty()) Valid.Add(Label());
179 return Standard_True;
180 }
181 return Standard_False;
182}
183
184
185//=======================================================================
186//function : GetID
187//purpose :
188//=======================================================================
189
190const Standard_GUID& TNaming_Naming::GetID ()
191{
192 static Standard_GUID TNaming_NamingID("c0a19201-5b78-11d1-8940-080009dc3333");
193 return TNaming_NamingID;
194}
195
196//=======================================================================
197//function : Insert
198//purpose :
199//=======================================================================
200
201Handle(TNaming_Naming) TNaming_Naming::Insert (const TDF_Label& under)
202{
203 Handle(TNaming_Naming) N;
204 TDF_Label child = TDF_TagSource::NewChild(under);
205 N = new TNaming_Naming ();
206 child.AddAttribute (N);
207 return N;
208}
209
210//=======================================================================
211//function : BuildNS
212//purpose : returns a new NamedShape, which is built as selection:
213// : TNaming_Builder::Select("S","S") at the new child label of the label <F>.
214//=======================================================================
215
216static Handle(TNaming_NamedShape) BuildNS (const TDF_Label& F,
217 const TopoDS_Shape& S,
218 const TNaming_NameType& Name)
219{
220 Handle (TNaming_Naming) Naming = TNaming_Naming::Insert (F);
221
222 TNaming_Name& theName = Naming->ChangeName();
223 theName.ShapeType(S.ShapeType());
224 theName.Shape(S);
7dcac1df 225 theName.Orientation(S.Orientation());
7fd59977 226 theName.Type(Name);
227 TNaming_Builder B(Naming->Label());
228 B.Select(S,S);
229 return B.NamedShape();
230}
231
232//=======================================================================
233//function : FindIndex
234//purpose :
235//=======================================================================
236
237static Standard_Integer FindIndex(const Handle(TNaming_NamedShape)& NS,
238 const TopoDS_Shape& S)
239{
240 TDF_LabelList Labels;
241 TopoDS_Shape IS = TNaming_Tool::InitialShape(S,NS->Label(),Labels);
242 Standard_Integer Index = 1;
243 for (TNaming_Iterator itNS(NS); itNS.More(); itNS.Next(),Index++) {
244 if (IS.IsSame(itNS.NewShape())) break;
245 }
246 return Index;
247}
248
249
250//=======================================================================
251//function : CompareInGeneration
252//purpose : returns true, only if the specified NS contains only <S> in
253// : the "new shapes" set.
254//=======================================================================
255
256static Standard_Boolean CompareInGeneration (const Handle(TNaming_NamedShape)& NS,
257 const TopoDS_Shape& S)
258{
259 for (TNaming_Iterator it(NS); it.More(); it.Next()) {
260 if (!it.NewShape().IsSame(S)) return 0;
261 }
262 return 1;
263}
264
265#ifdef OCC350
266
267//=======================================================================
268//function : GetShapeEvolutions
269//purpose : returns Standard_True, if target has parent in source; list contains inheritance chain
270//=======================================================================
271static Standard_Boolean GetShapeEvolutions(const TopoDS_Shape& theTarget, // this is changed in recursion
272 const Handle(TNaming_NamedShape)& theSource,
273 TopTools_ListOfShape& aList) // returns list in the backward order
274{
275 Handle(TNaming_NamedShape) aTarget = TNaming_Tool::NamedShape(theTarget,theSource->Label());
276 if (!aTarget.IsNull()) {
0797d9d3 277#ifdef OCCT_DEBUG_71
7fd59977 278 cout <<"GetShapeEvolutions: target NS = ";
279 Print_Entry(aTarget->Label());
280 cout <<"GetShapeEvolutions: Source NS = ";
281 Print_Entry(theSource->Label());
282 TCollection_AsciiString aNam("GetShapeEvolutions");
283 WriteNSOnLabel(aTarget,aNam);
284#endif
285 if (aTarget->Label() == theSource->Label()) return Standard_True; // check if target is in the source
286 } else return Standard_False;
287
288 TNaming_Iterator anIter(aTarget);
289 for(;anIter.More();anIter.Next()) { // check all appropriate old shapes of target
0797d9d3 290#ifdef OCCT_DEBUG_71
7fd59977 291 if(!anIter.OldShape().IsNull()) {
292 Write(anIter.OldShape(), "Target_OldS.brep");
293 cout <<"Target OldS TS =" <<anIter.OldShape().TShape()->This() <<endl;
294 }
295 if(!anIter.NewShape().IsNull()) {
296 Write(anIter.NewShape(), "Target_NewS.brep");
297 cout <<"Target NewS TS =" <<anIter.NewShape().TShape()->This() <<endl;
298 }
299#endif
300 if (anIter.OldShape().IsNull() || anIter.NewShape().IsNull()) continue;
301 if (!anIter.NewShape().IsSame(theTarget)) continue;
302 if (GetShapeEvolutions(anIter.OldShape(),theSource,aList)) { // recursion: now target is old shape
303 aList.Append(theTarget); // if oldshape has the source as parent (or belongs to it) , fill the list
304 return Standard_True;
305 }
306 }
307 return Standard_False;
308}
309
310//=======================================================================
311//function : CompareInModification
312//purpose : returns empty named shape if naming is already done
313//=======================================================================
314
315static Handle(TNaming_NamedShape) CompareInModification (const Handle(TNaming_NamedShape)& NS, // parent
316 const TopoDS_Shape& S) // target
317{
318 Handle(TNaming_NamedShape) aResult;
319 if (S.IsNull() || NS.IsNull()) return aResult;
0797d9d3 320#ifdef OCCT_DEBUG_71
7fd59977 321 cout <<"CompareInModification: parent NS = ";
322 Print_Entry(NS->Label());
323 Write(S, "CompareInM_S.brep");
324 TCollection_AsciiString aNam("CompareInM");
325 WriteNSOnLabel(NS,aNam);
326#endif
327 Handle(TNaming_NamedShape) aSource; // parent NamedShape, which can be found by TopoDS shape
328 TNaming_Iterator anIt(NS);
329 for(;anIt.More() && aSource.IsNull();anIt.Next()) {
330 if (!anIt.NewShape().IsNull()) {
331 aSource = TNaming_Tool::NamedShape(anIt.NewShape(),NS->Label());
0797d9d3 332#ifdef OCCT_DEBUG_71
7fd59977 333 TCollection_AsciiString aNam("CompareInM_Source");
334 WriteNSOnLabel(aSource,aNam);
335#endif
336 }
337 }
338 // searching for 1:n to the same label modifications (in this case current naming is insufficient)
339 TopTools_ListOfShape aList;
340 if (GetShapeEvolutions(S,aSource,aList) && aList.Extent() > 0) {
341 TopTools_ListIteratorOfListOfShape anIter(aList);
342 for(;anIter.More();anIter.Next()) {
343 aResult = TNaming_Tool::NamedShape(anIter.Value(),NS->Label());
344 if (aResult->Evolution()!=TNaming_MODIFY) { // evolution must be modify, otherwise everything is OK
345 aResult.Nullify();
346 return aResult;
347 }
348 TopTools_MapOfShape aMap; // collection of the old shapes of the shape from list
349 TNaming_Iterator aNIter1(aResult);
350
351 for(;aNIter1.More();aNIter1.Next()) {
352 if (aNIter1.NewShape().IsSame(anIter.Value())) {// if isSame
353 aMap.Add(aNIter1.OldShape());
354 }
355 }
356 TNaming_Iterator aNIter2(aResult); // if some another shapes has oldshape from map, return namedshape with this oldshape
357
358 for(;aNIter2.More();aNIter2.Next()) {
359 if (aNIter2.NewShape().IsSame(anIter.Value())) continue;
360 if (aMap.Contains(aNIter2.OldShape())) { // if one shape was modified to the two at the shared label, return this one
361 aResult = TNaming_Tool::NamedShape(aNIter2.OldShape(),NS->Label());
362 if (!aResult.IsNull()) return aResult;
363 }
364 }
365 }
366 aResult.Nullify();
367 }
368 return aResult;
369}
370#endif
371
372//=======================================================================
373static Standard_Boolean FillSMap(const TopoDS_Shape& S, TopTools_MapOfShape& MS)
374{
375 if(S.IsNull()) return Standard_False;
376 Standard_Boolean isHomogen(Standard_True);
377 TopAbs_ShapeEnum aPrevType(TopAbs_SHAPE);
378 TopoDS_Iterator it(S);
379 for (; it.More(); it.Next()) {
380 const TopAbs_ShapeEnum aType = it.Value().ShapeType();
0797d9d3 381#ifdef OCCT_DEBUG_CC
7fd59977 382 cout <<"TestSolution_FillMap: S_Type = :" << it.Value().ShapeType() <<" TShape = " << it.Value().TShape()->This() <<endl;
383#endif
384 if(aType > TopAbs_COMPSOLID) {
385 MS.Add(it.Value());
386 if(aPrevType == TopAbs_SHAPE)
387 aPrevType = aType;
388 else if(aPrevType != aType)
389 isHomogen = Standard_False;
390 }
391 else
392 if(!FillSMap(it.Value(), MS))
393 isHomogen = Standard_False;
394 }
395 return isHomogen;
396}
397//=======================================================================
398//function : Compare
399//purpose : checks naming of the shape <S> in the NamedShape <NS>.
400// : Returns true, if it's correct. Details ==>
401// : The method takes all modifications of the "NS" (see CurrentShape method),
402// : which are in the "MDF" (if it's not empty) before <Stop> shape and check them.
403// : whether these modifications contain only "S". If yes then the method
404// : returns true, otherwise it returns false.
405//=======================================================================
406
407static Standard_Boolean Compare (const Handle(TNaming_NamedShape)& NS,
408 const TNaming_Scope& MDF,
409 const Handle(TNaming_NamedShape)& Stop,
410 const TopoDS_Shape& S)
411{
412 TDF_LabelMap Forbiden;
413 TopTools_MapOfShape MS;
414 if (!Stop.IsNull()) TNaming_NamingTool::BuildDescendants(Stop,Forbiden);
415 TNaming_NamingTool::CurrentShape(MDF.GetValid(),Forbiden,NS,MS);
0797d9d3 416#ifdef OCCT_DEBUG_NBS
7fd59977 417 Write(S, "Compare_S.brep");
418 cout << "S: TShape = " <<S.TShape()->This() <<endl;
419 Standard_Integer i =1;
420 TopTools_MapIteratorOfMapOfShape it(MS);
421 TCollection_AsciiString aNam("Compare_MS_");
422 TCollection_AsciiString ext(".brep");
423 for(;it.More();it.Next(), i++) {
424 TCollection_AsciiString aName = aNam + i + ext;
425 Write(it.Key(), aName.ToCString()) ;
426 cout << aName.ToCString()<< ": TShape = " <<it.Key().TShape()->This() <<endl;
427 }
428#endif
429 return (MS.Contains(S) && MS.Extent() == 1);
430}
431//=======================================================================
432//function : TestSolution
433//purpose : returns true, if last modification of shape from "NS" is equal to "S":
434// : If shape "S" has atomic type (TopAbs_FACE, TopAbs_EDGE, TopAbs_VERTEX),
435// : then returns S.IsSame(shape from "NS").
436// : Otherwise the result of exploration of these shapes must be same.
437//=======================================================================
438
439static Standard_Boolean TestSolution(const TNaming_Scope& MDF,
440 const Handle(TNaming_NamedShape)& NS,
441 const TopoDS_Shape& S)
442{
443
444 if (NS.IsNull()) return Standard_False;
445 TopoDS_Shape Res = MDF.CurrentShape(NS);// last modification of NS taken into account Valid map
446 if(S.IsNull() || Res.IsNull()) return Standard_False;
0797d9d3 447#ifdef OCCT_DEBUG_CC
7fd59977 448 Write(S, "TSol_S.brep");
449 Write(Res, "TSol_Res.brep");
450#endif
451#ifdef OCC352
452
453 if ((S.ShapeType() == TopAbs_FACE ||
454 S.ShapeType() == TopAbs_EDGE ||
455 S.ShapeType() == TopAbs_VERTEX ) &&
456 Res.ShapeType() != TopAbs_COMPOUND) {
457 return (Res.IsSame(S));
458 } else if (S.ShapeType() == TopAbs_SOLID ||
459 S.ShapeType() == TopAbs_COMPSOLID) {
460 TopTools_MapOfShape aMS;
461 TopExp_Explorer exp;
462 for (exp.Init(S,TopAbs_FACE) ; exp.More(); exp.Next()) {
463 aMS.Add(exp.Current());
464 }
465 for (exp.Init(Res,TopAbs_FACE) ; exp.More(); exp.Next()) { //content of MS and Res should be the same
466 if (aMS.Contains(exp.Current())) {
467 aMS.Remove(exp.Current());
468 }
469 else return 0;
470 }
471 return aMS.IsEmpty();
472 } else {
473
474#else
475
476 if (S.ShapeType() == TopAbs_SOLID ||
477 S.ShapeType() == TopAbs_FACE ||
478 S.ShapeType() == TopAbs_EDGE ||
479 S.ShapeType() == TopAbs_VERTEX ) {
480 return (Res.IsSame(S));
481 }
482 else {
483
484#endif
485
486 TopTools_MapOfShape MS;
487 Standard_Boolean isHom = FillSMap(S, MS);
488 TopAbs_ShapeEnum aType(TopAbs_SHAPE);
489 TColStd_MapOfInteger aView;
490 TopTools_MapIteratorOfMapOfShape itm(MS);
491 for(;itm.More();itm.Next()) {
492 aType = itm.Key().ShapeType();
493 if(isHom)
494 break;
495 else
496 aView.Add(itm.Key().ShapeType());
497 }
498
499 if (MS.Contains(Res)) {
500 MS.Remove(Res);
501 if (MS.IsEmpty()) return 1;
502 }
503 if (Res.ShapeType() == TopAbs_SOLID ||
504 Res.ShapeType() == TopAbs_COMPSOLID ||
505 Res.ShapeType() == TopAbs_COMPOUND)
506 {
507 TopExp_Explorer exp;
508 if(isHom)
509 for (exp.Init(Res,aType) ; exp.More(); exp.Next()) {
510 if (MS.Contains(exp.Current())) {
511 MS.Remove(exp.Current());
512 }
513 } else {
514 TColStd_MapIteratorOfMapOfInteger itm(aView);
515 for(;itm.More();itm.Next()) {
516 TopAbs_ShapeEnum aType = (TopAbs_ShapeEnum)itm.Key();
517 for (exp.Init(Res,aType) ; exp.More(); exp.Next()) {
518 if (MS.Contains(exp.Current())) {
519 MS.Remove(exp.Current());
520 }
521 }
522 }
523 }
524 } else {
525 if(S.IsSame(Res))
526 return Standard_True;
527 TopoDS_Iterator it(Res);
528 for (; it.More(); it.Next()) { //content of MS and Res should be the same
529 if (MS.Contains(it.Value())) {
530 MS.Remove(it.Value());
531 }
532 else return 0;
533 }
534 }
535 return MS.IsEmpty();
536 }
537}
538
539//=======================================================================
540//function : FindNewShapeInFather
541//purpose :
542//=======================================================================
543
544static void FindNewShapeInFather (const Handle(TNaming_NamedShape)& NS,
545 TopoDS_Shape& SC)
546{
b2197f93 547 const TDF_Label& Father = NS->Label().Father();
7fd59977 548 TNaming_Iterator itLab(Father);
b2197f93 549 if(itLab.More())
550 SC = itLab.NewShape();
7fd59977 551}
552
553//=======================================================================
554//function : NextModif
555//purpose :
556//=======================================================================
557
558static Handle(TNaming_NamedShape) NextModif(const Handle(TNaming_NamedShape)& NS)
559{
560 Handle (TNaming_NamedShape) Next;
561 if (!NS.IsNull()) {
562 TNaming_NewShapeIterator it(NS);
563 if (it.More()&& it.IsModification()) Next = it.NamedShape();
564 }
565 return Next;
566}
567//=======================================================================
568// C1 - cand shape of the father, C2 - shape of rebuild child Naming attr.
569// (to be Compound of elementary shapes)
570//=======================================================================
571static Standard_Boolean IsContSame(const TopoDS_Shape& C1, const TopoDS_Shape& C2)
572{
573 Standard_Boolean aRes(Standard_False);
574 if(!C1.IsNull() && !C2.IsNull()) {
575 TopTools_MapOfShape aMap;
576 if(FillSMap(C1, aMap)) {
577 aRes = Standard_True;
578 TopoDS_Iterator it(C2);
579 for(;it.More();it.Next()) {
580 if(!aMap.Contains(it.Value())) {
581 aRes = Standard_False;
582 break;
583 }
584 }
585 }
586 }
587 return aRes;
588}
589
590//=======================================================================
591// Identifies the case when Filter haven't sense because of multiplicity
592//=======================================================================
593static Standard_Boolean IsMultipleCase(const TopoDS_Shape& S,
594 const TopoDS_Shape& Context,
595 const TopTools_MapOfShape& Neighbourgs) {
596
597 TopTools_IndexedDataMapOfShapeListOfShape aDM;
598 TNaming_MapOfShape aM;
599 TopTools_MapOfShape aNbs;
600 aNbs.Assign(Neighbourgs);
601 aNbs.Add(S);
602 TNaming_DataMapOfShapeMapOfShape aDMM;
603 TopExp::MapShapesAndAncestors (Context, TopAbs_EDGE, TopAbs_FACE, aDM);
604 TopTools_MapIteratorOfMapOfShape it(aNbs);
605 for(;it.More();it.Next()) {
606 if(aDM.Contains(it.Key())) {
607 TNaming_MapOfShape aMS;
608 const TopTools_ListOfShape& aL = aDM.FindFromKey(it.Key());
609 TopTools_ListIteratorOfListOfShape lit(aL);
610 for(;lit.More();lit.Next()) {
611 aM.Add(lit.Value());
612 aMS.Add(lit.Value());
613 }
614 if(aMS.Extent())
615 aDMM.Bind(it.Key(), aMS);
616 } else {
0797d9d3 617#ifdef OCCT_DEBUG
7fd59977 618 cout << "Key is not BOUND!" <<endl;
619#endif
620 return Standard_False;
621 }
622 }
623
624 Standard_Boolean isCommon(Standard_True);
625 TNaming_MapIteratorOfMapOfShape itm(aM);
626 for(;itm.More();itm.Next()) {
627 isCommon = Standard_True; // statement: this shape (itm.Key()) is common (to be checked below)
628 TNaming_DataMapIteratorOfDataMapOfShapeMapOfShape itdm(aDMM);
629 for (;itdm.More();itdm.Next()) {
7fd59977 630 const TNaming_MapOfShape& aMap = itdm.Value();
631 if(!aMap.Contains(itm.Key())) {
632 isCommon = Standard_False;
633 break;
634 }
635 }
636 if(isCommon) break; // common single face found
637 }
638 if(isCommon && aM.Extent() < aNbs.Extent()) {// number of unique faces (to have single solution)
639 //should be at least no less than (Nb of Neighbourgs) +1
640 return Standard_True;
641 }
642 return Standard_False;
643}
644//=======================================================================
645//function : Filter
646//purpose : sets the name with type "FILTERBYNEIGHBOURGS" and returns true,
647// : if it was correctly done.
648// :
649// :
650//=======================================================================
651
652static Standard_Boolean Filter (const TDF_Label& F,
653 TNaming_Scope& MDF,
654 const TopoDS_Shape& S,
655 const TopoDS_Shape& Context,
656 TNaming_Localizer& Localizer,
657 Handle(TNaming_NamedShape)& NS,
658 const Standard_Integer Lev)
659{
660 // 1. Localizer.FindNeighbourg("Context", "S", "Neighbourg") - looks for neighbourgs of "S" with the same shape type in the "Context"
661 // shape and adds them to the "Neighbourg" map;
662 // 2. If "Neighbourg" map is empty, tries to do the same with the new context shape: shape from the father label of the "S" named shape;
663 // 3. If "Neighbourg" map is still empty, returns false;
664 // 4. Adds to the new child of label "L" new TNaming_Naming attribute with the next properties name: shape type is shape type of the "S",
665 // type is TNaming_FILTERBYNEIGHBOURGS, stop shape ( "Until" ) is "Context" of corresponding NamedShape;
666 // first argument of name is "NS" NamedShape.
667 // 5. Adds to the Name all shapes from "Neighbourg" map: build it with the BuildName( new generated TNaming_Naming attribute, MDF,
668 // argument shape from "Neighbourg", "Context", NextModif( "Until" ), true ) method.
669 // 6. Creates resulting NamedShape with the "Regenerate" ( it just calls TName::Solve method ) method from TNaming_Naming class.
670 // 7. If Compare( result NamedShape, MDF, stop, "S" ) returns true, "Filter" method returns true, else returns false.
671
672 //------------------------------
673 // Construction des voisins ==> of neighbors.
674 //------------------------------
675 Standard_Integer aLev(Lev);
676 TopTools_MapOfShape Neighbourg;
677 Localizer.FindNeighbourg (Context,S,Neighbourg);
0797d9d3 678#ifdef OCCT_DEBUG_NBS
7fd59977 679 //DbgTools::DisplayShape(Context, F, Quantity_NOC_GREEN);
680 //DbgTools::DisplayShape(S, F, Quantity_NOC_BLUE1);
681 Write(Context, "FNBS_Context.brep");
682 Write(S, "FNBS_S.brep");
efd4b232 683 Write(Neighbourg, "NBS");
7fd59977 684#endif
685#ifdef OCC273
686 // mpv : NS and shape must be the same
687 Standard_Boolean isIn = Standard_False;
688 TNaming_Iterator anIter(NS);
689 for(;anIter.More();anIter.Next()) {
0797d9d3 690#ifdef OCCT_DEBUG
7fd59977 691 //DbgTools::DisplayShape(anIter.NewShape(), F, Quantity_NOC_RED);
692#endif
693 if (anIter.NewShape().IsSame(S)) {
694 isIn = Standard_True;
695 break;
696 }
697 }
698 if (!isIn) if (!TNaming_Tool::NamedShape(S,F).IsNull()) NS = TNaming_Tool::NamedShape(S,F);
699// if (!TNaming_Tool::NamedShape(S,F).IsNull()) NS = TNaming_Tool::NamedShape(S,F);
700#endif
701
702 if (Neighbourg.IsEmpty()) {
703 // Recherche du vrai context. (Research of context truth)
704 Handle(TNaming_NamedShape) GenS = TNaming_Tool::NamedShape(S,NS->Label());
705#ifdef BUC60847
706 if (GenS.IsNull()) return Standard_False;
707#endif
708 TDF_Label Father = (GenS->Label()).Father();
709 Father.FindAttribute(TNaming_NamedShape::GetID(),GenS);
710 TopoDS_Shape GoodContext = TNaming_Tool::GetShape(GenS);
711 Localizer.FindNeighbourg (GoodContext,S,Neighbourg);
712 }
713
714
715 if (Neighbourg.IsEmpty()) {
0797d9d3 716#ifdef OCCT_DEBUG
7fd59977 717 cout <<"FindNeighbourg: impossible"<<endl;
718#endif
719 return 0;
720 } else {
0797d9d3 721#ifdef OCCT_DEBUG_NBS
7fd59977 722 Write(Neighbourg, "Neighbourgs");
723#endif
724 aLev++;
725 //cout <<"Filter: Lev = " << aLev << endl;
726 }
727 if(aLev > 3) return 0;
728#ifdef ALLOW_CHILD_NBS
729 Handle(TNaming_Naming) aFNaming;
730 TopoDS_Shape aFatherCandSh;
731 F.FindAttribute(TNaming_Naming::GetID(), aFNaming);
732 if(!aFNaming.IsNull()) {
733 const TNaming_Name& aName = aFNaming->GetName();
734 if (aName.Type() == TNaming_FILTERBYNEIGHBOURGS) {
735 aFatherCandSh = aName.Arguments().First()->Get();
736 }
737 }
738 if(S.ShapeType() == TopAbs_EDGE && aFatherCandSh.IsNull()) {
739 //check the applicability
740 if(!NS.IsNull() && !NS->Get().IsNull() && NS->Get().ShapeType() == TopAbs_COMPOUND)
741 if(IsMultipleCase(S, Context, Neighbourg)) {
742 //cout << "Filter: ==> MultipleCase!" << endl;
743 NS->Label().FindAttribute(TNaming_Naming::GetID(), aFNaming);
744 if(!aFNaming.IsNull()) {
745 TNaming_Name& aName = aFNaming->ChangeName();
746 if (aName.Type() == TNaming_INTERSECTION) {
747 Standard_Integer ij(1);
748 TNaming_ListIteratorOfListOfNamedShape itA(aName.Arguments());
749 for (; itA.More(); itA.Next(), ij++) {
750 const TopoDS_Shape& aFace = TNaming_Tool::CurrentShape(itA.Value());
0797d9d3 751#ifdef OCCT_DEBUG_MOD
7fd59977 752 Write(aFace, "First_Face.brep");
753 cout <<"Selection TS = " << S.TShape()->This() <<endl;
754#endif
755 Standard_Integer i(1), indxW(0),indxE(0),nbW(0),nbE(0), indxF(0);
756 Standard_Boolean isFound(Standard_False);
757 TopoDS_Iterator it(aFace);
758 for (;it.More();it.Next(),i++) {
759 nbW++;
0797d9d3 760#ifdef OCCT_DEBUG_MOD
7fd59977 761 Write(it.Value(), "First_Wire.brep");
762#endif
763 if(!isFound) {
764 Standard_Integer j(1);
765 TopoDS_Iterator it2(it.Value());
766 for (;it2.More();it2.Next(),j++) {
767 nbE++;
0797d9d3 768#ifdef OCCT_DEBUG_MOD
7fd59977 769 Write(it2.Value(), "First_Wire.brep");
770 cout <<"Edge TS = " << it2.Value().TShape()->This() <<endl;
771#endif
772 if(S.IsEqual(it2.Value())) {
773 indxE = j;
774 indxW = i;
775 indxF = ij;
776 isFound = Standard_True;
777 }
778 }
779 }
780 }
781 if(isFound) {
782 Standard_Integer Index = indxE & 0x000000FF;
783 Index = Index | (nbE << 8);
784 Index = Index | (indxW << 16);
785 Index = Index | (nbW << 20);
786 Index = Index | (indxF << 24);
787 aName.Index(Index);
788 //------------------------------
789 // Compute the TNaming_NamedShape
790 //------------------------------
791 aFNaming->Regenerate(MDF.ChangeValid());
792 aFNaming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
793 Handle (TNaming_NamedShape) Until = TNaming_Tool::NamedShape(Context,NS->Label());
794 Handle (TNaming_NamedShape) Stop = NextModif(Until);
795 if (Compare (NS,MDF,Stop,S)) return 1;
796 break;
797 }
798 }
799 }
800 }
801 return 0;
802 }
803 }
804#endif
805
806 //-----------------------------------------------------
807 // Construction function de naming. et insertion sous F
808 //-----------------------------------------------------
809 Handle (TNaming_Naming) NF = TNaming_Naming::Insert (F);
810
811 Handle (TNaming_NamedShape) Until = TNaming_Tool::NamedShape(Context,NS->Label());
812 Handle (TNaming_NamedShape) Stop = NextModif(Until);
813 TNaming_Name& theName = NF->ChangeName();
814 theName.ShapeType(S.ShapeType());
815 theName.Shape(S);
7dcac1df 816 theName.Orientation(S.Orientation());
7fd59977 817 theName.Type(TNaming_FILTERBYNEIGHBOURGS);
818 theName.Append(NS);
819 theName.StopNamedShape (Until);
0797d9d3 820#ifdef OCCT_DEBUG_NBS
7fd59977 821 cout << "FilterByNBS: ";
822 Print_Entry(NF->Label());
efd4b232 823 cout <<"AppendNS = " ;
824 Print_Entry(NS->Label());
7fd59977 825#endif
826 //---------------------
827 // Naming des voisins.
828 //---------------------
829
830 TopTools_MapIteratorOfMapOfShape itN(Neighbourg);
831 for (; itN.More(); itN.Next()) {
832#ifdef ALLOW_CHILD_NBS
833 const TopoDS_Shape& aS = itN.Key();
834 Handle (TNaming_NamedShape) aNS =
835 BuildName(NF->Label(), MDF, aS, Context, Stop, 1);
0797d9d3 836#ifdef OCCT_DEBUG_NBS
7fd59977 837 const TopoDS_Shape& aS2 = aNS->Get();
838 if(!aS.IsNull())
839 cout << "Shape arg type = " << aS.ShapeType() <<" TSH = " << aS.TShape()->This()<<endl;
efd4b232 840 if(!aS2.IsNull()) {
7fd59977 841 cout << "Build shape type = " << aS2.ShapeType() <<" TSH = " << aS2.TShape()->This()<<endl;
efd4b232 842 Write (aS2, "NBS_BuildShape.brep");
843 }
844 if(aNS.IsNull()) {
845 cout <<"AppendNS = " ;
846 Print_Entry(aNS->Label());
847 }
7fd59977 848#endif
849
850 const TopoDS_Shape aSNS = aNS->Get(); //allow child level
851 Standard_Boolean allowChild(Standard_True);
852 if(!aSNS.IsNull() && aSNS.ShapeType() == TopAbs_COMPOUND && !aFatherCandSh.IsNull())
853 allowChild = !IsContSame(aFatherCandSh, aSNS);
854 if(allowChild && !aSNS.IsNull() && aS.ShapeType() != aSNS.ShapeType() &&
855 aSNS.ShapeType() == TopAbs_COMPOUND)
856 { // aLev < 3
0797d9d3 857#ifdef OCCT_DEBUG_NBS
7fd59977 858 cout <<"Father label = ";
859 Print_Entry(aNS->Label().Father());
860 Write(aS,"SelectionS.brep");
861 Write(aSNS,"SelectionSNS.brep");
862#endif
863 Handle(TNaming_Naming) aNaming;
864 Standard_Boolean StandardFilter(Standard_True);
865 aNS->FindAttribute(TNaming_Naming::GetID(), aNaming);
866 if(!aNaming.IsNull()) {
867 const TNaming_Name& aName = aNaming->GetName();
868 if (aName.Type() == TNaming_GENERATION)
869 StandardFilter = Standard_False;
870 if(StandardFilter)
871 if (!Compare (aNS,MDF,Stop,aS)) {
872 TNaming_Localizer aLocalizer;
873 Filter (NF->Label(), MDF,aS,Context, aLocalizer,aNS, aLev);
874 }
875 }
876 }
877 theName.Append(aNS);
878#else
879 theName.Append(BuildName(NF->Label(), MDF, itN.Key(), Context, Stop, 1));
880#endif
881 }
882 //------------------------------
883 // Compute the TNaming_NamedShape
884 //------------------------------
885 NF->Regenerate(MDF.ChangeValid());
886 NF->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
887
888 //-----------------
889 // Check du filtre.
890 //-----------------
891 if (Compare (NS,MDF,Stop,S)) return 1;
0797d9d3 892#ifdef OCCT_DEBUG
7fd59977 893 cout <<"TNaming_Naming::Name Filter insufficient"<<endl;
894#endif
895 return 0;
896}
897
898
899//=======================================================================
900//function : BuildNameInNS
901//purpose : Calls BuildName method, but with new context and new stop shape.
902// : Context is searched at the father label of the "Context" label :
903// : old shapes from the NamedShape at the defined father label.
904// : If it's impossible, then "S" set as context.
905// : If "S" is in new context, then stop shape is named shape,
906// : which belongs to the father label of the "Context" named shape.
907// : For example, face (F2) of prism (P) is generated from the edge (E)
908// : of primitive face (F1) of box (B). F2 is named as GENERATION from E.
909// : Naming of E is done with help BuildNameInNS function:
910// : with context B and stop shape P.
911//=======================================================================
912static Handle(TNaming_NamedShape) BuildNameInNS (const TDF_Label& F,
913 TNaming_Scope& MDF,
914 const TopoDS_Shape& S,
915 const Handle(TNaming_NamedShape)& Context,
916 const Handle(TNaming_NamedShape)& Stop,
917 const Standard_Boolean Geometry)
918
919{
920 // il faut determiner un nouveau context et un nouveau Stop.
1ec8a59e 921 // it is necessary to determine a new context and a new Stop
7fd59977 922 TopoDS_Shape SC;
923 Handle(TNaming_NamedShape) NewStop = Stop;
924
925 TNaming_Localizer::FindShapeContext (Context,S,SC);
926
927 if(!SC.IsNull()){
928// <Context> is Ident.NamedShapeOfGeneration() ==
929 TDF_Label Father = Context->Label().Father();
930 Father.FindAttribute(TNaming_NamedShape::GetID(),NewStop);
0797d9d3 931#ifdef OCCT_DEBUG_INNS
7fd59977 932 if(!Stop.IsNull())
933 {cout <<" Stop NS : "; Print_Entry( Stop->Label());}
934 if(!NewStop.IsNull())
935 {cout <<" NewStop : "; Print_Entry( NewStop->Label());}
936 cout <<"ContextLabel: "; Print_Entry( Context->Label());
937 cout <<"Father : "; Print_Entry( Father);
938#endif
939 }
0797d9d3 940#ifdef OCCT_DEBUG_INNS
7fd59977 941 if(NewStop.IsNull())
942 cout <<"BuildNameInNS:: NewStop shape is NULL" << endl;
943#endif
944 return BuildName (F,MDF,S,SC,NewStop,Geometry);
945}
946
947//=======================================================================
948//function :
949//purpose :
950//=======================================================================
951
952static Handle(TNaming_NamedShape) BuildName (const TDF_Label& F,
953 TNaming_Scope& MDF,
954 const TopoDS_Shape& Selection,
955 const TopoDS_Shape& Context,
956 const Handle(TNaming_NamedShape)& Stop,
957 const Standard_Boolean Geom)
958{
959
960
961
962 // Create an identifier
963 Standard_Boolean OnlyOne = !Geom;
964 Standard_Boolean IsGeneration = Standard_False;
0797d9d3 965#ifdef OCCT_DEBUG_MOD
7fd59977 966 cout <<"BuildName: F => ";
967 Print_Entry(F);
968 cout <<" Selection type = " << Selection.ShapeType() << " TS = " << Selection.TShape()->This() << endl;
969 Write(Selection, "BName_Selection.brep");
970 Write(Context, "BName_Context.brep");
971#endif
972 TNaming_Identifier Ident(F, Selection, Context,OnlyOne);
973
974 Handle (TNaming_Naming) Naming;
975 Handle (TNaming_NamedShape) NS;
976
977 if (!Ident.IsDone()) {
978 return BuildNS (F,Selection, TNaming_UNKNOWN);
979 }
980 if (Ident.IsFeature() && Stop.IsNull()) {
981 //-------------
982 // Deja Nomme
983 //-------------
984 if (!OnlyOne) return Ident.FeatureArg();
985 else NS = Ident.FeatureArg();
986 }
987 else {
988 //---------------------------------------------
989 // Construction de la fonction d identification.
990 //---------------------------------------------
991 //Standard_Boolean NotOnlyOne = 0;
992
993 Naming = TNaming_Naming::Insert(F);
994
995 TNaming_Name& theName = Naming->ChangeName();
996 theName.ShapeType(Selection.ShapeType());
997 theName.Shape(Selection);
7dcac1df 998 theName.Orientation(Selection.Orientation());
7fd59977 999 theName.Type(Ident.Type());
0797d9d3 1000#ifdef OCCT_DEBUG_MOD
7fd59977 1001 cout <<"BuildName: Inserted Naming Att at ";
1002 Print_Entry(Naming->Label());
1003 cout <<" NameType = " << theName.Type() <<endl;
1004#endif
1005 if (Ident.IsFeature()) {
1006 theName.Append(Ident.FeatureArg());
1007 }
1008 if (theName.Type() == TNaming_GENERATION) {
1009 theName.Append(Ident.NamedShapeOfGeneration());
1010 IsGeneration = Standard_True;
1011 }
1012 if (theName.Type() == TNaming_CONSTSHAPE) {
1013 theName.Index(FindIndex(Ident.FeatureArg(),Selection));
1014 }
1015 //------------------------------------
1016 // Renseignement du NamedShape d arret.
1017 //------------------------------------
1018 theName.StopNamedShape (Stop);
0797d9d3 1019#ifdef OCCT_DEBUG_MOD
7fd59977 1020 if(!Stop.IsNull()) {
1021 TCollection_AsciiString Es;
1022 TDF_Tool::Entry(Stop->Label(), Es);
1023 cout <<"StopNS at Label = "<< Es << endl;
1024 }
1025#endif
1026 //---------------------------------
1027 // Identification des arguments.
1028 //---------------------------------
1029
1030 for (Ident.InitArgs(); Ident.MoreArgs(); Ident.NextArg()) {
1031 if (Ident.ArgIsFeature()) {
1032 theName.Append(Ident.FeatureArg());
0797d9d3 1033#ifdef OCCT_DEBUG_MOD
7fd59977 1034 if(!Ident.FeatureArg().IsNull()) {
1035 TCollection_AsciiString E;
1036 TDF_Tool::Entry(Ident.FeatureArg()->Label(), E);
1037 cout <<"Added argument NS from Label = "<< E << endl;
1038 }
1039#endif
1040 }
1041 else {
0797d9d3 1042#ifdef OCCT_DEBUG_MOD
7fd59977 1043 cout <<"BuildName: NameType = " <<theName.Type() << " NS ";
1044 Print_Entry(Naming->Label());
1045 cout <<"Ident.ShapeArg() type = " << Ident.ShapeArg().ShapeType() << " TS = " << Ident.ShapeArg().TShape()->This() << endl;
1046 Write(Ident.ShapeArg(), "BName_ShapeArg.brep");
1047#endif
1048 if (theName.Type() == TNaming_GENERATION)
1049 theName.Append(BuildNameInNS(Naming->Label(),MDF,Ident.ShapeArg(),Ident.NamedShapeOfGeneration(),Stop,Geom));
1050 else
1051 theName.Append(BuildName(Naming->Label(),MDF,Ident.ShapeArg(),Context,Stop,Geom));
1052 }
1053 }
1054
1055 //------------------------
1056 // Reconstruction of Name
1057 //------------------------
1058 Naming->Regenerate(MDF.ChangeValid());
0797d9d3 1059#ifdef OCCT_DEBUG_MOD
7fd59977 1060 TCollection_AsciiString E2;
1061 TDF_Tool::Entry(Naming->Label(), E2);
1062 cout <<"Regenerated Naming Att at Label = "<< E2 << endl;
1063#endif
1064 Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1065 if(NS.IsNull()) return NS;
1066 if (MDF.WithValid()) MDF.Valid(NS->Label());
0797d9d3 1067#ifdef OCCT_DEBUG_MOD
7fd59977 1068 if(!NS.IsNull()) {
1069 TCollection_AsciiString E;
1070 TDF_Tool::Entry(NS->Label(), E);
1071 cout <<"Regenerated NS at Label = "<< E << endl;
1072 }
1073#endif
1074 }
1075
1076 if (OnlyOne) {
1077 //-------------------------------------------------
1078 // Filtre par les voisins
1079 // pour construire le nom correspondant a S.
1080 //-------------------------------------------------
1081
1082#ifdef OCC273
1083 if(NS.IsNull()) return NS;
1084#endif
1085
1086 TNaming_Localizer Localizer;
1087 TNaming_Iterator itNS(NS);
1088 if (itNS.More()) {
1089 //----------------
1090 // Check + Filtre
1091 //----------------
1092
1093#ifdef OCC350
1094
1095 Standard_Boolean StandardFilter = !IsGeneration;
1096 if (IsGeneration) {
1097 if (!CompareInGeneration (NS,Selection)) {
1098
1099 TopoDS_Shape NewContext;
1100 Handle(TNaming_NamedShape) NewStop;
1101 FindNewShapeInFather (Ident.NamedShapeOfGeneration(),NewContext);
1102 Filter (F,MDF,Selection,NewContext,Localizer,NS,0);
1103 }
1104 } else if (Ident.Type() == TNaming_MODIFUNTIL ||
1105 (Ident.Type() == TNaming_INTERSECTION && Naming->ChangeName().Arguments().Extent() == 1)) {
0797d9d3 1106#ifdef OCCT_DEBUG_MOD
7fd59977 1107 cout <<"BuildName(CompareInModification): NameType = " <<Ident.Type() << " NS ";
1108 Print_Entry(Ident.Type() == TNaming_MODIFUNTIL ? NS->Label() : Naming->ChangeName().Arguments().First()->Label());
1109 cout <<"Selection type = " << Selection.ShapeType() << " TS = " << Selection.TShape()->This() << endl;
1110#endif
1111 Handle(TNaming_NamedShape) NewNS =
1112 CompareInModification(Ident.Type() == TNaming_MODIFUNTIL ? NS : Naming->ChangeName().Arguments().First(), Selection);
1113 if (!NewNS.IsNull()) { // there is need to describe name in detail: modification with type 1:n in the same label
1114 StandardFilter = Standard_False;
1115 if (Ident.IsFeature()) { // for MODIFUNTIL: change it to the GENERATION
1116 Naming = TNaming_Naming::Insert(F);
1117 TNaming_Name& theName = Naming->ChangeName();
1118 theName.ShapeType(Selection.ShapeType());
7dcac1df 1119 theName.Shape(Selection);
1120 theName.Orientation(Selection.Orientation());
7fd59977 1121 theName.Type(TNaming_GENERATION);
1122 theName.Append(TNaming_Tool::NamedShape(Selection,F));
1123 theName.Append(NewNS);
1124 Naming->Regenerate(MDF.ChangeValid());
1125 Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1126
1127 }
1128 Filter (F,MDF,Selection,Context,Localizer,NS,0);
1129 }
1130 }
1131 if (StandardFilter) if (!Compare (NS,MDF,Stop,Selection)) {
1132 Filter (F,MDF,Selection,Context,Localizer,NS,0);
1133 }
1134
1135#else
1136
1137 if (IsGeneration) {
1138 if (!CompareInGeneration (NS,Selection)) {
1139 TopoDS_Shape NewContext;
1140 Handle(TNaming_NamedShape) NewStop;
1141 FindNewShapeInFather (Ident.NamedShapeOfGeneration(),NewContext);
1142 Filter (F,MDF,Selection,NewContext,Localizer,NS,0);
1143 }
1144 }
1145 else {
1146 if (!Compare (NS,MDF,Stop,Selection))
1147 Filter (F,MDF,Selection,Context,Localizer,NS,0);
1148 }
1149
1150#endif
1151
1152 }
1153 }
1154 if (MDF.WithValid()) MDF.Valid(NS->Label());
0797d9d3 1155#ifdef OCCT_DEBUG_MOD
7fd59977 1156 if(!NS.IsNull()) {
1157 TCollection_AsciiString E;
1158 TDF_Tool::Entry(NS->Label(), E);
1159 cout <<"Returned NS from Label = "<< E << endl;
1160 }
1161#endif
1162 return NS;
1163}
1164
1165//=======================================================================
1166//function : Validate
1167//purpose :
1168//=======================================================================
1169
1170static void Validate(TNaming_Scope& MDF,
1171 TNaming_OldShapeIterator& it)
1172{
1173 MDF.Valid(it.Label());
1174 MDF.ValidChildren(it.Label());
1175
1176 TNaming_OldShapeIterator it2(it);
1177 for (; it2.More(); it2.Next()) {
1178 Validate (MDF,it2);
1179 }
1180}
1181
1182//=======================================================================
1183//function : UnValidate
1184//purpose :
1185//=======================================================================
1186
1187static void UnValidate(TNaming_Scope& MDF,
1188 TNaming_NewShapeIterator& it)
1189{
1190 MDF.Unvalid(it.Label());
1191 MDF.UnvalidChildren(it.Label());
1192
1193 TNaming_NewShapeIterator it2(it);
1194 for (; it2.More(); it2.Next()) {
1195 UnValidate (MDF,it2);
1196 }
1197}
1198
1199//=======================================================================
1200//function : BuildScope
1201//purpose : adds to the MDF the label of <Context> NamedShape,
1202// : its children, all its oldShapes and its children.
1203// : unvalidates all newShapes and it's children.
1204// : If <Context> is null or next modification has an empty newShape
1205// : ( this shape was deleted ), then MDF.WithValid(Standard_False)
1206// : and nothing is added to the scope.
1207//=======================================================================
1208
1209static void BuildScope (TNaming_Scope& MDF,
1210 const TopoDS_Shape& Context,
1211 const TDF_Label& Acces)
1212{
1213 if (Context.IsNull()) {
1214 MDF.WithValid(Standard_False);
1215 return;
1216 }
1217
1218 //----------------------------------------------------
1219 // Is context the current state
1220 //----------------------------------------------------
1221 Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape(Context,Acces);
1222 Handle(TNaming_NamedShape) Next = NextModif(NS); // if NS has subsequent evolution = MODIFY, return it
1223 if (Next.IsNull()) {
1224 MDF.WithValid(Standard_False);
1225 return;
1226 }
1227 //-----------------------------
1228 // a posteriori naming
1229 //-----------------------------
1230 MDF.WithValid(Standard_True);
1231 MDF.Valid(NS->Label());
1232 MDF.ValidChildren(NS->Label());
1233 TNaming_OldShapeIterator it(Context,Acces);
1234
1235 for (; it.More(); it.Next()) {
1236 Validate(MDF,it);
1237 }
1238
1239 TNaming_NewShapeIterator it2(Context,Acces);
1240 for (;it2.More(); it2.Next()) {
1241 UnValidate (MDF,it2);
1242 }
1243}
1244
1ec8a59e 1245//=======================================================================
1246//function : HasAncFace
1247//purpose : Returns True & <Face> if ancestor face is found
7fd59977 1248//=======================================================================
1249static Standard_Boolean HasAncFace(const TopoDS_Shape& Context,
1ec8a59e 1250 const TopoDS_Shape& W, TopoDS_Shape& Face, Standard_Boolean& isOuter)
7fd59977 1251{
1252 Standard_Boolean hasFace(Standard_False);
1253 if(W.ShapeType() != TopAbs_WIRE)
1254 return hasFace;
1255 TopExp_Explorer exp(Context, TopAbs_FACE);
1256 for(;exp.More(); exp.Next()) {
1257 for (TopoDS_Iterator it(exp.Current()) ; it.More(); it.Next()) {
1258 if(it.Value().IsEqual(W)) {// is the Wire ?
1ec8a59e 1259 Face = exp.Current();
1260 if(!Face.IsNull()) {
1261 hasFace = Standard_True;
1262 // cout << "HasAncFace: TS = " <<theFace.TShape()->This() <<endl;
1263 const TopoDS_Face aFace(TopoDS::Face(Face));
1264 TopoDS_Wire anOuterW;
1265 if(TNaming::OuterWire(aFace, anOuterW)) {
1266 if(!anOuterW.IsNull() && anOuterW.IsEqual(W))
1267 isOuter = Standard_True;
1268 else
1269 isOuter = Standard_False;
1270 }
1271 break;
1272 }
1273 }
1274 }
7fd59977 1275 if(hasFace) break;
1276 }
1277 return hasFace;
1278}
1279
1280//=======================================================================
1281//function : BuildNameWire
1282//purpose : Names Wire
1283//=======================================================================
1284
1285static Handle(TNaming_NamedShape) BuildNameWire (const TDF_Label& F,
1286 TNaming_Scope& MDF,
1287 const TopoDS_Shape& Selection,
1288 const TopoDS_Shape& Context,
1289 const Handle(TNaming_NamedShape)& Stop,
1290 const Standard_Boolean Geom)
1291{
1292 Handle (TNaming_NamedShape) NS;
1293 Standard_Boolean found(Standard_False);
1294 Handle (TNaming_Naming) Naming;
1295 if(!F.FindAttribute(TNaming_Naming::GetID(), Naming)) {
1296 Naming = new TNaming_Naming ();
1297 F.AddAttribute (Naming);
1298 TNaming_Name& theName = Naming->ChangeName();
1299 theName.ShapeType(Selection.ShapeType());
7dcac1df 1300 theName.Shape(Selection);
1301 theName.Orientation(Selection.Orientation());
7fd59977 1302 }
1303
1304 TNaming_Name& theName = Naming->ChangeName();
1305 TopoDS_Shape aFace;
1ec8a59e 1306 Standard_Boolean isOuter(Standard_False);
1307 Standard_Boolean hasFace = HasAncFace(Context, Selection, aFace, isOuter);
1308 if(hasFace && Selection.ShapeType() > Context.ShapeType()) {
7fd59977 1309 theName.Type(TNaming_WIREIN);
1ec8a59e 1310 if(Context.ShapeType() == TopAbs_FACE) {
1311 for (TopoDS_Iterator it(Context) ; it.More(); it.Next()) {
1312 if(it.Value().IsEqual(Selection)) {
1313 if (TNaming_Selector::IsIdentified (F, Context, NS, Geom)) {
1314 theName.Append(NS);
1315 found = Standard_True;
1316 break;
1317 }
1318 }
7fd59977 1319 }
1ec8a59e 1320 if(found) {
1321 theName.Append(BuildName (Naming->Label(),MDF,aFace,Context,Stop,Geom));
1322 if(isOuter) {
1323 theName.Index(1);
1324 } else {
1325 theName.Index(-1);
1326 for (TopExp_Explorer exp(Selection,TopAbs_EDGE) ; exp.More(); exp.Next()) {
1327 if(exp.Current().IsNull()) continue;
1328 if (BRep_Tool::Degenerated(TopoDS::Edge(exp.Current()))) continue;
1329 theName.Append(TNaming_Naming::Name(Naming->Label(),exp.Current(),Context, Geom, 1, 0));
1330 }
1331 }
1332 } else
1333 return BuildNS (F,Selection, TNaming_UNKNOWN);
1334
1335 } else { // context is not Face
1336 theName.Append(BuildName (Naming->Label(),MDF,aFace,Context,Stop,Geom));
1337 if(isOuter) {
1338 theName.Index(1);
1339 } else {
1340 for (TopExp_Explorer exp(Selection,TopAbs_EDGE) ; exp.More(); exp.Next()) {
1341 if(exp.Current().IsNull()) continue;
1342 if (BRep_Tool::Degenerated(TopoDS::Edge(exp.Current()))) continue;
1343 theName.Append(TNaming_Naming::Name(Naming->Label(),exp.Current(),Context, Geom, 1, 0));
1344 }
1345 }
1346 }//
1347 }
1348 else { // => no Face
7fd59977 1349 theName.Type(TNaming_UNION);
1350 for (TopExp_Explorer exp(Selection,TopAbs_EDGE) ; exp.More(); exp.Next()) {
1ec8a59e 1351 if(exp.Current().IsNull()) continue;
1352 if (BRep_Tool::Degenerated(TopoDS::Edge(exp.Current()))) continue;
7fd59977 1353 theName.Append(BuildName (Naming->Label(),MDF,exp.Current(),Context,Stop,Geom));
1354 }
1355 }
1356 //Naming->GetName().Solve(Naming->Label(),MDF.GetValid());
1357 Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1358 return NS;
1359}
1360
1361//=======================================================================
1362static Standard_Boolean IsOneIn (const TopoDS_Shape& S, const TopoDS_Shape& Context)
1363{
1364 Standard_Boolean found(Standard_False);
1365 if(S.IsNull() || Context.IsNull()) return found;
1366 for (TopExp_Explorer exp(Context,S.ShapeType()); exp.More(); exp.Next()) {
1367 if (exp.Current().IsEqual(S)) {
1368 found = Standard_True;
1369 break;
1370 }
1371 }
1372 return found;
1373}
1374
1375//=======================================================================
1376static Standard_Boolean IsAllIn (const TopoDS_Shape& S, const TopoDS_Shape& Context)
1377{
0797d9d3 1378#ifdef OCCT_DEBUG_CC
7fd59977 1379 Write(S, "IsAllIn_Sel.brep");
1380#endif
1381 Standard_Boolean found(Standard_False);
1382 if(S.IsNull() || Context.IsNull()) return found;
1383 Standard_Integer num1(0), num2(0);
1384 for(TopoDS_Iterator it(S);it.More();it.Next(),num1++) {
0797d9d3 1385#ifdef OCCT_DEBUG_CC
7fd59977 1386 cout <<"S sub-shape type = " << it.Value().ShapeType() <<endl;
1387 Write (it.Value(), "Sel_ItValue.brep");
1388#endif
1389 if(it.Value().ShapeType() != TopAbs_COMPOUND)
1390 for (TopExp_Explorer exp(Context,it.Value().ShapeType()); exp.More(); exp.Next()) {
0797d9d3 1391#ifdef OCCT_DEBUG_CC
7fd59977 1392 cout <<"Context sub-shape type = " << exp.Current().ShapeType() <<endl;
1393 Write(exp.Current(), "Contex_Curnt.brep");
1394#endif
1395 if (exp.Current().IsEqual(it.Value())) {
1396 num2++;
1397 break;
1398 }
1399 } else {
1400 Standard_Boolean isAll = IsAllIn(it.Value(), Context);
1401 if(isAll)
1402 num2++;
1403 }
1404 }
1405 if(num1 == num2)
1406 found = Standard_True;
0797d9d3 1407#ifdef OCCT_DEBUG_CC
7fd59977 1408 else
1409 cout <<"Compound case : selected num1 = " << num1 << " context contains num2 = " << num2 << endl;
1410#endif
1411 return found;
1412}
1413//=======================================================================
1414//function : RepeatabilityInContext
1415//purpose :
1416//=======================================================================
1417static Standard_Integer RepeatabilityInContext(const TopoDS_Shape& Selection,
1418 const TopoDS_Shape& Context)
1419{
1420 Standard_Integer aNum(0);
1421 if (!Context.IsNull() && !Selection.IsNull()) {
1422// Write(Selection, "Repeat_Selection.brep");
1423// Write(Context, "Repeat_Context.brep");
1424 if (Context.ShapeType() < Selection.ShapeType()) {
1ec8a59e 1425 if(Selection.ShapeType() != TopAbs_SHELL) {
1426 for (TopExp_Explorer exp(Context,Selection.ShapeType()); exp.More(); exp.Next()) {
1427 if (exp.Current().IsSame(Selection))
1428 aNum++;
1429 }
1430 }
1431 }
7fd59977 1432 else if(Selection.ShapeType() == TopAbs_COMPOUND) {
1433 TopoDS_Iterator it(Selection);
1434 for(;it.More();it.Next()) {
1435 Standard_Integer n(0);
1436 for (TopExp_Explorer exp(Context,it.Value().ShapeType()); exp.More(); exp.Next()) {
1437 if (exp.Current().IsSame(it.Value())) {
1438 n++;
1439 }
1440 }
1441 if(n > aNum) aNum = n;
1442 }
1443 }
1444 }
0797d9d3 1445#ifdef OCCT_DEBUG_OR
7fd59977 1446 cout <<"RepeatabilityInContext: = " <<aNum <<endl;
1447#endif
1448 return aNum;
1449}
1ec8a59e 1450
1451//=======================================================================
1452//function : HasAncSolid
1453//purpose : Returns true if Sh has ancestor solid in this context
1454//=======================================================================
1455static Standard_Boolean HasAncSolid(const TopoDS_Shape& Context,
1456 const TopoDS_Shape& Sh, TopoDS_Shape& Solid,
1457 Standard_Boolean& isOuter)
1458{
1459 Standard_Boolean hasSolid(Standard_False);
1460 if(Sh.ShapeType() != TopAbs_SHELL)
1461 return hasSolid;
1462 TopExp_Explorer exp(Context, TopAbs_SOLID);
1463 for(;exp.More(); exp.Next()) {
1464 for (TopoDS_Iterator it(exp.Current()) ; it.More(); it.Next()) {
1465 if(it.Value().IsEqual(Sh)) {// is the Solid ?
1466 Solid = exp.Current();
1467 if(!Solid.IsNull()) {
1468 hasSolid = Standard_True;
1469 TopoDS_Shell anOuterShell;
1470 if(TNaming::OuterShell(TopoDS::Solid(Solid), anOuterShell)) {
0797d9d3 1471#ifdef OCCT_DEBUG_TSOL
1ec8a59e 1472 Write(anOuterShell, "OuterShell.brep");
1473#endif
1474 if(!anOuterShell.IsNull() && anOuterShell.IsEqual(Sh))
1475 isOuter = Standard_True;
1476 else
1477 isOuter = Standard_False;
1478 }
1479 break;
1480 }
1481 }
1482 }
1483 if(hasSolid) break;
1484 }
1485 return hasSolid;
1486}
1487//=======================================================================
1488//function : BuildNameShell
1489//purpose : Names Shell
1490//=======================================================================
1491
1492static Handle(TNaming_NamedShape) BuildNameShell (const TDF_Label& F,
1493 TNaming_Scope& MDF,
1494 const TopoDS_Shape& Selection,
1495 const TopoDS_Shape& Context,
1496 const Handle(TNaming_NamedShape)& Stop,
1497 const Standard_Boolean Geom)
1498{
1499 Handle (TNaming_NamedShape) NS;
1500 Standard_Boolean found(Standard_False);
1501 Handle (TNaming_Naming) Naming;
1502 if(!F.FindAttribute(TNaming_Naming::GetID(), Naming)) {
1503 Naming = new TNaming_Naming ();
1504 F.AddAttribute (Naming);
1505 TNaming_Name& theName = Naming->ChangeName();
1506 theName.ShapeType(Selection.ShapeType());
7dcac1df 1507 theName.Shape(Selection);
1508 theName.Orientation(Selection.Orientation());
1ec8a59e 1509 }
1510
1511 TNaming_Name& theName = Naming->ChangeName();
1512 TopoDS_Shape aSolid;
1513 Standard_Boolean isOuter(Standard_False);
1514 Standard_Boolean hasSolid = HasAncSolid(Context, Selection, aSolid, isOuter);
1515 if(hasSolid && Selection.ShapeType() > Context.ShapeType()) {
1516 theName.Type(TNaming_SHELLIN);// SHELLIN
1517
1518 if(Context.ShapeType() == TopAbs_SOLID) {
1519 for (TopoDS_Iterator it(Context) ; it.More(); it.Next()) {
0797d9d3 1520#ifdef OCCT_DEBUG_TSOL
1ec8a59e 1521 Write(it.Value(), "Shell_inSo.brep");
1522#endif
1523 if(it.Value().IsEqual(Selection)) {
1524 found = Standard_True;
1525 break;
1526 }
1527 }
1528 if(found) {
1529 // solid => aSolid which is also a context
1530 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(Context,F);
1531 if(!aNS.IsNull())
1532 theName.ContextLabel(aNS->Label());
1533 theName.Append(aNS);
1534 if(isOuter) {
1535 theName.Index(1);
1536 } else { //not OuterShell
1537 theName.Index(-1);
1538 for (TopExp_Explorer exp(Selection,TopAbs_FACE) ; exp.More(); exp.Next()) {
1539 if(exp.Current().IsNull()) continue;
1540 theName.Append(BuildName (Naming->Label(),MDF,exp.Current(),Context,Stop,Geom));
1541 }
1542 }
1543 } else
1544 return BuildNS (F,Selection, TNaming_UNKNOWN);
1545 } else {
1546 // context is not SOLID
1547 //theName.Append(BuildName (Naming->Label(),MDF,aSolid,Context,Stop,Geom));//###########
1548 if(isOuter) {
0797d9d3 1549#ifdef OCCT_DEBUG_TSOL
1ec8a59e 1550 Write(aSolid, "foundSolid.brep");
1551#endif
1552 theName.Index(1);
1553 Handle (TNaming_Naming) NamingSo = TNaming_Naming::Insert(F);
1554 TNaming_Name& theNameSo = NamingSo->ChangeName();
1555 theNameSo.ShapeType(aSolid.ShapeType());
1556 theNameSo.Shape(aSolid);
1557 theNameSo.Type(TNaming_UNION);
1558 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(Context,F);
1559 if(!aNS.IsNull())
1560 theNameSo.ContextLabel(aNS->Label());
1561 for (TopExp_Explorer exp(aSolid,TopAbs_FACE) ; exp.More(); exp.Next())
1562 theNameSo.Append(BuildName (NamingSo->Label(),MDF,exp.Current(),Context,Stop,Geom));
1563 NamingSo->GetName().Solve(NamingSo->Label(),MDF.GetValid());
1564 aNS.Nullify();
1565 NamingSo->Label().FindAttribute(TNaming_NamedShape::GetID(),aNS);
1566 theName.Append(aNS);
1567 } else {
1568 theName.Index(-1);
1569 // - name Solid: theName.Append(BuildName (Naming->Label(),MDF, aSolid,Context,Stop,Geom));
1570 Handle (TNaming_Naming) NamingSo = TNaming_Naming::Insert(F);
1571 TNaming_Name& theNameSo = NamingSo->ChangeName();
1572 theNameSo.ShapeType(aSolid.ShapeType());
1573 theNameSo.Shape(aSolid);
1574 theNameSo.Type(TNaming_UNION);
1575 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(Context,F);
1576 if(!aNS.IsNull())
1577 theNameSo.ContextLabel(aNS->Label());
1578 for (TopExp_Explorer exp(aSolid,TopAbs_FACE) ; exp.More(); exp.Next())
1579 theNameSo.Append(BuildName (NamingSo->Label(),MDF,exp.Current(),Context,Stop,Geom));
1580 NamingSo->GetName().Solve(NamingSo->Label(),MDF.GetValid());
1581 aNS.Nullify();
1582 NamingSo->Label().FindAttribute(TNaming_NamedShape::GetID(),aNS);
1583 theName.Append(aNS);
1584
1585 for (TopExp_Explorer exp(Selection,TopAbs_FACE) ; exp.More(); exp.Next()) {
1586 if(exp.Current().IsNull()) continue;
1587 theName.Append(BuildName (Naming->Label(),MDF,exp.Current(),Context,Stop,Geom));
1588 }
1589 }
1590 }//
1591 }
1592 else { // => no Solid
1593 theName.Type(TNaming_UNION);
1594 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(Context,F);
1595 if(!aNS.IsNull())
1596 theName.ContextLabel(aNS->Label());
1597 for (TopExp_Explorer exp(Selection,TopAbs_FACE) ; exp.More(); exp.Next()) {
1598 if(exp.Current().IsNull()) continue;
1599 theName.Append(BuildName (Naming->Label(),MDF,exp.Current(),Context,Stop,Geom));
1600 }
1601 }
1602 //Naming->GetName().Solve(Naming->Label(),MDF.GetValid());
1603 Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1604 return NS;
1605}
1606
7fd59977 1607//=======================================================================
1608//function : BuildAggregationNam
1609//purpose :
1610//=======================================================================
1611static void BuildAggregationName (const TDF_Label& F,
1612 TNaming_Scope& MDF,
1613 const TopoDS_Shape& S,
1614 const TopoDS_Shape& Context,
1615 const Handle(TNaming_NamedShape)& Stop,
1616 const Standard_Boolean Geom)
1617{
1618 const Standard_Boolean found2 = IsAllIn(S, Context);
1619 Handle (TNaming_Naming) Naming;
1620 if(!F.FindAttribute(TNaming_Naming::GetID(), Naming)) {
1621 Naming = new TNaming_Naming ();
1622 F.AddAttribute (Naming);
1623 TNaming_Name& theName = Naming->ChangeName();
1624 theName.ShapeType(S.ShapeType());
1625 theName.Shape(S);
7dcac1df 1626 theName.Orientation(S.Orientation());
7fd59977 1627 }
0797d9d3 1628#ifdef OCCT_DEBUG_CC
7fd59977 1629 cout <<"BuildAggregationName ==> ";
1630 Print_Entry(Naming->Label());
1631#endif
1632 TNaming_Name& theName = Naming->ChangeName();
1633 for (TopoDS_Iterator itc(S) ; itc.More(); itc.Next()) {
1634 const TopoDS_Shape& aS = itc.Value();
1635 if ((aS.ShapeType() == TopAbs_SOLID && !TNaming_Tool::NamedShape(aS,Naming->Label()).IsNull()) ||
1636 aS.ShapeType() == TopAbs_FACE ||
1637 aS.ShapeType() == TopAbs_EDGE ||
1638 aS.ShapeType() == TopAbs_VERTEX ) {
1639 theName.Append(BuildName (F, MDF, aS,Context,Stop,Geom));
1640 } else { // ==> union of union || union of wires
1641 TopAbs_ShapeEnum atomTyp;
1642 switch (aS.ShapeType())
1643 {
1644 case TopAbs_SOLID:
1645 case TopAbs_SHELL:
1646 atomTyp = TopAbs_FACE;
1647 break;
1648 case TopAbs_WIRE:
1649 atomTyp = TopAbs_EDGE;
1650 break;
1651 default:
1652 atomTyp = TopAbs_SHAPE;
1653 }
1654
1655 Handle(TNaming_NamedShape) aNS;
1656 Handle (TNaming_Naming) aNaming = TNaming_Naming::Insert(F);
1657 TNaming_Name& aName = aNaming->ChangeName();
1658 aName.ShapeType(aS.ShapeType());
7dcac1df 1659 aName.Shape(aS);
1660 theName.Orientation(aS.Orientation());
7fd59977 1661 aName.Type(TNaming_UNION);
1662
1663 if (atomTyp != TopAbs_SHAPE) {
1664 if(aS.ShapeType() == TopAbs_WIRE) {
1665 aNS = BuildNameWire (aNaming->Label(), MDF, aS, Context,Stop,Geom);
1666 }
1ec8a59e 1667 else if(aS.ShapeType() == TopAbs_SHELL)
1668 aNS = BuildNameShell (aNaming->Label(), MDF, aS, Context,Stop,Geom);
7fd59977 1669 else {
1670 for (TopExp_Explorer exp(aS,atomTyp) ; exp.More(); exp.Next()) {
1671 aName.Append(BuildName (aNaming->Label(),MDF,exp.Current(),Context,Stop,Geom));
1672 }
1673 }
1674 } else {
0797d9d3 1675#ifdef OCCT_DEBUG_CC
7fd59977 1676 cout << "atomic type is NOT defined ... ==> Aggregation" <<endl;
1677#endif
1678 BuildAggregationName(aNaming->Label(),MDF, aS, Context,Stop,Geom);
1679 }
1680 if(found2) {
1681 aNS = TNaming_Tool::NamedShape(Context, F);
1682 if(!aNS.IsNull())
1683 aNaming->ChangeName().ContextLabel(aNS->Label());
1684 }
1685
1686 aNaming->GetName().Solve(aNaming->Label(),MDF.GetValid());
1687 if(aNaming->Label().FindAttribute(TNaming_NamedShape::GetID(),aNS))
1688 if (!Geom && TestSolution(MDF,aNS,aS)) {
1689 theName.Append(aNS);
1690 }
1691 }
1692 }
1693}
1694
1695
1696//=======================================================================
1697//function : Name
1698//purpose :
1699//=======================================================================
1700
1701Handle(TNaming_NamedShape) TNaming_Naming::Name (const TDF_Label& F,
1702 const TopoDS_Shape& S,
1703 const TopoDS_Shape& Context,
1704 const Standard_Boolean Geom,
1705 const Standard_Boolean KeepOrientation,
1706 const Standard_Boolean BNProblem)
1707
1708{
1709 Handle(TNaming_NamedShape) NS;
1710 if (KeepOrientation) {
0797d9d3 1711#ifdef OCCT_DEBUG_INNS
7fd59977 1712 cout <<"KeepOR = 1: "; Print_Entry(F);
1713#endif
1714 Standard_Integer aNum = RepeatabilityInContext(S, Context);
1715
1716 Standard_Boolean aBNproblem = (BNProblem) ? (aNum /*== 1*/ && S != Context) : Standard_False;
1717
1718 if (aNum > 1 || aBNproblem) {
1719 TopoDS_Shape UC = TNaming::FindUniqueContext(S, Context);
1720 Handle(TopTools_HArray1OfShape) Arr;
1721 if (UC.IsNull() && S.ShapeType() == TopAbs_COMPOUND) {
1722 UC = TNaming::FindUniqueContextSet(S, Context, Arr);
0797d9d3 1723#ifdef OCCT_DEBUG_CC
7fd59977 1724 Write(UC, "UniqueContextSet.brep");
1725 Write(S, "InitialSelection.brep");
1726 if(S.ShapeType()==TopAbs_COMPOUND) {
1727 TCollection_AsciiString aNam("S_");
1728 TopoDS_Iterator it(S);
1729 for(int i=1;it.More();it.Next(),i++) {
1730 TCollection_AsciiString aName = aNam + i + ".brep";
1731 Write(it.Value(), aName.ToCString());
1732 }
1733 }
1734#endif
1735 }
1736 if(!UC.IsNull()) {
1737 Handle (TNaming_Naming) Naming = TNaming_Naming::Insert(F);
1738 TNaming_Name& theName = Naming->ChangeName();
1739 theName.ShapeType(S.ShapeType());
1740 theName.Shape(S);
1741 theName.Type(TNaming_ORIENTATION);
7dcac1df 1742 theName.Orientation(S.Orientation());
7fd59977 1743
1744 if (!TNaming_Selector::IsIdentified (F, S, NS, Geom))
1745 NS = TNaming_Naming::Name(Naming->Label(),S,Context,Geom,0);
1746 theName.Append (NS);
1747#ifdef MDTV_OR
1748 cout << " Sel Label ==> "; Print_Entry(NS->Label());
1749#endif
1750//szy 21.10.2009
1751 if(S.ShapeType() == TopAbs_EDGE && UC.ShapeType() == TopAbs_FACE) {
1752 if(RepeatabilityInContext(S, UC) == 2) { //sim. edge
1753 TopoDS_Iterator itw(UC);
1754 for(;itw.More();itw.Next()) {
1755 Standard_Boolean found(Standard_False);
1756 TopoDS_Iterator it(itw.Value());
1757 for(int i=1;it.More();it.Next(),i++) {
1758 if(it.Value().IsEqual(S)) {
1ec8a59e 1759 theName.Index(i);//We use this field to save a Seam Shape Index; Before this field was used for GENERATED only
7fd59977 1760 found = Standard_True;
1761#ifdef MDTV_OR
1762 cout << "ORDER = " << i <<endl;
1763#endif
1764 break;
1765 }
1766 }
1767 if(found) break;
1768 }
1769 }
1770 }
1771//
1772 if(S.ShapeType() == TopAbs_COMPOUND && Arr->Length() > 1) {
1773 // N arguments: to be optimized to avoid duplication of the same Context shape
1774 for(Standard_Integer i = Arr->Lower(); i <= Arr->Upper(); i++) {
1775 NS = TNaming_Naming::Name(Naming->Label(), Arr->Value(i), Context, Geom, 1, aBNproblem);
1776 theName.Append (NS);
1777 }
1778 } else {
1779 NS = TNaming_Naming::Name(Naming->Label(),UC,Context, Geom, 1, aBNproblem);
1780 theName.Append (NS);
1781#ifdef MDTV_OR
1782 cout << " Cont Label ==> "; Print_Entry(NS->Label());
1783#endif
1784 }
1785 //Naming->Update();
1786 TNaming_Scope MDF;
1787 BuildScope (MDF,Context,F);
1788 Naming->GetName().Solve(Naming->Label(),MDF.GetValid());
1789 Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1790 theName.ContextLabel(NS->Label());
1791 if (Geom) return NS;
1792 if(NS.IsNull()) {
1793 cout <<" %%% WARNING: TNaming_Naming::Name: FAILED"<<endl;
1794 return BuildNS (F,S, TNaming_UNKNOWN);
1795 }
1796
1797 if (!Geom && TestSolution(MDF,NS,S)) return NS;
1798 cout <<" %%% WARNING: TNaming_Naming::Name: FAILED"<<endl;
1799
1800 // Naming n is unsatisfactory
1801 return BuildNS (F,S, TNaming_UNKNOWN);
1802 }
1803 } else
1804 if (TNaming_Selector::IsIdentified (F, S, NS, Geom))
1805 return NS;
1806 }
1807
1808 //------------------------------------------------------------
1809 // Construction du MDF tel que <Context> soit le dernier etat
1810 // valide,
1811 // Ceci pour les localisation a posteriori par exemple.
1812 //------------------------------------------------------------
1813
1814 TNaming_Scope MDF;
1815 BuildScope (MDF,Context,F);
1816 Handle(TNaming_NamedShape) Stop;
1817
1818#ifdef OCC352
1819
1820 if ((S.ShapeType() == TopAbs_SOLID && !TNaming_Tool::NamedShape(S,F).IsNull()) ||
1821
1822#else
1823
1824 if (S.ShapeType() == TopAbs_SOLID ||
1825
1826#endif
1827
1828 S.ShapeType() == TopAbs_FACE ||
1829 S.ShapeType() == TopAbs_EDGE ||
1830 S.ShapeType() == TopAbs_VERTEX ) {
1831 //---------------------------------------
1832 // Localisation de S comme element simple.
1833 //---------------------------------------
1834 Handle(TNaming_NamedShape) NS = BuildName (F,MDF,S,Context,Stop,Geom);
1835 if (Geom) return NS;
1836 if (!Geom && TestSolution(MDF,NS,S)) return NS;
1837 }
1838 else {
1839 //----------------------------------------------------
1840 // Localisation de S comme ensemble d elements simples.
1841 //-----------------------------------------------------
1842 Handle(TNaming_NamedShape) NS;
1843 Handle (TNaming_Naming) Naming = TNaming_Naming::Insert(F);
1844 TNaming_Name& theName = Naming->ChangeName();
1845
1846 theName.ShapeType(S.ShapeType());// modified by vro 05.09.00
1847 theName.Shape(S);
7dcac1df 1848 theName.Orientation(S.Orientation());
7fd59977 1849 if(S.ShapeType() != TopAbs_WIRE)
1850 theName.Type(TNaming_UNION);
1851
1852#ifdef OCC352
1853 TopAbs_ShapeEnum atomType;
1854 switch (S.ShapeType()) {
1855 case TopAbs_COMPSOLID:
1856 case TopAbs_SOLID:
1857 case TopAbs_SHELL:
1858 atomType = TopAbs_FACE;
1859 break;
1860 case TopAbs_WIRE:
1861 atomType = TopAbs_EDGE;
1862 break;
1863 default:
1864 atomType = TopAbs_SHAPE;
1865 }
1866 Standard_Boolean found(Standard_False);
1867 if (!Context.IsNull()) {
1868 if (Context.ShapeType() < S.ShapeType())
1869 found = IsOneIn(S, Context);
1870 if(found) {
1871 NS = TNaming_Tool::NamedShape(Context, F);
1872 if(!NS.IsNull())
1873 theName.ContextLabel(NS->Label());
1874 }
1875 }
1876 if (atomType == TopAbs_SHAPE) {
1877 if(S.ShapeType() == TopAbs_COMPOUND) {
1878 BuildAggregationName(Naming->Label(),MDF, S, Context,Stop,Geom);
1879 } else {
1880 for (TopoDS_Iterator it(S) ; it.More(); it.Next()) {
1881 theName.Append(BuildName (Naming->Label(),MDF,it.Value(),Context,Stop,Geom));
1882 }
1883 }
1884 } else {
1885 if(S.ShapeType() == TopAbs_WIRE)
1ec8a59e 1886 NS = BuildNameWire (Naming->Label(), MDF, S, Context,Stop,Geom);
1887 else if(S.ShapeType() == TopAbs_SHELL) {
1888 NS = BuildNameShell (Naming->Label(), MDF, S, Context,Stop,Geom);
1889 }
1890 else {
1891 theName.Type(TNaming_UNION);
1892 for (TopExp_Explorer exp(S,atomType) ; exp.More(); exp.Next()) {
1893 theName.Append(BuildName (Naming->Label(),MDF,exp.Current(),Context,Stop,Geom));
1894 }
1895 }
7fd59977 1896 }
1897#else
1898 for (TopoDS_Iterator it(S) ; it.More(); it.Next()) {
1899 theName.Append(BuildName (Naming->Label(),MDF,it.Value(),Context,Stop,Geom));
1900 }
1901#endif
1902
1903 //Naming->Update();
1904 Naming->GetName().Solve(Naming->Label(),MDF.GetValid());
1905 Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1906 if (Geom) return NS;
1907
1908#ifdef OCC273
1909 if(NS.IsNull()) return BuildNS (F,S, TNaming_UNKNOWN);
1910#endif
1911
1912 if (!Geom && TestSolution(MDF,NS,S)) return NS;
1913 }
1914
1915 cout <<" %%% WARNING: TNaming_Naming::Name: FAILED"<<endl;
1916
1917 // Naming n is not satisfactory
1918 return BuildNS (F,S, TNaming_UNKNOWN);
1919}
1920
1921
1922//=======================================================================
1923//function : TNaming_Naming
1924//purpose :
1925//=======================================================================
1926
1927TNaming_Naming::TNaming_Naming() {}
1928
1929//=======================================================================
1930//function : ID
1931//purpose :
1932//=======================================================================
1933
1934const Standard_GUID& TNaming_Naming::ID () const
1935{
1936 return GetID();
1937}
1938
1939
1940//=======================================================================
1941//function : IsDefined
1942//purpose :
1943//=======================================================================
1944
1945Standard_Boolean TNaming_Naming::IsDefined() const
1946{
1947 return (myName.Type() != TNaming_UNKNOWN);
1948}
1949
1950//=======================================================================
1951//function : GetName
1952//purpose :
1953//=======================================================================
1954
1955const TNaming_Name& TNaming_Naming::GetName() const
1956{
1957 return myName;
1958}
1959
1960//=======================================================================
1961//function : ChangeName
1962//purpose :
1963//=======================================================================
1964
1965TNaming_Name& TNaming_Naming::ChangeName()
1966{
1967 return myName;
1968}
1969
1970//=======================================================================
1971//function : Regenerate
1972//purpose : idem designer
1973//=======================================================================
1974
1975Standard_Boolean TNaming_Naming::Regenerate (TDF_LabelMap& MDF)
1976
1977{
1978 return myName.Solve(Label(),MDF);
1979}
1980
1981
1982//=======================================================================
1983//function : NewEmpty
1984//purpose :
1985//=======================================================================
1986
1987Handle(TDF_Attribute) TNaming_Naming::NewEmpty () const
1988{
1989 return new TNaming_Naming ();
1990}
1991
1992
1993//=======================================================================
1994//function : Restore
1995//purpose :
1996//=======================================================================
1997
1998void TNaming_Naming::Restore(const Handle(TDF_Attribute)& other)
1999{
2000 Handle(TNaming_Naming) OtherNaming = Handle(TNaming_Naming)::DownCast(other);
2001 myName = OtherNaming->ChangeName();
2002}
2003
2004//=======================================================================
2005//function : Paste
2006//purpose :
2007//=======================================================================
2008
2009void TNaming_Naming::Paste (const Handle(TDF_Attribute)& into,
2010 const Handle(TDF_RelocationTable)& RT) const
2011{
2012 Handle(TNaming_Naming) NewNaming = Handle(TNaming_Naming)::DownCast(into);
2013 myName.Paste(NewNaming->ChangeName(),RT);
2014}
2015
2016//=======================================================================
2017//function : References
2018//purpose : Redefined from TDF_Attribute
2019//=======================================================================
2020
2021void TNaming_Naming::References(const Handle(TDF_DataSet)& DataSet) const
2022{
2023 // Iteration on NamedShape of the name
2024 TNaming_ListIteratorOfListOfNamedShape it(myName.Arguments());
2025 for (;it.More();it.Next()) DataSet->AddAttribute(it.Value());
2026 if (!myName.StopNamedShape().IsNull()) DataSet->AddAttribute(myName.StopNamedShape());
2027}
2028//=======================================================================
2029//function : Dump
2030//purpose :
2031//=======================================================================
2032
2033Standard_OStream& TNaming_Naming::Dump (Standard_OStream& anOS) const
2034{
2035 anOS << "TNaming_Naming";
2036 return anOS;
2037}
2038
2039
2040//=======================================================================
2041//function :ExtendedDump
2042//purpose :
2043//=======================================================================
2044
2045void TNaming_Naming::ExtendedDump(Standard_OStream& anOS,
2046 const TDF_IDFilter& /*aFilter*/,
2047 TDF_AttributeIndexedMap& /*aMap*/) const
2048{
2049 anOS << "TNaming_Naming ExtendedDump ";
2050 //anOS<<"myContext: #" <<aMap.Add(myContext)<<endl;
2051}
2052