0025418: Debug output to be limited to OCC development environment
[occt.git] / src / TNaming / TNaming_Name.cxx
1 // Created on: 1997-03-21
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <TNaming_Name.ixx>
18 #include <TNaming.hxx>
19 #include <TNaming_NamingTool.hxx>
20 #include <TNaming_Naming.hxx>
21 #include <TNaming_ShapesSet.hxx>
22 #include <TNaming_Builder.hxx>
23 #include <TNaming_NamedShape.hxx>
24 #include <TNaming_Iterator.hxx>
25 #include <TNaming_NewShapeIterator.hxx>
26 #include <TNaming_ListOfNamedShape.hxx>
27 #include <TNaming_ListIteratorOfListOfNamedShape.hxx>
28 #include <TopExp.hxx>
29 #include <TopExp_Explorer.hxx>
30 #include <TopoDS_Compound.hxx>
31 #include <BRep_Builder.hxx>
32 #include <BRepTools.hxx>
33
34 #include <TopTools_MapOfShape.hxx>
35 #include <TopTools_MapIteratorOfMapOfShape.hxx>
36 #include <TDF_Label.hxx>
37 #include <TDF_LabelList.hxx>
38 #include <TDF_LabelMap.hxx>
39 #include <TNaming_Tool.hxx>
40
41 #include <Standard_NotImplemented.hxx>
42 #include <Standard_ConstructionError.hxx>
43 #include <TopTools_Array1OfShape.hxx>
44 #include <TColStd_Array1OfInteger.hxx>
45 #include <TopoDS_Wire.hxx>
46 #include <TopoDS_Shell.hxx>
47
48 // mpv modifications 08.04.2002
49 #include <BRepBuilderAPI_MakeWire.hxx>
50 #include <BRepBuilderAPI_MakeFace.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopoDS_Solid.hxx>
54 #include <TopoDS_Face.hxx>
55 #include <TopoDS_CompSolid.hxx>
56 #include <TopoDS_Compound.hxx>
57 #include <TopoDS_Iterator.hxx>
58 #include <TopTools_ListOfShape.hxx>
59 #include <TopTools_ListIteratorOfListOfShape.hxx>
60 #include <BRepBuilderAPI_MakeSolid.hxx>
61 #include <TopTools_HArray2OfShape.hxx>
62 #include <TopTools_ListOfShape.hxx>
63 #include <TopTools_DataMapOfShapeListOfShape.hxx>
64 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
65 // end of mpv modifications 08.04.2002
66
67 #include <TNaming_NCollections.hxx>
68
69 #define BUC60925
70 #define OCC352
71
72 #ifdef OCCT_DEBUG_DBGTOOLS_WRITE
73 //#define OCC355
74 #define MDTV_DEB
75 #define MDTV_DEB_OR
76 #define MDTV_DEB_UNN
77 #define  MDTV_DEB_INT
78 #define MDTV_DEB_GEN
79 #define  MDTV_DEB_MODUN
80 #define MDTV_DEB_FNB
81 #define  MDTV_DEB_WIN
82 #define MDTV_DEB_ARG
83 #define MDTV_DEB_SHELL
84 #endif
85 #ifdef OCCT_DEBUG
86 #include <TCollection_AsciiString.hxx>
87 #include <TDF_Tool.hxx>
88 #include <BRepTools.hxx>
89 #endif
90 #ifdef OCCT_DEBUG
91 #include <TCollection_AsciiString.hxx>
92 #include <TDF_Tool.hxx>
93 #include <TDF_ChildIterator.hxx>
94 #include <TDF_MapIteratorOfLabelMap.hxx>
95 //=======================================================================
96 void PrintEntry(const TDF_Label&       label)
97 {
98   TCollection_AsciiString entry;
99   TDF_Tool::Entry(label, entry);
100   cout << "LabelEntry = "<< entry << endl;
101 }
102 //=======================================================================
103 void PrintEntries(const TDF_LabelMap& map)
104 {
105   cout << "=== Labels Map ===" <<endl;
106   TCollection_AsciiString entry;
107   TDF_MapIteratorOfLabelMap it(map);
108   for(;it.More();it.Next()) {
109     TDF_Tool::Entry(it.Key(), entry);
110       cout << "LabelEntry = "<< entry << endl;
111     }
112 }
113 #ifdef OCCT_DEBUG_DBGTOOLS_WRITE
114 //=======================================================================
115 static void DbgTools_Write(const TopoDS_Shape& shape,
116                       const Standard_CString filename) 
117 {
118   char buf[256];
119   if(strlen(filename) > 256) return;
120   strcpy (buf, filename);
121   char* p = buf;
122   while (*p) {
123     if(*p == ':')
124       *p = '-';
125     p++;
126   }
127   ofstream save (buf);
128   if(!save) 
129     cout << "File " << buf << " was not created: rdstate = " << save.rdstate() << endl;
130   save << "DBRep_DrawableShape" << endl << endl;
131   if(!shape.IsNull()) BRepTools::Write(shape, save);
132   save.close();
133 }
134 //=======================================================================
135 static void DbgTools_Write(const TopTools_MapOfShape& MS,  const Standard_CString filename) 
136 {
137   if (!MS.IsEmpty ()) {
138     TCollection_AsciiString aNam (filename);
139     Standard_Integer i(0);
140     TopTools_MapIteratorOfMapOfShape it(MS);
141     for(;it.More();it.Next(),i++) {
142       TCollection_AsciiString aName = aNam + "_" + i + ".brep";
143       DbgTools_Write ( it.Key(), aName.ToCString());
144     }
145   }
146 }
147 //=======================================================================
148 static void DbgTools_WriteNSOnLabel (const Handle(TNaming_NamedShape)& NS,  
149                                      const Standard_CString filename) 
150 {
151   if(!NS.IsNull() && !NS->IsEmpty() ) {
152     TCollection_AsciiString aNam (filename);
153     TCollection_AsciiString oldS ("_Old");
154     TCollection_AsciiString newS ("_New_");
155     Standard_Integer i(0);
156     TNaming_Iterator it(NS);
157     for(;it.More(); it.Next(),i++) {
158       TCollection_AsciiString aName1 = aNam + oldS + i + ".brep";
159       TCollection_AsciiString aName2 = aNam + newS + i + ".brep";
160       const TopoDS_Shape& oldShape = it.OldShape();
161       const TopoDS_Shape& newShape = it.NewShape();
162       if(!oldShape.IsNull())
163         DbgTools_Write ( oldShape, aName1.ToCString());
164       if(!newShape.IsNull())
165         DbgTools_Write ( newShape, aName2.ToCString());      
166     }
167   }
168 }
169 #endif
170 #endif
171 //
172 //====================================================================
173 static Standard_Boolean ValidArgs(const TNaming_ListOfNamedShape& Args)
174 {
175   TNaming_ListIteratorOfListOfNamedShape it(Args);
176   for (;it.More();it.Next()) {
177     const Handle(TNaming_NamedShape)& aNS = it.Value();
178     if(aNS.IsNull()) {
179 #ifdef OCCT_DEBUG_ARG 
180       cout << "ValidArgs:: NS (Naming argument) is NULL" <<endl;
181 #endif  
182       return Standard_False;
183     }
184     else 
185       if(aNS->IsEmpty()) {
186 #ifdef OCCT_DEBUG_ARG
187         TCollection_AsciiString entry;
188         TDF_Tool::Entry(aNS->Label(), entry);
189         cout << "ValidArgs:: Empty NS, Label = " << entry <<endl;
190 #endif  
191       return Standard_False;
192     }
193       else  
194         if(!aNS->IsValid()) {
195 #ifdef OCCT_DEBUG_ARG 
196           TCollection_AsciiString entry;
197           TDF_Tool::Entry(aNS->Label(), entry);
198           cout << "ValidArgs::Not valid NS Label = " << entry <<endl;
199 #endif  
200           return Standard_False; 
201         }
202     }
203   return Standard_True;
204 }
205
206 //=======================================================================
207 //function : TNaming_Name
208 //purpose  : 
209 //=======================================================================
210
211 TNaming_Name::TNaming_Name() : 
212    myType(TNaming_UNKNOWN),
213    myIndex(-1)
214 {
215 }
216
217
218 //=======================================================================
219 //function : Type
220 //purpose  : 
221 //=======================================================================
222
223 void TNaming_Name::Type(const TNaming_NameType aType) 
224 {
225   myType = aType;
226 }
227
228 //=======================================================================
229 //function : Type
230 //purpose  : 
231 //=======================================================================
232
233 void TNaming_Name::ShapeType(const TopAbs_ShapeEnum T) 
234 {
235   myShapeType = T;
236 }
237
238 //=======================================================================
239 //function : Shape
240 //purpose  : 
241 //=======================================================================
242
243 void TNaming_Name::Shape(const TopoDS_Shape& theShape) 
244 {
245   myShape = theShape;
246   
247 }
248
249 //=======================================================================
250 //function : Shape
251 //purpose  : 
252 //=======================================================================
253
254 TopoDS_Shape TNaming_Name::Shape() const
255 {
256   return myShape;
257 }
258
259 //=======================================================================
260 //function : Append
261 //purpose  : 
262 //=======================================================================
263
264 void TNaming_Name::Append(const Handle(TNaming_NamedShape)& arg) 
265 {
266   myArgs.Append (arg);
267 }
268
269 //=======================================================================
270 //function : StopNamedShape
271 //purpose  : 
272 //=======================================================================
273
274 void TNaming_Name::StopNamedShape (const Handle(TNaming_NamedShape)& arg) 
275 {
276   myStop = arg;
277 }
278
279 //=======================================================================
280 //function : Index
281 //purpose  : 
282 //=======================================================================
283
284 void TNaming_Name::Index (const Standard_Integer I)
285 {
286   myIndex = I;
287 }
288
289
290 //=======================================================================
291 //function : TNaming_NameType
292 //purpose  : 
293 //=======================================================================
294
295 TNaming_NameType TNaming_Name::Type() const 
296 {
297   return myType;
298 }
299
300 //=======================================================================
301 //function : TNaming_NameType
302 //purpose  : 
303 //=======================================================================
304
305 TopAbs_ShapeEnum TNaming_Name::ShapeType() const 
306 {
307   return myShapeType;
308 }
309
310 //=======================================================================
311 //function : Paste
312 //purpose  : 
313 //=======================================================================
314
315 void TNaming_Name::Paste (TNaming_Name& into,
316                           const Handle(TDF_RelocationTable)& RT) const
317 {
318   into.myType      = myType;
319   into.myShapeType = myShapeType;
320   into.myShape     = myShape;
321   into.myIndex     = myIndex;
322   into.myArgs.Clear();
323 //  into.myOrientation = myOrientation;
324   Handle(TNaming_NamedShape) NS;
325
326   for (TNaming_ListIteratorOfListOfNamedShape it(myArgs); it.More(); it.Next()) {
327     RT->HasRelocation(it.Value(),NS);
328     into.myArgs.Append (NS);
329   }
330   if (!myStop.IsNull()) {
331     RT->HasRelocation(myStop,NS);
332     into.myStop = NS;
333   }
334   if (!myContextLabel.IsNull()) {
335     RT->HasRelocation(myContextLabel,into.myContextLabel);
336   }
337 }
338
339 //=======================================================================
340 //function : Arguments
341 //purpose  : 
342 //=======================================================================
343       
344 const TNaming_ListOfNamedShape& TNaming_Name::Arguments() const
345 {
346   return myArgs;
347 }
348
349 //=======================================================================
350 //function : Arguments
351 //purpose  : 
352 //=======================================================================
353       
354 Handle(TNaming_NamedShape) TNaming_Name::StopNamedShape() const
355 {
356   return myStop;
357 }
358
359 //=======================================================================
360 //function : Index
361 //purpose  : 
362 //=======================================================================
363       
364 Standard_Integer TNaming_Name::Index() const
365 {
366   return myIndex;
367 }
368
369 //=======================================================================
370 //function : MakeShape
371 //purpose  : 
372 //=======================================================================
373
374 static TopoDS_Shape MakeShape (const TopTools_MapOfShape& MS) 
375 {  
376   if (!MS.IsEmpty ()) {
377     TopTools_MapIteratorOfMapOfShape it(MS);
378     if (MS.Extent() == 1) {
379       return it.Key();
380     }
381     else {
382       TopoDS_Compound C;
383       BRep_Builder B;
384       B.MakeCompound(C);
385       for (; it.More(); it.Next()){ 
386         B.Add(C,it.Key());
387       }
388       return C;
389     }
390   }
391   return TopoDS_Shape();  
392 }
393
394 #ifdef OCC352
395
396 //=======================================================================
397 //function : ShapeWithType
398 //purpose  : Tries to make shape with given type from the given shape
399 //=======================================================================
400
401 static TopoDS_Shape ShapeWithType(const TopoDS_Shape     theShape,
402                                   const TopAbs_ShapeEnum theType ) {
403   if (theShape.IsNull() || theType == TopAbs_SHAPE) return theShape;
404   Standard_Integer aType = theShape.ShapeType();
405   if (aType == theType) return theShape;
406
407   TopTools_ListOfShape aShapes;
408   if (aType == TopAbs_COMPOUND) {
409     TopoDS_Iterator anIter(theShape);
410     if (anIter.More()) aType = anIter.Value().ShapeType();
411     for(;anIter.More();anIter.Next()) aShapes.Append(anIter.Value());
412     if (aType == theType) {
413       if (aShapes.Extent() == 1) return aShapes.First();
414       else return theShape;
415     }
416   } else aShapes.Append(theShape);
417
418   TopoDS_Shape aResult;
419   TopTools_ListIteratorOfListOfShape aListIter(aShapes);
420
421   if (aType < theType) {
422     Standard_Integer aCount;
423     for(aCount=0;aListIter.More();aListIter.Next()) {
424       TopExp_Explorer anExp(aListIter.Value(),theType);
425       if (anExp.More()) {
426         if (!anExp.Current().IsNull()) {
427           aResult = anExp.Current();
428           aCount++;
429           if (aCount > 1) return theShape;
430         }
431       }
432     }
433     if (aCount == 1) return aResult;
434   } else { // if the shape type more complex than shapes from aShapes list, try make it
435     switch (aType) {
436     case TopAbs_VERTEX: // can't do something from vertex
437       break;
438     case TopAbs_EDGE: {// make wire from edges
439       if (theType <= TopAbs_SOLID) break;
440       BRepBuilderAPI_MakeWire aMakeWire;
441       aMakeWire.Add(aShapes);
442       if (!aMakeWire.IsDone()) return theShape;
443       if (theType == TopAbs_WIRE) return aMakeWire.Wire();
444       aShapes.Clear(); // don't break: we can do something more of it
445       aShapes.Append(aMakeWire.Wire());
446       aListIter.Initialize(aShapes);
447     }
448     case TopAbs_WIRE: {// make faceS from wires (one per one)
449       if (theType < TopAbs_SOLID) break;
450       TopTools_ListOfShape aFaces;
451       for(;aListIter.More();aListIter.Next()) {
452         BRepBuilderAPI_MakeFace aMakeFace(TopoDS::Wire(aListIter.Value()));
453         if (!aMakeFace.IsDone()) aFaces.Append(aMakeFace.Face());
454       }
455       if (theType == TopAbs_FACE) {
456         if (aFaces.Extent() == 1) return aFaces.First();
457         return theShape;
458       }
459       aShapes.Assign(aFaces); // don't break: we can do something more of it
460       aListIter.Initialize(aShapes);
461     }
462     case TopAbs_FACE: {// make shell from faces
463       if (theType < TopAbs_SOLID) break;
464       BRep_Builder aShellBuilder;
465       TopoDS_Shell aShell;
466       aShellBuilder.MakeShell(aShell);
467       for(;aListIter.More();aListIter.Next()) aShellBuilder.Add(aShell,TopoDS::Face(aListIter.Value()));
468       aShell.Closed (BRep_Tool::IsClosed (aShell));
469       if (theType == TopAbs_SHELL) return aShell;
470       aShapes.Clear(); // don't break: we can do something more of it
471       aShapes.Append(aShell);
472       aListIter.Initialize(aShapes);
473     }
474     case TopAbs_SHELL: {// make solids from shells (one per one)
475       TopTools_ListOfShape aSolids;
476       for(;aListIter.More();aListIter.Next()) {
477         BRepBuilderAPI_MakeSolid aMakeSolid(TopoDS::Shell(aListIter.Value()));
478         if (aMakeSolid.IsDone()) aSolids.Append(aMakeSolid.Solid());
479       }
480       if (theType == TopAbs_SOLID) {
481         if (aSolids.Extent() == 1) return aSolids.First();
482         return theShape;
483       }
484       aShapes.Assign(aSolids); // don't break: we can do something more of it
485       aListIter.Initialize(aShapes);
486     }
487     case TopAbs_SOLID: {// make compsolid from solids
488       BRep_Builder aCompBuilder;
489       TopoDS_CompSolid aCompSolid;
490       aCompBuilder.MakeCompSolid(aCompSolid);
491       for(;aListIter.More();aListIter.Next()) aCompBuilder.Add(aCompSolid,TopoDS::Solid(aListIter.Value()));
492       if (theType == TopAbs_COMPSOLID) return aCompSolid;
493     }
494     }
495   }
496   return theShape;
497 }
498
499 #endif
500
501 //=======================================================================
502 //function : FindModifUntil
503 //purpose  : 
504 //=======================================================================
505
506 static Standard_Boolean FindModifUntil (TNaming_NewShapeIterator&         it,
507                                         TopTools_MapOfShape&              MS,
508                                         const TopoDS_Shape&               S,
509                                         const Handle(TNaming_NamedShape)& Context)
510
511 #ifdef OCCT_DEBUG_MODUN
512   if(!Context.IsNull())
513     PrintEntry(Context->Label());
514 #endif
515   Standard_Boolean found = Standard_False;
516   for (; it.More(); it.Next()) {
517     if (!it.Shape().IsNull()) {
518 #ifdef OCCT_DEBUG_MODUN
519       if(!it.NamedShape().IsNull())
520         PrintEntry(it.NamedShape()->Label());
521 #endif
522       if (it.NamedShape() == Context) {
523         MS.Add(S);
524         found = Standard_True;
525       }
526       else { // New shape != Context
527         TNaming_NewShapeIterator it2(it);
528         found = FindModifUntil (it2,MS,it.Shape(),Context);
529       }
530     }
531   }
532   return found;
533 }
534
535 //=======================================================================
536 //function : ModifUntil
537 //purpose  : returns map <theMS> of generators of Target
538 //=======================================================================
539
540 static void SearchModifUntil (const TDF_LabelMap&               /*Valid*/,
541                               const Handle(TNaming_NamedShape)& Target,
542                               const TNaming_ListOfNamedShape& theListOfGenerators,
543                               TopTools_MapOfShape&              theMS)
544 {
545
546 #ifdef OCCT_DEBUG_MODUN
547    DbgTools_WriteNSOnLabel(Target, "SMUntil_"); // Target <== generated
548   Standard_Integer i = 0;
549   TCollection_AsciiString aGen1("Gens_New_"), aGen2("Gented_Old_"), Und("_");
550 #endif 
551   // Test si S apparait comme oldshape dans Context. : Test if S appears as oldshape in Context.
552   Standard_Boolean found = Standard_False;
553   for (TNaming_ListIteratorOfListOfNamedShape it(theListOfGenerators); it.More(); it.Next()) {
554     const Handle(TNaming_NamedShape)& aNS = it.Value();
555 #ifdef OCCT_DEBUG_MODUN  
556     i++;
557     Standard_Integer j = 0;
558 #endif
559     for (TNaming_Iterator itL (aNS); itL.More(); itL.Next()) { // <- generators
560       const TopoDS_Shape& S = itL.NewShape();
561       found = Standard_False;
562       
563 #ifdef OCCT_DEBUG_MODUN
564       j++;
565       Standard_Integer k = 0;
566       TCollection_AsciiString aNam1 = aGen1 + i + Und + j + ".brep";
567       DbgTools_Write(S, aNam1.ToCString());
568       PrintEntry(aNS->Label());//NSLabel
569 #endif   
570       TNaming_Iterator itC (Target);
571       for  (; itC.More(); itC.Next()) {  // <- generated
572         const TopoDS_Shape& OS = itC.OldShape();
573 #ifdef OCCT_DEBUG_MODUN
574         k++;
575         TCollection_AsciiString aNam2 = aGen2 + i + Und + j + Und + k + ".brep";
576         DbgTools_Write(OS, aNam2.ToCString());
577         PrintEntry(Target->Label());//Target Label
578 #endif 
579         if (OS.IsSame(S)) {
580           theMS.Add(S);
581           found = Standard_True;
582 #ifdef OCCT_DEBUG_MODUN
583           cout << aNam2 << " is Same with " << aNam1 <<endl;
584 #endif
585           break;
586         }
587       }
588       if (!found) {
589         TNaming_NewShapeIterator it1(itL);
590         found = FindModifUntil (it1,theMS,S,Target);
591       }
592     }
593   }
594 }
595
596 //=======================================================================
597 //function : ModifUntil
598 //purpose  : 
599 //=======================================================================
600 // NamedShape for this type is assembled from all last modifications of the 
601 // last argument shapes (see method  TNaming_NamingTool::CurrentShape), 
602 // which are not descendants (see method TNaming_NamingTool::BuildDescendants) 
603 // of the stop shape. This type of naming is used for identification shapes, 
604 // which has only one parent with evolution PRIMITIVE (or itself), which 
605 // uniquely identifies it. In most cases stop shape is empty and this algorithm 
606 // is equal to the algorithm for IDENTITY. 
607 //=======================================================================
608 static Standard_Boolean ModifUntil (const TDF_Label&                  L,
609                                     const TDF_LabelMap&               Valid,
610                                     const TNaming_ListOfNamedShape&   Args,
611                                     const Handle(TNaming_NamedShape)& Stop)
612 {
613   TopTools_MapOfShape MS; 
614   TDF_LabelMap Forbiden;
615 #ifdef BUC60925
616   if(!ValidArgs(Args)) return Standard_False;
617 #endif
618   TNaming_NamingTool::BuildDescendants (Stop, Forbiden); // fills Forbidden from Stop
619
620 #ifdef OCCT_DEBUG_GEN  
621   cout <<"Regenerating ModifUntil => ";
622   PrintEntry(L);
623   DbgTools_WriteNSOnLabel(Args.Last(), "ModifUntil-");
624  
625 #endif
626   // all last modifications of the last argument
627   TNaming_NamingTool::CurrentShape  (Valid, Forbiden,Args.Last(),MS); 
628 #ifdef OCCT_DEBUG_GEN  
629   Standard_Integer i(0);
630   TopTools_MapIteratorOfMapOfShape it(MS);
631   TCollection_AsciiString aNam("ModifUnti_MS_");
632   TCollection_AsciiString ext(".brep"); 
633 #endif
634   TNaming_Builder B(L);
635   for (TopTools_MapIteratorOfMapOfShape itM(MS); itM.More(); itM.Next()) {
636     const TopoDS_Shape& S = itM.Key();
637     B.Select(S,S);
638 #ifdef OCCT_DEBUG_GEN  
639     TCollection_AsciiString aName = aNam + ++i + ext;
640     DbgTools_Write(S, aName.ToCString()) ;
641     cout << aName.ToCString() << " TS = " << S.TShape()->This() <<endl;
642 #endif
643   }
644   return Standard_True;
645 }
646
647 //=======================================================================
648 //function : ContShape
649 //purpose  : 
650 //=======================================================================
651 // from the NS of the first argument TNaming_Iterator is started, shape "S" 
652 // is the NewShape from Iterator with index "myIndex" of the Name, this 
653 // shape and all last modifications (except NamedShapes - descendants of 
654 // the stop shape) are the parts of resulting NamedShape.
655 //=======================================================================
656 static Standard_Boolean ConstShape (const TDF_Label&                  L,
657                                     const TDF_LabelMap&               Valid,
658                                     const TNaming_ListOfNamedShape&   Args,
659                                     const Handle(TNaming_NamedShape)& Stop,
660                                     const Standard_Integer            Index)
661 {
662   TopTools_MapOfShape MS; 
663   TDF_LabelMap Forbiden;
664 #ifdef BUC60925
665   if(!ValidArgs(Args)) return Standard_False;
666 #endif
667   TNaming_NamingTool::BuildDescendants (Stop, Forbiden);
668
669   TopoDS_Shape S;
670   Standard_Integer i = 1;
671   for (TNaming_Iterator it(Args.First()); it.More(); it.Next(), i++) {
672     if (Index == i) {
673       S = it.NewShape();
674       break;
675     }
676   }
677   if (S.IsNull()) return Standard_False;
678
679   TNaming_NamingTool::CurrentShapeFromShape  (Valid,Forbiden,L,S,MS);
680
681  
682   TNaming_Builder B(L);
683   for (TopTools_MapIteratorOfMapOfShape itM(MS); itM.More(); itM.Next()) {
684     const TopoDS_Shape& SS = itM.Key();
685     B.Select(SS,SS);
686   }
687   return Standard_True;
688 }
689
690 //=======================================================================
691 //function : Intersection
692 //purpose  : 
693 //=======================================================================
694 //algorithm does next steps:
695 // 1. Sets to the "Forbiden" map  all shapes, which are descendants of stop 
696 //    shape. Named shapes at these labels can't be used.
697 // 2. Takes first argument (with method CurrentShape) and sets map "S" of 
698 //    ancestors (shapes, which belong to this one) of its shape with type 
699 //    "ShapeType" of Name.
700 // 3. Takes next argument of Name (with method CurrentShape) and removes 
701 //    from the map "S" all ancestors, which not belongs to the shape of 
702 //    this argument. This step is repeated for all arguments of this Name.
703 // 4. Adds to the result NamedShape all rest of shapes from the map "S".
704 //=======================================================================
705 static Standard_Boolean Intersection (const TDF_Label&                  L,
706                                       const TDF_LabelMap&               Valid,
707                                       const TNaming_ListOfNamedShape&   Args,
708                                       const Handle(TNaming_NamedShape)& Stop,
709                                       const TopAbs_ShapeEnum            ShapeType,
710                                       const Standard_Integer            Index)
711 {
712   if (Args.IsEmpty()) return Standard_False;
713 #ifdef BUC60925
714   if(!ValidArgs(Args)) return Standard_False;
715 #endif
716   TNaming_ListIteratorOfListOfNamedShape it(Args); 
717   TopTools_MapOfShape MS; 
718   TDF_LabelMap        Forbiden;
719
720 #ifdef OCCT_DEBUG_INT
721   if(!Stop.IsNull() && !Stop->Get().IsNull()) {
722     DbgTools_Write(Stop->Get(), "Ints_Stop.brep");
723     PrintEntry(Stop->Label());
724   }
725   cout <<"Ints: ShapeType = " << ShapeType << endl;
726   cout <<"Argument 1 at ";
727   PrintEntry(it.Value()->Label());
728 #endif 
729
730   TNaming_NamingTool::BuildDescendants (Stop, Forbiden); // <==<1>
731
732 #ifdef OCCT_DEBUG_INT
733   cout << "Intersection:: Valid Map: "<<endl;
734   PrintEntries(Valid);
735   cout << "Intersection:: Forbidden Map: "<<endl;
736   PrintEntries(Forbiden);
737 #endif
738   TopTools_ListOfShape aListOfAnc;
739   TNaming_NamingTool::CurrentShape  (Valid, Forbiden,it.Value(),MS); // first argument
740   TopoDS_Shape  CS = MakeShape(MS);
741   TNaming_ShapesSet S(CS,ShapeType); // <==<2>
742   aListOfAnc.Append(CS);
743 #ifdef OCCT_DEBUG_INT
744   if(!CS.IsNull())
745     DbgTools_Write(CS, "Int_CS_1.brep");
746   Standard_Integer i=2;
747   TCollection_AsciiString aNam("Int_CS_");
748   TCollection_AsciiString ext(".brep");
749  
750 #endif 
751   it.Next();  // <<===<3.1>
752   for (; it.More(); it.Next()) {
753     MS.Clear();
754     TNaming_NamingTool::CurrentShape (Valid,Forbiden,it.Value(),MS);
755     CS = MakeShape(MS);
756     aListOfAnc.Append(CS);
757 #ifdef OCCT_DEBUG_INT
758     TCollection_AsciiString aName = aNam + i++ + ext;      
759     DbgTools_Write(CS, aName.ToCString()) ;
760     cout <<"Argument " << i << " at ";
761     PrintEntry(it.Value()->Label());
762 #endif  
763
764     TNaming_ShapesSet OS(CS,ShapeType);
765     S.Filter(OS); //<<===<3.2>
766 #ifdef OCCT_DEBUG_INT
767     Standard_Integer j = 1;
768     TCollection_AsciiString aNam2("SSMap_"), aName3;
769     TopTools_MapIteratorOfMapOfShape itm(S.Map());
770     for(;itm.More();itm.Next(), j++) {
771       aName3 = aNam2 + i + "_" + j + ".brep";
772       DbgTools_Write(itm.Key(), aName3.ToCString());
773     }
774 #endif 
775   }
776
777 #ifdef OCCT_DEBUG_INT
778   aNam = "Int_S_";
779   i =1;
780 #endif
781
782   TNaming_Builder B(L); //<<===<4>
783   Standard_Boolean isOK(Standard_False);
784   if(S.Map().Extent() > 1 && Index > 0 && ShapeType == TopAbs_EDGE) {
785     Standard_Integer indxE(0), nbE(0), indxW(0),nbW(0), indxF(0);
786     indxE =  Index & 0x000000FF;
787     nbE   = (Index & 0x0000FF00) >> 8;
788     indxW = (Index & 0x000F0000) >> 16;
789     nbW   = (Index & 0x00F00000) >> 20;
790     indxF = (Index & 0x0F000000) >> 24;
791     Standard_Integer i(1);
792     TopoDS_Shape aS;
793     TopTools_ListIteratorOfListOfShape itl(aListOfAnc);
794     for(;itl.More();itl.Next(),i++) {
795       if(indxF == i) {
796         aS = itl.Value();
797         break;
798       }      
799     }
800 #ifdef OCCT_DEBUG_INT
801     cout <<"Kept: indxE = " << indxE  <<" maxENum = " << nbE << " indxW = " <<indxW << " nbW = " <<nbW<<endl;
802 #endif      
803     Standard_Integer aNbW(0), aCaseW(0);
804     TopoDS_Iterator it2(aS);
805     for (;it2.More();it2.Next()) aNbW++;  
806     if(aNbW == nbW) aCaseW = 1;//exact solution for wire (nb of wires is kept)
807     else aCaseW = 2; // indefinite description ==> compound which can include expected wire    
808     if(aCaseW == 1) {      
809       TopoDS_Shape aWire;
810       Standard_Integer i(1);
811       it2.Initialize(aS);
812       for (;it2.More();it2.Next(),i++) {
813         if(indxW == i) {
814           aWire = it2.Value();
815           break;
816         }
817       }
818       Standard_Integer aNbE(0), aCaseE(0);
819       it2.Initialize(aWire);
820       for (;it2.More();it2.Next()) aNbE++;
821       if(aNbE == nbE) aCaseE = 1;//exact solution for edge
822       else aCaseE = 2;
823       if(aCaseE == 1) {
824         i=1;
825         TopoDS_Shape anEdge;
826         it2.Initialize(aWire);
827         for (;it2.More();it2.Next(),i++) {
828           if(indxE == i) {
829             anEdge = it2.Value();
830             break;
831           }
832         }
833         if(!anEdge.IsNull()) {
834           B.Select(anEdge, anEdge);
835           isOK = Standard_True;
836         }
837       }
838     }   
839   } 
840   if(!isOK)
841 #ifdef OCCT_DEBUG_INT
842     for (TopTools_MapIteratorOfMapOfShape itM(S.Map()); itM.More(); itM.Next(),i++) {
843 #else
844       
845     for (TopTools_MapIteratorOfMapOfShape itM(S.Map()); itM.More(); itM.Next()) {
846 #endif
847       const TopoDS_Shape& S1 = itM.Key();
848 #ifdef OCCT_DEBUG_INT
849       TCollection_AsciiString aName = aNam + i + ext;      
850       DbgTools_Write(S1, aName.ToCString()) ;
851 #endif  
852
853       B.Select(S1,S1);
854       isOK = Standard_True;
855     }
856   return isOK;
857 }
858 //=======================================================================
859 static void KeepInList(const TopoDS_Shape& CS, const TopAbs_ShapeEnum Type, TopTools_ListOfShape& aList)   
860 {
861   if (CS.IsNull()) return;
862
863   if (Type == TopAbs_SHAPE) { 
864     if (CS.ShapeType() == TopAbs_SOLID ||
865         CS.ShapeType() == TopAbs_FACE  ||
866         CS.ShapeType() == TopAbs_EDGE  ||
867         CS.ShapeType() == TopAbs_VERTEX ) {
868       aList.Append(CS);
869     }
870     else {
871       for (TopoDS_Iterator it(CS) ; it.More(); it.Next()) {
872         aList.Append(it.Value());
873       }
874     }
875   }
876   else {
877     if (Type > CS.ShapeType()) {
878       for (TopExp_Explorer exp(CS,Type) ; exp.More(); exp.Next()) {
879         aList.Append(exp.Current());
880       }
881     } else {
882      aList.Append(CS);
883     }
884   }
885 }
886
887 //=======================================================================
888 //function : Union
889 //purpose  : 
890 //=======================================================================
891 // Resulting NamedShape contains compound of next shapes: 
892 // compound of last modifications of each argument (see CurrentShape method) 
893 // without descendants of the stop shape.
894 //=======================================================================
895 static Standard_Boolean Union (const TDF_Label&                  L,
896                                const TDF_LabelMap&               Valid,
897                                const TNaming_ListOfNamedShape&   Args,
898                                const Handle(TNaming_NamedShape)& Stop,
899                                const TopAbs_ShapeEnum            ShapeType,
900                                const TDF_Label&                  ContextLabel)
901 {  
902   if (Args.IsEmpty()) return Standard_False;
903 #ifdef BUC60925
904   if(!ValidArgs(Args)) return Standard_False;
905 #endif
906   // temporary solution for Orientation name
907   Standard_Boolean isOr(Standard_True);
908 /* not completed
909   const TDF_Label& aLabel = L.Father();
910   if(!aLabel.IsNull()) {
911   PrintEntry(L);
912   PrintEntry(aLabel);
913     Handle (TNaming_Naming)  Naming;
914     if(aLabel.FindAttribute(TNaming_Naming::GetID(), Naming)) {
915       const TNaming_Name& aName = Naming->GetName();
916       if(aName.Type() == TNaming_ORIENTATION) {
917         const TNaming_ListOfNamedShape&   Args = aName.Arguments();
918         if(Args.Extent() > 2) {
919           const Handle(TNaming_NamedShape)& A = Args.First();
920           if(!A.IsNull()) {
921             PrintEntry(A->Label());
922             if (A->Label() == L) 
923               isOr = Standard_True;         
924           }
925         } else 
926           if(!Args.Extent())
927             isOr = Standard_True;
928       }
929     }
930   }
931 */
932   // end of temp. sol.
933
934   TNaming_ListIteratorOfListOfNamedShape it(Args);
935   TopTools_MapOfShape MS; 
936   TDF_LabelMap        Forbiden;
937   
938   TNaming_NamingTool::BuildDescendants (Stop, Forbiden);//fill Forbidden
939   TNaming_NamingTool::CurrentShape  (Valid, Forbiden,it.Value(),MS); // fill MS with last modifications of the first argument
940   TopoDS_Shape  CS = MakeShape(MS);
941
942   TopTools_ListOfShape aListS;
943   if(isOr)
944     KeepInList(CS,ShapeType,aListS);
945   TNaming_ShapesSet S(CS,ShapeType);//fill internal map of shapeset by shapes of the specified type
946 #ifdef OCCT_DEBUG_UNN
947   TCollection_AsciiString entry; 
948   TDF_Tool::Entry(it.Value()->Label(), entry);
949   TCollection_AsciiString Nam("Arg_");
950   TCollection_AsciiString aNam = Nam + entry + "_" + "1.brep";
951   DbgTools_Write(CS, aNam.ToCString());
952   Standard_Integer ii = 1;
953 #endif
954   it.Next();
955   for (; it.More(); it.Next()) {
956 #ifdef OCCT_DEBUG_UNN  
957     TDF_Tool::Entry(it.Value()->Label(), entry);
958 #endif
959       MS.Clear();
960       TNaming_NamingTool::CurrentShape (Valid, Forbiden,it.Value(),MS);// fill MS with last modifications of the it.Value()
961       CS = MakeShape(MS);
962       if(isOr)
963         KeepInList(CS,ShapeType,aListS); 
964       TNaming_ShapesSet OS(CS,ShapeType);
965       S.Add(OS); //concatenate both shapesets
966  
967 #ifdef OCCT_DEBUG_UNN 
968     ii++;
969     TCollection_AsciiString aNm = Nam + entry + "_" + ii + ".brep";
970     DbgTools_Write(CS, aNm.ToCString());
971     cout <<"Arg: Entry = " <<entry <<"  TShape = " << CS.TShape() <<endl;
972 #endif
973   }
974
975 // start szy 27.05.08
976   TopoDS_Shape aCand;
977   Standard_Boolean found = Standard_False;
978   if(!ContextLabel.IsNull()) {
979     Handle(TNaming_NamedShape) CNS;
980     ContextLabel.FindAttribute(TNaming_NamedShape::GetID(),CNS);
981     TopoDS_Shape aContext;
982     if(!CNS.IsNull()) {
983       MS.Clear();
984       TNaming_NamingTool::CurrentShape (Valid, Forbiden, CNS, MS);
985       aContext = MakeShape(MS);
986 #ifdef OCCT_DEBUG_UNN 
987       TCollection_AsciiString anEntry;
988       TDF_Tool::Entry(ContextLabel, anEntry);
989       cout << "UNION: Context Label = " <<  anEntry << endl;
990       DbgTools_Write(aContext, "Union_Context.brep");
991       TCollection_AsciiString aN ("aMap_");
992       TopTools_MapIteratorOfMapOfShape it(S.Map());
993       for(Standard_Integer i=1; it.More();it.Next(), i++) {
994         TCollection_AsciiString aName = aN + i + ".brep";
995         DbgTools_Write(it.Key(), aName.ToCString());
996       }
997 #endif
998     }
999     TopTools_ListOfShape aList;
1000     TopExp_Explorer anExpl(aContext, ShapeType);
1001     for(;anExpl.More(); anExpl.Next()) 
1002       aList.Append(anExpl.Current());
1003 #ifdef OCCT_DEBUG_UNN
1004     cout <<"UNION: ShapeType = " << ShapeType << " List ext = " << aList.Extent()<<endl;
1005     TopAbs_ShapeEnum aTyp = TopAbs_SHAPE;
1006     TopTools_MapIteratorOfMapOfShape it1 (S.Map());
1007     for (int i=1;it1.More();it1.Next(),i++) {
1008       cout << "Map("<<i<<"): TShape = " << it1.Key().TShape() << " Orient = " << it1.Key().Orientation() <<endl;
1009       aTyp = it1.Key().ShapeType();
1010     }
1011     
1012     TopExp_Explorer exp(aContext, aTyp);
1013     for(int i =1;exp.More();exp.Next(), i++) {
1014      cout << "Context("<<i<<"): TShape = " << exp.Current().TShape() << " Orient = " << exp.Current().Orientation() <<endl;
1015     }
1016             
1017 #endif
1018     TopTools_ListIteratorOfListOfShape itl(aList);
1019     for(;itl.More();itl.Next()) {
1020       aCand = itl.Value(); 
1021 #ifdef OCCT_DEBUG_UNN 
1022       DbgTools_Write(aCand, "Cand.brep");
1023 #endif
1024       Standard_Integer num = S.Map().Extent();
1025       anExpl.Init(aCand, (ShapeType == TopAbs_WIRE) ? TopAbs_EDGE : TopAbs_FACE); 
1026       for(;anExpl.More();anExpl.Next()) {
1027         if(S.Map().Contains(anExpl.Current()))
1028           num--;
1029       } 
1030       if(num == 0) {
1031         found = Standard_True; 
1032         break;
1033       }
1034     }
1035 // end szy 27.05.08
1036   }
1037
1038   TNaming_Builder B(L);
1039 #ifdef OCCT_DEBUG_UNN
1040   if(!ContextLabel.IsNull()) {
1041     if(found) cout << "UNION: Shape is found in Context" <<endl;
1042     else cout << "UNION: Shape is NOT found in Context" <<endl;
1043   }
1044 #endif
1045 #ifdef OCC352
1046   if(found) 
1047     B.Select(aCand, aCand);
1048   else {
1049     BRep_Builder aCompoundBuilder;
1050     TopoDS_Compound aCompound;
1051     aCompoundBuilder.MakeCompound(aCompound);
1052     if(!isOr)
1053       for (TopTools_MapIteratorOfMapOfShape itM(S.Map()); itM.More(); itM.Next()) {
1054         aCompoundBuilder.Add(aCompound,itM.Key());
1055       }
1056     else
1057       for (TopTools_ListIteratorOfListOfShape itL(aListS); itL.More(); itL.Next()) {
1058         aCompoundBuilder.Add(aCompound,itL.Value());
1059       }
1060     TopoDS_Shape aShape = ShapeWithType(aCompound,ShapeType);
1061 #ifdef OCCT_DEBUG_UNN 
1062     DbgTools_Write(aShape, "Union_Selected.brep");
1063     DbgTools_Write(aCompound, "Union_Compound.brep");
1064 #endif
1065     B.Select(aShape,aShape);
1066   }
1067 #else
1068
1069   for (TopTools_MapIteratorOfMapOfShape itM(S.Map()); itM.More(); itM.Next()) {
1070     const TopoDS_Shape& S = itM.Key();
1071     B.Select(S,S);
1072   }
1073
1074 #endif
1075
1076   return Standard_True;
1077 }
1078
1079 //=======================================================================
1080 static TopoDS_Shape FindShape(const TNaming_DataMapOfShapeMapOfShape& DM) 
1081 {
1082   TopoDS_Shape aResult;
1083   Standard_Integer aNum = DM.Extent();
1084   if(aNum < 1) return aResult;  
1085   TopTools_ListOfShape List;
1086   TNaming_DataMapIteratorOfDataMapOfShapeMapOfShape it(DM);
1087   if(it.More()) {
1088     const TopoDS_Shape& aKey1 = it.Key();
1089     const TNaming_MapOfShape& aMap = it.Value();
1090
1091     TNaming_MapIteratorOfMapOfShape itm(aMap); // iterate first map
1092     for (;itm.More();itm.Next()) {
1093       const TopoDS_Shape& aS = itm.Key(); // element of the first map
1094       Standard_Boolean isCand(Standard_True); // aS is a Candidate
1095       TNaming_DataMapIteratorOfDataMapOfShapeMapOfShape it2(DM);
1096       for (;it2.More();it2.Next()) {
1097         const TopoDS_Shape& aKey2 = it2.Key();
1098         if(aKey2 == aKey1) continue;
1099         const TNaming_MapOfShape& aMap2 = it2.Value();
1100         if(!aMap2.Contains(aS)) isCand = Standard_False;
1101       } 
1102       if(isCand)
1103         List.Append(aS);
1104     }
1105   }
1106   if(List.IsEmpty()) return aResult;
1107   if(List.Extent() == 1) return List.First();
1108   TopTools_ListIteratorOfListOfShape itl (List);
1109   TopoDS_Compound Compound;
1110   BRep_Builder B;
1111   B.MakeCompound(Compound);
1112   for (; itl.More(); itl.Next()){ 
1113     B.Add(Compound,itl.Value());
1114   }
1115   return Compound; 
1116 }
1117
1118 //=======================================================================
1119 //function : Generation
1120 //purpose  : Resolves Name from arguments: arg1 - generated (target shape)
1121 //         : arg2 - the generator: the oldest ancestor (usually NS with 
1122 //         : PRIMITIVE evolution. (See TNaming_Localizer::FindGenerator).
1123 //         : Resulting NamedShape contains shape, which is in the first 
1124 //         : argument NamedShape and is modification of the last argument NS.
1125 //=======================================================================
1126
1127 static Standard_Boolean  Generated (const TDF_Label&                L,
1128                                     const TDF_LabelMap&             Valid,
1129                                     const TNaming_ListOfNamedShape& Args)
1130 {
1131   if (Args.Extent() < 2) {
1132     Standard_ConstructionError::Raise("TNaming_Name::Solve: => Generated");
1133   }
1134   // First argument : label of generation
1135   // Next arguments : generators.
1136
1137 #ifdef BUC60925
1138   if(!ValidArgs(Args)) return Standard_False;
1139 #endif
1140
1141   TDF_Label   LabelOfGeneration = Args.First()->Label();
1142 #ifdef OCCT_DEBUG_GEN
1143       DbgTools_Write(Args.First()->Get(),  "Generated.brep") ;
1144 #endif
1145   // Nouvell valeurs des generateurs dans l attribut de generation
1146   TopTools_MapOfShape aMS;
1147   TNaming_ListOfNamedShape aGenerators; 
1148   aGenerators.Assign(Args); 
1149   aGenerators.RemoveFirst(); 
1150 #ifdef OCCT_DEBUG_GEN
1151       DbgTools_Write(aGenerators.First()->Get(),  "Generators.brep") ;
1152 #endif
1153   SearchModifUntil (Valid, Args.First(), aGenerators, aMS);
1154   Handle(TNaming_Naming) aNaming;
1155   TopoDS_Shape aSelection;
1156   L.FindAttribute(TNaming_Naming::GetID(),aNaming);
1157   if(!aNaming.IsNull()) 
1158     aSelection = aNaming->GetName().Shape();
1159 #ifdef OCCT_DEBUG_GEN
1160   DbgTools_Write(aSelection,  "G_Selection.brep") ;
1161   cout << "Generated::SearchModifUntil aMS.Extent() = " << aMS.Extent() <<endl;
1162   DbgTools_Write(aMS, "SearchModifUntil_Result");
1163 #endif
1164    Handle(TNaming_NamedShape) anOldNS;
1165    Standard_Integer aVer = -1;// Initial build of name
1166    L.FindAttribute(TNaming_NamedShape::GetID(),anOldNS);
1167    if(!anOldNS.IsNull()) 
1168      aVer = anOldNS->Version();
1169
1170 #ifdef OCCT_DEBUG_GEN
1171   Standard_Integer i = 0, j=1;
1172   TCollection_AsciiString aNam2("Gen_New_");
1173   TCollection_AsciiString aNam1("Gen_Old_");
1174   TCollection_AsciiString ext(".brep");
1175 #endif
1176   TNaming_Builder B(L); // NS
1177   TopTools_ListOfShape aList;
1178   TNaming_DataMapOfShapeMapOfShape aDM; 
1179   for (TopTools_MapIteratorOfMapOfShape itMS(aMS); itMS.More(); itMS.Next()) {
1180     const TopoDS_Shape& OS = itMS.Key();
1181 #ifdef OCCT_DEBUG_GEN
1182     TCollection_AsciiString aName = aNam1 + ++i + ext;      
1183     DbgTools_Write(OS, aName.ToCString()) ;
1184     Standard_Integer j=0;
1185 #endif
1186     TNaming_MapOfShape aMapDM; 
1187     for (TNaming_NewShapeIterator itNew(OS,L); itNew.More(); itNew.Next()) 
1188       if (itNew.Label() == LabelOfGeneration) {
1189         aMapDM.Add(itNew.Shape());
1190         aList.Append(itNew.Shape());//szy 21.10.03
1191 #ifdef OCCT_DEBUG_GEN
1192         TCollection_AsciiString aName = aNam2 + i +  "_" + ++j + ext;      
1193         DbgTools_Write(itNew.Shape(), aName.ToCString()) ;
1194 #endif
1195       }
1196     if(aMapDM.Extent())
1197       aDM.Bind(OS, aMapDM);
1198   }
1199
1200   if(aVer == -1) { //initial 
1201     Standard_Integer i = 1;
1202     TNaming_Name& aName =  aNaming->ChangeName();
1203     TopTools_ListIteratorOfListOfShape it(aList);
1204     if(!aSelection.IsNull()) {
1205       for(;it.More();it.Next(),i++) {
1206         if(it.Value().IsSame(aSelection)) {
1207           B.Select(it.Value(), it.Value());
1208           aName.Index(i); // for debug - index of selection in the list
1209           break;
1210         }
1211       }
1212     } else {// Selection == Null
1213       for(;it.More();it.Next())
1214         B.Select(it.Value(), it.Value()); 
1215     }
1216   }
1217   else { 
1218     // *** Regeneration *** //
1219     if(aList.Extent() == 1) { // trivial case
1220       B.Select(aList.Last(), aList.Last()); 
1221     } 
1222     else {
1223       TNaming_Name& aName =  aNaming->ChangeName();
1224       const TopAbs_ShapeEnum aType = aName.ShapeType();
1225       TopTools_ListOfShape aList2; 
1226       TopTools_ListIteratorOfListOfShape it(aList);
1227       for(;it.More();it.Next()) {
1228         if(it.Value().ShapeType() == aType) //collect only the same type
1229           aList2.Append(it.Value());
1230       }
1231       if(!aList2.Extent()) return Standard_False; // Empty
1232
1233       Standard_Boolean found = Standard_False;
1234       TopoDS_Shape aShape = FindShape(aDM);
1235 #ifdef OCCT_DEBUG_GEN
1236       if(!aShape.IsNull())
1237         DbgTools_Write(aShape,  "G_FindShape.brep") ;
1238 #endif
1239       if(!aShape.IsNull()) found = Standard_True;
1240 #ifdef OCCT_DEBUG_GEN
1241       cout << "Generated ==>aGenerators.Extent() = " <<aGenerators.Extent() <<" aMS.Extent()= " <<aMS.Extent()<<endl;
1242 #endif
1243       if(found) {
1244 #ifdef OCCT_DEBUG_GEN
1245       cout << "Generated ==> Shape is found!" <<endl;
1246 #endif
1247         TopTools_ListOfShape aLM;
1248         Standard_Boolean aHas = Standard_False;
1249         Standard_Boolean a1NB = Standard_False;
1250         if(aGenerators.Extent() != aMS.Extent()) { //missed generators
1251           aHas = Standard_True;//has lost generatos
1252 #ifdef OCCT_DEBUG_GEN
1253       cout << "Generated ==> has lost generatos!" <<endl;
1254 #endif
1255           for (TNaming_ListIteratorOfListOfNamedShape itg(aGenerators); itg.More(); itg.Next()) {
1256             if(!aMS.Contains(itg.Value()->Get())) 
1257               aLM.Append(itg.Value()->Get());
1258           }
1259           if(aLM.Extent() == 1) {//lost 1
1260             TopTools_ListIteratorOfListOfShape itm(aLM);
1261             TopoDS_Shape aSM = itm.Value(); // Missed
1262             for (TopTools_MapIteratorOfMapOfShape itMS1(aMS); itMS1.More(); itMS1.Next()) {
1263               const TopoDS_Shape& aS = itMS1.Key();
1264               if(aSM.ShapeType() == aS.ShapeType()) {
1265                 if(aS.ShapeType() == TopAbs_EDGE) {
1266                   TopoDS_Vertex aVCom;
1267                   if(TopExp::CommonVertex(TopoDS::Edge(aS), TopoDS::Edge(aSM), aVCom))
1268                     {a1NB = Standard_True; 
1269                      break;} //lost only 1 neigbour
1270                 } else if(aS.ShapeType() == TopAbs_FACE) {
1271                   TopExp_Explorer expl1(aS, TopAbs_EDGE);
1272                   for(;expl1.More();expl1.Next()) {
1273                     TopExp_Explorer expl2(aSM, TopAbs_EDGE);
1274                     for(;expl2.More();expl2.Next()) {
1275                       if(expl1.Current().IsSame(expl2.Current()))
1276                         {a1NB = Standard_True;
1277                          break;} //lost only 1 neigbour
1278                     }
1279                   }
1280                 }
1281               }
1282             }
1283 //          if(aShape.ShapeType() == TopAbs_VERTEX && a1NBE) {
1284 //      //if atleast 1 Gen was missed and the Gen is Edge
1285 //            TopTools_ListIteratorOfListOfShape it(aList);
1286 //            for(;it.More();it.Next()) {
1287 //              if(it.Value().ShapeType() == TopAbs_EDGE) {
1288 //                const TopoDS_Shape& aV1 = TopExp::FirstVertex(TopoDS::Edge(it.Value()));
1289 //                const TopoDS_Shape& aV2 = TopExp::LastVertex(TopoDS::Edge(it.Value()));
1290 //                if(aShape.IsSame(aV1)) {aShape2 =  aV2;       cout << "##### => V2 " <<endl;break;}
1291 //                else 
1292 //                  if(aShape.IsSame(aV2)) {aShape2 =  aV1;     cout << "##### => V1 " <<endl;break;}
1293 //              }         
1294 //            }
1295 //          }
1296           }
1297         }
1298         if(!aHas) // all arguments were kept
1299           B.Select(aShape, aShape); //only this case is correct on 100%
1300         else {
1301           if (a1NB) //Has, but may be ...
1302             B.Select(aShape, aShape);   
1303           else { 
1304             // put Compound, may be if possible processed later in Sel. Driver
1305             TopTools_ListIteratorOfListOfShape it1(aList2);
1306             for(;it1.More();it1.Next())
1307               B.Select(it1.Value(), it1.Value()); 
1308           }
1309         }
1310     } else 
1311       { //not found
1312 #ifdef OCCT_DEBUG_GEN
1313         cout << "Generated ==> Shape is NOT found! Probably Compound will be built" <<endl;
1314 #endif
1315
1316         TopTools_ListIteratorOfListOfShape it2(aList2);
1317         for(;it2.More();it2.Next())
1318           B.Select(it2.Value(), it2.Value()); 
1319       }
1320     }
1321   }    
1322   return Standard_True;
1323 }
1324
1325 //=======================================================================
1326 //function : Identity
1327 //purpose  : Regenerates Naming attribute with Name = IDENTITY
1328 //=======================================================================
1329 // Name with this type must contain only one NamedShape attribute as argument. 
1330 // Algorithm takes all last modifications of NamedShape of this argument  
1331 // starting with this one ( see method TNaming_NamingTool::CurrentShape ). 
1332 // Algorithm takes only NamedShapes belonging to the labels from the Valid 
1333 // labels map (if it's not empty) and put to the resulting NamedShape as compound. 
1334 //=======================================================================
1335 static Standard_Boolean Identity (const TDF_Label&                L,
1336                                   const TDF_LabelMap&             Valid,
1337                                   const TNaming_ListOfNamedShape& Args,
1338                                   const TopAbs_ShapeEnum          ShapeType)
1339 {
1340   if (Args.Extent() > 2) {
1341     Standard_ConstructionError::Raise("TNaming_Name::Solve");
1342     }
1343 #ifdef BUC60925
1344   if(!ValidArgs(Args)) return Standard_False;
1345 #endif
1346   const Handle(TNaming_NamedShape)& A = Args.First();
1347   TopTools_MapOfShape MS;
1348   TDF_LabelMap        Forbiden;
1349   TNaming_NamingTool::CurrentShape (Valid,Forbiden,A,MS);
1350 #ifdef OCCT_DEBUG_SOL2
1351   //TCollection_AsciiString entry;
1352   //TDF_Tool::Entry(L, entry);
1353   //TDF_Tool::Entry(A->Label(), entry);
1354 #endif  
1355   TNaming_Builder B(L);
1356   for (TopTools_MapIteratorOfMapOfShape itM(MS); itM.More(); itM.Next()) {
1357 #ifdef OCC352
1358     const TopoDS_Shape& S = ShapeWithType(itM.Key(),ShapeType);
1359 #else
1360     const TopoDS_Shape& S = itM.Key();
1361 #endif
1362 #ifdef OCCT_DEBUG_SOL2
1363     //TopAbs_Orientation Or = S.Orientation();
1364 #endif
1365     B.Select(S,S);
1366   }
1367   return Standard_True;
1368 }
1369
1370 //=======================================================================
1371 //function : FilterByNeighbourgs
1372 //purpose  : regenerated the specified shape with help of its neighbours
1373 //=======================================================================
1374 // result -  is a subshape of the first argument of the Name with type = 
1375 // ShapeType of this Name, which has a common subshapes (boundaries) with 
1376 // each neighbour - shapes from the other arguments of the Name.
1377 //=======================================================================
1378 static Standard_Boolean  FilterByNeighbourgs (const TDF_Label&                L,
1379                                               const TDF_LabelMap&             Valid,
1380                                               const TNaming_ListOfNamedShape& Args,
1381                                               const Handle(TNaming_NamedShape)& Stop,
1382                                               const TopAbs_ShapeEnum          ShapeType)
1383 {  
1384
1385   TNaming_Builder B(L); 
1386
1387   TDF_LabelMap        Forbiden;
1388 #ifdef BUC60925
1389   if(!ValidArgs(Args)) return Standard_False;
1390 #endif
1391   TNaming_NamingTool::BuildDescendants (Stop, Forbiden); //all descendants of Stop (New shapes) are forbidden
1392   if (!Stop.IsNull())    Forbiden.Remove(Stop->Label());
1393   //----------------------------------------
1394   // First argument: collection has to be filtered.
1395   //----------------------------------------
1396   Handle(TNaming_NamedShape) Cand  = Args.First(); //collection of candidates
1397    
1398 #ifdef OCCT_DEBUG_FNB
1399   Standard_Integer i = 1;
1400   TCollection_AsciiString aNam("Cand_");
1401   TCollection_AsciiString ext(".brep");
1402   DbgTools_WriteNSOnLabel(Cand, aNam.ToCString());  
1403   cout << "Cand (Args.First()) Label = ";
1404   PrintEntry(Cand->Label());  
1405   cout << "Valid Label map:" <<endl;
1406   PrintEntries(Valid);
1407 #endif
1408
1409   TopTools_MapOfShape    SCand;
1410   TNaming_NamingTool::CurrentShape  (Valid, Forbiden,Cand,SCand);//fills SCand with last modifications of Cand. CandNS should be at the same level (before) as NS of FilterByNBS
1411
1412 #ifdef OCCT_DEBUG_FNB
1413   TCollection_AsciiString aNam2("SCand");
1414   DbgTools_Write(SCand, aNam2.ToCString());  
1415   cout <<"SCand Extent = " << SCand.Extent() << " Expected ShapeType = " << ShapeType << endl;
1416 #endif 
1417
1418   //------------------------------------------------------------
1419   // Autres arguments : contiennent les voisins du bon candidat.
1420   // Other arguments  : contains the neighbors of the good candidate.
1421   //------------------------------------------------------------
1422   TopAbs_ShapeEnum              TC = TopAbs_EDGE;
1423   if (ShapeType == TopAbs_EDGE) TC = TopAbs_VERTEX;
1424   if (ShapeType == TopAbs_VERTEX) TC = TopAbs_VERTEX; // szy 31.03.10 - to process case when Candidate is of type Vertex
1425  
1426 #ifdef OCCT_DEBUG_FNB
1427   i=1;
1428   aNam = "Boundaries";
1429 #endif
1430   Standard_Boolean isDone = Standard_False;
1431   if(SCand.Extent() == 1) { // check if a collection is inside
1432     TopTools_MapIteratorOfMapOfShape it(SCand);
1433     const TopoDS_Shape& aS = it.Key();
1434     if(!aS.IsNull()) 
1435       if(aS.ShapeType() == TopAbs_COMPOUND && aS.ShapeType() != ShapeType) {
1436         TopoDS_Iterator itt(aS);
1437         for(;itt.More();itt.Next()) 
1438           SCand.Add(itt.Value());      
1439         SCand.Remove(aS);
1440       }
1441   }
1442   for (TopTools_MapIteratorOfMapOfShape itCand(SCand); itCand.More(); itCand.Next()) { //1
1443     const TopoDS_Shape& S    = itCand.Key();
1444     TopTools_MapOfShape Boundaries;
1445     if(S.ShapeType() == TopAbs_VERTEX) //# szy 31.03.10
1446       Boundaries.Add (S); //#
1447     else  //#
1448       for (TopExp_Explorer exp(S,TC); exp.More(); exp.Next()) { //put boundaries of each candidate (from SCand) to the Boundaries map
1449         Boundaries.Add (exp.Current());
1450 #ifdef OCCT_DEBUG_FNB
1451         TCollection_AsciiString aName = aNam + i++ + ext;      
1452         DbgTools_Write(exp.Current(),  aName.ToCString()) ;
1453 #endif
1454       }
1455     
1456     TNaming_ListIteratorOfListOfNamedShape it(Args); 
1457     it.Next(); 
1458     Standard_Boolean Keep = 1;
1459 #ifdef OCCT_DEBUG_FNB
1460     cout <<"Args number = " << Args.Extent() <<endl;
1461     i=1;
1462     aNam = "Boundaries";
1463 #endif
1464     for ( ; it.More(); it.Next()) { //2 ==> for each Arg
1465       Standard_Boolean Connected = Standard_False;
1466       // Le candidat doit etre  connexe a au moins un shape de
1467       // chaque NamedShape des voisins.
1468       // The candidate should be connectedand and have at least one shape of NamedShape
1469       // of each neighbor.
1470       const Handle(TNaming_NamedShape)& NSVois = it.Value();  //neighbor 
1471
1472 #ifdef OCCT_DEBUG_FNB
1473       DbgTools_WriteNSOnLabel(NSVois, "Next_Neighbor_") ;
1474 #endif
1475       
1476       TopTools_MapOfShape    SVois;
1477       TNaming_NamingTool::CurrentShape  (Valid, Forbiden,NSVois,SVois); // fills SVois with last modifications of NSVois
1478
1479 #ifdef OCCT_DEBUG_FNB
1480       TCollection_AsciiString aNam2("SVois");
1481       DbgTools_Write(SVois, aNam2.ToCString());
1482 #endif 
1483
1484       for (TopTools_MapIteratorOfMapOfShape itVois(SVois); itVois.More(); itVois.Next()) { //6
1485         const TopoDS_Shape& Vois = itVois.Key();
1486         for (TopExp_Explorer exp1(Vois,TC); exp1.More(); exp1.Next()) { //7
1487           if (Boundaries.Contains(exp1.Current())) {
1488             Connected = Standard_True; // has common boundaries with candidate shape
1489 #ifdef OCCT_DEBUG_FNB
1490             DbgTools_Write(Vois, "Neighbor_Connected.brep");
1491 #endif
1492             break;
1493           }
1494         } //7
1495         if (Connected) break;
1496       } //6
1497       if (!Connected) {
1498         Keep = 0;
1499         break;
1500       }
1501     } //2
1502     if (Keep) {
1503       B.Select (S,S);
1504           isDone = Standard_True;
1505 #ifdef OCCT_DEBUG_FNB
1506       DbgTools_Write(S,  "FilterByNbs_Sel.brep") ;
1507 #endif
1508     }
1509   } //1
1510   return isDone;
1511 }
1512
1513 //=======================================================================
1514 static const TopoDS_Shape FindSubShapeInAncestor(const TopoDS_Shape& Selection, const TopoDS_Shape& Context )
1515 {
1516 #ifdef OCCT_DEBUG_OR_AG
1517   DbgTools_Write(Selection, "Orientation_Selection.brep");
1518   DbgTools_Write(Context, "Orientation_Context.brep");
1519   TopExp_Explorer expl1(Context, Selection.ShapeType());
1520   int i = 0;
1521   TCollection_AsciiString SS( "Orientation_Current_");
1522   for(;expl1.More(); expl1.Next()) {      
1523     if(expl1.Current().IsSame(Selection)) {
1524       i++;
1525       cout <<"FindSubShape:  = " <<expl1.Current().ShapeType() << " TS = " <<expl1.Current().TShape() << endl;
1526       TCollection_AsciiString nam = SS + i + ".brep";
1527       DbgTools_Write(expl1.Current(), nam.ToCString()); 
1528     }
1529   }
1530 #endif 
1531   if(Selection.ShapeType() != TopAbs_COMPOUND) {
1532     TopExp_Explorer anExpl(Context, Selection.ShapeType());
1533     for(;anExpl.More(); anExpl.Next()) {
1534 #ifdef OCCT_DEBUG_OR_AG
1535       cout <<"FindSubShape:  = " <<anExpl.Current().ShapeType() << " TS = " <<anExpl.Current().TShape()->This() << endl;
1536       DbgTools_Write(anExpl.Current(), "Orientation_Current.brep");
1537 #endif    
1538       if(anExpl.Current().IsSame(Selection)) 
1539         return anExpl.Current();
1540     }
1541   }
1542
1543   return TopoDS_Shape();
1544 }
1545
1546 //=======================================================================
1547 //static Standard_Integer Count(const TopoDS_Shape& S)
1548 //{
1549 //  Standard_Integer N(0);
1550 //  TopoDS_Iterator it(S);
1551 //  for(;it.More();it.Next()) {
1552 //    if(it.Value().ShapeType() != TopAbs_COMPOUND && it.Value().ShapeType() != TopAbs_COMPSOLID)
1553 //      N++;
1554 //    else {
1555 //      N += Count(it.Value());
1556 //    }
1557 //  }
1558 //  return N;
1559 //}
1560 //=======================================================================
1561 static Standard_Integer Aggregation (const TopoDS_Shape& S, const TopoDS_Shape& AS, TNaming_Builder& B)
1562 {
1563   Standard_Integer N(0);
1564   TopoDS_Iterator it(S);
1565   for(;it.More();it.Next()) {
1566     const TopoDS_Shape& sel = it.Value();
1567     if(sel.ShapeType() > TopAbs_COMPSOLID) {
1568       const TopoDS_Shape& CS = FindSubShapeInAncestor(sel, AS);
1569       if(!CS.IsNull()) {
1570         B.Select(CS, CS);
1571         N++;
1572       }
1573     } else 
1574       N += Aggregation(sel, AS, B);    
1575   }
1576   return N;
1577 }
1578
1579 //==========================================================================
1580 //function : Orientation
1581 //purpose  : to solve  ORIENTATION name
1582 // this function explores the second argument | arguments (Context) and 
1583 // keeps at the label (L) the first argument (S) with the orientation it  
1584 // has in the context. Index is used only for Seam edge recomputing 
1585 //==========================================================================
1586 static Standard_Boolean ORientation (const TDF_Label&                L,
1587                                      const TDF_LabelMap&             Valid,
1588                                      const TNaming_ListOfNamedShape& Args,
1589                                      const Handle(TNaming_NamedShape)& Stop,
1590                                      const Standard_Integer          Index)
1591 {
1592
1593   if(!ValidArgs(Args)) return Standard_False;
1594
1595   const Handle(TNaming_NamedShape)& A = Args.First();
1596   TopTools_MapOfShape MS; 
1597   TDF_LabelMap        Forbiden;
1598   TNaming_NamingTool::BuildDescendants (Stop, Forbiden);
1599   TNaming_NamingTool::CurrentShape (Valid,Forbiden,A,MS);
1600
1601   TopoDS_Shape S; 
1602   Standard_Boolean isSplit(Standard_False);
1603   if (!MS.IsEmpty ()) {
1604     TopTools_MapIteratorOfMapOfShape it(MS);
1605     if (MS.Extent() == 1) {
1606       S = it.Key(); 
1607     } 
1608     else {
1609       isSplit = Standard_True;
1610       S = MakeShape(MS);
1611 #ifdef OCCT_DEBUG_OR
1612       for(Standard_Integer i=1;it.More();it.Next(), i++) {
1613         TCollection_AsciiString aNam("OR_Selection_");
1614         TCollection_AsciiString aName = aNam + i + ".brep";
1615         DbgTools_Write(it.Key(), aName.ToCString());
1616       }
1617 #endif
1618     }
1619   }
1620
1621   TNaming_Builder B(L);
1622   if(S.IsNull()) 
1623     return Standard_False;
1624 #ifdef OCCT_DEBUG_OR
1625   DbgTools_Write(S, "Orientation_S.brep");
1626 #endif
1627
1628   TopTools_ListOfShape aSList;
1629   // tmp. solution
1630   if(S.ShapeType() == TopAbs_COMPOUND && !isSplit) {
1631     TopoDS_Iterator it(S);
1632     for(;it.More();it.Next())
1633       aSList.Append(it.Value());
1634   } //
1635  
1636   TopTools_MapOfShape MSC; 
1637   if(aSList.Extent() == 0) {
1638     const Handle(TNaming_NamedShape)& Anc = Args.Last();
1639 #ifdef OCCT_DEBUG_OR
1640     cout << "### ORIENTATION: Ancestor ";
1641     PrintEntry(Anc->Label());
1642 #endif
1643     MSC.Clear();
1644     TNaming_NamingTool::CurrentShape (Valid,Forbiden,Anc,MSC);
1645     if(MSC.Extent() == 1) {
1646       for (TopTools_MapIteratorOfMapOfShape itM(MSC); itM.More(); itM.Next()) {
1647         const TopoDS_Shape& AS = itM.Key(); 
1648 // <=== start 21.10.2009
1649         TopoDS_Shape CS;
1650         if(Index > 0) { //only for seam edge
1651           TopoDS_Iterator itw(AS);
1652           for(;itw.More();itw.Next()) {
1653             Standard_Boolean found(Standard_False);
1654             TopoDS_Iterator it(itw.Value());
1655             for(int i=1;it.More();it.Next(),i++) {
1656               if(i == Index && it.Value().IsSame(S)) {
1657                 CS = it.Value();
1658                 found = Standard_True;
1659 #ifdef OCCT_DEBUG_OR
1660                 cout << "ORIENTATION => ORDER = " << i <<endl;
1661 #endif
1662                 break;
1663               }
1664             }
1665             if(found) break;
1666           }
1667         } else
1668           CS =  FindSubShapeInAncestor(S, AS);
1669 // <=== end 21.10.2009
1670 #ifdef OCCT_DEBUG_OR
1671         cout << "ORIENTATION: Selection" <<" TShape = " <<CS.TShape() <<" Orientation = " << CS.Orientation() <<endl;
1672         cout << "ORIENTATION: Context " << "ShapeType = "<<AS.ShapeType() << " TShape = " <<AS.TShape() <<endl;
1673         DbgTools_Write(AS, "Orientation_Cnt.brep");
1674 #endif
1675         if(!CS.IsNull()) {
1676           B.Select(CS, CS);
1677         } else {
1678           if(!Aggregation(S, AS, B)) 
1679             return Standard_False;        
1680         }
1681       }  
1682     } else {
1683       const TopoDS_Shape  AS = MakeShape(MSC);
1684       const TopoDS_Shape& CS =  FindSubShapeInAncestor(S, AS);
1685       if(!CS.IsNull()) {
1686         B.Select(CS, CS);
1687       }  else {
1688         if(!Aggregation(S, AS, B)) 
1689           return Standard_False;        
1690       }
1691     }
1692   } else {
1693     TNaming_ListIteratorOfListOfNamedShape it(Args); 
1694     it.Next(); //skip first
1695
1696     // temporary solution. To be optimized (+ has connection with Union name)
1697     Handle(TopTools_HArray2OfShape) Arr; // Arr(1,1) - selection; Arr(1,2) - Context shape
1698     Arr = new TopTools_HArray2OfShape (1, aSList.Extent(), 1, 2);
1699     TopTools_ListIteratorOfListOfShape it1(aSList);
1700     Standard_Integer i = 1;
1701     for(; it1.More(); it1.Next(), it.Next(), i++) {
1702       Arr->SetValue(i, 1, it1.Value());
1703       MSC.Clear();
1704       TNaming_NamingTool::CurrentShape (Valid,Forbiden,it.Value(),MSC);
1705       if(MSC.Extent() == 1) {
1706         for (TopTools_MapIteratorOfMapOfShape itM(MSC); itM.More(); itM.Next()) {
1707           const TopoDS_Shape& AS = itM.Key(); 
1708           Arr->SetValue(i, 2, AS);
1709         }
1710       } else {
1711         const TopoDS_Shape  AS = MakeShape(MSC);
1712         Arr->SetValue(i, 2, AS);
1713       }
1714     } 
1715     
1716     if(aSList.Extent() == 1) {
1717       const TopoDS_Shape& S = Arr->Value(1,1);
1718       if(S.ShapeType() != TopAbs_COMPOUND) {        
1719         const TopoDS_Shape& CS =  FindSubShapeInAncestor(S, Arr->Value(1,2));
1720         if(!CS.IsNull()) {
1721           B.Select(CS, CS);
1722         } else 
1723           return Standard_False;
1724       } 
1725       else {
1726 #ifdef OCCT_DEBUG_OR
1727         DbgTools_Write(Arr->Value(1,2), "Ancestor.brep");
1728 #endif
1729         if(!Aggregation(S, Arr->Value(1,2), B)) { 
1730           return Standard_False;
1731         }
1732       }
1733     }
1734     else { // > 1   
1735       for(Standard_Integer i = Arr->LowerRow();i <= Arr->UpperRow();i++) {
1736         const TopoDS_Shape& S = Arr->Value(i,1);
1737         const TopoDS_Shape& AC = Arr->Value(i,2);
1738         if(S.ShapeType() != TopAbs_COMPOUND) {      
1739           const TopoDS_Shape& CS =  FindSubShapeInAncestor(S, AC);
1740           if(!CS.IsNull()) {
1741             B.Select(CS, CS);
1742           } else 
1743             return Standard_False;
1744         }
1745         else {
1746 #ifdef OCCT_DEBUG_OR
1747           DbgTools_Write(AC, "Aggregation.brep");
1748 #endif
1749           if(!Aggregation(S, AC, B)) { 
1750             return Standard_False;
1751           }
1752         }
1753       }
1754     }
1755   } // end of tmp. solution 
1756
1757   return Standard_True;
1758 }
1759
1760 //===========================================================================
1761 //function : WireIN
1762 //purpose  : to solve  WIREIN name
1763 //=======================================================================
1764 static Standard_Boolean WireIN(const TDF_Label&                L,
1765                                const TDF_LabelMap&             Valid,
1766                                const TNaming_ListOfNamedShape& Args, 
1767                                    const Handle(TNaming_NamedShape)& Stop,
1768                                    Standard_Integer Index)
1769 {
1770   Standard_Boolean aResult(Standard_False);
1771   if(!ValidArgs(Args)) return aResult;
1772   TopTools_MapOfShape MS; 
1773   TDF_LabelMap        Forbiden;
1774   if (Args.Extent() < 1 ) 
1775     Standard_ConstructionError::Raise("TNaming_Name::Solve"); 
1776   const Handle(TNaming_NamedShape)& A = Args.First();
1777   TNaming_NamingTool::CurrentShape (Valid,Forbiden,A,MS);
1778   if (MS.Extent() != 1) return aResult;
1779   TopTools_MapIteratorOfMapOfShape itM(MS); 
1780   const TopoDS_Shape& aCF = itM.Key()   ;
1781 #ifdef OCCT_DEBUG_WIN
1782   cout <<"MS Extent = " <<MS.Extent() <<endl;
1783   DbgTools_Write(aCF, "Context_Face.brep");
1784 #endif
1785   TNaming_Builder B(L);
1786   if(Index == 1 ){ //Outer wire case 
1787     TopoDS_Wire anOuterWire;
1788     TNaming::OuterWire(TopoDS::Face(aCF), anOuterWire);
1789         if(!anOuterWire.IsNull()) {
1790       B.Select(anOuterWire, anOuterWire);
1791           aResult = Standard_True;
1792         }
1793   } else { //has internal wires
1794         TNaming_ListOfNamedShape ArgsE;
1795     ArgsE.Assign(Args);
1796     ArgsE.RemoveFirst();
1797         // fill Map with edges 
1798     TNaming_ListIteratorOfListOfNamedShape it(ArgsE);
1799     TopTools_MapOfShape MS; 
1800     TDF_LabelMap        Forbiden;
1801   
1802     TNaming_NamingTool::BuildDescendants (Stop, Forbiden);//fill Forbidden
1803     TNaming_NamingTool::CurrentShape  (Valid, Forbiden,it.Value(),MS); // fill MS with last modifications of the first additional argument
1804     TopoDS_Shape  CS = MakeShape(MS);
1805
1806     TNaming_ShapesSet aSet(CS,TopAbs_EDGE);//fill internal map of shapeset by shapes of the specified type
1807 #ifdef OCCT_DEBUG_WIN
1808     TCollection_AsciiString entry; 
1809     TDF_Tool::Entry(it.Value()->Label(), entry);
1810     TCollection_AsciiString Nam("Arg_");
1811     TCollection_AsciiString aNam = Nam + entry + "_" + "2.brep";
1812     DbgTools_Write(CS, aNam.ToCString());
1813     Standard_Integer ii = 2;
1814 #endif
1815     it.Next();
1816     for (; it.More(); it.Next()) {
1817 #ifdef OCCT_DEBUG_WIN 
1818       TDF_Tool::Entry(it.Value()->Label(), entry);
1819 #endif
1820       MS.Clear();
1821       TNaming_NamingTool::CurrentShape (Valid, Forbiden,it.Value(),MS);// fill MS with last modifications of the it.Value()
1822       CS = MakeShape(MS); 
1823       TNaming_ShapesSet OS(CS,TopAbs_EDGE);
1824       aSet.Add(OS); //concatenate both shapesets
1825  
1826 #ifdef OCCT_DEBUG_WIN
1827       ii++;
1828       TCollection_AsciiString aNm = Nam + entry + "_" + ii + ".brep";
1829       DbgTools_Write(CS, aNm.ToCString());
1830       cout <<"Arg: Entry = " <<entry <<"  TShape = " << CS.TShape() <<endl;
1831 #endif
1832         }
1833
1834 #ifdef OCCT_DEBUG_WIN
1835     cout <<"WIREIN: " << " Internal Map ext = " << aSet.Map().Extent()<<endl;
1836     TopTools_MapIteratorOfMapOfShape it1 (aSet.Map());
1837     for (int i=1;it1.More();it1.Next(),i++) {
1838       cout << "Map("<<i<<"): TShape = " << it1.Key().TShape() << " Orient = " << it1.Key().Orientation() <<" Type = " <<
1839                   it1.Key().ShapeType()<<endl;
1840     }
1841     
1842     TopExp_Explorer exp(aCF, TopAbs_EDGE);
1843     for(int i =1;exp.More();exp.Next(), i++) {
1844      cout << "Context_Face("<<i<<"): TShape = " << exp.Current().TShape() << " Orient = " << exp.Current().Orientation() <<endl;
1845     }       
1846 #endif
1847 //end for edges
1848         
1849   for (TopoDS_Iterator itF(aCF); itF.More(); itF.Next()) {// find the expected wire in the face
1850     const TopoDS_Shape& S = itF.Value();//wire
1851         if(!S.IsNull()) {
1852 #ifdef OCCT_DEBUG_WIN    
1853       DbgTools_Write(S, "WireIN_S.brep");
1854       cout <<"WIREIN: ShapeType = " << S.ShapeType() << " TS = " << S.TShape()->This() <<endl;
1855 #endif       
1856       if(S.ShapeType() == TopAbs_WIRE) {
1857                 TopTools_MapOfShape aView;
1858                 Standard_Integer aNum(0x7FFFFFFF);
1859             for (TopoDS_Iterator it(S);it.More();it.Next())
1860           aView.Add(it.Value());// edges of wire of the face in map
1861
1862         TopTools_MapIteratorOfMapOfShape it (aSet.Map());
1863                 aNum = aView.Extent();
1864                 if(aNum == aSet.Map().Extent()) {
1865           for (;it.More();it.Next()) {
1866                         if(aView.Contains(it.Key())) {
1867                                 aNum--;
1868                         }
1869                   }
1870                 }
1871                 if(aNum == 0) {
1872                   B.Select(S, S);
1873               aResult = Standard_True;
1874               break;
1875                 }
1876           }
1877         }       
1878   } //
1879
1880   if(!aResult) {
1881         TopoDS_Wire anOuterWire;
1882     TNaming::OuterWire(TopoDS::Face(aCF), anOuterWire);
1883         if(!anOuterWire.IsNull()) {
1884       for (TopoDS_Iterator itF(aCF); itF.More(); itF.Next()) {
1885         const TopoDS_Shape& S = itF.Value();//wire
1886             if(!S.IsNull()&& S.ShapeType() == TopAbs_WIRE) {
1887                   if(S.IsEqual(anOuterWire)) continue;
1888                   B.Select(S, S);
1889                 }
1890           }
1891         }
1892   }
1893   }
1894   return aResult;
1895 }
1896 //===========================================================================
1897 //function : ShellIN
1898 //purpose  : to solve  SHELLIN name
1899 //===========================================================================
1900 static Standard_Boolean ShellIN(const TDF_Label&                L,
1901                                const TDF_LabelMap&             Valid,
1902                                const TNaming_ListOfNamedShape& Args, 
1903                                    const Handle(TNaming_NamedShape)& Stop,
1904                                    Standard_Integer Index)
1905 {
1906   Standard_Boolean aResult(Standard_False);
1907   if(!ValidArgs(Args)) 
1908           return aResult;
1909   TopTools_MapOfShape MS; 
1910   TDF_LabelMap        Forbiden;
1911   if (Args.Extent() < 1 ) 
1912     Standard_ConstructionError::Raise("TNaming_Name::Solve"); 
1913   const Handle(TNaming_NamedShape)& A = Args.First();
1914   TNaming_NamingTool::CurrentShape (Valid,Forbiden,A,MS);
1915   if (MS.Extent() != 1) return aResult;
1916   TopTools_MapIteratorOfMapOfShape itM(MS); 
1917   const TopoDS_Shape& aCSO = itM.Key()   ;
1918 #ifdef OCCT_DEBUG_SHELL
1919   cout <<"MS Extent = " <<MS.Extent() <<endl;
1920   DbgTools_Write(aCSO, "Context_Solid.brep");
1921 #endif
1922   TNaming_Builder B(L);
1923   if(Index == 1 ){ //Outer Shell case  
1924         TopoDS_Shell anOuterShell;
1925         TNaming::OuterShell(TopoDS::Solid(aCSO), anOuterShell);
1926         if(!anOuterShell.IsNull()) {
1927       B.Select(anOuterShell, anOuterShell);
1928           aResult = Standard_True;
1929 #ifdef OCCT_DEBUG_SHELL      
1930           cout << "Outer Shell case" <<endl;
1931       PrintEntry(L);
1932           DbgTools_Write(anOuterShell, "ShellOut_S.brep");
1933           it.Initialize(aCSO);
1934                 for(;it.More();it.Next()){ 
1935           DbgTools_Write(it.Value(), "ShOut_S.brep");
1936                 }
1937 #endif       
1938         }
1939   } else { //has internal Shells
1940         TNaming_ListOfNamedShape ArgsF;
1941     ArgsF.Assign(Args);
1942     ArgsF.RemoveFirst();
1943         // fill Map with faces 
1944     TNaming_ListIteratorOfListOfNamedShape it(ArgsF);
1945     TopTools_MapOfShape MS; 
1946     TDF_LabelMap        Forbiden;
1947   
1948     TNaming_NamingTool::BuildDescendants (Stop, Forbiden);//fill Forbidden
1949     TNaming_NamingTool::CurrentShape  (Valid, Forbiden,it.Value(),MS); // fill MS with last modifications of the first additional argument
1950     TopoDS_Shape  CS = MakeShape(MS);
1951
1952     TNaming_ShapesSet aSet(CS,TopAbs_FACE);//fill internal map of shapeset by shapes of the specified type
1953 #ifdef OCCT_DEBUG_SHELL
1954     TCollection_AsciiString entry; 
1955     TDF_Tool::Entry(it.Value()->Label(), entry);
1956     TCollection_AsciiString Nam("Arg_");
1957     TCollection_AsciiString aNam = Nam + entry + "_" + "2.brep";
1958     DbgTools_Write(CS, aNam.ToCString());
1959     Standard_Integer ii = 2;
1960 #endif
1961     it.Next();
1962     for (; it.More(); it.Next()) {
1963 #ifdef OCCT_DEBUG_SHELL 
1964       TDF_Tool::Entry(it.Value()->Label(), entry);
1965 #endif
1966       MS.Clear();
1967       TNaming_NamingTool::CurrentShape (Valid, Forbiden,it.Value(),MS);// fill MS with last modifications of the it.Value()
1968       CS = MakeShape(MS); 
1969       TNaming_ShapesSet OS(CS,TopAbs_FACE);
1970       aSet.Add(OS); //concatenate both shapesets
1971  
1972 #ifdef OCCT_DEBUG_SHELL
1973       ii++;
1974       TCollection_AsciiString aNm = Nam + entry + "_" + ii + ".brep";
1975       DbgTools_Write(CS, aNm.ToCString());
1976       cout <<"Arg: Entry = " <<entry <<"  TShape = " << CS.TShape() <<endl;
1977 #endif
1978         }
1979
1980 #ifdef OCCT_DEBUG_SHELL
1981     cout <<"SHELLIN: " << " Internal Map ext = " << aSet.Map().Extent()<<endl;
1982     TopTools_MapIteratorOfMapOfShape it1 (aSet.Map());
1983     for (int i=1;it1.More();it1.Next(),i++) {
1984       cout << "Map("<<i<<"): TShape = " << it1.Key().TShape() << " Orient = " << it1.Key().Orientation() <<" Type = " <<
1985                   it1.Key().ShapeType()<<endl;
1986     }
1987     
1988     TopExp_Explorer exp(aCSO, TopAbs_FACE);
1989     for(int i = 1;exp.More();exp.Next(), i++) {
1990      cout << "Context_Solid("<<i<<"): TShape = " << exp.Current().TShape() << " Orient = " << exp.Current().Orientation() <<endl;
1991     }       
1992 #endif
1993 //end for faces
1994         
1995   for (TopoDS_Iterator itS(aCSO); itS.More(); itS.Next()) {// find the expected shell in the solid
1996     const TopoDS_Shape& S = itS.Value();//shell
1997         if(!S.IsNull()) {
1998 #ifdef OCCT_DEBUG_SHELL    
1999       DbgTools_Write(S, "ShellIN_S.brep");
2000       cout <<"SHELLIN: ShapeType = " << S.ShapeType() << " TS = " << S.TShape()->This() <<endl;
2001 #endif       
2002           if(S.ShapeType() == TopAbs_SHELL) {
2003                 TopTools_MapOfShape aView;
2004                 Standard_Integer aNum(0x7FFFFFFF);
2005             for (TopoDS_Iterator it(S);it.More();it.Next())
2006           aView.Add(it.Value());// faces of shell of the solid in map
2007         
2008                 aNum = aView.Extent();
2009                 if(aNum == aSet.Map().Extent()) {
2010                   TopTools_MapIteratorOfMapOfShape it (aSet.Map());
2011           for (;it.More();it.Next()) {
2012                         if(aView.Contains(it.Key())) {
2013                                 aNum--;
2014                         }
2015                   }
2016                 }
2017                 if(aNum == 0) {
2018                   B.Select(S, S);
2019               aResult = Standard_True;
2020               break;
2021                 }
2022           }
2023         }       
2024   } //
2025
2026   if(!aResult) {    
2027         TopoDS_Shell anOuterShell; 
2028         TNaming::OuterShell(TopoDS::Solid(aCSO), anOuterShell);
2029         if(!anOuterShell.IsNull()) {
2030       for (TopoDS_Iterator itS(aCSO); itS.More(); itS.Next()) {
2031         const TopoDS_Shape& S = itS.Value();//shell
2032             if(!S.IsNull()&& S.ShapeType() == TopAbs_SHELL) {
2033                   if(S.IsEqual(anOuterShell)) continue;
2034                   B.Select(S, S);
2035                 }
2036           }
2037         }
2038   }
2039   }
2040   return aResult;
2041 }
2042 #ifdef OCCT_DEBUG
2043 //=======================================================================
2044 static  Standard_CString NameTypeToString (const TNaming_NameType Type)
2045 {
2046   switch(Type)
2047     {
2048     case  TNaming_UNKNOWN             : return "UNKNOWN";
2049     case  TNaming_IDENTITY            : return "IDENTITY";
2050     case  TNaming_MODIFUNTIL          : return "MODIFUNTIL";
2051     case  TNaming_GENERATION          : return "GENERATION";
2052     case  TNaming_INTERSECTION        : return "INTERSECTION";
2053     case  TNaming_UNION               : return "UNION";
2054     case  TNaming_SUBSTRACTION        : return "SUBSTRACTION";
2055     case  TNaming_CONSTSHAPE          : return "CONSTSHAPE";
2056     case  TNaming_FILTERBYNEIGHBOURGS : return "FILTERBYNEIGHBOURGS";
2057     case  TNaming_ORIENTATION         : return "ORIENTATION";
2058     case  TNaming_WIREIN              : return "WIREIN";
2059       default :
2060         Standard_DomainError::Raise("TNaming_NameType; enum term unknown ");
2061     }
2062   return "";
2063 }
2064 #endif
2065 //=======================================================================
2066 //function : Solve
2067 //purpose  : 
2068 //=======================================================================
2069
2070 Standard_Boolean TNaming_Name::Solve(const TDF_Label&    aLab,
2071                                      const TDF_LabelMap& Valid) const
2072 {
2073   Standard_Boolean Done = 0;
2074 #ifdef OCCT_DEBUG_WIN
2075   PrintEntry(aLab);
2076 #endif
2077   try {
2078   switch (myType) {
2079   case TNaming_UNKNOWN :
2080     {
2081       break;
2082     }  
2083   case TNaming_IDENTITY :
2084     {  
2085       Done = Identity(aLab,Valid,myArgs,myShapeType); 
2086       break;
2087     }
2088   case TNaming_MODIFUNTIL:
2089     {
2090       Done = ModifUntil (aLab,Valid,myArgs,myStop);
2091       break;
2092     }
2093   case TNaming_GENERATION:
2094     {
2095       Done = Generated (aLab,Valid,myArgs);
2096       break;
2097     }
2098   case TNaming_INTERSECTION:
2099     {
2100       Done = Intersection (aLab,Valid,myArgs,myStop,myShapeType,myIndex); 
2101       break;
2102     }
2103   case TNaming_UNION:
2104     {
2105       Done = Union (aLab,Valid,myArgs,myStop,myShapeType, myContextLabel);
2106       break;
2107     }
2108   case TNaming_SUBSTRACTION: 
2109     {
2110       Standard_NotImplemented::Raise();
2111 //      Done = Substraction (aLab,Valid,myArgs);
2112       break;
2113     }
2114   case TNaming_CONSTSHAPE:
2115     {
2116       Done = ConstShape (aLab,Valid,myArgs,myStop,myIndex);
2117       break;
2118     }
2119   case TNaming_FILTERBYNEIGHBOURGS:
2120     {
2121       Done = FilterByNeighbourgs (aLab,Valid,myArgs,myStop,myShapeType);
2122       break;
2123     }
2124   case TNaming_ORIENTATION: 
2125     {
2126       Done = ORientation (aLab,Valid,myArgs,myStop,myIndex);
2127       break;
2128     }
2129   case TNaming_WIREIN: 
2130     {
2131 #ifdef OCCT_DEBUG_WIN  
2132       cout << "Name::Solve: NameType = " << myType << "  ";
2133   PrintEntry(aLab);
2134 #endif
2135       Done = WireIN (aLab,Valid,myArgs,myStop,myIndex);
2136       break;
2137     }
2138 case TNaming_SHELLIN: 
2139     {
2140 #ifdef OCCT_DEBUG_SHELL
2141       cout << "Name::Solve: NameType = " << myType << "  ";
2142       PrintEntry(aLab);
2143 #endif
2144       Done = ShellIN (aLab,Valid,myArgs,myStop,myIndex);      
2145       break;
2146     }
2147   }
2148 } catch (...) {
2149 #ifdef OCCT_DEBUG
2150   cout << "Name::Solve: EXCEPTION==> NameType = " << NameTypeToString(myType) << "  ";
2151   PrintEntry(aLab);
2152 #endif
2153 }
2154   return Done;
2155 }
2156
2157 //=======================================================================
2158 //function : ContextLabel
2159 //purpose  : Set
2160 //=======================================================================
2161
2162 void TNaming_Name::ContextLabel(const TDF_Label& theLabel)
2163 {
2164   myContextLabel = theLabel;
2165 }
2166
2167 //=======================================================================
2168 //function : ContextLabel
2169 //purpose  : Get
2170 //=======================================================================
2171
2172 const TDF_Label&  TNaming_Name::ContextLabel() const
2173
2174   return myContextLabel;
2175 }
2176
2177 //=======================================================================
2178 //function : Orientation
2179 //purpose  : Set
2180 //=======================================================================
2181 void TNaming_Name::Orientation(const TopAbs_Orientation theOrientation) 
2182 {
2183   myOrientation = theOrientation;
2184 }
2185