Refactor into a class. It's gonna get big!

This commit is contained in:
Dion Moult
2019-08-31 17:37:53 +10:00
parent 2f4e82e0b9
commit bd35a9924e
+85 -70
View File
@@ -1,83 +1,98 @@
import ifcopenshell import ifcopenshell
import bpy import bpy
template_file = '/home/dion/Projects/blender-bim-ifc/template.ifc' class IfcExporter():
output_file = '/home/dion/Projects/blender-bim-ifc/output.ifc' def __init__(self):
f = ifcopenshell.open(template_file) 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] def export(self):
owner_history = f.by_type("ifcownerhistory")[0] 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( def set_common_definitions(self):
None, "Model", self.origin = self.file.by_type("IfcAxis2Placement3D")[0]
3, 1.0E-05, # Owner history doesn't actually work like this, but for now, it does :)
origin, self.owner_history = self.file.by_type("ifcownerhistory")[0]
f.createIfcDirection((0., 1., 0.)))
subcontext = f.createIfcGeometricRepresentationSubContext( def create_context(self):
"Body", "Model", self.ifc_context = self.file.createIfcGeometricRepresentationContext(
None, None, None, None, None, "Model",
context, None, "MODEL_VIEW", None) 3, 1.0E-05,
self.origin,
self.file.createIfcDirection((0., 1., 0.)))
project = f.createIfcProject( self.ifc_subcontext = self.file.createIfcGeometricRepresentationSubContext(
ifcopenshell.guid.new(), "Body", "Model",
owner_history, None, None, None, None,
'Name', None, self.ifc_context, None, "MODEL_VIEW", None)
None, None, None, [context],
f.by_type("IfcUnitAssignment")[0])
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( def create_building(self):
ifcopenshell.guid.new(), self.ifc_building_placement = self.file.createIfcLocalPlacement(None, self.origin)
owner_history, 'My building', None, None, self.ifc_building = self.file.createIfcBuilding(
building_placement, ifcopenshell.guid.new(),
None, None, 'ELEMENT', None, None, None) self.owner_history, 'My building', None, None,
self.ifc_building_placement,
building_storey_placement = f.createIfcLocalPlacement(building_placement, origin) 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( def create_building_storey(self):
ifcopenshell.guid.new(), self.ifc_building_storey_placement = self.file.createIfcLocalPlacement(self.ifc_building_placement, self.origin)
owner_history, 'Ground floor', None, None, self.ifc_building_storey = self.file.createIfcBuildingStorey(
building_storey_placement, None, None, 'ELEMENT', None) ifcopenshell.guid.new(),
self.owner_history, 'Ground floor', None, None,
f.createIfcRelAggregates( self.ifc_building_storey_placement, None, None, 'ELEMENT', None)
ifcopenshell.guid.new(), self.file.createIfcRelAggregates(
owner_history, None, None, building, [building_storey]) ifcopenshell.guid.new(),
self.owner_history, None, None, self.ifc_building, [self.ifc_building_storey])
f.createIfcRelAggregates(
ifcopenshell.guid.new(),
owner_history, None, None, project, [building])
ifc_products = [] def create_products(self):
ifc_products = []
for object in bpy.context.selected_objects: for object in bpy.context.selected_objects:
ifc_vertices = [] ifc_vertices = []
ifc_faces = [] ifc_faces = []
for vertice in object.data.vertices: for vertice in object.data.vertices:
ifc_vertices.append( ifc_vertices.append(
f.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 object.data.polygons:
ifc_faces.append(f.createIfcFace([ ifc_faces.append(self.file.createIfcFace([
f.createIfcFaceOuterBound( self.file.createIfcFaceOuterBound(
f.createIfcPolyLoop([ifc_vertices[vertice] for vertice in polygon.vertices]), self.file.createIfcPolyLoop([ifc_vertices[vertice] for vertice in polygon.vertices]),
True)])) True)]))
representation = f.createIfcProductDefinitionShape(None, None, representation = self.file.createIfcProductDefinitionShape(None, None,
[f.createIfcShapeRepresentation( [self.file.createIfcShapeRepresentation(
subcontext, 'Body', 'Brep', self.ifc_subcontext, 'Body', 'Brep',
[f.createIfcFacetedBrep(f.createIfcClosedShell(ifc_faces))])]) [self.file.createIfcFacetedBrep(self.file.createIfcClosedShell(ifc_faces))])])
placement = f.createIfcLocalPlacement(building_storey_placement, placement = self.file.createIfcLocalPlacement(self.ifc_building_storey_placement,
f.createIfcAxis2Placement3D( self.file.createIfcAxis2Placement3D(
f.createIfcCartesianPoint( self.file.createIfcCartesianPoint(
(object.location.x, object.location.y, object.location.z)))) (object.location.x, object.location.y, object.location.z))))
ifc_products.append(f.createIfcBuildingElementProxy( ifc_products.append(self.file.createIfcBuildingElementProxy(
ifcopenshell.guid.new(), ifcopenshell.guid.new(),
owner_history, object.name, None, None, placement, representation, None, None)) self.owner_history, object.name, None, None, placement, representation, None, None))
f.createIfcRelContainedInSpatialStructure(
ifcopenshell.guid.new(), owner_history, None, None, ifc_products, building_storey)
f.write(output_file) self.file.createIfcRelContainedInSpatialStructure(
ifcopenshell.guid.new(), self.owner_history, None, None, ifc_products, self.ifc_building_storey)
ifc_exporter = IfcExporter()
ifc_exporter.export()