mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-20 06:58:56 +00:00
bim.mep_connect_elements to connect elements by the closest ports
Previously it was looking for exact match. Now operator also adjusts connected ports
This commit is contained in:
@@ -219,12 +219,15 @@ class DisconnectPort(bpy.types.Operator, Operator):
|
|||||||
class MEPConnectElements(bpy.types.Operator, Operator):
|
class MEPConnectElements(bpy.types.Operator, Operator):
|
||||||
bl_idname = "bim.mep_connect_elements"
|
bl_idname = "bim.mep_connect_elements"
|
||||||
bl_label = "Connect MEP Elements"
|
bl_label = "Connect MEP Elements"
|
||||||
bl_description = "Connects two selected elements if they have ports with matching location"
|
bl_description = "Connects two selected elements by their closest located ports and adjusts them"
|
||||||
bl_options = {"REGISTER", "UNDO"}
|
bl_options = {"REGISTER", "UNDO"}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def poll(cls, context):
|
def poll(cls, context):
|
||||||
return len(context.selected_objects) == 2
|
if not len(context.selected_objects) == 2:
|
||||||
|
cls.poll_message_set("Need to select 2 objects.")
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def _execute(self, context):
|
def _execute(self, context):
|
||||||
obj1 = context.active_object
|
obj1 = context.active_object
|
||||||
@@ -233,23 +236,32 @@ class MEPConnectElements(bpy.types.Operator, Operator):
|
|||||||
el1 = tool.Ifc.get_entity(obj1)
|
el1 = tool.Ifc.get_entity(obj1)
|
||||||
el2 = tool.Ifc.get_entity(obj2)
|
el2 = tool.Ifc.get_entity(obj2)
|
||||||
|
|
||||||
|
connected_elements = ifcopenshell.util.system.get_connected_to(el1)
|
||||||
|
connected_elements += ifcopenshell.util.system.get_connected_to(el2)
|
||||||
|
|
||||||
|
if el2 in connected_elements:
|
||||||
|
self.report({"ERROR"}, "MEP elements are already connected to each other.")
|
||||||
|
return {"CANCELLED"}
|
||||||
|
|
||||||
obj1_ports = [p for p in tool.System.get_ports(el1) if not tool.System.get_connected_port(p)]
|
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)]
|
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:
|
if not obj1_ports or not obj2_ports:
|
||||||
self.report({"ERROR"}, "Couldn't find free ports to connect.")
|
self.report({"ERROR"}, "Couldn't find free ports to connect.")
|
||||||
return
|
return {"CANCELLED"}
|
||||||
|
|
||||||
|
ports_distance = dict()
|
||||||
for port1 in obj1_ports:
|
for port1 in obj1_ports:
|
||||||
port1_location = tool.Model.get_element_matrix(port1).translation
|
port1_location = tool.Model.get_element_matrix(port1).translation
|
||||||
for port2 in obj2_ports:
|
for port2 in obj2_ports:
|
||||||
port2_location = tool.Model.get_element_matrix(port2).translation
|
port2_location = tool.Model.get_element_matrix(port2).translation
|
||||||
if tool.Cad.are_vectors_equal(port1_location, port2_location):
|
distance = (port1_location - port2_location).length
|
||||||
core.connect_port(tool.Ifc, port1, port2)
|
ports_distance[(port1, port2)] = distance
|
||||||
return {"FINISHED"}
|
|
||||||
|
|
||||||
self.report({"ERROR"}, "Couldn't find any matching ports to connect.")
|
closest_ports = min(ports_distance, key=lambda x: ports_distance[x])
|
||||||
return {"CANCELLED"}
|
core.connect_port(tool.Ifc, *closest_ports)
|
||||||
|
bpy.ops.bim.regenerate_distribution_element()
|
||||||
|
return {"FINISHED"}
|
||||||
|
|
||||||
|
|
||||||
class SetFlowDirection(bpy.types.Operator, Operator):
|
class SetFlowDirection(bpy.types.Operator, Operator):
|
||||||
|
|||||||
Reference in New Issue
Block a user