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