forked from yorikvanhavre/BIM_Workbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BimViews.py
336 lines (283 loc) · 12.8 KB
/
BimViews.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# ***************************************************************************
# * *
# * Copyright (c) 2018 Yorik van Havre <[email protected]> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 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 Library 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 FreeCAD commands for the BIM workbench"""
import os, sys
import FreeCAD
from BimTranslateUtils import *
UPDATEINTERVAL = 2000 # number of milliseconds between BIM Views window update
class BIM_Views:
def GetResources(self):
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "BIM_Views.svg"),
"MenuText": QT_TRANSLATE_NOOP("BIM_Views", "Views manager"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Views", "Shows or hides the views manager"
),
"Accel": "Ctrl+9",
}
def Activated(self):
import FreeCADGui
from PySide import QtCore, QtGui
vm = findWidget()
bimviewsbutton = None
mw = FreeCADGui.getMainWindow()
st = mw.statusBar()
statuswidget = st.findChild(QtGui.QToolBar, "BIMStatusWidget")
if statuswidget:
if hasattr(statuswidget, "bimviewsbutton"):
bimviewsbutton = statuswidget.bimviewsbutton
if vm:
if vm.isVisible():
vm.hide()
if bimviewsbutton:
bimviewsbutton.setChecked(False)
FreeCAD.ParamGet(
"User parameter:BaseApp/Preferences/Mod/BIM"
).SetBool("RestoreBimViews", False)
else:
vm.show()
if bimviewsbutton:
bimviewsbutton.setChecked(True)
FreeCAD.ParamGet(
"User parameter:BaseApp/Preferences/Mod/BIM"
).SetBool("RestoreBimViews", True)
self.update()
else:
vm = QtGui.QDockWidget()
# create the dialog
dialog = FreeCADGui.PySideUic.loadUi(
os.path.join(os.path.dirname(__file__), "dialogViews.ui")
)
vm.setWidget(dialog)
vm.tree = dialog.tree
# set button sizes
size = FreeCAD.ParamGet(
"User parameter:BaseApp/Preferences/General"
).GetInt("ToolbarIconSize", 24)
for button in [
dialog.buttonAddLevel,
dialog.buttonAddProxy,
dialog.buttonDelete,
dialog.buttonToggle,
dialog.buttonIsolate,
dialog.buttonSaveView,
dialog.buttonRename,
]:
button.setMaximumSize(QtCore.QSize(size + 4, size + 4))
button.setIconSize(QtCore.QSize(size, size))
# set button icons
import Arch_rc, Draft_rc
dialog.buttonAddLevel.setIcon(QtGui.QIcon(":/icons/Arch_Floor.svg"))
dialog.buttonAddProxy.setIcon(QtGui.QIcon(":/icons/Draft_SelectPlane.svg"))
dialog.buttonDelete.setIcon(QtGui.QIcon(":/icons/delete.svg"))
dialog.buttonToggle.setIcon(QtGui.QIcon(":/icons/dagViewVisible.svg"))
dialog.buttonIsolate.setIcon(QtGui.QIcon(":/icons/view-refresh.svg"))
dialog.buttonSaveView.setIcon(QtGui.QIcon(":/icons/view-perspective.svg"))
dialog.buttonRename.setIcon(
QtGui.QIcon(":/icons/accessories-text-editor.svg")
)
# connect signals
dialog.buttonAddLevel.clicked.connect(self.addLevel)
dialog.buttonAddProxy.clicked.connect(self.addProxy)
dialog.buttonDelete.clicked.connect(self.delete)
dialog.buttonToggle.clicked.connect(self.toggle)
dialog.buttonIsolate.clicked.connect(self.isolate)
dialog.buttonSaveView.clicked.connect(self.saveView)
dialog.buttonRename.clicked.connect(self.rename)
dialog.tree.itemClicked.connect(self.select)
dialog.tree.itemDoubleClicked.connect(show)
dialog.tree.itemChanged.connect(self.renameObject)
# set the dock widget
vm.setObjectName("BIM Views Manager")
vm.setWindowTitle(translate("BIM", "BIM Views manager"))
mw = FreeCADGui.getMainWindow()
mw.addDockWidget(QtCore.Qt.LeftDockWidgetArea, vm)
# restore saved settings
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM")
vm.tree.setColumnWidth(0, pref.GetInt("ViewManagerColumnWidth", 100))
vm.setFloating(pref.GetBool("ViewManagerFloating", False))
# check the status bar button
if bimviewsbutton:
bimviewsbutton.setChecked(True)
pref.SetBool("RestoreBimViews", True)
self.update()
def update(self, retrigger=True):
"updates the view manager"
from PySide import QtCore, QtGui
import FreeCADGui
vm = findWidget()
if vm and FreeCAD.ActiveDocument:
if vm.isVisible() and (vm.tree.state() != vm.tree.EditingState):
vm.tree.clear()
import Draft
for obj in FreeCAD.ActiveDocument.Objects:
t = Draft.getType(obj)
if obj and (t in ["Building", "BuildingPart", "WorkingPlaneProxy"]):
u = ""
if t in ["Building", "BuildingPart"]:
u = FreeCAD.Units.Quantity(
obj.Placement.Base.z, FreeCAD.Units.Length
).UserString
it = QtGui.QTreeWidgetItem([obj.Label, u])
it.setFlags(it.flags() | QtCore.Qt.ItemIsEditable)
it.setToolTip(0, obj.Name)
if obj.ViewObject:
if hasattr(obj.ViewObject, "Proxy") and hasattr(
obj.ViewObject.Proxy, "getIcon"
):
it.setIcon(
0, QtGui.QIcon(obj.ViewObject.Proxy.getIcon())
)
vm.tree.addTopLevelItem(it)
# if obj.Name in selected:
if obj in FreeCADGui.Selection.getSelection():
it.setSelected(True)
if retrigger:
QtCore.QTimer.singleShot(UPDATEINTERVAL, self.update)
# save state
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM")
pref.SetInt("ViewManagerColumnWidth", vm.tree.columnWidth(0))
pref.SetBool("ViewManagerFloating", vm.isFloating())
def select(self, item, column=None):
"selects a doc object corresponding to an item"
import FreeCADGui
name = item.toolTip(0)
obj = FreeCAD.ActiveDocument.getObject(name)
if obj:
FreeCADGui.Selection.clearSelection()
FreeCADGui.Selection.addSelection(obj)
def addLevel(self):
"adds a building part"
import Arch
FreeCAD.ActiveDocument.openTransaction("Create BuildingPart")
Arch.makeFloor()
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
self.update(False)
def addProxy(self):
"adds a WP proxy"
import Draft
FreeCAD.ActiveDocument.openTransaction("Create WP Proxy")
Draft.makeWorkingPlaneProxy(FreeCAD.DraftWorkingPlane.getPlacement())
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
self.update(False)
def delete(self):
"deletes the selected object"
vm = findWidget()
if vm:
if vm.tree.selectedItems():
FreeCAD.ActiveDocument.openTransaction("Delete")
for item in vm.tree.selectedItems():
obj = FreeCAD.ActiveDocument.getObject(item.toolTip(0))
if obj:
FreeCAD.ActiveDocument.removeObject(obj.Name)
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
self.update(False)
def rename(self):
"renames the selected object"
vm = findWidget()
if vm:
if vm.tree.selectedItems():
if vm.tree.selectedItems():
item = vm.tree.selectedItems()[-1]
vm.tree.editItem(item, 0)
def renameObject(self, item, column):
"renames the actual object"
if column == 0:
obj = FreeCAD.ActiveDocument.getObject(item.toolTip(column))
if obj:
obj.Label = item.text(column)
def toggle(self):
"toggle selected item on/off"
vm = findWidget()
if vm:
for item in vm.tree.selectedItems():
obj = FreeCAD.ActiveDocument.getObject(item.toolTip(0))
if obj:
obj.ViewObject.Visibility = not (obj.ViewObject.Visibility)
FreeCAD.ActiveDocument.recompute()
def isolate(self):
"turns all items off except the selected ones"
vm = findWidget()
if vm:
onnames = [item.toolTip(0) for item in vm.tree.selectedItems()]
for i in range(vm.tree.topLevelItemCount()):
item = vm.tree.topLevelItem(i)
if item.toolTip(0) not in onnames:
obj = FreeCAD.ActiveDocument.getObject(item.toolTip(0))
if obj:
obj.ViewObject.Visibility = False
FreeCAD.ActiveDocument.recompute()
def saveView(self):
"save the current camera angle to the selected item"
vm = findWidget()
if vm:
for item in vm.tree.selectedItems():
obj = FreeCAD.ActiveDocument.getObject(item.toolTip(0))
if obj:
if hasattr(obj.ViewObject.Proxy, "writeCamera"):
obj.ViewObject.Proxy.writeCamera()
FreeCAD.ActiveDocument.recompute()
# These functions need to be localized outside the command class, as they are used outside this module
def findWidget():
"finds the manager widget, if present"
import FreeCADGui
from PySide import QtGui
mw = FreeCADGui.getMainWindow()
vm = mw.findChild(QtGui.QDockWidget, "BIM Views Manager")
if vm:
return vm
return None
def show(item, column=None):
"item has been double-clicked"
import FreeCADGui
obj = None
vm = findWidget()
if isinstance(item, str) or (
(sys.version_info.major < 3) and isinstance(item, unicode)
):
# called from Python code
obj = FreeCAD.ActiveDocument.getObject(item)
else:
# called from GUI
if column == 1:
# user clicked the level field
if vm:
vm.tree.editItem(item, column)
return
else:
# TODO find a way to not edit the object name
obj = FreeCAD.ActiveDocument.getObject(item.toolTip(0))
if obj:
FreeCADGui.Selection.clearSelection()
FreeCADGui.Selection.addSelection(obj)
FreeCADGui.runCommand("Draft_SelectPlane")
if vm:
# store the last double-clicked item for the BIM WPView command
if isinstance(item, str) or (
(sys.version_info.major < 3) and isinstance(item, unicode)
):
vm.lastSelected = item
else:
vm.lastSelected = item.toolTip(0)