From 07bd99d46188aa7b56cc1fec90d674120612856b Mon Sep 17 00:00:00 2001 From: rileywong311 Date: Wed, 26 Jul 2023 16:11:05 -0700 Subject: [PATCH] Add "remove brick relation" operator --- .../blenderbim/bim/module/brick/__init__.py | 1 + .../blenderbim/bim/module/brick/data.py | 30 +++++++++++-------- .../blenderbim/bim/module/brick/operator.py | 15 +++++++++- .../blenderbim/bim/module/brick/prop.py | 4 ++- .../blenderbim/bim/module/brick/ui.py | 19 +++++++++--- src/blenderbim/blenderbim/core/brick.py | 4 +++ src/blenderbim/blenderbim/tool/brick.py | 7 +++++ 7 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/brick/__init__.py b/src/blenderbim/blenderbim/bim/module/brick/__init__.py index f2f3e03d6f..3f10093136 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/brick/__init__.py @@ -36,6 +36,7 @@ classes = ( operator.SerializeBrick, operator.AddBrickNamespace, operator.SetBrickListRoot, + operator.RemoveBrickRelation, prop.Brick, prop.BIMBrickProperties, ui.BIM_PT_brickschema, diff --git a/src/blenderbim/blenderbim/bim/module/brick/data.py b/src/blenderbim/blenderbim/bim/module/brick/data.py index b8bbdd67f2..b277ce3ed6 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/data.py +++ b/src/blenderbim/blenderbim/bim/module/brick/data.py @@ -80,28 +80,32 @@ class BrickschemaData: ) ) for row in query: - predicate = row.get("predicate").toPython().split("#")[-1] + predicate = row.get("predicate") + predicate_name = predicate.toPython().split("#")[-1] object = row.get("object") + object_name = object.toPython().split("#")[-1] results.append( { "predicate": predicate, - "object": object.toPython().split("#")[-1], + "predicate_name": predicate_name, + "object": object, + "object_name": object_name, "is_uri": isinstance(object, URIRef), "object_uri": object.toPython(), "is_globalid": predicate == "globalID", } ) - if isinstance(row.get("object"), BNode): - for s, p, o in BrickStore.graph.triples((object, None, None)): - results.append( - { - "predicate": predicate + ":" + p.toPython().split("#")[-1], - "object": o.toPython().split("#")[-1], - "is_uri": isinstance(o, URIRef), - "object_uri": o.toPython(), - "is_globalid": p.toPython().split("#")[-1] == "globalID", - } - ) + # if isinstance(row.get("object"), BNode): + # for s, p, o in BrickStore.graph.triples((object, None, None)): + # results.append( + # { + # "predicate": predicate + ":" + p.toPython().split("#")[-1], + # "object": o.toPython().split("#")[-1], + # "is_uri": isinstance(o, URIRef), + # "object_uri": o.toPython(), + # "is_globalid": p.toPython().split("#")[-1] == "globalID", + # } + # ) return results diff --git a/src/blenderbim/blenderbim/bim/module/brick/operator.py b/src/blenderbim/blenderbim/bim/module/brick/operator.py index 381147725d..8f7073533f 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/operator.py +++ b/src/blenderbim/blenderbim/bim/module/brick/operator.py @@ -263,4 +263,17 @@ class SetBrickListRoot(bpy.types.Operator, Operator): def _execute(self, context): root = context.scene.BIMBrickProperties.brick_list_root - core.set_brick_list_root(tool.Brick, brick_root=root) \ No newline at end of file + core.set_brick_list_root(tool.Brick, brick_root=root) + + +class RemoveBrickRelation(bpy.types.Operator, Operator): + bl_idname = "bim.remove_brick_relation" + bl_label = "Remove Relation" + bl_options = {"REGISTER", "UNDO"} + predicate: bpy.props.StringProperty(name="Relation") + object: bpy.props.StringProperty(name="Object") + + def _execute(self, context): + props = context.scene.BIMBrickProperties + brick = props.bricks[props.active_brick_index] + core.remove_brick_relation(tool.Brick, brick_uri=brick.uri, predicate=self.predicate, object=self.object) \ No newline at end of file diff --git a/src/blenderbim/blenderbim/bim/module/brick/prop.py b/src/blenderbim/blenderbim/bim/module/brick/prop.py index 87e882b364..2e04ff84cf 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/prop.py +++ b/src/blenderbim/blenderbim/bim/module/brick/prop.py @@ -75,4 +75,6 @@ class BIMBrickProperties(PropertyGroup): new_brick_namespace_alias: StringProperty(name="New Brick Namespace Alias") new_brick_namespace_uri: StringProperty(name="New Brick Namespace URI") brick_list_root: EnumProperty(name="Brick List Root", items=get_brick_roots) - brick_entity_create_type: EnumProperty(name="Brick Entity Types", items=get_brick_roots) \ No newline at end of file + brick_entity_create_type: EnumProperty(name="Brick Entity Types", items=get_brick_roots) + brick_create_relations_toggled: BoolProperty(name="Brick Create Relations Toggled", default=False) + brick_edit_relations_toggled: BoolProperty(name="Brick Edit Relations Toggled", default=False) \ No newline at end of file diff --git a/src/blenderbim/blenderbim/bim/module/brick/ui.py b/src/blenderbim/blenderbim/bim/module/brick/ui.py index a7759b4ecf..0e442e5770 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/ui.py +++ b/src/blenderbim/blenderbim/bim/module/brick/ui.py @@ -107,16 +107,27 @@ class BIM_PT_brickschema(Panel): self.layout.template_list("BIM_UL_bricks", "", self.props, "bricks", self.props, "active_brick_index") + if BrickschemaData.data["relations"]: + row = self.layout.row(align=True) + col = row.column() + col.alignment = "RIGHT" + row.prop(data=self.props, property="brick_create_relations_toggled", text="", icon="OUTLINER_DATA_GP_LAYER") + row.prop(data=self.props, property="brick_edit_relations_toggled", text="", icon="TOOL_SETTINGS") + for relation in BrickschemaData.data["relations"]: row = self.layout.row(align=True) - row.label(text=relation["predicate"]) - row.label(text=relation["object"]) - if relation["is_uri"]: + row.label(text=relation["predicate_name"]) + row.label(text=relation["object_name"]) + if self.props.brick_edit_relations_toggled and relation["predicate_name"] != "type": + op = row.operator("bim.remove_brick_relation", text="", icon="UNLINKED") + op.predicate = relation["predicate"] + op.object = relation["object"] + if relation["is_uri"] and relation["predicate_name"] != "type": op = row.operator("bim.view_brick_item", text="", icon="DISCLOSURE_TRI_RIGHT") op.item = relation["object_uri"] if relation["is_globalid"]: op = row.operator("bim.select_global_id", icon="RESTRICT_SELECT_OFF", text="") - op.global_id = relation["object"] + op.global_id = relation["object_name"] class BIM_PT_ifc_brickschema_references(Panel): diff --git a/src/blenderbim/blenderbim/core/brick.py b/src/blenderbim/blenderbim/core/brick.py index 9a8c4409ef..ab668d597d 100644 --- a/src/blenderbim/blenderbim/core/brick.py +++ b/src/blenderbim/blenderbim/core/brick.py @@ -126,3 +126,7 @@ def set_brick_list_root(brick, brick_root=None): brick.set_active_brick_class(brick_root) brick.clear_breadcrumbs() + +def remove_brick_relation(brick, brick_uri=None, predicate=None, object=None): + brick.remove_relation(brick_uri, predicate, object) + brick.run_refresh_brick_viewer() \ No newline at end of file diff --git a/src/blenderbim/blenderbim/tool/brick.py b/src/blenderbim/blenderbim/tool/brick.py index 392a673c9a..21eca756cb 100644 --- a/src/blenderbim/blenderbim/tool/brick.py +++ b/src/blenderbim/blenderbim/tool/brick.py @@ -104,6 +104,13 @@ class Brick(blenderbim.core.tool.Brick): ns_brick = Namespace("https://brickschema.org/schema/Brick#") BrickStore.graph.add((URIRef(source), ns_brick["feeds"], URIRef(destination))) + @classmethod + def remove_relation(cls, brick_uri, predicate, object): + if BrickStore.graph.triples((brick_uri, predicate, object)): + with BrickStore.new_changeset() as cs: + for triple in BrickStore.graph.triples((brick_uri, predicate, object)): + cs.remove(triple) + @classmethod def clear_brick_browser(cls): bpy.context.scene.BIMBrickProperties.bricks.clear()