summaryrefslogtreecommitdiff
path: root/src/TNaming/TNaming.cxx
blob: 9ea1979a4c38e7fbfec6ec1af9535e19c51bdbb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
// File:	TNaming.cxx
// Created:	Tue Jan 20 10:22:16 1998
// Author:	Yves FRICAUD
//		<yfr@claquox.paris1.matra-dtv.fr>


#include <TNaming.ixx>

#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Compound.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopTools_MapOfShape.hxx>
#include <TopTools_MapIteratorOfMapOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_DataMapOfShapeShape.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
#include <TopTools_DataMapOfOrientedShapeShape.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfOrientedShapeShape.hxx>
//
#include <TDF_Label.hxx>
#include <TDF_ChildIterator.hxx>
#include <TNaming_Builder.hxx>
#include <TNaming_NamedShape.hxx>
#include <TNaming_UsedShapes.hxx>
#include <TNaming_ShapesSet.hxx>
#include <TNaming_Iterator.hxx>
#include <TNaming_NewShapeIterator.hxx>
#include <TNaming_ListOfNamedShape.hxx>
#include <TNaming_ListIteratorOfListOfNamedShape.hxx>
#include <TNaming_RefShape.hxx>
#include <TNaming_DataMapOfShapePtrRefShape.hxx>
#include <TNaming_Tool.hxx>

// CopyShape
#include <TColStd_IndexedDataMapOfTransientTransient.hxx>
#include <TNaming_TranslateTool.hxx>
#include <TopLoc_Datum3D.hxx>

#define BUC60862
#ifdef DEB
//#define MDTV_DEB
//#define MDTV_DEB_FSET
#endif
#ifdef MDTV_DEB
#include <BRepTools.hxx>
#include <TCollection_AsciiString.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfOrientedShapeShape.hxx>
static void WriteS(const TopoDS_Shape& shape,
		      const Standard_CString filename) 
{
  char buf[256];
  if(strlen(filename) > 256) return;
#if defined WNT 
  strcpy_s (buf, filename);
#else
  strcpy (buf, filename);
#endif
  char* p = buf;
  while (*p) {
    if(*p == ':')
      *p = '-';
    p++;
  }
  ofstream save (buf);
  if(!save) 
    cout << "File " << buf << " was not created: rdstate = " << save.rdstate() << endl;
  save << "DBRep_DrawableShape" << endl << endl;
  if(!shape.IsNull()) BRepTools::Write(shape, save);
  save.close();
}
#endif
//=======================================================================
//function : MapShapes
//purpose  : TNaming
//=======================================================================

static void MapShapes(const TopoDS_Shape& SCible, const TopoDS_Shape& SSource, TopTools_DataMapOfShapeShape& M)
{
  M.Bind(SCible,SSource);
  TopoDS_Iterator icible(SCible);
  TopoDS_Iterator isource(SSource);
  while (icible.More()) {
    if (!M.IsBound(icible.Value())) MapShapes(icible.Value(),isource.Value(),M);
    icible.Next();
    isource.Next();
  }
}

//=======================================================================
//function : MapShapes
//purpose  : TNaming
//=======================================================================

static void MapShapes(const TDF_Label& LCible, const TDF_Label& LSource, TopTools_DataMapOfShapeShape& M)
{
  TNaming_Iterator icible(LCible);
  TNaming_Iterator isource(LSource);
  while (icible.More()) {
    if (!icible.OldShape().IsNull()) {
      if (!M.IsBound(icible.OldShape())) MapShapes(icible.OldShape(),isource.OldShape(),M);
    }
    if (!icible.NewShape().IsNull()) {
      if (!M.IsBound(icible.NewShape())) MapShapes(icible.NewShape(),isource.NewShape(),M);
    }
    icible.Next();
    isource.Next();
  }

  TDF_ChildIterator iccible(LCible);
  TDF_ChildIterator icsource(LSource);
  while (iccible.More()) {
    MapShapes(iccible.Value(),icsource.Value(),M);
    iccible.Next();
    icsource.Next();
  }
} 

//=======================================================================
//function : SubstituteShape
//purpose  : TNaming
//=======================================================================

void static SubstituteShape(const TopoDS_Shape& oldShape,
			    const TopoDS_Shape& newShape,
			    TNaming_DataMapOfShapePtrRefShape& amap) 
{
  if (oldShape.IsSame(newShape)) {
    cout <<"import_tool::Substitute : oldShape IsSame newShape"<<endl;
  }

  if (!amap.IsBound(oldShape)) {
      return;
  }
  TNaming_RefShape* pos;
  pos = amap.ChangeFind(oldShape);
  pos->Shape(newShape);
  amap.UnBind(oldShape);
  amap.Bind(newShape,pos);
}

//=======================================================================
//function : MakeShape
//purpose  : ANaming 
//=======================================================================

TopoDS_Shape TNaming::MakeShape (const TopTools_MapOfShape& MS) 
{  
  if (!MS.IsEmpty ()) {
    TopTools_MapIteratorOfMapOfShape it(MS);
    if (MS.Extent() == 1) {
      return it.Key();
    }
    else {
      TopoDS_Compound C;
      BRep_Builder B;
      B.MakeCompound(C);
      for (; it.More(); it.Next()){ 
	B.Add(C,it.Key());
      }
      return C;
    }
  }
  return TopoDS_Shape();  
}

//=======================================================================
//function : Substitute
//purpose  : TNaming
//=======================================================================

void TNaming::Substitute(const TDF_Label& LSource, 
			 const TDF_Label& LCible ,
			 TopTools_DataMapOfShapeShape&  M) 
{
//attention pour etre en parallele au niveau structure il faut que Lciblble corresponde au premier fils recopie
  MapShapes(LCible,LSource,M);
  Handle(TNaming_UsedShapes) US;
  LCible.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
  TNaming_DataMapOfShapePtrRefShape& amap = US->Map();
  for (TopTools_DataMapIteratorOfDataMapOfShapeShape It(M);It.More();It.Next()) {
    SubstituteShape(It.Key(),It.Value(),amap);
 }   
}  

//=======================================================================
//function : SubstituteSShape
//purpose  : 
//=======================================================================
Standard_Boolean TNaming::SubstituteSShape(const TDF_Label& Lab, const TopoDS_Shape& From, TopoDS_Shape& To)
{
  Handle(TNaming_UsedShapes) US;
  Lab.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
  TNaming_DataMapOfShapePtrRefShape& amap = US->Map();
  if (!amap.IsBound(To)) 
    return Standard_False;
  TNaming_RefShape* pos;
  pos = amap.ChangeFind(To);
  if(!amap.UnBind(To)) return Standard_False;
  //update shape
  To.Orientation(From.Orientation());
  pos->Shape(To);
  return amap.Bind(To, pos);
}

//=======================================================================
//function : Rebuild
//purpose  : TNaming
//=======================================================================

static Standard_Boolean  Rebuild (const TopoDS_Shape& S,
				  TopTools_DataMapOfShapeShape&  M)
{
  Standard_Boolean IsModified = Standard_False;
  if (M.IsBound(S)) return IsModified;
  
  BRep_Builder     B;
  TopoDS_Iterator  iteS (S.Oriented(TopAbs_FORWARD));
  
  // Reconstruction des sous shapes si necessaire.
  for (; iteS.More(); iteS.Next()) {
    const TopoDS_Shape& SS = iteS.Value();
    if (Rebuild (SS,M)) IsModified = Standard_True;
  }
  if (!IsModified) {
    M.Bind(S,S);
    return Standard_True;
  }

  // Reconstruction de S
  TopoDS_Shape NewS = S.Oriented(TopAbs_FORWARD);
  NewS.EmptyCopy();
  if (NewS.ShapeType() == TopAbs_EDGE) {
    Standard_Real f,l;
    BRep_Tool::Range(TopoDS::Edge(S),f,l);
    B.Range(TopoDS::Edge(NewS),f,l);
  }
  iteS.Initialize(S.Oriented(TopAbs_FORWARD));
  for (iteS.Initialize(S.Oriented(TopAbs_FORWARD)) ;iteS.More(); iteS.Next()) { 
    const TopoDS_Shape& OS = iteS.Value();
    const TopoDS_Shape& NS = M(OS);
    B.Add(NewS,NS.Oriented(OS.Orientation()));
  }
  M.Bind (S,NewS.Oriented(S.Orientation()));
  return IsModified;
}


//=======================================================================
//function : Update
//purpose  : TNaming
//=======================================================================

void TNaming::Update(const TDF_Label& L,
		     TopTools_DataMapOfShapeShape& M)

{
  //Reconstruction des shapes de L suite aux substitutions decrites dans la map M.
  // ex : si une face est remplacee par une autre il faut reconstruire les shapes
  //      qui contiennent cette face.
  Handle(TNaming_UsedShapes) US;
  L.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
  TNaming_DataMapOfShapePtrRefShape& amap = US->Map();

  for (TNaming_Iterator it(L); it.More(); it.Next()) {
    if (!it.OldShape().IsNull()) {
      const TopoDS_Shape& S = it.OldShape();
      if (!M.IsBound(S))
	Rebuild (S,M);
      SubstituteShape(S,M(S),amap);
    }
    if (!it.NewShape().IsNull()) {
      const TopoDS_Shape& S = it.NewShape();
      if (!M.IsBound(S)) 
	Rebuild (S,M);
      SubstituteShape(S,M(S),amap);
    }
  }

  // SI Les shapes dans les sous-labels sont des sous shapes des shapes de L
  // si les shapes de L n ont pas changes les shapes des sous-labels ne seront 
  // pas changes non plus.
  for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
    TNaming::Update (ciL.Value(),M);
  }
}

//=======================================================================
//function : BuilCompound
//purpose  : TNaming
//=======================================================================

static void BuildCompound(TopoDS_Compound& C,
			  const TDF_Label& L)
{
  BRep_Builder B;
  for (TNaming_Iterator it(L); it.More(); it.Next()) {
    if (!it.OldShape().IsNull()) {
      B.Add(C,it.OldShape());
    }
    if (!it.NewShape().IsNull()) {
      B.Add(C,it.NewShape());
    }
  }
  for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
    BuildCompound (C,ciL.Value());
  }
}

//=======================================================================
//function : Update
//purpose  : TNaming
//=======================================================================

static void BuildMap(const TDF_Label& L,
		     BRepBuilderAPI_Transform& Transformer,
		     TopTools_DataMapOfShapeShape& M)
{		   
  Handle(TNaming_UsedShapes) US;
  L.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
  for (TNaming_Iterator it(L); it.More(); it.Next()) {
    if (!it.OldShape().IsNull()) {
      const TopoDS_Shape& S = it.OldShape();
      M.Bind(S,Transformer.ModifiedShape(S));
    }
    if (!it.NewShape().IsNull()) {
      const TopoDS_Shape& S = it.NewShape();
      M.Bind(S,Transformer.ModifiedShape(S));
    }
  }
  for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
    BuildMap (ciL.Value(),Transformer,M);
  }
}   

//=======================================================================
//function : LoadNamedShape
//purpose  : TNaming
//=======================================================================

static void LoadNamedShape (TNaming_Builder& B, 
			    TNaming_Evolution Evol, 
			    const TopoDS_Shape& OS, 
			    const TopoDS_Shape& NS)
{    
  switch (Evol) {
  case TNaming_PRIMITIVE :
    {
      B.Generated(NS);
      break;
    }
  case TNaming_GENERATED :
    {
      B.Generated(OS,NS);
      break;
    }
  case TNaming_MODIFY : 
    {
      B.Modify(OS,NS);
      break;
    }
  case TNaming_DELETE : 
    {
      B.Delete (OS);
      break;
    }
  case TNaming_SELECTED :
    {
      B.Select(NS,OS);
    }
  default:
    break;
  }
}

//=======================================================================
//function : Displace
//purpose  : TNaming
//=======================================================================

void TNaming::Displace (const TDF_Label& L,
			const TopLoc_Location& Loc,
			const Standard_Boolean WithOld)
{  


  TopTools_ListOfShape Olds;
  TopTools_ListOfShape News;
  TNaming_Evolution    Evol;
  TNaming_Iterator     it(L);
  
  if (it.More()) {   // dp on continue de visiter les fils meme s'il y a pas de shape
    Evol = it.Evolution();
    for ( ; it.More(); it.Next()) {
      Olds.Append(it.OldShape());
      News.Append(it.NewShape());
      
    }
    
    TopTools_ListIteratorOfListOfShape itOlds(Olds);
    TopTools_ListIteratorOfListOfShape itNews(News);
    TNaming_Builder B(L);
    
    for ( ;itOlds.More() ; itOlds.Next(),itNews.Next()) {
      TopoDS_Shape OS,NS;
      const TopoDS_Shape& SO     = itOlds.Value();
      const TopoDS_Shape& SN     = itNews.Value();
      OS = SO;
      if (WithOld && !SO.IsNull()) OS = SO.Moved(Loc);
      if (!SN.IsNull()) NS = SN.Moved(Loc);
      
      LoadNamedShape ( B, Evol, OS, NS);
    }
  }
  for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
    Displace (ciL.Value(),Loc);
  }
}

//=======================================================================
//function : Replace
//purpose  : TNaming
//=======================================================================

static void Replace (const TDF_Label&                    L,
		     const TopTools_DataMapOfShapeShape& M)
{  
    
  TNaming_Evolution    Evol;
  TNaming_Iterator     it(L);
  
  if (!it.More()) return;
  Evol = it.Evolution();

  TNaming_Builder B(L);
  
  TopoDS_Shape OS,NS;

  for ( ; it.More(); it.Next()) {  
    if (!it.OldShape().IsNull()) {
      OS = it.OldShape();
      if (M.IsBound(OS)) OS = M(OS);
    }
    if (!it.NewShape().IsNull()) {
      NS = it.NewShape();
      if (M.IsBound(NS)) NS = M(NS);
    }
    LoadNamedShape ( B, Evol, OS, NS);
  }
  for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
    Replace (ciL.Value(),M);
  }
}

//=======================================================================
//function : Update
//purpose  : TNaming
//=======================================================================

void TNaming::Transform(const TDF_Label& L,
			const gp_Trsf&   T)
{

  //--------------------------------------------------------------------
  // Construction du compound qui contient tous les shapes sous le label
  // et ses fils.
  //--------------------------------------------------------------------
  TopoDS_Compound CompShape;
  BRep_Builder    B;
  B.MakeCompound(CompShape);

  BuildCompound (CompShape,L);

  //----------------------------
  // Transformation du compound.
  //-----------------------------
  BRepBuilderAPI_Transform Transformer(CompShape, T);

  //-----------------------------------------------------------
  //Remplacement des shapes initiaux par les shapes transformes.
  //-----------------------------------------------------------
  TopTools_DataMapOfShapeShape M;
  BuildMap (L,Transformer,M);
  Replace (L,M);
}

//=======================================================================
//function : IDList
//purpose  : TNaming
//=======================================================================

void TNaming::IDList(TDF_IDList& anIDList)
{ anIDList.Append(TNaming_NamedShape::GetID()); }


//=======================================================================
//function : Replicate
//purpose  : 
//=======================================================================

void TNaming::Replicate(const Handle(TNaming_NamedShape)& NS,
			const gp_Trsf& T,
			const TDF_Label& L)
{
  TopoDS_Shape SH = TNaming_Tool::CurrentShape(NS);
  TNaming::Replicate(SH, T, L);
}

//=======================================================================
//function : Replicate
//purpose  : TNaming
//=======================================================================

void TNaming::Replicate (const TopoDS_Shape& SH,
			 const gp_Trsf& T,
			 const TDF_Label& L)
{
  // transform
  BRepBuilderAPI_Transform opeTrsf(T);
  if (SH.ShapeType() == TopAbs_FACE || SH.ShapeType() == TopAbs_WIRE ) {  
    opeTrsf.Perform(SH, Standard_True); // pour le pattern de prism
  }
  else {
    opeTrsf.Perform(SH, Standard_False);
  }
  const TopoDS_Shape& newSH = opeTrsf.Shape();
  //BRepLib::UpdateTolerances(newSH, Standard_True);
  
  // principal shape

  TNaming_Builder Builder(L);
  Builder.Generated(SH, newSH);    
  
  // sub shape
  TopAbs_ShapeEnum SST = TopAbs_FACE;
  if (SH.ShapeType() == TopAbs_FACE || SH.ShapeType() == TopAbs_WIRE )  
    SST = TopAbs_EDGE;
  
  TNaming_Builder Builder2 (L.FindChild(1,Standard_True)); 
  for (TopExp_Explorer exp(SH, SST); exp.More(); exp.Next()) {
    const TopoDS_Shape& oldSubShape = exp.Current();
    const TopoDS_Shape& newSubShape = opeTrsf.ModifiedShape(oldSubShape);
    Builder2.Generated(oldSubShape, newSubShape);
  }
}


//=======================================================================
//function : ShapeCopy
//purpose  : TNaming
//=======================================================================

static TopoDS_Shape  ShapeCopy(const TopoDS_Shape& S, 
			       TopTools_DataMapOfShapeShape& M)
{
  if (S.IsNull())   return S;
  if (M.IsBound(S)) return M(S);
  //----------------------------
  //construction de la copie.
  // 1- copie des sous shapes.
  // 2- reconstruction du TShape
  //----------------------------
  BRep_Builder     B;
  TopoDS_Iterator  it (S.Oriented(TopAbs_FORWARD));

  for ( ; it.More(); it.Next()) {
    const TopoDS_Shape& SS    = it.Value();
    TopoDS_Shape        NewSS = ShapeCopy(SS,M);
  }
  TopoDS_Shape NewS = S.Oriented(TopAbs_FORWARD);
  NewS.EmptyCopy();
  if (NewS.ShapeType() == TopAbs_EDGE) {
    Standard_Real f,l;
    BRep_Tool::Range(TopoDS::Edge(S),f,l);
    B.Range(TopoDS::Edge(NewS),f,l);
  }
  for (it.Initialize(S.Oriented(TopAbs_FORWARD)) ;it.More(); it.Next()) { 
    const TopoDS_Shape& OS = it.Value();
    const TopoDS_Shape& NS = M(OS);
    B.Add(NewS,NS.Oriented(OS.Orientation()));
  }
  NewS.Orientation(S.Orientation());

  NewS.Free      (S.Free())      ;NewS.Modified(S.Modified());NewS.Checked (S.Checked());
  NewS.Orientable(S.Orientable());NewS.Closed  (S.Closed())  ;NewS.Infinite(S.Infinite());  
  NewS.Convex    (S.Convex());

  M.Bind (S,NewS);
  return NewS;
}

//=======================================================================
//function : ChangeShapes
//purpose  : TNaming
//=======================================================================

void TNaming::ChangeShapes(const TDF_Label&              L,
			   TopTools_DataMapOfShapeShape& M)
{
  TopTools_ListOfShape Olds;
  TopTools_ListOfShape News;

  Handle(TNaming_NamedShape) NS;
  L.FindAttribute(TNaming_NamedShape::GetID(),NS);

  if (!NS.IsNull()) {
    TNaming_Evolution Evol = NS->Evolution();
    for (TNaming_Iterator it(L); it.More(); it.Next()) { 
      const TopoDS_Shape& S1 = it.OldShape();
      const TopoDS_Shape& S2 = it.NewShape();
      Olds.Append(ShapeCopy(S1,M));News.Append(ShapeCopy(S2,M));
    }

    TopTools_ListIteratorOfListOfShape itOlds(Olds);
    TopTools_ListIteratorOfListOfShape itNews(News);

    TNaming_Builder B(L);
    
    for ( ;itOlds.More() ; itOlds.Next(),itNews.Next()) {
      LoadNamedShape ( B, Evol, itOlds.Value(), itNews.Value());
    }
  }

  for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) {
    ChangeShapes (ciL.Value(),M);
  }
}


//=======================================================================
//function : 
//purpose  : 
//=======================================================================

Standard_OStream& TNaming::Print (const TNaming_Evolution EVOL,  Standard_OStream& s)
{
  switch(EVOL){
  case TNaming_PRIMITIVE :
    {
      s <<"PRIMITIVE"; break;
    }
  case TNaming_GENERATED :
    {
      s <<"GENERATED"; break;
    }  
  case TNaming_MODIFY :
    {
      s <<"MODIFY"; break;
    }
  case TNaming_DELETE :
    {
      s <<"DELETE"; break;
    }
  case TNaming_SELECTED :
    {
      s <<"SELECTED"; break;
    }
    default :
      s << "UNKNOWN_Evolution"; break;
    }
  return s;
}



//=======================================================================
//function : 
//purpose  : 
//=======================================================================

Standard_OStream& TNaming::Print (const TNaming_NameType NAME,  Standard_OStream& s)
{

  switch (NAME) {
  case TNaming_UNKNOWN :
    {
      s <<"UNKNOWN"; break;
    }
  case TNaming_IDENTITY :
    {
      s <<"IDENTITY"; break;
    }
  case TNaming_MODIFUNTIL :
    {
      s <<"MODIFUNTIL"; break;
    }
  case TNaming_GENERATION :
    {
      s <<"GENERATION"; break;
    }
  case TNaming_INTERSECTION :
    {
      s <<"INTERSECTION"; break;
    }
  case TNaming_UNION:
    {
      s <<"UNION"; break;
    }
  case TNaming_SUBSTRACTION :
    {
      s <<"SUBSTRACTION"; break;
    }
  case TNaming_CONSTSHAPE :
    {
      s <<"CONSTSHAPE"; break;
    }
  case TNaming_FILTERBYNEIGHBOURGS:
    {
      s <<"FILTERBYNEIGHBOURGS"; break;
    }
  case TNaming_ORIENTATION:
    {
      s <<"ORIENTATION"; break;
    }
  case TNaming_WIREIN:
    {
      s <<"WIREIN"; break;
    }
    default :
      {
	s <<"UNKNOWN_NameType"; break;
      }
  } 
  return s;
}

#ifdef BUC60862

//=======================================================================
//function : Print
//purpose  : Prints UsedShapes.
//=======================================================================

Standard_OStream& TNaming::Print (const TDF_Label& ACCESS,  Standard_OStream& s) {
  Handle(TNaming_UsedShapes) US;
  if (!ACCESS.Root().FindAttribute(TNaming_UsedShapes::GetID(), US)) {
#ifdef DEB
    cout<<"TNaming::Print(US): Bad access"<<endl;
#endif
    return s;
  }
  return US->Dump(s);
}

#endif

//=======================================================================
//function : BuildMapIn
//purpose  : 
//=======================================================================
static void BuildMapIn(const TopoDS_Shape& Context, const TopAbs_ShapeEnum StopType, 
			 TopTools_DataMapOfOrientedShapeShape& Map)
{
  TopAbs_ShapeEnum aType;     
  if((Context.ShapeType() == TopAbs_SOLID || Context.ShapeType() == TopAbs_FACE) && (StopType - Context.ShapeType()) != 1)
    aType = (TopAbs_ShapeEnum)(Context.ShapeType() +2);
  else
    aType = (TopAbs_ShapeEnum)(Context.ShapeType()+1);
  for (TopExp_Explorer exp(Context,aType); exp.More(); exp.Next()) {
#ifdef MDTV_DEB
    if(!Map.Bind(exp.Current(), Context))
      cout << "Not bind = " <<exp.Current().ShapeType() <<endl; 
    else 
      cout <<"Bind = " <<exp.Current().ShapeType() << " to Context = " <<Context.ShapeType()<<endl;
#else
    Map.Bind(exp.Current(), Context);
#endif
    if(exp.Current().ShapeType() < StopType ) {
      BuildMapIn(exp.Current(), StopType, Map);
    }
  }
  // fix for NMT case
  if(Context.ShapeType() < StopType) {
    TopoDS_Iterator it(Context);
    for(;it.More();it.Next()) {
      if(it.Value().Orientation() != TopAbs_FORWARD && it.Value().Orientation() != TopAbs_REVERSED) {
	Map.Bind(it.Value(), Context);
	//cout << "INTERNAL || EXTERNAL Orientation found" <<endl;
      }
    }
  }
}
//=======================================================================
//function : BuildMapC0
//purpose  : builds data map: key - context, C0 - top context
//=======================================================================
static void BuildMapC0(const TopoDS_Shape& Context, const TopoDS_Shape& C0, const TopAbs_ShapeEnum StopType, 
		     TopTools_DataMapOfOrientedShapeShape& Map)
{
  TopoDS_Iterator anIt(Context);
  while(anIt.More()) {
    const TopoDS_Shape& aKey = anIt.Value();
#ifdef MDTV_DEB
    if(!Map.Bind(aKey, C0)) 
      cout << "Not bind = " <<aKey.ShapeType() <<endl;      
#else
    Map.Bind(aKey, C0);
#endif
    if(aKey.ShapeType() < StopType ) {
      if(aKey.ShapeType() < TopAbs_SOLID) {
	BuildMapC0(aKey, Context, StopType, Map);
      }
      else
	BuildMapIn(aKey, StopType, Map);
    }
    anIt.Next();
  }
}

//=======================================================================
//function : BuildMap
//purpose  : builds data map: key - context
//=======================================================================
static void BuildMap(const TopoDS_Shape& Context, const TopAbs_ShapeEnum StopType, 
		     TopTools_DataMapOfOrientedShapeShape& Map)
{
#ifdef MDTV_DEB
  TCollection_AsciiString Nam("Cnt_");
  Standard_Integer i = 0;
#endif
  TopoDS_Iterator anIt(Context);
  while(anIt.More()) {
    const TopoDS_Shape& aKey = anIt.Value();
#ifdef MDTV_DEB
    if(!Map.Bind(aKey, Context)) 
      cout << "Not bind = " <<aKey.ShapeType() <<endl;      
#else
    Map.Bind(aKey, Context);
#endif
    if(aKey.ShapeType() < StopType ) {
      if(aKey.ShapeType() < TopAbs_SOLID)
	BuildMapC0(aKey, Context, StopType, Map);
      else
	BuildMapIn(aKey, StopType, Map);
    }
    anIt.Next();
  }
}
//=======================================================================
//function : FindUniqueContext
//purpose  : Find unique context of selection
//=======================================================================
TopoDS_Shape TNaming::FindUniqueContext(const TopoDS_Shape& Selection, const TopoDS_Shape& Context)
{
  TopTools_DataMapOfOrientedShapeShape aMap; 
  BuildMap(Context, Selection.ShapeType(), aMap);
#ifdef MDTV_DEB
  TopTools_DataMapIteratorOfDataMapOfOrientedShapeShape it (aMap);
  for (;it.More();it.Next()) {
    cout <<"FindUniqueContext: Key - " <<it.Key().ShapeType()<< " " << it.Key().TShape() <<" OR = " <<it.Key().Orientation() <<
      "  Context - " << it.Value().ShapeType() << "  " << it.Value().TShape()  << " OR = " <<it.Value().Orientation() <<endl;
  }
#endif
  if(aMap.IsBound(Selection))
    return aMap.Find(Selection); 
  return TopoDS_Shape();
}

//=======================================================================
//function : FindUniqueContexts
//purpose  : Find unique context of selection which is pure concatenation 
//         : of atomic shapes (Compound)
//=======================================================================
TopoDS_Shape TNaming::FindUniqueContextSet(const TopoDS_Shape& Selection, const TopoDS_Shape& Context,
					   Handle(TopTools_HArray1OfShape)& Arr)
{
  if(Selection.ShapeType() == TopAbs_COMPOUND) {
    TopTools_DataMapOfOrientedShapeShape aMap;
    Standard_Integer Up(0);
    TopAbs_ShapeEnum aStopType(TopAbs_COMPOUND);
    TopoDS_Iterator it(Selection);
    for(;it.More(); it.Next()) {
      const TopoDS_Shape& aS = it.Value(); 
      if(aS.ShapeType() > aStopType)
	aStopType = aS.ShapeType();	
      Up++;
    }
    if(Up > 0) 
      Arr = new  TopTools_HArray1OfShape(1, Up);
    if(aStopType == TopAbs_SHAPE)
      aStopType = Selection.ShapeType();
    BuildMap(Context, aStopType, aMap);
    if(aMap.IsBound(Selection))
      return aMap.Find(Selection);
    else if(Selection.ShapeType() == TopAbs_COMPOUND) {
      Standard_Integer num1(0),num2(0);
      TopoDS_Compound CompShape;
      BRep_Builder    B;
      B.MakeCompound(CompShape);
      TopoDS_Iterator it(Selection);
      TopTools_MapOfShape aView;
      for(;it.More(); it.Next(),num1++) {
	if(aMap.IsBound(it.Value())) {
	  if(aView.Add(aMap.Find(it.Value()))) {
	    B.Add(CompShape, aMap.Find(it.Value()));	    
	  }
	  if(!Arr.IsNull()) 
	    Arr->SetValue(num1+1, aMap.Find(it.Value()));
	  
	  if(aMap.Find(it.Value()) == Context)
	    num2++;
	}
      }
      if(num1 == num2 && num2)
	return Context;
      else {
	TopoDS_Iterator it(CompShape);
	Standard_Integer n(0);
	TopoDS_Shape aCmp;
	for(;it.More();it.Next()) {
	    n++;
	    aCmp = it.Value();
	}
	if(n == 1) {
#ifdef MDTV_DEB_FSET
	  cout << "FindUniqueContextSet: n = " << n <<endl;
#endif
	  return aCmp;
	}
	return CompShape;
      }
    }
  }
  return TopoDS_Shape();
}