@@ -477,10 +477,7 @@ def create(self, cls="IfcBuildingElementProxy", parent=None, geometry=None, fram
477477 entity = self ._create_entity (cls_name , ** kwargs )
478478
479479 if parent :
480- if hasattr (entity , "ContainedInStructure" ):
481- self ._create_entity ("IfcRelContainedInSpatialStructure" , GlobalId = ifcopenshell .guid .new (), RelatingStructure = parent , RelatedElements = [entity ])
482- else :
483- self ._create_entity ("IfcRelAggregates" , GlobalId = ifcopenshell .guid .new (), RelatingObject = parent , RelatedObjects = [entity ])
480+ self .create_relationship (parent , entity )
484481
485482 if geometry :
486483 # TODO: Deal with instancing
@@ -497,6 +494,47 @@ def create(self, cls="IfcBuildingElementProxy", parent=None, geometry=None, fram
497494
498495 return entity
499496
497+ def create_relationship (self , parent : Base , child : Base ) -> Base :
498+ """
499+ Create the correct relationship between two entities based on their types.
500+
501+ Parameters
502+ ----------
503+ parent : :class:`compas_ifc.entities.base.Base`
504+ The parent entity.
505+ child : :class:`compas_ifc.entities.base.Base`
506+ The child entity.
507+
508+ Returns
509+ -------
510+ :class:`compas_ifc.entities.base.Base`
511+ The created relationship.
512+
513+ """
514+
515+ guid = ifcopenshell .guid .new ()
516+ if parent .is_a ("IfcSpatialStructureElement" ):
517+ if child .is_a ("IfcSpatialStructureElement" ):
518+ return self ._create_entity ("IfcRelAggregates" , GlobalId = guid , RelatingObject = parent , RelatedObjects = [child ])
519+ return self ._create_entity ("IfcRelContainedInSpatialStructure" , GlobalId = guid , RelatingStructure = parent , RelatedElements = [child ])
520+
521+ if parent .is_a ("IfcElementAssembly" ) or child .is_a ("IfcBuildingElementPart" ):
522+ return self ._create_entity ("IfcRelAggregates" , GlobalId = guid , RelatingObject = parent , RelatedObjects = [child ])
523+
524+ if parent .is_a ("IfcPort" ):
525+ if not child .is_a ("IfcPort" ):
526+ return self ._create_entity ("IfcRelConnectsPortToElement" , GlobalId = guid , RelatingPort = parent , RelatedElement = child )
527+ return self ._create_entity ("IfcRelConnectsPorts" , GlobalId = guid , RelatingPort = parent , RelatedPort = child )
528+
529+ if parent .is_a ("IfcElement" ) and child .is_a ("IfcElement" ):
530+ return self ._create_entity ("IfcRelConnectsElements" , GlobalId = guid , RelatingElement = parent , RelatedElement = child )
531+
532+ if parent .is_a ("IfcGroup" ):
533+ return self ._create_entity ("IfcRelAssignsToGroup" , GlobalId = guid , RelatingGroup = parent , RelatedObjects = [child ])
534+
535+ # Default case
536+ return self ._create_entity ("IfcRelAggregates" , GlobalId = guid , RelatingObject = parent , RelatedObjects = [child ])
537+
500538 def search_ifc_classes (self , name : str , n : int = 5 ) -> list [Type ["Base" ]]:
501539 """
502540 Search for IFC classes by name
0 commit comments