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