-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ifc_commands.py
258 lines (212 loc) · 8.62 KB
/
ifc_commands.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# ***************************************************************************
# * *
# * Copyright (c) 2023 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 *
# * *
# ***************************************************************************
"""This module contains IFC-related FreeCAD commands"""
import os
import FreeCAD
import FreeCADGui
translate = FreeCAD.Qt.translate
QT_TRANSLATE_NOOP = FreeCAD.Qt.QT_TRANSLATE_NOOP
def get_project():
"""Gets the current project"""
import ifc_tools
if FreeCADGui.Selection.getSelection():
return ifc_tools.get_project(FreeCADGui.Selection.getSelection()[0])
else:
return ifc_tools.get_project(FreeCAD.ActiveDocument)
class IFC_Diff:
"""Shows a diff of the changes in the current IFC document"""
def GetResources(self):
tt = QT_TRANSLATE_NOOP(
"IFC_Diff", "Shows the current unsaved changes in the IFC file"
)
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "IFC.svg"),
"MenuText": QT_TRANSLATE_NOOP("IFC_Diff", "IFC Diff..."),
"ToolTip": tt,
"Accel": "I, D",
}
def Activated(self):
import ifc_diff
proj = get_project()
if proj:
diff = ifc_diff.get_diff(proj)
ifc_diff.show_diff(diff)
class IFC_Expand:
"""Expands the children of the selected objects or document"""
def GetResources(self):
tt = QT_TRANSLATE_NOOP(
"IFC_Expand", "Expands the children of the selected objects or document"
)
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "IFC.svg"),
"MenuText": QT_TRANSLATE_NOOP("IFC_Expand", "IFC Expand"),
"ToolTip": tt,
"Accel": "I, E",
}
def Activated(self):
ns = []
for obj in FreeCADGui.Selection.getSelection():
if hasattr(obj.ViewObject, "Proxy"):
if hasattr(obj.ViewObject.Proxy, "hasChildren"):
if obj.ViewObject.Proxy.hasChildren(obj):
no = obj.ViewObject.Proxy.expandChildren(obj)
ns.extend(no)
else:
import ifc_generator
import ifc_tools
document = FreeCAD.ActiveDocument
ifc_generator.delete_ghost(document)
ifcfile = ifc_tools.get_ifcfile(document)
if ifcfile:
ns = ifc_tools.create_children(
document, ifcfile, recursive=True, only_structure=True
)
if ns:
document.recompute()
FreeCADGui.Selection.clearSelection()
for o in ns:
FreeCADGui.Selection.addSelection(o)
class IFC_ConvertDocument:
"""Converts the active document to an IFC document"""
def GetResources(self):
tt = QT_TRANSLATE_NOOP(
"IFC_ConvertDocument", "Converts the active document to an IFC document"
)
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "IFC.svg"),
"MenuText": QT_TRANSLATE_NOOP("IFC_ConvertDocument", "Convert document"),
"ToolTip": tt,
# "Accel": "I, C",
}
def Activated(self):
doc = FreeCAD.ActiveDocument
if (
hasattr(doc, "Proxy")
and hasattr(doc.Proxy, "ifcfile")
and doc.Proxy.ifcfile
):
FreeCAD.Console.PrintError(
translate("BIM", "The active document is already an IFC document")
)
else:
import ifc_tools
ifc_tools.convert_document(doc)
class IFC_MakeProject:
"""Converts the current selection to an IFC project"""
def GetResources(self):
tt = QT_TRANSLATE_NOOP(
"IFC_MakeProject", "Converts the current selection to an IFC project"
)
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "IFC.svg"),
"MenuText": QT_TRANSLATE_NOOP("IFC_MakeProject", "Make IFC project"),
"ToolTip": tt,
"Accel": "I, P",
}
def IsActive(self):
return bool(FreeCADGui.Selection.getSelection())
def Activated(self):
import exportIFC # lazy loading
import ifc_tools
from PySide import QtCore, QtGui
doc = FreeCAD.ActiveDocument
objs = FreeCADGui.Selection.getSelection()
sf = QtGui.QFileDialog.getSaveFileName(
None,
"Save an IFC file",
None,
"Industry Foundation Classes (*.ifc)",
)
if sf and sf[0]:
sf = sf[0]
if not sf.lower().endswith(".ifc"):
sf += ".ifc"
exportIFC.export(objs, sf)
ifc_tools.create_document_object(doc, sf, strategy=2)
ifc_tools.remove_tree(objs)
doc.recompute()
class IFC_Save:
"""Saves the current IFC document"""
def GetResources(self):
tt = QT_TRANSLATE_NOOP(
"IFC_Save", "Saves the current IFC document"
)
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "IFC_document.svg"),
"MenuText": QT_TRANSLATE_NOOP("IFC_Save", "Save IFC file"),
"ToolTip": tt,
"Accel": "Ctrl+S",
}
def IsActive(self):
doc = FreeCAD.ActiveDocument
if getattr(doc, "IfcFilePath", None):
if getattr(getattr(doc, "Proxy", None), "ifcfile", None):
return True
return False
def Activated(self):
import ifc_tools # lazy loading
doc = FreeCAD.ActiveDocument
ifc_tools.save(doc)
gdoc = FreeCADGui.getDocument(doc.Name)
try:
gdoc.Modified = False
except:
pass
class IFC_SaveAs:
"""Saves the current IFC document as another name"""
def GetResources(self):
tt = QT_TRANSLATE_NOOP(
"IFC_SaveAs", "Saves the current IFC document as another file"
)
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "IFC_document.svg"),
"MenuText": QT_TRANSLATE_NOOP("IFC_SaveAs", "Save IFC file as..."),
"ToolTip": tt,
"Accel": "Ctrl+Shift+S",
}
def IsActive(self):
doc = FreeCAD.ActiveDocument
if getattr(doc, "IfcFilePath", None):
if getattr(getattr(doc, "Proxy", None), "ifcfile", None):
return True
return False
def Activated(self):
import ifc_tools # lazy loading
import ifc_viewproviders
doc = FreeCAD.ActiveDocument
if ifc_viewproviders.get_filepath(doc):
ifc_tools.save(doc)
gdoc = FreeCADGui.getDocument(doc.Name)
try:
gdoc.Modified = False
except:
pass
def get_commands():
"""Returns a list of IFC commands"""
return ["IFC_Diff", "IFC_Expand", "IFC_MakeProject"]
# initialize commands
FreeCADGui.addCommand("IFC_Diff", IFC_Diff())
FreeCADGui.addCommand("IFC_Expand", IFC_Expand())
FreeCADGui.addCommand("IFC_ConvertDocument", IFC_ConvertDocument())
FreeCADGui.addCommand("IFC_MakeProject", IFC_MakeProject())
FreeCADGui.addCommand("IFC_Save", IFC_Save())
FreeCADGui.addCommand("IFC_SaveAs", IFC_SaveAs())