IfcOpenShell, how to add a new property and value to an object?

edited August 25 in General

Hey guys, I'm pretty new to IfcOpenShell.
What I'm trying to achieve is to iterate through a dictionary of attributes (keys) and their corresponding values and add them to an existing object.
I know I can access an object's attributes using something like:
wall.Name
or even change the Name to something that I want:
wall.Name = 'some value'
However, I don't want to have to type the name of the attribute since I'm iterating through a dictionary of attribute names and values.
Additionally, I know it is possible to change the values inside the someobject.get_info(), although that does not update the real object's attributes.
If I try to print the object, it still has the "original" set of attributes:
print(str(someobject))
Something like:
currentObject.my_custom_attributeName = my_custom_attributeValue
does not work.
How can I achieve this?
Thank you!

Tagged:

Comments

  • What about assigning a propertyset with Python IfOpenShell?


    # Create and assign property set import ifcopenshell ifc_file = 'path\to_your\ifc_file' ifcfile = ifcopenshell.open(ifc_file) products = ifcfile.by_type("IfcProduct") owner_history = ifcfile.by_type("IfcOwnerHistory")[0] walls=[] for i in products: if i.is_a("IfcWall"): walls.append(i) property_values = [ ifcfile.createIfcPropertySingleValue("Some Property Set Name ", "Some Property Name", ifcfile.create_entity("IfcText", "Some Value"), None), ] for wall in walls: property_set = ifcfile.createIfcPropertySet(wall.GlobalId, owner_history, "Some Property Set Name ", None, property_values) ifcfile.createIfcRelDefinesByProperties(wall.GlobalId, owner_history, None, None, [wall], property_set) ifcfile.write(ifc_file)
    Jesusbill
  • I just tested this little script on a simple IfcWall,

    IFC file after running this script.

  • In IFC, attributes are fixed. If you want to add arbitrary properties, use property sets, not attributes. This is as @Coen has explained. However, there is a simpler method using the IfcOpenShell API.

    import ifcopenshell
    import ifcopenshell.api
    ifc = ifcopenshell.open('path\to_your\ifc_file')
    element = ifc.by_type("IfcElement")[0] # Just as an example, you will need to select the elements you want to add properties to yourself.
    pset = ifcopenshell.api.run("pset.add_pset", ifc, product=element, name="Your Property Set Name")
    ifcopenshell.api.run("pset.edit_pset", ifc, pset=pset, properties={"foo": "foobar", "foo2": "foobaz"})
    
  • @Moult
    Maybe it's convenient to mention how to install the ifcopenshell.api as you described here.

  • edited August 26

    Thanks for your answers @Moult and @Coen!
    But what should I do in case I need to change the actual attributes of an object? I mean, I know I can type wall.Name = "Some other name", or wall.OwnerHistory = "some new value", etc. However, if I have a dictionary with a set of keys (attribute names that already exist on target object) and values (attribute values that I want to change on target object), what should I do?
    It should be something like:
    1. Select target object given an ID
    2. Iterate through a given dictionary and then change the values of target object's attributes

    Do you have any advice?
    Thank you once again.

  • @Coen indeed thanks.

    @FDinis once you have the pset element, use ifcopenshell.api.run("pset.edit_pset", ifc, pset=pset, properties={"foo": "foobar", "foo2": "foobaz"}). It will take care of editing it.

  • @Moult
    Thank you! :)

Sign In or Register to comment.