From deecd60ff1a155a8e013d9191144eb4e7ba68e33 Mon Sep 17 00:00:00 2001 From: rileywong311 Date: Fri, 28 Jul 2023 17:07:04 -0700 Subject: [PATCH] Implement basic Brick create relation --- .../blenderbim/bim/module/brick/__init__.py | 2 +- .../blenderbim/bim/module/brick/operator.py | 19 ++++++++------- .../blenderbim/bim/module/brick/prop.py | 19 ++++++++++++++- .../blenderbim/bim/module/brick/ui.py | 24 +++++++++++++++++-- src/blenderbim/blenderbim/core/brick.py | 4 ++-- src/blenderbim/blenderbim/tool/brick.py | 16 ++++++++++--- 6 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/brick/__init__.py b/src/blenderbim/blenderbim/bim/module/brick/__init__.py index 3f10093136..ca3e084128 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/brick/__init__.py @@ -21,7 +21,7 @@ from . import ui, prop, operator classes = ( operator.AddBrick, - operator.AddBrickFeed, + operator.AddBrickRelation, operator.AssignBrickReference, operator.CloseBrickProject, operator.ConvertBrickProject, diff --git a/src/blenderbim/blenderbim/bim/module/brick/operator.py b/src/blenderbim/blenderbim/bim/module/brick/operator.py index 8f7073533f..0bdddcb370 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/operator.py +++ b/src/blenderbim/blenderbim/bim/module/brick/operator.py @@ -132,19 +132,20 @@ class AddBrick(bpy.types.Operator, Operator): ) -class AddBrickFeed(bpy.types.Operator, Operator): - bl_idname = "bim.add_brick_feed" - bl_label = "Add Brick Feed" +class AddBrickRelation(bpy.types.Operator, Operator): + bl_idname = "bim.add_brick_relation" + bl_label = "Add Brick Relation" bl_options = {"REGISTER", "UNDO"} def _execute(self, context): - source = tool.Ifc.get_entity([o for o in context.selected_objects if o != context.active_object][0]) - destination = tool.Ifc.get_entity(context.active_object) - core.add_brick_feed( - tool.Ifc, + props = context.scene.BIMBrickProperties + brick = props.bricks[props.active_brick_index] + core.add_brick_relation( tool.Brick, - source=source, - destination=destination, + brick_uri=brick.uri, + predicate=props.new_brick_relation_type, + namespace=props.new_brick_relation_namespace, + object=props.new_brick_relation_object ) diff --git a/src/blenderbim/blenderbim/bim/module/brick/prop.py b/src/blenderbim/blenderbim/bim/module/brick/prop.py index 2e04ff84cf..c1ed4be59d 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/prop.py +++ b/src/blenderbim/blenderbim/bim/module/brick/prop.py @@ -55,6 +55,18 @@ def get_brick_roots(self, context): return [(root, root, "") for root in BrickStore.root_classes] +def get_brick_relations(self, context): # this will be queried from graph + return [("https://brickschema.org/schema/Brick#hasLocation", "hasLocation", ""), + ("https://brickschema.org/schema/Brick#isLocationOf", "isLocationOf", ""), + ("https://brickschema.org/schema/Brick#hasPart", "hasPart", ""), + ("https://brickschema.org/schema/Brick#isPartOf", "isPartOf", ""), + ("https://brickschema.org/schema/Brick#hasPoint", "hasPoint", ""), + ("https://brickschema.org/schema/Brick#isPointOf", "isPointOf", ""), + ("https://brickschema.org/schema/Brick#feeds", "feeds", ""), + ("https://brickschema.org/schema/Brick#isFedBy", "isFedBy", ""), + ("http://www.w3.org/2000/01/rdf-schema#label", "label", "")] + + class Brick(PropertyGroup): name: StringProperty(name="Name") label: StringProperty(name="Label") @@ -77,4 +89,9 @@ class BIMBrickProperties(PropertyGroup): 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) 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 + brick_edit_relations_toggled: BoolProperty(name="Brick Edit Relations Toggled", default=False) + new_brick_relation_type: EnumProperty(name="New Brick Relation Type", items=get_brick_relations) + new_brick_relation_namespace: EnumProperty(name="New Brick Relation Namespace", items=get_namespaces) + new_brick_relation_object: StringProperty(name="New Brick Relation Object") + add_relation_failed: BoolProperty(name="Add Relation Failed", default=False) + split_screen_toggled: BoolProperty(name="Split Screen 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 0e442e5770..bd97b3a3fd 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/ui.py +++ b/src/blenderbim/blenderbim/bim/module/brick/ui.py @@ -98,7 +98,6 @@ class BIM_PT_brickschema(Panel): row.operator("bim.rewind_brick_class", text="", icon="FRAME_PREV") col = row.column() col.alignment = "RIGHT" - # row.operator("bim.add_brick_feed", text="", icon="PLUGIN") row.operator("bim.remove_brick", text="", icon="X") # row.operator("bim.refresh_brick_viewer", text="", icon="FILE_REFRESH") @@ -111,8 +110,29 @@ class BIM_PT_brickschema(Panel): 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_create_relations_toggled", text="", icon="PLUGIN") row.prop(data=self.props, property="brick_edit_relations_toggled", text="", icon="TOOL_SETTINGS") + + if self.props.brick_create_relations_toggled: + row = self.layout.row(align=True) + col = row.column() + col.alignment = "LEFT" + row.label(text="Create Relation:") + col = row.column() + col.alignment = "RIGHT" + row.prop(data=self.props, property="split_screen_toggled", text="", icon="WINDOW") + + row = self.layout.row(align=True) + prop_with_search(row, self.props, "new_brick_relation_namespace", text="") + + row = self.layout.row(align=True) + prop_with_search(row, self.props, "new_brick_relation_type", text="") + row.prop(data=self.props, property="new_brick_relation_object", text="") + row.operator("bim.add_brick_relation", text="", icon="ADD") + + if self.props.add_relation_failed: + row = self.layout.row(align=True) + row.label(text="Failed to find this entity!", icon="ERROR") for relation in BrickschemaData.data["relations"]: row = self.layout.row(align=True) diff --git a/src/blenderbim/blenderbim/core/brick.py b/src/blenderbim/blenderbim/core/brick.py index ab668d597d..e92001dff5 100644 --- a/src/blenderbim/blenderbim/core/brick.py +++ b/src/blenderbim/blenderbim/core/brick.py @@ -78,8 +78,8 @@ def add_brick(ifc, brick, element=None, namespace=None, brick_class=None, librar brick.run_refresh_brick_viewer() -def add_brick_feed(ifc, brick, source=None, destination=None): - brick.add_feed(brick.get_brick(source), brick.get_brick(destination)) +def add_brick_relation(brick, brick_uri=None, predicate=None, namespace=None, object=None): + brick.add_relation(brick_uri, predicate, namespace, object) brick.run_refresh_brick_viewer() diff --git a/src/blenderbim/blenderbim/tool/brick.py b/src/blenderbim/blenderbim/tool/brick.py index 21eca756cb..e08074784d 100644 --- a/src/blenderbim/blenderbim/tool/brick.py +++ b/src/blenderbim/blenderbim/tool/brick.py @@ -100,9 +100,19 @@ class Brick(blenderbim.core.tool.Brick): ) @classmethod - def add_feed(cls, source, destination): - ns_brick = Namespace("https://brickschema.org/schema/Brick#") - BrickStore.graph.add((URIRef(source), ns_brick["feeds"], URIRef(destination))) + def add_relation(cls, brick_uri, predicate, namespace, object): + object_uri = namespace + object + query = BrickStore.graph.query( + "ASK { <{object_uri}> ?predicate ?object . }".replace( + "{object_uri}", object_uri + ) + ) + if query: + with BrickStore.new_changeset() as cs: + cs.add((URIRef(brick_uri), URIRef(predicate), URIRef(object_uri))) + bpy.context.scene.BIMBrickProperties.add_relation_failed = False + else: + bpy.context.scene.BIMBrickProperties.add_relation_failed = True @classmethod def remove_relation(cls, brick_uri, predicate, object):