Parsing functionality of IfcOpenShell in detail

The parsing functionality is a very interesting feature of IfcOpenShell-python. It enables the user to extract information of his/her interest from a .ifc file. For eg. in the below example, we will extract NominalDiameter, CrossSectionArea etc of reinforcing bar from .ifc file using python interactive shell.

In IfcOpenShell-python there are two main functions present.

  1. by_type(<argument>): This enable the user to what type of information (i.e. IfcProject, IfcRoot, IfcSlab etc.) you want to extract from .ifc file. Arguments name is same as all the entities present in IFC2x4 (http://www.buildingsmart-tech.org). For eg. if user want to extract a information of “IfcWall” then they simple put “IfcWall” as argument.
  2. is_a(): We can use this function in two different ways. For eg. if we use is_a() without any argument then it only tell that information present is belonging to which entity or if we use is_a("IfcWall") with one argrument then it will simply check whether the argument name is same or not with entity name of data present in the form of True or False.

Note: Rest of the classes, functions and there arguments name are same as present in IFC2x4. You can understand more in the below example.

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> # Importing ifcopenshell                                            
>>> import ifcopenshell                                                 
>>> # Openning .ifc file using open() fuction which is present in ifcopenshell library 
>>> file = ifcopenshell.open("Slab.ifc")                                
>>> # Here we use by_type() function because we want to extract information of IfcReinforcingBar from .ifc file 
>>> rebars = file.by_type("IfcReinforcingBar")                          
>>> # All the rebars present in the .ifc file is store in rebars variable in the form of array
>>> rebars                                                              
>>> [#218=IfcReinforcingBar('3jNYnJqWb9afBZKX5CCrhW',#41,'Rebar Bar:NC Rebar : Shape M_00:390027: 1',$,'Rebar Bar:NC Rebar:53673',#201,#216,'390027',$,16.,0.000201061929829747,5920.,.NOTDEFINED.,$),
 #251=IfcReinforcingBar('3jNYnJqWb9afBZKX9CCrhW',#41,'Rebar Bar:NC Rebar : Shape M_00:390027: 2',$,'Rebar Bar:NC Rebar:53673',#250,#248,'390027',$,16.,0.000201061929829747,5920.,.NOTDEFINED.,$),
 #270=IfcReinforcingBar('3jNYnJqWb9afBZKXDCCrhW',#41,'Rebar Bar:NC Rebar : Shape M_00:390027: 3',$,'Rebar Bar:NC Rebar:53673',#269,#267,'390027',$,16.,0.000201061929829747,5920.,.NOTDEFINED.,$)]
>>> rebar = rebars[0]                                                   
>>> rebar                                                               
>>> #218=IfcReinforcingBar('3jNYnJqWb9afBZKX5CCrhW',#41,'Rebar Bar:NC Rebar : Shape M_00:390027: 1',$,'Rebar Bar:NC Rebar:53673',#201,#216,'390027',$,16.,0.000201061929829747,5920.,.NOTDEFINED.,$)
>>> # Extracting actual data of rebar. Here you will find all the list of arguments present in IfcReinforcingBar (http://www.buildingsmart-tech.org/ifc/IFC2x4/alpha/html/ifcstructuralelementsdomain/lexical/ifcreinforcingbar.htm) 
>>> rebar.NominalDiameter                                               
>>> 16.0                                                                
>>> rebar.CrossSectionArea                                              
>>> 0.000201061929829747                                                
>>> rebar.BarLength                                                     
>>> 5920.0                                                              
>>> rebar.BarRole                                                       
>>> 'NOTDEFINED'                                                        
>>> rebar.BarSurface                                                    
>>> rebar.OwnerHistory                                                  
>>> #41=IfcOwnerHistory(#38,#5,$,.NOCHANGE.,$,$,$,1488377749)           
>>> hist = rebar.OwnerHistory                                           
>>> # Using is_a() function to find the data present in hist variable is belonging to which entity
>>> hist.is_a()                                                         
>>> 'IfcOwnerHistory'                                                   
>>> # Here we check whether a data is belonging to specific entities or not
>>> hist.is_a("IfcOwnerHistory")                                        
>>> True                                                                
>>> hist.is_a("IfcWall")                                                
>>> False                                                               
>>> exit()  

Get sample IFC file Slab.ifc.

Reference:
http://academy.ifcopenshell.org/using-the-parsing-functionality-of-ifcopenshell-interactively
http://www.buildingsmart-tech.org/ifc/IFC2x4/alpha/html/ifcstructuralelementsdomain/lexical/ifcreinforcingbar.htm