-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ifc_diff.py
78 lines (66 loc) · 3.4 KB
/
ifc_diff.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
# ***************************************************************************
# * *
# * 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 *
# * *
# ***************************************************************************
"""Diffing tool for NativeIFC project objects"""
import os
import difflib
import FreeCAD
import FreeCADGui
import ifcopenshell
translate = FreeCAD.Qt.translate
def get_diff(proj):
"""Obtains a diff between the current version and the saved version of a project"""
if not getattr(proj, "IfcFilePath", None):
old = []
else:
# cannot use open() here as it gives different encoding
# than ifcopenshell and diff does not work
f = ifcopenshell.open(proj.IfcFilePath)
old = f.wrapped_data.to_string().split("\n")
new = proj.Proxy.ifcfile.wrapped_data.to_string().split("\n")
# diff = difflib.HtmlDiff().make_file(old,new) # UGLY
res = [l for l in difflib.unified_diff(old, new, lineterm="")]
res = [l for l in res if l.startswith("+") or l.startswith("-")]
res = [l for l in res if not l.startswith("+++") and not l.startswith("---")]
return "\n".join(res)
def htmlize(diff):
"""Returns an HTML version of a diff list"""
html = "<html><body>\n"
if diff:
diff = diff.split("\n")
for l in diff:
if l.startswith("+"):
html += "<span style='color:green;'>" + l[:100] + "</span><br/>\n"
elif l.startswith("-"):
html += "<span style='color:red;'>" + l[:100] + "</span><br/>\n"
else:
html += l + "<br/>\n"
else:
html += translate("BIM", "No changes to display.") + "<br/>\n"
html += "</body></html>"
return html
def show_diff(diff):
"""Shows a dialog showing the diff contents"""
b = os.path.dirname(__file__)
dlg = FreeCADGui.PySideUic.loadUi(os.path.join(b, "ui", "dialogDiff.ui"))
dlg.textEdit.setStyleSheet("background-color: white; color: black;")
dlg.textEdit.setHtml(htmlize(diff))
result = dlg.exec_()