@@ -975,10 +975,13 @@ void IfcEntityInstanceData::load() const {
975975}
976976
977977IfcEntityInstanceData::IfcEntityInstanceData (const IfcEntityInstanceData& e) {
978- file = e. file ;
978+ file = 0 ;
979979 type_ = e.type_ ;
980980 id_ = 0 ;
981981
982+ // In order not to have the instance read from file
983+ initialized_ = true ;
984+
982985 const unsigned int count = e.getArgumentCount ();
983986 for (unsigned int i = 0 ; i < count; ++i) {
984987 this ->setArgument (i, e.getArgument (i));
@@ -1185,6 +1188,7 @@ void IfcEntityInstanceData::setArgument(unsigned int i, Argument* a, IfcUtil::Ar
11851188 case IfcUtil::Argument_AGGREGATE_OF_EMPTY_AGGREGATE: {
11861189 IfcUtil::ArgumentType t2 = IfcSchema::Type::GetAttributeType (type (), (unsigned char )i);
11871190 delete copy;
1191+ copy = 0 ;
11881192 setArgument (i, a, t2);
11891193 break ; }
11901194 default :
@@ -1193,6 +1197,10 @@ void IfcEntityInstanceData::setArgument(unsigned int i, Argument* a, IfcUtil::Ar
11931197 break ;
11941198 }
11951199
1200+ if (!copy) {
1201+ return ;
1202+ }
1203+
11961204 if (i < attributes_.size ()) {
11971205 Argument* current_attribute = attributes_[i];
11981206 if (this ->file ) {
@@ -1422,13 +1430,18 @@ IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) {
14221430 return mit->second ;
14231431 }
14241432
1433+ IfcUtil::IfcBaseClass* new_entity = entity;
1434+
14251435 // Obtain all forward references by a depth-first
14261436 // traversal and add them to the file.
14271437 try {
14281438 IfcEntityList::ptr entity_attributes = traverse (entity, 1 );
14291439 for (IfcEntityList::it it = entity_attributes->begin (); it != entity_attributes->end (); ++it) {
14301440 if (*it != entity) {
1431- entity_file_map.insert (entity_entity_map_t::value_type (*it, addEntity (*it)));
1441+ entity_entity_map_t ::iterator mit2 = entity_file_map.find (*it);
1442+ if (mit2 == entity_file_map.end ()) {
1443+ entity_file_map.insert (entity_entity_map_t::value_type (*it, addEntity (*it)));
1444+ }
14321445 }
14331446 }
14341447 } catch (...) {
@@ -1448,7 +1461,7 @@ IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) {
14481461 // need to be updated to point to instances in this file.
14491462 IfcFile* other_file = entity->entity ->file ;
14501463 IfcEntityInstanceData* we = new IfcEntityInstanceData (*entity->entity );
1451- entity = IfcSchema::SchemaEntity (we);
1464+ new_entity = IfcSchema::SchemaEntity (we);
14521465
14531466 // In case an entity is added that contains geometry, the unit
14541467 // information needs to be accounted for for IfcLengthMeasures.
@@ -1523,12 +1536,16 @@ IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) {
15231536 // A new entity instance name is generated and
15241537 // the instance is pointed to this file.
15251538 we->file = this ;
1526- we->set_id (FreshId ());
1539+ if (!IfcSchema::Type::IsSimple (we->type ())) {
1540+ we->set_id (FreshId ());
1541+ }
1542+
1543+ entity_file_map.insert (entity_entity_map_t::value_type (entity, new_entity));
15271544 }
15281545
15291546 // For subtypes of IfcRoot, the GUID mapping needs to be updated.
1530- if (entity ->is (IfcSchema::Type::IfcRoot)) {
1531- IfcSchema::IfcRoot* ifc_root = (IfcSchema::IfcRoot*) entity ;
1547+ if (new_entity ->is (IfcSchema::Type::IfcRoot)) {
1548+ IfcSchema::IfcRoot* ifc_root = (IfcSchema::IfcRoot*) new_entity ;
15321549 try {
15331550 const std::string guid = ifc_root->GlobalId ();
15341551 if ( byguid.find (guid) != byguid.end () ) {
@@ -1543,14 +1560,14 @@ IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) {
15431560 }
15441561
15451562 // The mapping by entity type is updated.
1546- IfcSchema::Type::Enum ty = entity ->type ();
1563+ IfcSchema::Type::Enum ty = new_entity ->type ();
15471564 for (;;) {
15481565 IfcEntityList::ptr instances_by_type = entitiesByType (ty);
15491566 if (!instances_by_type) {
15501567 instances_by_type = IfcEntityList::ptr (new IfcEntityList ());
15511568 bytype[ty] = instances_by_type;
15521569 }
1553- instances_by_type->push (entity );
1570+ instances_by_type->push (new_entity );
15541571 boost::optional<IfcSchema::Type::Enum> pt = IfcSchema::Type::Parent (ty);
15551572 if (pt) {
15561573 ty = *pt;
@@ -1561,12 +1578,12 @@ IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) {
15611578 }
15621579
15631580 int new_id = -1 ;
1564- if (!entity ->entity ->file ) {
1581+ if (!new_entity ->entity ->file ) {
15651582 // For newly created entities ensure a valid ENTITY_INSTANCE_NAME is set
1566- entity ->entity ->file = this ;
1567- new_id = entity ->entity ->set_id ();
1583+ new_entity ->entity ->file = this ;
1584+ new_id = new_entity ->entity ->set_id ();
15681585 } else {
1569- new_id = entity ->entity ->id ();
1586+ new_id = new_entity ->entity ->id ();
15701587 }
15711588
15721589 if (byid.find (new_id) != byid.end ()) {
@@ -1577,30 +1594,30 @@ IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) {
15771594 }
15781595
15791596 // The mapping by entity instance name is updated.
1580- byid[new_id] = entity ;
1597+ byid[new_id] = new_entity ;
15811598
15821599 // The mapping by reference is updated.
15831600 IfcEntityList::ptr entity_attributes (new IfcEntityList);
15841601 try {
1585- entity_attributes = traverse (entity , 1 );
1602+ entity_attributes = traverse (new_entity , 1 );
15861603 } catch (const std::exception& e) {
15871604 Logger::Error (e);
15881605 }
15891606
15901607 for (IfcEntityList::it it = entity_attributes->begin (); it != entity_attributes->end (); ++it) {
15911608 IfcUtil::IfcBaseClass* entity_attribute = *it;
1592- if (*it == entity ) continue ;
1609+ if (*it == new_entity ) continue ;
15931610 try {
15941611 if (!IfcSchema::Type::IsSimple (entity_attribute->type ())) {
15951612 unsigned entity_attribute_id = entity_attribute->entity ->id ();
1596- byref[entity_attribute_id].push_back (entity ->entity ->id ());
1613+ byref[entity_attribute_id].push_back (new_entity ->entity ->id ());
15971614 }
15981615 } catch (const std::exception& e) {
15991616 Logger::Error (e);
16001617 }
16011618 }
16021619
1603- return entity ;
1620+ return new_entity ;
16041621}
16051622
16061623void IfcFile::removeEntity (IfcUtil::IfcBaseClass* entity) {
0 commit comments