mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-29 03:37:21 +00:00
#1539 Option to specify id/instance_name in file::add()
This commit is contained in:
@@ -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.
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
|||||||
@@ -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()) {
|
||||||
|
if (id == -1) {
|
||||||
we->set_id(FreshId());
|
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
|
||||||
@@ -1754,9 +1765,15 @@ if (ty->as_entity()) {
|
|||||||
if (!new_entity->data().file) {
|
if (!new_entity->data().file) {
|
||||||
// For newly created entities ensure a valid ENTITY_INSTANCE_NAME is set
|
// For newly created entities ensure a valid ENTITY_INSTANCE_NAME is set
|
||||||
new_entity->data().file = this;
|
new_entity->data().file = this;
|
||||||
new_id = new_entity->data().set_id();
|
boost::optional<unsigned> id_value;
|
||||||
|
if (id != -1) {
|
||||||
|
id_value = (unsigned)id;
|
||||||
|
if ((unsigned)id > MaxId) {
|
||||||
|
MaxId = (unsigned)id;
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
|
new_id = new_entity->data().set_id(id_value);
|
||||||
|
} else {
|
||||||
new_id = new_entity->data().id();
|
new_id = new_entity->data().id();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user