From 4f8a53f048ce93420a1582bff15f5ed3cf90b4a8 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 10 Aug 2023 15:06:16 +0500 Subject: [PATCH] quickly connect mep elements if they have matching ports Demo - https://imgur.com/a/o9u8M1u --- .../blenderbim/bim/module/system/__init__.py | 1 + .../blenderbim/bim/module/system/operator.py | 41 +++++++++++++++++++ .../blenderbim/bim/module/system/ui.py | 1 + 3 files changed, 43 insertions(+) diff --git a/src/blenderbim/blenderbim/bim/module/system/__init__.py b/src/blenderbim/blenderbim/bim/module/system/__init__.py index 6cc772e135..a57b7fb5a8 100644 --- a/src/blenderbim/blenderbim/bim/module/system/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/system/__init__.py @@ -34,6 +34,7 @@ classes = ( operator.RemovePort, operator.RemoveSystem, operator.SelectSystemProducts, + operator.MEPConnectElements, operator.SetFlowDirection, operator.ShowPorts, operator.UnassignSystem, diff --git a/src/blenderbim/blenderbim/bim/module/system/operator.py b/src/blenderbim/blenderbim/bim/module/system/operator.py index 00b86936e3..e3a35e3670 100644 --- a/src/blenderbim/blenderbim/bim/module/system/operator.py +++ b/src/blenderbim/blenderbim/bim/module/system/operator.py @@ -23,6 +23,7 @@ import blenderbim.core.system as core import blenderbim.bim.handler from blenderbim.bim.ifc import IfcStore from blenderbim.bim.module.system.data import PortData +from mathutils import Matrix class Operator: @@ -215,6 +216,46 @@ class DisconnectPort(bpy.types.Operator, Operator): core.disconnect_port(tool.Ifc, port=element) +class MEPConnectElements(bpy.types.Operator, Operator): + bl_idname = "bim.mep_connect_elements" + bl_label = "Connect MEP Elements" + bl_description = "Connects two selected elements if they have ports with matching location" + bl_options = {"REGISTER", "UNDO"} + + @classmethod + def poll(cls, context): + return len(context.selected_objects) == 2 + + def _execute(self, context): + obj1 = context.active_object + obj2 = next(o for o in context.selected_objects if o != obj1) + + el1 = tool.Ifc.get_entity(obj1) + el2 = tool.Ifc.get_entity(obj2) + + obj1_ports = [p for p in tool.System.get_ports(el1) if not tool.System.get_connected_port(p)] + obj2_ports = [p for p in tool.System.get_ports(el2) if not tool.System.get_connected_port(p)] + + if not obj1_ports or not obj2_ports: + self.report({"ERROR"}, "Couldn't find free ports to connect.") + return + + def get_element_matrix(element): + placement = ifcopenshell.util.placement.get_local_placement(element.ObjectPlacement) + return Matrix(placement) + + for port1 in obj1_ports: + port1_location = get_element_matrix(port1).translation + for port2 in obj2_ports: + port2_location = get_element_matrix(port2).translation + if tool.Cad.are_vectors_equal(port1_location, port2_location): + core.connect_port(tool.Ifc, port1, port2) + return {"FINISHED"} + + self.report({"ERROR"}, "Couldn't find any matching ports to connect.") + return {"CANCELLED"} + + class SetFlowDirection(bpy.types.Operator, Operator): bl_idname = "bim.set_flow_direction" bl_label = "Set Flow Direction" diff --git a/src/blenderbim/blenderbim/bim/module/system/ui.py b/src/blenderbim/blenderbim/bim/module/system/ui.py index 643d10ea93..b0b411e23a 100644 --- a/src/blenderbim/blenderbim/bim/module/system/ui.py +++ b/src/blenderbim/blenderbim/bim/module/system/ui.py @@ -166,6 +166,7 @@ class BIM_PT_ports(Panel): row = self.layout.row(align=True) total_ports = PortData.data["total_ports"] row.label(text=f"{total_ports} Ports Found", icon="PLUGIN") + row.operator("bim.mep_connect_elements", text="", icon="PLUGIN") row.operator("bim.show_ports", icon="HIDE_OFF", text="") row.operator("bim.hide_ports", icon="HIDE_ON", text="") row.operator("bim.add_port", icon="ADD", text="")