#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')
>>> #3=IfcPerson('Foobar',$,$,$,$,$,$,$)
"""
eid = -1
try:
eid = kwargs.pop("_id", -1)
except: pass
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()
attrs = list(enumerate(args)) + [(e.wrapped_data.get_argument_index(name), arg) for name, arg in kwargs.items()]
for idx, arg in attrs:
@@ -120,12 +124,12 @@ class file(object):
"""
return self[guid]
def add(self, inst):
def add(self, inst, _id=None):
"""Adds an entity including any dependent entities to an IFC file.
If the entity already exists, it is not re-added."""
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):
"""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();
IfcUtil::IfcBaseClass* addEntity(IfcUtil::IfcBaseClass* entity);
IfcUtil::IfcBaseClass* addEntity(IfcUtil::IfcBaseClass* entity, int id=-1);
void addEntities(IfcEntityList::ptr es);
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()) {
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.
we->file = this;
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
@@ -1720,62 +1731,68 @@ IfcUtil::IfcBaseClass* IfcFile::addEntity(IfcUtil::IfcBaseClass* entity) {
}
}
// The mapping by entity type is updated.
const IfcParse::declaration* ty = &new_entity->declaration();
// The mapping by entity type is updated.
const IfcParse::declaration* ty = &new_entity->declaration();
if (ty->as_entity()) {
IfcEntityList::ptr insts = instances_by_type_excl_subtypes(ty);
if (!insts) {
insts = IfcEntityList::ptr(new IfcEntityList());
bytype_excl[ty] = insts;
}
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 (ty->as_entity()) {
IfcEntityList::ptr insts = instances_by_type_excl_subtypes(ty);
if (!insts) {
insts = IfcEntityList::ptr(new IfcEntityList());
bytype_excl[ty] = insts;
}
insts->push(new_entity);
}
if (byid.find(new_id) != byid.end()) {
// This should not happen
std::stringstream ss;
ss << "Overwriting entity with id " << new_id;
Logger::Message(Logger::LOG_WARNING, ss.str());
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;
}
}
// The mapping by entity instance name is updated.
byid[new_id] = new_entity;
}
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;
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()) {
build_inverses_(new_entity);
}
if (byid.find(new_id) != byid.end()) {
// 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) {