mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-16 18:44:47 +00:00
103 lines
4.2 KiB
Python
103 lines
4.2 KiB
Python
# Bonsai - OpenBIM Blender Add-on
|
|
# Copyright (C) 2020, 2021 Dion Moult <dion@thinkmoult.com>
|
|
#
|
|
# This file is part of Bonsai.
|
|
#
|
|
# Bonsai is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Bonsai is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import bpy
|
|
import ifcopenshell
|
|
import ifcopenshell.api
|
|
import ifcopenshell.api.context
|
|
import ifcopenshell.api.geometry
|
|
import ifcopenshell.api.project
|
|
import ifcopenshell.api.root
|
|
import ifcopenshell.api.style
|
|
import ifcopenshell.api.unit
|
|
import bonsai.tool as tool
|
|
|
|
|
|
class LibraryGenerator:
|
|
def generate(self):
|
|
ifcopenshell.api.pre_listeners = {}
|
|
ifcopenshell.api.post_listeners = {}
|
|
|
|
self.file = ifcopenshell.api.project.create_file()
|
|
self.project = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject", name="Bonsai Demo")
|
|
self.library = ifcopenshell.api.root.create_entity(
|
|
self.file, ifc_class="IfcProjectLibrary", name="Bonsai Demo Library"
|
|
)
|
|
ifcopenshell.api.project.assign_declaration(
|
|
self.file, definitions=[self.library], relating_context=self.library
|
|
)
|
|
ifcopenshell.api.unit.assign_unit(self.file, length={"is_metric": True, "raw": "METERS"})
|
|
model = ifcopenshell.api.context.add_context(self.file, context_type="Model")
|
|
self.representations = {
|
|
"body": ifcopenshell.api.context.add_context(
|
|
self.file,
|
|
context_type="Model",
|
|
context_identifier="Body",
|
|
target_view="MODEL_VIEW",
|
|
parent=model,
|
|
),
|
|
"clearance": ifcopenshell.api.context.add_context(
|
|
self.file,
|
|
context_type="Model",
|
|
context_identifier="Clearance",
|
|
target_view="MODEL_VIEW",
|
|
parent=model,
|
|
),
|
|
}
|
|
|
|
self.create_type("IfcBuildingElementProxyType", "Site Shed 3x6m", {"body": "Site Shed 3x6m"})
|
|
self.create_type("IfcBuildingElementProxyType", "Site Shed 3x12m", {"body": "Site Shed 3x12m"})
|
|
self.create_type(
|
|
"IfcBuildingElementProxyType",
|
|
"Mobile Crane 50T",
|
|
{"body": "Mobile Crane 50T", "clearance": "Mobile Crane 50T - Clearance"},
|
|
)
|
|
|
|
self.file.write("bonsai-site-library.ifc")
|
|
|
|
def create_type(self, ifc_class, name, representations):
|
|
element = ifcopenshell.api.root.create_entity(self.file, ifc_class=ifc_class, name=name)
|
|
for rep_name, obj_name in representations.items():
|
|
obj = bpy.data.objects.get(obj_name)
|
|
representation = ifcopenshell.api.geometry.add_representation(
|
|
self.file,
|
|
context=self.representations[rep_name],
|
|
blender_object=obj,
|
|
geometry=obj.data,
|
|
total_items=max(1, len(obj.material_slots)),
|
|
)
|
|
styles = []
|
|
for slot in obj.material_slots:
|
|
style = ifcopenshell.api.style.add_style(self.file, name=slot.material.name)
|
|
ifcopenshell.api.style.add_surface_style(
|
|
self.file,
|
|
style=style,
|
|
ifc_class="IfcSurfaceStyleRendering",
|
|
attributes=tool.Style.get_surface_rendering_attributes(slot.material),
|
|
)
|
|
styles.append(style)
|
|
if styles:
|
|
ifcopenshell.api.style.assign_representation_styles(
|
|
self.file, shape_representation=representation, styles=styles
|
|
)
|
|
ifcopenshell.api.geometry.assign_representation(self.file, product=element, representation=representation)
|
|
ifcopenshell.api.project.assign_declaration(self.file, definitions=[element], relating_context=self.library)
|
|
|
|
|
|
LibraryGenerator().generate()
|