|
22 | 22 | import ifcopenshell |
23 | 23 | import ifcopenshell.api |
24 | 24 | import ifcopenshell.util.unit |
| 25 | +import ifcopenshell.util.system |
25 | 26 | import ifcopenshell.util.element |
26 | 27 | import ifcopenshell.util.representation |
27 | 28 | import mathutils.geometry |
|
39 | 40 | float_is_zero = lambda f: 0.0001 >= f >= -0.0001 |
40 | 41 |
|
41 | 42 |
|
| 43 | +class RegenerateDistributionElement(bpy.types.Operator, tool.Ifc.Operator): |
| 44 | + bl_idname = "bim.regenerate_distribution_element" |
| 45 | + bl_description = ( |
| 46 | + "Regenerates the positions and segment lengths of a distribution element and all connected elements." |
| 47 | + ) |
| 48 | + bl_label = "Regenerate Distribution Element" |
| 49 | + bl_options = {"REGISTER", "UNDO"} |
| 50 | + |
| 51 | + def _execute(self, context): |
| 52 | + current_element = tool.Ifc.get_entity(bpy.context.active_object) |
| 53 | + processed_elements = set() |
| 54 | + |
| 55 | + # The goal is to regenerate all recursively connected elements that |
| 56 | + # minimise movement as much as possible. |
| 57 | + |
| 58 | + # A queue is a list of branches. A branch is a list of elements in |
| 59 | + # sequence, each one connecting to another element. An element in a |
| 60 | + # branch may have a child queue. The queue and child queues are |
| 61 | + # acyclic. |
| 62 | + |
| 63 | + def extend_branch(element, branch, predecessor=None): |
| 64 | + processed_elements.add(element) |
| 65 | + branch_element = {"element": element, "children": [], "predecessor": predecessor} |
| 66 | + branch.append(branch_element) |
| 67 | + |
| 68 | + connected = {e for e in ifcopenshell.util.system.get_connected_to(element) if e not in processed_elements} |
| 69 | + connected.update( |
| 70 | + [e for e in ifcopenshell.util.system.get_connected_from(element) if e not in processed_elements] |
| 71 | + ) |
| 72 | + |
| 73 | + if len(connected) == 1: |
| 74 | + extend_branch(list(connected)[0], branch, element) |
| 75 | + else: |
| 76 | + for connected_element in connected: |
| 77 | + branch_element["children"].append(extend_branch(connected_element, [], element)) |
| 78 | + |
| 79 | + return branch |
| 80 | + |
| 81 | + queue = extend_branch(current_element, [])[0]["children"] |
| 82 | + |
| 83 | + # import pprint |
| 84 | + # pprint.pprint(queue) |
| 85 | + |
| 86 | + def process_branch(branch): |
| 87 | + for branch_element in branch: |
| 88 | + element = branch_element["element"] |
| 89 | + print('processing', element) |
| 90 | + predecessor = branch_element["predecessor"] |
| 91 | + if False: # If the element does not need to be transformed, return early. |
| 92 | + return |
| 93 | + # Perform the extend, translate, rotate, etc the element as necessary based on the predecessor. |
| 94 | + # For segments, prioritise extensions instead of translations. |
| 95 | + # For everything else, only translate. No rotation. |
| 96 | + for child_branch in branch_element["children"]: |
| 97 | + process_branch(child_branch) |
| 98 | + |
| 99 | + for branch in queue: |
| 100 | + process_branch(branch) |
| 101 | + |
| 102 | + |
42 | 103 | class FitFlowSegments(bpy.types.Operator, tool.Ifc.Operator): |
43 | 104 | bl_idname = "bim.fit_flow_segments" |
44 | 105 | bl_description = "Add a fitting based on currently selected elements and cursor" |
|
0 commit comments