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.
This commit is contained in:
rileywong311
2023-08-15 16:24:02 -07:00
parent edb8d636d0
commit 4066cfc7ff
2 changed files with 26 additions and 31 deletions
@@ -220,8 +220,11 @@ class BIM_PT_ifc_brickschema_references(Panel):
if not BrickschemaReferencesData.data["libraries"]: if not BrickschemaReferencesData.data["libraries"]:
row = self.layout.row(align=True) row = self.layout.row(align=True)
row.label(text="No IFC Libraries") if BrickStore.path:
row.operator("bim.convert_brick_project", text="", icon="ADD") 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 return
row = self.layout.row(align=True) row = self.layout.row(align=True)
+21 -29
View File
@@ -28,8 +28,9 @@ import datetime
try: try:
import brickschema import brickschema
import brickschema.persistent import brickschema.persistent
from brickschema.namespaces import REF, A
import urllib.parse import urllib.parse
from rdflib import Literal, URIRef, Namespace from rdflib import Literal, URIRef, Namespace, BNode
from rdflib.namespace import RDF from rdflib.namespace import RDF
except: except:
# See #1860 # See #1860
@@ -49,7 +50,7 @@ class Brick(blenderbim.core.tool.Brick):
ns = Namespace(namespace) ns = Namespace(namespace)
brick = ns[ifcopenshell.guid.expand(ifcopenshell.guid.new())] brick = ns[ifcopenshell.guid.expand(ifcopenshell.guid.new())]
with BrickStore.new_changeset() as cs: 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))) cs.add((brick, URIRef("http://www.w3.org/2000/01/rdf-schema#label"), Literal(label)))
return str(brick) return str(brick)
@@ -68,7 +69,7 @@ class Brick(blenderbim.core.tool.Brick):
ns = Namespace(namespace) ns = Namespace(namespace)
brick = ns[element.GlobalId] brick = ns[element.GlobalId]
with BrickStore.new_changeset() as cs: with BrickStore.new_changeset() as cs:
cs.add((brick, RDF.type, URIRef(brick_class))) cs.add((brick, A, URIRef(brick_class)))
if element.Name: if element.Name:
cs.add((brick, URIRef("http://www.w3.org/2000/01/rdf-schema#label"), Literal(element.Name))) cs.add((brick, URIRef("http://www.w3.org/2000/01/rdf-schema#label"), Literal(element.Name)))
else: else:
@@ -79,34 +80,25 @@ class Brick(blenderbim.core.tool.Brick):
def add_brickifc_project(cls, namespace): def add_brickifc_project(cls, namespace):
project = tool.Ifc.get().by_type("IfcProject")[0] project = tool.Ifc.get().by_type("IfcProject")[0]
ns = Namespace(namespace) ns = Namespace(namespace)
ns_brickifc = Namespace("https://brickschema.org/extension/ifc#")
BrickStore.graph.bind("brickifc", ns_brickifc)
brick_project = ns[project.GlobalId] brick_project = ns[project.GlobalId]
BrickStore.graph.add((brick_project, RDF.type, ns_brickifc["Project"])) with BrickStore.new_changeset() as cs:
if project.Name: cs.add((brick_project, A, REF.ifcProject))
BrickStore.graph.add( cs.add((brick_project, REF.ifcProjectID, Literal(project.GlobalId)))
(brick_project, URIRef("http://www.w3.org/2000/01/rdf-schema#label"), Literal(project.Name)) cs.add((brick_project, REF.ifcFileLocation, Literal(bpy.context.scene.BIMProperties.ifc_file)))
) if project.Name:
BrickStore.graph.add((brick_project, ns_brickifc["projectID"], Literal(project.GlobalId))) cs.add((brick_project, URIRef("http://www.w3.org/2000/01/rdf-schema#label"), Literal(project.Name)))
BrickStore.graph.add(
(brick_project, ns_brickifc["fileLocation"], Literal(bpy.context.scene.BIMProperties.ifc_file))
)
return str(brick_project) return str(brick_project)
@classmethod @classmethod
def add_brickifc_reference(cls, brick, element, project): def add_brickifc_reference(cls, brick, element, project):
ns_brickifc = Namespace("https://brickschema.org/extension/ifc#") with BrickStore.new_changeset() as cs:
BrickStore.graph.bind("brickifc", ns_brickifc) bnode = BNode()
BrickStore.graph.add( cs.add((URIRef(brick), REF.hasExternalReference, bnode))
( cs.add((bnode, A, REF.IFCReference))
URIRef(brick), cs.add((bnode, REF.hasIfcProjectReference, URIRef(project)))
ns_brickifc["hasIFCReference"], cs.add((bnode, REF.ifcGlobalID , Literal(element.GlobalId)))
[ if element.Name:
(ns_brickifc["hasProjectReference"], URIRef(project)), cs.add((bnode, REF.ifcName, Literal(element.Name)))
(ns_brickifc["globalID"], Literal(element.GlobalId)),
],
)
)
@classmethod @classmethod
def add_relation(cls, brick_uri, predicate, object): 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] project = tool.Ifc.get().by_type("IfcProject")[0]
query = BrickStore.graph.query( query = BrickStore.graph.query(
""" """
PREFIX brickifc: <https://brickschema.org/extension/ifc#> PREFIX ref: <https://brickschema.org/schema/Brick/ref#>
SELECT ?proj WHERE { SELECT ?proj WHERE {
?proj a brickifc:Project . ?proj a ref:ifcProject .
?proj brickifc:projectID "{project_globalid}" ?proj ref:ifcProjectID "{project_globalid}" .
} }
LIMIT 1 LIMIT 1
""".replace( """.replace(