-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BimLayers.py
614 lines (538 loc) · 24.6 KB
/
BimLayers.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# ***************************************************************************
# * *
# * Copyright (c) 2019 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 *
# * *
# ***************************************************************************
"""Layers manager for FreeCAD"""
import os
import FreeCAD
import Draft
from BimTranslateUtils import *
def getColorIcon(color):
"returns a QtGui.QIcon from a color 3-float tuple"
from PySide import QtCore, QtGui
c = QtGui.QColor(int(color[0] * 255), int(color[1] * 255), int(color[2] * 255))
im = QtGui.QImage(48, 48, QtGui.QImage.Format_ARGB32)
im.fill(c)
px = QtGui.QPixmap.fromImage(im)
return QtGui.QIcon(px)
class BIM_Layers:
"The BIM_Layers FreeCAD command"
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "BIM_Layers.svg"
),
"MenuText": QT_TRANSLATE_NOOP("BIM_Layers", "Manage layers..."),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Layers", "Set/modify the different layers of your BIM project"
),
}
def Activated(self):
import FreeCADGui
from PySide import QtCore, QtGui
# store changes to be committed
self.deleteList = []
# store assignlist
self.assignList = {}
# create the dialog
self.dialog = FreeCADGui.PySideUic.loadUi(
os.path.join(os.path.dirname(__file__), "dialogLayers.ui")
)
# store the ifc icon
self.ifcicon = QtGui.QIcon(
os.path.join(os.path.dirname(__file__), "icons", "IFC.svg")
)
# set nice icons
self.dialog.setWindowIcon(QtGui.QIcon(":/icons/Draft_Layer.svg"))
self.dialog.buttonNew.setIcon(QtGui.QIcon(":/icons/document-new.svg"))
self.dialog.buttonDelete.setIcon(QtGui.QIcon(":/icons/delete.svg"))
self.dialog.buttonSelectAll.setIcon(QtGui.QIcon(":/icons/edit-select-all.svg"))
self.dialog.buttonToggle.setIcon(QtGui.QIcon(":/icons/dagViewVisible.svg"))
self.dialog.buttonIsolate.setIcon(QtGui.QIcon(":/icons/view-refresh.svg"))
self.dialog.buttonCancel.setIcon(QtGui.QIcon(":/icons/edit_Cancel.svg"))
self.dialog.buttonOK.setIcon(QtGui.QIcon(":/icons/edit_OK.svg"))
self.dialog.buttonAssign.setIcon(QtGui.QIcon(":/icons/button_right.svg"))
self.dialog.buttonIFC.setIcon(self.ifcicon)
# restore window geometry from stored state
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM")
w = pref.GetInt("LayersManagerWidth", 640)
h = pref.GetInt("LayersManagerHeight", 320)
self.dialog.resize(w, h)
# center the dialog over FreeCAD window
mw = FreeCADGui.getMainWindow()
self.dialog.move(
mw.frameGeometry().topLeft()
+ mw.rect().center()
- self.dialog.rect().center()
)
# connect signals/slots
self.dialog.buttonNew.clicked.connect(self.addItem)
self.dialog.buttonDelete.clicked.connect(self.onDelete)
self.dialog.buttonSelectAll.clicked.connect(self.dialog.tree.selectAll)
self.dialog.buttonToggle.clicked.connect(self.onToggle)
self.dialog.buttonCancel.clicked.connect(self.dialog.reject)
self.dialog.buttonIsolate.clicked.connect(self.onIsolate)
self.dialog.buttonOK.clicked.connect(self.accept)
self.dialog.buttonAssign.clicked.connect(self.onAssign)
self.dialog.buttonIFC.clicked.connect(self.onIFC)
self.dialog.rejected.connect(self.reject)
# set the model up
self.model = QtGui.QStandardItemModel()
self.dialog.tree.setModel(self.model)
self.dialog.tree.setUniformRowHeights(True)
self.dialog.tree.setItemDelegate(BIM_Layers_Delegate())
self.dialog.tree.setItemsExpandable(False)
self.dialog.tree.setRootIsDecorated(False) # removes spacing in first column
self.dialog.tree.setSelectionMode(
QtGui.QTreeView.ExtendedSelection
) # allow to select many
# fill the tree view
self.update()
# rock 'n roll!!!
self.dialog.show()
def accept(self):
"when OK button is pressed"
changed = False
# delete layers
for name in self.deleteList:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
FreeCAD.ActiveDocument.removeObject(name)
# assign
for target, objs in self.assignList.items():
target_obj = FreeCAD.ActiveDocument.getObject(target)
if target_obj:
for obj in objs:
target_obj.Proxy.addObject(target_obj, obj)
# apply changes
for row in range(self.model.rowCount()):
# get or create layer
name = self.model.item(row, 1).toolTip()
obj = None
if name:
obj = FreeCAD.ActiveDocument.getObject(name)
if not obj:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
if self.model.item(row, 1).icon().isNull():
obj = Draft.make_layer(self.model.item(row, 1).text())
else:
import ifc_tools
import FreeCADGui
active = FreeCADGui.ActiveDocument.ActiveView.getActiveObject(
"NativeIFC"
)
project = None
if active:
project = ifc_tools.get_project(active)
else:
projects = [
o
for o in FreeCAD.ActiveDocument.Objects
if hasattr(o, "Proxy") and hasattr(o.Proxy, "ifcfile")
]
if projects:
project = projects[0]
if len(projects) > 1:
FreeCAD.Console.PrintWarning(
translate(
"BIM",
"Warning: The new layer was added to the project",
)
+ " "
+ project.Label
+ "\n"
)
else:
FreeCAD.Console.PrintError(
translate(
"BIM", "There is no IFC project in this document"
)
+ "\n"
)
if project:
obj = ifc_tools.create_layer(
self.model.item(row, 1).text(), project
)
# visibility
checked = (
True
if self.model.item(row, 0).checkState() == QtCore.Qt.Checked
else False
)
if checked != obj.ViewObject.Visibility:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
obj.ViewObject.Visibility = checked
# label
label = self.model.item(row, 1).text()
if label:
if obj.Label != label:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
obj.Label = label
# line width
width = self.model.item(row, 2).data(QtCore.Qt.DisplayRole)
if width:
if obj.ViewObject.LineWidth != width:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
obj.ViewObject.LineWidth = width
# draw style
style = self.model.item(row, 3).text()
if style:
if obj.ViewObject.DrawStyle != style:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
obj.ViewObject.DrawStyle = style
# line color
color = self.model.item(row, 4).data(QtCore.Qt.UserRole)
if color:
if obj.ViewObject.LineColor[3:] != color:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
obj.ViewObject.LineColor = color
# shape color
color = self.model.item(row, 5).data(QtCore.Qt.UserRole)
if color:
if obj.ViewObject.ShapeColor[3:] != color:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
obj.ViewObject.ShapeColor = color
# transparency
transparency = self.model.item(row, 6).data(QtCore.Qt.DisplayRole)
if transparency:
if obj.ViewObject.Transparency != transparency:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
obj.ViewObject.Transparency = transparency
# line print color
color = self.model.item(row, 7).data(QtCore.Qt.UserRole)
if color:
if not "LinePrintColor" in obj.ViewObject.PropertiesList:
if hasattr(obj.ViewObject.Proxy, "set_properties"):
obj.ViewObject.Proxy.set_properties(obj.ViewObject)
if "LinePrintColor" in obj.ViewObject.PropertiesList:
if obj.ViewObject.LinePrintColor[3:] != color:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
obj.ViewObject.LinePrintColor = color
# recompute
if changed:
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
# exit
self.dialog.reject()
def reject(self):
"when Cancel button is pressed or dialog is closed"
# save dialog size
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM")
pref.SetInt("LayersManagerWidth", self.dialog.width())
pref.SetInt("LayersManagerHeight", self.dialog.height())
return True
def update(self):
"rebuild the model from document contents"
self.model.clear()
# set header
self.model.setHorizontalHeaderLabels(
[
translate("BIM", "On"),
translate("BIM", "Name"),
translate("BIM", "Line width"),
translate("BIM", "Draw style"),
translate("BIM", "Line color"),
translate("BIM", "Face color"),
translate("BIM", "Transparency"),
translate("BIM", "Line print color"),
]
)
self.dialog.tree.header().setDefaultSectionSize(72)
self.dialog.tree.setColumnWidth(0, 32) # on/off column
self.dialog.tree.setColumnWidth(1, 128) # name column
# populate
objs = [
obj
for obj in FreeCAD.ActiveDocument.Objects
if Draft.getType(obj) == "Layer"
]
objs.sort(key=lambda o: o.Label)
for obj in objs:
self.addItem(obj)
def addItem(self, obj=None):
"adds a row to the model"
import FreeCADGui
from PySide import QtCore, QtGui
# create row with default values
onItem = QtGui.QStandardItem()
onItem.setCheckable(True)
onItem.setCheckState(QtCore.Qt.Checked)
nameItem = QtGui.QStandardItem(translate("BIM", "New Layer"))
widthItem = QtGui.QStandardItem()
widthItem.setData(
self.getPref("DefaultShapeLineWidth", 2, "Integer"), QtCore.Qt.DisplayRole
)
styleItem = QtGui.QStandardItem("Solid")
lineColorItem = QtGui.QStandardItem()
lineColorItem.setData(
self.getPref("DefaultShapeLineColor", 421075455), QtCore.Qt.UserRole
)
shapeColorItem = QtGui.QStandardItem()
shapeColorItem.setData(
self.getPref("DefaultShapeColor", 3435973887), QtCore.Qt.UserRole
)
transparencyItem = QtGui.QStandardItem()
transparencyItem.setData(0, QtCore.Qt.DisplayRole)
linePrintColorItem = QtGui.QStandardItem()
linePrintColorItem.setData(
self.getPref("DefaultPrintColor", 0), QtCore.Qt.UserRole
)
if FreeCADGui.ActiveDocument.ActiveView.getActiveObject("NativeIFC"):
nameItem.setIcon(self.ifcicon)
# populate with object data
if obj:
if hasattr(obj, "StepId"):
nameItem.setIcon(self.ifcicon)
onItem.setCheckState(
QtCore.Qt.Checked if obj.ViewObject.Visibility else QtCore.Qt.Unchecked
)
nameItem.setText(obj.Label)
nameItem.setToolTip(obj.Name)
widthItem.setData(obj.ViewObject.LineWidth, QtCore.Qt.DisplayRole)
styleItem.setText(obj.ViewObject.DrawStyle)
lineColorItem.setData(obj.ViewObject.LineColor[:3], QtCore.Qt.UserRole)
shapeColorItem.setData(obj.ViewObject.ShapeColor[:3], QtCore.Qt.UserRole)
transparencyItem.setData(obj.ViewObject.Transparency, QtCore.Qt.DisplayRole)
if hasattr(obj.ViewObject, "LinePrintColor"):
linePrintColorItem.setData(
obj.ViewObject.LinePrintColor[:3], QtCore.Qt.UserRole
)
lineColorItem.setIcon(getColorIcon(lineColorItem.data(QtCore.Qt.UserRole)))
shapeColorItem.setIcon(getColorIcon(shapeColorItem.data(QtCore.Qt.UserRole)))
linePrintColorItem.setIcon(
getColorIcon(linePrintColorItem.data(QtCore.Qt.UserRole))
)
# append row
self.model.appendRow(
[
onItem,
nameItem,
widthItem,
styleItem,
lineColorItem,
shapeColorItem,
transparencyItem,
linePrintColorItem,
]
)
def getPref(self, value, default, valuetype="Unsigned"):
"retrieves a view pref value"
p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/View")
if valuetype == "Unsigned":
c = p.GetUnsigned(value, default)
r = float((c >> 24) & 0xFF) / 255.0
g = float((c >> 16) & 0xFF) / 255.0
b = float((c >> 8) & 0xFF) / 255.0
return (
r,
g,
b,
)
elif valuetype == "Integer":
return p.GetInt(value, default)
def onDelete(self):
"delete selected rows"
rows = []
for index in self.dialog.tree.selectedIndexes():
if not index.row() in rows:
rows.append(index.row())
# append layer name to the delete list
if index.column() == 1:
name = self.model.itemFromIndex(index).toolTip()
if name:
if not name in self.deleteList:
self.deleteList.append(name)
# delete rows starting from the lowest, to not alter row indexes while deleting
rows.sort()
rows.reverse()
for row in rows:
self.model.takeRow(row)
def onToggle(self):
"toggle selected layers on/off"
from PySide import QtCore, QtGui
state = None
for index in self.dialog.tree.selectedIndexes():
if index.column() == 0:
# get state from first selected row
if state is None:
if (
self.model.itemFromIndex(index).checkState()
== QtCore.Qt.Checked
):
state = QtCore.Qt.Unchecked
else:
state = QtCore.Qt.Checked
self.model.itemFromIndex(index).setCheckState(state)
def onIsolate(self):
"isolates the selected layers (turns all the others off"
from PySide import QtCore, QtGui
onrows = []
for index in self.dialog.tree.selectedIndexes():
if not index.row() in onrows:
onrows.append(index.row())
for row in range(self.model.rowCount()):
if not row in onrows:
self.model.item(row, 0).setCheckState(QtCore.Qt.Unchecked)
def onIFC(self):
"attributes this layer to an IFC project"
from PySide import QtCore, QtGui
for index in self.dialog.tree.selectedIndexes():
if index.column() == 1:
name = self.model.itemFromIndex(index).toolTip()
if name:
obj = FreeCAD.ActiveDocument.getObject(name)
if obj:
if hasattr(obj, "StepId"):
return
item = self.model.itemFromIndex(index)
if item.icon().isNull():
item.setIcon(self.ifcicon)
else:
item.setIcon(QtGui.QIcon())
def onAssign(self):
"attributes selected objects to a selected layer"
import FreeCADGui
for index in self.dialog.tree.selectedIndexes():
if index.column() == 1:
name = self.model.itemFromIndex(index).toolTip()
if name:
target = FreeCAD.ActiveDocument.getObject(name)
if target:
selected = FreeCADGui.Selection.getSelection()
if selected:
self.assignList.setdefault(target.Name, [])
for i in selected:
if i not in self.assignList[target.Name]:
self.ssignList[target.Name].append(i)
if FreeCAD.GuiUp:
from PySide import QtCore, QtGui
class BIM_Layers_Delegate(QtGui.QStyledItemDelegate):
"model delegate"
def __init__(self, parent=None, *args):
QtGui.QStyledItemDelegate.__init__(self, parent, *args)
# setEditorData() is triggered several times.
# But we want to show the color dialog only the first time
self.first = True
def createEditor(self, parent, option, index):
if index.column() == 0: # Layer on/off
editor = QtGui.QCheckBox(parent)
if index.column() == 1: # Layer name
editor = QtGui.QLineEdit(parent)
elif index.column() == 2: # Line width
editor = QtGui.QSpinBox(parent)
editor.setMaximum(99)
elif index.column() == 3: # Line style
editor = QtGui.QComboBox(parent)
editor.addItems(["Solid", "Dashed", "Dotted", "Dashdot"])
elif index.column() == 4: # Line color
editor = QtGui.QLineEdit(parent)
self.first = True
elif index.column() == 5: # Shape color
editor = QtGui.QLineEdit(parent)
self.first = True
elif index.column() == 6: # Transparency
editor = QtGui.QSpinBox(parent)
editor.setMaximum(100)
elif index.column() == 7: # Line print color
editor = QtGui.QLineEdit(parent)
self.first = True
return editor
def setEditorData(self, editor, index):
if index.column() == 0: # Layer on/off
editor.setChecked(index.data())
elif index.column() == 1: # Layer name
editor.setText(index.data())
elif index.column() == 2: # Line width
editor.setValue(index.data())
elif index.column() == 3: # Line style
editor.setCurrentIndex(
["Solid", "Dashed", "Dotted", "Dashdot"].index(index.data())
)
elif index.column() == 4: # Line color
editor.setText(str(index.data(QtCore.Qt.UserRole)))
if self.first:
c = index.data(QtCore.Qt.UserRole)
color = QtGui.QColorDialog.getColor(
QtGui.QColor(int(c[0] * 255), int(c[1] * 255), int(c[2] * 255))
)
editor.setText(str(color.getRgbF()))
self.first = False
elif index.column() == 5: # Shape color
editor.setText(str(index.data(QtCore.Qt.UserRole)))
if self.first:
c = index.data(QtCore.Qt.UserRole)
color = QtGui.QColorDialog.getColor(
QtGui.QColor(int(c[0] * 255), int(c[1] * 255), int(c[2] * 255))
)
editor.setText(str(color.getRgbF()))
self.first = False
elif index.column() == 6: # Transparency
editor.setValue(index.data())
elif index.column() == 7: # Line print color
editor.setText(str(index.data(QtCore.Qt.UserRole)))
if self.first:
c = index.data(QtCore.Qt.UserRole)
color = QtGui.QColorDialog.getColor(
QtGui.QColor(int(c[0] * 255), int(c[1] * 255), int(c[2] * 255))
)
editor.setText(str(color.getRgbF()))
self.first = False
def setModelData(self, editor, model, index):
if index.column() == 0: # Layer on/off
model.setData(index, editor.isChecked())
elif index.column() == 1: # Layer name
model.setData(index, editor.text())
elif index.column() == 2: # Line width
model.setData(index, editor.value())
elif index.column() == 3: # Line style
model.setData(
index,
["Solid", "Dashed", "Dotted", "Dashdot"][editor.currentIndex()],
)
elif index.column() == 4: # Line color
model.setData(index, eval(editor.text()), QtCore.Qt.UserRole)
model.itemFromIndex(index).setIcon(getColorIcon(eval(editor.text())))
elif index.column() == 5: # Shape color
model.setData(index, eval(editor.text()), QtCore.Qt.UserRole)
model.itemFromIndex(index).setIcon(getColorIcon(eval(editor.text())))
elif index.column() == 6: # Transparency
model.setData(index, editor.value())
elif index.column() == 7: # Line prin color
model.setData(index, eval(editor.text()), QtCore.Qt.UserRole)
model.itemFromIndex(index).setIcon(getColorIcon(eval(editor.text())))