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