cleanup and black .

This commit is contained in:
falken10vdl
2026-01-13 08:49:57 +01:00
parent 1eb2b65268
commit 791a41d649
6 changed files with 66 additions and 68 deletions
+26 -29
View File
@@ -186,7 +186,6 @@ class ShowPorts(bpy.types.Operator, tool.Ifc.Operator):
bl_idname = "bim.show_ports" bl_idname = "bim.show_ports"
bl_label = "Show Ports" bl_label = "Show Ports"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
element_id: bpy.props.IntProperty(default=0, options={"SKIP_SAVE"})
@classmethod @classmethod
def poll(cls, context): def poll(cls, context):
@@ -199,12 +198,9 @@ class ShowPorts(bpy.types.Operator, tool.Ifc.Operator):
def _execute(self, context): def _execute(self, context):
# Ifc.Operator - as operator will sync object's position with IFC. # Ifc.Operator - as operator will sync object's position with IFC.
if self.element_id != 0: element = tool.Ifc.get_entity(context.active_object)
element = tool.Ifc.get().by_id(self.element_id)
else:
element = tool.Ifc.get_entity(context.active_object)
core.show_ports(tool.Ifc, tool.System, tool.Spatial, element=element) core.show_ports(tool.Ifc, tool.System, tool.Spatial, element=element)
for port in tool.System.get_ports(element): for port in tool.System.get_ports(element):
connected_port = tool.System.get_connected_port(port) connected_port = tool.System.get_connected_port(port)
if connected_port: if connected_port:
@@ -260,37 +256,39 @@ class ConnectPort(bpy.types.Operator, tool.Ifc.Operator):
obj1 = context.active_object obj1 = context.active_object
obj2 = context.selected_objects[0] if context.selected_objects[1] == obj1 else context.selected_objects[1] obj2 = context.selected_objects[0] if context.selected_objects[1] == obj1 else context.selected_objects[1]
direction = tool.Ifc.get_entity(obj1).FlowDirection or "NOTDEFINED" direction = tool.Ifc.get_entity(obj1).FlowDirection or "NOTDEFINED"
core.connect_port(tool.Ifc, port1=tool.Ifc.get_entity(obj1), port2=tool.Ifc.get_entity(obj2), direction=direction) core.connect_port(
tool.Ifc, port1=tool.Ifc.get_entity(obj1), port2=tool.Ifc.get_entity(obj2), direction=direction
)
class AddRelatedPortConnection(bpy.types.Operator, tool.Ifc.Operator): class AddRelatedPortConnection(bpy.types.Operator, tool.Ifc.Operator):
bl_idname = "bim.add_related_port_connection" bl_idname = "bim.add_related_port_connection"
bl_label = "Connect Port" bl_label = "Connect Port"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
relating_port_id: bpy.props.IntProperty() relating_port_id: bpy.props.IntProperty()
def invoke(self, context, event): def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self) return context.window_manager.invoke_props_dialog(self)
def draw(self, context): def draw(self, context):
props = tool.System.get_system_props() props = tool.System.get_system_props()
self.layout.prop(props, "related_port", text="Select Port") self.layout.prop(props, "related_port", text="Select Port")
def _execute(self, context): def _execute(self, context):
props = tool.System.get_system_props() props = tool.System.get_system_props()
if not props.related_port or props.related_port == "NONE": if not props.related_port or props.related_port == "NONE":
return {"CANCELLED"} return {"CANCELLED"}
port_obj = bpy.data.objects.get(props.related_port) port_obj = bpy.data.objects.get(props.related_port)
related_port = tool.Ifc.get_entity(port_obj) related_port = tool.Ifc.get_entity(port_obj)
relating_port = tool.Ifc.get().by_id(self.relating_port_id) relating_port = tool.Ifc.get().by_id(self.relating_port_id)
direction = relating_port.FlowDirection or "NOTDEFINED" direction = relating_port.FlowDirection or "NOTDEFINED"
core.connect_port(tool.Ifc, port1=relating_port, port2=related_port, direction=direction) core.connect_port(tool.Ifc, port1=relating_port, port2=related_port, direction=direction)
PortData.is_loaded = False PortData.is_loaded = False
return {"FINISHED"} return {"FINISHED"}
@@ -436,13 +434,10 @@ class CycleFlowDirection(bpy.types.Operator, tool.Ifc.Operator):
@classmethod @classmethod
def description(cls, context, operator): def description(cls, context, operator):
try: port = tool.Ifc.get().by_id(operator.port_id)
port = tool.Ifc.get().by_id(operator.port_id) if port and port.is_a("IfcDistributionPort"):
if port and port.is_a("IfcDistributionPort"): current_direction = port.FlowDirection or "NOTDEFINED"
current_direction = port.FlowDirection or "NOTDEFINED" return f"Current flow direction: {current_direction}. Click to cycle: SOURCE → SINK → SOURCEANDSINK → NOTDEFINED"
return f"Current flow direction: {current_direction}. Click to cycle: SOURCE → SINK → SOURCEANDSINK → NOTDEFINED"
except:
pass
return "Cycle through flow directions: SOURCE → SINK → SOURCEANDSINK → NOTDEFINED → SOURCE..." return "Cycle through flow directions: SOURCE → SINK → SOURCEANDSINK → NOTDEFINED → SOURCE..."
def _execute(self, context): def _execute(self, context):
@@ -451,30 +446,32 @@ class CycleFlowDirection(bpy.types.Operator, tool.Ifc.Operator):
return {"CANCELLED"} return {"CANCELLED"}
current_direction = port.FlowDirection or "NOTDEFINED" current_direction = port.FlowDirection or "NOTDEFINED"
flow_cycle_map = { flow_cycle_map = {
"SOURCE": "SINK", "SOURCE": "SINK",
"SINK": "SOURCEANDSINK", "SINK": "SOURCEANDSINK",
"SOURCEANDSINK": "NOTDEFINED", "SOURCEANDSINK": "NOTDEFINED",
"NOTDEFINED": "SOURCE" "NOTDEFINED": "SOURCE",
} }
next_direction = flow_cycle_map.get(current_direction, "SOURCE") next_direction = flow_cycle_map.get(current_direction, "SOURCE")
tool.Ifc.run("attribute.edit_attributes", product=port, attributes={"FlowDirection": next_direction}) tool.Ifc.run("attribute.edit_attributes", product=port, attributes={"FlowDirection": next_direction})
connected_port = tool.System.get_connected_port(port) connected_port = tool.System.get_connected_port(port)
if connected_port: if connected_port:
connected_direction_map = { connected_direction_map = {
"SOURCE": "SINK", "SOURCE": "SINK",
"SINK": "SOURCE", "SINK": "SOURCE",
"SOURCEANDSINK": "SOURCEANDSINK", "SOURCEANDSINK": "SOURCEANDSINK",
"NOTDEFINED": "NOTDEFINED" "NOTDEFINED": "NOTDEFINED",
} }
connected_direction = connected_direction_map.get(next_direction, "NOTDEFINED") connected_direction = connected_direction_map.get(next_direction, "NOTDEFINED")
tool.Ifc.run("attribute.edit_attributes", product=connected_port, attributes={"FlowDirection": connected_direction}) tool.Ifc.run(
"attribute.edit_attributes", product=connected_port, attributes={"FlowDirection": connected_direction}
)
PortData.is_loaded = False PortData.is_loaded = False
return {"FINISHED"} return {"FINISHED"}
+7 -6
View File
@@ -19,7 +19,6 @@
import bpy import bpy
import bonsai.bim.handler import bonsai.bim.handler
import bonsai.tool as tool import bonsai.tool as tool
import bonsai.core.system as core
from bonsai.bim.module.system.data import SystemData from bonsai.bim.module.system.data import SystemData
import bonsai.bim.module.system.decorator as decorator import bonsai.bim.module.system.decorator as decorator
from bonsai.bim.prop import StrProperty, Attribute from bonsai.bim.prop import StrProperty, Attribute
@@ -93,23 +92,25 @@ def toggle_decorations(self: "BIMSystemProperties", context: bpy.types.Context)
decorator.SystemDecorator.uninstall() decorator.SystemDecorator.uninstall()
def get_available_ports_for_connection(self: "BIMSystemProperties", context: bpy.types.Context) -> list[tuple[str, str, str]]: def get_available_ports_for_connection(
self: "BIMSystemProperties", context: bpy.types.Context
) -> list[tuple[str, str, str]]:
items = [] items = []
active_object_ports = set(tool.System.get_ports(tool.Ifc.get_entity(context.active_object))) active_object_ports = set(tool.System.get_ports(tool.Ifc.get_entity(context.active_object)))
ifc_file = tool.Ifc.get() ifc_file = tool.Ifc.get()
for ifc_port in ifc_file.by_type("IfcDistributionPort"): for ifc_port in ifc_file.by_type("IfcDistributionPort"):
port = tool.Ifc.get_object(ifc_port) port = tool.Ifc.get_object(ifc_port)
if not port: if not port:
continue continue
if tool.System.get_connected_port(ifc_port) is not None or ifc_port in active_object_ports: if tool.System.get_connected_port(ifc_port) is not None or ifc_port in active_object_ports:
continue continue
port_object = tool.Ifc.get_object(tool.System.get_port_relating_element(ifc_port)) port_object = tool.Ifc.get_object(tool.System.get_port_relating_element(ifc_port))
suggestion = f"{port_object.name} > {port.name}" suggestion = f"{port_object.name} > {port.name}"
items.append((port.name, suggestion, "")) items.append((port.name, suggestion, ""))
return items if items else [("NONE", "Ports are hidden or not available", "")] return items if items else [("NONE", "Ports are hidden or not available", "")]
+25 -30
View File
@@ -162,11 +162,9 @@ class BIM_PT_ports(Panel):
if total_ports == 0: if total_ports == 0:
return return
props = tool.System.get_system_props()
row = self.layout.row(align=True) row = self.layout.row(align=True)
row.label(text="Ports located on object and connected Port/Objects:") row.label(text=f"Ports located in: {context.active_object.name} and connected Port/Objects:")
row = self.layout.row(align=True) row = self.layout.row(align=True)
cols = [row.column(align=True) for i in range(9)] cols = [row.column(align=True) for i in range(9)]
cols[3].scale_x = 1.0 cols[3].scale_x = 1.0
@@ -175,18 +173,17 @@ class BIM_PT_ports(Panel):
for port_data in PortData.data["located_ports_data"]: for port_data in PortData.data["located_ports_data"]:
flow_direction_icon = FLOW_DIRECTION_TO_ICON[port_data["FlowDirection"] or "NOTDEFINED"] flow_direction_icon = FLOW_DIRECTION_TO_ICON[port_data["FlowDirection"] or "NOTDEFINED"]
if port_data["connected_obj_name"]: if port_data["connected_obj_name"]:
cols[0].operator("bim.disconnect_port", text="", icon="UNLINKED").element_id = port_data["id"] cols[0].operator("bim.disconnect_port", text="", icon="UNLINKED").element_id = port_data["id"]
op = cols[1].operator("bim.cycle_flow_direction", text="", icon=flow_direction_icon, emboss=True) op = cols[1].operator("bim.cycle_flow_direction", text="", icon=flow_direction_icon, emboss=True)
op.port_id = port_data["id"] op.port_id = port_data["id"]
else: else:
op = cols[0].operator("bim.add_related_port_connection", text="", icon='PLUGIN') op = cols[0].operator("bim.add_related_port_connection", text="", icon="PLUGIN")
op.relating_port_id = port_data["id"] op.relating_port_id = port_data["id"]
op = cols[1].operator("bim.cycle_flow_direction", text="", icon=flow_direction_icon, emboss=True) op = cols[1].operator("bim.cycle_flow_direction", text="", icon=flow_direction_icon, emboss=True)
op.port_id = port_data["id"] op.port_id = port_data["id"]
# Port information
if port_data["port_obj_name"]: if port_data["port_obj_name"]:
cols[2].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = port_data["id"] cols[2].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = port_data["id"]
cols[3].label(text=port_data["port_obj_name"]) cols[3].label(text=port_data["port_obj_name"])
@@ -196,16 +193,20 @@ class BIM_PT_ports(Panel):
if port_data["connected_obj_name"]: if port_data["connected_obj_name"]:
connected_obj = bpy.data.objects[port_data["connected_obj_name"]] connected_obj = bpy.data.objects[port_data["connected_obj_name"]]
port = tool.Ifc.get().by_id(port_data["id"]) port = tool.Ifc.get().by_id(port_data["id"])
connected_port = tool.System.get_connected_port(port) connected_port = tool.System.get_connected_port(port)
if connected_port: if connected_port:
connected_port_obj = tool.Ifc.get_object(connected_port) connected_port_obj = tool.Ifc.get_object(connected_port)
if connected_port_obj: if connected_port_obj:
connected_port_flow_dir = FLOW_DIRECTION_TO_ICON[connected_port.FlowDirection or "NOTDEFINED"] connected_port_flow_dir = FLOW_DIRECTION_TO_ICON[connected_port.FlowDirection or "NOTDEFINED"]
op = cols[4].operator("bim.cycle_flow_direction", text="", icon=connected_port_flow_dir, emboss=True) op = cols[4].operator(
"bim.cycle_flow_direction", text="", icon=connected_port_flow_dir, emboss=True
)
op.port_id = connected_port.id() op.port_id = connected_port.id()
cols[5].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = connected_port.id() cols[5].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = (
connected_port.id()
)
cols[6].label(text=connected_port_obj.name) cols[6].label(text=connected_port_obj.name)
else: else:
blank4 = cols[4].column(align=True) blank4 = cols[4].column(align=True)
@@ -223,7 +224,7 @@ class BIM_PT_ports(Panel):
blank5.scale_x = 0.1 blank5.scale_x = 0.1
blank5.label(text="", icon="BLANK1") blank5.label(text="", icon="BLANK1")
cols[6].label(text="") cols[6].label(text="")
ifc_id = tool.Blender.get_ifc_definition_id(connected_obj) ifc_id = tool.Blender.get_ifc_definition_id(connected_obj)
cols[7].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = ifc_id cols[7].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = ifc_id
cols[8].label(text=port_data["connected_obj_name"]) cols[8].label(text=port_data["connected_obj_name"])
@@ -234,9 +235,9 @@ class BIM_PT_ports(Panel):
blank5 = cols[5].column(align=True) blank5 = cols[5].column(align=True)
blank5.scale_x = 0.1 blank5.scale_x = 0.1
blank5.label(text="", icon="BLANK1") blank5.label(text="", icon="BLANK1")
cols[6].label(text="Port is disconnected") cols[6].label(text="Port is disconnected")
blank7 = cols[7].column(align=True) blank7 = cols[7].column(align=True)
blank7.scale_x = 0.1 blank7.scale_x = 0.1
blank7.label(text="", icon="BLANK1") blank7.label(text="", icon="BLANK1")
@@ -265,17 +266,8 @@ class BIM_PT_port(Panel):
return True return True
def draw(self, context): def draw(self, context):
self.props = tool.System.get_system_props()
layout = self.layout layout = self.layout
row = layout.row(align=True) row = layout.row(align=True)
if not PortData.is_loaded:
PortData.load()
relating_object_name = PortData.data["port_relating_object_name"] if PortData.data["is_port"] else ""
row.label(text=f"IfcDistributionPort located in: {relating_object_name}")
row.operator("bim.remove_port", icon="X", text="")
if not PortData.is_loaded: if not PortData.is_loaded:
PortData.load() PortData.load()
@@ -283,18 +275,21 @@ class BIM_PT_port(Panel):
if not PortData.data["is_port"]: if not PortData.data["is_port"]:
return return
relating_object_name = PortData.data["port_relating_object_name"]
row.label(text=f"IfcDistributionPort located in: {relating_object_name}")
row.operator("bim.remove_port", icon="X", text="")
element = tool.Ifc.get_entity(context.active_object) element = tool.Ifc.get_entity(context.active_object)
props = tool.System.get_system_props()
row = layout.row(align=True) row = layout.row(align=True)
cols = [row.column(align=True) for i in range(9)] cols = [row.column(align=True) for i in range(9)]
cols[3].scale_x = 1.0 cols[3].scale_x = 1.0
cols[6].scale_x = 1.0 cols[6].scale_x = 1.0
cols[8].scale_x = 1.33 cols[8].scale_x = 1.33
flow_direction_icon = FLOW_DIRECTION_TO_ICON[element.FlowDirection or "NOTDEFINED"] flow_direction_icon = FLOW_DIRECTION_TO_ICON[element.FlowDirection or "NOTDEFINED"]
connected_port = tool.System.get_connected_port(element) connected_port = tool.System.get_connected_port(element)
if connected_port: if connected_port:
cols[0].operator("bim.disconnect_port", text="", icon="UNLINKED") cols[0].operator("bim.disconnect_port", text="", icon="UNLINKED")
op = cols[1].operator("bim.cycle_flow_direction", text="", icon=flow_direction_icon, emboss=True) op = cols[1].operator("bim.cycle_flow_direction", text="", icon=flow_direction_icon, emboss=True)
@@ -303,10 +298,10 @@ class BIM_PT_port(Panel):
cols[0].operator("bim.connect_port", icon="PLUGIN", text="") cols[0].operator("bim.connect_port", icon="PLUGIN", text="")
op = cols[1].operator("bim.cycle_flow_direction", text="", icon=flow_direction_icon, emboss=True) op = cols[1].operator("bim.cycle_flow_direction", text="", icon=flow_direction_icon, emboss=True)
op.port_id = element.id() op.port_id = element.id()
cols[2].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = element.id() cols[2].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = element.id()
cols[3].label(text=context.active_object.name) cols[3].label(text=context.active_object.name)
if connected_port: if connected_port:
connected_port_flow_dir = FLOW_DIRECTION_TO_ICON[connected_port.FlowDirection or "NOTDEFINED"] connected_port_flow_dir = FLOW_DIRECTION_TO_ICON[connected_port.FlowDirection or "NOTDEFINED"]
op = cols[4].operator("bim.cycle_flow_direction", text="", icon=connected_port_flow_dir, emboss=True) op = cols[4].operator("bim.cycle_flow_direction", text="", icon=connected_port_flow_dir, emboss=True)
@@ -314,7 +309,7 @@ class BIM_PT_port(Panel):
cols[5].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = connected_port.id() cols[5].operator("bim.select_entity", text="", icon="RESTRICT_SELECT_OFF").ifc_id = connected_port.id()
connected_port_obj = tool.Ifc.get_object(connected_port) connected_port_obj = tool.Ifc.get_object(connected_port)
cols[6].label(text=connected_port_obj.name if connected_port_obj else "Hidden Port") cols[6].label(text=connected_port_obj.name if connected_port_obj else "Hidden Port")
connected_object_name = PortData.data["port_connected_object_name"] connected_object_name = PortData.data["port_connected_object_name"]
if connected_object_name: if connected_object_name:
connected_obj = bpy.data.objects[connected_object_name] connected_obj = bpy.data.objects[connected_object_name]
+6 -1
View File
@@ -132,7 +132,12 @@ def remove_port(ifc: type[tool.Ifc], system: type[tool.System], port: ifcopenshe
ifc.run("root.remove_product", product=port) ifc.run("root.remove_product", product=port)
def connect_port(ifc: type[tool.Ifc], port1: ifcopenshell.entity_instance, port2: ifcopenshell.entity_instance, direction: str = "NOTDEFINED") -> None: def connect_port(
ifc: type[tool.Ifc],
port1: ifcopenshell.entity_instance,
port2: ifcopenshell.entity_instance,
direction: str = "NOTDEFINED",
) -> None:
ifc.run("system.connect_port", port1=port1, port2=port2, direction=direction) ifc.run("system.connect_port", port1=port1, port2=port2, direction=direction)
+1
View File
@@ -100,6 +100,7 @@ class System(bonsai.core.tool.System):
@classmethod @classmethod
def create_empty_at_cursor_with_element_orientation(cls, element: ifcopenshell.entity_instance) -> bpy.types.Object: def create_empty_at_cursor_with_element_orientation(cls, element: ifcopenshell.entity_instance) -> bpy.types.Object:
# Is this necessary anymore?
element_obj = tool.Ifc.get_object(element) element_obj = tool.Ifc.get_object(element)
obj = bpy.data.objects.new("Port", None) obj = bpy.data.objects.new("Port", None)
obj.matrix_world = element_obj.matrix_world obj.matrix_world = element_obj.matrix_world
@@ -490,8 +490,7 @@ class Usecase:
attribute_class = None attribute_class = None
if "." in attribute: if "." in attribute:
attribute, attribute_class = attribute.split(".") attribute, attribute_class = attribute.split(".")
inverse_values = getattr(element, attribute, []) for inverse in getattr(element, attribute, []):
for inverse in inverse_values:
if attribute_class and inverse.is_a(attribute_class): if attribute_class and inverse.is_a(attribute_class):
self.add_inverse_element(inverse) self.add_inverse_element(inverse)
elif not attribute_class: elif not attribute_class: