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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//-------------------------------------------------------------------------
// Filename      : OCCLump.cpp
//
// Purpose       : 
//
// Creator       : David White
//
// Creation Date : 7/18/2000
//
//-------------------------------------------------------------------------

// ********** BEGIN STANDARD INCLUDES      **********
#include <assert.h>
// ********** END STANDARD INCLUDES        **********
// ********** BEGIN CUBIT INCLUDES         **********
#include "OCCQueryEngine.hpp"
#include "OCCLump.hpp"
#include "CastTo.hpp"
#include "Surface.hpp"
#include "DLIList.hpp"
#include "CubitVector.hpp"
#include "CubitString.hpp"
#include "ShellSM.hpp"
#include "BodySM.hpp"
#include "Body.hpp"

#include "OCCBody.hpp"
#include "OCCShell.hpp"
#include "OCCSurface.hpp"
#include "OCCLoop.hpp"
#include "OCCCoEdge.hpp"
#include "OCCCurve.hpp"
#include "OCCPoint.hpp"
#include "OCCAttribSet.hpp"

#include <TopExp.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include "BRepBuilderAPI_Transform.hxx"
#include "BRepBuilderAPI_GTransform.hxx"
#include "TopTools_DataMapOfShapeInteger.hxx"
#include "TopTools_ListIteratorOfListOfShape.hxx"
#include "BRepAlgoAPI_BooleanOperation.hxx"
#include "BRepBuilderAPI_MakeShape.hxx"
#include "BRepBuilderAPI_ModifyShape.hxx"
#include "Bnd_Box.hxx"
#include "BRepBndLib.hxx"
#include "GProp_GProps.hxx"
#include "BRepGProp.hxx"
#include "BRepClass3d_SolidExplorer.hxx"
#include "BRepClass3d_SClassifier.hxx"
#include "TopExp_Explorer.hxx"
#include "TopoDS.hxx"
#include "BRep_Tool.hxx"
#include "LocOpe_SplitShape.hxx"
// ********** END CUBIT INCLUDES           **********

// ********** BEGIN FORWARD DECLARATIONS   **********
class RefVolume;
// ********** END FORWARD DECLARATIONS     **********

// ********** BEGIN STATIC DECLARATIONS    **********
// ********** END STATIC DECLARATIONS      **********

// ********** BEGIN PUBLIC FUNCTIONS       **********

//-------------------------------------------------------------------------
// Purpose       : The constructor with a pointer to TopoDS_Solid
//
// Special Notes :
//
//-------------------------------------------------------------------------
OCCLump::OCCLump(TopoDS_Solid *theSolid, OCCSurface* surface, OCCShell* shell)
{
  myTopoDSSolid = theSolid;
  mySheetSurface = surface;
  myShell = shell;
  if(myTopoDSSolid && !myTopoDSSolid->IsNull())
    assert(myTopoDSSolid->ShapeType() == TopAbs_SOLID);
}

OCCLump::~OCCLump()
{ 
  if (myTopoDSSolid)
  {
    myTopoDSSolid->Nullify();
    delete myTopoDSSolid;
  }
}

void OCCLump::set_TopoDS_Solid(TopoDS_Solid solid)
{
  if(myTopoDSSolid && solid.IsEqual(*myTopoDSSolid) )
    return;

  if(myTopoDSSolid)
    myTopoDSSolid->Nullify() ;

  *myTopoDSSolid = solid;
}
//-------------------------------------------------------------------------
// Purpose       : Find centroid
//
// Special Notes :
//
// Author       : Jane Hu
//
// Creation Date : 12/06/07
//-------------------------------------------------------------------------
CubitStatus OCCLump::mass_properties( CubitVector& centroid,
                                      double& volume )
{
  if (mySheetSurface || myShell)
    return CUBIT_FAILURE;

  GProp_GProps myProps;
  BRepGProp::VolumeProperties(*myTopoDSSolid, myProps);
  volume = myProps.Mass();
  gp_Pnt pt = myProps.CentreOfMass();
  centroid.set(pt.X(), pt.Y(), pt.Z());

  return CUBIT_SUCCESS;
}

//-------------------------------------------------------------------------
// Purpose       : The purpose of this function is to append a
//                 attribute to the GE. The name is attached to the 
//                 underlying solid model entity this one points to.
//
//
// Special Notes : 
//
// Creator       : Malcolm J. Panthaki
//
// Creation Date : 11/21/96
//-------------------------------------------------------------------------
void OCCLump::append_simple_attribute_virt(const CubitSimpleAttrib &csa)
{ 
  TopoDS_Shape shape;
  if(mySheetSurface)
    shape = *(mySheetSurface->get_TopoDS_Face());
  else if(myShell)
    shape = *(myShell->get_TopoDS_Shell());
  else
    shape =*myTopoDSSolid;
  OCCAttribSet::append_attribute(csa, shape); 
}

//-------------------------------------------------------------------------
// Purpose       : The purpose of this function is to remove a simple 
//                 attribute attached to this geometry entity. The name is 
//                 removed from the underlying BODY this points to.
//
// Special Notes : 
//
// Creator       : David R. White
//
// Creation Date : 03/18/97
//-------------------------------------------------------------------------
void OCCLump::remove_simple_attribute_virt(const CubitSimpleAttrib &csa )
{ 
  TopoDS_Shape shape;
  if(mySheetSurface)
    shape = *(mySheetSurface->get_TopoDS_Face());
  else if(myShell)
    shape = *(myShell->get_TopoDS_Shell());
  else
    shape =*myTopoDSSolid;
  OCCAttribSet::remove_attribute(csa, shape); 
}

//-------------------------------------------------------------------------
// Purpose       : The purpose of this function is to remove all simple 
//                 attributes attached to this geometry entity.  Also
//                 removes lingering GTC attributes.
//
//
// Special Notes : 
//
// Creator       : Greg Nielson
//
// Creation Date : 07/10/98
//-------------------------------------------------------------------------
void OCCLump::remove_all_simple_attribute_virt()
{ 
  TopoDS_Shape shape;
  if(mySheetSurface)
    shape = *(mySheetSurface->get_TopoDS_Face());
  else if(myShell)
    shape = *(myShell->get_TopoDS_Shell());
  else
    shape =*myTopoDSSolid;
  OCCAttribSet::remove_attribute(CubitSimpleAttrib(), shape);
}

//-------------------------------------------------------------------------
// Purpose       : The purpose of this function is to get the  
//                 attributes attached to this geometry entity. The name is 
//                 attached to the underlying BODY this points to.
//
// Special Notes : 
//
//-------------------------------------------------------------------------
CubitStatus OCCLump::get_simple_attribute(DLIList<CubitSimpleAttrib>& csa_list)
{ 
  TopoDS_Shape shape;
  if(mySheetSurface)
    shape = *(mySheetSurface->get_TopoDS_Face());
  else if(myShell)
    shape = *(myShell->get_TopoDS_Shell());
  else
    shape =*myTopoDSSolid;
  return OCCAttribSet::get_attributes(shape, csa_list ); 
}

CubitStatus OCCLump::get_simple_attribute( const CubitString& name,
                                        DLIList<CubitSimpleAttrib>& csa_list )
{
  TopoDS_Shape shape;
  if(mySheetSurface)
    shape = *(mySheetSurface->get_TopoDS_Face());
  else if(myShell)
    shape = *(myShell->get_TopoDS_Shell());
  else
    shape =*myTopoDSSolid;
  return OCCAttribSet::get_attributes( name, shape, csa_list ); 
}


//-------------------------------------------------------------------------
// Purpose       : Get the bounding box of the object.
//
// Special Notes :
//
//-------------------------------------------------------------------------
CubitBox OCCLump::bounding_box() const 
{
  Bnd_Box box;
  TopoDS_Shape shape;
  if(mySheetSurface)
    shape = *(mySheetSurface->get_TopoDS_Face());
  else if(myShell)
    shape = *(myShell->get_TopoDS_Shell());
  else
    shape =*myTopoDSSolid;

  //calculate the bounding box
  BRepBndLib::Add(shape, box);
  double min[3], max[3];

  //get values
  box.Get(min[0], min[1], min[2], max[0], max[1], max[2]);

  //update boundingbox.
  CubitBox cBox(min, max);
  return cBox;
}

//-------------------------------------------------------------------------
// Purpose       : Get geometry modeling engine: OCCGeometryEngine
//
// Special Notes :
//
//-------------------------------------------------------------------------
GeometryQueryEngine* 
                 OCCLump::get_geometry_query_engine() const
{
   return OCCQueryEngine::instance();
}                 

//-------------------------------------------------------------------------
// Purpose       : Returns the volume of the Lump
//
// Special Notes :
//
// Creator       : 
//
// Creation Date : 
//-------------------------------------------------------------------------
double OCCLump::measure()
{
  if(mySheetSurface) 
    return mySheetSurface->measure();
  else if(myShell)
    return myShell->measure();

  GProp_GProps myProps;
  BRepGProp::VolumeProperties(*myTopoDSSolid, myProps);
  return myProps.Mass();
}

void OCCLump::get_parents_virt(DLIList<TopologyBridge*> &bodies) 
{
  if(mySheetSurface)
    bodies.append(mySheetSurface->my_body());
  else if (myShell)
    bodies.append(myShell->my_body());
  else
    bodies.append(myBodyPtr);
}

BodySM* OCCLump::get_body() const
{
  if(mySheetSurface)
    return mySheetSurface->my_body();
  if(myShell)
    return myShell->my_body();
  return myBodyPtr;
}

void OCCLump::get_children_virt(DLIList<TopologyBridge*> &shellsms)
{
  if (mySheetSurface)
  {
    shellsms.append(mySheetSurface->my_shell());
    return;
  }
  else if(myShell)
  {
    shellsms.append(myShell);
    return;
  }
  TopTools_IndexedMapOfShape M;
  TopExp::MapShapes(*myTopoDSSolid, TopAbs_SHELL, M);
  int ii;
  for (ii=1; ii<=M.Extent(); ii++) {
	  TopologyBridge *shell = OCCQueryEngine::instance()->occ_to_cgm(M(ii));
          if (shell)
	    shellsms.append_unique(shell);
  }
}



CubitPointContainment OCCLump::point_containment( const CubitVector &point )
{
  if (mySheetSurface || myShell)
    return CUBIT_PNT_UNKNOWN;

  TopoDS_Solid * solid = get_TopoDS_Solid();
  gp_Pnt pnt(point.x(), point.y(), point.z());
  BRepClass3d_SolidExplorer ex(*solid);
   
  //use face tolerance at the tolerence to see if the point is on.
  TopExp_Explorer Ex;
  Ex.Init(*solid, TopAbs_FACE);
  TopoDS_Face face = TopoDS::Face(Ex.Current());
  
  double dtol = BRep_Tool::Tolerance(face);
  BRepClass3d_SClassifier ps(ex, pnt, dtol);
  
  TopAbs_State state = ps.State();
  if (state == TopAbs_IN)
     return CUBIT_PNT_INSIDE;
  else if (state == TopAbs_OUT)
     return CUBIT_PNT_OUTSIDE;
  else if (state == TopAbs_ON)
     return CUBIT_PNT_BOUNDARY;

  return CUBIT_PNT_UNKNOWN;
  
}

//----------------------------------------------------------------
// Function: to update the core Solid
//           for any movement of the lump.
// Author: Jane Hu
//----------------------------------------------------------------
CubitStatus OCCLump::update_OCC_entity( BRepBuilderAPI_ModifyShape *aBRepTrsf,
                                        BRepAlgoAPI_BooleanOperation *op)
{
  if(mySheetSurface || myShell)
    return CUBIT_FAILURE;

  assert(aBRepTrsf != NULL || op != NULL);
 
  TopoDS_Shape shape;
  if(aBRepTrsf)
    shape = aBRepTrsf->ModifiedShape(*get_TopoDS_Solid());

  else
  {
    TopTools_ListOfShape shapes;
    shapes.Assign(op->Modified(*get_TopoDS_Solid()));
    if(shapes.Extent() == 0)
      shapes.Assign(op->Generated(*get_TopoDS_Solid()));
    if(shapes.Extent() > 1)
    { 
      //update all attributes first.
      TopTools_ListIteratorOfListOfShape it;
      it.Initialize(shapes);
      for(; it.More(); it.Next())
      {
        shape = it.Value();
        OCCQueryEngine::instance()->copy_attributes(*get_TopoDS_Solid(), 
                                                    shape);
      }
      shape = shapes.First();
    }
    else if (shapes.Extent() == 1)
      shape = shapes.First();
    else if(op->IsDeleted(*get_TopoDS_Solid()))
      ;
    else
      return CUBIT_SUCCESS;
  }
  
  TopoDS_Solid solid;
  if(aBRepTrsf || !op->IsDeleted(*get_TopoDS_Solid()))
    solid = TopoDS::Solid(shape);

  //set the lumps
  DLIList<TopologyBridge *> shells;
  this->get_children_virt(shells);
  for (int i = 1; i <= shells.size(); i++)
  {
     OCCShell *shell = CAST_TO(shells.get_and_step(), OCCShell);
     shell->update_OCC_entity(aBRepTrsf, op);
  }
  OCCQueryEngine::instance()->update_OCC_map(*myTopoDSSolid, solid);

  return CUBIT_SUCCESS;
}

//----------------------------------------------------------------
// Function: TopoDS_Shape level function to update the core Lump
//           for any movement  or Boolean operation of the body.
// Author: Jane Hu
//----------------------------------------------------------------
CubitStatus OCCLump::update_OCC_entity(TopoDS_Solid& old_solid,
                                       TopoDS_Shape& new_shape,
                                       BRepBuilderAPI_MakeShape *op,
                                       LocOpe_SplitShape* sp)
{
  //set the Shells
  TopTools_IndexedMapOfShape M;
  TopoDS_Shape shape, shape2;
  TopExp::MapShapes(new_shape, TopAbs_SOLID,M);
  TopoDS_Solid new_solid;
  CubitBoolean isCompound = CUBIT_FALSE;
  if(M.Extent() > 1 )
    isCompound = CUBIT_TRUE;

  if(M.Extent() == 1 )
    new_solid = TopoDS::Solid(M(1));  

  M.Clear();
  TopExp::MapShapes(old_solid, TopAbs_SHELL, M);
  TopTools_ListOfShape shapes;<--- Shadowed declaration
 
  for(int ii=1; ii<=M.Extent(); ii++)
  {
    TopoDS_Shell shell = TopoDS::Shell(M(ii));

    TopTools_ListOfShape shapes;<--- Shadow variable
    TopTools_IndexedMapOfShape M2;
    if (op)
    {
      shapes.Assign(op->Modified(shell));
      if(shapes.Extent() == 0)
         shapes.Assign(op->Generated(shell));
      // bug fix for hollow operation, in which a new shell is genearted
      // but the shapes we get from old_shell->Generated() is still old_shell
      // not the new one.
      if(!new_solid.IsNull())  
         TopExp::MapShapes(new_solid, TopAbs_SHELL, M2);
    }
    else if(sp)
      shapes.Assign(sp->DescendantShapes(shell));

    if (shapes.Extent() == 1)
    {
      shape = shapes.First(); 
      if(M2.Extent() > 0)
        shape2 = TopoDS::Shell(M2(1)); 
      if(M2.Extent() == 1 && !shape.IsSame(shape2))
         shape = shape2;
      else if(M2.Extent() > 1)
      {
         shapes.Clear(); 
         for(int jj = 1; jj <= M2.Extent(); jj++) 
           shapes.Append(M2(jj));
      }
    }

    else if(shapes.Extent() > 1)
    {
      //update all attributes first.
      TopTools_ListIteratorOfListOfShape it;
      it.Initialize(shapes);
      for(; it.More(); it.Next())
      {
        shape = it.Value();
        OCCQueryEngine::instance()->copy_attributes(shell, shape);
      }
      shape = shapes.First();
    }
    else if(op->IsDeleted(shell))
    {
       TopTools_IndexedMapOfShape M_new;
       TopExp::MapShapes(new_shape, TopAbs_SHELL, M_new);
       if (M_new.Extent()== 1)
         shape = M_new(1);
       else
         shape.Nullify();
    }
    else
    {
       shape = shell;
       continue;
    }
 
    if(shapes.Extent() > 0 || (op && op->IsDeleted(shell)))
      OCCShell::update_OCC_entity(shell, shape, op, sp);
  }
  if(!new_solid.IsNull() && !old_solid.IsSame(new_solid))
    OCCQueryEngine::instance()->update_OCC_map(old_solid, new_solid);
  else if(isCompound)
    OCCQueryEngine::instance()->update_OCC_map(old_solid, new_shape);  
  return CUBIT_SUCCESS;
}