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