-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ifc_import.py
218 lines (187 loc) · 7.79 KB
/
ifc_import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# ***************************************************************************
# * *
# * Copyright (c) 2022 Yorik van Havre <[email protected]> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License (GPL) *
# * as published by the Free Software Foundation; either version 3 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program 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 Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
import importlib
import os
import time
import FreeCAD
import ifc_tools
import ifc_psets
import ifc_materials
import ifc_layers
import ifc_status
if FreeCAD.GuiUp:
import FreeCADGui
params = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/NativeIFC")
def open(filename):
"""Opens an IFC file"""
from PySide import QtCore # lazy loading
name = os.path.splitext(os.path.basename(filename))[0]
FreeCAD.IsOpeningIFC = True
doc = FreeCAD.newDocument()
doc.Label = name
FreeCAD.setActiveDocument(doc.Name)
insert(filename, doc.Name, singledoc=True)
del FreeCAD.IsOpeningIFC
QtCore.QTimer.singleShot(100, unset_modified)
return doc
def insert(
filename,
docname,
strategy=None,
shapemode=None,
switchwb=None,
silent=False,
singledoc=None,
):
"""Inserts an IFC document in a FreeCAD document"""
from PySide import QtCore # lazy loading
strategy, shapemode, switchwb = get_options(strategy, shapemode, switchwb, silent)
if strategy is None:
print("Aborted.")
return
stime = time.time()
try:
document = FreeCAD.getDocument(docname)
except:
document = FreeCAD.newDocument()
if singledoc is None:
singledoc = params.GetBool("SingleDoc", False)
if singledoc:
prj_obj = ifc_tools.convert_document(document, filename, shapemode, strategy)
QtCore.QTimer.singleShot(100, toggle_lock_on)
else:
prj_obj = ifc_tools.create_document_object(
document, filename, shapemode, strategy
)
QtCore.QTimer.singleShot(100, toggle_lock_off)
if params.GetBool("LoadOrphans", True):
ifc_tools.load_orphans(prj_obj)
if not silent and params.GetBool("LoadMaterials", False):
ifc_materials.load_materials(prj_obj)
if params.GetBool("LoadLayers", False):
ifc_layers.load_layers(prj_obj)
if params.GetBool("LoadPsets", False):
ifc_psets.load_psets(prj_obj)
document.recompute()
# print a reference to the IFC file on the console
if FreeCAD.GuiUp and params.GetBool("IfcFileToConsole", False):
if isinstance(prj_obj, FreeCAD.DocumentObject):
pstr = "FreeCAD.getDocument('{}').{}.Proxy.ifcfile"
pstr = pstr.format(prj_obj.Document.Name, prj_obj.Name)
else:
pstr = "FreeCAD.getDocument('{}').Proxy.ifcfile"
pstr = pstr.format(prj_obj.Name)
pstr = "ifcfile = " + pstr
pstr += " # warning: make sure you know what you are doing when using this!"
FreeCADGui.doCommand(pstr)
endtime = "%02d:%02d" % (divmod(round(time.time() - stime, 1), 60))
fsize = round(os.path.getsize(filename) / 1048576, 2)
print("Imported", os.path.basename(filename), "(", fsize, "Mb ) in", endtime)
if FreeCAD.GuiUp and switchwb:
from StartPage import StartPage
StartPage.postStart()
return document
def get_options(strategy=None, shapemode=None, switchwb=None, silent=False):
"""Shows a dialog to get import options
shapemode: 0 = full shape
1 = coin only
2 = no representation
strategy: 0 = only root object
1 = only bbuilding structure,
2 = all children
"""
psets = params.GetBool("LoadPsets", False)
materials = params.GetBool("LoadMaterials", False)
layers = params.GetBool("LoadLayers", False)
singledoc = params.GetBool("SingleDoc", False)
if strategy is None:
strategy = params.GetInt("ImportStrategy", 0)
if shapemode is None:
shapemode = params.GetInt("ShapeMode", 0)
if switchwb is None:
switchwb = params.GetBool("SwitchWB", True)
if silent:
return strategy, shapemode, switchwb
ask = params.GetBool("AskAgain", False)
if ask and FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtGui
dlg = FreeCADGui.PySideUic.loadUi(
os.path.join(os.path.dirname(__file__), "ui", "dialogImport.ui")
)
dlg.checkSwitchWB.hide() # TODO see what to do with this...
dlg.comboStrategy.setCurrentIndex(strategy)
dlg.comboShapeMode.setCurrentIndex(shapemode)
dlg.checkSwitchWB.setChecked(switchwb)
dlg.checkAskAgain.setChecked(ask)
dlg.checkLoadPsets.setChecked(psets)
dlg.checkLoadMaterials.setChecked(materials)
dlg.checkLoadLayers.setChecked(layers)
dlg.comboSingleDoc.setCurrentIndex(1 - int(singledoc))
result = dlg.exec_()
if not result:
return None, None, None
strategy = dlg.comboStrategy.currentIndex()
shapemode = dlg.comboShapeMode.currentIndex()
switchwb = dlg.checkSwitchWB.isChecked()
ask = dlg.checkAskAgain.isChecked()
psets = dlg.checkLoadPsets.isChecked()
materials = dlg.checkLoadMaterials.isChecked()
layers = dlg.checkLoadLayers.isChecked()
singledoc = dlg.comboSingleDoc.currentIndex()
params.SetInt("ImportStrategy", strategy)
params.SetInt("ShapeMode", shapemode)
params.SetBool("SwitchWB", switchwb)
params.SetBool("AskAgain", ask)
params.SetBool("LoadPsets", psets)
params.SetBool("LoadMaterials", materials)
params.SetBool("LoadLayers", layers)
params.SetBool("SingleDoc", bool(1 - singledoc))
return strategy, shapemode, switchwb
def get_project_type(silent=False):
"""Gets the type of project to make"""
ask = params.GetBool("ProjectAskAgain", True)
ptype = params.GetBool("ProjectFull", False)
if silent:
return ptype
if ask and FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtGui
dlg = FreeCADGui.PySideUic.loadUi(
os.path.join(os.path.dirname(__file__), "ui", "dialogCreateProject.ui")
)
result = dlg.exec_()
ask = not (dlg.checkBox.isChecked())
ptype = bool(result)
params.SetBool("ProjectAskAgain", ask)
params.SetBool("ProjectFull", ptype)
return ptype
# convenience functions
def toggle_lock_on():
ifc_status.toggle_lock(True)
def toggle_lock_off():
ifc_status.toggle_lock(False)
def unset_modified():
try:
FreeCADGui.ActiveDocument.Modified = False
except:
pass