fbf0c80e27e6d23f5590b181307532b2e0ff1e9e
[occt.git] / src / MgtTopoDS / MgtTopoDS.cxx
1 // Created on: 1993-03-09
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23 #include <MgtTopoDS.ixx>
24 #include <MgtTopLoc.hxx>
25
26 #include <PTopoDS_HShape.hxx>
27 #include <PTopoDS_TShape.hxx>
28 #include <PTopoDS_Shape1.hxx>
29 #include <PTopoDS_TShape1.hxx>
30
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_TShape.hxx>
33 #include <TopoDS_Iterator.hxx>
34
35 #include <PTopoDS_HArray1OfHShape.hxx>
36 #include <PTopoDS_HArray1OfShape1.hxx>
37
38 #include <BRepTools.hxx>
39
40 #include <PTColStd_TransientPersistentMap.hxx>
41 #include <PTColStd_PersistentTransientMap.hxx>
42
43 #ifdef chrono
44 #include <OSD_Timer.hxx>
45 extern OSD_Timer VertexTimer;
46 extern OSD_Timer EdgeTimer;
47 extern OSD_Timer WireTimer;
48 extern OSD_Timer FaceTimer;
49 extern OSD_Timer ShellTimer;
50 extern OSD_Timer SolidTimer;
51 extern OSD_Timer StoreEdgeTimer;
52 #define TIMER_START(THETIMER) THETIMER.Start()
53 #define TIMER_STOP(THETIMER) THETIMER.Stop()
54 #else
55 #define TIMER_START(THETIMER)
56 #define TIMER_STOP(THETIMER)
57 #endif
58
59 //=======================================================================
60 //function : Translate
61 //purpose  : Translation Transient Shape -> Persistent Shape
62 //           Used for upwards compatibility.
63 //=======================================================================
64
65 Handle(PTopoDS_HShape) MgtTopoDS::Translate
66 (const TopoDS_Shape& aShape,
67  const Handle(MgtTopoDS_TranslateTool)& TrTool,
68  PTColStd_TransientPersistentMap &aMap)
69 {
70   if (aShape.IsNull()) 
71     return new PTopoDS_HShape();
72
73   // Translate the top-level shape
74   Handle(PTopoDS_HShape) pHShape = new PTopoDS_HShape();
75
76   Standard_Boolean translated = aMap.IsBound(aShape.TShape());
77   if (translated) {
78     // get the translated TShape
79     
80     Handle(Standard_Persistent) aPers = aMap.Find(aShape.TShape());
81     Handle(PTopoDS_TShape)& pTS = (Handle(PTopoDS_TShape)&) aPers;
82
83     pHShape->TShape(pTS);
84   }
85   else {
86
87     // create if not translated and update
88     
89     switch (aShape.ShapeType()) {
90       
91     case TopAbs_VERTEX :
92       TrTool->MakeVertex(pHShape);
93       TrTool->UpdateVertex(aShape,pHShape, aMap);
94       break;
95       
96     case TopAbs_EDGE :
97 #ifdef chrono
98       StoreEdgeTimer.Start();
99 #endif
100       TrTool->MakeEdge(pHShape);
101       TrTool->UpdateEdge(aShape,pHShape, aMap);
102 #ifdef chrono
103       StoreEdgeTimer.Stop();
104 #endif
105       break;
106       
107     case TopAbs_WIRE :
108       TrTool->MakeWire(pHShape);
109       //TrTool.UpdateWire(aShape,pHShape);
110       TrTool->UpdateShape(aShape,pHShape);
111       break;
112       
113     case TopAbs_FACE :
114       TrTool->MakeFace(pHShape);
115       TrTool->UpdateFace(aShape,pHShape, aMap);
116       break;
117     
118     case TopAbs_SHELL :
119       TrTool->MakeShell(pHShape);
120       //TrTool.UpdateShell(aShape,pHShape);
121       TrTool->UpdateShape(aShape,pHShape);
122       break;
123     
124     case TopAbs_SOLID :
125       TrTool->MakeSolid(pHShape);
126       //TrTool.UpdateSolid(aShape,pHShape);
127       TrTool->UpdateShape(aShape,pHShape);
128       break;
129       
130     case TopAbs_COMPSOLID :
131       TrTool->MakeCompSolid(pHShape);
132       //TrTool.UpdateCompSolid(aShape,pHShape);
133       TrTool->UpdateShape(aShape,pHShape);
134       break;
135       
136   case TopAbs_COMPOUND :
137       TrTool->MakeCompound(pHShape);
138       //TrTool.UpdateCompound(aShape,pHShape);
139       TrTool->UpdateShape(aShape,pHShape);
140       break;
141 #ifndef DEB
142     default:
143       break;
144 #endif
145     }
146   
147     // bind and copy the sub-elements
148
149     aMap.Bind(aShape.TShape(),pHShape->TShape());
150     // copy current Shape
151     TopoDS_Shape S = aShape;
152     S.Orientation(TopAbs_FORWARD);
153     S.Location(TopLoc_Location());
154     // count the number of <sub-shape> of the Shape's TShape
155     Standard_Integer nbElem = 0;
156     TopoDS_Iterator ItCount(S);
157     while (ItCount.More()){
158       nbElem ++;
159       ItCount.Next();
160     }
161
162     if (nbElem != 0) {
163       Handle(PTopoDS_HArray1OfHShape) myArray = 
164         new PTopoDS_HArray1OfHShape(1,nbElem);
165       // translate <sub-shapes>
166       TopoDS_Iterator ItTrans(S);
167       Standard_Integer i = 1;
168       while(ItTrans.More()) {
169         myArray->SetValue(i, MgtTopoDS::Translate(ItTrans.Value(),
170                                                   TrTool, aMap));
171         i++;
172         ItTrans.Next();
173       }
174       pHShape->TShape()->Shapes(myArray);
175     }
176     
177   }
178   
179   pHShape->Orientation(aShape.Orientation());
180   pHShape->Location(MgtTopLoc::Translate(aShape.Location(), aMap));
181   return pHShape;
182 }
183
184
185 //=======================================================================
186 //function : Translate
187 //purpose  : Translation Persistent HShape -> Transient Shape
188 //           Used for upwards compatibility.
189 //=======================================================================
190
191 void MgtTopoDS::Translate
192 (const Handle(PTopoDS_HShape)&             aHShape, 
193  const Handle(MgtTopoDS_TranslateTool)&    TrTool,
194  PTColStd_PersistentTransientMap&          aMap,
195  TopoDS_Shape&                             theShape)
196 {
197   if (aHShape->TShape().IsNull()) return;
198
199   // Translate the top-level shape
200 #ifndef DEB // Linux porting - to avoid warnings
201   if ( aMap.IsBound(aHShape->TShape()) ) {
202 #else
203   Standard_Boolean translated = aMap.IsBound(aHShape->TShape());
204   if (translated) {
205 #endif
206     // get the translated TShape
207     Handle(TopoDS_TShape) TS = 
208       *((Handle(TopoDS_TShape)*) &aMap.Find(aHShape->TShape()));
209     theShape.TShape(TS);
210   }
211
212   else {
213     // create if not translated and update
214     switch (aHShape->TShape()->ShapeType()) {
215       
216     case TopAbs_VERTEX :
217       TIMER_START(VertexTimer);
218       TrTool->MakeVertex(theShape);
219       TrTool->UpdateVertex(aHShape,theShape, aMap);
220       TIMER_STOP(VertexTimer);
221       break;
222       
223     case TopAbs_EDGE :
224       TIMER_START(EdgeTimer);
225       TrTool->MakeEdge(theShape);
226       TrTool->UpdateEdge(aHShape,theShape, aMap);
227       TIMER_STOP(EdgeTimer);
228       break;
229       
230     case TopAbs_WIRE :
231       TIMER_START(WireTimer);
232       TrTool->MakeWire(theShape);
233       //TrTool.UpdateWire(aHShape,theShape);
234       TrTool->UpdateShape(aHShape,theShape);
235       TIMER_STOP(WireTimer);
236       break;
237       
238     case TopAbs_FACE :
239       TIMER_START(FaceTimer);
240       TrTool->MakeFace(theShape);
241       TrTool->UpdateFace(aHShape,theShape, aMap);
242       TIMER_STOP(FaceTimer);
243       break;
244       
245     case TopAbs_SHELL :
246       TIMER_START(ShellTimer);
247       TrTool->MakeShell(theShape);
248       //TrTool.UpdateShell(aHShape,theShape);
249       TrTool->UpdateShape(aHShape,theShape);
250       TIMER_STOP(ShellTimer);
251       break;
252       
253     case TopAbs_SOLID :
254       TIMER_START(SolidTimer);
255       TrTool->MakeSolid(theShape);
256       //TrTool.UpdateSolid(aHShape,theShape);
257       TrTool->UpdateShape(aHShape,theShape);
258       TIMER_STOP(SolidTimer);
259       break;
260       
261     case TopAbs_COMPSOLID :
262       TrTool->MakeCompSolid(theShape);
263       //TrTool.UpdateCompSolid(aHShape,theShape);
264       TrTool->UpdateShape(aHShape,theShape);
265       break;
266       
267     case TopAbs_COMPOUND :
268       TrTool->MakeCompound(theShape);
269       //TrTool.UpdateCompound(aHShape,theShape);
270       TrTool->UpdateShape(aHShape,theShape);
271       break;
272 #ifndef DEB
273     default:
274       break;
275 #endif
276     }
277     
278     // bind and copy the sub-elements
279
280     Standard_Boolean wasFree = theShape.Free();
281     theShape.Free(Standard_True);
282     aMap.Bind(aHShape->TShape(), theShape.TShape());
283     // count the number of <sub-shape> of the Shape's TShape
284     // Is there any sub-shape 
285     if (!aHShape->TShape()->Shapes().IsNull()) {
286 #ifndef DEB // Linux porting - to avoid warnings
287       for (Standard_Integer i = 1 ; i<= aHShape->TShape()->Shapes()->Length(); i++) {
288 #else
289       Standard_Integer nbElem = aHShape->TShape()->Shapes()->Length();
290       for (Standard_Integer i = 1 ; i<=nbElem ; i++) {
291 #endif
292         TopoDS_Shape subShape;
293         MgtTopoDS::Translate(aHShape->TShape()->Shapes(i),
294                              TrTool, aMap, subShape);
295         TrTool->Add(theShape,subShape);
296       }
297     }
298     theShape.Free(wasFree);
299   }
300
301   theShape.Orientation(aHShape->Orientation());
302   theShape.Location(MgtTopLoc::Translate(aHShape->Location(), aMap));
303 }
304
305
306 //=======================================================================
307 //function : Translate1
308 //purpose  : Translation Transient Shape1 -> Persistent Shape
309 //=======================================================================
310
311 void MgtTopoDS::Translate1
312 (const TopoDS_Shape& aShape,
313  const Handle(MgtTopoDS_TranslateTool1)& TrTool,
314  PTColStd_TransientPersistentMap &aMap,
315  PTopoDS_Shape1& aPShape)
316 {
317   if (aShape.IsNull()) return;
318
319   if (aMap.IsBound(aShape.TShape())) {
320     // get the translated TShape
321     Handle(PTopoDS_TShape1) pTS = 
322       *((Handle(PTopoDS_TShape1)*) &aMap.Find(aShape.TShape()));
323     aPShape.TShape(pTS);
324   }
325   else {
326
327     // create if not translated and update
328     
329     switch (aShape.ShapeType()) {
330       
331     case TopAbs_VERTEX :
332       TrTool->MakeVertex(aPShape);
333       TrTool->UpdateVertex(aShape,aPShape,aMap);
334       break;
335       
336     case TopAbs_EDGE :
337       TIMER_START(StoreEdgeTimer);
338       TrTool->MakeEdge(aPShape);
339       TrTool->UpdateEdge(aShape,aPShape,aMap);
340       TIMER_STOP(StoreEdgeTimer);
341       break;
342       
343     case TopAbs_WIRE :
344       TrTool->MakeWire(aPShape);
345       TrTool->UpdateShape(aShape,aPShape);
346       break;
347       
348     case TopAbs_FACE :
349       TrTool->MakeFace(aPShape);
350       TrTool->UpdateFace(aShape,aPShape,aMap);
351       break;
352     
353     case TopAbs_SHELL :
354       TrTool->MakeShell(aPShape);
355       TrTool->UpdateShape(aShape,aPShape);
356       break;
357     
358     case TopAbs_SOLID :
359       TrTool->MakeSolid(aPShape);
360       TrTool->UpdateShape(aShape,aPShape);
361       break;
362       
363     case TopAbs_COMPSOLID :
364       TrTool->MakeCompSolid(aPShape);
365       TrTool->UpdateShape(aShape,aPShape);
366       break;
367       
368   case TopAbs_COMPOUND :
369       TrTool->MakeCompound(aPShape);
370       TrTool->UpdateShape(aShape,aPShape);
371       break;
372 #ifndef DEB
373     default:
374       break;
375 #endif
376     }
377   
378     // bind and copy the sub-elements
379     aMap.Bind(aShape.TShape(),aPShape.TShape());
380     // copy current Shape
381     TopoDS_Shape S = aShape;
382     S.Orientation(TopAbs_FORWARD);
383     S.Location(TopLoc_Location());
384     // count the number of <sub-shape> of the Shape's TShape
385     Standard_Integer nbElem = 0;
386     TopoDS_Iterator itr(S);
387     for (;itr.More();++nbElem,itr.Next());
388
389     if (nbElem) {
390       Handle(PTopoDS_HArray1OfShape1) myArray = 
391         new PTopoDS_HArray1OfShape1(1,nbElem);
392       // translate <sub-shapes>
393       
394       PTopoDS_Shape1 pSh;
395       nbElem = 1;
396       for (itr.Initialize(S);itr.More();++nbElem,itr.Next()) {
397         MgtTopoDS::Translate1(itr.Value(),TrTool,aMap,pSh);
398         myArray->SetValue(nbElem,pSh);
399         pSh.Nullify();
400       }
401       aPShape.TShape()->Shapes(myArray);
402     }
403     
404   }
405   
406   aPShape.Orientation(aShape.Orientation());
407   aPShape.Location(MgtTopLoc::Translate(aShape.Location(), aMap));
408 }
409
410
411 //=======================================================================
412 //function : Translate1
413 //purpose  : Translation Persistent Shape -> Transient Shape
414 //=======================================================================
415
416 void MgtTopoDS::Translate1
417 (const PTopoDS_Shape1& aPShape, 
418  const Handle(MgtTopoDS_TranslateTool1)& TrTool,
419  PTColStd_PersistentTransientMap& aMap,
420  TopoDS_Shape& aShape)
421 {
422   if (aPShape.TShape().IsNull()) return;
423   
424   // Translate the top-level shape
425   if (aMap.IsBound(aPShape.TShape())) {
426     // get the translated TShape
427     Handle(TopoDS_TShape) TS = 
428       *((Handle(TopoDS_TShape)*) &aMap.Find(aPShape.TShape()));
429     aShape.TShape(TS);
430   }
431   else {
432
433     // create if not translated and update
434     
435     switch (aPShape.TShape()->ShapeType()) {
436       
437     case TopAbs_VERTEX :
438       TIMER_START(VertexTimer);
439       TrTool->MakeVertex(aShape);
440       TrTool->UpdateVertex(aPShape,aShape, aMap);
441       TIMER_STOP(VertexTimer);
442       break;
443       
444     case TopAbs_EDGE :
445       TIMER_START(EdgeTimer);
446       TrTool->MakeEdge(aShape);
447       TrTool->UpdateEdge(aPShape,aShape, aMap);
448       TIMER_STOP(EdgeTimer);
449       break;
450       
451     case TopAbs_WIRE :
452       TIMER_START(WireTimer);
453       TrTool->MakeWire(aShape);
454       //TrTool.UpdateWire(aPShape,aShape);
455       TrTool->UpdateShape(aPShape,aShape);
456       TIMER_STOP(WireTimer);
457       break;
458       
459     case TopAbs_FACE :
460       TIMER_START(FaceTimer);
461       TrTool->MakeFace(aShape);
462       TrTool->UpdateFace(aPShape,aShape, aMap);
463       TIMER_STOP(FaceTimer);
464       break;
465       
466     case TopAbs_SHELL :
467       TIMER_START(ShellTimer);
468       TrTool->MakeShell(aShape);
469       //TrTool.UpdateShell(aPShape,aShape);
470       TrTool->UpdateShape(aPShape,aShape);
471       TIMER_STOP(ShellTimer);
472       break;
473       
474     case TopAbs_SOLID :
475       TIMER_START(SolidTimer);
476       TrTool->MakeSolid(aShape);
477       //TrTool.UpdateSolid(aPShape,aShape);
478       TrTool->UpdateShape(aPShape,aShape);
479       TIMER_STOP(SolidTimer);
480       break;
481       
482     case TopAbs_COMPSOLID :
483       TrTool->MakeCompSolid(aShape);
484       //TrTool.UpdateCompSolid(aPShape,aShape);
485       TrTool->UpdateShape(aPShape,aShape);
486       break;
487       
488     case TopAbs_COMPOUND :
489       TrTool->MakeCompound(aShape);
490       //TrTool.UpdateCompound(aPShape,aShape);
491       TrTool->UpdateShape(aPShape,aShape);
492       break;
493 #ifndef DEB
494     default:
495       break;
496 #endif
497     }
498     
499     // bind and copy the sub-elements
500
501     Standard_Boolean wasFree = aShape.Free();
502     aShape.Free(Standard_True);
503     aMap.Bind(aPShape.TShape(), aShape.TShape());
504     // count the number of <sub-shape> of the Shape's TShape
505     // Is there any sub-shape 
506     if (!aPShape.TShape()->Shapes().IsNull()) {
507       Standard_Integer nbElem = aPShape.TShape()->Shapes()->Length();
508       for (Standard_Integer i = 1 ; i<=nbElem ; i++) {
509         TopoDS_Shape subShape;
510         MgtTopoDS::Translate1(aPShape.TShape()->Shapes(i),
511                               TrTool, aMap,
512                               subShape);
513         TrTool->Add(aShape,subShape);
514       }
515     }
516     aShape.Free(wasFree);
517   }
518
519   aShape.Orientation(aPShape.Orientation());
520   aShape.Location(MgtTopLoc::Translate(aPShape.Location(), aMap));
521 }