mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-18 06:21:40 +00:00
Fix #5243. Remove usage of types_with_super which is no longer available.
This commit is contained in:
@@ -40,25 +40,107 @@ def get_fallback_schema(version: str) -> str:
|
|||||||
return version
|
return version
|
||||||
|
|
||||||
|
|
||||||
def is_a(entity: ifcopenshell.entity_instance, ifc_class: str) -> bool:
|
def get_declaration(element: ifcopenshell.entity_instance):
|
||||||
|
"""Get the schema declaration of an actively used entity instance
|
||||||
|
|
||||||
|
IFC models are made out of instances (e.g. with a STEP ID) of entities
|
||||||
|
(e.g. IfcWall). Those entities are defined through a **Schema
|
||||||
|
Declaration**.
|
||||||
|
|
||||||
|
**Schema Declaration** objects can be used to query information about the
|
||||||
|
IFC schema itself, such as data types, enumeration values, and inheritance.
|
||||||
|
|
||||||
|
:param element: Any instance, typically from a loaded or created IFC model
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
wall = model.createIfcWall()
|
||||||
|
declaration = ifcopenshell.util.schema.get_declaration(wall)
|
||||||
|
print(declaration.name()) # IfcWall
|
||||||
|
print(declaration.is_abstract()) # False
|
||||||
|
print(declaration.supertype().name()) # IfcBuildingElement
|
||||||
|
"""
|
||||||
|
return element.wrapped_data.declaration().as_entity()
|
||||||
|
|
||||||
|
|
||||||
|
def is_a(declaration: ifcopenshell.ifcopenshell_wrapper.entity, ifc_class: str) -> bool:
|
||||||
|
"""Checks if a schema declaration is a class
|
||||||
|
|
||||||
|
:param declaration: The declaration from the schema.
|
||||||
|
:param ifc_class: A case insensitive IFC class name (e.g. IfcRoot)
|
||||||
|
:return: True is the declaration is of that class
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
wall = model.createIfcWall()
|
||||||
|
declaration = ifcopenshell.util.schema.get_declaration(wall)
|
||||||
|
ifcopenshell.util.schema.is_a(declaration, "IfcRoot") # True
|
||||||
|
"""
|
||||||
ifc_class = ifc_class.upper()
|
ifc_class = ifc_class.upper()
|
||||||
if entity.name_uc() == ifc_class:
|
if declaration.name_uc() == ifc_class:
|
||||||
return True
|
return True
|
||||||
if entity.supertype():
|
if declaration.supertype():
|
||||||
return is_a(entity.supertype(), ifc_class)
|
return is_a(declaration.supertype(), ifc_class)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_subtypes(entity):
|
def get_supertypes(
|
||||||
def get_classes(declaration):
|
declaration: ifcopenshell.ifcopenshell_wrapper.entity,
|
||||||
|
) -> list[ifcopenshell.ifcopenshell_wrapper.entity]:
|
||||||
|
"""Gets a list of supertype declarations
|
||||||
|
|
||||||
|
:param declaration: The declaration from the schema, as an entity.
|
||||||
|
:return: A list of supertypes in order from parent to grandparent.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
wall = model.createIfcWall()
|
||||||
|
results = ifcopenshell.util.schema.get_supertypes(wall.wrapped_data.declaration().as_entity())
|
||||||
|
# [<entity IfcBuildingElement>, <entity IfcElement>, ..., <entity IfcRoot>]
|
||||||
|
"""
|
||||||
|
results = []
|
||||||
|
while True:
|
||||||
|
if not (declaration := declaration.supertype()):
|
||||||
|
break
|
||||||
|
results.append(declaration)
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
def get_subtypes(
|
||||||
|
declaration: ifcopenshell.ifcopenshell_wrapper.entity,
|
||||||
|
) -> list[ifcopenshell.ifcopenshell_wrapper.entity]:
|
||||||
|
"""Get a flat list of subtype declarations
|
||||||
|
|
||||||
|
Abstract classes are skipped.
|
||||||
|
|
||||||
|
Inconsistently, the declaration itself is also added to this list. This
|
||||||
|
should be fixed exclude the declaration itself.
|
||||||
|
|
||||||
|
:param declaration: The declaration from the schema, as an entity.
|
||||||
|
:return: A list of subtypes in order from child to grandchild.
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
schema = ifcopenshell.schema_by_name("IFC4")
|
||||||
|
declaration = schema.declaration_by_name("IfcFlowSegment")
|
||||||
|
print(ifcopenshell.util.schema.get_subtypes(declaration))
|
||||||
|
[<entity IfcFlowSegment>, <entity IfcCableCarrierSegment>, ..., <entity IfcPipeSegment>]
|
||||||
|
"""
|
||||||
|
def get_classes(decl):
|
||||||
results = []
|
results = []
|
||||||
if not declaration.is_abstract():
|
if not decl.is_abstract():
|
||||||
results.append(declaration)
|
results.append(decl)
|
||||||
for subtype in declaration.subtypes():
|
for subtype in decl.subtypes():
|
||||||
results.extend(get_classes(subtype))
|
results.extend(get_classes(subtype))
|
||||||
return results
|
return results
|
||||||
|
|
||||||
return get_classes(entity)
|
return get_classes(declaration)
|
||||||
|
|
||||||
|
|
||||||
def reassign_class(
|
def reassign_class(
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
from py2neo.data import Node, Relationship
|
from py2neo.data import Node, Relationship
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
import ifcopenshell
|
import ifcopenshell
|
||||||
|
import ifcopenshell.util.schema
|
||||||
from py2neo import Graph
|
from py2neo import Graph
|
||||||
|
|
||||||
|
|
||||||
@@ -33,9 +34,10 @@ def create_pure_node_from_ifc_entity(ifc_entity, ifc_file, hierarchy=True):
|
|||||||
node["id"] = str(uuid4())
|
node["id"] = str(uuid4())
|
||||||
node["name"] = ifc_entity.is_a()
|
node["name"] = ifc_entity.is_a()
|
||||||
if hierarchy:
|
if hierarchy:
|
||||||
for label in ifc_file.wrapped_data.types_with_super():
|
node.add_label(ifc_entity.is_a())
|
||||||
if ifc_entity.is_a(label):
|
declaration = ifcopenshell.util.schema.get_declaration(ifc_entity)
|
||||||
node.add_label(label)
|
for supertype in ifcopenshell.util.schema.get_supertypes(declaration):
|
||||||
|
node.add_label(supertype.name())
|
||||||
else:
|
else:
|
||||||
node.add_label(ifc_entity.is_a())
|
node.add_label(ifc_entity.is_a())
|
||||||
attributes_type = ["ENTITY INSTANCE", "AGGREGATE OF ENTITY INSTANCE", "DERIVED"]
|
attributes_type = ["ENTITY INSTANCE", "AGGREGATE OF ENTITY INSTANCE", "DERIVED"]
|
||||||
|
|||||||
Reference in New Issue
Block a user