#1539 Option to specify id/instance_name in file::add()

This commit is contained in:
Thomas Krijnen
2021-06-27 11:14:26 +02:00
parent 37a61c549f
commit 2fd2b49709
3 changed files with 76 additions and 55 deletions
+7 -3
View File
@@ -80,8 +80,12 @@ class file(object):
f.create_entity('IfcPerson', Identification='Foobar') f.create_entity('IfcPerson', Identification='Foobar')
>>> #3=IfcPerson('Foobar',$,$,$,$,$,$,$) >>> #3=IfcPerson('Foobar',$,$,$,$,$,$,$)
""" """
eid = -1
try:
eid = kwargs.pop("_id", -1)
except: pass
e = entity_instance((self.schema, type)) e = entity_instance((self.schema, type))
self.wrapped_data.add(e.wrapped_data) self.wrapped_data.add(e.wrapped_data, eid)
e.wrapped_data.this.disown() e.wrapped_data.this.disown()
attrs = list(enumerate(args)) + [(e.wrapped_data.get_argument_index(name), arg) for name, arg in kwargs.items()] attrs = list(enumerate(args)) + [(e.wrapped_data.get_argument_index(name), arg) for name, arg in kwargs.items()]
for idx, arg in attrs: for idx, arg in attrs:
@@ -120,12 +124,12 @@ class file(object):
""" """
return self[guid] return self[guid]
def add(self, inst): def add(self, inst, _id=None):
"""Adds an entity including any dependent entities to an IFC file. """Adds an entity including any dependent entities to an IFC file.
If the entity already exists, it is not re-added.""" If the entity already exists, it is not re-added."""
inst.wrapped_data.this.disown() inst.wrapped_data.this.disown()
return entity_instance(self.wrapped_data.add(inst.wrapped_data)) return entity_instance(self.wrapped_data.add(inst.wrapped_data, -1 if _id is None else _id))
def by_type(self, type, include_subtypes=True): def by_type(self, type, include_subtypes=True):
"""Return IFC objects filtered by IFC Type and wrapped with the entity_instance class. """Return IFC objects filtered by IFC Type and wrapped with the entity_instance class.
+1 -1
View File
@@ -230,7 +230,7 @@ public:
void recalculate_id_counter(); void recalculate_id_counter();
IfcUtil::IfcBaseClass* addEntity(IfcUtil::IfcBaseClass* entity); IfcUtil::IfcBaseClass* addEntity(IfcUtil::IfcBaseClass* entity, int id=-1);
void addEntities(IfcEntityList::ptr es); void addEntities(IfcEntityList::ptr es);
void batch() { batch_mode_ = true; } void batch() { batch_mode_ = true; }
+68 -51
View File
@@ -1562,7 +1562,11 @@ void IfcFile::addEntities(IfcEntityList::ptr es) {
} }
} }
IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) { IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity, int id) {
if (id != -1 && byid.find((unsigned)id) != byid.end()) {
throw IfcParse::IfcException("An instance with id " + boost::lexical_cast<std::string>(id) + " is already part of this file");
}
if (entity->declaration().schema() != schema()) { if (entity->declaration().schema() != schema()) {
throw IfcParse::IfcException("Unabled to add instance from " + entity->declaration().schema()->name() + " schema to file with " + schema()->name() + " schema"); throw IfcParse::IfcException("Unabled to add instance from " + entity->declaration().schema()->name() + " schema to file with " + schema()->name() + " schema");
} }
@@ -1698,7 +1702,14 @@ IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) {
// the instance is pointed to this file. // the instance is pointed to this file.
we->file = this; we->file = this;
if (we->type()->as_entity()) { if (we->type()->as_entity()) {
we->set_id(FreshId()); if (id == -1) {
we->set_id(FreshId());
} else {
we->set_id((unsigned int)id);
if ((unsigned) id > MaxId) {
MaxId = (unsigned)id;
}
}
} }
// @todo entity_file_map: use weak_ptr // @todo entity_file_map: use weak_ptr
@@ -1720,62 +1731,68 @@ IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) {
} }
} }
// The mapping by entity type is updated. // The mapping by entity type is updated.
const IfcParse::declaration* ty = &new_entity->declaration(); const IfcParse::declaration* ty = &new_entity->declaration();
if (ty->as_entity()) { if (ty->as_entity()) {
IfcEntityList::ptr insts = instances_by_type_excl_subtypes(ty); IfcEntityList::ptr insts = instances_by_type_excl_subtypes(ty);
if (!insts) { if (!insts) {
insts = IfcEntityList::ptr(new IfcEntityList()); insts = IfcEntityList::ptr(new IfcEntityList());
bytype_excl[ty] = insts; bytype_excl[ty] = insts;
} }
insts->push(new_entity); insts->push(new_entity);
}
for (; ty->as_entity();) {
IfcEntityList::ptr insts = instances_by_type(ty);
if (!insts) {
insts = IfcEntityList::ptr(new IfcEntityList());
bytype[ty] = insts;
}
insts->push(new_entity);
const IfcParse::declaration* pt = ty->as_entity()->supertype();
if (pt) {
ty = pt;
}
else {
break;
}
}
if (ty->as_entity()) {
int new_id = -1;
if (!new_entity->data().file) {
// For newly created entities ensure a valid ENTITY_INSTANCE_NAME is set
new_entity->data().file = this;
new_id = new_entity->data().set_id();
}
else {
new_id = new_entity->data().id();
} }
if (byid.find(new_id) != byid.end()) { for (; ty->as_entity();) {
// This should not happen IfcEntityList::ptr insts = instances_by_type(ty);
std::stringstream ss; if (!insts) {
ss << "Overwriting entity with id " << new_id; insts = IfcEntityList::ptr(new IfcEntityList());
Logger::Message(Logger::LOG_WARNING, ss.str()); bytype[ty] = insts;
}
insts->push(new_entity);
const IfcParse::declaration* pt = ty->as_entity()->supertype();
if (pt) {
ty = pt;
}
else {
break;
}
} }
// The mapping by entity instance name is updated. if (ty->as_entity()) {
byid[new_id] = new_entity; int new_id = -1;
} if (!new_entity->data().file) {
// For newly created entities ensure a valid ENTITY_INSTANCE_NAME is set
new_entity->data().file = this;
boost::optional<unsigned> id_value;
if (id != -1) {
id_value = (unsigned)id;
if ((unsigned)id > MaxId) {
MaxId = (unsigned)id;
}
}
new_id = new_entity->data().set_id(id_value);
} else {
new_id = new_entity->data().id();
}
if (parsing_complete_ && ty->as_entity()) { if (byid.find(new_id) != byid.end()) {
build_inverses_(new_entity); // This should not happen
} std::stringstream ss;
ss << "Overwriting entity with id " << new_id;
Logger::Message(Logger::LOG_WARNING, ss.str());
}
return new_entity; // The mapping by entity instance name is updated.
byid[new_id] = new_entity;
}
if (parsing_complete_ && ty->as_entity()) {
build_inverses_(new_entity);
}
return new_entity;
} }
void IfcFile::removeEntity(IfcUtil::IfcBaseClass* entity) { void IfcFile::removeEntity(IfcUtil::IfcBaseClass* entity) {