Refactor AddBrickRelation to combine object namespace+label in operator

This is an important change that should make adding relations through other methods easier to implement
This commit is contained in:
rileywong311
2023-08-01 22:10:17 -07:00
committed by Riley Wong
parent 589b3928f3
commit 7bfa6115ee
3 changed files with 11 additions and 9 deletions
@@ -144,12 +144,15 @@ class AddBrickRelation(bpy.types.Operator, Operator):
def _execute(self, context): def _execute(self, context):
props = context.scene.BIMBrickProperties props = context.scene.BIMBrickProperties
brick = props.bricks[props.active_brick_index] brick = props.bricks[props.active_brick_index]
if props.new_brick_relation_type == "http://www.w3.org/2000/01/rdf-schema#label":
object = props.new_brick_relation_object
else:
object = props.new_brick_relation_namespace + props.new_brick_relation_object
core.add_brick_relation( core.add_brick_relation(
tool.Brick, tool.Brick,
brick_uri=brick.uri, brick_uri=brick.uri,
predicate=props.new_brick_relation_type, predicate=props.new_brick_relation_type,
namespace=props.new_brick_relation_namespace, object=object
object=props.new_brick_relation_object
) )
+2 -2
View File
@@ -78,8 +78,8 @@ def add_brick(ifc, brick, element=None, namespace=None, brick_class=None, librar
brick.run_refresh_brick_viewer() brick.run_refresh_brick_viewer()
def add_brick_relation(brick, brick_uri=None, predicate=None, namespace=None, object=None): def add_brick_relation(brick, brick_uri=None, predicate=None, object=None):
brick.add_relation(brick_uri, predicate, namespace, object) brick.add_relation(brick_uri, predicate, object)
brick.run_refresh_brick_viewer() brick.run_refresh_brick_viewer()
+4 -5
View File
@@ -100,22 +100,21 @@ class Brick(blenderbim.core.tool.Brick):
) )
@classmethod @classmethod
def add_relation(cls, brick_uri, predicate, namespace, object): def add_relation(cls, brick_uri, predicate, object):
if predicate == "http://www.w3.org/2000/01/rdf-schema#label": if predicate == "http://www.w3.org/2000/01/rdf-schema#label":
with BrickStore.new_changeset() as cs: with BrickStore.new_changeset() as cs:
cs.add((URIRef(brick_uri), URIRef(predicate), Literal(object))) cs.add((URIRef(brick_uri), URIRef(predicate), Literal(object)))
bpy.context.scene.BIMBrickProperties.new_brick_relation_type = BrickStore.relationships[0][0] bpy.context.scene.BIMBrickProperties.new_brick_relation_type = BrickStore.relationships[0][0]
bpy.context.scene.BIMBrickProperties.add_relation_failed = False bpy.context.scene.BIMBrickProperties.add_relation_failed = False
return return
object_uri = namespace + object
query = BrickStore.graph.query( query = BrickStore.graph.query(
"ASK { <{object_uri}> ?predicate ?object . }".replace( "ASK { <{object_uri}> a ?o . }".replace(
"{object_uri}", object_uri "{object_uri}", object
) )
) )
if query: if query:
with BrickStore.new_changeset() as cs: with BrickStore.new_changeset() as cs:
cs.add((URIRef(brick_uri), URIRef(predicate), URIRef(object_uri))) cs.add((URIRef(brick_uri), URIRef(predicate), URIRef(object)))
bpy.context.scene.BIMBrickProperties.add_relation_failed = False bpy.context.scene.BIMBrickProperties.add_relation_failed = False
else: else:
bpy.context.scene.BIMBrickProperties.add_relation_failed = True bpy.context.scene.BIMBrickProperties.add_relation_failed = True