From 75e64959990e97b5115d32bdca3105aa04eaac9c Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 27 May 2024 16:16:57 +0500 Subject: [PATCH] =?UTF-8?q?load=20and=20link=20ifc=20files=20by=20drag'n'd?= =?UTF-8?q?rop=20=F0=9F=A5=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Example - https://imgur.com/a/kIiqk3R Now you can drag'n'drop files to Blender and they will get loaded as projects (currently there is no safe check whether current project is saved) or you can hold ALT while drag'n'dropping and link single or multiple IFC files. Actually, it's possible to set it up that way so multiple files won't need ALT modifier (as we can't load multiple projects, only link them) but I've kept ALT modifier for that case too to avoid confusing situations. Blender 4.1+ only as there wasn't bpy.types.FileHandler before. --- .../blenderbim/bim/module/project/__init__.py | 5 +++ .../blenderbim/bim/module/project/operator.py | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/blenderbim/blenderbim/bim/module/project/__init__.py b/src/blenderbim/blenderbim/bim/module/project/__init__.py index e0e8b02d0e..46d97ccac9 100644 --- a/src/blenderbim/blenderbim/bim/module/project/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/project/__init__.py @@ -76,6 +76,11 @@ classes = ( gizmo.ClippingPlane, ) +if bpy.app.version >= (4, 1, 0): + classes += ( + operator.IFCFileHandlerOperator, + operator.BIM_FH_import_ifc, + ) addon_keymaps = [] diff --git a/src/blenderbim/blenderbim/bim/module/project/operator.py b/src/blenderbim/blenderbim/bim/module/project/operator.py index 94bec7dabf..8f44c76f8f 100644 --- a/src/blenderbim/blenderbim/bim/module/project/operator.py +++ b/src/blenderbim/blenderbim/bim/module/project/operator.py @@ -2085,3 +2085,46 @@ class BIM_OT_load_clipping_planes(bpy.types.Operator): obj.location = values["location"] obj.rotation_euler = values["rotation"] return {"FINISHED"} + + +if bpy.app.version >= (4, 1, 0): + + class IFCFileHandlerOperator(bpy.types.Operator): + bl_idname = "bim.load_project_file_handler" + bl_label = "Import .ifc file" + bl_options = {"REGISTER", "UNDO", "INTERNAL"} + + directory: bpy.props.StringProperty(subtype="FILE_PATH", options={"SKIP_SAVE", "HIDDEN"}) + files: bpy.props.CollectionProperty(type=bpy.types.OperatorFileListElement, options={"SKIP_SAVE", "HIDDEN"}) + + def invoke(self, context, event): + # Keeping code in .invoke() as we'll probably add some + # popup windows later. + + # `files` contain only .ifc files. + filepath = Path(self.directory) + # If user is just drag'n'dropping a single file -> load it as a new project, + # if they're holding ALT -> link the file/files to the current project. + if event.alt: + # Passing self.files directly results in TypeError. + serialized_files = [{"name": f.name} for f in self.files] + return bpy.ops.bim.link_ifc(directory=self.directory, files=serialized_files) + else: + if len(self.files) == 1: + return bpy.ops.bim.load_project(filepath=(filepath / self.files[0].name).as_posix()) + else: + self.report( + {"INFO"}, + "To link multiple IFC files hold ALT while drag'n'dropping them.", + ) + return {"FINISHED"} + + class BIM_FH_import_ifc(bpy.types.FileHandler): + bl_label = "IFC File Handler" + bl_import_operator = IFCFileHandlerOperator.bl_idname + bl_file_extensions = ".ifc" + + # FileHandler won't work without poll_drop defined. + @classmethod + def poll_drop(cls, context): + return True