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 199: Line 199:
 
The usage of IfcOpenShell for geometry processing is currently considered to be moderate to advanced. There are two approaches to processing geometry. One approach is to traverse the <code>Representation</code> attribute of the IFC element, and parse it yourself. This requires an in-depth understanding of IFC geometric representations, as well as its many caveats with units and transformations, but can be very simple to extract specific types of geometry. The second approach is to use IfcOpenShell's shape processing features, which will convert almost all IFC representations into a triangulated mesh. Regardless of the source format, once it is in a mesh representation, you may use standard mesh geometry processing algorithms to analyse the geometry. This makes it easier to write generic code for any representation, but may be harder to extract certain geometric features.
 
The usage of IfcOpenShell for geometry processing is currently considered to be moderate to advanced. There are two approaches to processing geometry. One approach is to traverse the <code>Representation</code> attribute of the IFC element, and parse it yourself. This requires an in-depth understanding of IFC geometric representations, as well as its many caveats with units and transformations, but can be very simple to extract specific types of geometry. The second approach is to use IfcOpenShell's shape processing features, which will convert almost all IFC representations into a triangulated mesh. Regardless of the source format, once it is in a mesh representation, you may use standard mesh geometry processing algorithms to analyse the geometry. This makes it easier to write generic code for any representation, but may be harder to extract certain geometric features.
  
The simplest way to get started is to use the <code>create_shape</code> function to convert an element into vertices, edges, and faces.
+
Note: this article is a work in progress.
  
<syntaxhighlight lang="python">
+
TODO: talk about [https://github.com/IfcOpenShell/IfcOpenShell/issues/866 naming in ids].
import ifcopenshell
 
import ifcopenshell.geom
 
 
 
ifc_file = ifcopenshell.open('/path/to/your/file.ifc')
 
 
 
element = ifc_file.by_type('IfcWall')[0]
 
 
 
settings = ifcopenshell.geom.settings()
 
shape = ifcopenshell.geom.create_shape(settings, element)
 
faces = shape.geometry.faces # Indices of vertices per triangle face e.g. [f1v1, f1v2, f1v3, f2v1, f2v2, f2v3, ...]
 
verts = shape.geometry.verts # X Y Z of vertices in flattened list e.g. [v1x, v1y, v1z, v2x, v2y, v2z, ...]
 
materials = shape.geometry.materials # Material names and colour style information that are relevant to this shape
 
material_ids = shape.geometry.material_ids # Indices of material applied per triangle face e.g. [f1m, f2m, ...]
 
 
 
# Since the lists are flattened, you may prefer to group them per face like so depending on your geometry kernel
 
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)]
 
</syntaxhighlight>
 
 
 
Once you have a list of vertices, edges, and faces, you can perform any standard mesh algorithm to do more geometric analysis. There are a series of settings you can apply when creating a shape. These are documented [https://github.com/IfcOpenShell/IfcOpenShell/blob/v0.7.0/docs/geom/settings.md here].
 
 
 
All shapes are given an ID to uniquely identify it. These IDs following a [https://github.com/IfcOpenShell/IfcOpenShell/issues/866 naming scheme].
 
 
 
If you have a lot of geometry to process, it is advised to use the geometry iterator. This will process shapes using more than one CPU and is significantly faster.
 
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">

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)