forked from yorikvanhavre/BIM_Workbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BimDiff.py
363 lines (318 loc) · 22 KB
/
BimDiff.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
#***************************************************************************
#* *
#* 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 *
#* *
#***************************************************************************
"""This module contains FreeCAD commands for the BIM workbench"""
import os
import FreeCAD
import Draft
from BimTranslateUtils import *
def QT_TRANSLATE_NOOP(ctx,txt): return txt # dummy function for the QT translator
class BIM_Diff:
def GetResources(self):
return {'Pixmap' : os.path.join(os.path.dirname(__file__),"icons","BIM_Diff.svg"),
'MenuText': QT_TRANSLATE_NOOP("BIM_Diff", "IFC Diff"),
'ToolTip' : QT_TRANSLATE_NOOP("BIM_Diff", "Shows the difference between two IFC-based documents"),
}
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
def Activated(self):
# you need two documents open, containing BIM objects with same IDs
# make the main doc the active one before running this script!
# what will be compared: IDs, geometry, materials. Everything else is discarded.
MOVE_TOLERANCE = 0.2 # the max allowed move in mm
VOL_TOLERANCE = 250 # the max allowed volume diff in mm^3
import FreeCADGui
from PySide import QtCore,QtGui
documents = FreeCAD.listDocuments()
if len(documents) == 2:
reply = QtGui.QMessageBox.question(None, "", translate("BIM","The document currently viewed must be your main one. The other contains newer objects that you wish to merge into this one. Make sure only the objects you wish to compare are visible in both. Proceed?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
activedoc = FreeCAD.ActiveDocument
if list(documents.keys())[0] == activedoc.Name:
otherdoc = list(documents.values())[1]
else:
otherdoc = list(documents.values())[0]
# build lists of BIM objects with IFC ID
objswithoutid = [] # let's try to match these later on
activedocids = {} # main document, the original freecad one
for obj in activedoc.Objects:
if hasattr(obj,"IfcData") and obj.ViewObject.Visibility:
if "IfcUID" in obj.IfcData:
activedocids[obj.IfcData["IfcUID"]] = obj
elif obj.isDerivedFrom("Part::Feature"): # discard BuildingParts
objswithoutid.append(obj)
otherdocids = {} # other doc to be merged to the main one
for obj in otherdoc.Objects:
if hasattr(obj,"IfcData") and obj.ViewObject.Visibility:
if "IfcUID" in obj.IfcData:
otherdocids[obj.IfcData["IfcUID"]] = obj
toselect = [] # objects to select when finished
additions = [] # objects added
subtractions = [] # objects subtracted
modified = [] # objects modified
moved = [] # objects moved
matchanged = [] # object is same, but material changed
matchangedghost = [] # store shapes of objects whose material has changed to print a blue ghost later on
renamed = {} # object label changes
propertieschanged = {} # objects whose IFC properties are different
for id,obj in otherdocids.items():
if id in activedocids:
# this object already exists
mainobj = activedocids[id]
if obj.Label != mainobj.Label:
# object has a different name
renamed[mainobj.Name] = obj.Label
if obj.IfcProperties and (obj.IfcProperties != mainobj.IfcProperties):
# properties have changed
propertieschanged[id] = obj.IfcProperties
if hasattr(obj,"Shape") and hasattr(mainobj,"Shape"):
v = abs(obj.Shape.Volume - mainobj.Shape.Volume)
if v < VOL_TOLERANCE:
# identical volume
l = (obj.Shape.BoundBox.Center.sub(mainobj.Shape.BoundBox.Center)).Length
if l < MOVE_TOLERANCE:
# identical position
if abs(obj.Shape.BoundBox.XMin - mainobj.Shape.BoundBox.XMin) < MOVE_TOLERANCE and \
abs(obj.Shape.BoundBox.YMin - mainobj.Shape.BoundBox.YMin) < MOVE_TOLERANCE and \
abs(obj.Shape.BoundBox.YMin - mainobj.Shape.BoundBox.YMin) < MOVE_TOLERANCE:
# same boundbox
if hasattr(obj,"Material") and hasattr(mainobj,"Material") and (obj.Material and mainobj.Material and (obj.Material.Label == mainobj.Material.Label)) or (obj.Material == mainobj.Material):
# same material names
obj.ViewObject.hide()
else:
print("Object",mainobj.Label,"material has changed")
obj.ViewObject.hide() # we hide these objects since the shape hasn't changed but we keep their shapes
matchangedghost.append(obj.Shape)
matchanged.append(obj)
else:
print("Object",mainobj.Label,"shape bound box has changed")
toselect.append(obj)
modified.append(obj)
else:
print("Object",mainobj.Label,"position has moved by",l,"mm")
toselect.append(obj)
moved.append(obj)
else:
print("Object",mainobj.Label,"shape has changed by",v,"mm^3")
toselect.append(obj)
modified.append(obj)
else:
print("Object",mainobj.Label,"one of the objects has no shape")
toselect.append(obj)
else:
print("Object",obj.Label,"doesn't exist yet in main doc")
toselect.append(obj)
additions.append(obj)
for id,obj in activedocids.items():
if not id in otherdocids:
if obj.isDerivedFrom("Part::Feature"): # don't count building parts
print("Object",obj.Label,"doesn't exist anymore in new doc")
subtractions.append(obj)
# try to find our objects without ID
newids = {}
for obj in objswithoutid:
for id,otherobj in otherdocids.items():
if not id in activedocids:
if abs(otherobj.Shape.Volume - obj.Shape.Volume) < VOL_TOLERANCE:
if (otherobj.Shape.BoundBox.Center.sub(obj.Shape.BoundBox.Center)).Length < MOVE_TOLERANCE:
if abs(obj.Shape.BoundBox.XMin - otherobj.Shape.BoundBox.XMin) < MOVE_TOLERANCE and \
abs(obj.Shape.BoundBox.YMin - otherobj.Shape.BoundBox.YMin) < MOVE_TOLERANCE and \
abs(obj.Shape.BoundBox.YMin - otherobj.Shape.BoundBox.YMin) < MOVE_TOLERANCE:
# shapes are identical. It's the same object!
newids[obj.Name] = id
break
else:
print("Object",obj.Label,"has no ID and wasn't found in the new doc")
subtractions.append(obj)
matnames = {} # existing materials
for obj in activedoc.Objects:
if Draft.getType(obj) == "Material":
matnames[obj.Label] = obj
newmats = {} # new materials
for obj in otherdoc.Objects:
if Draft.getType(obj) == "Material":
if not obj.Label in matnames:
print("Material",obj.Label,"doesn't exist in main doc")
toselect.append(obj)
newmats[obj.Label] = obj
if newmats:
group = otherdoc.addObject("App::DocumentObjectGroup","New_materials")
for newmat in newmats.values():
group.addObject(newmat)
if toselect:
FreeCAD.setActiveDocument(otherdoc.Name)
FreeCAD.ActiveDocument=FreeCADGui.getDocument(otherdoc.Name)
FreeCADGui.ActiveDocument=FreeCADGui.getDocument(otherdoc.Name)
FreeCADGui.Selection.clearSelection()
for obj in toselect:
FreeCADGui.Selection.addSelection(obj)
if additions:
shape = Part.makeCompound([a.Shape for a in additions])
obj = activedoc.addObject("Part::Feature","Additions")
obj.Shape = shape
obj.ViewObject.LineWidth = 5
obj.ViewObject.LineColor = (0.0,1.0,0.0)
obj.ViewObject.ShapeColor = (0.0,1.0,0.0)
obj.ViewObject.Transparency = 60
if subtractions:
shape = Part.makeCompound([s.Shape for s in subtractions])
obj = activedoc.addObject("Part::Feature","Subtractions")
obj.Shape = shape
obj.ViewObject.LineWidth = 5
obj.ViewObject.LineColor = (1.0,0.0,0.0)
obj.ViewObject.ShapeColor = (1.0,0.0,0.0)
obj.ViewObject.Transparency = 60
if modified:
shape = Part.makeCompound([m.Shape for m in modified])
obj = activedoc.addObject("Part::Feature","Modified")
obj.Shape = shape
obj.ViewObject.LineWidth = 5
obj.ViewObject.LineColor = (1.0,0.5,0.0)
obj.ViewObject.ShapeColor = (1.0,0.5,0.0)
obj.ViewObject.Transparency = 60
if moved:
shape = Part.makeCompound([m.Shape for m in moved])
obj = activedoc.addObject("Part::Feature","Moved")
obj.Shape = shape
obj.ViewObject.LineWidth = 5
obj.ViewObject.LineColor = (1.0,1.0,0.0)
obj.ViewObject.ShapeColor = (1.0,1.0,0.0)
obj.ViewObject.Transparency = 60
if matchangedghost:
shape = Part.makeCompound(matchangedghost)
obj = otherdoc.addObject("Part::Feature","Material_changed")
obj.Shape = shape
obj.ViewObject.LineWidth = 1
obj.ViewObject.LineColor = (0.0,0.0,1.0)
obj.ViewObject.ShapeColor = (0.0,0.0,1.0)
obj.ViewObject.Transparency = 90
if matchanged:
reply = QtGui.QMessageBox.question(None, "", str(len(matchanged))+" "+translate("BIM","objects still have the same shape but have a different material. Do you wish to update them in the main document?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
for obj in matchanged:
mat = obj.Material
if mat:
mainobj = activedocids[obj.IfcData["IfcUID"]]
if mainobj.Material:
mainmatlabel = mainobj.Material.Label
else:
mainmatlabel = "None"
if mat.Label in matnames:
# the new material already exists, just change it
print("Changing material of",mainobj.Label,"from",mainmatlabel,"to",mat.Label)
mainobj.Material = matnames[mat.Label]
else:
# copy the material over
newmat = activedoc.addObject("App::MaterialObjectPython","Material")
newmat.Label = mat.Label
import ArchMaterial
ArchMaterial._ArchMaterial(newmat)
ArchMaterial._ViewProviderArchMaterial(newmat.ViewObject)
newmat.Material = mat.Material
print("Changing material of",mainobj.Label,"from",mainmatlabel,"to",newmat.Label)
mainobj.Material = newmat
matnames[newmat.Label] = newmat
if newids:
reply = QtGui.QMessageBox.question(None, "", str(len(newids))+" "+translate("BIM","objects have no IFC ID in the main document, but an identical object with an ID exists in the new document. Transfer these IDs to the original objects?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
for name,id in newids.items():
obj = activedoc.getObject(name)
if obj:
print("Transferring new id to object",obj.Label)
a = obj.IfcData
a["IfcUID"] = id
obj.IfcData = a
if renamed:
reply = QtGui.QMessageBox.question(None, "", str(len(renamed))+" "+translate("BIM","objects had their name changed. Rename them?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
for name,label in renamed.items():
obj = activedoc.getObject(name)
if obj:
print("Renaming object",obj.Label,"to",label)
obj.Label = label
if propertieschanged:
reply = QtGui.QMessageBox.question(None, "", str(len(propertieschanged))+" "+translate("BIM","objects had their properties changed. Update?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
for id,prop in propertieschanged.items():
obj = activedocids[id]
print("Updating properties of ",obj.Label)
obj.IfcProperties = prop
if moved:
reply = QtGui.QMessageBox.question(None, "", str(len(moved))+" "+translate("BIM","objects have their location changed. Move them to their new position?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
for obj in moved:
mainobj = activedocids[obj.IfcData["IfcUID"]]
otherobj = otherdocids[obj.IfcData["IfcUID"]]
delta = otherobj.Shape.BoundBox.Center.sub(mainobj.Shape.BoundBox.Center)
print("Moving object ",mainobj.Label)
Draft.move(mainobj,delta)
reply = QtGui.QMessageBox.question(None, "", translate("BIM","Do you wish to colorize the objects that have moved in yellow in the other file (to serve as a diff)?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
for obj in moved:
otherobj = otherdocids[obj.IfcData["IfcUID"]]
try:
otherobj.ViewObject.LineColor = (1.0,1.0,0.0)
otherobj.ViewObject.ShapeColor = (1.0,1.0,0.0)
otherobj.ViewObject.Transparency = 60
except AttributeError:
print(otherobj.Label,"cannot be colorized")
if modified:
reply = QtGui.QMessageBox.question(None, "", translate("BIM","Do you wish to colorize the objects that have been modified in orange in the other file (to serve as a diff)?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
for obj in modified:
otherobj = otherdocids[obj.IfcData["IfcUID"]]
try:
otherobj.ViewObject.LineColor = (1.0,0.5,0.0)
otherobj.ViewObject.ShapeColor = (1.0,0.5,0.0)
otherobj.ViewObject.Transparency = 60
except AttributeError:
print(otherobj.Label,"cannot be colorized")
if subtractions:
reply = QtGui.QMessageBox.question(None, "", str(len(subtractions))+" "+translate("BIM","objects don't exist anymore in the new document. Move them to a 'To Delete' group?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
group = activedoc.addObject("App::DocumentObjectGroup","ToDelete")
group.Label = "To Delete"
for obj in subtractions:
group.addObject(obj)
reply = QtGui.QMessageBox.question(None, "", translate("BIM","Do you wish to colorize the objects that have been removed in red in the other file (to serve as a diff)?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
for obj in subtractions:
otherobj = otherdoc.addObject("Part::Feature","Deleted")
otherobj.Shape = obj.Shape
otherobj.ViewObject.LineColor = (1.0,0.0,0.0)
otherobj.ViewObject.ShapeColor = (1.0,0.0,0.0)
otherobj.ViewObject.Transparency = 60
if additions:
reply = QtGui.QMessageBox.question(None, "", translate("BIM","Do you wish to colorize the objects that have been added in green in the other file (to serve as a diff)?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
for obj in additions:
otherobj = otherdocids[obj.IfcData["IfcUID"]]
try:
otherobj.ViewObject.LineColor = (0.0,1.0,0.0)
otherobj.ViewObject.ShapeColor = (0.0,1.0,0.0)
otherobj.ViewObject.Transparency = 60
except AttributeError:
print(otherobj.Label,"cannot be colorized")
else:
QtGui.QMessageBox.information(None,"",translate("BIM","You need two documents open to run this tool. One which is your main document, and one that contains new objects that you wish to compare against the existing one. Make sure only the objects you wish to compare in both documents are visible."))