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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024 | /**
* MOAB, a Mesh-Oriented datABase, is a software component for creating,
* storing and accessing finite element mesh data.
*
* Copyright 2004 Sandia Corporation. Under the terms of Contract
* DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
* retains certain rights in this software.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*/
#ifdef WIN32
#pragma warning( disable : 4786 )
#endif
#include "CGMConfig.h"
#include "GeometryQueryTool.hpp"
#include "ModelQueryEngine.hpp"
#include "RefEntityName.hpp"
#include "GMem.hpp"
#include "RefGroup.hpp"
#include "RefVolume.hpp"
#include "RefFace.hpp"
#include "RefEdge.hpp"
#include "RefVertex.hpp"
#include "SenseEntity.hpp"
#include "Surface.hpp"
#include "Curve.hpp"
#include "Body.hpp"
#include "InitCGMA.hpp"
#include "moab/Core.hpp"
#include "moab/Interface.hpp"
#include "moab/Range.hpp"
#include "MBTagConventions.hpp"
#include "moab/FileOptions.hpp"
#include "moab/GeomTopoTool.hpp"
#include "CubitCompat.hpp"
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <iostream>
#include "ReadCGM.hpp"
#include "moab/ReadUtilIface.hpp"
namespace moab
{
#define GF_CUBIT_FILE_TYPE "CUBIT"
#define GF_STEP_FILE_TYPE "STEP"
#define GF_IGES_FILE_TYPE "IGES"
#define GF_OCC_BREP_FILE_TYPE "OCC"
#define GF_FACET_FILE_TYPE "FACET"
ReaderIface* ReadCGM::factory( Interface* iface )
{
return new ReadCGM( iface );
}
ReadCGM::ReadCGM( Interface* impl )
: geom_tag( 0 ), id_tag( 0 ), name_tag( 0 ), category_tag( 0 ), faceting_tol_tag( 0 ), geometry_resabs_tag( 0 )
{
assert( NULL != impl );
mdbImpl = impl;
myGeomTool = new GeomTopoTool( impl );
impl->query_interface( readUtilIface );
assert( NULL != readUtilIface );
// initialise counters
failed_curve_count = 0;
failed_surface_count = 0;
ErrorCode rval;
// get some tag handles
int negone = -1, zero = 0 /*, negonearr[] = {-1, -1, -1, -1}*/;<--- Variable 'zero' is assigned a value that is never used.
rval = mdbImpl->tag_get_handle( GEOM_DIMENSION_TAG_NAME, 1, MB_TYPE_INTEGER, geom_tag, MB_TAG_SPARSE | MB_TAG_CREAT,
&negone );
assert( !rval );
id_tag = mdbImpl->globalId_tag();
rval =
mdbImpl->tag_get_handle( NAME_TAG_NAME, NAME_TAG_SIZE, MB_TYPE_OPAQUE, name_tag, MB_TAG_SPARSE | MB_TAG_CREAT );
assert( !rval );
rval = mdbImpl->tag_get_handle( CATEGORY_TAG_NAME, CATEGORY_TAG_SIZE, MB_TYPE_OPAQUE, category_tag,
MB_TAG_SPARSE | MB_TAG_CREAT );
assert( !rval );
rval = mdbImpl->tag_get_handle( "FACETING_TOL", 1, MB_TYPE_DOUBLE, faceting_tol_tag, MB_TAG_SPARSE | MB_TAG_CREAT );
assert( !rval );
rval = mdbImpl->tag_get_handle( "GEOMETRY_RESABS", 1, MB_TYPE_DOUBLE, geometry_resabs_tag,
MB_TAG_SPARSE | MB_TAG_CREAT );
assert( !rval );
#ifdef NDEBUG
if( !rval )
{
}; // Line to avoid compiler warning about variable set but not used
#endif
}
ReadCGM::~ReadCGM()
{
mdbImpl->release_interface( readUtilIface );
delete myGeomTool;
}
ErrorCode ReadCGM::read_tag_values( const char* /* file_name */,
const char* /* tag_name */,
const FileOptions& /* opts */,
std::vector< int >& /* tag_values_out */,
const SubsetList* /* subset_list */ )
{
return MB_NOT_IMPLEMENTED;
}
// Sets options passed into ReadCGM::load_file
ErrorCode ReadCGM::set_options( const FileOptions& opts,
int& norm_tol,
double& faceting_tol,
double& len_tol,
bool& act_att,
bool& verbose_warnings,
bool& fatal_on_curves )
{
ErrorCode rval;
// Default Values
int DEFAULT_NORM = 5;<--- The scope of the variable 'DEFAULT_NORM' can be reduced. [+]The scope of the variable 'DEFAULT_NORM' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
double DEFAULT_FACET_TOL = 0.001;<--- The scope of the variable 'DEFAULT_FACET_TOL' can be reduced. [+]The scope of the variable 'DEFAULT_FACET_TOL' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
double DEFAULT_LEN_TOL = 0.0;<--- The scope of the variable 'DEFAULT_LEN_TOL' can be reduced. [+]The scope of the variable 'DEFAULT_LEN_TOL' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
act_att = true;
// Check for the options.
if( MB_SUCCESS != opts.get_int_option( "FACET_NORMAL_TOLERANCE", norm_tol ) ) norm_tol = DEFAULT_NORM;
if( MB_SUCCESS != opts.get_real_option( "FACET_DISTANCE_TOLERANCE", faceting_tol ) )
faceting_tol = DEFAULT_FACET_TOL;
if( MB_SUCCESS != opts.get_real_option( "MAX_FACET_EDGE_LENGTH", len_tol ) ) len_tol = DEFAULT_LEN_TOL;
if( MB_SUCCESS == opts.get_null_option( "VERBOSE_CGM_WARNINGS" ) ) verbose_warnings = true;
if( MB_SUCCESS == opts.get_null_option( "FATAL_ON_CURVES" ) ) fatal_on_curves = true;
const char* name = "CGM_ATTRIBS";
const char* value = "no";
rval = opts.match_option( name, value );
if( MB_SUCCESS == rval ) act_att = false;
return MB_SUCCESS;
}
ErrorCode ReadCGM::create_entity_sets( std::map< RefEntity*, EntityHandle > ( &entmap )[5] )
{
ErrorCode rval;
const char geom_categories[][CATEGORY_TAG_SIZE] = { "Vertex\0", "Curve\0", "Surface\0", "Volume\0", "Group\0" };
const char* const names[] = { "Vertex", "Curve", "Surface", "Volume" };
DLIList< RefEntity* > entlist;
for( int dim = 0; dim < 4; dim++ )
{
entlist.clean_out();
GeometryQueryTool::instance()->ref_entity_list( names[dim], entlist, true );
entlist.reset();
for( int i = entlist.size(); i--; )
{
RefEntity* ent = entlist.get_and_step();
EntityHandle handle;
// Create the new meshset
rval = mdbImpl->create_meshset( dim == 1 ? MESHSET_ORDERED : MESHSET_SET, handle );
if( MB_SUCCESS != rval ) return rval;
// Map the geom reference entity to the corresponding moab meshset
entmap[dim][ent] = handle;
// Create tags for the new meshset
rval = mdbImpl->tag_set_data( geom_tag, &handle, 1, &dim );
if( MB_SUCCESS != rval ) return rval;
int id = ent->id();
rval = mdbImpl->tag_set_data( id_tag, &handle, 1, &id );
if( MB_SUCCESS != rval ) return rval;
rval = mdbImpl->tag_set_data( category_tag, &handle, 1, &geom_categories[dim] );
if( MB_SUCCESS != rval ) return rval;
}
}
return MB_SUCCESS;
}
ErrorCode ReadCGM::create_topology( std::map< RefEntity*, EntityHandle > ( &entitymap )[5] )
{
ErrorCode rval;
DLIList< RefEntity* > entitylist;
std::map< RefEntity*, EntityHandle >::iterator ci;
for( int dim = 1; dim < 4; ++dim )
{
for( ci = entitymap[dim].begin(); ci != entitymap[dim].end(); ++ci )
{
entitylist.clean_out();
ci->first->get_child_ref_entities( entitylist );
entitylist.reset();
for( int i = entitylist.size(); i--; )
{
RefEntity* ent = entitylist.get_and_step();
EntityHandle h = entitymap[dim - 1][ent];
rval = mdbImpl->add_parent_child( ci->second, h );
if( MB_SUCCESS != rval ) return rval;
}
}
}
return MB_SUCCESS;
}
ErrorCode ReadCGM::store_surface_senses( std::map< RefEntity*, EntityHandle >& surface_map,
std::map< RefEntity*, EntityHandle >& volume_map )
{
ErrorCode rval;
std::map< RefEntity*, EntityHandle >::iterator ci;
for( ci = surface_map.begin(); ci != surface_map.end(); ++ci )
{
RefFace* face = (RefFace*)( ci->first );
BasicTopologyEntity *forward = 0, *reverse = 0;
for( SenseEntity* cf = face->get_first_sense_entity_ptr(); cf; cf = cf->next_on_bte() )
{
BasicTopologyEntity* vol = cf->get_parent_basic_topology_entity_ptr();
// Allocate vol to the proper topology entity (forward or reverse)
if( cf->get_sense() == CUBIT_UNKNOWN || cf->get_sense() != face->get_surface_ptr()->bridge_sense() )
{
// Check that each surface has a sense for only one volume
if( reverse )
{
std::cout << "Surface " << face->id() << " has reverse sense "
<< "with multiple volume " << reverse->id() << " and "
<< "volume " << vol->id() << std::endl;
return MB_FAILURE;
}
reverse = vol;
}
if( cf->get_sense() == CUBIT_UNKNOWN || cf->get_sense() == face->get_surface_ptr()->bridge_sense() )
{
// Check that each surface has a sense for only one volume
if( forward )
{
std::cout << "Surface " << face->id() << " has forward sense "
<< "with multiple volume " << forward->id() << " and "
<< "volume " << vol->id() << std::endl;
return MB_FAILURE;
}
forward = vol;
}
}
if( forward )
{
rval = myGeomTool->set_sense( ci->second, volume_map[forward], SENSE_FORWARD );
if( MB_SUCCESS != rval ) return rval;
}
if( reverse )
{
rval = myGeomTool->set_sense( ci->second, volume_map[reverse], SENSE_REVERSE );
if( MB_SUCCESS != rval ) return rval;
}
}
return MB_SUCCESS;
}
ErrorCode ReadCGM::store_curve_senses( std::map< RefEntity*, EntityHandle >& curve_map,
std::map< RefEntity*, EntityHandle >& surface_map )
{
ErrorCode rval;
std::vector< EntityHandle > ents;
std::vector< int > senses;
std::map< RefEntity*, EntityHandle >::iterator ci;
for( ci = curve_map.begin(); ci != curve_map.end(); ++ci )
{
RefEdge* edge = (RefEdge*)( ci->first );
ents.clear();
senses.clear();
for( SenseEntity* ce = edge->get_first_sense_entity_ptr(); ce; ce = ce->next_on_bte() )
{
BasicTopologyEntity* fac = ce->get_parent_basic_topology_entity_ptr();
EntityHandle face = surface_map[fac];
if( ce->get_sense() == CUBIT_UNKNOWN || ce->get_sense() != edge->get_curve_ptr()->bridge_sense() )
{
ents.push_back( face );
senses.push_back( SENSE_REVERSE );
}
if( ce->get_sense() == CUBIT_UNKNOWN || ce->get_sense() == edge->get_curve_ptr()->bridge_sense() )
{
ents.push_back( face );
senses.push_back( SENSE_FORWARD );
}
}
rval = myGeomTool->set_senses( ci->second, ents, senses );
if( MB_SUCCESS != rval ) return rval;
}
return MB_SUCCESS;
}
ErrorCode ReadCGM::store_groups( std::map< RefEntity*, EntityHandle > ( &entitymap )[5] )
{
ErrorCode rval;
// Create entity sets for all ref groups
rval = create_group_entsets( entitymap[4] );
if( rval != MB_SUCCESS ) return rval;
// Store group names and entities in the mesh
rval = store_group_content( entitymap );
if( rval != MB_SUCCESS ) return rval;
return MB_SUCCESS;
}
ErrorCode ReadCGM::create_group_entsets( std::map< RefEntity*, EntityHandle >& group_map )
{
ErrorCode rval;
const char geom_categories[][CATEGORY_TAG_SIZE] = { "Vertex\0", "Curve\0", "Surface\0", "Volume\0", "Group\0" };
DLIList< RefEntity* > entitylist;
// Create entity sets for all ref groups
std::vector< Tag > extra_name_tags;
#if CGM_MAJOR_VERSION > 13
DLIList< CubitString > name_list;
#else
DLIList< CubitString* > name_list;
#endif
entitylist.clean_out();
// Get all entity groups from the CGM model
GeometryQueryTool::instance()->ref_entity_list( "group", entitylist );
entitylist.reset();
// Loop over all groups
for( int i = entitylist.size(); i--; )
{
// Take the next group
RefEntity* grp = entitylist.get_and_step();
name_list.clean_out();
// Get the names of all entities in this group from the solid model
#if CGM_MAJOR_VERSION > 13
RefEntityName::instance()->get_refentity_name( grp, name_list );
#else
// True argument is optional, but for large multi-names situation, it should save
// some cpu time
RefEntityName::instance()->get_refentity_name( grp, name_list, true );
#endif
if( name_list.size() == 0 ) continue;
// Set pointer to first name of the group and set the first name to name1
name_list.reset();
#if CGM_MAJOR_VERSION > 13
CubitString name1 = name_list.get();
#else
CubitString name1 = *name_list.get();
#endif
// Create entity handle for the group
EntityHandle h;
rval = mdbImpl->create_meshset( MESHSET_SET, h );
if( MB_SUCCESS != rval ) return rval;
// Set tag data for the group
char namebuf[NAME_TAG_SIZE];
memset( namebuf, '\0', NAME_TAG_SIZE );
strncpy( namebuf, name1.c_str(), NAME_TAG_SIZE - 1 );
if( name1.length() >= (unsigned)NAME_TAG_SIZE )
std::cout << "WARNING: group name '" << name1.c_str() << "' truncated to '" << namebuf << "'" << std::endl;
rval = mdbImpl->tag_set_data( name_tag, &h, 1, namebuf );
if( MB_SUCCESS != rval ) return MB_FAILURE;
int id = grp->id();
rval = mdbImpl->tag_set_data( id_tag, &h, 1, &id );
if( MB_SUCCESS != rval ) return MB_FAILURE;
rval = mdbImpl->tag_set_data( category_tag, &h, 1, &geom_categories[4] );
if( MB_SUCCESS != rval ) return MB_FAILURE;
// Check for extra group names
if( name_list.size() > 1 )
{
for( int j = extra_name_tags.size(); j < name_list.size(); ++j )
{
sprintf( namebuf, "EXTRA_%s%d", NAME_TAG_NAME, j );
Tag t;
rval =
mdbImpl->tag_get_handle( namebuf, NAME_TAG_SIZE, MB_TYPE_OPAQUE, t, MB_TAG_SPARSE | MB_TAG_CREAT );
assert( !rval );
extra_name_tags.push_back( t );
}
// Add extra group names to the group handle
for( int j = 0; j < name_list.size(); ++j )
{
#if CGM_MAJOR_VERSION > 13
name1 = name_list.get_and_step();
#else
name1 = *name_list.get_and_step();
#endif
memset( namebuf, '\0', NAME_TAG_SIZE );
strncpy( namebuf, name1.c_str(), NAME_TAG_SIZE - 1 );
if( name1.length() >= (unsigned)NAME_TAG_SIZE )
std::cout << "WARNING: group name '" << name1.c_str() << "' truncated to '" << namebuf << "'"
<< std::endl;
rval = mdbImpl->tag_set_data( extra_name_tags[j], &h, 1, namebuf );
if( MB_SUCCESS != rval ) return MB_FAILURE;
}
}
// Add the group handle
group_map[grp] = h;
}
return MB_SUCCESS;
}
ErrorCode ReadCGM::store_group_content( std::map< RefEntity*, EntityHandle > ( &entitymap )[5] )
{
ErrorCode rval;
DLIList< RefEntity* > entlist;
std::map< RefEntity*, EntityHandle >::iterator ci;
// Store contents for each group
entlist.reset();
for( ci = entitymap[4].begin(); ci != entitymap[4].end(); ++ci )
{
RefGroup* grp = (RefGroup*)( ci->first );
entlist.clean_out();
grp->get_child_ref_entities( entlist );
Range entities;
while( entlist.size() )
{
RefEntity* ent = entlist.pop();
int dim = ent->dimension();
if( dim < 0 )
{
Body* body;
if( entitymap[4].find( ent ) != entitymap[4].end() )
{
// Child is another group; examine its contents
entities.insert( entitymap[4][ent] );
}
else if( ( body = dynamic_cast< Body* >( ent ) ) != NULL )
{
// Child is a CGM Body, which presumably comprises some volumes--
// extract volumes as if they belonged to group.
DLIList< RefVolume* > vols;
body->ref_volumes( vols );
for( int vi = vols.size(); vi--; )
{
RefVolume* vol = vols.get_and_step();
if( entitymap[3].find( vol ) != entitymap[3].end() )
{
entities.insert( entitymap[3][vol] );
}
else
{
std::cerr << "Warning: CGM Body has orphan RefVolume" << std::endl;
}
}
}
else
{
// Otherwise, warn user.
std::cerr << "Warning: A dim<0 entity is being ignored by ReadCGM." << std::endl;
}
}
else if( dim < 4 )
{
if( entitymap[dim].find( ent ) != entitymap[dim].end() ) entities.insert( entitymap[dim][ent] );
}
}
if( !entities.empty() )
{
rval = mdbImpl->add_entities( ci->second, entities );
if( MB_SUCCESS != rval ) return MB_FAILURE;
}
}
return MB_SUCCESS;
}
void ReadCGM::set_cgm_attributes( bool const act_attributes, bool const verbose )
{
if( act_attributes )
{
CGMApp::instance()->attrib_manager()->set_all_auto_read_flags( act_attributes );
CGMApp::instance()->attrib_manager()->set_all_auto_actuate_flags( act_attributes );
}
if( !verbose )
{
CGMApp::instance()->attrib_manager()->silent_flag( true );
}
}
ErrorCode ReadCGM::create_vertices( std::map< RefEntity*, EntityHandle >& vertex_map )
{
ErrorCode rval;
std::map< RefEntity*, EntityHandle >::iterator ci;
for( ci = vertex_map.begin(); ci != vertex_map.end(); ++ci )
{
CubitVector pos = dynamic_cast< RefVertex* >( ci->first )->coordinates();
double coords[3] = { pos.x(), pos.y(), pos.z() };
EntityHandle vh;
rval = mdbImpl->create_vertex( coords, vh );
if( MB_SUCCESS != rval ) return MB_FAILURE;
// Add the vertex to its tagged meshset
rval = mdbImpl->add_entities( ci->second, &vh, 1 );
if( MB_SUCCESS != rval ) return MB_FAILURE;
// Replace the meshset handle with the vertex handle
// This makes adding the vertex to higher dim sets easier
ci->second = vh;
}
return MB_SUCCESS;
}
ErrorCode ReadCGM::create_curve_facets( std::map< RefEntity*, EntityHandle >& curve_map,
std::map< RefEntity*, EntityHandle >& vertex_map,
#if CGM_MAJOR_VERSION > 12
int norm_tol,
#else
int /* norm_tol */,
#endif
double faceting_tol,
bool verbose_warn,
bool fatal_on_curves )
{
ErrorCode rval;
CubitStatus s;
// Maximum allowable curve-endpoint proximity warnings
// If this integer becomes negative, then abs(curve_warnings) is the
// number of warnings that were suppressed.
int curve_warnings = 0;
// Map iterator
std::map< RefEntity*, EntityHandle >::iterator ci;
// Create geometry for all curves
GMem data;
for( ci = curve_map.begin(); ci != curve_map.end(); ++ci )
{
// Get the start and end points of the curve in the form of a reference edge
RefEdge* edge = dynamic_cast< RefEdge* >( ci->first );
// Get the edge's curve information
Curve* curve = edge->get_curve_ptr();
// Clean out previous curve information
data.clean_out();
// Facet curve according to parameters and CGM version
#if CGM_MAJOR_VERSION > 12
s = edge->get_graphics( data, norm_tol, faceting_tol );
#else
s = edge->get_graphics( data, faceting_tol );
#endif
if( s != CUBIT_SUCCESS )
{
// if we fatal on curves
if( fatal_on_curves )
{
std::cout << "Failed to facet the curve " << edge->id() << std::endl;
return MB_FAILURE;
}
// otherwise record them
else
{
failed_curve_count++;
failed_curves.push_back( edge->id() );
}
continue;
}
std::vector< CubitVector > points;
for( int i = 0; i < data.pointListCount; ++i )
// Add Cubit vertext points to a list
points.push_back( CubitVector( data.point_list()[i].x, data.point_list()[i].y, data.point_list()[i].z ) );
// Need to reverse data?
if( curve->bridge_sense() == CUBIT_REVERSED ) std::reverse( points.begin(), points.end() );
// Check for closed curve
RefVertex *start_vtx, *end_vtx;
start_vtx = edge->start_vertex();
end_vtx = edge->end_vertex();
// Special case for point curve
if( points.size() < 2 )
{
if( start_vtx != end_vtx || curve->measure() > GEOMETRY_RESABS )
{
std::cerr << "Warning: No facetting for curve " << edge->id() << std::endl;
continue;
}
EntityHandle h = vertex_map[start_vtx];
rval = mdbImpl->add_entities( ci->second, &h, 1 );
if( MB_SUCCESS != rval ) return MB_FAILURE;
continue;
}
// Check to see if the first and last interior vertices are considered to be
// coincident by CUBIT
const bool closed = ( points.front() - points.back() ).length() < GEOMETRY_RESABS;
if( closed != ( start_vtx == end_vtx ) )
{
std::cerr << "Warning: topology and geometry inconsistant for possibly closed curve " << edge->id()
<< std::endl;
}
// Check proximity of vertices to end coordinates
if( ( start_vtx->coordinates() - points.front() ).length() > GEOMETRY_RESABS ||
( end_vtx->coordinates() - points.back() ).length() > GEOMETRY_RESABS )
{
curve_warnings--;
if( curve_warnings >= 0 || verbose_warn )
{
std::cerr << "Warning: vertices not at ends of curve " << edge->id() << std::endl;
if( curve_warnings == 0 && !verbose_warn )
{
std::cerr << " further instances of this warning will be suppressed..." << std::endl;
}
}
}
// Create interior points
std::vector< EntityHandle > verts, edges;
verts.push_back( vertex_map[start_vtx] );
for( size_t i = 1; i < points.size() - 1; ++i )
{
double coords[] = { points[i].x(), points[i].y(), points[i].z() };
EntityHandle h;
// Create vertex entity
rval = mdbImpl->create_vertex( coords, h );
if( MB_SUCCESS != rval ) return MB_FAILURE;
verts.push_back( h );
}
verts.push_back( vertex_map[end_vtx] );
// Create edges
for( size_t i = 0; i < verts.size() - 1; ++i )
{
EntityHandle h;
rval = mdbImpl->create_element( MBEDGE, &verts[i], 2, h );
if( MB_SUCCESS != rval ) return MB_FAILURE;
edges.push_back( h );
}
// If closed, remove duplicate
if( verts.front() == verts.back() ) verts.pop_back();
// Add entities to the curve meshset from entitymap
rval = mdbImpl->add_entities( ci->second, &verts[0], verts.size() );
if( MB_SUCCESS != rval ) return MB_FAILURE;
rval = mdbImpl->add_entities( ci->second, &edges[0], edges.size() );
if( MB_SUCCESS != rval ) return MB_FAILURE;
}
if( !verbose_warn && curve_warnings < 0 )
{
std::cerr << "Suppressed " << -curve_warnings << " 'vertices not at ends of curve' warnings." << std::endl;
std::cerr << "To see all warnings, use reader param VERBOSE_CGM_WARNINGS." << std::endl;
}
return MB_SUCCESS;
}
ErrorCode ReadCGM::create_surface_facets( std::map< RefEntity*, EntityHandle >& surface_map,
std::map< RefEntity*, EntityHandle >& vertex_map,
int norm_tol,
double facet_tol,
double length_tol )
{
ErrorCode rval;
std::map< RefEntity*, EntityHandle >::iterator ci;
CubitStatus s;
#if( ( CGM_MAJOR_VERSION == 14 && CGM_MINOR_VERSION > 2 ) || CGM_MAJOR_VERSION >= 15 )
DLIList< TopologyEntity* > me_list;
#else
DLIList< ModelEntity* > me_list;
#endif
GMem data;
// Create geometry for all surfaces
for( ci = surface_map.begin(); ci != surface_map.end(); ++ci )
{
RefFace* face = dynamic_cast< RefFace* >( ci->first );
data.clean_out();
s = face->get_graphics( data, norm_tol, facet_tol, length_tol );
if( CUBIT_SUCCESS != s ) return MB_FAILURE;
// Declare array of all vertex handles
std::vector< EntityHandle > verts( data.pointListCount, 0 );
// Get list of geometric vertices in surface
me_list.clean_out();
ModelQueryEngine::instance()->query_model( *face, DagType::ref_vertex_type(), me_list );
// For each geometric vertex, find a single coincident point in facets
// Otherwise, print a warning
for( int i = me_list.size(); i--; )
{
// Assign geometric vertex
RefVertex* vtx = dynamic_cast< RefVertex* >( me_list.get_and_step() );
CubitVector pos = vtx->coordinates();
for( int j = 0; j < data.pointListCount; ++j )
{
// Assign facet vertex
CubitVector vpos( data.point_list()[j].x, data.point_list()[j].y, data.point_list()[j].z );
// Check to see if they are considered coincident
if( ( pos - vpos ).length_squared() < GEOMETRY_RESABS * GEOMETRY_RESABS )
{
// If this facet vertex has already been found coincident, print warning
if( verts[j] ) std::cerr << "Warning: Coincident vertices in surface " << face->id() << std::endl;
// If a coincidence is found, keep track of it in the verts vector
verts[j] = vertex_map[vtx];
break;
}
}
}
// Now create vertices for the remaining points in the facetting
for( int i = 0; i < data.pointListCount; ++i )
{
if( verts[i] ) // If a geometric vertex
continue;
double coords[] = { data.point_list()[i].x, data.point_list()[i].y, data.point_list()[i].z };
// Return vertex handle to verts to fill in all remaining facet
// vertices
rval = mdbImpl->create_vertex( coords, verts[i] );
if( MB_SUCCESS != rval ) return rval;
}
// record the failures for information
if( data.fListCount == 0 )
{
failed_surface_count++;
failed_surfaces.push_back( face->id() );
}
// Now create facets
Range facets;
std::vector< EntityHandle > corners;
for( int i = 0; i < data.fListCount; i += data.facet_list()[i] + 1 )
{
// Get number of facet verts
int* facet = data.facet_list() + i;
corners.resize( *facet );
for( int j = 1; j <= *facet; ++j )
{
if( facet[j] >= (int)verts.size() )
{
std::cerr << "ERROR: Invalid facet data for surface " << face->id() << std::endl;
return MB_FAILURE;
}
corners[j - 1] = verts[facet[j]];
}
EntityType type;
if( *facet == 3 )
type = MBTRI;
else
{
std::cerr << "Warning: non-triangle facet in surface " << face->id() << std::endl;
std::cerr << " entity has " << *facet << " edges" << std::endl;
if( *facet == 4 )
type = MBQUAD;
else
type = MBPOLYGON;
}
// if (surf->bridge_sense() == CUBIT_REVERSED)
// std::reverse(corners.begin(), corners.end());
EntityHandle h;
rval = mdbImpl->create_element( type, &corners[0], corners.size(), h );
if( MB_SUCCESS != rval ) return MB_FAILURE;
facets.insert( h );
}
// Add vertices and facets to surface set
rval = mdbImpl->add_entities( ci->second, &verts[0], verts.size() );
if( MB_SUCCESS != rval ) return MB_FAILURE;
rval = mdbImpl->add_entities( ci->second, facets );
if( MB_SUCCESS != rval ) return MB_FAILURE;
}
return MB_SUCCESS;
}
// Copy geometry into mesh database
ErrorCode ReadCGM::load_file( const char* cgm_file_name,
const EntityHandle* file_set,
const FileOptions& opts,
const ReaderIface::SubsetList* subset_list,
const Tag* /*file_id_tag*/ )
{
// Blocks_to_load and num_blocks are ignored.
ErrorCode rval;
if( subset_list )
{
MB_SET_ERR( MB_UNSUPPORTED_OPERATION, "Reading subset of files not supported for CGM data" );
}
int norm_tol;
double faceting_tol;
double len_tol;
bool act_att = true;
bool verbose_warnings = false;
bool fatal_on_curves = false;
rval = set_options( opts, norm_tol, faceting_tol, len_tol, act_att, verbose_warnings, fatal_on_curves );
if( MB_SUCCESS != rval ) return rval;
// Always tag with the faceting_tol and geometry absolute resolution
// If file_set is defined, use that, otherwise (file_set == NULL) tag the interface
EntityHandle set = file_set ? *file_set : 0;
rval = mdbImpl->tag_set_data( faceting_tol_tag, &set, 1, &faceting_tol );
if( MB_SUCCESS != rval ) return rval;
rval = mdbImpl->tag_set_data( geometry_resabs_tag, &set, 1, &GEOMETRY_RESABS );
if( MB_SUCCESS != rval ) return rval;
// Initialize CGM
InitCGMA::initialize_cgma();
// Determine CGM settings and amount of output
set_cgm_attributes( act_att, verbose_warnings );
CubitStatus s;
// Get CGM file type
const char* file_type = 0;
file_type = get_geom_file_type( cgm_file_name );
if( !file_type || !strcmp( file_type, "CUBIT" ) ) return MB_FAILURE;
s = CubitCompat_import_solid_model( cgm_file_name, file_type );
if( CUBIT_SUCCESS != s )
{
MB_SET_ERR( MB_FAILURE, cgm_file_name << ": Failed to read file of type \"" << file_type << "\"" );
}
// Create entity sets for all geometric entities
std::map< RefEntity*, EntityHandle > entmap[5]; // One for each dim, and one for groups
rval = create_entity_sets( entmap );
if( rval != MB_SUCCESS ) return rval;
// Create topology for all geometric entities
rval = create_topology( entmap );
if( rval != MB_SUCCESS ) return rval;
// Store CoFace senses
rval = store_surface_senses( entmap[2], entmap[3] );
if( rval != MB_SUCCESS ) return rval;
// Store CoEdge senses
rval = store_curve_senses( entmap[1], entmap[2] );
if( rval != MB_SUCCESS ) return rval;
// Get group information and store it in the mesh
rval = store_groups( entmap );
if( rval != MB_SUCCESS ) return rval;
// Done with volumes and groups
entmap[3].clear();
entmap[4].clear();
// Create geometry for all vertices and replace
rval = create_vertices( entmap[0] );
if( rval != MB_SUCCESS ) return rval;
// Create facets for all curves
rval = create_curve_facets( entmap[1], entmap[0], norm_tol, faceting_tol, verbose_warnings, fatal_on_curves );
if( rval != MB_SUCCESS ) return rval;
// Create facets for surfaces
rval = create_surface_facets( entmap[2], entmap[0], norm_tol, faceting_tol, len_tol );
if( rval != MB_SUCCESS ) return rval;
// print the fail information
dump_fail_counts();
return MB_SUCCESS;
}
// return the number of curves that failed to facet
int ReadCGM::get_failed_curve_count()
{
return failed_curve_count;
}
// return the number of surfaces that failed to facet
int ReadCGM::get_failed_surface_count()
{
return failed_surface_count;
}
void ReadCGM::dump_fail_counts()
{
std::cout << "***** Faceting Summary Information *****" << std::endl;
std::cout << "----- Curve Fail Information -----" << std::endl;
std::cout << "There were " << failed_curve_count << " curves that could not be faceted." << std::endl;
if( failed_curve_count > 0 )
{
std::cout << "The curves were ";
for( int i = 0; i < failed_curve_count; i++ )
{
std::cout << failed_curves[i] << " ";
if( ( i % 10 == 0 ) & ( i > 0 ) ) std::cout << std::endl;
}
}
std::cout << std::endl;
std::cout << "----- Facet Fail Information -----" << std::endl;
std::cout << "There were " << failed_surface_count << " surfaces that could not be faceted." << std::endl;
if( failed_surface_count > 0 )
{
std::cout << "The surfaces were ";
for( int i = 0; i < failed_surface_count; i++ )
{
std::cout << failed_surfaces[i] << " ";
if( ( i % 10 == 0 ) & ( i > 0 ) ) std::cout << std::endl;
}
}
std::cout << std::endl;
std::cout << "***** End of Faceting Summary Information *****" << std::endl;
return;
}
const char* ReadCGM::get_geom_file_type( const char* name )
{
FILE* file;
const char* result = 0;
file = fopen( name, "r" );
if( file )
{
result = get_geom_fptr_type( file );
fclose( file );
}
return result;
}
const char* ReadCGM::get_geom_fptr_type( FILE* file )
{
static const char* CUBIT_NAME = GF_CUBIT_FILE_TYPE;<--- The scope of the variable 'CUBIT_NAME' can be reduced. [+]The scope of the variable 'CUBIT_NAME' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
static const char* STEP_NAME = GF_STEP_FILE_TYPE;
static const char* IGES_NAME = GF_IGES_FILE_TYPE;
static const char* BREP_NAME = GF_OCC_BREP_FILE_TYPE;
static const char* FACET_NAME = GF_FACET_FILE_TYPE;
if( is_cubit_file( file ) )
return CUBIT_NAME;
else if( is_step_file( file ) )
return STEP_NAME;
else if( is_iges_file( file ) )
return IGES_NAME;
else if( is_occ_brep_file( file ) )
return BREP_NAME;
else if( is_facet_file( file ) )
return FACET_NAME;
else
return NULL;
}
int ReadCGM::is_cubit_file( FILE* file )
{
unsigned char buffer[4];
return !fseek( file, 0, SEEK_SET ) && fread( buffer, 4, 1, file ) && !memcmp( buffer, "CUBE", 4 );
}
int ReadCGM::is_step_file( FILE* file )
{
unsigned char buffer[9];
return !fseek( file, 0, SEEK_SET ) && fread( buffer, 9, 1, file ) && !memcmp( buffer, "ISO-10303", 9 );
}
int ReadCGM::is_iges_file( FILE* file )
{
unsigned char buffer[10];
return !fseek( file, 72, SEEK_SET ) && fread( buffer, 10, 1, file ) && !memcmp( buffer, "S 1", 8 );
}
int ReadCGM::is_occ_brep_file( FILE* file )
{
unsigned char buffer[6];
return !fseek( file, 0, SEEK_SET ) && fread( buffer, 6, 1, file ) && !memcmp( buffer, "DBRep_", 6 );
}
int ReadCGM::is_facet_file( FILE* file )
{
unsigned char buffer[10];
return !fseek( file, 0, SEEK_SET ) && fread( buffer, 10, 1, file ) && !memcmp( buffer, "MESH_BASED", 10 );
}
void ReadCGM::tokenize( const std::string& str, std::vector< std::string >& tokens, const char* delimiters )
{
std::string::size_type last = str.find_first_not_of( delimiters, 0 );
std::string::size_type pos = str.find_first_of( delimiters, last );
while( std::string::npos != pos && std::string::npos != last )
{
tokens.push_back( str.substr( last, pos - last ) );
last = str.find_first_not_of( delimiters, pos );
pos = str.find_first_of( delimiters, last );
if( std::string::npos == pos ) pos = str.size();
}
}
} // namespace moab
|