-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BimMaterial.py
565 lines (516 loc) · 23.3 KB
/
BimMaterial.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
# ***************************************************************************
# * *
# * Copyright (c) 2017 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
import FreeCAD
from BimTranslateUtils import *
if FreeCAD.GuiUp:
from PySide import QtCore, QtGui
class MatLineEdit(QtGui.QLineEdit):
"custom QLineEdit widget that has the power to catch up/down arrow keypress"
def __init__(self, parent=None):
QtGui.QLineEdit.__init__(self, parent)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Up:
self.emit(QtCore.SIGNAL("up()"))
elif event.key() == QtCore.Qt.Key_Down:
self.emit(QtCore.SIGNAL("down()"))
else:
QtGui.QLineEdit.keyPressEvent(self, event)
class BIM_Material:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "BIM_Material.svg"
),
"MenuText": QT_TRANSLATE_NOOP("BIM_Material", "Material"),
"Accel": "M, A",
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Material", "Sets or creates a material for selected objects"
),
}
def Activated(self):
import FreeCADGui
self.dlg = None
self.dlg = QtGui.QDialog()
self.dlg.objects = [
obj
for obj in FreeCADGui.Selection.getSelection()
if hasattr(obj, "Material") or hasattr(obj, "StepId")
]
p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM")
w = p.GetInt("BimMaterialDialogWidth", 230)
h = p.GetInt("BimMaterialDialogHeight", 350)
self.dlg.resize(w, h)
self.dlg.setWindowTitle(translate("BIM", "Select material"))
self.dlg.setWindowIcon(QtGui.QIcon(":/icons/Arch_Material.svg"))
mw = FreeCADGui.getMainWindow()
self.dlg.move(
mw.frameGeometry().topLeft() + mw.rect().center() - self.dlg.rect().center()
)
lay = QtGui.QVBoxLayout(self.dlg)
matList = QtGui.QListWidget(self.dlg)
matList.setSortingEnabled(True)
matList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.dlg.matList = matList
lay.addWidget(matList)
# populate materials list
self.rescan(rebuild=True)
if matList.count():
# add search box
searchLayout = QtGui.QHBoxLayout()
searchLayout.setSpacing(2)
searchBox = MatLineEdit(self.dlg)
searchBox.setPlaceholderText(translate("BIM", "Search..."))
searchBox.setToolTip(translate("BIM", "Searches object labels"))
self.dlg.searchBox = searchBox
searchLayout.addWidget(searchBox)
searchBox.textChanged.connect(self.onSearch)
# searchBox.up.connect(self.onUpArrow)
QtCore.QObject.connect(searchBox, QtCore.SIGNAL("up()"), self.onUpArrow)
# searchBox.down.connect(self.onUpArrow)
QtCore.QObject.connect(searchBox, QtCore.SIGNAL("down()"), self.onDownArrow)
# add clear button
buttonClear = QtGui.QToolButton(self.dlg)
buttonClear.setFixedSize(18, 21)
buttonClear.setStyleSheet("QToolButton {margin-bottom:1px}")
buttonClear.setIcon(QtGui.QIcon(":/icons/edit-cleartext.svg"))
buttonClear.setToolTip(translate("BIM", "Clears the search field"))
buttonClear.setAutoRaise(True)
searchLayout.addWidget(buttonClear)
buttonClear.clicked.connect(self.onClearSearch)
lay.addLayout(searchLayout)
# create
createLayout = QtGui.QHBoxLayout()
buttonCreate = QtGui.QPushButton(
translate("BIM", "Create new material"), self.dlg
)
buttonCreate.setIcon(QtGui.QIcon(":/icons/Arch_Material.svg"))
createLayout.addWidget(buttonCreate)
buttonCreate.clicked.connect(self.onCreate)
# create multi
buttonMulti = QtGui.QPushButton(
translate("BIM", "Create new multi-material"), self.dlg
)
buttonMulti.setIcon(QtGui.QIcon(":/icons/Arch_Material_Multi.svg"))
createLayout.addWidget(buttonMulti)
buttonMulti.clicked.connect(self.onMulti)
lay.addLayout(createLayout)
# merge dupes
opsLayout = QtGui.QHBoxLayout()
buttonMergeDupes = QtGui.QPushButton(
translate("BIM", "Merge duplicates"), self.dlg
)
buttonMergeDupes.setIcon(QtGui.QIcon(":/icons/view-refresh.svg"))
opsLayout.addWidget(buttonMergeDupes)
buttonMergeDupes.clicked.connect(self.onMergeDupes)
# delete unused
buttonDeleteUnused = QtGui.QPushButton(
translate("BIM", "Delete unused"), self.dlg
)
buttonDeleteUnused.setIcon(QtGui.QIcon(":/icons/delete.svg"))
opsLayout.addWidget(buttonDeleteUnused)
buttonDeleteUnused.clicked.connect(self.onDeleteUnused)
lay.addLayout(opsLayout)
# add standard buttons
buttonBox = QtGui.QDialogButtonBox(self.dlg)
buttonBox.setOrientation(QtCore.Qt.Horizontal)
buttonBox.setStandardButtons(
QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok
)
lay.addWidget(buttonBox)
buttonBox.accepted.connect(self.onAccept)
buttonBox.rejected.connect(self.onReject)
# set context menu
self.contextMenu = QtGui.QMenu()
context1 = self.contextMenu.addAction(translate("BIM", "Rename"))
QtCore.QObject.connect(
context1, QtCore.SIGNAL("triggered()"), self.onStartRename
)
context2 = self.contextMenu.addAction(translate("BIM", "Duplicate"))
QtCore.QObject.connect(
context2, QtCore.SIGNAL("triggered()"), self.onDuplicate
)
context3 = self.contextMenu.addAction(translate("BIM", "Merge to..."))
QtCore.QObject.connect(
context3, QtCore.SIGNAL("triggered()"), self.onMergeTo
)
context4 = self.contextMenu.addAction(translate("BIM", "Delete"))
QtCore.QObject.connect(
context4, QtCore.SIGNAL("triggered()"), self.onDelete
)
# other signal/slots to connect
QtCore.QObject.connect(
matList,
QtCore.SIGNAL("customContextMenuRequested(QPoint)"),
self.onRightClick,
)
matList.itemDoubleClicked.connect(self.onAccept)
matList.itemChanged.connect(self.onEndRename)
self.dlg.show()
self.dlg.searchBox.setFocus()
else:
# no material in the document
self.dlg = None
FreeCADGui.runCommand("Arch_Material")
def onRightClick(self, pos):
parentPosition = self.dlg.matList.mapToGlobal(QtCore.QPoint(0, 0))
self.contextMenu.move(parentPosition + pos)
self.contextMenu.show()
def onMergeDupes(self):
if self.dlg:
todelete = []
first = True
for mat in self.dlg.materials:
orig = None
for om in mats:
if om.Label == mat.Label:
orig = om
break
else:
if (
mat.Label[-1].isdigit()
and mat.Label[-2].isdigit()
and mat.Label[-3].isdigit()
):
for om in self.dlg.materials:
if om.Label == mat.Label[:-3].strip():
orig = om
break
if orig:
for par in mat.InList:
for prop in par.PropertiesList:
if getattr(par, prop) == mat:
FreeCAD.Console.PrintMessage(
"Changed property '"
+ prop
+ "' of object "
+ par.Label
+ " from "
+ mat.Label
+ " to "
+ orig.Label
+ "\n"
)
if first:
FreeCAD.ActiveDocument.openTransaction(
"Merge materials"
)
first = False
setattr(par, prop, orig)
todelete.append(mat)
for tod in todelete:
if not tod.InList:
FreeCAD.Console.PrintMessage(
translate("BIM", "Merging duplicate material")
+ " "
+ tod.Label
+ "\n"
)
if first:
FreeCAD.ActiveDocument.openTransaction("Merge materials")
first = False
FreeCAD.ActiveDocument.removeObject(tod.Name)
elif (len(tod.InList) == 1) and (
tod.InList[0].isDerivedFrom("App::DocumentObjectGroup")
):
FreeCAD.Console.PrintMessage(
translate("BIM", "Merging duplicate material")
+ " "
+ tod.Label
+ "\n"
)
if first:
FreeCAD.ActiveDocument.openTransaction("Merge materials")
first = False
FreeCAD.ActiveDocument.removeObject(tod.Name)
else:
FreeCAD.Console.PrintMessage(
translate("BIM", "Unable to delete material")
+ " "
+ tod.Label
+ ": "
+ translate("BIM", "InList not empty")
+ "\n"
)
if not first:
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
self.rescan(rebuild=True)
def onDeleteUnused(self):
first = True
if self.dlg:
for i in range(self.dlg.matList.count()):
item = self.dlg.matList.item(i)
if item:
obj = FreeCAD.ActiveDocument.getObject(item.toolTip())
if obj:
parents = [
parent
for parent in obj.InList
if not parent.isDerivedFrom("App::DocumentObjectGroup")
]
if not parents:
name = obj.Name
label = obj.Label
if first:
FreeCAD.ActiveDocument.openTransaction(
"Delete materials"
)
first = False
FreeCAD.Console.PrintMessage(
translate("BIM", "Deleting unused material")
+ " "
+ label
+ "\n"
)
FreeCAD.ActiveDocument.removeObject(name)
if not first:
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
self.rescan(rebuild=True)
def onDuplicate(self):
if self.dlg:
item = self.dlg.matList.currentItem()
if item:
oldmat = FreeCAD.ActiveDocument.getObject(item.toolTip())
if oldmat:
import Arch
newmat = Arch.makeMaterial()
newmat.Label = item.text()
newmat.Material = oldmat.Material
FreeCAD.ActiveDocument.recompute()
i = QtGui.QListWidgetItem(
self.createIcon(newmat), newmat.Label, self.dlg.matList
)
i.setToolTip(newmat.Name)
i.setFlags(i.flags() | QtCore.Qt.ItemIsEditable)
self.dlg.matList.setCurrentItem(i)
self.rescan()
def onStartRename(self):
if self.dlg:
item = self.dlg.matList.currentItem()
if item:
self.dlg.matList.editItem(item)
def onEndRename(self, item):
obj = FreeCAD.ActiveDocument.getObject(item.toolTip())
if obj:
if obj.Label != item.text():
obj.Label = item.text()
def onMergeTo(self):
import FreeCADGui
if self.dlg:
item = self.dlg.matList.currentItem()
if item:
oldmat = FreeCAD.ActiveDocument.getObject(item.toolTip())
# load dialog
form = FreeCADGui.PySideUic.loadUi(
os.path.join(os.path.dirname(__file__), "dialogListWidget.ui")
)
# center the dialog over FreeCAD window
mw = FreeCADGui.getMainWindow()
form.move(
mw.frameGeometry().topLeft()
+ mw.rect().center()
- form.rect().center()
)
form.setWindowTitle(translate("BIM", "Select material to merge to"))
form.setWindowIcon(QtGui.QIcon(":/icons/Arch_Material.svg"))
for i in range(self.dlg.matList.count()):
oit = self.dlg.matList.item(i)
if oit != item:
nit = QtGui.QListWidgetItem(
oit.icon(), oit.text(), form.listWidget
)
nit.setToolTip(oit.toolTip())
result = form.exec_()
if result:
mergeto = form.listWidget.currentItem()
if mergeto:
mergemat = FreeCAD.ActiveDocument.getObject(mergeto.toolTip())
if oldmat and mergemat:
parents = [
parent
for parent in oldmat.InList
if (
hasattr(parent, "Material")
and (parent.Material == oldmat)
)
]
name = oldmat.Name
FreeCAD.ActiveDocument.openTransaction("Merge material")
for parent in parents:
parent.Material = mergemat
FreeCAD.ActiveDocument.removeObject(name)
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
self.rescan()
else:
return
def onDelete(self):
if self.dlg:
item = self.dlg.matList.currentItem()
if item:
obj = FreeCAD.ActiveDocument.getObject(item.toolTip())
if obj:
parents = [
parent
for parent in obj.InList
if not parent.isDerivedFrom("App::DocumentObjectGroup")
]
if parents:
QtGui.QMessageBox.information(
None,
"",
translate("BIM", "This material is used by:")
+ " "
+ ",".join([p.Label for p in parents]),
)
else:
self.dlg.matList.takeItem(self.dlg.matList.currentRow())
name = obj.Name
FreeCAD.ActiveDocument.openTransaction("Delete material")
FreeCAD.ActiveDocument.removeObject(name)
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
self.rescan()
def onCreate(self):
import FreeCADGui
if self.dlg:
self.dlg.hide()
FreeCADGui.runCommand("Arch_Material")
def onMulti(self):
import FreeCADGui
if self.dlg:
self.dlg.hide()
FreeCADGui.runCommand("Arch_MultiMaterial")
def onAccept(self, item=None):
if self.dlg:
item = self.dlg.matList.currentItem()
if item and self.dlg.objects:
mat = FreeCAD.ActiveDocument.getObject(item.toolTip())
if mat:
if self.dlg.objects:
FreeCAD.ActiveDocument.openTransaction("Change material")
for obj in self.dlg.objects:
if hasattr(obj, "StepId"):
import ifc_tools
ifc_tools.set_material(mat, obj)
else:
obj.Material = mat
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM")
p.SetInt("BimMaterialDialogWidth", self.dlg.width())
p.SetInt("BimMaterialDialogHeight", self.dlg.height())
from DraftGui import todo
todo.delay(self.dlg.hide, None)
def onReject(self):
if self.dlg:
self.dlg.hide()
def onUpArrow(self):
if self.dlg:
i = self.dlg.matList.currentRow()
if i > 0:
self.dlg.matList.setCurrentRow(i - 1)
else:
self.dlg.matList.setCurrentRow(self.dlg.matList.count() - 1)
def onDownArrow(self):
if self.dlg:
i = self.dlg.matList.currentRow()
if i < self.dlg.matList.count() - 1:
self.dlg.matList.setCurrentRow(i + 1)
else:
self.dlg.matList.setCurrentRow(0)
def onClearSearch(self):
if self.dlg:
self.dlg.searchBox.setText("")
def onSearch(self, text):
from PySide import QtCore, QtGui
self.dlg.matList.clear()
for o in self.dlg.materials:
if text.lower() in o.Label.lower():
i = QtGui.QListWidgetItem(self.createIcon(o), o.Label, self.dlg.matList)
i.setToolTip(o.Name)
i.setFlags(i.flags() | QtCore.Qt.ItemIsEditable)
self.dlg.matList.setCurrentItem(i)
def rescan(self, rebuild=False):
from PySide import QtCore, QtGui
if self.dlg:
self.dlg.materials = []
for o in FreeCAD.ActiveDocument.Objects:
if o.isDerivedFrom("App::MaterialObjectPython") or (
(o.TypeId == "App::FeaturePython") and hasattr(o, "Materials")
):
self.dlg.materials.append(o)
if rebuild:
c = self.dlg.matList.currentItem()
name = None
if c:
name = c.toolTip()
elif len(self.dlg.objects) == 1:
if getattr(self.dlg.objects[0], "Material", None):
if hasattr(self.dlg.objects[0].Material, "Name"):
name = self.dlg.objects[0].Material.Name
else:
name = "None"
self.dlg.matList.clear()
for o in self.dlg.materials:
i = QtGui.QListWidgetItem(
self.createIcon(o), o.Label, self.dlg.matList
)
i.setToolTip(o.Name)
i.setFlags(i.flags() | QtCore.Qt.ItemIsEditable)
if o.Name == name:
self.dlg.matList.setCurrentItem(i)
def createIcon(self, obj):
from PySide import QtCore, QtGui
if hasattr(obj, "Materials"):
return QtGui.QIcon(":/icons/Arch_Material_Multi.svg")
elif obj.ViewObject.Icon:
return obj.ViewObject.Icon
elif hasattr(obj, "Color"):
c = obj.Color
matcolor = QtGui.QColor(int(c[0] * 255), int(c[1] * 255), int(c[2] * 255))
darkcolor = QtGui.QColor(int(c[0] * 125), int(c[1] * 125), int(c[2] * 125))
im = QtGui.QImage(48, 48, QtGui.QImage.Format_ARGB32)
im.fill(QtCore.Qt.transparent)
pt = QtGui.QPainter(im)
pt.setPen(
QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine, QtCore.Qt.FlatCap)
)
# pt.setBrush(QtGui.QBrush(matcolor, QtCore.Qt.SolidPattern))
gradient = QtGui.QLinearGradient(0, 0, 48, 48)
gradient.setColorAt(0, matcolor)
gradient.setColorAt(1, darkcolor)
pt.setBrush(QtGui.QBrush(gradient))
pt.drawEllipse(6, 6, 36, 36)
pt.setPen(
QtGui.QPen(QtCore.Qt.white, 1, QtCore.Qt.SolidLine, QtCore.Qt.FlatCap)
)
pt.setBrush(QtGui.QBrush(QtCore.Qt.white, QtCore.Qt.SolidPattern))
pt.drawEllipse(12, 12, 12, 12)
pt.end()
px = QtGui.QPixmap.fromImage(im)
return QtGui.QIcon(px)
else:
return QtGui.QIcon(":/icons/Arch_Material.svg")