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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//-- File: RefGroup.cc

#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "RefGroup.hpp"
#include "CubitObservable.hpp"
#include "CubitBox.hpp"
#include "DLIList.hpp"  
#include "CastTo.hpp"
#include "GeometryQueryTool.hpp"
#include "RefEntityFactory.hpp"
#include "AppUtil.hpp"
#include "GeometryEvent.hpp"

RefGroup::RefGroup( const char* name, int id)
{
  recursionMark = 0;

  if( id == 0 )
    entityId = RefEntityFactory::instance()->next_ref_group_id();
  else 
    entityId = id;

  // assign default names
  assign_default_name();
  
  if (name != NULL)
    entity_name( name );
}

RefGroup::RefGroup (DLIList<RefEntity*>& entity_list)
{
  recursionMark = 0;
  //entityList = entity_list;
  int i;
  for ( i = entity_list.size(); i > 0; i-- )
  {
    RefEntity *ent_ptr = entity_list.get_and_step();
    if ( !entityList.move_to(ent_ptr) )
      entityList.append(ent_ptr);
  }  
  entityList.reset();
  for (i=entityList.size(); i > 0; i--)
    register_observable(entityList.get_and_step());

  entityId = RefEntityFactory::instance()->next_ref_group_id();

    // Notify Model about the creation of this object
  AppUtil::instance()->send_event(GeometryEvent(GeometryEvent::TOPOLOGY_ENTITY_CONSTRUCTED, this)) ;

  // assign default names
  assign_default_name();

}

RefGroup::RefGroup(int /*proe_type*/)
{
   // This constructor was created just to get around not doing a notify,
   // so we can use it for Pro/E parts and assemblies.
  recursionMark = 0;
}

RefGroup::~RefGroup ()
{
   remove_all_ref_entities();
}

CubitStatus RefGroup::add_ref_entity(RefEntity *ref_entity, bool emit_event)
{
  CubitStatus status = CUBIT_FAILURE;
  
  //  Force failure of commands like: group 3 add group 3
  if ( ( ref_entity->entity_type_info() == entity_type_info() ) &&
	   ( ref_entity->entity_name() == entity_name() ) )
	  return CUBIT_FAILURE;

  if (!entityList.move_to(ref_entity)) 
  {
    entityList.append(ref_entity);
    register_observable(ref_entity);
    status = CUBIT_SUCCESS;
    if (emit_event)
      AppUtil::instance()->send_event(GeometryEvent(GeometryEvent::GROUP_MODIFIED, this));
  }
  return status;
}

CubitStatus RefGroup::add_ref_entity(DLIList<RefEntity*>& entity_list, 
                                        bool emit_event)
{
  RefEntity* entity = NULL;
  CubitStatus status = CUBIT_FAILURE;
  for (int i=entity_list.size(); i > 0; i--) 
  {
    entity = entity_list.get_and_step();
    if ( entityList.move_to(entity) == CUBIT_FALSE )
    {
       entityList.append(entity);
       register_observable(entity);
       status = CUBIT_SUCCESS;
    }
  }

  if (emit_event && status == CUBIT_SUCCESS)
      AppUtil::instance()->send_event(GeometryEvent(GeometryEvent::GROUP_MODIFIED, this));

  return status;
}

CubitStatus RefGroup::remove_ref_entity(RefEntity *entity,
                                        bool emit_event)
{
  CubitStatus status = CUBIT_FAILURE;
  if (entityList.remove(entity)) {
    unregister_observable(entity);
    status = CUBIT_SUCCESS;
    if (emit_event)
      AppUtil::instance()->send_event(GeometryEvent(GeometryEvent::GROUP_MODIFIED, this));
  }
  return status;
}

CubitStatus RefGroup::remove_ref_entity(DLIList<RefEntity*> &entity_list,
                                        bool emit_event)
{
  
  int i;
  RefEntity *entity;
  CubitStatus status = CUBIT_FAILURE;
  for (i = entity_list.size(); i > 0; i--) {
    entity = entity_list.get_and_step();
    if (entityList.remove(entity)) {
      unregister_observable(entity);
      status = CUBIT_SUCCESS;
    }
  }

  if (emit_event && status == CUBIT_SUCCESS)
      AppUtil::instance()->send_event(GeometryEvent(GeometryEvent::GROUP_MODIFIED, this));
    
  return status;
}

int RefGroup::remove_all_ref_entities()
{
  int num_entities = entityList.size();
  for (int i=entityList.size(); i > 0; i--) {
    unregister_observable(entityList.get_and_step());
  }
  entityList.clean_out();
  return num_entities;
}

void RefGroup::get_child_ref_entities(DLIList<RefEntity*>& entity_list)
{
  entityList.reset();
  entity_list.merge_unique(entityList, CUBIT_TRUE );
}

int RefGroup::get_num_child_ref_entities_by_type(const std::type_info &entity_type)
{
  int count = 0;
  int size = entityList.size();
  for(int i=0; i<size; i++)
  {
    RefEntity *cur_ent = entityList[i];
    if(cur_ent->entity_type_info() == entity_type)
      count++;
  }
  return count;
}

RefEntity* RefGroup::get_child_ref_entity_by_index(const std::type_info& entity_type, int index)
{
  RefEntity *entity = NULL;
  int size = entityList.size();
  int type_index = 0;
  for(int i=0; i<size && !entity; i++)
  {
    RefEntity *cur_ent = entityList[i];
    if(cur_ent->entity_type_info() == entity_type)
    {
      if(type_index == index)
        entity = cur_ent;
      else
        type_index++;
    }
  }
  return entity;
}

void RefGroup::get_child_entities(DLIList<CubitEntity*>& cub_entity_list)
{
  cub_entity_list.casting_merge_unique(entityList, CUBIT_TRUE);
}

void RefGroup::expand_group( DLIList<RefEntity*> & entity_list )
{
    //This function will get the list of child entities for this group and 
    //if there are any groups in its child list, it will call this fucntion
    //again on those groups.  The end result should be that the returned
    //list does not contain groups... just basic_topology entities
    //(the other ref_entities) 
  DLIList<RefEntity*> group_list, mixed_list;
  RefEntity *ref_entity_ptr;
  RefGroup *group;
  mixed_list.append( this );

  // don't swap the upper and lower bounds on ii.
  for ( int ii = 0; ii < mixed_list.size(); ii++ )
  {
    //Note, the list gets added to, so don't do a simple get_and_step
    mixed_list.reset();
    mixed_list.step(ii);
    ref_entity_ptr = mixed_list.get();
    
    group = CAST_TO( ref_entity_ptr, RefGroup );
    if (group)
      {
	// avoid infinite recursion of groups A and B containing each other
        if (!group_list.move_to(group)) {
	  group->get_child_ref_entities(mixed_list);
	  group_list.append(group);
        }
      }
    // In the future if we have other RefEntities that are grouping
    // entities then we should have another statement here...
    else
      entity_list.append( ref_entity_ptr );
  }

  return;
}

void RefGroup::get_parent_ref_entities(DLIList<RefEntity*>&)
{
  //- appends all ref entities that own this to entity_list.
  //- Goes up just one dimension.
  // There is nothing above us. Do nothing
}

int RefGroup::maximum_dimension()
{
  recursionMark = 1;
  // This routine returns the maximum dimension of its owned subentities.
  // The 'only' kludge is that if one of its subentities is a RefGroup, it
  // must call 'maximum_dimension' on that entity instead of dimension().
  
  RefEntity *entity;
  int test_dim = 0;
  int dimension = 0;
  
  for (int i = entityList.size(); i > 0; i--)  
  {
    entity = entityList.get_and_step();
    RefGroup* group;
    if ( (group = CAST_TO(entity,RefGroup) ) != NULL )
    {
      if (group->recursionMark == 0)
	test_dim = group->maximum_dimension();
    } 
    else 
    {
      test_dim = entity->dimension();
    }
    dimension = CUBIT_MAX(dimension, test_dim);
  }
  recursionMark = 0;
  return dimension;
}

CubitBox RefGroup::bounding_box()
{
  recursionMark = 1;
 
  CubitBox super_box = CubitBox();
  int super_box_defined = CUBIT_FALSE;
  
  for (int i = entityList.size(); i > 0; i--) {
      
    RefEntity *entity = entityList.get_and_step();
    RefGroup *group = CAST_TO(entity, RefGroup);
    if (!group || group->recursionMark == 0) {
      CubitBox entity_box = entity->bounding_box();
      
      // "Concatenate" this box with the super_box, creating a bounding
      // box that bounds the entities (from the list), processed so far.
      if (super_box_defined)
	super_box |= entity_box;
      else {
	super_box = entity_box;
	super_box_defined = CUBIT_TRUE;
      }
    }
  }
  recursionMark = 0;

  return super_box;
}

void RefGroup::get_sub_entities(DLIList<RefEntity*> &entity_list)
{  
  recursionMark = 1;

  //- appends all ref entities owned by this entity on entity_list
  //- and recurses all the way down to dimension 0
  DLIList<RefEntity*> local_entity_list;
  get_child_ref_entities(local_entity_list);

  // *need* to merge now if a group
  // else more efficient to merge later
  entity_list.merge_unique(local_entity_list, CUBIT_TRUE);
DLIList<RefEntity*> temp_list, temp_list2;


  for (int i=local_entity_list.size(); i > 0; i--) {
    // take some care to avoid infinite recursion
    RefEntity *child = local_entity_list.get_and_step();
    RefGroup *group = CAST_TO(child, RefGroup);
    if (!group || group->recursionMark == 0) {
	temp_list2.clean_out();
        child->get_all_child_ref_entities(temp_list2);  
 	temp_list.merge_unique(temp_list2);
     }
  }

  entity_list.merge_unique(temp_list);
  recursionMark = 0;
}

void RefGroup::is_mergeable(AutoMergeStatus val)
{
  recursionMark = 1;
  autoMergeStatus = val;
  DLIList<RefEntity*> children;
  get_child_ref_entities( children );
  for ( int i = children.size(); i > 0; i-- ) {
    RefEntity *child = children.get_and_step();
    RefGroup *group = CAST_TO(child, RefGroup);
    if (!group || group->recursionMark == 0)
      child->is_mergeable(val);
  }
  recursionMark = 0;
}

CubitVector RefGroup::center_point()
{ return bounding_box().center(); }


int RefGroup::subtract(RefGroup *group_to_subtract, RefGroup *target_group)
{
  // Get the list of RefEntities associated with each group
  DLIList<RefEntity*> final_list;
  DLIList<RefEntity*> entity_list_2;

  get_child_ref_entities(final_list);
  group_to_subtract->get_child_ref_entities(entity_list_2);

  // At this point, we have three groups, all non-null. Note that
  // some or all of these may be the same group, so don't destroy
  // the target group until all information is generated.
  
  // 1. Final = group 2
  // 2. Remove from final all items that are in both final and group 1
  for (int i=entity_list_2.size(); i > 0; i--) {
    RefEntity *entity = entity_list_2.get_and_step();
    final_list.remove(entity);
  }

  target_group->remove_all_ref_entities();
  target_group->add_ref_entity(final_list);

  return CUBIT_SUCCESS;
}

int RefGroup::intersect(RefGroup *other_group, RefGroup *target_group)
{
  // Get the list of RefEntities associated with each group
  DLIList<RefEntity*> entity_list_1;
  DLIList<RefEntity*> entity_list_2;

  get_child_ref_entities(entity_list_1);
  other_group->get_child_ref_entities(entity_list_2);

  DLIList<RefEntity*> final_list;

  // At this point, we have three groups, all non-null. Note that
  // some or all of these may be the same group, so don't destroy
  // the target group until all information is generated.
  
  // Final = all items in both group 1 and group 2.
  for (int i=entity_list_2.size(); i > 0; i--) {
    RefEntity *entity = entity_list_2.get_and_step();
    if (entity_list_1.move_to(entity)) {
      final_list.append_unique(entity);
    }
  }

  target_group->remove_all_ref_entities();
  target_group->add_ref_entity(final_list);

  return CUBIT_SUCCESS;
}
  
int RefGroup::unite(RefGroup *other_group, RefGroup *target_group)
{
  // Get the list of RefEntities associated with each group
  DLIList<RefEntity*> final_list;
  DLIList<RefEntity*> entity_list_2;

  get_child_ref_entities(final_list);
  other_group->get_child_ref_entities(entity_list_2);

  // At this point, we have three groups, all non-null. Note that
  // some or all of these may be the same group, so don't destroy
  // the target group until all information is generated.
  
  // 1. Final = group 1
  // 2. Add all items that are in group 2, but not already in group 1
  for (int i=entity_list_2.size(); i > 0; i--) {
    RefEntity *entity = entity_list_2.get_and_step();
    final_list.append_unique(entity);
  }
  
  target_group->remove_all_ref_entities();
  target_group->add_ref_entity(final_list);

  return CUBIT_SUCCESS;
}

int RefGroup::validate()
{
  // NOTE: RefGroup::validate() should not call RefEntity::validate()
  //       directly since the contained entities will make that
  //       call in their respective validate() functions.

  recursionMark = 1;
  int error = 0;
  for (int i = entityList.size(); i > 0; i--) {
    RefEntity *entity = entityList.get_and_step();
    RefGroup *group = CAST_TO(entity, RefGroup);
    if (!group || group->recursionMark == 0) 
      error += entity->validate();
  }
  recursionMark = 0;
  return error;
}

CubitStatus RefGroup::delete_group(RefGroup *group_ptr, CubitBoolean propagate)
{
   // This function will delete the corresponding group from the Model
   // If propagate is CUBIT_TRUE, the contained groups are deleted also.

   AppUtil::instance()->send_event(GeometryEvent(GeometryEvent::TOPOLOGY_ENTITY_DESTRUCTED, group_ptr));

   if( propagate )
   {
      DLIList<RefGroup*> contained_groups;
      get_contained_groups ( group_ptr, contained_groups );
      for( int i=0; i<contained_groups.size(); i++ )
      {
         delete_group( contained_groups.get_and_step(), false );
      }
   }
   
   group_ptr->remove_all_ref_entities();
   delete group_ptr;

   return CUBIT_SUCCESS;
}

void RefGroup::delete_all_groups()
{
   DLIList<RefGroup*> ref_groups;
   GeometryQueryTool::instance()->ref_groups(ref_groups);
   int num_groups = ref_groups.size();
   for( int i=0; i<num_groups; i++ )
      delete_group( ref_groups.get_and_step() );
   
}

void RefGroup::get_contained_groups (RefGroup *group_ptr, DLIList<RefGroup*> &contained_groups)
{
   DLIList<RefEntity*> group_list, mixed_list;
   RefEntity *ref_entity_ptr;
   RefGroup *group;
   
   mixed_list.append( group_ptr );
   
   // Using expand_group algorithm to get sub_groups of group_ptr
   // don't swap the upper and lower bounds on ii.
   for ( int ii = 0; ii < mixed_list.size(); ii++ )
   {
      // Note, the list gets added to, so don't do a simple get_and_step
      mixed_list.reset();
      mixed_list.step(ii);
      ref_entity_ptr = mixed_list.get();
      
      group = CAST_TO( ref_entity_ptr, RefGroup ); //which it should be the first time around
      if (group)
      {
         // avoid infinite recursion of groups A and B containing each other
         if (!group_list.move_to(group)) {
            group->get_child_ref_entities(mixed_list);
            group_list.append(group);
         }  
      }
   }
   CAST_LIST(group_list, contained_groups, RefGroup);
}

void RefGroup::get_groups_within( CubitEntity* cubit_entity_ptr, 
                                  DLIList<RefGroup*> &groups_within,
                                  const CubitBoolean recursive)
{
  RefEntity* ref_entity_ptr = CAST_TO( cubit_entity_ptr, RefEntity );
  if( ref_entity_ptr != NULL )
  {
    get_groups_within( ref_entity_ptr, groups_within, recursive);
    return;
  }

  return;
}

void RefGroup::get_groups_within( RefEntity* ref_entity_ptr, 
                                  DLIList<RefGroup*> &groups_within,
                                  const CubitBoolean recursive)
{
  RefGroup* ref_group_ptr;
  
  // Get the observer's of this entity
  DLIList <CubitObserver*> observer_list;
  ref_entity_ptr->get_observer_list( observer_list );
  
  // Now, loop through the observer list, adding them
  for (int i=0; i<observer_list.size(); i++)
  {
    ref_group_ptr = CAST_TO (observer_list.get(), RefGroup);
    
    if( ref_group_ptr != NULL )
    {
      // Add this observer
      groups_within.append_unique( ref_group_ptr );
      
      // Add all the groups the observer is within
      if (recursive) get_groups_within( ref_group_ptr, groups_within );
    }
    observer_list.step();
  }
}

// Here we need to move through all the model's group's, since observer's
// aren't recursive (i.e., if group 2 contains group 3 which contains
// hex 1, then hex 1's observer is only group 3.  Group 2 actually
// contains hex 1 however, but through group 3).
void RefGroup::get_groups_within( RefGroup* ref_group_ptr, 
                                  DLIList<RefGroup*> &groups_within,
                                  const CubitBoolean recursive)
{
  // Get the model's groups
  DLIList<RefGroup*> model_group_list;
  GeometryQueryTool::instance()->ref_groups(model_group_list);
  int i, j;
  RefGroup* model_group;
  RefGroup* contained_group;
  
  // Go through all the groups, finding which ones contain
  // the entity, at any level below (checks groups within
  // groups).
  for( i=0; i<model_group_list.size(); i++ )
  {
    model_group = model_group_list.get_and_step();
    
    // See if it exists directly in model_group
    DLIList<RefEntity*> contained_ref_entities;
    model_group->get_child_ref_entities( contained_ref_entities );
    if( contained_ref_entities.move_to( (RefEntity*)ref_group_ptr ) )
      groups_within.append_unique( model_group );

    if (!recursive) continue;
    
    // Now check all of model group's contained groups
    DLIList<RefGroup*> contained_group_list;
    model_group->get_contained_groups( model_group, contained_group_list );
    contained_group_list.remove( model_group );
    for( j=0; j<contained_group_list.size(); j++ )
    {
      contained_group = contained_group_list.get_and_step();
      
      contained_ref_entities.clean_out();
      contained_group->get_child_ref_entities( contained_ref_entities );
      
      if( contained_ref_entities.move_to( (RefEntity*)ref_group_ptr ) )
        groups_within.append_unique( model_group );
    }
  }
}

void RefGroup::notify_observer(const CubitEvent *observer_event)
{
  const GeometryEvent* geom_event = dynamic_cast<const GeometryEvent*>(observer_event);
  if (geom_event != NULL)
  {
    RefEntity *entity = geom_event->get_entity();

    int event = geom_event->get_type();
    if (event == GeometryEvent::TOPOLOGY_ENTITY_CONSTRUCTED) {
      add_ref_entity(entity);
      return;
    }
    else if (event ==GeometryEvent:: TOPOLOGY_ENTITY_DESTRUCTED) {
      remove_ref_entity(entity);
      return;
    }
    else if ( event == GeometryEvent::ENTITIES_MERGED )
    {
        // Out with the old...
      remove_ref_entity(entity);

        // ...in with the new.
      const MergeEvent *merge_event = static_cast<const MergeEvent*>(geom_event);
      RefEntity *kept_entity = 
        CAST_TO( merge_event->get_kept_entity(), RefEntity );
      add_ref_entity(kept_entity);
    }
  }
  return;
}

/*
void RefGroup::draw (int color)
{
   recursionMark = 1;
   if (is_visible())
   {
      for (int i = entityList.size(); i > 0; i--)
      {
         RefEntity *entity = entityList.get_and_step();
         RefGroup *group = CAST_TO(entity, RefGroup);
         if (!group || group->recursionMark == 0)
            entity->draw(color);
      }
   }
   recursionMark = 0;
}
*/