Door - added to type manager

Also added predefined type for door and bit of refactoring
This commit is contained in:
Andrej730
2023-02-06 14:42:40 +05:00
parent e57ff19e16
commit 0f5264af99
5 changed files with 96 additions and 84 deletions
@@ -30,7 +30,9 @@ import blenderbim.tool as tool
import blenderbim.core.geometry as core
from blenderbim.bim.helper import convert_property_group_from_si
from blenderbim.bim.ifc import IfcStore
from blenderbim.bim.module.model.window import create_bm_window_frame, create_bm_window, create_bm_box
from blenderbim.bim.module.model.window import create_bm_window, create_bm_box
from blenderbim.bim.module.model.helper import get_ifc_context_or_create, replace_ifc_representation_for_object
from mathutils import Vector
from pprint import pprint
@@ -75,66 +77,27 @@ def update_door_modifier_representation(context):
},
}
def get_context_or_create(context_type, context_identifier, target_view):
ifc_context = ifcopenshell.util.representation.get_context(ifc_file, context_type, context_identifier, target_view)
if not ifc_context:
parent_context = ifcopenshell.util.representation.get_context(ifc_file, context_type)
ifc_context = ifcopenshell.api.run(
"context.add_context",
ifc_file,
context_type=context_type,
context_identifier=context_identifier,
target_view=target_view,
parent=model_context,
)
return ifc_context
# ELEVATION_VIEW representation
ifc_context = get_context_or_create("Model", "Profile", "ELEVATION_VIEW")
ifc_context = get_ifc_context_or_create(ifc_file, "Model", "Profile", "ELEVATION_VIEW")
representation_data["context"] = ifc_context
elevation_representation = ifcopenshell.api.run(
"geometry.add_door_representation", ifc_file, **representation_data
)
replace_representation_for_object(ifc_file, ifc_context, obj, elevation_representation)
replace_ifc_representation_for_object(ifc_file, ifc_context, obj, elevation_representation)
# PLAN_VIEW representation
ifc_context = get_context_or_create("Plan", "Annotation", "PLAN_VIEW")
ifc_context = get_ifc_context_or_create(ifc_file, "Plan", "Annotation", "PLAN_VIEW")
representation_data["context"] = ifc_context
elevation_representation = ifcopenshell.api.run(
"geometry.add_door_representation", ifc_file, **representation_data
)
replace_representation_for_object(ifc_file, ifc_context, obj, elevation_representation)
replace_ifc_representation_for_object(ifc_file, ifc_context, obj, elevation_representation)
# MODEL_VIEW representation
ifc_context = ifcopenshell.util.representation.get_context(ifc_file, "Model", "Body", "MODEL_VIEW")
representation_data["context"] = ifc_context
model_representation = ifcopenshell.api.run("geometry.add_door_representation", ifc_file, **representation_data)
replace_representation_for_object(ifc_file, ifc_context, obj, model_representation)
def replace_representation_for_object(ifc_file, ifc_context, obj, new_representation):
ifc_element = tool.Ifc.get_entity(obj)
old_representation = ifcopenshell.util.representation.get_representation(
ifc_element, ifc_context.ContextType, ifc_context.ContextIdentifier, ifc_context.TargetView
)
if old_representation:
for inverse in ifc_file.get_inverse(old_representation):
ifcopenshell.util.element.replace_attribute(inverse, old_representation, new_representation)
core.remove_representation(tool.Ifc, tool.Geometry, obj=obj, representation=old_representation)
else:
ifcopenshell.api.run(
"geometry.assign_representation", ifc_file, product=ifc_element, representation=new_representation
)
core.switch_representation(
tool.Ifc,
tool.Geometry,
obj=obj,
representation=new_representation,
should_reload=True,
is_global=False,
should_sync_changes_first=True,
)
replace_ifc_representation_for_object(ifc_file, ifc_context, obj, model_representation)
def create_bm_door_lining(bm, size: Vector, thickness: Vector, position:Vector=V(0,0,0).freeze()):
@@ -363,6 +326,8 @@ class BIM_OT_add_door(Operator):
ifc_class="IfcDoor",
should_add_representation=False
)
element.PredefinedType = "DOOR"
bpy.ops.object.select_all(action="DESELECT")
bpy.context.view_layer.objects.active = None
bpy.context.view_layer.objects.active = obj
@@ -0,0 +1,64 @@
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2023 @Andrej730
#
# This file is part of BlenderBIM Add-on.
#
# BlenderBIM Add-on 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.
#
# BlenderBIM Add-on 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 BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import bpy
import bmesh
import ifcopenshell
import ifcopenshell.util.unit
import blenderbim.core.geometry as core
import blenderbim.tool as tool
def get_ifc_context_or_create(ifc_file, context_type, context_identifier, target_view):
ifc_context = ifcopenshell.util.representation.get_context(ifc_file, context_type, context_identifier, target_view)
if not ifc_context:
parent_context = ifcopenshell.util.representation.get_context(ifc_file, context_type)
ifc_context = ifcopenshell.api.run(
"context.add_context",
ifc_file,
context_type=context_type,
context_identifier=context_identifier,
target_view=target_view,
parent=parent_context,
)
return ifc_context
def replace_ifc_representation_for_object(ifc_file, ifc_context, obj, new_representation):
ifc_element = tool.Ifc.get_entity(obj)
old_representation = ifcopenshell.util.representation.get_representation(
ifc_element, ifc_context.ContextType, ifc_context.ContextIdentifier, ifc_context.TargetView
)
if old_representation:
for inverse in ifc_file.get_inverse(old_representation):
ifcopenshell.util.element.replace_attribute(inverse, old_representation, new_representation)
core.remove_representation(tool.Ifc, tool.Geometry, obj=obj, representation=old_representation)
else:
ifcopenshell.api.run(
"geometry.assign_representation", ifc_file, product=ifc_element, representation=new_representation
)
core.switch_representation(
tool.Ifc,
tool.Geometry,
obj=obj,
representation=new_representation,
should_reload=True,
is_global=False,
should_sync_changes_first=True,
)
@@ -242,6 +242,7 @@ class BIMModelProperties(PropertyGroup):
("PROFILESET", "Extruded Profile", ""),
("EMPTY", "Non-Geometric Type", ""),
("WINDOW", "Window", ""),
("DOOR", "Door", ""),
("STAIR", "Stair", ""),
),
name="Type Template",
@@ -31,6 +31,7 @@ import blenderbim.tool as tool
import blenderbim.core.geometry as core
from blenderbim.bim.helper import convert_property_group_from_si
from blenderbim.bim.ifc import IfcStore
from blenderbim.bim.module.model.helper import get_ifc_context_or_create, replace_ifc_representation_for_object
from mathutils import Vector
from pprint import pprint
@@ -75,54 +76,18 @@ def update_window_modifier_representation(context):
representation_data["panel_properties"].append(panel_data)
# ELEVATION_VIEW representation
ifc_context = ifcopenshell.util.representation.get_context(ifc_file, "Model", "Profile", "ELEVATION_VIEW")
if not ifc_context:
model_context = ifcopenshell.util.representation.get_context(ifc_file, "Model")
ifc_context = ifcopenshell.api.run(
"context.add_context",
ifc_file,
context_type="Model",
context_identifier="Profile",
target_view="ELEVATION_VIEW",
parent=model_context,
)
ifc_context = get_ifc_context_or_create(ifc_file, "Model", "Profile", "ELEVATION_VIEW")
representation_data["context"] = ifc_context
elevation_representation = ifcopenshell.api.run(
"geometry.add_window_representation", ifc_file, **representation_data
)
replace_representation_for_object(ifc_file, ifc_context, obj, elevation_representation)
replace_ifc_representation_for_object(ifc_file, ifc_context, obj, elevation_representation)
# MODEL_VIEW representation
ifc_context = ifcopenshell.util.representation.get_context(ifc_file, "Model", "Body", "MODEL_VIEW")
representation_data["context"] = ifc_context
model_representation = ifcopenshell.api.run("geometry.add_window_representation", ifc_file, **representation_data)
replace_representation_for_object(ifc_file, ifc_context, obj, model_representation)
def replace_representation_for_object(ifc_file, ifc_context, obj, new_representation):
ifc_element = tool.Ifc.get_entity(obj)
old_representation = ifcopenshell.util.representation.get_representation(
ifc_element, ifc_context.ContextType, ifc_context.ContextIdentifier, ifc_context.TargetView
)
if old_representation:
for inverse in ifc_file.get_inverse(old_representation):
ifcopenshell.util.element.replace_attribute(inverse, old_representation, new_representation)
core.remove_representation(tool.Ifc, tool.Geometry, obj=obj, representation=old_representation)
else:
ifcopenshell.api.run(
"geometry.assign_representation", ifc_file, product=ifc_element, representation=new_representation
)
core.switch_representation(
tool.Ifc,
tool.Geometry,
obj=obj,
representation=new_representation,
should_reload=True,
is_global=False,
should_sync_changes_first=True,
)
replace_ifc_representation_for_object(ifc_file, ifc_context, obj, model_representation)
def create_bm_window_frame(bm, size: Vector, thickness: Vector, position: Vector = V(0,0,0)):
@@ -167,6 +167,7 @@ class AddType(bpy.types.Operator, tool.Ifc.Operator):
body = ifcopenshell.util.representation.get_context(ifc_file, "Model", "Body", "MODEL_VIEW")
if not body:
return {"FINISHED"}
if template == "MESH":
location = context.scene.cursor.location
if context.active_object and context.selected_objects and context.active_object.data:
@@ -289,6 +290,22 @@ class AddType(bpy.types.Operator, tool.Ifc.Operator):
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.bim.add_window()
elif template == "DOOR":
mesh = bpy.data.meshes.new("IfcDoor")
obj = bpy.data.objects.new("TYPEX", mesh)
element = blenderbim.core.root.assign_class(
tool.Ifc,
tool.Collector,
tool.Root,
obj=obj,
ifc_class="IfcDoorType",
should_add_representation=False
)
bpy.ops.object.select_all(action="DESELECT")
bpy.context.view_layer.objects.active = None
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.bim.add_door()
elif template == "STAIR":
mesh = bpy.data.meshes.new("IfcStairFlight")