Skip to content

01: Working with the IFC model

jakob-beetz edited this page Dec 22, 2016 · 3 revisions

#The model object in IfcOpenShell

Immediately after you loaded the model into the viewer (or you loaded a model in your own application using e.g. model = ifcopenshell.open('mymodel.ifc')), the Python interpreter holds a variable in memory that allows you to access the model contents: Notice, how

project = model.by_type("IfcProject")
print ( project)

prints out the SPFF line

[#1=IfcProject('0YvctVUKr0kugbFTf53O9L',#2,'Hello wall project','Simple wall with door',$,$,$,(#20),#7)]

surrounded by [ ]. This is the common representation of a python list object, see here. In order to access some of the attributes of we first need to access the first (and in case of IfcProject only) element in that list by using th [index] operator:

project = model.by_type("IfcProject")
print (project[0])

Which will get us the same SPFF line

#1=IfcProject('0YvctVUKr0kugbFTf53O9L',#2,'Hello wall project','Simple wall with door',$,$,$,(#20),#7)

as above except the square brackets []. This actually is a string representation of an object instance, in this case, an instance of a IfcProject ENTITY.

In fact, the model variable offers a number of ways to access model contents:

##Accessing all instances of an entity - the by_type() function lets you access all entities of a particular IFC ENTITY definition as listed in the specification. Note that you can use the function to get subtypes of certain entities:

walls = model.by_type("IfcWall")
print (walls)

gets you

#45=IfcWallStandardCase('3vB2YO$MX4xv5uCqZZG05x',#2,'Wall xyz','Description of Wall',$,#46,#51,$)

which is an instance of IfcWallStandardCase, which is a subtype of IfcWall. This also means, that you can access e.g. all subtypes of IfcProduct by

products = model.by_type("IfcProduct")
print (products)

which returns

[
#45=IfcWallStandardCase('3vB2YO$MX4xv5uCqZZG05x',#2,'Wall xyz','Description of Wall',$,#46,#51,$), 
#97=IfcOpeningElement('2LcE70iQb51PEZynawyvuT',#2,'Opening Element xyz','Description of Opening',$,#98,#103,$),
#124=IfcDoor('0LV8Pid0X3IA3jJLVDPidY',#2,'A common door','Description of a standard door',$,#125,#130,$,1.400,0.7)
]

including the IfcOpeningElement, while

products = model.by_type("IfcBuildingElement")
print (products)

just returns

[
#45=IfcWallStandardCase('3vB2YO$MX4xv5uCqZZG05x',#2,'Wall xyz','Description of Wall',$,#46,#51,$),
#124=IfcDoor('0LV8Pid0X3IA3jJLVDPidY',#2,'A common door','Description of a standard door',$,#125,#130,$,1.400,0.7)
]

since the opening is not a subclass of IfcBuildingElement(http://www.buildingsmart-tech.org/ifc/IFC2x4/alpha/html/ifcproductextension/lexical/ifcbuildingelement.htm).

Note: If this behavior is undesired, and you just want objects of exactly that type without its subclasess can easily filter the type of by using the is_a() function like so

products = model.by_type("IfcWall")
walls = []
for product in products:
    if product.is_a("IfcWall"):
        walls.append(product)

The for ... in part is referred to as a loop. This structure will be used extensively, so please refer to Python tutorial to get the hang of it.

##The by_id() function lets you access object by their SPFF # id like so:

print (model.by_id(1))

prints

#1=IfcProject('0YvctVUKr0kugbFTf53O9L',#2,'Hello wall project','Simple wall with door',$,$,$,(#20),#7)

##The by_guid() function lets you access objects in the model by guid, e.g.

# suppose we have the GUID of the project (example above)
project = model.by_guid('0YvctVUKr0kugbFTf53O9L')
print (project.Name)

prints

Hello wall project
Clone this wiki locally