From 47e259ff2f34892db950f746f07b7268dae72436 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 15 Jan 2026 16:54:37 +0500 Subject: [PATCH] ifcsverchok - operator to apply graph to Bonsai file --- src/bonsai/bonsai/bim/module/misc/__init__.py | 1 + src/bonsai/bonsai/bim/module/misc/operator.py | 21 ++++++++++++++++++ src/bonsai/bonsai/tool/model.py | 22 +++++++++++++++++++ src/ifcsverchok/__init__.py | 2 ++ src/ifcsverchok/ifcstore.py | 5 +++++ 5 files changed, 51 insertions(+) diff --git a/src/bonsai/bonsai/bim/module/misc/__init__.py b/src/bonsai/bonsai/bim/module/misc/__init__.py index 27ab626183..31926217fc 100644 --- a/src/bonsai/bonsai/bim/module/misc/__init__.py +++ b/src/bonsai/bonsai/bim/module/misc/__init__.py @@ -22,6 +22,7 @@ from . import ui, prop, operator classes = ( operator.DrawSystemArrows, operator.GetConnectedSystemElements, + operator.IfcSverchokUseBonsaiFile, operator.ResizeToStorey, operator.SetOverrideColour, operator.SnapSpacesTogether, diff --git a/src/bonsai/bonsai/bim/module/misc/operator.py b/src/bonsai/bonsai/bim/module/misc/operator.py index 15fca6b547..ee3c592f3e 100644 --- a/src/bonsai/bonsai/bim/module/misc/operator.py +++ b/src/bonsai/bonsai/bim/module/misc/operator.py @@ -348,3 +348,24 @@ class DrawSystemArrows(bpy.types.Operator, tool.Ifc.Operator): ) ) return matrix + + +class IfcSverchokUseBonsaiFile(bpy.types.Operator, tool.Ifc.Operator): + bl_idname = "bim.ifcsverchok_use_bonsai_file" + bl_label = "Use Bonsai IFC File" + bl_description = "Apply current IfcSverchok tree to the active Bonsai file." + bl_options = {"REGISTER", "UNDO"} + + def _execute(self, context): + import sverchok.node_tree + + ifc_file = tool.Ifc.get() + if ifc_file is None: + self.report({"ERROR"}, "No active IFC file.") + return {"CANCELLED"} + + space_data = context.space_data + assert isinstance(space_data, bpy.types.SpaceNodeEditor) + node_tree = space_data.node_tree + assert isinstance(node_tree, sverchok.node_tree.SverchCustomTree) + tool.Model.run_ifcsverchok_graph_on_bonsai_file(node_tree) diff --git a/src/bonsai/bonsai/tool/model.py b/src/bonsai/bonsai/tool/model.py index e7da203a85..d4c85e8d87 100644 --- a/src/bonsai/bonsai/tool/model.py +++ b/src/bonsai/bonsai/tool/model.py @@ -2669,3 +2669,25 @@ class Model(bonsai.core.tool.Model): material.OffsetFromReferenceLine = custom_offset cls.recreate_wall(element, wall) + + @classmethod + def run_ifcsverchok_graph_on_bonsai_file(cls, node_tree: sverchok.node_tree.SverchCustomTree) -> None: + from ifcsverchok.ifcstore import SvIfcStore + from sverchok.core.update_system import UpdateTree + + # We should be very careful and use bonsai file just for 1 graph update. + # To avoid producing duplicated data in non-ephemeral file. + SvIfcStore.use_bonsai_file = True + try: + # The ones below refresh asyncronously, so we're using different method to get results synchronously. + # - bpy.ops.node.sverchok_update_context(force_mode=True) + # - node_tree.force_update() + # TODO: Ideally we should find shape output node and update only it's furtherest children. + # Because user might have some nodes just floating around unused. + # ` update_tree = UpdateTree.get(node_tree); update_tree.add_outdated(nodes)` can be used for this. + UpdateTree.reset_tree(node_tree) + nodes_to_update = UpdateTree.main_update(node_tree) + # Consuming generator, which triggers the update. + list(nodes_to_update) + finally: + SvIfcStore.use_bonsai_file = False diff --git a/src/ifcsverchok/__init__.py b/src/ifcsverchok/__init__.py index 9745707e4d..2eb7366fc9 100644 --- a/src/ifcsverchok/__init__.py +++ b/src/ifcsverchok/__init__.py @@ -297,11 +297,13 @@ class IFC_PT_write_file_panel(bpy.types.Panel): def draw(self, context): ng = context.space_data.node_tree layout = self.layout + assert layout row = layout.split(factor=0.2, align=True) row = layout.row() row2 = layout.row() row.operator("ifc.sverchok_update_current", text="IFC Re-run all nodes") row2.operator("ifc.write_file_panel") + layout.operator("bim.ifcsverchok_use_bonsai_file") CLASSES = [IFC_Sv_UpdateCurrent, IFC_Sv_write_file, IFC_PT_write_file_panel] diff --git a/src/ifcsverchok/ifcstore.py b/src/ifcsverchok/ifcstore.py index 6fda9b2cea..990db56a0e 100644 --- a/src/ifcsverchok/ifcstore.py +++ b/src/ifcsverchok/ifcstore.py @@ -18,6 +18,7 @@ from typing import Any, Union +import bonsai.tool as tool import bpy import ifcopenshell import ifcopenshell.api @@ -50,6 +51,8 @@ class SvIfcStore: future = [] schema_identifiers = ["IFC4", "IFC2X3"] + use_bonsai_file = False + @staticmethod def purge() -> None: SvIfcStore.path = "" @@ -95,6 +98,8 @@ class SvIfcStore: @staticmethod def get_file() -> ifcopenshell.file: + if SvIfcStore.use_bonsai_file: + return tool.Ifc.get() if SvIfcStore.file is None: SvIfcStore.create_boilerplate() return SvIfcStore.file