From 6f8dcc637fd5fb4168b362e052f0048101537918 Mon Sep 17 00:00:00 2001 From: Gorgious56 Date: Wed, 17 Apr 2024 11:52:00 +0200 Subject: [PATCH] #4532 : You can now store clipping planes to a json file and restore them The file is stored along the ifc file --- .../blenderbim/bim/module/project/__init__.py | 2 + .../blenderbim/bim/module/project/operator.py | 60 +++++++++++++++++++ src/blenderbim/blenderbim/bim/ui.py | 4 +- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/blenderbim/blenderbim/bim/module/project/__init__.py b/src/blenderbim/blenderbim/bim/module/project/__init__.py index ceafc1fa70..ec1008cd33 100644 --- a/src/blenderbim/blenderbim/bim/module/project/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/project/__init__.py @@ -25,6 +25,8 @@ classes = ( operator.AppendLibraryElement, operator.AppendLibraryElementByQuery, operator.AssignLibraryDeclaration, + operator.BIM_OT_load_clipping_planes, + operator.BIM_OT_save_clipping_planes, operator.ChangeLibraryElement, operator.CreateClippingPlane, operator.CreateProject, diff --git a/src/blenderbim/blenderbim/bim/module/project/operator.py b/src/blenderbim/blenderbim/bim/module/project/operator.py index 8e96df2dcc..7449f8bcad 100644 --- a/src/blenderbim/blenderbim/bim/module/project/operator.py +++ b/src/blenderbim/blenderbim/bim/module/project/operator.py @@ -39,6 +39,8 @@ from blenderbim.bim.ifc import IfcStore from blenderbim.bim.ui import IFCFileSelector from blenderbim.bim import import_ifc from blenderbim.bim import export_ifc +from collections import defaultdict +import json from math import radians from pathlib import Path from mathutils import Vector, Matrix @@ -1999,3 +2001,61 @@ class FlipClippingPlane(bpy.types.Operator): obj.rotation_euler[0] += radians(180) context.view_layer.update() return {"FINISHED"} + + +CLIPPING_PLANES_FILE_NAME = "ClippingPlanes.json" # TODO un-hardcode := + + +class BIM_OT_save_clipping_planes(bpy.types.Operator): + bl_idname = "bim.save_clipping_planes" + bl_label = "Save Clipping Planes" + bl_description = "Save Clipping Planes to Disk" + bl_options = {"REGISTER", "UNDO"} + + @classmethod + def poll(cls, context): + if IfcStore.path: + return context.scene.BIMProjectProperties.clipping_planes + cls.poll_message_set("Please Save The IFC File") + + def execute(self, context): + clipping_planes_to_serialize = defaultdict(dict) + clipping_planes = context.scene.BIMProjectProperties.clipping_planes + for clipping_plane in clipping_planes: + obj = clipping_plane.obj + name = obj.name + clipping_planes_to_serialize[name]["location"] = obj.location[0:3] + clipping_planes_to_serialize[name]["rotation"] = obj.rotation_euler[0:3] + with open(Path(IfcStore.path).with_name(CLIPPING_PLANES_FILE_NAME), "w") as file: + json.dump(clipping_planes_to_serialize, file, indent=4) + return {"FINISHED"} + + +class BIM_OT_load_clipping_planes(bpy.types.Operator): + bl_idname = "bim.load_clipping_planes" + bl_label = "Load Clipping Planes" + bl_description = "Load Clipping Planes from Disk" + bl_options = {"REGISTER", "UNDO"} + + @classmethod + def poll(cls, context): + if filepath := IfcStore.path: + if Path(filepath).with_name(CLIPPING_PLANES_FILE_NAME).exists(): + return True + else: + cls.poll_message_set(f"No Clipping Planes File in Folder {filepath}") + else: + cls.poll_message_set("Please Save The IFC File") + + def execute(self, context): + bpy.data.batch_remove(context.scene.BIMProjectProperties.clipping_planes_objs) + context.scene.BIMProjectProperties.clipping_planes.clear() + with open(Path(IfcStore.path).with_name(CLIPPING_PLANES_FILE_NAME), "r") as file: + clipping_planes_dict = json.load(file) + for name, values in clipping_planes_dict.items(): + bpy.ops.bim.create_clipping_plane() + obj = context.scene.BIMProjectProperties.clipping_planes_objs[-1] + obj.name = name + obj.location = values["location"] + obj.rotation_euler = values["rotation"] + return {"FINISHED"} diff --git a/src/blenderbim/blenderbim/bim/ui.py b/src/blenderbim/blenderbim/bim/ui.py index bed2349191..a6345523b6 100644 --- a/src/blenderbim/blenderbim/bim/ui.py +++ b/src/blenderbim/blenderbim/bim/ui.py @@ -131,6 +131,8 @@ class BIM_PT_section_with_cappings(Panel): box = layout.box() header = box.row(align=True) header.label(text="Clipping Planes") + header.operator("bim.save_clipping_planes", text="", icon="EXPORT") + header.operator("bim.load_clipping_planes", text="", icon="IMPORT") header.operator("bim.create_clipping_plane", text="", icon="ADD") box.template_list( @@ -150,7 +152,7 @@ class BIM_PT_section_with_cappings(Panel): class BIM_UL_clipping_plane(bpy.types.UIList): def draw_item(self, context, layout, data, item, icon, active_data, active_propname): - if item: + if item and item.obj: obj = item.obj row = layout.row(align=True) row.prop(obj, "name", text="", emboss=False)