0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / TNaming / TNaming.cxx
1 // Created on: 1998-01-20
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.ixx>
18
19 #include <BRep_Builder.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepBuilderAPI_Transform.hxx>
22 #include <TopoDS.hxx>
23 #include <TopoDS_Shape.hxx>
24 #include <TopoDS_Compound.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <TopoDS_Iterator.hxx>
27 #include <TopTools_MapOfShape.hxx>
28 #include <TopTools_MapIteratorOfMapOfShape.hxx>
29 #include <TopTools_ListOfShape.hxx>
30 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 #include <TopTools_DataMapOfShapeShape.hxx>
32 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
33 #include <TopTools_DataMapOfOrientedShapeShape.hxx>
34 #include <TopTools_DataMapIteratorOfDataMapOfOrientedShapeShape.hxx>
35 #include <TDF_Label.hxx>
36 #include <TDF_ChildIterator.hxx>
37 #include <TNaming_Builder.hxx>
38 #include <TNaming_NamedShape.hxx>
39 #include <TNaming_UsedShapes.hxx>
40 #include <TNaming_ShapesSet.hxx>
41 #include <TNaming_Iterator.hxx>
42 #include <TNaming_NewShapeIterator.hxx>
43 #include <TNaming_ListOfNamedShape.hxx>
44 #include <TNaming_ListIteratorOfListOfNamedShape.hxx>
45 #include <TNaming_RefShape.hxx>
46 #include <TNaming_DataMapOfShapePtrRefShape.hxx>
47 #include <TNaming_Tool.hxx>
48
49 #include <IntTools_FClass2d.hxx>
50 #include <BRepClass3d_SolidClassifier.hxx>
51
52 // CopyShape
53 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
54 #include <TNaming_TranslateTool.hxx>
55 #include <TopLoc_Datum3D.hxx>
56
57 #define BUC60862
58 #ifdef DEB
59 //#define MDTV_DEB
60 //#define MDTV_DEB_FSET
61 #endif
62 #ifdef MDTV_DEB
63 #include <BRepTools.hxx>
64 #include <TCollection_AsciiString.hxx>
65 #include <TopTools_DataMapIteratorOfDataMapOfOrientedShapeShape.hxx>
66 static void WriteS(const TopoDS_Shape& shape,
67                       const Standard_CString filename) 
68 {
69   char buf[256];
70   if(strlen(filename) > 256) return;
71 #if defined WNT 
72   strcpy_s (buf, filename);
73 #else
74   strcpy (buf, filename);
75 #endif
76   char* p = buf;
77   while (*p) {
78     if(*p == ':')
79       *p = '-';
80     p++;
81   }
82   ofstream save (buf);
83   if(!save) 
84     cout << "File " << buf << " was not created: rdstate = " << save.rdstate() << endl;
85   save << "DBRep_DrawableShape" << endl << endl;
86   if(!shape.IsNull()) BRepTools::Write(shape, save);
87   save.close();
88 }
89 #endif
90 //=======================================================================
91 //function : MapShapes
92 //purpose  : TNaming
93 //=======================================================================
94
95 static void MapShapes(const TopoDS_Shape& SCible, const TopoDS_Shape& SSource, TopTools_DataMapOfShapeShape& M)
96 {
97   M.Bind(SCible,SSource);
98   TopoDS_Iterator icible(SCible);
99   TopoDS_Iterator isource(SSource);
100   while (icible.More()) {
101     if (!M.IsBound(icible.Value())) MapShapes(icible.Value(),isource.Value(),M);
102     icible.Next();
103     isource.Next();
104   }
105 }
106
107 //=======================================================================
108 //function : MapShapes
109 //purpose  : TNaming
110 //=======================================================================
111
112 static void MapShapes(const TDF_Label& LCible, const TDF_Label& LSource, TopTools_DataMapOfShapeShape& M)
113 {
114   TNaming_Iterator icible(LCible);
115   TNaming_Iterator isource(LSource);
116   while (icible.More()) {
117     if (!icible.OldShape().IsNull()) {
118       if (!M.IsBound(icible.OldShape())) MapShapes(icible.OldShape(),isource.OldShape(),M);
119     }
120     if (!icible.NewShape().IsNull()) {
121       if (!M.IsBound(icible.NewShape())) MapShapes(icible.NewShape(),isource.NewShape(),M);
122     }
123     icible.Next();
124     isource.Next();
125   }
126
127   TDF_ChildIterator iccible(LCible);
128   TDF_ChildIterator icsource(LSource);
129   while (iccible.More()) {
130     MapShapes(iccible.Value(),icsource.Value(),M);
131     iccible.Next();
132     icsource.Next();
133   }
134
135
136 //=======================================================================
137 //function : SubstituteShape
138 //purpose  : TNaming
139 //=======================================================================
140
141 static void SubstituteShape(const TopoDS_Shape& oldShape,
142                             const TopoDS_Shape& newShape,
143                             TNaming_DataMapOfShapePtrRefShape& amap) 
144 {
145   if (oldShape.IsSame(newShape)) {
146     cout <<"import_tool::Substitute : oldShape IsSame newShape"<<endl;
147   }
148
149   if (!amap.IsBound(oldShape)) {
150       return;
151   }
152   TNaming_RefShape* pos;
153   pos = amap.ChangeFind(oldShape);
154   pos->Shape(newShape);
155   amap.UnBind(oldShape);
156   amap.Bind(newShape,pos);
157 }
158
159 //=======================================================================
160 //function : MakeShape
161 //purpose  : ANaming 
162 //=======================================================================
163
164 TopoDS_Shape TNaming::MakeShape (const TopTools_MapOfShape& MS) 
165 {  
166   if (!MS.IsEmpty ()) {
167     TopTools_MapIteratorOfMapOfShape it(MS);
168     if (MS.Extent() == 1) {
169       return it.Key();
170     }
171     else {
172       TopoDS_Compound C;
173       BRep_Builder B;
174       B.MakeCompound(C);
175       for (; it.More(); it.Next()){ 
176         B.Add(C,it.Key());
177       }
178       return C;
179     }
180   }
181   return TopoDS_Shape();  
182 }
183
184 //=======================================================================
185 //function : Substitute
186 //purpose  : TNaming
187 //=======================================================================
188
189 void TNaming::Substitute(const TDF_Label& LSource, 
190                          const TDF_Label& LCible ,
191                          TopTools_DataMapOfShapeShape&  M) 
192 {
193 //attention pour etre en parallele au niveau structure il faut que Lciblble corresponde au premier fils recopie
194   MapShapes(LCible,LSource,M);
195   Handle(TNaming_UsedShapes) US;
196   LCible.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
197   TNaming_DataMapOfShapePtrRefShape& amap = US->Map();
198   for (TopTools_DataMapIteratorOfDataMapOfShapeShape It(M);It.More();It.Next()) {
199     SubstituteShape(It.Key(),It.Value(),amap);
200  }   
201 }  
202
203 //=======================================================================
204 //function : SubstituteSShape
205 //purpose  : 
206 //=======================================================================
207 Standard_Boolean TNaming::SubstituteSShape(const TDF_Label& Lab, const TopoDS_Shape& From, TopoDS_Shape& To)
208 {
209   Handle(TNaming_UsedShapes) US;
210   Lab.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
211   TNaming_DataMapOfShapePtrRefShape& amap = US->Map();
212   if (!amap.IsBound(To)) 
213     return Standard_False;
214   TNaming_RefShape* pos;
215   pos = amap.ChangeFind(To);
216   if(!amap.UnBind(To)) return Standard_False;
217   //update shape
218   To.Orientation(From.Orientation());
219   pos->Shape(To);
220   return amap.Bind(To, pos);
221 }
222
223 //=======================================================================
224 //function : Rebuild
225 //purpose  : TNaming
226 //=======================================================================
227
228 static Standard_Boolean  Rebuild (const TopoDS_Shape& S,
229                                   TopTools_DataMapOfShapeShape&  M)
230 {
231   Standard_Boolean IsModified = Standard_False;
232   if (M.IsBound(S)) return IsModified;
233   
234   BRep_Builder     B;
235   TopoDS_Iterator  iteS (S.Oriented(TopAbs_FORWARD));
236   
237   // Reconstruction des sous shapes si necessaire.
238   for (; iteS.More(); iteS.Next()) {
239     const TopoDS_Shape& SS = iteS.Value();
240     if (Rebuild (SS,M)) IsModified = Standard_True;
241   }
242   if (!IsModified) {
243     M.Bind(S,S);
244     return Standard_True;
245   }
246
247   // Reconstruction de S
248   TopoDS_Shape NewS = S.Oriented(TopAbs_FORWARD);
249   NewS.EmptyCopy();
250   if (NewS.ShapeType() == TopAbs_EDGE) {
251     Standard_Real f,l;
252     BRep_Tool::Range(TopoDS::Edge(S),f,l);
253     B.Range(TopoDS::Edge(NewS),f,l);
254   }
255   iteS.Initialize(S.Oriented(TopAbs_FORWARD));
256   for (iteS.Initialize(S.Oriented(TopAbs_FORWARD)) ;iteS.More(); iteS.Next()) { 
257     const TopoDS_Shape& OS = iteS.Value();
258     const TopoDS_Shape& NS = M(OS);
259     B.Add(NewS,NS.Oriented(OS.Orientation()));
260   }
261   M.Bind (S,NewS.Oriented(S.Orientation()));
262   return IsModified;
263 }
264
265
266 //=======================================================================
267 //function : Update
268 //purpose  : TNaming
269 //=======================================================================
270
271 void TNaming::Update(const TDF_Label& L,
272                      TopTools_DataMapOfShapeShape& M)
273
274 {
275   //Reconstruction des shapes de L suite aux substitutions decrites dans la map M.
276   // ex : si une face est remplacee par une autre il faut reconstruire les shapes
277   //      qui contiennent cette face.
278   Handle(TNaming_UsedShapes) US;
279   L.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
280   TNaming_DataMapOfShapePtrRefShape& amap = US->Map();
281
282   for (TNaming_Iterator it(L); it.More(); it.Next()) {
283     if (!it.OldShape().IsNull()) {
284       const TopoDS_Shape& S = it.OldShape();
285       if (!M.IsBound(S))
286         Rebuild (S,M);
287       SubstituteShape(S,M(S),amap);
288     }
289     if (!it.NewShape().IsNull()) {
290       const TopoDS_Shape& S = it.NewShape();
291       if (!M.IsBound(S)) 
292         Rebuild (S,M);
293       SubstituteShape(S,M(S),amap);
294     }
295   }
296
297   // SI Les shapes dans les sous-labels sont des sous shapes des shapes de L
298   // si les shapes de L n ont pas changes les shapes des sous-labels ne seront 
299   // pas changes non plus.
300   for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
301     TNaming::Update (ciL.Value(),M);
302   }
303 }
304
305 //=======================================================================
306 //function : BuilCompound
307 //purpose  : TNaming
308 //=======================================================================
309
310 static void BuildCompound(TopoDS_Compound& C,
311                           const TDF_Label& L)
312 {
313   BRep_Builder B;
314   for (TNaming_Iterator it(L); it.More(); it.Next()) {
315     if (!it.OldShape().IsNull()) {
316       B.Add(C,it.OldShape());
317     }
318     if (!it.NewShape().IsNull()) {
319       B.Add(C,it.NewShape());
320     }
321   }
322   for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
323     BuildCompound (C,ciL.Value());
324   }
325 }
326
327 //=======================================================================
328 //function : Update
329 //purpose  : TNaming
330 //=======================================================================
331
332 static void BuildMap(const TDF_Label& L,
333                      BRepBuilderAPI_Transform& Transformer,
334                      TopTools_DataMapOfShapeShape& M)
335 {                  
336   Handle(TNaming_UsedShapes) US;
337   L.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
338   for (TNaming_Iterator it(L); it.More(); it.Next()) {
339     if (!it.OldShape().IsNull()) {
340       const TopoDS_Shape& S = it.OldShape();
341       M.Bind(S,Transformer.ModifiedShape(S));
342     }
343     if (!it.NewShape().IsNull()) {
344       const TopoDS_Shape& S = it.NewShape();
345       M.Bind(S,Transformer.ModifiedShape(S));
346     }
347   }
348   for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
349     BuildMap (ciL.Value(),Transformer,M);
350   }
351 }   
352
353 //=======================================================================
354 //function : LoadNamedShape
355 //purpose  : TNaming
356 //=======================================================================
357
358 static void LoadNamedShape (TNaming_Builder& B, 
359                             TNaming_Evolution Evol, 
360                             const TopoDS_Shape& OS, 
361                             const TopoDS_Shape& NS)
362 {    
363   switch (Evol) {
364   case TNaming_PRIMITIVE :
365     {
366       B.Generated(NS);
367       break;
368     }
369   case TNaming_GENERATED :
370     {
371       B.Generated(OS,NS);
372       break;
373     }
374   case TNaming_MODIFY : 
375     {
376       B.Modify(OS,NS);
377       break;
378     }
379   case TNaming_DELETE : 
380     {
381       B.Delete (OS);
382       break;
383     }
384   case TNaming_SELECTED :
385     {
386       B.Select(NS,OS);
387           break;
388     }
389   default:
390     break;
391   }
392 }
393
394 //=======================================================================
395 //function : Displace
396 //purpose  : TNaming
397 //=======================================================================
398
399 void TNaming::Displace (const TDF_Label& L,
400                         const TopLoc_Location& Loc,
401                         const Standard_Boolean WithOld)
402 {  
403
404
405   TopTools_ListOfShape Olds;
406   TopTools_ListOfShape News;
407   TNaming_Evolution    Evol;
408   TNaming_Iterator     it(L);
409   
410   if (it.More()) {   // dp on continue de visiter les fils meme s'il y a pas de shape
411     Evol = it.Evolution();
412     for ( ; it.More(); it.Next()) {
413       Olds.Append(it.OldShape());
414       News.Append(it.NewShape());
415       
416     }
417     
418     TopTools_ListIteratorOfListOfShape itOlds(Olds);
419     TopTools_ListIteratorOfListOfShape itNews(News);
420     TNaming_Builder B(L);
421     
422     for ( ;itOlds.More() ; itOlds.Next(),itNews.Next()) {
423       TopoDS_Shape OS,NS;
424       const TopoDS_Shape& SO     = itOlds.Value();
425       const TopoDS_Shape& SN     = itNews.Value();
426       OS = SO;
427       if (WithOld && !SO.IsNull()) OS = SO.Moved(Loc);
428       if (!SN.IsNull()) NS = SN.Moved(Loc);
429       
430       LoadNamedShape ( B, Evol, OS, NS);
431     }
432   }
433   for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
434     Displace (ciL.Value(),Loc);
435   }
436 }
437
438 //=======================================================================
439 //function : Replace
440 //purpose  : TNaming
441 //=======================================================================
442
443 static void Replace (const TDF_Label&                    L,
444                      const TopTools_DataMapOfShapeShape& M)
445 {  
446     
447   TNaming_Evolution    Evol;
448   TNaming_Iterator     it(L);
449   
450   if (!it.More()) return;
451   Evol = it.Evolution();
452
453   TNaming_Builder B(L);
454   
455   TopoDS_Shape OS,NS;
456
457   for ( ; it.More(); it.Next()) {  
458     if (!it.OldShape().IsNull()) {
459       OS = it.OldShape();
460       if (M.IsBound(OS)) OS = M(OS);
461     }
462     if (!it.NewShape().IsNull()) {
463       NS = it.NewShape();
464       if (M.IsBound(NS)) NS = M(NS);
465     }
466     LoadNamedShape ( B, Evol, OS, NS);
467   }
468   for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
469     Replace (ciL.Value(),M);
470   }
471 }
472
473 //=======================================================================
474 //function : Update
475 //purpose  : TNaming
476 //=======================================================================
477
478 void TNaming::Transform(const TDF_Label& L,
479                         const gp_Trsf&   T)
480 {
481
482   //--------------------------------------------------------------------
483   // Construction du compound qui contient tous les shapes sous le label
484   // et ses fils.
485   //--------------------------------------------------------------------
486   TopoDS_Compound CompShape;
487   BRep_Builder    B;
488   B.MakeCompound(CompShape);
489
490   BuildCompound (CompShape,L);
491
492   //----------------------------
493   // Transformation du compound.
494   //-----------------------------
495   BRepBuilderAPI_Transform Transformer(CompShape, T);
496
497   //-----------------------------------------------------------
498   //Remplacement des shapes initiaux par les shapes transformes.
499   //-----------------------------------------------------------
500   TopTools_DataMapOfShapeShape M;
501   BuildMap (L,Transformer,M);
502   Replace (L,M);
503 }
504
505 //=======================================================================
506 //function : IDList
507 //purpose  : TNaming
508 //=======================================================================
509
510 void TNaming::IDList(TDF_IDList& anIDList)
511 { anIDList.Append(TNaming_NamedShape::GetID()); }
512
513
514 //=======================================================================
515 //function : Replicate
516 //purpose  : 
517 //=======================================================================
518
519 void TNaming::Replicate(const Handle(TNaming_NamedShape)& NS,
520                         const gp_Trsf& T,
521                         const TDF_Label& L)
522 {
523   TopoDS_Shape SH = TNaming_Tool::CurrentShape(NS);
524   TNaming::Replicate(SH, T, L);
525 }
526
527 //=======================================================================
528 //function : Replicate
529 //purpose  : TNaming
530 //=======================================================================
531
532 void TNaming::Replicate (const TopoDS_Shape& SH,
533                          const gp_Trsf& T,
534                          const TDF_Label& L)
535 {
536   // transform
537   BRepBuilderAPI_Transform opeTrsf(T);
538   if (SH.ShapeType() == TopAbs_FACE || SH.ShapeType() == TopAbs_WIRE ) {  
539     opeTrsf.Perform(SH, Standard_True); // pour le pattern de prism
540   }
541   else {
542     opeTrsf.Perform(SH, Standard_False);
543   }
544   const TopoDS_Shape& newSH = opeTrsf.Shape();
545   //BRepLib::UpdateTolerances(newSH, Standard_True);
546   
547   // principal shape
548
549   TNaming_Builder Builder(L);
550   Builder.Generated(SH, newSH);    
551   
552   // sub shape
553   TopAbs_ShapeEnum SST = TopAbs_FACE;
554   if (SH.ShapeType() == TopAbs_FACE || SH.ShapeType() == TopAbs_WIRE )  
555     SST = TopAbs_EDGE;
556   
557   TNaming_Builder Builder2 (L.FindChild(1,Standard_True)); 
558   for (TopExp_Explorer exp(SH, SST); exp.More(); exp.Next()) {
559     const TopoDS_Shape& oldSubShape = exp.Current();
560     const TopoDS_Shape& newSubShape = opeTrsf.ModifiedShape(oldSubShape);
561     Builder2.Generated(oldSubShape, newSubShape);
562   }
563 }
564
565
566 //=======================================================================
567 //function : ShapeCopy
568 //purpose  : TNaming
569 //=======================================================================
570
571 static TopoDS_Shape  ShapeCopy(const TopoDS_Shape& S, 
572                                TopTools_DataMapOfShapeShape& M)
573 {
574   if (S.IsNull())   return S;
575   if (M.IsBound(S)) return M(S);
576   //----------------------------
577   //construction de la copie.
578   // 1- copie des sous shapes.
579   // 2- reconstruction du TShape
580   //----------------------------
581   BRep_Builder     B;
582   TopoDS_Iterator  it (S.Oriented(TopAbs_FORWARD));
583
584   for ( ; it.More(); it.Next()) {
585     const TopoDS_Shape& SS    = it.Value();
586     TopoDS_Shape        NewSS = ShapeCopy(SS,M);
587   }
588   TopoDS_Shape NewS = S.Oriented(TopAbs_FORWARD);
589   NewS.EmptyCopy();
590   if (NewS.ShapeType() == TopAbs_EDGE) {
591     Standard_Real f,l;
592     BRep_Tool::Range(TopoDS::Edge(S),f,l);
593     B.Range(TopoDS::Edge(NewS),f,l);
594   }
595   for (it.Initialize(S.Oriented(TopAbs_FORWARD)) ;it.More(); it.Next()) { 
596     const TopoDS_Shape& OS = it.Value();
597     const TopoDS_Shape& NS = M(OS);
598     B.Add(NewS,NS.Oriented(OS.Orientation()));
599   }
600   NewS.Orientation(S.Orientation());
601
602   NewS.Free      (S.Free())      ;NewS.Modified(S.Modified());NewS.Checked (S.Checked());
603   NewS.Orientable(S.Orientable());NewS.Closed  (S.Closed())  ;NewS.Infinite(S.Infinite());  
604   NewS.Convex    (S.Convex());
605
606   M.Bind (S,NewS);
607   return NewS;
608 }
609
610 //=======================================================================
611 //function : ChangeShapes
612 //purpose  : TNaming
613 //=======================================================================
614
615 void TNaming::ChangeShapes(const TDF_Label&              L,
616                            TopTools_DataMapOfShapeShape& M)
617 {
618   TopTools_ListOfShape Olds;
619   TopTools_ListOfShape News;
620
621   Handle(TNaming_NamedShape) NS;
622   L.FindAttribute(TNaming_NamedShape::GetID(),NS);
623
624   if (!NS.IsNull()) {
625     TNaming_Evolution Evol = NS->Evolution();
626     for (TNaming_Iterator it(L); it.More(); it.Next()) { 
627       const TopoDS_Shape& S1 = it.OldShape();
628       const TopoDS_Shape& S2 = it.NewShape();
629       Olds.Append(ShapeCopy(S1,M));News.Append(ShapeCopy(S2,M));
630     }
631
632     TopTools_ListIteratorOfListOfShape itOlds(Olds);
633     TopTools_ListIteratorOfListOfShape itNews(News);
634
635     TNaming_Builder B(L);
636     
637     for ( ;itOlds.More() ; itOlds.Next(),itNews.Next()) {
638       LoadNamedShape ( B, Evol, itOlds.Value(), itNews.Value());
639     }
640   }
641
642   for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
643     ChangeShapes (ciL.Value(),M);
644   }
645 }
646
647
648 //=======================================================================
649 //function : 
650 //purpose  : 
651 //=======================================================================
652
653 Standard_OStream& TNaming::Print (const TNaming_Evolution EVOL,  Standard_OStream& s)
654 {
655   switch(EVOL){
656   case TNaming_PRIMITIVE :
657     {
658       s <<"PRIMITIVE"; break;
659     }
660   case TNaming_GENERATED :
661     {
662       s <<"GENERATED"; break;
663     }  
664   case TNaming_MODIFY :
665     {
666       s <<"MODIFY"; break;
667     }
668   case TNaming_DELETE :
669     {
670       s <<"DELETE"; break;
671     }
672   case TNaming_SELECTED :
673     {
674       s <<"SELECTED"; break;
675     }
676     default :
677       s << "UNKNOWN_Evolution"; break;
678     }
679   return s;
680 }
681
682
683
684 //=======================================================================
685 //function : 
686 //purpose  : 
687 //=======================================================================
688
689 Standard_OStream& TNaming::Print (const TNaming_NameType NAME,  Standard_OStream& s)
690 {
691
692   switch (NAME) {
693   case TNaming_UNKNOWN :
694     {
695       s <<"UNKNOWN"; break;
696     }
697   case TNaming_IDENTITY :
698     {
699       s <<"IDENTITY"; break;
700     }
701   case TNaming_MODIFUNTIL :
702     {
703       s <<"MODIFUNTIL"; break;
704     }
705   case TNaming_GENERATION :
706     {
707       s <<"GENERATION"; break;
708     }
709   case TNaming_INTERSECTION :
710     {
711       s <<"INTERSECTION"; break;
712     }
713   case TNaming_UNION:
714     {
715       s <<"UNION"; break;
716     }
717   case TNaming_SUBSTRACTION :
718     {
719       s <<"SUBSTRACTION"; break;
720     }
721   case TNaming_CONSTSHAPE :
722     {
723       s <<"CONSTSHAPE"; break;
724     }
725   case TNaming_FILTERBYNEIGHBOURGS:
726     {
727       s <<"FILTERBYNEIGHBOURGS"; break;
728     }
729   case TNaming_ORIENTATION:
730     {
731       s <<"ORIENTATION"; break;
732     }
733   case TNaming_WIREIN:
734     {
735       s <<"WIREIN"; break;
736     }
737         case TNaming_SHELLIN:
738     {
739       s <<"SHELLIN"; break;
740     }
741     default :
742       {
743         s <<"UNKNOWN_NameType"; break;
744       }
745   } 
746   return s;
747 }
748
749 #ifdef BUC60862
750
751 //=======================================================================
752 //function : Print
753 //purpose  : Prints UsedShapes.
754 //=======================================================================
755
756 Standard_OStream& TNaming::Print (const TDF_Label& ACCESS,  Standard_OStream& s) {
757   Handle(TNaming_UsedShapes) US;
758   if (!ACCESS.Root().FindAttribute(TNaming_UsedShapes::GetID(), US)) {
759 #ifdef TNAMING_DEB
760     cout<<"TNaming::Print(US): Bad access"<<endl;
761 #endif
762     return s;
763   }
764   return US->Dump(s);
765 }
766
767 #endif
768
769 //=======================================================================
770 //function : BuildMapIn
771 //purpose  : 
772 //=======================================================================
773 static void BuildMapIn(const TopoDS_Shape& Context, const TopAbs_ShapeEnum StopType, 
774                          TopTools_DataMapOfOrientedShapeShape& Map)
775 {
776   TopAbs_ShapeEnum aType;     
777   if((Context.ShapeType() == TopAbs_SOLID || Context.ShapeType() == TopAbs_FACE) && (StopType - Context.ShapeType()) != 1)
778     aType = (TopAbs_ShapeEnum)(Context.ShapeType() +2);
779   else
780     aType = (TopAbs_ShapeEnum)(Context.ShapeType()+1);
781   for (TopExp_Explorer exp(Context,aType); exp.More(); exp.Next()) {
782 #ifdef MDTV_DEB
783     if(!Map.Bind(exp.Current(), Context))
784       cout << "Not bind = " <<exp.Current().ShapeType() <<endl; 
785     else 
786       cout <<"Bind = " <<exp.Current().ShapeType() << " to Context = " <<Context.ShapeType()<<endl;
787 #else
788     Map.Bind(exp.Current(), Context);
789 #endif
790     if(exp.Current().ShapeType() < StopType ) {
791       BuildMapIn(exp.Current(), StopType, Map);
792     }
793   }
794   // fix for NMT case
795   if(Context.ShapeType() < StopType) {
796     TopoDS_Iterator it(Context);
797     for(;it.More();it.Next()) {
798       if(it.Value().Orientation() != TopAbs_FORWARD && it.Value().Orientation() != TopAbs_REVERSED) {
799         Map.Bind(it.Value(), Context);
800         //cout << "INTERNAL || EXTERNAL Orientation found" <<endl;
801       }
802     }
803   }
804 }
805 //=======================================================================
806 //function : BuildMapC0
807 //purpose  : builds data map: key - context, C0 - top context
808 //=======================================================================
809 static void BuildMapC0(const TopoDS_Shape& Context, const TopoDS_Shape& C0, const TopAbs_ShapeEnum StopType, 
810                      TopTools_DataMapOfOrientedShapeShape& Map)
811 {
812   TopoDS_Iterator anIt(Context);
813   while(anIt.More()) {
814     const TopoDS_Shape& aKey = anIt.Value();
815 #ifdef MDTV_DEB
816     if(!Map.Bind(aKey, C0)) 
817       cout << "Not bind = " <<aKey.ShapeType() <<endl;      
818 #else
819     Map.Bind(aKey, C0);
820 #endif
821     if(aKey.ShapeType() < StopType ) {
822       if(aKey.ShapeType() < TopAbs_SOLID) {
823         BuildMapC0(aKey, Context, StopType, Map);
824       }
825       else
826         BuildMapIn(aKey, StopType, Map);
827     }
828     anIt.Next();
829   }
830 }
831
832 //=======================================================================
833 //function : BuildMap
834 //purpose  : builds data map: key - context
835 //=======================================================================
836 static void BuildMap(const TopoDS_Shape& Context, const TopAbs_ShapeEnum StopType, 
837                      TopTools_DataMapOfOrientedShapeShape& Map)
838 {
839 #ifdef MDTV_DEB
840   TCollection_AsciiString Nam("Cnt_");
841   Standard_Integer i = 0;
842 #endif
843   TopoDS_Iterator anIt(Context);
844   while(anIt.More()) {
845     const TopoDS_Shape& aKey = anIt.Value();
846 #ifdef MDTV_DEB
847     if(!Map.Bind(aKey, Context)) 
848       cout << "Not bind = " <<aKey.ShapeType() <<endl;      
849 #else
850     Map.Bind(aKey, Context);
851 #endif
852     if(aKey.ShapeType() < StopType ) {
853       if(aKey.ShapeType() < TopAbs_SOLID)
854         BuildMapC0(aKey, Context, StopType, Map);
855       else
856         BuildMapIn(aKey, StopType, Map);
857     }
858     anIt.Next();
859   }
860 }
861 //=======================================================================
862 //function : FindUniqueContext
863 //purpose  : Find unique context of selection
864 //=======================================================================
865 TopoDS_Shape TNaming::FindUniqueContext(const TopoDS_Shape& Selection, const TopoDS_Shape& Context)
866 {
867   TopTools_DataMapOfOrientedShapeShape aMap; 
868   BuildMap(Context, Selection.ShapeType(), aMap);
869 #ifdef MDTV_DEB
870   TopTools_DataMapIteratorOfDataMapOfOrientedShapeShape it (aMap);
871   for (;it.More();it.Next()) {
872     cout <<"FindUniqueContext: Key - " <<it.Key().ShapeType()<< " " << it.Key().TShape() <<" OR = " <<it.Key().Orientation() <<
873       "  Context - " << it.Value().ShapeType() << "  " << it.Value().TShape()  << " OR = " <<it.Value().Orientation() <<endl;
874   }
875 #endif
876   if(aMap.IsBound(Selection))
877     return aMap.Find(Selection); 
878   return TopoDS_Shape();
879 }
880
881 //=======================================================================
882 //function : FindUniqueContexts
883 //purpose  : Find unique context of selection which is pure concatenation 
884 //         : of atomic shapes (Compound)
885 //=======================================================================
886 TopoDS_Shape TNaming::FindUniqueContextSet(const TopoDS_Shape& Selection, const TopoDS_Shape& Context,
887                                            Handle(TopTools_HArray1OfShape)& Arr)
888 {
889   if(Selection.ShapeType() == TopAbs_COMPOUND) {
890     TopTools_DataMapOfOrientedShapeShape aMap;
891     Standard_Integer Up(0);
892     TopAbs_ShapeEnum aStopType(TopAbs_COMPOUND);
893     TopoDS_Iterator it(Selection);
894     for(;it.More(); it.Next()) {
895       const TopoDS_Shape& aS = it.Value(); 
896       if(aS.ShapeType() > aStopType)
897         aStopType = aS.ShapeType();     
898       Up++;
899     }
900     if(Up > 0) 
901       Arr = new  TopTools_HArray1OfShape(1, Up);
902     if(aStopType == TopAbs_SHAPE)
903       aStopType = Selection.ShapeType();
904     BuildMap(Context, aStopType, aMap);
905     if(aMap.IsBound(Selection))
906       return aMap.Find(Selection);
907     else if(Selection.ShapeType() == TopAbs_COMPOUND) {
908       Standard_Integer num1(0),num2(0);
909       TopoDS_Compound CompShape;
910       BRep_Builder    B;
911       B.MakeCompound(CompShape);
912       TopoDS_Iterator it(Selection);
913       TopTools_MapOfShape aView;
914       for(;it.More(); it.Next(),num1++) {
915         if(aMap.IsBound(it.Value())) {
916           if(aView.Add(aMap.Find(it.Value()))) {
917             B.Add(CompShape, aMap.Find(it.Value()));        
918           }
919           if(!Arr.IsNull()) 
920             Arr->SetValue(num1+1, aMap.Find(it.Value()));
921           
922           if(aMap.Find(it.Value()) == Context)
923             num2++;
924         }
925       }
926       if(num1 == num2 && num2)
927         return Context;
928       else {
929         TopoDS_Iterator it(CompShape);
930         Standard_Integer n(0);
931         TopoDS_Shape aCmp;
932         for(;it.More();it.Next()) {
933             n++;
934             aCmp = it.Value();
935         }
936         if(n == 1) {
937 #ifdef MDTV_DEB_FSET
938           cout << "FindUniqueContextSet: n = " << n <<endl;
939 #endif
940           return aCmp;
941         }
942         return CompShape;
943       }
944     }
945   }
946   return TopoDS_Shape();
947 }
948
949 //=======================================================================
950 //function : OuterWire
951 //purpose  : Returns True & <theWire> if Outer wire is found.
952 //=======================================================================
953 Standard_Boolean TNaming::OuterWire(const TopoDS_Face& theFace, TopoDS_Wire& theWire)
954
955   TopoDS_Face aFx;
956   TopoDS_Wire aWx;
957   BRep_Builder aBB;
958   IntTools_FClass2d aFC;
959   Standard_Boolean bFlag(Standard_False);
960   Standard_Real aTol = BRep_Tool::Tolerance(theFace);
961   TopoDS_Iterator aIt(theFace);
962   for (; aIt.More(); aIt.Next()) {
963     aWx=*((TopoDS_Wire*)&aIt.Value());
964     aFx = theFace;
965     aFx.EmptyCopy();
966     aBB.Add(aFx, aWx);
967     aFC.Init(aFx, aTol);
968     bFlag = aFC.IsHole();
969     if (!bFlag) 
970       break;
971   }
972   theWire=aWx;
973   return !bFlag;// if bFlag == True -  not found
974 }
975 //=======================================================================
976 //function : IsInternal
977 //purpose  :
978 //=======================================================================
979 static Standard_Boolean IsInternal(const TopoDS_Shape& aSx)
980 {   
981   TopAbs_Orientation aOr; 
982   Standard_Boolean bInternal(Standard_False);
983   TopoDS_Iterator aIt(aSx);
984   if(aIt.More()) {
985     const TopoDS_Shape& aSy = aIt.Value();
986     aOr = aSy.Orientation();
987     bInternal = (aOr == TopAbs_INTERNAL || aOr == TopAbs_EXTERNAL);    
988   }
989   return bInternal;
990 }
991 //=======================================================================
992 //function : OuterShell
993 //purpose  : returns True & <theShell>, if Outer shell is found
994 //=======================================================================
995 Standard_Boolean TNaming::OuterShell(const TopoDS_Solid& theSolid, 
996                                              TopoDS_Shell& theShell)
997 {
998   TopoDS_Solid aSDx;
999   TopoDS_Shell aSHx;
1000   TopAbs_State aState;
1001   Standard_Boolean bFound(Standard_False);
1002   Standard_Real aTol(1.e-7);
1003   //
1004   BRep_Builder aBB;
1005   BRepClass3d_SolidClassifier aSC;
1006   TopoDS_Iterator aIt(theSolid);
1007   for (; aIt.More(); aIt.Next()) {
1008     const TopoDS_Shape& aSx = aIt.Value();
1009
1010     if (aSx.ShapeType() != TopAbs_SHELL)
1011       continue;
1012     if (IsInternal(aSx)) 
1013       continue;
1014     //
1015     aSHx = *((TopoDS_Shell*)&aSx);
1016     //
1017     aSDx = theSolid;
1018     aSDx.EmptyCopy();
1019     //
1020     aBB.Add(aSDx, aSHx);
1021     //
1022     aSC.Load(aSDx);
1023     //BRepClass3d_SolidClassifier& aSC=aCtx.SolidClassifier(aSDx);
1024     aSC.PerformInfinitePoint(aTol);
1025     aState = aSC.State();
1026     if(aState == TopAbs_OUT) {
1027       bFound = Standard_True;
1028       break;
1029     }
1030   }
1031   theShell = aSHx;
1032
1033   return bFound;
1034 }
1035
1036
1037