summaryrefslogtreecommitdiff
path: root/src/MgtTopoDS/MgtTopoDS.cxx
blob: 07d5a7b5c9471ee8ec1a872a9db2d9dc785e411b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// File:	MgtTopoDS.cxx
// Created:	Tue Mar  9 16:30:58 1993
// Author:	Remi LEQUETTE
//		<rle@phobox>


#include <MgtTopoDS.ixx>
#include <MgtTopLoc.hxx>

#include <PTopoDS_HShape.hxx>
#include <PTopoDS_TShape.hxx>
#include <PTopoDS_Shape1.hxx>
#include <PTopoDS_TShape1.hxx>

#include <TopoDS_Shape.hxx>
#include <TopoDS_TShape.hxx>
#include <TopoDS_Iterator.hxx>

#include <PTopoDS_HArray1OfHShape.hxx>
#include <PTopoDS_HArray1OfShape1.hxx>

#include <BRepTools.hxx>

#include <PTColStd_TransientPersistentMap.hxx>
#include <PTColStd_PersistentTransientMap.hxx>

#ifdef chrono
#include <OSD_Timer.hxx>
extern OSD_Timer VertexTimer;
extern OSD_Timer EdgeTimer;
extern OSD_Timer WireTimer;
extern OSD_Timer FaceTimer;
extern OSD_Timer ShellTimer;
extern OSD_Timer SolidTimer;
extern OSD_Timer StoreEdgeTimer;
#define TIMER_START(THETIMER) THETIMER.Start()
#define TIMER_STOP(THETIMER) THETIMER.Stop()
#else
#define TIMER_START(THETIMER)
#define TIMER_STOP(THETIMER)
#endif

//=======================================================================
//function : Translate
//purpose  : Translation Transient Shape -> Persistent Shape
//           Used for upwards compatibility.
//=======================================================================

Handle(PTopoDS_HShape) MgtTopoDS::Translate
(const TopoDS_Shape& aShape,
 const Handle(MgtTopoDS_TranslateTool)& TrTool,
 PTColStd_TransientPersistentMap &aMap)
{
  if (aShape.IsNull()) 
    return new PTopoDS_HShape();

  // Translate the top-level shape
  Handle(PTopoDS_HShape) pHShape = new PTopoDS_HShape();

  Standard_Boolean translated = aMap.IsBound(aShape.TShape());
  if (translated) {
    // get the translated TShape
    
    Handle(Standard_Persistent) aPers = aMap.Find(aShape.TShape());
    Handle(PTopoDS_TShape)& pTS = (Handle(PTopoDS_TShape)&) aPers;

    pHShape->TShape(pTS);
  }
  else {

    // create if not translated and update
    
    switch (aShape.ShapeType()) {
      
    case TopAbs_VERTEX :
      TrTool->MakeVertex(pHShape);
      TrTool->UpdateVertex(aShape,pHShape, aMap);
      break;
      
    case TopAbs_EDGE :
#ifdef chrono
      StoreEdgeTimer.Start();
#endif
      TrTool->MakeEdge(pHShape);
      TrTool->UpdateEdge(aShape,pHShape, aMap);
#ifdef chrono
      StoreEdgeTimer.Stop();
#endif
      break;
      
    case TopAbs_WIRE :
      TrTool->MakeWire(pHShape);
      //TrTool.UpdateWire(aShape,pHShape);
      TrTool->UpdateShape(aShape,pHShape);
      break;
      
    case TopAbs_FACE :
      TrTool->MakeFace(pHShape);
      TrTool->UpdateFace(aShape,pHShape, aMap);
      break;
    
    case TopAbs_SHELL :
      TrTool->MakeShell(pHShape);
      //TrTool.UpdateShell(aShape,pHShape);
      TrTool->UpdateShape(aShape,pHShape);
      break;
    
    case TopAbs_SOLID :
      TrTool->MakeSolid(pHShape);
      //TrTool.UpdateSolid(aShape,pHShape);
      TrTool->UpdateShape(aShape,pHShape);
      break;
      
    case TopAbs_COMPSOLID :
      TrTool->MakeCompSolid(pHShape);
      //TrTool.UpdateCompSolid(aShape,pHShape);
      TrTool->UpdateShape(aShape,pHShape);
      break;
      
  case TopAbs_COMPOUND :
      TrTool->MakeCompound(pHShape);
      //TrTool.UpdateCompound(aShape,pHShape);
      TrTool->UpdateShape(aShape,pHShape);
      break;
#ifndef DEB
    default:
      break;
#endif
    }
  
    // bind and copy the sub-elements

    aMap.Bind(aShape.TShape(),pHShape->TShape());
    // copy current Shape
    TopoDS_Shape S = aShape;
    S.Orientation(TopAbs_FORWARD);
    S.Location(TopLoc_Location());
    // count the number of <sub-shape> of the Shape's TShape
    Standard_Integer nbElem = 0;
    TopoDS_Iterator ItCount(S);
    while (ItCount.More()){
      nbElem ++;
      ItCount.Next();
    }

    if (nbElem != 0) {
      Handle(PTopoDS_HArray1OfHShape) myArray = 
	new PTopoDS_HArray1OfHShape(1,nbElem);
      // translate <sub-shapes>
      TopoDS_Iterator ItTrans(S);
      Standard_Integer i = 1;
      while(ItTrans.More()) {
	myArray->SetValue(i, MgtTopoDS::Translate(ItTrans.Value(),
						  TrTool, aMap));
	i++;
	ItTrans.Next();
      }
      pHShape->TShape()->Shapes(myArray);
    }
    
  }
  
  pHShape->Orientation(aShape.Orientation());
  pHShape->Location(MgtTopLoc::Translate(aShape.Location(), aMap));
  return pHShape;
}


//=======================================================================
//function : Translate
//purpose  : Translation Persistent HShape -> Transient Shape
//           Used for upwards compatibility.
//=======================================================================

void MgtTopoDS::Translate
(const Handle(PTopoDS_HShape)&             aHShape, 
 const Handle(MgtTopoDS_TranslateTool)&    TrTool,
 PTColStd_PersistentTransientMap&          aMap,
 TopoDS_Shape&                             theShape)
{
  if (aHShape->TShape().IsNull()) return;

  // Translate the top-level shape
#ifndef DEB // Linux porting - to avoid warnings
  if ( aMap.IsBound(aHShape->TShape()) ) {
#else
  Standard_Boolean translated = aMap.IsBound(aHShape->TShape());
  if (translated) {
#endif
    // get the translated TShape
    Handle(TopoDS_TShape) TS = 
      *((Handle(TopoDS_TShape)*) &aMap.Find(aHShape->TShape()));
    theShape.TShape(TS);
  }

  else {
    // create if not translated and update
    switch (aHShape->TShape()->ShapeType()) {
      
    case TopAbs_VERTEX :
      TIMER_START(VertexTimer);
      TrTool->MakeVertex(theShape);
      TrTool->UpdateVertex(aHShape,theShape, aMap);
      TIMER_STOP(VertexTimer);
      break;
      
    case TopAbs_EDGE :
      TIMER_START(EdgeTimer);
      TrTool->MakeEdge(theShape);
      TrTool->UpdateEdge(aHShape,theShape, aMap);
      TIMER_STOP(EdgeTimer);
      break;
      
    case TopAbs_WIRE :
      TIMER_START(WireTimer);
      TrTool->MakeWire(theShape);
      //TrTool.UpdateWire(aHShape,theShape);
      TrTool->UpdateShape(aHShape,theShape);
      TIMER_STOP(WireTimer);
      break;
      
    case TopAbs_FACE :
      TIMER_START(FaceTimer);
      TrTool->MakeFace(theShape);
      TrTool->UpdateFace(aHShape,theShape, aMap);
      TIMER_STOP(FaceTimer);
      break;
      
    case TopAbs_SHELL :
      TIMER_START(ShellTimer);
      TrTool->MakeShell(theShape);
      //TrTool.UpdateShell(aHShape,theShape);
      TrTool->UpdateShape(aHShape,theShape);
      TIMER_STOP(ShellTimer);
      break;
      
    case TopAbs_SOLID :
      TIMER_START(SolidTimer);
      TrTool->MakeSolid(theShape);
      //TrTool.UpdateSolid(aHShape,theShape);
      TrTool->UpdateShape(aHShape,theShape);
      TIMER_STOP(SolidTimer);
      break;
      
    case TopAbs_COMPSOLID :
      TrTool->MakeCompSolid(theShape);
      //TrTool.UpdateCompSolid(aHShape,theShape);
      TrTool->UpdateShape(aHShape,theShape);
      break;
      
    case TopAbs_COMPOUND :
      TrTool->MakeCompound(theShape);
      //TrTool.UpdateCompound(aHShape,theShape);
      TrTool->UpdateShape(aHShape,theShape);
      break;
#ifndef DEB
    default:
      break;
#endif
    }
    
    // bind and copy the sub-elements

    Standard_Boolean wasFree = theShape.Free();
    theShape.Free(Standard_True);
    aMap.Bind(aHShape->TShape(), theShape.TShape());
    // count the number of <sub-shape> of the Shape's TShape
    // Is there any sub-shape 
    if (!aHShape->TShape()->Shapes().IsNull()) {
#ifndef DEB // Linux porting - to avoid warnings
      for (Standard_Integer i = 1 ; i<= aHShape->TShape()->Shapes()->Length(); i++) {
#else
      Standard_Integer nbElem = aHShape->TShape()->Shapes()->Length();
      for (Standard_Integer i = 1 ; i<=nbElem ; i++) {
#endif
	TopoDS_Shape subShape;
	MgtTopoDS::Translate(aHShape->TShape()->Shapes(i),
			     TrTool, aMap, subShape);
	TrTool->Add(theShape,subShape);
      }
    }
    theShape.Free(wasFree);
  }

  theShape.Orientation(aHShape->Orientation());
  theShape.Location(MgtTopLoc::Translate(aHShape->Location(), aMap));
}


//=======================================================================
//function : Translate1
//purpose  : Translation Transient Shape1 -> Persistent Shape
//=======================================================================

void MgtTopoDS::Translate1
(const TopoDS_Shape& aShape,
 const Handle(MgtTopoDS_TranslateTool1)& TrTool,
 PTColStd_TransientPersistentMap &aMap,
 PTopoDS_Shape1& aPShape)
{
  if (aShape.IsNull()) return;

  if (aMap.IsBound(aShape.TShape())) {
    // get the translated TShape
    Handle(PTopoDS_TShape1) pTS = 
      *((Handle(PTopoDS_TShape1)*) &aMap.Find(aShape.TShape()));
    aPShape.TShape(pTS);
  }
  else {

    // create if not translated and update
    
    switch (aShape.ShapeType()) {
      
    case TopAbs_VERTEX :
      TrTool->MakeVertex(aPShape);
      TrTool->UpdateVertex(aShape,aPShape,aMap);
      break;
      
    case TopAbs_EDGE :
      TIMER_START(StoreEdgeTimer);
      TrTool->MakeEdge(aPShape);
      TrTool->UpdateEdge(aShape,aPShape,aMap);
      TIMER_STOP(StoreEdgeTimer);
      break;
      
    case TopAbs_WIRE :
      TrTool->MakeWire(aPShape);
      TrTool->UpdateShape(aShape,aPShape);
      break;
      
    case TopAbs_FACE :
      TrTool->MakeFace(aPShape);
      TrTool->UpdateFace(aShape,aPShape,aMap);
      break;
    
    case TopAbs_SHELL :
      TrTool->MakeShell(aPShape);
      TrTool->UpdateShape(aShape,aPShape);
      break;
    
    case TopAbs_SOLID :
      TrTool->MakeSolid(aPShape);
      TrTool->UpdateShape(aShape,aPShape);
      break;
      
    case TopAbs_COMPSOLID :
      TrTool->MakeCompSolid(aPShape);
      TrTool->UpdateShape(aShape,aPShape);
      break;
      
  case TopAbs_COMPOUND :
      TrTool->MakeCompound(aPShape);
      TrTool->UpdateShape(aShape,aPShape);
      break;
#ifndef DEB
    default:
      break;
#endif
    }
  
    // bind and copy the sub-elements
    aMap.Bind(aShape.TShape(),aPShape.TShape());
    // copy current Shape
    TopoDS_Shape S = aShape;
    S.Orientation(TopAbs_FORWARD);
    S.Location(TopLoc_Location());
    // count the number of <sub-shape> of the Shape's TShape
    Standard_Integer nbElem = 0;
    TopoDS_Iterator itr(S);
    for (;itr.More();++nbElem,itr.Next()) {}

    if (nbElem) {
      Handle(PTopoDS_HArray1OfShape1) myArray = 
	new PTopoDS_HArray1OfShape1(1,nbElem);
      // translate <sub-shapes>
      
      PTopoDS_Shape1 pSh;
      nbElem = 1;
      for (itr.Initialize(S);itr.More();++nbElem,itr.Next()) {
	MgtTopoDS::Translate1(itr.Value(),TrTool,aMap,pSh);
	myArray->SetValue(nbElem,pSh);
	pSh.Nullify();
      }
      aPShape.TShape()->Shapes(myArray);
    }
    
  }
  
  aPShape.Orientation(aShape.Orientation());
  aPShape.Location(MgtTopLoc::Translate(aShape.Location(), aMap));
}


//=======================================================================
//function : Translate1
//purpose  : Translation Persistent Shape -> Transient Shape
//=======================================================================

void MgtTopoDS::Translate1
(const PTopoDS_Shape1& aPShape, 
 const Handle(MgtTopoDS_TranslateTool1)& TrTool,
 PTColStd_PersistentTransientMap& aMap,
 TopoDS_Shape& aShape)
{
  if (aPShape.TShape().IsNull()) return;
  
  // Translate the top-level shape
  if (aMap.IsBound(aPShape.TShape())) {
    // get the translated TShape
    Handle(TopoDS_TShape) TS = 
      *((Handle(TopoDS_TShape)*) &aMap.Find(aPShape.TShape()));
    aShape.TShape(TS);
  }
  else {

    // create if not translated and update
    
    switch (aPShape.TShape()->ShapeType()) {
      
    case TopAbs_VERTEX :
      TIMER_START(VertexTimer);
      TrTool->MakeVertex(aShape);
      TrTool->UpdateVertex(aPShape,aShape, aMap);
      TIMER_STOP(VertexTimer);
      break;
      
    case TopAbs_EDGE :
      TIMER_START(EdgeTimer);
      TrTool->MakeEdge(aShape);
      TrTool->UpdateEdge(aPShape,aShape, aMap);
      TIMER_STOP(EdgeTimer);
      break;
      
    case TopAbs_WIRE :
      TIMER_START(WireTimer);
      TrTool->MakeWire(aShape);
      //TrTool.UpdateWire(aPShape,aShape);
      TrTool->UpdateShape(aPShape,aShape);
      TIMER_STOP(WireTimer);
      break;
      
    case TopAbs_FACE :
      TIMER_START(FaceTimer);
      TrTool->MakeFace(aShape);
      TrTool->UpdateFace(aPShape,aShape, aMap);
      TIMER_STOP(FaceTimer);
      break;
      
    case TopAbs_SHELL :
      TIMER_START(ShellTimer);
      TrTool->MakeShell(aShape);
      //TrTool.UpdateShell(aPShape,aShape);
      TrTool->UpdateShape(aPShape,aShape);
      TIMER_STOP(ShellTimer);
      break;
      
    case TopAbs_SOLID :
      TIMER_START(SolidTimer);
      TrTool->MakeSolid(aShape);
      //TrTool.UpdateSolid(aPShape,aShape);
      TrTool->UpdateShape(aPShape,aShape);
      TIMER_STOP(SolidTimer);
      break;
      
    case TopAbs_COMPSOLID :
      TrTool->MakeCompSolid(aShape);
      //TrTool.UpdateCompSolid(aPShape,aShape);
      TrTool->UpdateShape(aPShape,aShape);
      break;
      
    case TopAbs_COMPOUND :
      TrTool->MakeCompound(aShape);
      //TrTool.UpdateCompound(aPShape,aShape);
      TrTool->UpdateShape(aPShape,aShape);
      break;
#ifndef DEB
    default:
      break;
#endif
    }
    
    // bind and copy the sub-elements

    Standard_Boolean wasFree = aShape.Free();
    aShape.Free(Standard_True);
    aMap.Bind(aPShape.TShape(), aShape.TShape());
    // count the number of <sub-shape> of the Shape's TShape
    // Is there any sub-shape 
    if (!aPShape.TShape()->Shapes().IsNull()) {
      Standard_Integer nbElem = aPShape.TShape()->Shapes()->Length();
      for (Standard_Integer i = 1 ; i<=nbElem ; i++) {
	TopoDS_Shape subShape;
	MgtTopoDS::Translate1(aPShape.TShape()->Shapes(i),
			      TrTool, aMap,
			      subShape);
	TrTool->Add(aShape,subShape);
      }
    }
    aShape.Free(wasFree);
  }

  aShape.Orientation(aPShape.Orientation());
  aShape.Location(MgtTopLoc::Translate(aPShape.Location(), aMap));
}