From f9e766138cf003a6c5bcd08981f246bd5b4827c6 Mon Sep 17 00:00:00 2001 From: martinaCodes Date: Mon, 31 Oct 2022 18:50:17 +0100 Subject: [PATCH] new pick_ifc_class node + minor fixes. Add_spatial_element is WIP --- src/ifcsverchok/__init__.py | 1 + .../nodes/ifc/add_spatial_element.py | 47 ++++++-- src/ifcsverchok/nodes/ifc/by_type.py | 26 +++-- src/ifcsverchok/nodes/ifc/create_entity.py | 14 ++- src/ifcsverchok/nodes/ifc/pick_ifc_class.py | 110 ++++++++++++++++++ 5 files changed, 177 insertions(+), 21 deletions(-) create mode 100644 src/ifcsverchok/nodes/ifc/pick_ifc_class.py diff --git a/src/ifcsverchok/__init__.py b/src/ifcsverchok/__init__.py index 0c21d4f9fc..7a376eb856 100644 --- a/src/ifcsverchok/__init__.py +++ b/src/ifcsverchok/__init__.py @@ -53,6 +53,7 @@ def nodes_index(): ("ifc.create_entity", "SvIfcCreateEntity"), ("ifc.create_shape", "SvIfcCreateShape"), ("ifc.read_entity", "SvIfcReadEntity"), + ("ifc.pick_ifc_class", "SvIfcPickIfcClass"), ("ifc.by_id", "SvIfcById"), ("ifc.by_guid", "SvIfcByGuid"), ("ifc.by_type", "SvIfcByType"), diff --git a/src/ifcsverchok/nodes/ifc/add_spatial_element.py b/src/ifcsverchok/nodes/ifc/add_spatial_element.py index 24d1aa6f02..d474e9b309 100644 --- a/src/ifcsverchok/nodes/ifc/add_spatial_element.py +++ b/src/ifcsverchok/nodes/ifc/add_spatial_element.py @@ -16,26 +16,33 @@ from sverchok.data_structure import updateNode, flatten_data, repeat_last_for_le class SvIfcAddSpatialElement(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfcCore): bl_idname = "SvIfcAddSpatialElement" bl_label = "IFC Add Spatial Element" + node_dict = {} + + n_id: StringProperty() Name: StringProperty(name="Name(s)", update=updateNode) IfcClass: StringProperty(name="IFC Class", update=updateNode, default="IfcSpace") Elements: StringProperty(name="Elements", update=updateNode) len_elements = 0 def sv_init(self, context): - self.inputs.new("SvStringsSocket", "Name(s)").prop_name = "Name" + self.inputs.new("SvStringsSocket", "Names").prop_name = "Name" self.inputs.new("SvStringsSocket", "IfcClass").prop_name = "IfcClass" self.inputs.new("SvStringsSocket", "Elements").prop_name = "Elements" self.outputs.new("SvStringsSocket", "Entities") - self.outputs.new("SvStringsSocket", "file") def process(self): + print("#"*20, "\n running add_spatial_element PROCESS()... \n", "#"*20,) + print("#"*20, "\n hash(self):", hash(self), "\n", "#"*20,) if not any(socket.is_linked for socket in self.outputs): return - self.names = flatten_data(self.inputs["Name(s)"].sv_get(), target_level=1) - self.ifc_class = self.inputs["IfcClass"].sv_get()[0][0] + self.names = flatten_data(self.inputs["Names"].sv_get(), target_level=1) + self.ifc_class = flatten_data(self.inputs["IfcClass"].sv_get(), target_level=1)[0] self.elements = ensure_min_nesting(self.inputs["Elements"].sv_get(), 2) self.elements = flatten_data(self.elements, target_level=2) + if not self.elements[0][0]: + raise Exception('Mandatory input "Element(s)" is missing.') + return print("elements: ", self.elements) self.file = SvIfcStore.get_file() self.elements = [[self.file.by_id(step_id) for step_id in element] for element in self.elements] @@ -44,16 +51,35 @@ class SvIfcAddSpatialElement(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.h print("elements: ", self.elements) self.names = self.repeat_input_unique(self.names, len(self.elements)) print("Names: ",self.names) + print("IfcClass: ",self.ifc_class) + + self.sv_input_names = [i.name for i in self.inputs] + if hash(self) not in self.node_dict: + self.node_dict[hash(self)] = {} #happens if node is already on canvas when blender loads + if not self.node_dict[hash(self)]: + self.node_dict[hash(self)].update(dict.fromkeys(self.sv_input_names, 0)) + + print("node_dict: ", self.node_dict) + edit = False + for i in range(len(self.inputs)): + input = self.inputs[i].sv_get(deepcopy=False) + if isinstance(self.node_dict[hash(self)][self.inputs[i].name], list) and input != self.node_dict[hash(self)][self.inputs[i].name]: + edit = True + print("Edit = True") + self.node_dict[hash(self)][self.inputs[i].name] = input + if (self.node_id not in SvIfcStore.id_map) or (len(self.elements) != self.len_elements): print("\nRunning create") elements = self.create() self.len_elements = len(self.elements) else: - elements = self.edit() + if edit is True: + elements = self.edit() + else: + elements = SvIfcStore.id_map[self.node_id] print("\n ##### results: ", elements) self.outputs["Entities"].sv_set(elements) - self.outputs["file"].sv_set([self.file]) def create(self): results = [] @@ -74,10 +100,14 @@ class SvIfcAddSpatialElement(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.h def edit(self): results = [] results_ids = SvIfcStore.id_map[self.node_id] + print("#"*20, "\n running add_spatial_element edit()... \n", "#"*20,) + print("#"*20, "\n results_ids:", results_ids, "\n", "#"*20,) for result_id in results_ids: result = self.file.by_id(result_id) + print("#"*20, "\n result", result, "\n", "#"*20,) subelements = set(ifcopenshell.util.element.get_decomposition(result)) for element in self.elements: + print("\n\n\n","#"*50) print("\nElement: ", element) element_set = set(element) print("\nRESULT: ", result) @@ -85,7 +115,10 @@ class SvIfcAddSpatialElement(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.h print("elements_set: ", element_set) for removed_element in subelements - element_set: # Just realised I don't have a spatial.unassign_container, but if so we'd do it here - pass + if removed_element.is_a("IfcSpatialElement") or removed_element.is_a("IfcSpatialStructureElement"): + ifcopenshell.api.run("aggregate.unassign_object", self.file, product=removed_element, relating_object=result) + else: + pass for added_element in element_set - subelements: if added_element.is_a("IfcSpatialElement") or added_element.is_a("IfcSpatialStructureElement"): ifcopenshell.api.run("aggregate.assign_object", self.file, product=added_element, relating_object=result) diff --git a/src/ifcsverchok/nodes/ifc/by_type.py b/src/ifcsverchok/nodes/ifc/by_type.py index 901f275e9f..4498ae767d 100644 --- a/src/ifcsverchok/nodes/ifc/by_type.py +++ b/src/ifcsverchok/nodes/ifc/by_type.py @@ -23,7 +23,7 @@ import ifcsverchok.helper from bpy.props import StringProperty, EnumProperty from ifcsverchok.ifcstore import SvIfcStore from sverchok.node_tree import SverchCustomTreeNode -from sverchok.data_structure import updateNode +from sverchok.data_structure import updateNode, flatten_data def get_ifc_products(self, context): @@ -103,17 +103,27 @@ class SvIfcByType(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfc if self.file: self.ifc_products = get_ifc_products(self, bpy.context) self.ifc_classes = get_ifc_classes(self, bpy.context) - self.sv_input_names = ["ifc_product", "ifc_class", "custom_ifc_class"] - super().process() + # self.sv_input_names = ["ifc_product", "ifc_class", "custom_ifc_class"] + self.ifc_product = flatten_data(self.inputs["ifc_product"].sv_get(), target_level=1)[0] + self.ifc_class = flatten_data(self.inputs["ifc_class"].sv_get(), target_level=1)[0] + self.custom_ifc_class = flatten_data(self.inputs["custom_ifc_class"].sv_get(), target_level=1)[0] - def process_ifc(self, ifc_product, ifc_class, custom_ifc_class): - if custom_ifc_class: - self.outputs["Entity"].sv_set([self.file.by_type(custom_ifc_class)]) - elif ifc_class: - self.outputs["Entity"].sv_set([self.file.by_type(ifc_class)]) + # super().process() + if self.custom_ifc_class: + self.outputs["Entity"].sv_set([self.file.by_type(self.custom_ifc_class)]) + elif self.ifc_class: + self.outputs["Entity"].sv_set([self.file.by_type(self.ifc_class)]) else: self.outputs["Entity"].sv_set([]) + # def process_ifc(self, ifc_product, ifc_class, custom_ifc_class): + # if custom_ifc_class: + # self.outputs["Entity"].sv_set([self.file.by_type(custom_ifc_class)]) + # elif ifc_class: + # self.outputs["Entity"].sv_set([self.file.by_type(ifc_class)]) + # else: + # self.outputs["Entity"].sv_set([]) + def register(): bpy.utils.register_class(SvIfcByType) diff --git a/src/ifcsverchok/nodes/ifc/create_entity.py b/src/ifcsverchok/nodes/ifc/create_entity.py index da060ee868..4328c57dd8 100644 --- a/src/ifcsverchok/nodes/ifc/create_entity.py +++ b/src/ifcsverchok/nodes/ifc/create_entity.py @@ -1,5 +1,5 @@ import bpy -from mathutils import Matrix +from mathutils import Matrix, Vector import ifcopenshell import ifcsverchok.helper from ifcsverchok.ifcstore import SvIfcStore @@ -64,7 +64,7 @@ class SvIfcCreateEntity(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper 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] + self.ifc_class = flatten_data(self.inputs["IfcClass"].sv_get(), target_level=1)[0] self.representations = flatten_data(self.inputs["Representations"].sv_get(), target_level=1) self.locations = flatten_data(self.inputs["Locations"].sv_get(default=[]), target_level=1) self.properties = self.inputs["Properties"].sv_get() @@ -78,7 +78,7 @@ class SvIfcCreateEntity(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper self.node_dict[hash(self)].update(dict.fromkeys(self.sv_input_names, 0)) print("node_dict: ", self.node_dict) if not self.inputs["IfcClass"].sv_get()[0][0]: - raise Exception("Insert IfcClass") + raise Exception('Mandatory input "IfcClass" is missing.') return edit = False @@ -109,7 +109,7 @@ class SvIfcCreateEntity(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper self.descriptions = self.repeat_input_unique(self.descriptions, len(self.names)) # print("REPRESENTATION: ", self.representations) - # print("IfcClass: ",self.ifc_class) + print("IfcClass: ", self.ifc_class) print("Names: ",self.names) print("Descriptions: ",self.descriptions) # print("\nrepresentations after convert: ", self.representations) @@ -144,6 +144,7 @@ class SvIfcCreateEntity(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper except IndexError: pass try: + print("IS INSTANCE MATRIX: ", isinstance(self.locations[i], Matrix), isinstance(self.locations[i], Vector)) 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]) @@ -177,8 +178,9 @@ class SvIfcCreateEntity(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper 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]) + ifcopenshell.api.run("geometry.edit_object_placement", self.file, product=entity, matrix=self.locations[i]) except IndexError: pass if entity.is_a() != self.ifc_class: diff --git a/src/ifcsverchok/nodes/ifc/pick_ifc_class.py b/src/ifcsverchok/nodes/ifc/pick_ifc_class.py new file mode 100644 index 0000000000..6b4fd5bd4b --- /dev/null +++ b/src/ifcsverchok/nodes/ifc/pick_ifc_class.py @@ -0,0 +1,110 @@ + +# IfcSverchok - IFC Sverchok extension +# Copyright (C) 2020, 2021 Dion Moult +# +# This file is part of IfcSverchok. +# +# IfcSverchok is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# IfcSverchok is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with IfcSverchok. If not, see . + +import bpy +import ifcopenshell +import ifcsverchok.helper +from bpy.props import StringProperty, EnumProperty +from ifcsverchok.ifcstore import SvIfcStore +from sverchok.node_tree import SverchCustomTreeNode +from sverchok.data_structure import updateNode + + +def get_ifc_products(self, context): + ifc_products = getattr(self, "ifc_products", []) + file = SvIfcStore.get_file() + if not file: + return [] + if ifc_products and file: + return ifc_products + ifc_products = [] + ifc_products.extend( + [ + (e, e, "") + for e in [ + "IfcElement", + "IfcElementType", + "IfcSpatialElement", + "IfcGroup", + "IfcStructuralItem", + "IfcContext", + "IfcAnnotation", + ] + ] + ) + if file.schema == "IFC2X3": + ifc_products[2] = ("IfcSpatialStructureElement", "IfcSpatialStructureElement", "") + return ifc_products + + +def update_ifc_products(self, context): + if hasattr(self, "ifc_classes"): + self.ifc_classes.clear() + + +def get_ifc_classes(self, context): + ifc_classes = getattr(self, "ifc_classes", []) + if ifc_classes: + return self.ifc_classes + file = SvIfcStore.get_file() + if not file: + return [] + schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(file.schema) + declaration = schema.declaration_by_name(self.ifc_product) + + def get_classes(declaration): + results = [] + if not declaration.is_abstract(): + results.append(declaration.name()) + for subtype in declaration.subtypes(): + results.extend(get_classes(subtype)) + return results + + classes = get_classes(declaration) + ifc_classes.extend([(c, c, "") for c in sorted(classes)]) + return ifc_classes + + +class SvIfcPickIfcClass(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfcCore): + bl_idname = "SvIfcPickIfcClass" + bl_label = "IFC Class Picker" + ifc_product: EnumProperty(items=get_ifc_products, name="IfcProduct", description="Pick an IfcProduct from drop-down.", update=update_ifc_products) + ifc_class: EnumProperty(items=get_ifc_classes, name="IfcClass", description="Pick an IfcClass from drop-down.", update=updateNode) + + def sv_init(self, context): + self.inputs.new("SvStringsSocket", "ifc_product").prop_name = "ifc_product" + self.inputs.new("SvStringsSocket", "ifc_class").prop_name = "ifc_class" + self.outputs.new("SvStringsSocket", "IfcClass") + self.width = 200 + + def draw_buttons(self, context, layout): + layout.operator("node.sv_ifc_tooltip", text="", icon="QUESTION", emboss=False).tooltip = "Ifc Class Picker" + + def process(self): + # ifc_product = self.inputs["ifc_product"].sv_get() + ifc_class = self.inputs["ifc_class"].sv_get()[0][0] + self.outputs["IfcClass"].sv_set([ifc_class]) + + +def register(): + bpy.utils.register_class(SvIfcPickIfcClass) + + +def unregister(): + bpy.utils.unregister_class(SvIfcPickIfcClass)