Skip to content

Commit 226c943

Browse files
committed
IfcDiff now supports file objects as inputs instead of filepaths
1 parent 548a297 commit 226c943

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

src/blenderbim/blenderbim/bim/module/diff/operator.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,14 @@ def invoke(self, context, event):
115115
def execute(self, context):
116116
import ifcdiff
117117

118-
ifc_diff = ifcdiff.IfcDiff(
119-
context.scene.DiffProperties.old_file,
120-
context.scene.DiffProperties.new_file,
121-
self.filepath,
122-
[r.relationship for r in context.scene.DiffProperties.diff_relationships],
123-
context.scene.DiffProperties.diff_filter_elements,
124-
)
118+
old = ifcopenshell.open(context.scene.DiffProperties.old_file)
119+
new = ifcopenshell.open(context.scene.DiffProperties.new_file)
120+
relationships = [r.relationship for r in context.scene.DiffProperties.diff_relationships]
121+
query = context.scene.DiffProperties.diff_filter_elements
122+
123+
ifc_diff = ifcdiff.IfcDiff(old, new, relationships=relationships, filter_elements=query)
125124
ifc_diff.diff()
126-
ifc_diff.export()
125+
ifc_diff.export(self.filepath)
127126
context.scene.DiffProperties.diff_json_file = self.filepath
128127
blenderbim.bim.handler.refresh_ui_data()
129128
return {"FINISHED"}

src/ifcdiff/ifcdiff.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,10 @@ class IfcDiff:
3838
3939
If you are using IfcDiff as a library, this is the class you should use.
4040
41-
:param old_file: Filepath to the old file
42-
:type old_file: string
43-
:param new_file: Filepath to the new file
44-
:type new_file: string
45-
:param output_file: Filepath to the output JSON file to store the diff
46-
results
47-
:type output_file: string
41+
:param old: IFC file object for the old model
42+
:type old: ifcopenshell.file.file
43+
:param new: IFC file object for the old model
44+
:type new: ifcopenshell.file.file
4845
:param relationships: List of relationships to check. An empty list means
4946
that only geometry and attributes are compared.
5047
:type relationships: list[string]
@@ -66,10 +63,9 @@ class IfcDiff:
6663
ifc_diff.export()
6764
"""
6865

69-
def __init__(self, old_file, new_file, output_file, relationships=None, is_shallow=True, filter_elements=None):
70-
self.old_file = old_file
71-
self.new_file = new_file
72-
self.output_file = output_file
66+
def __init__(self, old, new, relationships=None, is_shallow=True, filter_elements=None):
67+
self.old = old
68+
self.new = new
7369
self.change_register = {}
7470
self.representation_ids = {}
7571
self.relationships = relationships
@@ -78,9 +74,7 @@ def __init__(self, old_file, new_file, output_file, relationships=None, is_shall
7874
self.filter_elements = filter_elements
7975

8076
def diff(self):
81-
print("# IFC Diff")
8277
logging.disable(logging.CRITICAL)
83-
self.load()
8478

8579
self.precision = self.get_precision()
8680

@@ -97,11 +91,6 @@ def diff(self):
9791
same_elements = new_elements - self.added_elements
9892
total_same_elements = len(same_elements)
9993

100-
print(" - {} item(s) were deleted".format(len(self.deleted_elements)))
101-
print(" - {} item(s) were added".format(len(self.added_elements)))
102-
print(" - {} item(s) were retained between the old and new IFC file".format(total_same_elements))
103-
104-
start = time.time()
10594
total_diffed = 0
10695

10796
for global_id in same_elements:
@@ -118,12 +107,10 @@ def diff(self):
118107
if diff:
119108
self.change_register.setdefault(new.GlobalId, {}).update({"geometry_changed": True})
120109

121-
print(" - {} item(s) were changed either geometrically or with data".format(len(self.change_register.keys())))
122-
print("# Diff finished in {:.2f} seconds".format(time.time() - start))
123110
logging.disable(logging.NOTSET)
124111

125-
def export(self):
126-
with open(self.output_file, "w", encoding="utf-8") as diff_file:
112+
def export(self, path):
113+
with open(path, "w", encoding="utf-8") as diff_file:
127114
json.dump(
128115
{
129116
"added": list(self.added_elements),
@@ -134,12 +121,6 @@ def export(self):
134121
indent=4,
135122
)
136123

137-
def load(self):
138-
print("Loading old file ...")
139-
self.old = ifcopenshell.open(self.old_file)
140-
print("Loading new file ...")
141-
self.new = ifcopenshell.open(self.new_file)
142-
143124
def get_precision(self):
144125
contexts = [c for c in self.new.by_type("IfcGeometricRepresentationContext") if c.ContextType == "Model"]
145126
if contexts:
@@ -295,6 +276,24 @@ def give_up_diffing(self, level, diff_instance) -> bool:
295276
)
296277
args = parser.parse_args()
297278

298-
ifc_diff = IfcDiff(args.old, args.new, args.output, args.relationships.split())
279+
print("# IFC Diff")
280+
281+
start = time.time()
282+
print("Loading old file ...")
283+
old = ifcopenshell.open(args.old)
284+
print("Loading new file ...")
285+
new = ifcopenshell.open(args.new)
286+
287+
print("# Loading finished in {:.2f} seconds".format(time.time() - start))
288+
start = time.time()
289+
290+
ifc_diff = IfcDiff(old, new, args.relationships.split())
299291
ifc_diff.diff()
300-
ifc_diff.export()
292+
293+
print(" - {} item(s) were deleted".format(len(ifc_diff.deleted_elements)))
294+
print(" - {} item(s) were added".format(len(ifc_diff.added_elements)))
295+
print(" - {} item(s) were changed".format(len(ifc_diff.change_register.keys())))
296+
297+
print("# Diff finished in {:.2f} seconds".format(time.time() - start))
298+
299+
ifc_diff.export(args.output)

0 commit comments

Comments
 (0)