From 36899d362e948c3964ed69d8e939b7c429c60c46 Mon Sep 17 00:00:00 2001 From: martinaCodes Date: Mon, 31 Oct 2022 00:25:45 +0100 Subject: [PATCH] added blender panel for saving file + minor fix in create_entity --- src/ifcsverchok/__init__.py | 64 +++++++++++++++++++++- src/ifcsverchok/nodes/ifc/create_entity.py | 14 +++-- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/ifcsverchok/__init__.py b/src/ifcsverchok/__init__.py index 29c84a109f..0c21d4f9fc 100644 --- a/src/ifcsverchok/__init__.py +++ b/src/ifcsverchok/__init__.py @@ -42,7 +42,6 @@ from sverchok.utils.extra_categories import ( from sverchok.ui.nodeview_space_menu import make_extra_category_menus from sverchok.utils.logging import info, debug - def nodes_index(): return [ ( @@ -94,7 +93,65 @@ imported_modules = make_node_list() reload_event = False import bpy +import os +from os.path import abspath, splitext +import ifcopenshell +from ifcsverchok.ifcstore import SvIfcStore +from sverchok.data_structure import flatten_data +class IFC_Sv_write_file(bpy.types.Operator): + bl_idname = "ifc.write_file_panel" + bl_label = "Write File" + bl_options = {"REGISTER", "UNDO"} + bl_description = "File path to write to." + filepath: bpy.props.StringProperty(subtype="FILE_PATH") + + @classmethod + def poll(cls, context): + return any("IFC" in n for n in context.space_data.edit_tree.nodes.keys()) + + def execute(self, context): + self.file = SvIfcStore.file + if not self.file: + raise Exception("No IFC file in SvIfcStore.") + _, ext = splitext(self.filepath) + if not ext: + raise Exception("Bad path. Provide a path to a file.") + else: + if not (self.file.by_type("IfcSpatialElement") or self.file.by_type("IfcSpatialStructureElement")): + print("No Ifc Spatial Element found. Adding all elements to IfcBuilding.") + elements = self.file.by_type("IfcElement") + building = ifcopenshell.api.run("root.create_entity", self.file, name="DefaultBuilding", ifc_class="IfcBuilding") + for element in elements: + ifcopenshell.api.run("spatial.assign_container", self.file, product=element, relating_structure=building) + self.file.write(self.filepath) + print(f"File written to: {self.filepath}") + return {"FINISHED"} + + def invoke(self, context, event): + context.window_manager.fileselect_add(self) + return {"RUNNING_MODAL"} + +class IFC_PT_write_file_panel(bpy.types.Panel): + bl_idname = "IFC_PT_write_file_panel" + bl_label = "Write IFC to file" + bl_options = {"DEFAULT_CLOSED"} + bl_space_type = "NODE_EDITOR" + bl_region_type = "UI" + bl_category = "IfcSverchok" + + @classmethod + def poll(cls, context): + if context.space_data.edit_tree and any("IFC" in n for n in context.space_data.edit_tree.nodes.keys()): + return True + + def draw(self, context): + layout = self.layout + row = layout.split(factor=0.2, align=True) + row = layout.row() + row.operator("ifc.write_file_panel") + +CLASSES = [IFC_Sv_write_file, IFC_PT_write_file_panel] def register_nodes(): node_modules = make_node_list() @@ -148,7 +205,8 @@ def register(): global our_menu_classes debug("Registering ifcsverchok") - + for klass in CLASSES: + bpy.utils.register_class(klass) register_nodes() extra_nodes = importlib.import_module(".nodes", "ifcsverchok") auto_gather_node_classes(extra_nodes) @@ -173,3 +231,5 @@ def unregister(): print(e) unregister_extra_category_provider("IFCSVERCHOK") unregister_nodes() + for klass in CLASSES: + bpy.utils.unregister_class(klass) diff --git a/src/ifcsverchok/nodes/ifc/create_entity.py b/src/ifcsverchok/nodes/ifc/create_entity.py index 4ae62b1394..da060ee868 100644 --- a/src/ifcsverchok/nodes/ifc/create_entity.py +++ b/src/ifcsverchok/nodes/ifc/create_entity.py @@ -1,6 +1,5 @@ import bpy - -# from helper import SayHello +from mathutils import Matrix import ifcopenshell import ifcsverchok.helper from ifcsverchok.ifcstore import SvIfcStore @@ -62,7 +61,7 @@ class SvIfcCreateEntity(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper def process(self): print("#"*20, "\n running create_entity3 PROCESS()... \n", "#"*20,) print("#"*20, "\n hash(self):", hash(self), "\n", "#"*20,) - + self.names = flatten_data(self.inputs["Names"].sv_get(), target_level=1) self.descriptions = flatten_data(self.inputs["Descriptions"].sv_get(), target_level=1) self.ifc_class = self.inputs["IfcClass"].sv_get()[0][0] @@ -139,12 +138,15 @@ class SvIfcCreateEntity(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper try: entity = ifcopenshell.api.run("root.create_entity", self.file, ifc_class=self.ifc_class, name=self.names[i], description=self.descriptions[i]) try: - ifcopenshell.api.run("geometry.assign_representation", self.file, product=entity, representation=self.representations[i]) + print("representations[i]: ", self.representations[i]) + if self.representations[i]: + ifcopenshell.api.run("geometry.assign_representation", self.file, product=entity, representation=self.representations[i]) except IndexError: pass try: - print("LOCATION[i]: ", self.locations[i]) - ifcopenshell.api.run("geometry.edit_object_placement", self.file, product=entity, matrix=self.locations[i]) + if isinstance(self.locations[i], Matrix): + print("LOCATION[i]: ", self.locations[i], type(self.locations[i])) + ifcopenshell.api.run("geometry.edit_object_placement", self.file, product=entity, matrix=self.locations[i]) except IndexError: pass entities_ids.append(entity.id())