# Object-Oriented Programming and the IFC file format

Classes, methods, properties... what are they?

Object-Oriented Programming (OOP) is a common and popular coding style. It’s found in most CAD APIs so, as building professionals, we need to understand it.

Luckily, the theoretical part is quite straight forward and can be understood with an analogy with familiar elements like block (AutoCad), family (Revit), Component (SketchUp).

As a bonus, understanding OOP serves as a backbone for BIM (Building Information Management), together with databases, which you can learn more about here.

To follow along some very basic programming knowledge is assumed. Basically you just need to know what the words variable, array, and function mean.

## Object

As you may have guessed by now, OOP has something to do with Objects. But what are they? Simply put an object or instance is a group of information related to the same thing.

For example, a door can be an object. It has information related to it, like its dimensions, material, price, manufacturer, etc. An intangible thing like a place can be an object too. Even mathematical constructs can be objects.

Following the CAD analogy, an object is a piece of geometry that you but in the model.

What we want is to have a structured way to describe these characteristics. Welcome, classes.

## Class, Properties, Methods

A class, or prototype, is the DNA of the object. All the information about the object structure is described by its DNA. In the CAD analogy, a class is that separate file that contains the piece o geometry that you import in the main model. The definition is not in the global space but in the object file.

Another popular analogy is that of the Cookie cutter, meaning that the cutter is the class that defines the characteristics of the object it makes, the cookie.

If you change that file, all the instances of that object contained in global space are updated accordingly; this concept is called inheritance.

To show some code let’s implement a 2d vector class. A vector has two components, from which we can retrieve a length and an angle. If we have multiple vectors we can also sum and multiply them.

[Figure 1: bidimensional vector representation]

// creating a class for a bidimensional vector

class Vector2D {

constructor(_x, _y) {

this.x = _x

this.y = _y

}

this.getLength() {

length = sqrt(this.x * this.x + this.y * this.y)

return length

}

this.getAngle() {

angle = atan2(this.y, this.x)

return angle

}

this.setLength(length) {

angle = this.getAngle()

this.x = cos(angle) * length

this.y = sin(angle) * length

}

this.setAngle(angle) {

let length = this.getLength()

this.x = cos(angle) * length

this.y = sin(angle) * length

}

this.sum(otherVector2D) {

this.x += otherVector2D.x

this.y += otherVector2D.y

}

this.scale(scalar) {

this.x *= scalar.x

this.y *= scalar.y

}

this.normalize() {

this.setLength(1)

}

this.dotProduct(otherVector2D) {

return this.x * otherVector2D.x + this.y * otherVector2D.y

}

}

// instantiating an array of 10 random vectors of type Vector2D

vectors = new Array()

for (i = 0; i < 10; i++) {

v = new Vector(random(15), random(20))

vectors.push(v)

}

What this thing is telling us is that there’s a class called Vector2D.

A Vector2D is defined by two parameters _x and _y, that through a special function called constructor get transferred to the class, and become properties (aka attributes) this.x and this.y. The keyword this is a placeholder that highlights the property nature of that variable. In the global scope (programming way to say space) this will be replaced by the specific object name. So to get the x property of vector vectors[0] we would write vectorsv[0].x (where, as you see we used vectors[0] instead of this).

The functions, defined in the class, that operate on the vector object type are called methods.

Every element of the vectors array is an object of Vector2D type. We can also say that these elements are instances of the Vector2D class. If we modify the class, the change is propagated to all the instances, much like we are used to in a CAD application when we modify blocks/families/components.

## IFC (Industry Foundation Classes)

Now that we know what a class is, from a programming standpoint, we can see how this concept travels through the building sector. We already talked about the similarities with the whole blocks/families/components realm.

Another interesting ramification lays in IFC (Industry Foundation Classes), the most popular exchange file type and logic structure that BIM builds upon. So let’s see how an object is described in IFC (using XML type formatting as of ISO 10303-28, a list of all available formats is kept on buildingSMART website).

[Figure 2: the IfcDoor class as part of the IFC specification, with an IfcDoorStandardCase instance]

Here is the code of that door we were referring to:

<xs:element name="IfcDoor" type="ifc:IfcDoor" substitutionGroup="ifc:IfcBuildingElement" nillable="true"/>
 <xs:complexType name="IfcDoor">
  <xs:complexContent>
   <xs:extension base="ifc:IfcBuildingElement">
    <xs:attribute name="OverallHeight" type="ifc:IfcPositiveLengthMeasure" use="optional"/>
    <xs:attribute name="OverallWidth" type="ifc:IfcPositiveLengthMeasure" use="optional"/>
    <xs:attribute name="PredefinedType" type="ifc:IfcDoorTypeEnum" use="optional"/>
    <xs:attribute name="OperationType" type="ifc:IfcDoorTypeOperationEnum" use="optional"/>
    <xs:attribute name="UserDefinedOperationType" type="ifc:IfcLabel" use="optional"/>
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>

The syntax is different, but the concepts about classes hold. In the element tag, we see IfcDoor, which is the name of the class; a few lines after we see attribute tags for OverallHeight, OverallWidth etc.

Trough APIs of specific applications that can read this IFC file, we would also have methods to interact with this door, for example, one to open or close it.

## Conclusions

I hope this article made a bit clearer what classes are in the world of programming and building engineering, and will be the starting point to explore more on this key concept.

## Useful References

1. Coding math Episode 7: Vectors part 2 by Keith Peters | https://www.youtube.com/watch?v=zYOGtlY6xaM
2. IFC on buildingSMART | https://www.buildingsmart.org/standards/bsi-standards/industry-foundation-classes/
3. Technical notes on IFC | https://technical.buildingsmart.org/standards/ifc
4. Complete IfcDoor specification | https://standards.buildingsmart.org/IFC/RELEASE/IFC4/ADD2_TC1/HTML/schema/ifcsharedbldgelements/lexical/ifcdoor.htm

## Credits

1. Cover image background photo by Filip Kominik on Unsplash
2. Figure 2 from buildingSMART

17 May 2020
Mattia Bressanelli