Menu

Is there a way to build a class that inherits from an IFC class?

Help
Faez Amiri
2021-08-18
2021-08-25
  • Faez Amiri

    Faez Amiri - 2021-08-18

    Hello everyone,
    I am working on parking components in REVIT which are exported as instances of the IfcBuildingElementProxy class. We computed some new attributes and functions for these component with help of IfcOpenShell and wondered if we could add these new attributes to the object's built-in attributes (like Name, GuId, Type, etc.).
    If we try to build a new class that can assign these new attributes to our objects, the built-in attributes of the ifc class wont be available except in case that the new class inherits from the object's very first ifc class.
    Is there a way to introduce the ifc classes to our .py code and define some child classes for them which is able to produce instances that are more enhanced?
    thanks

     

    Last edit: Faez Amiri 2021-08-18
    • Thomas Krijnen

      Thomas Krijnen - 2021-08-20

      Currently there isn't a mechanism for that. Suppose you subclass from ifcopenshell.entity_instance. Add such an instance to the file and later retrieve that instance from the file or through a relationship from another instance. The object you get returned would be a regular ifcopenshell.entity_instance and not your embellished class.

      I don't fully understand what you're aiming to do. Is this just for readability of the code or is there some more intricate functionality you're envisioning.

      One thing you could do is not use subclassing but composition:

      class my_custom_entity:
        def __init__(self, ifc_inst):
          self.ifc_inst = ifc_inst
        def _calc_area(self):
           return 5
        area = property(_calc_area)
        def __getattr__(self, k):
          return getattr(self.ifc_inst, k)
      

      By means of the dynamic attribute lookup getattr you'd be able to use the new area property as well as the IFC attributes on instances of this class.

       
  • Faez Amiri

    Faez Amiri - 2021-08-25

    Hi,
    thanks for your help, I tried and it helped.
    the reason I wanted to do such a thing was for code readability as you said. I am implementing this in a code checking module and makes it more structured; also the properties computed in one module could be easily accessible in another module just by calling the instances of the custom class.

     

    Last edit: Faez Amiri 2021-08-25

Log in to post a comment.