forked from stepcode/stepcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTEPcomplex.cc
More file actions
702 lines (623 loc) · 23.5 KB
/
STEPcomplex.cc
File metadata and controls
702 lines (623 loc) · 23.5 KB
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
#include <ctype.h>
#include <STEPcomplex.h>
#include <complexSupport.h>
#include <STEPattribute.h>
#include <sstream>
#include "sc_memmgr.h"
extern const char *
ReadStdKeyword( istream & in, std::string & buf, int skipInitWS );
STEPcomplex::STEPcomplex( Registry * registry, int fileid )
: SDAI_Application_instance( fileid, 1 ), sc( 0 ), _registry( registry ), visited( 0 ) {
head = this;
}
STEPcomplex::STEPcomplex( Registry * registry, const std::string ** names,
int fileid, const char * schnm )
: SDAI_Application_instance( fileid, 1 ), sc( 0 ), _registry( registry ), visited( 0 ) {
char * nms[BUFSIZ];
int j, k;
head = this;
// Create a char ** list of names and call Initialize to build all:
for( j = 0; names[j]; j++ ) {
nms[j] = new char[( names[j] )->length() + 1 ];
strcpy( nms[j], names[j]->c_str() );
}
nms[j] = NULL;
Initialize( ( const char ** )nms, schnm );
for( k = 0; k < j; k++ ) {
delete [] nms[k];
}
}
STEPcomplex::STEPcomplex( Registry * registry, const char ** names, int fileid,
const char * schnm )
: SDAI_Application_instance( fileid, 1 ), sc( 0 ), _registry( registry ), visited( 0 ) {
head = this;
Initialize( names, schnm );
}
/**
* Called by the STEPcomplex constructors to validate the names in our
* list, see if they represent a legal entity combination, and build the
* complex list. schnm is the name of the current schema if applicable.
* (One may be defined under the FILE_SCHEMA section of a Part 21 file
* header.) If the current schema is not the schema in which an entity
* was defined, it may be referenced by a different name in this schema.
* (This is the case if schema B USEs or REFERENCEs entity X from schema
* A and renames it to Y.) Registry::FindEntity() below knows how to
* search using the current name of each entity based on schnm.
*/
void STEPcomplex::Initialize( const char ** names, const char * schnm ) {
// Create an EntNode list consisting of all the names in the complex ent:
EntNode * ents = new EntNode( names ),
*eptr = ents, *prev = NULL, *enext;
const EntityDescriptor * enDesc;
char nm[BUFSIZ];
bool invalid = false, outOfOrder = false;
// Splice out the invalid names from our list:
while( eptr ) {
enext = eptr->next;
enDesc = _registry->FindEntity( *eptr, schnm );
if( enDesc ) {
if( enDesc->Supertypes().EntryCount() > 1 ) {
eptr->multSuprs( true );
}
if( StrCmpIns( *eptr, enDesc->Name() ) ) {
// If this entity was referred by another name rather than the
// original. May be the case if FindEntity() determined that
// eptr's name was a legal renaming of enDesc's. (Entities and
// types may be renamed with the USE/REF clause - see header
// comments.) If so, change eptr's name (since the complex
// support structs only deal with the original names) and have
// ents re-ort eptr properly in the list:
eptr->Name( StrToLower( enDesc->Name(), nm ) );
outOfOrder = true;
}
prev = eptr;
} else {
invalid = true;
cerr << "ERROR: Invalid entity \"" << eptr->Name()
<< "\" found in complex entity.\n";
if( !prev ) {
// if we need to delete the first node,
ents = eptr->next;
} else {
prev->next = eptr->next;
}
eptr->next = NULL;
// must set eptr->next to NULL or next line would del entire list
delete eptr;
}
eptr = enext;
}
// If we changed the name of any of the entities, resort:
if( outOfOrder ) {
ents->sort( &ents );
// This fn may change the value of ents if the list should have a new
// first EndNode.
}
// Set error message according to results of above:
if( invalid ) {
if( !ents ) {
// not a single legal name
_error.severity( SEVERITY_WARNING );
// SEV_WARNING - we have to skip this entity altogether, but will
// continue with the next entity.
_error.UserMsg( "No legal entity names found in instance" );
return;
}
_error.severity( SEVERITY_INCOMPLETE );
_error.UserMsg( "Some illegal entity names found in instance" );
// some illegal entity names, but some legal
}
// Check if a complex entity can be formed from the resulting combination:
if( !_registry->CompCol()->supports( ents ) ) {
_error.severity( SEVERITY_WARNING );
_error.UserMsg(
"Entity combination does not represent a legal complex entity" );
cerr << "ERROR: Could not create instance of the following complex"
<< " entity:" << endl;
eptr = ents;
while( eptr ) {
cerr << *eptr << endl;
eptr = eptr->next;
}
cerr << endl;
return;
}
// Finally, build what we can:
BuildAttrs( *ents );
for( eptr = ents->next; eptr; eptr = eptr->next ) {
AddEntityPart( *eptr );
}
AssignDerives();
delete ents;
}
STEPcomplex::~STEPcomplex() {
STEPcomplex_attr_data attr_data;
if( sc ) {
delete sc;
}
for( attr_data = _attr_data_list.begin();
attr_data != _attr_data_list.end();
attr_data ++ ) {
delete *attr_data; //FIXME deletes void*, undefined
}
_attr_data_list.clear();
}
void STEPcomplex::AssignDerives() {
STEPattribute * a = 0;
STEPcomplex * scomp1 = head;
STEPcomplex * scomp2;
const AttrDescriptorList * attrList;
AttrDescLinkNode * attrPtr;
const AttrDescriptor * ad;
while( scomp1 && scomp1->eDesc ) {
a = 0;
attrList = &( scomp1->eDesc->ExplicitAttr() );
attrPtr = ( AttrDescLinkNode * )attrList->GetHead();
// assign nm to be derived attr
// while( more derived attr for entity part )
while( attrPtr != 0 ) {
ad = attrPtr->AttrDesc();
if( ( ad->Derived() ) == LTrue ) {
const char * nm = ad->Name();
const char * attrNm = 0;
if( strrchr( nm, '.' ) ) {
attrNm = strrchr( nm, '.' );
attrNm++;
} else {
attrNm = nm;
}
scomp2 = head;
while( scomp2 && !a ) {
if( scomp1 != scomp2 ) {
scomp2->MakeDerived( attrNm );
a = scomp2->GetSTEPattribute( attrNm );
}
scomp2 = scomp2->sc;
}
}
// increment attr
attrPtr = ( AttrDescLinkNode * )attrPtr->NextNode();
}
scomp1 = scomp1->sc;
}
}
/** \fn STEPcomplex::AddEntityPart
** this function should only be called for the head entity
** in the list of entity parts.
*/
void STEPcomplex::AddEntityPart( const char * name ) {
STEPcomplex * scomplex;
if( name ) {
scomplex = new STEPcomplex( _registry, STEPfile_id );
scomplex->BuildAttrs( name );
if( scomplex->eDesc ) {
scomplex->head = this;
AppendEntity( scomplex );
} else {
cout << scomplex->_error.DetailMsg() << endl;
delete scomplex;
}
}
}
STEPcomplex * STEPcomplex::EntityPart( const char * name, const char * currSch ) {
STEPcomplex * scomp = head;
while( scomp ) {
if( scomp->eDesc ) {
if( scomp->eDesc->CurrName( name, currSch ) ) {
return scomp;
}
} else
cout << "Bug in STEPcomplex::EntityPart(): entity part has "
<< "no EntityDescriptor\n";
scomp = scomp->sc;
}
return 0;
}
int STEPcomplex::EntityExists( const char * name, const char * currSch ) {
return ( EntityPart( name, currSch ) ? 1 : 0 );
}
/**
** Check if a given entity (by descriptor) is the same as this one.
** For a complex entity, we'll check the EntityDescriptor of each entity
** in the complex 'chain'
*/
const EntityDescriptor * STEPcomplex::IsA( const EntityDescriptor * ed ) const {
const EntityDescriptor * return_ed = eDesc->IsA( ed );
if( !return_ed && sc ) {
return sc->IsA( ed );
} else {
return return_ed;
}
}
Severity STEPcomplex::ValidLevel( ErrorDescriptor * error, InstMgr * im,
int clearError ) {
(void) error; //unused
(void) im;
(void) clearError;
cout << "STEPcomplex::ValidLevel() not implemented.\n";
return SEVERITY_NULL;
}
void STEPcomplex::AppendEntity( STEPcomplex * stepc ) {
if( sc ) {
sc->AppendEntity( stepc );
} else {
sc = stepc;
}
}
// READ
Severity STEPcomplex::STEPread( int id, int addFileId, class InstMgr * instance_set,
istream & in, const char * currSch, bool /*useTechCor*/, bool /*strict*/ ) {
char c;
std::string typeNm;
STEPcomplex * stepc = 0;
ClearError( 1 );
STEPfile_id = id;
stepc = head;
while( stepc ) {
stepc->visited = 0;
stepc = stepc->sc;
}
in >> ws;
in.get( c );
if( c == '(' ) { // opening paren for subsuperRecord
in >> ws;
c = in.peek();
while( c != ')' ) {
typeNm.clear();
in >> ws;
ReadStdKeyword( in, typeNm, 1 ); // read the type name
in >> ws;
c = in.peek();
if( c != '(' ) {
_error.AppendToDetailMsg(
"Missing open paren before entity attr values.\n" );
cout << "ERROR: missing open paren\n";
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
STEPread_error( c, 0, in, currSch );
return _error.severity();
}
stepc = EntityPart( typeNm.c_str(), currSch );
if( stepc )
stepc->SDAI_Application_instance::STEPread( id, addFileId,
instance_set, in,
currSch );
else {
cout << "ERROR: complex entity part \"" << typeNm
<< "\" does not exist.\n";
_error.AppendToDetailMsg(
"Complex entity part of instance does not exist.\n" );
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
STEPread_error( c, 0, in, currSch );
return _error.severity();
}
in >> ws;
c = in.peek();
}
if( c != ')' )
cout <<
"ERROR: missing ending paren for complex entity instance.\n";
else {
in.get( c ); // read the closing paren
}
}
return _error.severity();
}
//FIXME delete this?
#ifdef buildwhileread
// READ
Severity STEPcomplex::STEPread( int id, int addFileId, class InstMgr * instance_set,
istream & in, const char * currSch ) {
ClearError( 1 );
STEPfile_id = id;
STEPcomplex stepc = head;
while( stepc ) {
stepc->visited = 0;
stepc = stepc->sc;
}
char c;
in >> ws;
in.get( c );
if( c == '(' ) {
std::string s;
in >> ws;
in.get( c );
while( in && ( c != '(' ) && !isspace( c ) ) { // get the entity name
s.Append( c );
in.get( c );
}
if( isspace( c ) ) {
in >> ws;
in.get( c );
}
if( c != '(' ) {
_error.AppendToDetailMsg(
"Missing open paren before entity attr values.\n" );
cout << "ERROR: missing open paren\n";
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
STEPread_error( c, 0, in, currSch );
return _error.severity();
} else { // c == '('
in.putback( c );
}
cout << s << endl;
BuildAttrs( s.c_str() );
SDAI_Application_instance::STEPread( id, addFileId, instance_set,
in, currSch );
in >> ws;
in.get( c );
while( c != ')' ) {
s.set_null();
while( in && ( c != '(' ) && !isspace( c ) ) { // get the entity name
s.Append( c );
in.get( c );
}
if( isspace( c ) ) {
in >> ws;
in.get( c );
}
if( c != '(' ) {
_error.AppendToDetailMsg(
"Missing open paren before entity attr values.\n" );
cout << "ERROR: missing open paren\n";
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
STEPread_error( c, 0, in, currSch );
return _error.severity();
} else { // c == '('
in.putback( c );
}
cout << s << endl; // diagnostics DAS
STEPcomplex * stepc = new STEPcomplex( _registry );
AppendEntity( stepc );
stepc->BuildAttrs( s.c_str() );
stepc->SDAI_Application_instance::STEPread( id, addFileId,
instance_set, in,
currSch );
in >> ws;
in.get( c );
}
}
return _error.severity();
}
#endif
void STEPcomplex::BuildAttrs( const char * s ) {
// assign inherited member variable
eDesc = ( class EntityDescriptor * )_registry->FindEntity( s );
if( eDesc ) {
const AttrDescriptorList * attrList;
SDAI_Integer * integer_data;
SDAI_String * string_data;
SDAI_Binary * binary_data;
SDAI_Real * real_data;
SDAI_BOOLEAN * boolean_data;
SDAI_LOGICAL * logical_data;
SDAI_Application_instance ** entity_data;
SDAI_Enum * enum_data;
SDAI_Select * select_data;
STEPaggregate * aggr_data;
attrList = &( eDesc->ExplicitAttr() );
//////////////////////////////////////////////
// find out how many attrs there are
//////////////////////////////////////////////
STEPattribute * a = 0;
AttrDescLinkNode * attrPtr = ( AttrDescLinkNode * )attrList->GetHead();
while( attrPtr != 0 ) {
const AttrDescriptor * ad = attrPtr->AttrDesc();
if( ( ad->Derived() ) != LTrue ) {
switch( ad->NonRefType() ) {
case INTEGER_TYPE:
integer_data = new SDAI_Integer;
_attr_data_list.push_back( ( void * ) integer_data );
a = new STEPattribute( *ad, integer_data );
break;
case STRING_TYPE:
string_data = new SDAI_String;
_attr_data_list.push_back( ( void * ) string_data );
a = new STEPattribute( *ad, string_data );
break;
case BINARY_TYPE:
binary_data = new SDAI_Binary;
_attr_data_list.push_back( ( void * ) binary_data );
a = new STEPattribute( *ad, binary_data );
break;
case REAL_TYPE:
case NUMBER_TYPE:
real_data = new SDAI_Real;
_attr_data_list.push_back( ( void * ) real_data );
a = new STEPattribute( *ad, real_data );
break;
case BOOLEAN_TYPE:
boolean_data = new SDAI_BOOLEAN;
_attr_data_list.push_back( ( void * ) boolean_data );
a = new STEPattribute( *ad, boolean_data );
break;
case LOGICAL_TYPE:
logical_data = new SDAI_LOGICAL;
_attr_data_list.push_back( ( void * ) logical_data );
a = new STEPattribute( *ad, logical_data );
break;
case ENTITY_TYPE:
entity_data = new( SDAI_Application_instance * );
_attr_data_list.push_back( ( void * ) entity_data );
a = new STEPattribute( *ad, entity_data );
break;
case ENUM_TYPE: {
EnumTypeDescriptor * enumD =
( EnumTypeDescriptor * )ad->ReferentType();
enum_data = enumD->CreateEnum();
_attr_data_list.push_back( ( void * ) enum_data );
a = new STEPattribute( *ad, enum_data );
break;
}
case SELECT_TYPE: {
SelectTypeDescriptor * selectD =
( SelectTypeDescriptor * )ad->ReferentType();
select_data = selectD->CreateSelect();
_attr_data_list.push_back( ( void * ) select_data );
a = new STEPattribute( *ad, select_data );
break;
}
case AGGREGATE_TYPE:
case ARRAY_TYPE: // DAS
case BAG_TYPE: // DAS
case SET_TYPE: // DAS
case LIST_TYPE: { // DAS
AggrTypeDescriptor * aggrD =
( AggrTypeDescriptor * )ad->ReferentType();
aggr_data = aggrD->CreateAggregate();
//_attr_data_list.push_back( ( void * ) aggr_data );
_attr_data_list.push_back( aggr_data );
a = new STEPattribute( *ad, aggr_data );
break;
}
default:
_error.AppendToDetailMsg( "STEPcomplex::BuildAttrs: Found attribute of unknown type. Creating default attribute.\n" );
_error.GreaterSeverity( SEVERITY_WARNING );
a = new STEPattribute();
}
a -> set_null();
attributes.push( a );
}
attrPtr = ( AttrDescLinkNode * )attrPtr->NextNode();
}
} else {
_error.AppendToDetailMsg( "Entity does not exist.\n" );
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
}
}
void STEPcomplex::STEPread_error( char c, int index, istream & in, const char * schnm ) {
(void) schnm; //unused
cout << "STEPcomplex::STEPread_error(), index=" << index << ", entity #" << STEPfile_id << "." << endl;
streampos p = in.tellg();
std::string q, r;
getline( in, q );
getline( in, r );
cout << "Remainder of this line:" << endl << c << q << endl << "Next line:" << endl << r << endl;
in.seekg( p );
}
/**
** These functions take into account the current schema, so that if an entity
** or attribute type is renamed in this schema (using the USE/REF clause) the
** new name is written out. They do not, however, comply with the requirement
** in the standard that the entity parts of a complex entity must be printed in
** alphabetical order. The nodes are sorted alphabetically according to their
** original names but not according to their renamed names (DAR 6/5/97).
*/
void STEPcomplex::STEPwrite( ostream & out, const char * currSch, int writeComment ) {
if( writeComment && !p21Comment.empty() ) {
out << p21Comment;
}
out << "#" << STEPfile_id << "=(\n";
WriteExtMapEntities( out, currSch );
out << ");\n";
}
const char * STEPcomplex::STEPwrite( std::string & buf, const char * currSch ) {
buf.clear();
stringstream ss;
ss << "#" << STEPfile_id << "=(";
WriteExtMapEntities( ss, currSch );
ss << ");";
ss << ends;
buf.append( ss.str() );
return const_cast<char *>( buf.c_str() );
}
/** \copydoc STEPcomplex::STEPwrite */
void STEPcomplex::WriteExtMapEntities( ostream & out, const char * currSch ) {
std::string tmp;
out << StrToUpper( EntityName( currSch ), tmp );
out << "(";
int n = attributes.list_length();
for( int i = 0 ; i < n; i++ ) {
( attributes[i] ).STEPwrite( out, currSch );
if( i < n - 1 ) {
out << ",";
}
}
out << ")\n";
if( sc ) {
sc->WriteExtMapEntities( out, currSch );
}
}
/** \copydoc STEPcomplex::STEPwrite */
const char * STEPcomplex::WriteExtMapEntities( std::string & buf, const char * currSch ) {
std::string tmp;
StrToUpper( EntityName( currSch ), tmp );
buf.append( tmp );
buf.append( "i" );
int n = attributes.list_length();
for( int i = 0 ; i < n; i++ ) {
buf.append( attributes[i].asStr( currSch ) );
if( i < n - 1 ) {
buf.append( "," );
}
}
buf.append( ")\n" );
if( sc ) {
sc->WriteExtMapEntities( buf, currSch );
}
return buf.c_str();
}
void STEPcomplex::CopyAs( SDAI_Application_instance * se ) {
if( !se->IsComplex() ) {
char errStr[BUFSIZ];
cerr << "STEPcomplex::CopyAs() called with non-complex entity: "
<< __FILE__ << __LINE__ << "\n" << _POC_ "\n";
sprintf( errStr,
"STEPcomplex::CopyAs(): %s - entity #%d.\n",
"Programming ERROR - called with non-complex entity",
STEPfile_id );
_error.AppendToDetailMsg( errStr );
_error.AppendToUserMsg( errStr );
_error.GreaterSeverity( SEVERITY_BUG );
return;
} else {
STEPcomplex * scpartCpyTo = head;
STEPcomplex * scpartCpyFrom = ( ( STEPcomplex * )se )->head;
while( scpartCpyTo && scpartCpyFrom ) {
scpartCpyTo->SDAI_Application_instance::CopyAs( scpartCpyFrom );
scpartCpyTo = scpartCpyTo->sc;
scpartCpyFrom = scpartCpyFrom->sc;
}
}
}
SDAI_Application_instance * STEPcomplex::Replicate() {
if( !IsComplex() ) {
return SDAI_Application_instance::Replicate();
} else if( !_registry ) {
return S_ENTITY_NULL;
} else {
int nameCount = 64;
std::string ** nameList = new std::string *[nameCount];
STEPcomplex * scomp = this->head;
int i = 0;
while( scomp && ( i < 63 ) ) {
nameList[i] = new std::string( "" );
nameList[i]->append( scomp->eDesc->Name() );
i++;
scomp = scomp->sc;
}
nameList[i] = ( std::string * )0;
if( i == 63 ) {
char errStr[BUFSIZ];
cerr << "STEPcomplex::Replicate() name buffer too small: "
<< __FILE__ << __LINE__ << "\n" << _POC_ "\n";
sprintf( errStr,
"STEPcomplex::Replicate(): %s - entity #%d.\n",
"Programming ERROR - name buffer too small",
STEPfile_id );
_error.AppendToDetailMsg( errStr );
_error.AppendToUserMsg( errStr );
_error.GreaterSeverity( SEVERITY_BUG );
}
STEPcomplex * seNew = new STEPcomplex( _registry,
( const std::string ** )nameList,
1111 );
seNew -> CopyAs( this );
return seNew;
// TODO need to:
// get registry
// create list of entity names
// send them to constructor for STEPcomplex
// implement STEPcomplex::CopyAs()
// call seNew->CopyAs()
}
}