Files
IfcOpenShell/export.py
T
2019-09-01 17:23:48 +10:00

298 lines
13 KiB
Python

import ifcopenshell
import bpy
class IfcParser():
def __init__(self):
self.selected_products = []
self.selected_types = []
self.spatial_structure_elements = []
self.spatial_structure_elements_tree = []
self.rel_contained_in_spatial_structure = {}
self.representations = []
self.type_products = []
self.context = {}
self.products = []
def parse(self):
self.sort_selected_into_products_and_types()
self.spatial_structure_elements = self.get_spatial_structure_elements()
self.representations = self.get_representations()
self.type_products = self.get_type_products()
collection_name_filter = []
product_index = 0
for object in self.selected_products:
product_data = {
'ifc': None,
'raw': object,
'class': self.get_ifc_class(object.name),
'relating_structure': None,
'representation': self.get_representation_reference(object.data.name),
'attributes': self.get_object_attributes(object)
}
for collection in object.users_collection:
if self.is_a_spatial_structure_element(self.get_ifc_class(collection.name)):
reference = self.get_spatial_structure_element_reference(collection.name)
self.rel_contained_in_spatial_structure.setdefault(reference, []).append(product_index)
product_data['relating_structure'] = reference
collection_name_filter.append(collection.name)
self.products.append(product_data)
product_index += 1
self.context = self.get_context()
self.spatial_structure_elements_tree = self.get_spatial_structure_elements_tree(
self.context['raw'].children, collection_name_filter)
def get_object_attributes(self, object):
attributes = { 'Name': self.get_ifc_name(object.name) }
attributes.update({ key[3:]: object[key] for key in object.keys() if key[0:3] == 'Ifc'})
return attributes
def sort_selected_into_products_and_types(self):
for object in bpy.context.selected_objects:
if self.is_object_in_types_collection(object):
self.selected_types.append(object)
else:
self.selected_products.append(object)
def is_object_in_types_collection(self, object):
for collection in object.users_collection:
if self.is_a_types_collection(self.get_ifc_class(collection.name)):
return True
return False
def get_context(self):
for collection in bpy.data.collections:
if self.is_a_context(self.get_ifc_class(collection.name)):
return {
'ifc': None,
'raw': collection,
'class': self.get_ifc_class(collection.name),
'attributes': { 'Name': self.get_ifc_name(collection.name) }
}
def get_spatial_structure_elements(self):
elements = []
for collection in bpy.data.collections:
if self.is_a_spatial_structure_element(self.get_ifc_class(collection.name)):
elements.append({
'ifc': None,
'raw': collection,
'class': self.get_ifc_class(collection.name),
'attributes': { 'Name': self.get_ifc_name(collection.name)}
})
return elements
def get_representations(self):
representations = {}
for object in self.selected_products + self.selected_types:
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_type_products(self):
types_collection = self.get_types_collection()
if not types_collection:
return []
return [{
'ifc': None,
'raw': object,
'class': self.get_ifc_class(object.name),
'representation': self.get_representation_reference(object.data.name),
'attributes': self.get_object_attributes(object)
} for object in types_collection.objects ]
def get_types_collection(self):
for collection in bpy.data.collections:
if self.is_a_types_collection(self.get_ifc_class(collection.name)):
return collection
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):
collection_tree = []
for collection in collections:
if not self.is_a_spatial_structure_element(self.get_ifc_class(collection.name)):
continue
children = self.get_spatial_structure_elements_tree(
collection.children, name_filter)
if collection.name in name_filter \
or children:
collection_tree.append({
'reference': self.get_spatial_structure_element_reference(collection.name),
'children': children
})
return collection_tree
def get_spatial_structure_element_reference(self, name):
return [ e['attributes']['Name'] for e in self.spatial_structure_elements ].index(self.get_ifc_name(name))
def get_ifc_class(self, name):
return name.split('/')[0]
def get_ifc_name(self, name):
try:
return name.split('/')[1]
except IndexError:
print('ERROR: Name "{}" does not follow the format of "IfcClass/Name"'.format(name))
def is_a_spatial_structure_element(self, class_name):
# We assume that any collection we can't identify is a spatial structure
return class_name[0:3] == 'Ifc' \
and not self.is_a_context(class_name) \
and not self.is_a_types_collection(class_name)
def is_a_context(self, class_name):
return class_name in ['IfcProject', 'IfcProjectLibrary']
def is_a_types_collection(self, class_name):
return class_name == 'IfcTypeProduct'
class IfcExporter():
def __init__(self, ifc_parser):
self.template_file = '/home/dion/Projects/blender-bim-ifc/template.ifc'
self.output_file = '/home/dion/Projects/blender-bim-ifc/output.ifc'
self.data_dir = '/home/dion/Projects/blender-bim-ifc/data/'
self.ifc_parser = ifc_parser
def export(self):
self.file = ifcopenshell.open(self.template_file)
self.set_common_definitions()
self.ifc_parser.parse()
self.create_rep_context()
self.create_context()
self.create_representations()
self.create_type_products()
self.create_spatial_structure_elements(self.ifc_parser.spatial_structure_elements_tree)
self.create_products()
self.relate_elements_to_spatial_structures()
self.file.write(self.output_file)
def set_common_definitions(self):
self.origin = self.file.by_type("IfcAxis2Placement3D")[0]
# Owner history doesn't actually work like this, but for now, it does :)
self.owner_history = self.file.by_type("ifcownerhistory")[0]
def create_rep_context(self):
self.ifc_rep_context = self.file.createIfcGeometricRepresentationContext(
None, "Model",
3, 1.0E-05,
self.origin,
self.file.createIfcDirection((0., 1., 0.)))
self.ifc_rep_subcontext = self.file.createIfcGeometricRepresentationSubContext(
"Body", "Model",
None, None, None, None,
self.ifc_rep_context, None, "MODEL_VIEW", None)
def create_context(self):
context = self.ifc_parser.context
attributes = context['attributes']
attributes.update({
'GlobalId': ifcopenshell.guid.new(),
'RepresentationContexts': [self.ifc_rep_context],
'UnitsInContext': self.file.by_type("IfcUnitAssignment")[0]
})
self.ifc_parser.context['ifc'] = self.file.create_entity(self.ifc_parser.context['class'], **attributes)
def create_type_products(self):
for product in self.ifc_parser.type_products:
representation = self.ifc_parser.representations[product['representation']]['ifc']
placement = self.create_ifc_axis_2_placement_3d(product['raw'].location)
representation_map = self.file.createIfcRepresentationMap(placement, representation)
product['attributes'].update({
'GlobalId': ifcopenshell.guid.new(),
'RepresentationMaps': [representation_map]
})
try:
product['ifc'] = self.file.create_entity(product['class'], **product['attributes'])
except RuntimeError as e:
print('The type product "{}/{}" could not be created: {}'.format(product['class'], product['attributes']['Name'], e.args))
def create_spatial_structure_elements(self, element_tree, relating_object=None):
if relating_object == None:
relating_object = self.ifc_parser.context['ifc']
placement_rel_to = None
else:
placement_rel_to = relating_object.ObjectPlacement
related_objects = []
for node in element_tree:
element = self.ifc_parser.spatial_structure_elements[node['reference']]
element['attributes'].update({
'GlobalId': ifcopenshell.guid.new(), # TODO: unhardcode
'OwnerHistory': self.owner_history, # TODO: unhardcode
'ObjectPlacement': self.file.createIfcLocalPlacement(placement_rel_to, self.origin)
})
element['ifc'] = self.file.create_entity(element['class'], **element['attributes'])
related_objects.append(element['ifc'])
self.create_spatial_structure_elements(node['children'], element['ifc'])
if related_objects:
self.file.createIfcRelAggregates(
ifcopenshell.guid.new(),
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):
for product in self.ifc_parser.products:
placement_rel_to = self.ifc_parser.spatial_structure_elements[product['relating_structure']]['ifc'].ObjectPlacement
placement = self.file.createIfcLocalPlacement(placement_rel_to,
self.create_ifc_axis_2_placement_3d(product['raw'].location))
representation = self.file.createIfcProductDefinitionShape(
None, None,
[self.ifc_parser.representations[product['representation']]['ifc']])
product['attributes'].update({
'GlobalId': ifcopenshell.guid.new(), # TODO: unhardcode
'OwnerHistory': self.owner_history, # TODO: unhardcode
'ObjectPlacement': placement,
'Representation': representation
})
try:
product['ifc'] = self.file.create_entity(product['class'], **product['attributes'])
except RuntimeError as e:
print('The product "{}/{}" could not be created: {}'.format(product['class'], product['attributes']['Name'], e.args))
def create_ifc_axis_2_placement_3d(self, point):
return self.file.createIfcAxis2Placement3D(
self.file.createIfcCartesianPoint((point.x, point.y, point.z)))
def create_representation(self, mesh):
ifc_vertices = []
ifc_faces = []
for vertice in mesh.vertices:
ifc_vertices.append(
self.file.createIfcCartesianPoint((vertice.co.x, vertice.co.y, vertice.co.z)))
for polygon in mesh.polygons:
ifc_faces.append(self.file.createIfcFace([
self.file.createIfcFaceOuterBound(
self.file.createIfcPolyLoop([ifc_vertices[vertice] for vertice in polygon.vertices]),
True)]))
return self.file.createIfcShapeRepresentation(
self.ifc_rep_subcontext, 'Body', 'Brep',
[self.file.createIfcFacetedBrep(self.file.createIfcClosedShell(ifc_faces))])
def relate_elements_to_spatial_structures(self):
for relating_structure, related_elements in self.ifc_parser.rel_contained_in_spatial_structure.items():
self.file.createIfcRelContainedInSpatialStructure(
ifcopenshell.guid.new(), self.owner_history, None, None,
[ self.ifc_parser.products[e]['ifc'] for e in related_elements],
self.ifc_parser.spatial_structure_elements[relating_structure]['ifc'])
ifc_parser = IfcParser()
ifc_exporter = IfcExporter(ifc_parser)
ifc_exporter.export()