mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-09 13:52:23 +00:00
Support object instancing, so product representations are reused if the mesh data is the same
This commit is contained in:
@@ -6,11 +6,13 @@ class IfcParser():
|
|||||||
self.spatial_structure_elements = []
|
self.spatial_structure_elements = []
|
||||||
self.spatial_structure_elements_tree = []
|
self.spatial_structure_elements_tree = []
|
||||||
self.rel_contained_in_spatial_structure = {}
|
self.rel_contained_in_spatial_structure = {}
|
||||||
|
self.representations = []
|
||||||
self.context = {}
|
self.context = {}
|
||||||
self.products = []
|
self.products = []
|
||||||
|
|
||||||
def parse(self):
|
def parse(self):
|
||||||
self.spatial_structure_elements = self.get_spatial_structure_elements()
|
self.spatial_structure_elements = self.get_spatial_structure_elements()
|
||||||
|
self.representations = self.get_representations()
|
||||||
|
|
||||||
collection_name_filter = []
|
collection_name_filter = []
|
||||||
product_index = 0
|
product_index = 0
|
||||||
@@ -20,6 +22,7 @@ class IfcParser():
|
|||||||
'class': self.get_ifc_class(object.name),
|
'class': self.get_ifc_class(object.name),
|
||||||
'raw': object,
|
'raw': object,
|
||||||
'relating_structure': None,
|
'relating_structure': None,
|
||||||
|
'representation': self.get_representation_reference(object.data.name),
|
||||||
'attributes': { 'Name': self.get_ifc_name(object.name) }
|
'attributes': { 'Name': self.get_ifc_name(object.name) }
|
||||||
}
|
}
|
||||||
for collection in object.users_collection:
|
for collection in object.users_collection:
|
||||||
@@ -57,6 +60,22 @@ class IfcParser():
|
|||||||
})
|
})
|
||||||
return elements
|
return elements
|
||||||
|
|
||||||
|
def get_representations(self):
|
||||||
|
representations = {}
|
||||||
|
for object in bpy.context.selected_objects:
|
||||||
|
representations[object.data.name] = object.data
|
||||||
|
results = []
|
||||||
|
for name, value in representations.items():
|
||||||
|
results.append({
|
||||||
|
'ifc': None,
|
||||||
|
'raw': value,
|
||||||
|
'attributes': { 'Name': name }
|
||||||
|
})
|
||||||
|
return results
|
||||||
|
|
||||||
|
def get_representation_reference(self, name):
|
||||||
|
return [ r['attributes']['Name'] for r in self.representations ].index(name)
|
||||||
|
|
||||||
def get_spatial_structure_elements_tree(self, collections, name_filter):
|
def get_spatial_structure_elements_tree(self, collections, name_filter):
|
||||||
collection_tree = []
|
collection_tree = []
|
||||||
|
|
||||||
@@ -109,6 +128,7 @@ class IfcExporter():
|
|||||||
self.create_rep_context()
|
self.create_rep_context()
|
||||||
self.create_context()
|
self.create_context()
|
||||||
self.create_spatial_structure_elements(self.ifc_parser.spatial_structure_elements_tree)
|
self.create_spatial_structure_elements(self.ifc_parser.spatial_structure_elements_tree)
|
||||||
|
self.create_representations()
|
||||||
self.create_products()
|
self.create_products()
|
||||||
self.relate_elements_to_spatial_structures()
|
self.relate_elements_to_spatial_structures()
|
||||||
self.file.write(self.output_file)
|
self.file.write(self.output_file)
|
||||||
@@ -162,6 +182,10 @@ class IfcExporter():
|
|||||||
ifcopenshell.guid.new(),
|
ifcopenshell.guid.new(),
|
||||||
self.owner_history, None, None, relating_object, related_objects)
|
self.owner_history, None, None, relating_object, related_objects)
|
||||||
|
|
||||||
|
def create_representations(self):
|
||||||
|
for representation in self.ifc_parser.representations:
|
||||||
|
representation['ifc'] = self.create_representation(representation['raw'])
|
||||||
|
|
||||||
def create_products(self):
|
def create_products(self):
|
||||||
for product in self.ifc_parser.products:
|
for product in self.ifc_parser.products:
|
||||||
object = product['raw']
|
object = product['raw']
|
||||||
@@ -170,26 +194,25 @@ class IfcExporter():
|
|||||||
self.file.createIfcAxis2Placement3D(
|
self.file.createIfcAxis2Placement3D(
|
||||||
self.file.createIfcCartesianPoint(
|
self.file.createIfcCartesianPoint(
|
||||||
(object.location.x, object.location.y, object.location.z))))
|
(object.location.x, object.location.y, object.location.z))))
|
||||||
representation = self.create_representation(object)
|
|
||||||
product['attributes'].update({
|
product['attributes'].update({
|
||||||
'GlobalId': ifcopenshell.guid.new(), # TODO: unhardcode
|
'GlobalId': ifcopenshell.guid.new(), # TODO: unhardcode
|
||||||
'OwnerHistory': self.owner_history, # TODO: unhardcode
|
'OwnerHistory': self.owner_history, # TODO: unhardcode
|
||||||
'ObjectPlacement': placement,
|
'ObjectPlacement': placement,
|
||||||
'Representation': representation
|
'Representation': self.ifc_parser.representations[product['representation']]['ifc']
|
||||||
})
|
})
|
||||||
try:
|
try:
|
||||||
product['ifc'] = self.file.create_entity(product['class'], **product['attributes'])
|
product['ifc'] = self.file.create_entity(product['class'], **product['attributes'])
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
print('The product "{}/{}" could not be created: {}'.format(product['class'], product['attributes']['Name'], e.args))
|
print('The product "{}/{}" could not be created: {}'.format(product['class'], product['attributes']['Name'], e.args))
|
||||||
|
|
||||||
def create_representation(self, object):
|
def create_representation(self, mesh):
|
||||||
ifc_vertices = []
|
ifc_vertices = []
|
||||||
ifc_faces = []
|
ifc_faces = []
|
||||||
|
|
||||||
for vertice in object.data.vertices:
|
for vertice in mesh.vertices:
|
||||||
ifc_vertices.append(
|
ifc_vertices.append(
|
||||||
self.file.createIfcCartesianPoint((vertice.co.x, vertice.co.y, vertice.co.z)))
|
self.file.createIfcCartesianPoint((vertice.co.x, vertice.co.y, vertice.co.z)))
|
||||||
for polygon in object.data.polygons:
|
for polygon in mesh.polygons:
|
||||||
ifc_faces.append(self.file.createIfcFace([
|
ifc_faces.append(self.file.createIfcFace([
|
||||||
self.file.createIfcFaceOuterBound(
|
self.file.createIfcFaceOuterBound(
|
||||||
self.file.createIfcPolyLoop([ifc_vertices[vertice] for vertice in polygon.vertices]),
|
self.file.createIfcPolyLoop([ifc_vertices[vertice] for vertice in polygon.vertices]),
|
||||||
|
|||||||
Reference in New Issue
Block a user