Skip to content

00: Hello IFC world

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

Hello world, hello IFC

Printing to the console

As a first check, to test whether the setup works, please start the viewer and load a model. Then, type in the words

print ("hello world")

and press the Run button (or press Ctrl+P) which should print out the words hello world to the console window under the editor.

All code in the editor is regular Python code and will be evaluated by the Python interpreter when "Run" is pressed. As in many programming languages, a python script consists of expressions that are executed by the interpreter and evaluated. A simple expression e.g. can be 1 + 1. This will instruct the interpreter to calculate the sum of 1 plus 1. In order to see the result, we have to do something with it. A simple way would be to print the result to the console. This can be achieved by using some of the functionalities offered by the Python language and interpreter. There are hundreds of functions built into the core language of Python itself. One of these handy functions is the print function, which takes any number of arguments, evaluates them and prints it to the console (or any other output) like so:

print (1 + 1)

which should result in the console output

2

Variables

Instead of printing out the results of an operation (like 1+1) it is often necessary to store the result in a variable. In Python, this is achieved by the assignment operator =:

result = 1 + 1

This stores or 'assigns' the result of the operation "1 + 1" on the right hand side of the equals sign in a new variable result (the left hand side). Within certain limits, you are free to come up with any variable name of your own (e.g. my_result, resultaat, aResult etc.) Notice however, that you should have in mind that the variable name should be clear, readable and tell others (or yourself, when you pick up your work later on), what the purpose of the variable is.

We can then operate further with the variable, by e.g. printing out its contents:

result = 1 + 1 
print ("the result is: ", result)
result = result + 1
print ("now the result is: ", result)

which should give you

the result is: 2
now the result is: 3

built-in variables in the IfcOpenShell viewer

Before even the first line of code is written, the viewer and the application come with two main variables, that have been created and assigned with values when you started the application and loaded a model: The model variable, granting access to the loaded IFC model(s) and the viewer variable, that allows some view-related interactions (like selection, coloring etc.) that do not influence the model itself. Important: as with all variables and keywords in Python, words are case-sensitive, i.e. Model is another variable than model or MODEL. The essential variable model let's you access the whole contents of the loaded IFC model.

print (model.by_type("IfcProject"))

This should print something like

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

wich is the verbatim line from the STEP Physical File Format (SPFF), ISO 10303 part 21 that you have loaded as a model. This will look different depending on what kind of file you loaded.