IfcOpenShell: Code example not working

edited January 2023 in General

Code example "Get the classification of an element" from here https://blenderbim.org/docs-python/ifcopenshell-python/code_examples.html is not working for me.
Get this error " import ifcopenshell.util.attribute ModuleNotFoundError: No module named 'ifcopenshell.util.attribute'"
How can I retrieve classification of an element?

Tagged:

Comments

  • Without seeing the code, it's hard to troubleshoot.
    I've attached a similar working example. Perhaps that will help catch what was wrong.

  • How can I retrieve classification of an element?

    Maybe something like this?

    def get_classification_code(self, context, ifcproduct, ifc_version):
    
            #Classifications of an object may be referenced from an external source rather than being
            #contained within the IFC model. This is done through the IfcClassificationReference class.
            #ClassificationRefForObjects', 
    
            assembly_code_list = []
    
            if ifcproduct.HasAssociations:
    
                if ifcproduct.HasAssociations[0].is_a() == 'IfcRelAssociatesClassification':
    
                    if ifcproduct.HasAssociations[0].RelatingClassification:
    
                        #for IFC2x3
                        if ifc_version == 'IFC2X3':
                            if ifcproduct.HasAssociations[0].RelatingClassification.ItemReference:
                                assembly_code_list.append(ifcproduct.HasAssociations[0].RelatingClassification.ItemReference)
    
                        #for IFC4 
                        if ifc_version == 'IFC4':
                            if ifcproduct.HasAssociations[0].RelatingClassification.Identification:
                                assembly_code_list.append(ifcproduct.HasAssociations[0].RelatingClassification.Identification)
    
                if ifcproduct.HasAssociations[0].is_a() == 'IfcRelAssociatesMaterial':
    
                    for i in ifcproduct.HasAssociations:
                        if i.is_a() == 'IfcRelAssociatesClassification':
    
                            #for IFC2x3
                            if ifc_version == 'IFC2X3':
                                if i.RelatingClassification.ItemReference:
                                    assembly_code_list.append(i.RelatingClassification.ItemReference)
    
                            #for IFC4     
                            if ifc_version == 'IFC4':
                                if i.RelatingClassification:
                                    assembly_code_list.append(i.RelatingClassification.Identification)
    

    No module named 'ifcopenshell.util.attribute

    I don't know about that module, maybe @Moult has better ideas then my code.

  • You don't need ifcopenshell.util.attribute to get the classification, as shown in the example https://blenderbim.org/docs-python/ifcopenshell-python/code_examples.html#get-the-classification-of-an-element you need to import ifcopenshell.util.classification.

    If importing doesn't work for you, then we need to ask how you installed IfcOpenShell. Make sure you follow the instructions at https://blenderbim.org/docs-python/ifcopenshell-python/installation.html

  • I'm wondering if ifcopenshell.util.classification should also work in regular Python (without Blender?)
    At the moment I can import ifcopenshell to Python but can not import ifcopenshell.util.classification

  • Yes, it should work without Blender. Almost everything in ifcopenshell can be used without Blender. How did you install ifcopenshell? It sounds like an installation issue.

  • I installed with cmd using "pip install ifcopenshell"

  • Uninstalling ifcopenshell and then installing from zip file worked. Thank you

  • edited January 2023

    @Coen said:

    How can I retrieve classification of an element?

    Maybe something like this?

    def get_classification_code(self, context, ifcproduct, ifc_version):
        
            #Classifications of an object may be referenced from an external source rather than being
            #contained within the IFC model. This is done through the IfcClassificationReference class.
            #ClassificationRefForObjects', 
            
            assembly_code_list = []
    
            if ifcproduct.HasAssociations:
         
                if ifcproduct.HasAssociations[0].is_a() == 'IfcRelAssociatesClassification':
                  
                    if ifcproduct.HasAssociations[0].RelatingClassification:
                        
                        #for IFC2x3
                        if ifc_version == 'IFC2X3':
                            if ifcproduct.HasAssociations[0].RelatingClassification.ItemReference:
                                assembly_code_list.append(ifcproduct.HasAssociations[0].RelatingClassification.ItemReference)
                           
                        #for IFC4 
                        if ifc_version == 'IFC4':
                            if ifcproduct.HasAssociations[0].RelatingClassification.Identification:
                                assembly_code_list.append(ifcproduct.HasAssociations[0].RelatingClassification.Identification)
                                
                if ifcproduct.HasAssociations[0].is_a() == 'IfcRelAssociatesMaterial':
                
                    for i in ifcproduct.HasAssociations:
                        if i.is_a() == 'IfcRelAssociatesClassification':
                            
                            #for IFC2x3
                            if ifc_version == 'IFC2X3':
                                if i.RelatingClassification.ItemReference:
                                    assembly_code_list.append(i.RelatingClassification.ItemReference)
                                
                            #for IFC4     
                            if ifc_version == 'IFC4':
                                if i.RelatingClassification:
                                    assembly_code_list.append(i.RelatingClassification.Identification)
    

    No module named 'ifcopenshell.util.attribute

    I don't know about that module, maybe @Moult has better ideas then my code.

    Or something like this. An object can contain multiple classifications. Only tested for IFC2X3TC1

     def get_classifications(element) -> list:
            """Get classification code (ItemReference)."""
            classifications = []
            for association in element.HasAssociations:
                if association.is_a("IfcRelAssociatesClassification"):
                    item_reference = association.RelatingClassification.ItemReference
                    classifications.append(str(item_reference))
            return classifications
    
  • Or something like this. An object can contain multiple classifications. Only tested for IFC2X3TC1

     def get_classifications(element) -> list:
            """Get classification code (ItemReference)."""
            classifications = []
            for association in element.HasAssociations:
                if association.is_a("IfcRelAssociatesClassification"):
                    item_reference = association.RelatingClassification.ItemReference
                    classifications.append(str(item_reference))
            return classifications
    
    Coen
  • edited January 2023

    added relevant tag & software to subject line
    you can see other posts tagged with IfcOpenShell here:
    https://community.osarch.org/discussions/tagged/ifcopenshell

Sign In or Register to comment.