Skip to content

Element attributes

In this chapter we focus on managing the attributes appended to model elements. This means attributes of elements like walls, slabs, reinforcement or 2D-Lines or 3D-solids. This chapter applies also to model elements without geometrical representation like element groups. Handling of project attributes is described in a separate chapter.

Create elements with attributes

Every object, that can be created in a drawing file, like wall or 3d object, is a child object of the AllplanElement class. In all those kind of objects, attributes are organized in a data structure represented by the Attributes object. It is composed of usually one AttributeSet object, which in turn is composed usually several Attribute objects, like AttributeString or AttributeInteger (depending on the value type of the attribute). The relationships are shown on the graph below.

classDiagram
    direction BT

    class AllplanElement{
        <<Abstract>>
        Attributes Attributes
        CommonProperties CommonProperties
        Any GeometryObject
        list[LabelElement] LabelElements
    }
    class WallElement {
        WallProperties
    }
    class ModelElement3D {
        TextureDefinition TextureDefinition
        TextureMappingProperties TextureMapping
    }
    class Attributes{
        list[AttributeSet] AttributeSets
    }
    class AttributeSet{
        list[Attribute] Attributes
    }
    class Attribute{
        <<Abstract>>
    }

    class AttributeString{
        int id
        str value
    }
    class AttributeInteger{
        int id
        int value
    }
    class AttributeDouble{
        int id
        float value
    }
    class AttributeDate{
        int id
        int day
        int month
        int year
    }
    class AttributeEnum{
        int id
        int value
    }

    AttributeString --|> Attribute
    AttributeInteger --|> Attribute
    AttributeDouble --|> Attribute
    AttributeDate --|> Attribute
    AttributeEnum --|> Attribute

    Attribute "*" --o "1" AttributeSet

    AttributeSet "1..*" --o "1" Attributes

    Attributes "0..1" --o "*" AllplanElement

    WallElement --|> AllplanElement
    ModelElement3D --|> AllplanElement

Example

In this example we have a model_element which is an AllplanElement object and we want to append two attributes to it:

  • Status (N° 49) of New building
  • Fire risk factor (N° 1398) of A2
attr_fire_risk_factor = AllplanBaseElements.AttributeString(1398, "A2") #(1)!
attr_status = AllplanBaseElements.AttributeEnum(49, 0) #(2)!

attr_set = AllplanBaseElements.AttributeSet([attr_fire_risk_factor, attr_status])

attributes = AllplanBaseElements.Attributes([attr_set])

model_element.SetAttributes(attributes)
  1. The attribute Fire risk factor has a value type string, that is why we have to instantiate the AttributeString object.
  2. The attribute Status is an enumeration attribute, where the value New building has the 0 key.

Using PythonPart framework

As shown in the example above, appending an attribute to an AllplanElement requires the knowledge about value type of the attribute we want to append. The easier way is to use the BuildingElementAttributeList, which will do part of the work for us. This class offers methods, which can create an attribute list by:

  • adding attributes explicitly using ID and value,
  • retrieving attributes from the property palette parameters.

Example

Just as in the example before, we have a model_element which is an AllplanElement object and we want to append two attributes to it:

  • Status (N° 49) of New building
  • Fire risk factor (N° 1398) of A2
attribute_list = BuildingElementAttributeList()

attribute_list.add_attribute(1398, "A2") #(1)!
attribute_list.add_attribute(49, "New building") #(2)!

attribute_list.add_attributes_from_parameters(self.building_element) #(3)!

attr_set = AllplanBaseElements.AttributeSet(attribute_list.get_attribute_list()) #(4)!
attributes = AllplanBaseElements.Attributes([attr_set])

model_element.SetAttributes(attributes)
  1. The method add_attribute only requires attribute ID and the value and works fine with all attribute value types.
  2. In case of enumeration attribute the method will also work, when passing value to it. Passing a key like add_attribute(49, 0) will also work just fine.
  3. The method add_attributes_from_parameters will add all the attribute parameters from the property palette into the list.
  4. The method get_attribute_list will cast the BuildingElementAttributeList into a python list of Attribute objects so they can be passed to an AttributeSet object.

Example

For a complete usage see the example Attributes located in:

  • …\etc\Examples\PythonParts\BasisExamples\Attributes.pyp
  • …\etc\PythonPartsExampleScripts\BasisExamples\Attributes.py

Create PythonPart with attributes

If the object created with the Python script is supposed to stay parametric, we must create a PythonPart holding all the AllplanElements. In this case appending the attributes to AllplanElement will make them invisible for evaluation e.g., in reports or legends. To avoid this, the attributes must be appended to the PythonPart. The utility class PythonPartUtil offers the method add_attribute_list to do just that.

Example

In the following example we want to append the same two attributes, as in the previous example, to our PythonPart:

  • Status (N° 49) of New building
  • Fire risk factor (N° 1398) of A2
pyp_util = PythonPartUtil()
...
attribute_list = BuildingElementAttributeList()
attribute_list.add_attribute(1398, "A2")
attribute_list.add_attribute(49, "New building")

pyp_util.add_attribute_list(attribute_list) #(1)!

python_part = pyp_util.create_pythonpart(build_ele) #(2)!
  1. We pass the BuildingElementAttributeList directly. Creating AttributeSet and Attributes objects is done by the framework.
  2. The method create_pythonpart appends all attribute parameters from the property palette (represented here by the build_ele object), so there is no need to use the method add_attributes_from_parameters as we done it in the previous example.

Modify attributes of existing elements

Existing model elements can be accessed by a selection. As a result, we become a list of BaseElementAdapter objects, which we describe in detail in this chapter. The service class ElementsAttributeService provides methods to read and modify the attributes of adapters and is therefore the best way to manage the attributes of existing model elements.

Getting attributes

The method GetAttributes gets the attributes from an element adapter as a list of tuples consisting of two values: attribute id and its value.

Example

Calling the method like this

AllplanBaseElements.ElementsAttributeService.GetAttributes(element_adapter)

can result in a list like this:

[(1398, "A2"), #(1)!
(49, "New building")] #(2)!
  1. The attribute Fire risk factor with its value A2
  2. The attribute Status with its value. Note that Status is an enumeration attribute. In this case we get the enumeration value, not the key.

The optional argument readState of the method GetAttributes enables to choose, which attributes should be read. Refer to the documentation of the enumeration class eAttibuteReadState to see, what options are possible.

Setting attributes

The methods ChangeAttribute and ChangeAttributes provide the possibility to change the value of an element attribute(s) or, if the given attribute is not present in the element, to append it to the element.

Example

In this example we are appending the same attributes as before, but to all existing elements in the model.

element_adapters = AllplanBaseElements.ElementsSelectService.SelectAllElements(doc)

AllplanBaseElements.ElementsAttributeService.ChangeAttribute(1398, 'A2', element_adapters)
AllplanBaseElements.ElementsAttributeService.ChangeAttribute(49, 'New building', element_adapters)
attr_list = [(1398, 'A2'), (49, 'New building')]
element_adapters = AllplanBaseElements.ElementsSelectService.SelectAllElements(doc)

AllplanBaseElements.ElementsAttributeService.ChangeAttributes(attr_list, element_adapters)

Example

For a complete usage see the example PythonPart ElementsAttributeService located in:

  • …\etc\Examples\PythonParts\ServiceExamples\ElementsAttributeService.pyp
  • …\etc\PythonPartsExampleScripts\ServiceExamples\ElementsAttributeService.py