From 4066cfc7ff4476ec7e9a44a5d2515e3398d670d7 Mon Sep 17 00:00:00 2001 From: rileywong311 Date: Tue, 15 Aug 2023 16:24:02 -0700 Subject: [PATCH] Support Add Brick with IfcEntity selected (part 2 aka references) Can now assign references again. See https://github.com/BrickSchema/Brick/tree/master/examples/ifc for examples. --- .../blenderbim/bim/module/brick/ui.py | 7 ++- src/blenderbim/blenderbim/tool/brick.py | 50 ++++++++----------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/brick/ui.py b/src/blenderbim/blenderbim/bim/module/brick/ui.py index 5642735858..ccc065b47b 100644 --- a/src/blenderbim/blenderbim/bim/module/brick/ui.py +++ b/src/blenderbim/blenderbim/bim/module/brick/ui.py @@ -220,8 +220,11 @@ class BIM_PT_ifc_brickschema_references(Panel): if not BrickschemaReferencesData.data["libraries"]: row = self.layout.row(align=True) - row.label(text="No IFC Libraries") - row.operator("bim.convert_brick_project", text="", icon="ADD") + if BrickStore.path: + row.label(text="No IFC Libraries") + row.operator("bim.convert_brick_project", text="", icon="ADD") + else: + row.label(text="No IFC Libraries. Save the Brick project to create a new library.", icon="ERROR") return row = self.layout.row(align=True) diff --git a/src/blenderbim/blenderbim/tool/brick.py b/src/blenderbim/blenderbim/tool/brick.py index bdc3aaa4ee..2da3ebf9ed 100644 --- a/src/blenderbim/blenderbim/tool/brick.py +++ b/src/blenderbim/blenderbim/tool/brick.py @@ -28,8 +28,9 @@ import datetime try: import brickschema import brickschema.persistent + from brickschema.namespaces import REF, A import urllib.parse - from rdflib import Literal, URIRef, Namespace + from rdflib import Literal, URIRef, Namespace, BNode from rdflib.namespace import RDF except: # See #1860 @@ -49,7 +50,7 @@ class Brick(blenderbim.core.tool.Brick): ns = Namespace(namespace) brick = ns[ifcopenshell.guid.expand(ifcopenshell.guid.new())] with BrickStore.new_changeset() as cs: - cs.add((brick, RDF.type, URIRef(brick_class))) + cs.add((brick, A, URIRef(brick_class))) cs.add((brick, URIRef("http://www.w3.org/2000/01/rdf-schema#label"), Literal(label))) return str(brick) @@ -68,7 +69,7 @@ class Brick(blenderbim.core.tool.Brick): ns = Namespace(namespace) brick = ns[element.GlobalId] with BrickStore.new_changeset() as cs: - cs.add((brick, RDF.type, URIRef(brick_class))) + cs.add((brick, A, URIRef(brick_class))) if element.Name: cs.add((brick, URIRef("http://www.w3.org/2000/01/rdf-schema#label"), Literal(element.Name))) else: @@ -79,34 +80,25 @@ class Brick(blenderbim.core.tool.Brick): def add_brickifc_project(cls, namespace): project = tool.Ifc.get().by_type("IfcProject")[0] ns = Namespace(namespace) - ns_brickifc = Namespace("https://brickschema.org/extension/ifc#") - BrickStore.graph.bind("brickifc", ns_brickifc) brick_project = ns[project.GlobalId] - BrickStore.graph.add((brick_project, RDF.type, ns_brickifc["Project"])) - if project.Name: - BrickStore.graph.add( - (brick_project, URIRef("http://www.w3.org/2000/01/rdf-schema#label"), Literal(project.Name)) - ) - BrickStore.graph.add((brick_project, ns_brickifc["projectID"], Literal(project.GlobalId))) - BrickStore.graph.add( - (brick_project, ns_brickifc["fileLocation"], Literal(bpy.context.scene.BIMProperties.ifc_file)) - ) + with BrickStore.new_changeset() as cs: + cs.add((brick_project, A, REF.ifcProject)) + cs.add((brick_project, REF.ifcProjectID, Literal(project.GlobalId))) + cs.add((brick_project, REF.ifcFileLocation, Literal(bpy.context.scene.BIMProperties.ifc_file))) + if project.Name: + cs.add((brick_project, URIRef("http://www.w3.org/2000/01/rdf-schema#label"), Literal(project.Name))) return str(brick_project) @classmethod def add_brickifc_reference(cls, brick, element, project): - ns_brickifc = Namespace("https://brickschema.org/extension/ifc#") - BrickStore.graph.bind("brickifc", ns_brickifc) - BrickStore.graph.add( - ( - URIRef(brick), - ns_brickifc["hasIFCReference"], - [ - (ns_brickifc["hasProjectReference"], URIRef(project)), - (ns_brickifc["globalID"], Literal(element.GlobalId)), - ], - ) - ) + with BrickStore.new_changeset() as cs: + bnode = BNode() + cs.add((URIRef(brick), REF.hasExternalReference, bnode)) + cs.add((bnode, A, REF.IFCReference)) + cs.add((bnode, REF.hasIfcProjectReference, URIRef(project))) + cs.add((bnode, REF.ifcGlobalID , Literal(element.GlobalId))) + if element.Name: + cs.add((bnode, REF.ifcName, Literal(element.Name))) @classmethod def add_relation(cls, brick_uri, predicate, object): @@ -189,10 +181,10 @@ class Brick(blenderbim.core.tool.Brick): project = tool.Ifc.get().by_type("IfcProject")[0] query = BrickStore.graph.query( """ - PREFIX brickifc: + PREFIX ref: SELECT ?proj WHERE { - ?proj a brickifc:Project . - ?proj brickifc:projectID "{project_globalid}" + ?proj a ref:ifcProject . + ?proj ref:ifcProjectID "{project_globalid}" . } LIMIT 1 """.replace(