2021-08-17 08:39:38 +10:00
# IfcSverchok - IFC Sverchok extension
# Copyright (C) 2020, 2021 Dion Moult <dion@thinkmoult.com>
#
# 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 <http://www.gnu.org/licenses/>.
2020-10-10 10:21:17 +11:00
import bpy
import logging
import ifcopenshell
import ifcsverchok . helper
2022-10-23 20:25:36 +02:00
from ifcsverchok . ifcstore import SvIfcStore
2020-10-10 10:21:17 +11:00
import blenderbim . bim . import_ifc
2022-10-23 20:25:36 +02:00
from bpy . props import StringProperty , PointerProperty , BoolProperty
2020-10-10 10:21:17 +11:00
from sverchok . node_tree import SverchCustomTreeNode
2022-10-23 20:25:36 +02:00
from sverchok . data_structure import updateNode , flatten_data
2020-10-10 10:21:17 +11:00
2022-08-25 22:01:35 +02:00
2022-10-23 20:25:36 +02:00
# class SvIfcCreateShapeRefresh(bpy.types.Operator):
# bl_idname = "node.sv_ifc_create_shape_refresh"
# bl_label = "IFC Create Shape Refresh"
# bl_options = {"UNDO"}
2020-10-10 10:21:17 +11:00
2022-10-23 20:25:36 +02:00
# tree_name: StringProperty(default="")
# node_name: StringProperty(default="")
2020-10-10 10:21:17 +11:00
2022-10-23 20:25:36 +02:00
# def execute(self, context):
# node = bpy.data.node_groups[self.tree_name].nodes[self.node_name]
# node.process()
# return {"FINISHED"}
2020-10-10 10:21:17 +11:00
class SvIfcCreateShape ( bpy . types . Node , SverchCustomTreeNode , ifcsverchok . helper . SvIfcCore ) :
2022-10-23 20:25:36 +02:00
"""
Triggers: Ifc create shape by entity
Tooltip: Create Blender shape by Ifc Entity
"""
is_scene_dependent = True
is_interactive = False
node_dict = { }
def refresh_node ( self , context ) :
if self . refresh_local :
self . process ( )
self . refresh_local = False
2022-10-23 21:51:26 +02:00
refresh_local : BoolProperty ( name = " Create shape(s) " , description = " Update Node " , update = refresh_node )
2022-10-23 20:25:36 +02:00
2020-10-10 10:21:17 +11:00
bl_idname = " SvIfcCreateShape "
2022-10-23 20:25:36 +02:00
bl_label = " IFC Create Blender Shape "
entity : StringProperty ( name = " Entities " , update = updateNode )
2020-10-10 10:21:17 +11:00
def sv_init ( self , context ) :
2022-10-23 20:25:36 +02:00
self . inputs . new ( " SvStringsSocket " , " Entities " ) . prop_name = " entity "
self . outputs . new ( ' SvStringsSocket ' , " Object(s) " )
2020-10-10 10:21:17 +11:00
def draw_buttons ( self , context , layout ) :
2022-10-23 20:25:36 +02:00
row = layout . row ( align = True )
row . operator ( " node.sv_ifc_tooltip " , text = " " , icon = " QUESTION " , emboss = False ) . tooltip = " Create Blender shape from Ifc Entity. Takes one or multiple Ifc Entities. "
row . prop ( self , ' refresh_local ' , icon = ' FILE_REFRESH ' )
2020-10-10 10:21:17 +11:00
def process ( self ) :
2022-10-23 20:25:36 +02:00
self . entities = flatten_data ( self . inputs [ " Entities " ] . sv_get ( ) , target_level = 1 )
if not self . entities [ 0 ] :
return
if self . refresh_local or hash ( self ) not in self . node_dict :
self . file = SvIfcStore . get_file ( )
blender_objects = self . create ( )
self . node_dict [ hash ( self ) ] = blender_objects
else :
blender_objects = self . node_dict [ hash ( self ) ]
self . outputs [ " Object(s) " ] . sv_set ( blender_objects )
def create ( self ) :
blender_objects = [ ]
for entity in self . entities :
try :
if not entity . is_a ( " IfcProduct " ) :
return
logger = logging . getLogger ( " ImportIFC " )
self . ifc_import_settings = blenderbim . bim . import_ifc . IfcImportSettings . factory ( bpy . context , " " , logger )
settings = ifcopenshell . geom . settings ( )
shape = ifcopenshell . geom . create_shape ( settings , entity )
ifc_importer = blenderbim . bim . import_ifc . IfcImporter ( self . ifc_import_settings )
ifc_importer . file = self . file
mesh = ifc_importer . create_mesh ( entity , shape )
obj = bpy . data . objects . new ( " IFC Element " , mesh )
blender_objects . append ( obj )
bpy . context . scene . collection . objects . link ( obj )
except :
raise Exception ( " Entity could not be converted into a shape. Entity: {} " . format ( entity ) )
return blender_objects
2020-10-10 10:21:17 +11:00
def register ( ) :
2022-10-23 20:25:36 +02:00
# bpy.utils.register_class(SvIfcCreateShapeRefresh)
2020-10-10 10:21:17 +11:00
bpy . utils . register_class ( SvIfcCreateShape )
2020-11-01 19:24:01 +07:00
2020-10-10 10:21:17 +11:00
def unregister ( ) :
bpy . utils . unregister_class ( SvIfcCreateShape )
2022-10-23 20:25:36 +02:00
# bpy.utils.unregister_class(SvIfcCreateShapeRefresh)