Editing IfcOpenShell code examples

From Wiki.OSArch

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 10: Line 10:
 
import ifcopenshell
 
import ifcopenshell
 
ifc = ifcopenshell.open('/path/to/your/file.ifc')
 
ifc = ifcopenshell.open('/path/to/your/file.ifc')
</syntaxhighlight>
 
 
...or if you already opened the file in BlenderBIM, for example, you could use...
 
 
<syntaxhighlight lang="python">
 
import ifcopenshell
 
from blenderbim.bim.ifc import IfcStore
 
ifc = ifcopenshell.open(IfcStore.path)
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 240: Line 232:
 
     iterator = ifcopenshell.geom.iterator(settings, ifc_file, multiprocessing.cpu_count())
 
     iterator = ifcopenshell.geom.iterator(settings, ifc_file, multiprocessing.cpu_count())
 
     if iterator.initialize():
 
     if iterator.initialize():
         while True:
+
         while iterator.next():
 
             shape = iterator.get()
 
             shape = iterator.get()
 
             element = ifc_file.by_guid(shape.guid)
 
             element = ifc_file.by_guid(shape.guid)
Line 251: Line 243:
 
             grouped_verts = [[verts[i], verts[i + 1], verts[i + 2]] for i in range(0, len(verts), 3)]
 
             grouped_verts = [[verts[i], verts[i + 1], verts[i + 2]] for i in range(0, len(verts), 3)]
 
             grouped_faces = [[faces[i], faces[i + 1], faces[i + 2]] for i in range(0, len(faces), 3)]
 
             grouped_faces = [[faces[i], faces[i + 1], faces[i + 2]] for i in range(0, len(faces), 3)]
            if not iterator.next():
 
                break
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
==Geometry tree (clash detection/ray tracing etc…)==
 
You can use [[https://blenderbim.org/docs-python/ifcopenshell-python/geometry_tree.html geometry tree]] to perform various task like clash detection and ray tracing. Trees are common terms is geometry libraries. IfcOpenShell tree has [[https://en.wikipedia.org/wiki/Bounding_volume_hierarchy BVHTree]] functionalities.
 
  
 
=IFC Query Syntax=
 
=IFC Query Syntax=
Line 326: Line 313:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
You can also use query a specific element. It is what you do when using [[BlenderBIM_Add-on/BlenderBIM_IFCCSV | IfcCsv]] for example.
 
 
<syntaxhighlight lang="python">
 
# This gets all walls and slabs in a particular building storey
 
wall = selector.parse(ifc, '.IfcWall')[0]
 
wall_name = selector.get_element_value(wall, "Name")
 
wall_type_name = selector.get_element_value(wall, "type.Name")
 
wall_is_external = selector.get_element_value(wall, "Pset_WallCommon.IsExternal")
 
</syntaxhighlight>
 
  
 
=Exploring IFC schema=
 
=Exploring IFC schema=
Line 387: Line 364:
 
<entity IfcFlowSegment>
 
<entity IfcFlowSegment>
 
</syntaxhighlight>
 
</syntaxhighlight>
 
Another way to get a list of supertypes :
 
<!-- From discussion here: https://app.element.io/#/room/#OSArch:matrix.org/$LKrDGOp4OT3Qfbpl-Yj8QeUDG5mowS0YXrAKr0OBjts -->
 
<syntaxhighlight lang="python">
 
>>> import ifcopenshell
 
>>> s = ifcopenshell.ifcopenshell_wrapper.schema_by_name('IFC4')
 
>>> e = s.declaration_by_name('IfcWall')
 
>>> inheritance = []
 
>>> while e.supertype():
 
...    inheritance.append(e.supertype())
 
...    e = e.supertype()
 
...
 
>>> inheritance
 
[<entity IfcBuildingElement>, <entity IfcElement>, <entity IfcProduct>, <entity IfcObject>, <entity IfcObjectDefinition>, <entity IfcRoot>]
 
</syntaxhighlight>
 
 
  
 
Get subtypes :
 
Get subtypes :
Line 413: Line 374:
 
  <entity IfcPipeSegment>)
 
  <entity IfcPipeSegment>)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
=Property and quantity sets (Pset and Qto)=
 
=Property and quantity sets (Pset and Qto)=
Line 473: Line 433:
 
python -m ifcopenshell.validate some_file.ifc
 
python -m ifcopenshell.validate some_file.ifc
 
</syntaxhighlight>
 
</syntaxhighlight>
On GNU/Linux you can store result in a json file:
 
<syntaxhighlight lang="bash">
 
python -m ifcopenshell.validate --json some_file.ifc >> json_output_path.json
 
</syntaxhighlight>
 
 
 
==Inside a script==
 
==Inside a script==
 
It looks similar to command line way but you need to provide a logger (standard logger or json_logger as below).
 
It looks similar to command line way but you need to provide a logger (standard logger or json_logger as below).

Please note that all contributions to Wiki.OSArch are considered to be released under the Creative Commons Attribution-ShareAlike (see Wiki.OSArch:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)