5f12272dacc77497aa067b45b015eea206c77ce0
[occt.git] / src / MgtBRep / MgtBRep_TranslateTool.cxx
1 // Created on: 1993-06-14
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-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 <MgtBRep_TranslateTool.ixx>
18 #include <MgtTopLoc.hxx>
19 #include <MgtGeom.hxx>
20 #include <MgtGeom2d.hxx>
21 #include <MgtPoly.hxx>
22
23 #include <TopoDS.hxx>
24
25 #include <BRep_Builder.hxx>
26 #include <BRep_TVertex.hxx>
27 #include <BRep_TEdge.hxx>
28 #include <BRep_TFace.hxx>
29
30 #include <BRep_Curve3D.hxx>
31 #include <BRep_CurveOnSurface.hxx>
32 #include <BRep_CurveOnClosedSurface.hxx>
33 #include <BRep_CurveOn2Surfaces.hxx>
34 #include <BRep_Polygon3D.hxx>
35 #include <BRep_PolygonOnSurface.hxx>
36 #include <BRep_PolygonOnClosedSurface.hxx>
37 #include <BRep_PolygonOnTriangulation.hxx>
38 #include <BRep_PolygonOnClosedTriangulation.hxx>
39 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
40
41 #include <BRep_PointOnCurve.hxx>
42 #include <BRep_PointOnCurveOnSurface.hxx>
43 #include <BRep_PointOnSurface.hxx>
44 #include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
45
46 #include <PTopoDS_TWire.hxx>
47 #include <PTopoDS_TShell.hxx>
48 #include <PTopoDS_TSolid.hxx>
49 #include <PTopoDS_TCompSolid.hxx>
50 #include <PTopoDS_TCompound.hxx>
51
52 #include <PBRep_TVertex.hxx>
53 #include <PBRep_TEdge.hxx>
54 #include <PBRep_TFace.hxx>
55
56 #include <PBRep_Curve3D.hxx>
57 #include <PBRep_CurveOnSurface.hxx>
58 #include <PBRep_CurveOnClosedSurface.hxx>
59 #include <PBRep_CurveOn2Surfaces.hxx>
60
61 #include <PBRep_PointOnCurve.hxx>
62 #include <PBRep_PointOnCurveOnSurface.hxx>
63 #include <PBRep_PointOnSurface.hxx>
64 #include <PBRep_Polygon3D.hxx>
65 #include <PBRep_PolygonOnSurface.hxx>
66 #include <PBRep_PolygonOnClosedSurface.hxx>
67 #include <PBRep_PolygonOnTriangulation.hxx>
68 #include <PBRep_PolygonOnClosedTriangulation.hxx>
69
70 #include <Standard_NullObject.hxx>
71
72
73 // Used for testing DownCast time
74 #define MgtBRepSpeedDownCast
75
76 #ifdef chrono
77 #include <OSD_Timer.hxx>
78 extern OSD_Timer CurveTimer;
79 extern OSD_Timer Curve2dTimer;
80 extern OSD_Timer SurfaceTimer;
81 #endif
82
83 // *****************************
84 // Auxiliary translation methods
85 // *****************************
86
87 //=======================================================================
88 //function : Translate
89 //purpose  : Curve T->P
90 //=======================================================================
91
92 Handle(PGeom_Curve) MgtBRep_TranslateTool::Translate
93 (const Handle(Geom_Curve)& TP,
94  PTColStd_TransientPersistentMap& aMap) const 
95 {
96   Handle(PGeom_Curve) PP;
97   if (!TP.IsNull()) {
98     if (aMap.IsBound(TP)) {
99 #ifdef MgtBRepSpeedDownCast
100       Handle(Standard_Persistent) aPers = aMap.Find(TP);
101       PP = (Handle(PGeom_Curve)&) aPers;
102 #else
103       PP = Handle(PGeom_Curve)::DownCast(aMap.Find(TP));
104 #endif
105     }
106     else {
107       PP = MgtGeom::Translate(TP);
108       aMap.Bind(TP,PP);
109     }
110   }
111   return PP;
112 }
113
114 //=======================================================================
115 //function : Translate
116 //purpose  : Curve P->T
117 //=======================================================================
118
119 Handle(Geom_Curve) MgtBRep_TranslateTool::Translate
120 (const Handle(PGeom_Curve)& PP,
121  PTColStd_PersistentTransientMap& aMap) const  
122 {
123 #ifdef chrono
124   CurveTimer.Start();
125 #endif
126   Handle(Geom_Curve) TP;
127   if (!PP.IsNull()) {
128     if (aMap.IsBound(PP)) {
129 #ifdef MgtBRepSpeedDownCast
130       Handle(Standard_Transient) aTrans = aMap.Find(PP);
131       TP = (Handle(Geom_Curve)&) aTrans;
132 #else
133       TP = Handle(Geom_Curve)::DownCast(aMap.Find(PP));
134 #endif
135     }
136     else {
137       TP = MgtGeom::Translate(PP);
138       aMap.Bind(PP, TP);
139     }
140   }
141 #ifdef chrono
142   CurveTimer.Stop();
143 #endif
144   return TP;
145 }
146
147 //=======================================================================
148 //function : Translate
149 //purpose  : Curve2d T->P
150 //=======================================================================
151
152 Handle(PGeom2d_Curve) MgtBRep_TranslateTool::Translate
153 (const Handle(Geom2d_Curve)& TP,
154  PTColStd_TransientPersistentMap& aMap) const 
155 {
156   Handle(PGeom2d_Curve) PP;
157   if (!TP.IsNull()) {
158     if (aMap.IsBound(TP)) {
159 #ifdef MgtBRepSpeedDownCast
160       Handle(Standard_Persistent) aPers = aMap.Find(TP);
161       PP = (Handle(PGeom2d_Curve)&) aPers;
162 #else
163       PP = Handle(PGeom2d_Curve)::DownCast(aMap.Find(TP));
164 #endif
165     }
166     else {
167       PP = MgtGeom2d::Translate(TP);
168       aMap.Bind(TP,PP);
169     }
170   }
171   return PP;
172 }
173
174 //=======================================================================
175 //function : Translate
176 //purpose  : Curve2d P->T
177 //=======================================================================
178
179 Handle(Geom2d_Curve) MgtBRep_TranslateTool::Translate
180 (const Handle(PGeom2d_Curve)& PP,
181  PTColStd_PersistentTransientMap& aMap) const 
182 {
183 #ifdef chrono
184   Curve2dTimer.Start();
185 #endif
186   Handle(Geom2d_Curve) TP;
187   if (!PP.IsNull()) {
188     if (aMap.IsBound(PP)) {
189 #ifdef MgtBRepSpeedDownCast
190       Handle(Standard_Transient) aTrans = aMap.Find(PP);
191       TP = (Handle(Geom2d_Curve)&) aTrans;
192 #else
193       TP = Handle(Geom2d_Curve)::DownCast(aMap.Find(PP));
194 #endif
195     }
196     else {
197       TP = MgtGeom2d::Translate(PP);
198       aMap.Bind(PP, TP);
199     }
200   }
201 #ifdef chrono
202   Curve2dTimer.Stop();
203 #endif
204   return TP;
205 }
206
207 //=======================================================================
208 //function : Translate
209 //purpose  : Surface T->P
210 //=======================================================================
211
212 Handle(PGeom_Surface) MgtBRep_TranslateTool::Translate
213 (const Handle(Geom_Surface)& TP,
214  PTColStd_TransientPersistentMap& aMap) const 
215 {
216   Handle(PGeom_Surface) PP;
217   if (!TP.IsNull()) {
218     if (aMap.IsBound(TP)) {
219 #ifdef MgtBRepSpeedDownCast
220       Handle(Standard_Persistent) aPers = aMap.Find(TP);
221       PP = (Handle(PGeom_Surface)&) aPers;
222 #else
223       PP = Handle(PGeom_Surface)::DownCast(aMap.Find(TP));
224 #endif
225     }
226     else {
227       PP = MgtGeom::Translate(TP);
228       aMap.Bind(TP,PP);
229     }
230   }
231   return PP;
232 }
233
234 //=======================================================================
235 //function : Translate
236 //purpose  : Surface P->T
237 //=======================================================================
238
239 Handle(Geom_Surface) MgtBRep_TranslateTool::Translate
240 (const Handle(PGeom_Surface)& PP,
241  PTColStd_PersistentTransientMap& aMap) const 
242 {
243 #ifdef chrono
244   SurfaceTimer.Start();
245 #endif
246   Handle(Geom_Surface) TP;
247   if (!PP.IsNull()) {
248     if (aMap.IsBound(PP)) {
249 #ifdef MgtBRepSpeedDownCast
250       Handle(Standard_Transient) aTrans = aMap.Find(PP);
251       TP = (Handle(Geom_Surface)&) aTrans;
252 #else
253       TP = Handle(Geom_Surface)::DownCast(aMap.Find(PP));
254 #endif
255     }
256     else {
257       TP = MgtGeom::Translate(PP);
258       aMap.Bind(PP,TP);
259     }
260   }
261 #ifdef chrono
262   SurfaceTimer.Stop();
263 #endif
264   return TP;
265 }
266
267 //=======================================================================
268 //function : MgtBRep_TranslateTool
269 //purpose  : Constructor 
270 //=======================================================================
271
272 MgtBRep_TranslateTool::MgtBRep_TranslateTool
273 (const MgtBRep_TriangleMode aTriMode) : 
274 myTriangleMode(aTriMode)
275 {
276 }
277
278 //=======================================================================
279 //function : Add
280 //purpose  : 
281 //=======================================================================
282
283 void MgtBRep_TranslateTool::Add(TopoDS_Shape& S1,
284                                 const TopoDS_Shape& S2) const 
285 {
286   BRep_Builder B;
287   B.Add(S1,S2);
288 }
289
290 //=======================================================================
291 //function : MakeVertex
292 //purpose  : 
293 //=======================================================================
294
295 void  MgtBRep_TranslateTool::MakeVertex(TopoDS_Shape& S) const 
296 {
297   BRep_Builder B;
298   B.MakeVertex(TopoDS::Vertex(S));
299 }
300
301
302 //=======================================================================
303 //function : MakeVertex
304 //purpose  : 
305 //=======================================================================
306
307 void  MgtBRep_TranslateTool::MakeVertex(const Handle(PTopoDS_HShape)& S) const 
308 {
309   Handle(PBRep_TVertex) T = new PBRep_TVertex();
310   S->TShape(T);
311 }
312
313
314 //=======================================================================
315 //function : MakeEdge
316 //purpose  : 
317 //=======================================================================
318
319 void  MgtBRep_TranslateTool::MakeEdge(TopoDS_Shape& S) const 
320 {
321   BRep_Builder B;
322   B.MakeEdge(TopoDS::Edge(S));
323 }
324
325
326 //=======================================================================
327 //function : MakeEdge
328 //purpose  : 
329 //=======================================================================
330
331 void  MgtBRep_TranslateTool::MakeEdge(const Handle(PTopoDS_HShape)& S) const 
332 {
333   Handle(PBRep_TEdge) T = new PBRep_TEdge();
334   S->TShape(T);
335 }
336
337
338 //=======================================================================
339 //function : MakeWire
340 //purpose  : 
341 //=======================================================================
342
343 void  MgtBRep_TranslateTool::MakeWire(TopoDS_Shape& S) const 
344 {
345   BRep_Builder B;
346   B.MakeWire(TopoDS::Wire(S));
347 }
348
349
350 //=======================================================================
351 //function : MakeWire
352 //purpose  : 
353 //=======================================================================
354
355 void  MgtBRep_TranslateTool::MakeWire(const Handle(PTopoDS_HShape)& S) const 
356 {
357   Handle(PTopoDS_TWire) T = new PTopoDS_TWire();
358   S->TShape(T);
359 }
360
361
362 //=======================================================================
363 //function : MakeFace
364 //purpose  : 
365 //=======================================================================
366
367 void  MgtBRep_TranslateTool::MakeFace(TopoDS_Shape& S) const 
368 {
369   BRep_Builder B;
370   B.MakeFace(TopoDS::Face(S));
371 }
372
373
374 //=======================================================================
375 //function : MakeFace
376 //purpose  : 
377 //=======================================================================
378
379 void  MgtBRep_TranslateTool::MakeFace(const Handle(PTopoDS_HShape)& S) const 
380 {
381   Handle(PBRep_TFace) T = new PBRep_TFace();
382   S->TShape(T);
383 }
384
385
386 //=======================================================================
387 //function : MakeShell
388 //purpose  : 
389 //=======================================================================
390
391 void  MgtBRep_TranslateTool::MakeShell(TopoDS_Shape& S) const 
392 {
393   BRep_Builder B;
394   B.MakeShell(TopoDS::Shell(S));
395 }
396
397
398 //=======================================================================
399 //function : MakeShell
400 //purpose  : 
401 //=======================================================================
402
403 void  MgtBRep_TranslateTool::MakeShell(const Handle(PTopoDS_HShape)& S) const 
404 {
405   Handle(PTopoDS_TShell) T = new PTopoDS_TShell();
406   S->TShape(T);
407 }
408
409
410 //=======================================================================
411 //function : MakeSolid
412 //purpose  : 
413 //=======================================================================
414
415 void  MgtBRep_TranslateTool::MakeSolid(TopoDS_Shape& S) const 
416 {
417   BRep_Builder B;
418   B.MakeSolid(TopoDS::Solid(S));
419 }
420
421
422 //=======================================================================
423 //function : MakeSolid
424 //purpose  : 
425 //=======================================================================
426
427 void  MgtBRep_TranslateTool::MakeSolid(const Handle(PTopoDS_HShape)& S) const 
428 {
429   Handle(PTopoDS_TSolid) T = new PTopoDS_TSolid();
430   S->TShape(T);
431 }
432
433
434 //=======================================================================
435 //function : MakeCompSolid
436 //purpose  : 
437 //=======================================================================
438
439 void  MgtBRep_TranslateTool::MakeCompSolid(TopoDS_Shape& S) const 
440 {
441   BRep_Builder B;
442   B.MakeCompSolid(TopoDS::CompSolid(S));
443 }
444
445
446 //=======================================================================
447 //function : MakeCompSolid
448 //purpose  : 
449 //=======================================================================
450
451 void  
452 MgtBRep_TranslateTool::MakeCompSolid(const Handle(PTopoDS_HShape)& S) const 
453 {
454   Handle(PTopoDS_TCompSolid) T = new PTopoDS_TCompSolid();
455   S->TShape(T);
456 }
457
458
459 //=======================================================================
460 //function : MakeCompound
461 //purpose  : 
462 //=======================================================================
463
464 void  MgtBRep_TranslateTool::MakeCompound(TopoDS_Shape& S) const 
465 {
466   BRep_Builder B;
467   B.MakeCompound(TopoDS::Compound(S));
468 }
469
470
471 //=======================================================================
472 //function : MakeCompound
473 //purpose  : 
474 //=======================================================================
475
476 void  
477 MgtBRep_TranslateTool::MakeCompound(const Handle(PTopoDS_HShape)& S) const 
478 {
479   Handle(PTopoDS_TCompound) T = new PTopoDS_TCompound();
480   S->TShape(T);
481 }
482
483
484 //=======================================================================
485 //function : UpdateVertex
486 //purpose  : Transient->Persistent
487 //=======================================================================
488
489 void  MgtBRep_TranslateTool::UpdateVertex
490 (const TopoDS_Shape& S1,
491  const Handle(PTopoDS_HShape)& S2,
492  PTColStd_TransientPersistentMap& aMap) const 
493 {
494 #ifdef MgtBRepSpeedDownCast
495   Handle(TopoDS_TShape) aTransTShape = S1.TShape();
496   Handle(BRep_TVertex)& TTV = (Handle(BRep_TVertex)&) aTransTShape;
497   Handle(PTopoDS_TShape) aPersTShape = S2->TShape();
498   Handle(PBRep_TVertex)& PTV = (Handle(PBRep_TVertex)&) aPersTShape;
499 #else
500   Handle(BRep_TVertex)  TTV = Handle(BRep_TVertex)::DownCast(S1.TShape());
501   Handle(PBRep_TVertex) PTV = Handle(PBRep_TVertex)::DownCast(S2->TShape());
502 #endif
503
504   // Point
505   PTV->Pnt(TTV->Pnt());
506
507   // Tolerance
508   PTV->Tolerance(TTV->Tolerance());
509
510   // Representations
511   BRep_ListIteratorOfListOfPointRepresentation itpr(TTV->Points());
512
513   Handle(PBRep_PointRepresentation) PPR, CPPR;
514   while (itpr.More()) {
515
516     const Handle(BRep_PointRepresentation)& PR = itpr.Value();
517
518     if (PR->IsPointOnCurve()) {
519       Handle(PBRep_PointOnCurve) POC = new 
520         PBRep_PointOnCurve(PR->Parameter(),
521                            MgtBRep_TranslateTool::Translate(PR->Curve(), aMap),
522                            MgtTopLoc::Translate(PR->Location(), aMap));
523       CPPR = POC;
524     }
525
526     else if (PR->IsPointOnCurveOnSurface()) {
527       Handle(PBRep_PointOnCurveOnSurface) POCS = 
528         new PBRep_PointOnCurveOnSurface(PR->Parameter(),
529                                         MgtBRep_TranslateTool::Translate(PR->PCurve(), aMap),
530                                         MgtBRep_TranslateTool::Translate(PR->Surface(), aMap),
531                                         MgtTopLoc::Translate(PR->Location(), aMap));
532       CPPR = POCS;
533     }
534
535     else if (PR->IsPointOnSurface()) {
536       Handle(PBRep_PointOnSurface) POS = 
537         new PBRep_PointOnSurface(PR->Parameter(),
538                                  PR->Parameter2(),
539                                  MgtBRep_TranslateTool::Translate(PR->Surface(), aMap),
540                                  MgtTopLoc::Translate(PR->Location(), aMap));
541       CPPR = POS;
542     }
543     
544     CPPR->Next(PPR);
545     PPR = CPPR;
546     itpr.Next();
547   }
548   
549   PTV->Points(PPR);
550   
551   MgtTopoDS_TranslateTool::UpdateVertex(S1,S2, aMap);
552 }
553
554
555 //=======================================================================
556 //function : UpdateVertex
557 //purpose  : Persistent->Transient
558 //=======================================================================
559
560 void MgtBRep_TranslateTool::UpdateVertex
561 (const Handle(PTopoDS_HShape)& S1, 
562  TopoDS_Shape& S2,
563  PTColStd_PersistentTransientMap& aMap) const 
564 {
565 #ifdef MgtBRepSpeedDownCast
566   Handle(PTopoDS_TShape) aPersTShape = S1->TShape();
567   Handle(PBRep_TVertex)& PTV = (Handle(PBRep_TVertex)&) aPersTShape;
568
569   Handle(TopoDS_TShape) aTransTShape = S2.TShape();
570   Handle(BRep_TVertex)&  TTV = (Handle(BRep_TVertex)&) aTransTShape;
571 #else
572   Handle(PBRep_TVertex) PTV = Handle(PBRep_TVertex)::DownCast(S1->TShape());
573   Handle(BRep_TVertex)  TTV = Handle(BRep_TVertex)::DownCast(S2.TShape());
574 #endif
575
576   // Point
577   TTV->Pnt(PTV->Pnt());
578
579   // Tolerance
580   TTV->Tolerance(PTV->Tolerance());
581
582   // Representations
583   Handle(PBRep_PointRepresentation) PPR = PTV->Points();
584   BRep_ListOfPointRepresentation& lpr = TTV->ChangePoints();
585
586   lpr.Clear();
587
588   Handle(BRep_PointRepresentation) PR;
589
590   while (!PPR.IsNull()) {
591
592     if (PPR->IsPointOnCurve()) {
593       Handle(PBRep_PointOnCurve)& POC = (Handle(PBRep_PointOnCurve)&) PPR;
594       Handle(BRep_PointOnCurve) OC =
595         new BRep_PointOnCurve(POC->Parameter(),
596                               MgtBRep_TranslateTool::Translate(POC->Curve(), aMap),
597                               MgtTopLoc::Translate(POC->Location(), aMap));
598       PR = OC;
599     }
600     
601     else if (PPR->IsPointOnCurveOnSurface()) {
602       Handle(PBRep_PointOnCurveOnSurface)& POCS =
603         (Handle(PBRep_PointOnCurveOnSurface)&) PPR;
604       Handle(BRep_PointOnCurveOnSurface) OCS =
605         new BRep_PointOnCurveOnSurface(POCS->Parameter(),
606                                        MgtBRep_TranslateTool::Translate(POCS->PCurve(), aMap),
607                                        MgtBRep_TranslateTool::Translate(POCS->Surface(), aMap),
608                                        MgtTopLoc::Translate(POCS->Location(), aMap));
609       PR = OCS;
610     }
611     
612     else if (PPR->IsPointOnSurface()) {
613       Handle(PBRep_PointOnSurface)& POS = (Handle(PBRep_PointOnSurface)&) PPR;
614       Handle(BRep_PointOnSurface) OS =
615         new BRep_PointOnSurface(POS->Parameter(),
616                                 POS->Parameter2(),
617                                 MgtBRep_TranslateTool::Translate(POS->Surface(), aMap),
618                                 MgtTopLoc::Translate(POS->Location(), aMap));
619       PR = OS;
620     }
621     
622     lpr.Prepend(PR);
623     PPR = PPR->Next();
624   }
625   
626   MgtTopoDS_TranslateTool::UpdateVertex(S1,S2, aMap);
627 }
628
629
630 //=======================================================================
631 //function : UpdateEdge
632 //purpose  : Transient->Persistent
633 //=======================================================================
634
635 void  
636 MgtBRep_TranslateTool::UpdateEdge(const TopoDS_Shape& S1,
637                                   const Handle(PTopoDS_HShape)& S2,
638                                   PTColStd_TransientPersistentMap& aMap) const 
639 {
640 #ifdef MgtBRepSpeedDownCast
641   Handle(TopoDS_TShape) aTransTShape = S1.TShape();
642   Handle(BRep_TEdge)&  TTE = (Handle(BRep_TEdge)&) aTransTShape;
643
644   Handle(PTopoDS_TShape) aPersTShape = S2->TShape();
645   Handle(PBRep_TEdge)& PTE = (Handle(PBRep_TEdge)&) aPersTShape;
646 #else
647   Handle(BRep_TEdge)  TTE = Handle(BRep_TEdge)::DownCast(S1.TShape());
648   Handle(PBRep_TEdge) PTE = Handle(PBRep_TEdge)::DownCast(S2->TShape());
649 #endif
650
651   // tolerance
652   PTE->Tolerance(TTE->Tolerance());
653   
654   // same parameter
655   PTE->SameParameter(TTE->SameParameter());
656
657   // same range
658   PTE->SameRange(TTE->SameRange());
659
660   // Degenerated
661   PTE->Degenerated(TTE->Degenerated());
662   
663   // Representations
664   BRep_ListIteratorOfListOfCurveRepresentation itcr(TTE->Curves());
665
666   Handle(PBRep_CurveRepresentation) PCR, CPCR;
667   Handle(BRep_GCurve) GC;
668   Standard_Real f, l;
669
670   while (itcr.More()) {
671
672     const Handle(BRep_CurveRepresentation)& CR = itcr.Value();
673     
674     GC = Handle(BRep_GCurve)::DownCast(CR);
675     if (!GC.IsNull()) {
676       GC->Range(f, l);
677
678       // CurveRepresentation is Curve3D
679       if (CR->IsCurve3D()) {
680         Handle(PBRep_Curve3D) C3D = new 
681           PBRep_Curve3D(MgtBRep_TranslateTool::Translate(CR->Curve3D(),aMap),
682                         f, l, MgtTopLoc::Translate(CR->Location(), aMap));
683         CPCR = C3D;
684       }
685       
686       // CurveRepresentation is CurveOnSurface
687       else if (CR->IsCurveOnSurface()) {
688         
689         Handle(BRep_CurveOnSurface)& theCOS=(Handle(BRep_CurveOnSurface)&) CR;
690         Handle(PBRep_CurveOnSurface) COS;
691
692         // CurveRepresentation is CurveOnSurface
693         if (!CR->IsCurveOnClosedSurface()) {
694           COS =
695             new PBRep_CurveOnSurface
696               (MgtBRep_TranslateTool::Translate(CR->PCurve(), aMap),
697                f, l,
698                MgtBRep_TranslateTool::Translate(CR->Surface(), aMap),
699                MgtTopLoc::Translate(CR->Location(), aMap));
700         }
701         
702         // CurveRepresentation is CurveOnClosedSurface
703         else {
704           // get UVPoints for the CurveOnClosedSurface definition.
705           Handle(BRep_CurveOnClosedSurface)& theCOCS =
706             (Handle(BRep_CurveOnClosedSurface)&) CR;
707           gp_Pnt2d Pnt21, Pnt22;
708           theCOCS->UVPoints2(Pnt21, Pnt22);
709           Handle(PBRep_CurveOnClosedSurface) COCS =
710             new PBRep_CurveOnClosedSurface
711               (MgtBRep_TranslateTool::Translate(CR->PCurve(), aMap),
712                MgtBRep_TranslateTool::Translate(CR->PCurve2(), aMap),
713                f, l, 
714                MgtBRep_TranslateTool::Translate(CR->Surface(), aMap),
715                MgtTopLoc::Translate(CR->Location(), aMap),
716                CR->Continuity());
717           COCS->SetUVPoints2(Pnt21, Pnt22);
718           COS = COCS;
719         }
720
721         // get UVPoints for the CurveOnSurface definition.
722         gp_Pnt2d Pnt1, Pnt2;
723         theCOS->UVPoints(Pnt1, Pnt2);
724
725         // If we don't do that, there is anything (NAN: Not A Number) instead.
726         COS->SetUVPoints(Pnt1, Pnt2);
727         CPCR = COS;
728         
729       }
730     }
731     
732     // CurveRepresentation is CurveOn2Surfaces
733     else if (CR->IsRegularity()) {
734       
735       Handle(PBRep_CurveOn2Surfaces) R =
736         new PBRep_CurveOn2Surfaces
737           (MgtBRep_TranslateTool::Translate(CR->Surface(), aMap),
738            MgtBRep_TranslateTool::Translate(CR->Surface2(), aMap),
739            MgtTopLoc::Translate(CR->Location(), aMap),
740            MgtTopLoc::Translate(CR->Location2(), aMap),
741            CR->Continuity());
742       CPCR = R;
743     }
744     
745     // CurveRepresentation is Polygon or Triangulation
746     else if (myTriangleMode == MgtBRep_WithTriangle) {
747       
748       // CurveRepresentation is Polygon3D
749       if (CR->IsPolygon3D()) {
750         Handle(PBRep_Polygon3D) P3D = 
751           new PBRep_Polygon3D(MgtPoly::Translate(CR->Polygon3D(), aMap),
752                               MgtTopLoc::Translate(CR->Location(), aMap));
753         CPCR = P3D;
754       }
755
756       // CurveRepresentation is PolygonOnSurface
757       else if (CR->IsPolygonOnSurface()) {
758
759         // CurveRepresentation is PolygonOnClosedSurface
760         if (CR->IsPolygonOnClosedSurface()) {
761           Handle(PBRep_PolygonOnClosedSurface)  PolOCS = new 
762             PBRep_PolygonOnClosedSurface(MgtPoly::Translate(CR->Polygon(), aMap),
763                                          MgtPoly::Translate(CR->Polygon2(), aMap),
764                                          MgtBRep_TranslateTool::Translate(CR->Surface(), aMap),
765                                          MgtTopLoc::Translate(CR->Location(), aMap));
766           CPCR = PolOCS;
767         }
768
769         // CurveRepresentation is PolygonOnSurface
770         else {
771           Handle(PBRep_PolygonOnSurface) PolOS = new
772             PBRep_PolygonOnSurface(MgtPoly::Translate(CR->Polygon(), aMap),
773                                    MgtBRep_TranslateTool::Translate(CR->Surface(),aMap),
774                                    MgtTopLoc::Translate(CR->Location(), aMap));
775           CPCR = PolOS;
776         }
777       }
778
779       // CurveRepresentation is PolygonOnTriangulation
780       else if (CR->IsPolygonOnTriangulation()) {
781
782         // CurveRepresentation is PolygonOnClosedTriangulation
783         if (CR->IsPolygonOnClosedTriangulation()) {
784
785           Handle(PBRep_PolygonOnClosedTriangulation) PolOCT= new
786             PBRep_PolygonOnClosedTriangulation
787               (MgtPoly::Translate(CR->PolygonOnTriangulation(), aMap),
788                MgtPoly::Translate(CR->PolygonOnTriangulation2(), aMap),
789                MgtPoly::Translate(CR->Triangulation(), aMap),
790                MgtTopLoc::Translate(CR->Location(), aMap));
791           CPCR = PolOCT;
792         }
793
794         // CurveRepresentation is PolygonOnTriangulation
795         else {
796           Handle(PBRep_PolygonOnTriangulation) PolOT = new
797             PBRep_PolygonOnTriangulation
798               (MgtPoly::Translate(CR->PolygonOnTriangulation(), aMap),
799                MgtPoly::Translate(CR->Triangulation(), aMap),
800                MgtTopLoc::Translate(CR->Location(), aMap));
801           CPCR = PolOT;
802         }
803       }
804     }
805     else {
806       // jumps the curve representation
807       itcr.Next();
808       continue;
809     }
810     
811     Standard_NullObject_Raise_if (CPCR.IsNull(), "Null CurveRepresentation");
812     
813     CPCR->Next(PCR);
814     PCR = CPCR;
815     itcr.Next();
816   }
817   
818   // set
819   PTE->Curves(PCR);
820   
821   MgtTopoDS_TranslateTool::UpdateEdge(S1,S2, aMap);
822 }
823
824
825 //=======================================================================
826 //function : UpdateEdge
827 //purpose  : Persistent->Transient
828 //=======================================================================
829
830 void  
831 MgtBRep_TranslateTool::UpdateEdge(const Handle(PTopoDS_HShape)& S1,
832                                   TopoDS_Shape& S2,
833                                   PTColStd_PersistentTransientMap& aMap) const 
834 {
835 #ifdef MgtBRepSpeedDownCast
836   Handle(PTopoDS_TShape) aPersTShape = S1->TShape();
837   Handle(PBRep_TEdge)& PTE = (Handle(PBRep_TEdge)&) aPersTShape;
838
839   Handle(TopoDS_TShape) aTransTShape = S2.TShape();
840   Handle(BRep_TEdge)&  TTE = (Handle(BRep_TEdge)&) aTransTShape;
841 #else
842   Handle(PBRep_TEdge) PTE = Handle(PBRep_TEdge)::DownCast(S1->TShape());
843   Handle(BRep_TEdge)  TTE = Handle(BRep_TEdge)::DownCast(S2.TShape());
844 #endif
845
846   // tolerance
847   TTE->Tolerance(PTE->Tolerance());
848   
849   // same parameter
850   TTE->SameParameter(PTE->SameParameter());
851
852   // same range
853   TTE->SameRange(PTE->SameRange());
854
855   // Degenerated
856   TTE->Degenerated(PTE->Degenerated());
857   
858   // Representations
859   Handle(PBRep_CurveRepresentation) PCR = PTE->Curves();
860   BRep_ListOfCurveRepresentation& lcr = TTE->ChangeCurves();
861
862   lcr.Clear();
863   Handle(BRep_CurveRepresentation) CR;
864
865   while (!PCR.IsNull()) {
866     
867     if (PCR->IsGCurve()) {
868       Handle(PBRep_GCurve)& PGC = (Handle(PBRep_GCurve)&) PCR;
869       if (PCR->IsCurveOnSurface()) {
870         gp_Pnt2d Pnt1, Pnt2;
871         if (!PCR->IsCurveOnClosedSurface()) {
872           // CurveRepresentation is a PBRep_CurveOnSurface
873           Handle(PBRep_CurveOnSurface)& PCOS = (Handle(PBRep_CurveOnSurface)&) PCR;
874           Handle(BRep_CurveOnSurface) COS =
875             new BRep_CurveOnSurface
876               (MgtBRep_TranslateTool::Translate(PCOS->PCurve(), aMap),
877                MgtBRep_TranslateTool::Translate(PCOS->Surface(), aMap),
878                MgtTopLoc::Translate(PCOS->Location(), aMap));
879           Pnt1 = PCOS->FirstUV();
880           Pnt2 = PCOS->LastUV();
881           COS->SetUVPoints(Pnt1, Pnt2);
882           COS->SetRange(PGC->First(), PGC->Last());
883           CR = COS;
884         }
885         else {
886           // CurveRepresentation is a PBRep_CurveOnClosedSurface
887           gp_Pnt2d Pnt21, Pnt22;
888           Handle(PBRep_CurveOnClosedSurface)& PCOCS =
889             (Handle(PBRep_CurveOnClosedSurface)&) PCR;
890           Handle(BRep_CurveOnClosedSurface) COCS =
891             new BRep_CurveOnClosedSurface
892               (MgtBRep_TranslateTool::Translate(PCOCS->PCurve(), aMap),
893                MgtBRep_TranslateTool::Translate(PCOCS->PCurve2(), aMap),
894                MgtBRep_TranslateTool::Translate(PCOCS->Surface(), aMap),
895                MgtTopLoc::Translate(PCOCS->Location(), aMap),
896                PCOCS->Continuity());
897           Pnt21 = PCOCS->FirstUV2();
898           Pnt22 = PCOCS->LastUV2();
899           COCS->SetUVPoints(Pnt1, Pnt2); // Is it necessary?
900           COCS->SetUVPoints2(Pnt21, Pnt22);
901           COCS->SetRange(PGC->First(), PGC->Last());
902           CR = COCS;
903         }
904       }
905       else if (PCR->IsCurve3D()) {
906         Handle(PBRep_Curve3D)& PC3D = (Handle(PBRep_Curve3D)&) PCR;
907         if (! PC3D->Curve3D().IsNull()) {
908           Handle(BRep_Curve3D) C3D =
909             new BRep_Curve3D(MgtBRep_TranslateTool::Translate(PC3D->Curve3D(), aMap),
910                              MgtTopLoc::Translate(PC3D->Location(), aMap));
911           C3D->SetRange(PGC->First(), PGC->Last());
912           CR = C3D;
913         }
914       }
915     }
916     else if (PCR->IsRegularity()) {
917       Handle(PBRep_CurveOn2Surfaces)& PR = 
918         (Handle(PBRep_CurveOn2Surfaces)&) PCR;
919       Handle(BRep_CurveOn2Surfaces) R =
920         new BRep_CurveOn2Surfaces
921           (MgtBRep_TranslateTool::Translate(PR->Surface(), aMap),
922            MgtBRep_TranslateTool::Translate(PR->Surface2(), aMap),
923            MgtTopLoc::Translate(PR->Location(), aMap),
924            MgtTopLoc::Translate(PR->Location2(), aMap),
925            PR->Continuity());
926       CR = R;
927     }
928     
929     else if (myTriangleMode == MgtBRep_WithTriangle) {
930       if (PCR->IsPolygon3D()) {
931         Handle(PBRep_Polygon3D)& PP3D = (Handle(PBRep_Polygon3D)&) PCR;
932         Handle(BRep_Polygon3D) P3D = new
933           BRep_Polygon3D(MgtPoly::Translate(PP3D->Polygon3D(), aMap),
934                          MgtTopLoc::Translate(PP3D->Location(), aMap));
935         CR = P3D;
936       }
937       else if (PCR->IsPolygonOnSurface()) {
938         if (PCR->IsPolygonOnClosedSurface()) {
939           Handle(PBRep_PolygonOnClosedSurface)& PPol = 
940             (Handle(PBRep_PolygonOnClosedSurface)&) PCR;
941           Handle(BRep_PolygonOnClosedSurface)  PolOCS = new 
942             BRep_PolygonOnClosedSurface
943               (MgtPoly::Translate(PPol->Polygon(), aMap),
944                MgtPoly::Translate(PPol->Polygon2(), aMap),
945                MgtBRep_TranslateTool::Translate(PPol->Surface(), aMap),
946                MgtTopLoc::Translate(PPol->Location(), aMap));
947           CR = PolOCS;
948         }
949         else {
950           Handle(PBRep_PolygonOnSurface)& PPol = 
951             (Handle(PBRep_PolygonOnSurface)&) PCR;
952           Handle(BRep_PolygonOnSurface) PolOS = new
953             BRep_PolygonOnSurface
954               (MgtPoly::Translate(PPol->Polygon(), aMap),
955                MgtBRep_TranslateTool::Translate(PPol->Surface(), aMap),
956                MgtTopLoc::Translate(PPol->Location(), aMap));
957           CR = PolOS;
958         }
959       }
960       // PCR is a PolygonOnTriangulation
961       else if (PCR->IsPolygonOnTriangulation()) {
962         
963         Handle(PBRep_PolygonOnTriangulation)& POT =
964           (Handle(PBRep_PolygonOnTriangulation)&) PCR;
965         
966         // PCR is a PolygonOnClosedTriangulation
967         if (PCR->IsPolygonOnClosedTriangulation()) {
968           Handle(PBRep_PolygonOnClosedTriangulation)& POCT =
969             (Handle(PBRep_PolygonOnClosedTriangulation)&) PCR;
970           
971           Handle(BRep_PolygonOnClosedTriangulation) PolOCT= new
972             BRep_PolygonOnClosedTriangulation
973               (MgtPoly::Translate(POCT->PolygonOnTriangulation(), aMap),
974                MgtPoly::Translate(POCT->PolygonOnTriangulation2(), aMap),
975                MgtPoly::Translate(POCT->Triangulation(), aMap),
976                MgtTopLoc::Translate(POCT->Location(), aMap));
977           CR = PolOCT;
978         }
979         // PCR is a PolygonOnTriangulation
980         else {
981           Handle(BRep_PolygonOnTriangulation) PolOT = new
982             BRep_PolygonOnTriangulation
983               (MgtPoly::Translate(POT->PolygonOnTriangulation(), aMap),
984                MgtPoly::Translate(POT->Triangulation(), aMap),
985                MgtTopLoc::Translate(POT->Location(), aMap));
986           CR = PolOT;
987         }
988       }
989     }
990     else {
991       // jumps the Curve Representation
992       PCR = PCR->Next();
993       continue;
994     }
995     
996     Standard_NullObject_Raise_if (CR.IsNull(), "Persistant CurveRep is Null");
997
998     if(!CR.IsNull()) {
999       lcr.Prepend(CR); 
1000     }
1001     PCR = PCR->Next();
1002   }
1003   
1004   MgtTopoDS_TranslateTool::UpdateEdge(S1,S2, aMap);
1005 }
1006
1007
1008 //=======================================================================
1009 //function : UpdateFace
1010 //purpose  : Transient->Persistent
1011 //=======================================================================
1012
1013 void  
1014 MgtBRep_TranslateTool::UpdateFace(const TopoDS_Shape& S1, 
1015                                   const Handle(PTopoDS_HShape)& S2,
1016                                   PTColStd_TransientPersistentMap& aMap) const 
1017 {
1018 #ifdef MgtBRepSpeedDownCast
1019   Handle(TopoDS_TShape) aTransTShape = S1.TShape();
1020   Handle(BRep_TFace)&  TTF = (Handle(BRep_TFace)&) aTransTShape;
1021
1022   Handle(PTopoDS_TShape) aPersTShape = S2->TShape();
1023   Handle(PBRep_TFace)& PTF = (Handle(PBRep_TFace)&) aPersTShape;
1024 #else
1025   Handle(BRep_TFace)  TTF = Handle(BRep_TFace)::DownCast(S1.TShape());
1026   Handle(PBRep_TFace) PTF = Handle(PBRep_TFace)::DownCast(S2->TShape());
1027 #endif
1028
1029   // natural restriction
1030   PTF->NaturalRestriction(TTF->NaturalRestriction());
1031
1032   // tolerance
1033   PTF->Tolerance(TTF->Tolerance());
1034
1035   // location
1036   PTF->Location(MgtTopLoc::Translate(TTF->Location(), aMap));
1037
1038   // surface
1039   PTF->Surface(MgtBRep_TranslateTool::Translate(TTF->Surface(), aMap));
1040
1041   // Triangulation
1042   if (myTriangleMode == MgtBRep_WithTriangle) {
1043     PTF->Triangulation(MgtPoly::Translate(TTF->Triangulation(), aMap));
1044   }
1045   
1046   MgtTopoDS_TranslateTool::UpdateFace(S1,S2, aMap);
1047 }
1048
1049
1050 //=======================================================================
1051 //function : UpdateFace
1052 //purpose  : Persistent->Transient
1053 //=======================================================================
1054
1055 void  
1056 MgtBRep_TranslateTool::UpdateFace(const Handle(PTopoDS_HShape)& S1, 
1057                                   TopoDS_Shape& S2,
1058                                   PTColStd_PersistentTransientMap& aMap) const 
1059 {
1060 #ifdef MgtBRepSpeedDownCast
1061   Handle(PTopoDS_TShape) aPersTShape = S1->TShape();
1062   Handle(PBRep_TFace)& PTF = (Handle(PBRep_TFace)&) aPersTShape;
1063
1064   Handle(TopoDS_TShape) aTransTShape = S2.TShape();
1065   Handle(BRep_TFace)&  TTF = (Handle(BRep_TFace)&) aTransTShape;
1066 #else
1067   Handle(PBRep_TFace) PTF = Handle(PBRep_TFace)::DownCast(S1->TShape());
1068   Handle(BRep_TFace)  TTF = Handle(BRep_TFace)::DownCast(S2.TShape());
1069 #endif
1070
1071   // natural restriction
1072   TTF->NaturalRestriction(PTF->NaturalRestriction());
1073
1074   // tolerance
1075   TTF->Tolerance(PTF->Tolerance());
1076
1077   // location
1078   TTF->Location(MgtTopLoc::Translate(PTF->Location(), aMap));
1079
1080   // surface
1081   TTF->Surface(MgtBRep_TranslateTool::Translate(PTF->Surface(), aMap));
1082
1083   // Triangulation
1084   if ( myTriangleMode == MgtBRep_WithTriangle) {
1085     TTF->Triangulation(MgtPoly::Translate(PTF->Triangulation(), aMap));
1086   }
1087
1088   MgtTopoDS_TranslateTool::UpdateFace(S1,S2, aMap);
1089 }
1090