From bd35a9924e8fc354eccf8749b228565a7af0926c Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sat, 31 Aug 2019 17:37:53 +1000 Subject: [PATCH] Refactor into a class. It's gonna get big! --- export.py | 155 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 85 insertions(+), 70 deletions(-) diff --git a/export.py b/export.py index 2acd24068c..35788ce7fc 100644 --- a/export.py +++ b/export.py @@ -1,83 +1,98 @@ import ifcopenshell import bpy -template_file = '/home/dion/Projects/blender-bim-ifc/template.ifc' -output_file = '/home/dion/Projects/blender-bim-ifc/output.ifc' -f = ifcopenshell.open(template_file) +class IfcExporter(): + def __init__(self): + self.template_file = '/home/dion/Projects/blender-bim-ifc/template.ifc' + self.output_file = '/home/dion/Projects/blender-bim-ifc/output.ifc' -origin = f.by_type("IfcAxis2Placement3D")[0] -owner_history = f.by_type("ifcownerhistory")[0] + def export(self): + self.file = ifcopenshell.open(self.template_file) + self.set_common_definitions() + self.create_context() + self.create_project() + self.create_building() + self.create_building_storey() + self.create_products() + self.file.write(self.output_file) -context = f.createIfcGeometricRepresentationContext( - None, "Model", - 3, 1.0E-05, - origin, - f.createIfcDirection((0., 1., 0.))) + 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] -subcontext = f.createIfcGeometricRepresentationSubContext( - "Body", "Model", - None, None, None, None, - context, None, "MODEL_VIEW", None) + def create_context(self): + self.ifc_context = self.file.createIfcGeometricRepresentationContext( + None, "Model", + 3, 1.0E-05, + self.origin, + self.file.createIfcDirection((0., 1., 0.))) -project = f.createIfcProject( - ifcopenshell.guid.new(), - owner_history, - 'Name', None, - None, None, None, [context], - f.by_type("IfcUnitAssignment")[0]) + self.ifc_subcontext = self.file.createIfcGeometricRepresentationSubContext( + "Body", "Model", + None, None, None, None, + self.ifc_context, None, "MODEL_VIEW", None) -building_placement = f.createIfcLocalPlacement(None, origin) + def create_project(self): + self.project = self.file.createIfcProject( + ifcopenshell.guid.new(), + self.owner_history, + 'Name', None, + None, None, None, [self.ifc_context], + self.file.by_type("IfcUnitAssignment")[0]) -building = f.createIfcBuilding( - ifcopenshell.guid.new(), - owner_history, 'My building', None, None, - building_placement, - None, None, 'ELEMENT', None, None, None) - -building_storey_placement = f.createIfcLocalPlacement(building_placement, origin) + def create_building(self): + self.ifc_building_placement = self.file.createIfcLocalPlacement(None, self.origin) + self.ifc_building = self.file.createIfcBuilding( + ifcopenshell.guid.new(), + self.owner_history, 'My building', None, None, + self.ifc_building_placement, + None, None, 'ELEMENT', None, None, None) + self.file.createIfcRelAggregates( + ifcopenshell.guid.new(), + self.owner_history, None, None, self.project, [self.ifc_building]) -building_storey = f.createIfcBuildingStorey( - ifcopenshell.guid.new(), - owner_history, 'Ground floor', None, None, - building_storey_placement, None, None, 'ELEMENT', None) - -f.createIfcRelAggregates( - ifcopenshell.guid.new(), - owner_history, None, None, building, [building_storey]) - -f.createIfcRelAggregates( - ifcopenshell.guid.new(), - owner_history, None, None, project, [building]) + def create_building_storey(self): + self.ifc_building_storey_placement = self.file.createIfcLocalPlacement(self.ifc_building_placement, self.origin) + self.ifc_building_storey = self.file.createIfcBuildingStorey( + ifcopenshell.guid.new(), + self.owner_history, 'Ground floor', None, None, + self.ifc_building_storey_placement, None, None, 'ELEMENT', None) + self.file.createIfcRelAggregates( + ifcopenshell.guid.new(), + self.owner_history, None, None, self.ifc_building, [self.ifc_building_storey]) -ifc_products = [] + def create_products(self): + ifc_products = [] -for object in bpy.context.selected_objects: - ifc_vertices = [] - ifc_faces = [] - for vertice in object.data.vertices: - ifc_vertices.append( - f.createIfcCartesianPoint((vertice.co.x, vertice.co.y, vertice.co.z))) - for polygon in object.data.polygons: - ifc_faces.append(f.createIfcFace([ - f.createIfcFaceOuterBound( - f.createIfcPolyLoop([ifc_vertices[vertice] for vertice in polygon.vertices]), - True)])) - - representation = f.createIfcProductDefinitionShape(None, None, - [f.createIfcShapeRepresentation( - subcontext, 'Body', 'Brep', - [f.createIfcFacetedBrep(f.createIfcClosedShell(ifc_faces))])]) - - placement = f.createIfcLocalPlacement(building_storey_placement, - f.createIfcAxis2Placement3D( - f.createIfcCartesianPoint( - (object.location.x, object.location.y, object.location.z)))) - - ifc_products.append(f.createIfcBuildingElementProxy( - ifcopenshell.guid.new(), - owner_history, object.name, None, None, placement, representation, None, None)) - -f.createIfcRelContainedInSpatialStructure( - ifcopenshell.guid.new(), owner_history, None, None, ifc_products, building_storey) + for object in bpy.context.selected_objects: + ifc_vertices = [] + ifc_faces = [] + for vertice in object.data.vertices: + ifc_vertices.append( + self.file.createIfcCartesianPoint((vertice.co.x, vertice.co.y, vertice.co.z))) + for polygon in object.data.polygons: + ifc_faces.append(self.file.createIfcFace([ + self.file.createIfcFaceOuterBound( + self.file.createIfcPolyLoop([ifc_vertices[vertice] for vertice in polygon.vertices]), + True)])) + + representation = self.file.createIfcProductDefinitionShape(None, None, + [self.file.createIfcShapeRepresentation( + self.ifc_subcontext, 'Body', 'Brep', + [self.file.createIfcFacetedBrep(self.file.createIfcClosedShell(ifc_faces))])]) + + placement = self.file.createIfcLocalPlacement(self.ifc_building_storey_placement, + self.file.createIfcAxis2Placement3D( + self.file.createIfcCartesianPoint( + (object.location.x, object.location.y, object.location.z)))) + + ifc_products.append(self.file.createIfcBuildingElementProxy( + ifcopenshell.guid.new(), + self.owner_history, object.name, None, None, placement, representation, None, None)) -f.write(output_file) \ No newline at end of file + self.file.createIfcRelContainedInSpatialStructure( + ifcopenshell.guid.new(), self.owner_history, None, None, ifc_products, self.ifc_building_storey) + +ifc_exporter = IfcExporter() +ifc_exporter.export()