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/>.
2022-10-23 21:51:26 +02:00
from os . path import abspath , splitext
2020-09-22 21:22:10 +10:00
import bpy
import ifcopenshell
import ifcsverchok . helper
2022-10-23 21:51:26 +02:00
from ifcsverchok . ifcstore import SvIfcStore
from bpy . props import StringProperty , BoolProperty
2020-09-22 21:22:10 +10:00
from sverchok . node_tree import SverchCustomTreeNode
2022-10-23 21:51:26 +02:00
from sverchok . data_structure import updateNode , flatten_data
2020-09-22 21:22:10 +10:00
2020-10-01 14:47:42 +10:00
class SvIfcWriteFile ( bpy . types . Node , SverchCustomTreeNode , ifcsverchok . helper . SvIfcCore ) :
2022-10-23 21:51:26 +02:00
"""
Triggers: Ifc write to file
Tooltip: Write active Sverchok Ifc file to path
"""
def refresh_node_local ( self , context ) :
if self . refresh_local :
self . process ( )
self . refresh_local = False
# out = ""
refresh_local : BoolProperty ( name = " Write " , description = " Write to file " , update = refresh_node_local )
2020-10-01 14:47:42 +10:00
bl_idname = " SvIfcWriteFile "
bl_label = " IFC Write File "
2022-10-23 21:51:26 +02:00
path : StringProperty ( name = " path " , description = " File path to write to. Can be relative. " , update = updateNode )
2020-09-22 21:22:10 +10:00
def sv_init ( self , context ) :
self . inputs . new ( " SvStringsSocket " , " path " ) . prop_name = " path "
2022-10-23 21:51:26 +02:00
self . outputs . new ( " SvStringsSocket " , " output " )
def draw_buttons ( self , context , layout ) :
row = layout . row ( align = True )
2022-10-24 00:08:59 +02:00
row . operator ( " node.sv_ifc_tooltip " , text = " " , icon = " QUESTION " , emboss = False ) . tooltip = " Writes active Ifc file to path. \n It will overwrite an existing file. "
2022-10-23 21:51:26 +02:00
row . prop ( self , ' refresh_local ' , icon = ' FILE_REFRESH ' )
2020-09-22 21:22:10 +10:00
def process ( self ) :
2022-10-23 21:51:26 +02:00
path = flatten_data ( self . inputs [ " path " ] . sv_get ( ) , target_level = 1 ) [ 0 ]
if not path :
return
path = abspath ( path )
file = SvIfcStore . get_file ( )
_ , ext = splitext ( path )
if not ext :
raise Exception ( " Bad path. Provide a path to a file. " )
if self . refresh_local and ext :
file . write ( path )
self . outputs [ " output " ] . sv_set ( f " File written successfully to: { path } . " )
2020-09-22 21:22:10 +10:00
2020-11-01 19:24:01 +07:00
2020-09-22 21:22:10 +10:00
def register ( ) :
2020-10-01 14:47:42 +10:00
bpy . utils . register_class ( SvIfcWriteFile )
2020-09-22 21:22:10 +10:00
2020-11-01 19:24:01 +07:00
2020-09-22 21:22:10 +10:00
def unregister ( ) :
2020-10-01 14:47:42 +10:00
bpy . utils . unregister_class ( SvIfcWriteFile )