Skip to content

Commit 97cf1da

Browse files
committed
IfcOpenShell#2339 Read directly from raw data instead of using strings for IfcDiff output
1 parent 836a162 commit 97cf1da

5 files changed

Lines changed: 75 additions & 39 deletions

File tree

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ class DiffData:
3333

3434
@classmethod
3535
def load(cls):
36-
cls.data = {"diff_json": cls.diff_json(), "changes": cls.changes()}
36+
cls.data["diff_json"] = cls.diff_json()
37+
cls.data["total_added"] = cls.total_added()
38+
cls.data["total_deleted"] = cls.total_deleted()
39+
cls.data["total_changed"] = cls.total_changed()
40+
cls.data["changes"] = cls.changes()
3741
cls.is_loaded = True
3842

3943
@classmethod
@@ -48,6 +52,24 @@ def diff_json(cls):
4852
cls.diff = json.load(file)
4953
return cls.diff
5054

55+
@classmethod
56+
def total_added(cls):
57+
diff = cls.diff_json()
58+
if diff:
59+
return len(diff["added"])
60+
61+
@classmethod
62+
def total_deleted(cls):
63+
diff = cls.diff_json()
64+
if diff:
65+
return len(diff["deleted"])
66+
67+
@classmethod
68+
def total_changed(cls):
69+
diff = cls.diff_json()
70+
if diff:
71+
return len(diff["changed"].keys())
72+
5173
@classmethod
5274
def changes(cls):
5375
diff = cls.diff_json()
@@ -56,4 +78,9 @@ def changes(cls):
5678
element = tool.Ifc.get_entity(bpy.context.active_object)
5779
if not element or not hasattr(element, "GlobalId"):
5880
return {}
59-
return {k.upper().replace("_", " "): str(v) for k, v in diff["changed"].get(element.GlobalId, {}).items()}
81+
changes = {k.upper().replace("_", " "): str(v) for k, v in diff["changed"].get(element.GlobalId, {}).items()}
82+
if element.GlobalId in diff["added"]:
83+
changes["Added"] = True
84+
elif element.GlobalId in diff["deleted"]:
85+
changes["Deleted"] = True
86+
return changes

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@
1717
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import bpy
20+
import json
2021
import ifccsv
2122
import ifcopenshell
22-
import json
23+
import blenderbim.bim.handler
2324
import blenderbim.tool as tool
24-
from blenderbim.bim.ifc import IfcStore
2525

2626

2727
class SelectDiffJsonFile(bpy.types.Operator):
2828
bl_idname = "bim.select_diff_json_file"
2929
bl_label = "Select Diff JSON File"
3030
bl_options = {"REGISTER", "UNDO"}
3131
filepath: bpy.props.StringProperty(subtype="FILE_PATH")
32+
filter_glob: bpy.props.StringProperty(default="*.json", options={"HIDDEN"})
3233

3334
def execute(self, context):
3435
context.scene.DiffProperties.diff_json_file = self.filepath
@@ -72,9 +73,10 @@ class SelectDiffOldFile(bpy.types.Operator):
7273
bl_label = "Select Diff Old File"
7374
bl_options = {"REGISTER", "UNDO"}
7475
filepath: bpy.props.StringProperty(subtype="FILE_PATH")
76+
filter_glob: bpy.props.StringProperty(default="*.ifc", options={"HIDDEN"})
7577

7678
def execute(self, context):
77-
context.scene.DiffProperties.diff_old_file = self.filepath
79+
context.scene.DiffProperties.old_file = self.filepath
7880
return {"FINISHED"}
7981

8082
def invoke(self, context, event):
@@ -87,9 +89,10 @@ class SelectDiffNewFile(bpy.types.Operator):
8789
bl_label = "Select Diff New File"
8890
bl_options = {"REGISTER", "UNDO"}
8991
filepath: bpy.props.StringProperty(subtype="FILE_PATH")
92+
filter_glob: bpy.props.StringProperty(default="*.ifc", options={"HIDDEN"})
9093

9194
def execute(self, context):
92-
context.scene.DiffProperties.diff_new_file = self.filepath
95+
context.scene.DiffProperties.new_file = self.filepath
9396
return {"FINISHED"}
9497

9598
def invoke(self, context, event):
@@ -113,14 +116,14 @@ def execute(self, context):
113116
import ifcdiff
114117

115118
ifc_diff = ifcdiff.IfcDiff(
116-
context.scene.DiffProperties.diff_old_file,
117-
context.scene.DiffProperties.diff_new_file,
119+
context.scene.DiffProperties.old_file,
120+
context.scene.DiffProperties.new_file,
118121
self.filepath,
119122
[r.relationship for r in context.scene.DiffProperties.diff_relationships],
120123
context.scene.DiffProperties.diff_filter_elements,
121124
)
122-
diff = ifc_diff.diff()
125+
ifc_diff.diff()
123126
ifc_diff.export()
124127
context.scene.DiffProperties.diff_json_file = self.filepath
125-
context.scene.DiffProperties.diff_result = diff
128+
blenderbim.bim.handler.refresh_ui_data()
126129
return {"FINISHED"}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ class Relationships(PropertyGroup):
4444

4545

4646
class DiffProperties(PropertyGroup):
47-
diff_json_file: StringProperty(default="", name="Diff JSON File", update=update_diff_json_file)
48-
diff_old_file: StringProperty(default="", name="Diff Old IFC File")
49-
diff_new_file: StringProperty(default="", name="Diff New IFC File")
50-
diff_relationships: CollectionProperty(type=Relationships, name="Diff Relationships")
51-
diff_filter_elements: StringProperty(default="", name="Diff Filter")
52-
diff_result: StringProperty(default="", name="Diff Result")
47+
diff_json_file: StringProperty(default="", name="JSON Output", update=update_diff_json_file)
48+
old_file: StringProperty(default="", name="Old IFC File")
49+
new_file: StringProperty(default="", name="New IFC File")
50+
diff_relationships: CollectionProperty(type=Relationships, name="Relationships")
51+
diff_filter_elements: StringProperty(default="", name="Filter")

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

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,55 +40,63 @@ def draw(self, context):
4040
layout.use_property_split = True
4141

4242
scene = context.scene
43-
bim_properties = scene.DiffProperties
43+
props = scene.DiffProperties
4444

4545
layout.label(text="IFC Diff Setup:")
4646

4747
row = layout.row(align=True)
48-
row.prop(bim_properties, "diff_old_file")
48+
row.prop(props, "old_file")
4949
row.operator("bim.select_diff_old_file", icon="FILE_FOLDER", text="")
5050

5151
row = layout.row(align=True)
52-
row.prop(bim_properties, "diff_new_file")
52+
row.prop(props, "new_file")
5353
row.operator("bim.select_diff_new_file", icon="FILE_FOLDER", text="")
5454

5555
row = layout.row(align=True)
56-
row.prop(bim_properties, "diff_relationships")
57-
row.context_pointer_set("bim_prop_group", bim_properties)
56+
row.prop(props, "diff_relationships")
57+
row.context_pointer_set("bim_prop_group", props)
5858
add = row.operator("bim.edit_blender_collection", icon="ADD", text="")
5959
add.option = "add"
6060
add.collection = "diff_relationships"
61-
62-
for index, r in enumerate(bim_properties.diff_relationships):
61+
62+
for index, r in enumerate(props.diff_relationships):
6363
row = layout.row(align=True)
64-
row.context_pointer_set("bim_prop_group", bim_properties)
64+
row.context_pointer_set("bim_prop_group", props)
6565
row.prop(r, "relationship", text=" ")
6666
remove = row.operator("bim.edit_blender_collection", icon="REMOVE", text="")
6767
remove.option = "remove"
6868
remove.collection = "diff_relationships"
6969
remove.index = index
70-
70+
7171
row = layout.row(align=True)
72-
row.prop(bim_properties, "diff_filter_elements")
72+
row.prop(props, "diff_filter_elements")
7373
row.operator("bim.ifc_selector", icon="FILTER", text="")
7474

7575
row = layout.row()
7676
row.operator("bim.execute_ifc_diff")
77-
if bim_properties.diff_result:
78-
row = layout.row()
79-
row.alignment = "CENTER"
80-
row.label(text=bim_properties.diff_result)
8177

82-
# TODO: show if there ifc diff operation is sucessful
8378
row = layout.row(align=True)
84-
row.prop(bim_properties, "diff_json_file")
79+
row.prop(props, "diff_json_file")
8580
row.operator("bim.select_diff_json_file", icon="FILE_FOLDER", text="")
8681
row.operator("bim.visualise_diff", icon="HIDE_OFF", text="")
87-
88-
if DiffData.data["changes"]:
82+
83+
if DiffData.data["diff_json"]:
8984
row = layout.row()
90-
row.label(text="Diff Results:")
85+
row.alignment = "CENTER"
86+
row.label(text=f"{DiffData.data['total_added']} added")
87+
row.label(text=f"{DiffData.data['total_deleted']} deleted")
88+
row.label(text=f"{DiffData.data['total_changed']} changed")
89+
90+
if DiffData.data["changes"]:
91+
box = layout.box()
92+
row = box.row()
93+
row.label(text="Active Object Changes:")
9194
for key, value in DiffData.data["changes"].items():
92-
row = layout.row()
93-
row.label(text=key)
94-
row.label(text=value)
95+
row = box.row()
96+
if key == "Added":
97+
icon = "ADD"
98+
elif key == "Deleted":
99+
icon = "X"
100+
else:
101+
icon = "GREASEPENCIL"
102+
row.label(text=key, icon=icon)

src/ifcdiff/ifcdiff.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ def diff(self):
121121
print(" - {} item(s) were changed either geometrically or with data".format(len(self.change_register.keys())))
122122
print("# Diff finished in {:.2f} seconds".format(time.time() - start))
123123
logging.disable(logging.NOTSET)
124-
return f"# Diff finished in {time.time() - start:.2f} seconds"
125124

126125
def export(self):
127126
with open(self.output_file, "w", encoding="utf-8") as diff_file:

0 commit comments

Comments
 (0)