IfcOpenShell python bindings get started

Thing you want to do

As the title says, IfcOpenShell is an open source library that can parse IFC, originally written in C ++, but also has Python bindings. I will. Among the IfcOpenShell tools, there is IfcConvert, which enables IFC geometry extraction. Various format conversions such as OBJ, IGES, DAE, STL are possible, and only specific shapes can be extracted by GUID or name. This is generally sufficient, but when you try to do a lot with IFC's attribute information and shapes, you'll definitely want to mess with 3D data such as geometry and topology (although it's usually not the case).

In that case, it is common to use a library (CAD kernel) called OpenCASCADE, which is actually included in the library. This library is a long-established open source library used for FreeCAD etc., and seems to provide parametric 3D modeler function.

By the way, IFC analysis libraries are IFC ++ and xBIM Toolkit for open source, and [IFC Engine] for paid. ](Http://www.ifcbrowser.com/) and apstex IFC Framework (some are free).

IFC analysis with IfcOpenShell and drawing with python OCC

Preparation

Due to the library, we will prepare a Python 3.5 environment. In my case, I installed Anaconda and used conda to install the required libraries. It seems that the supported Python is up to 3.5, so use that as the environment and install the corresponding version of OpenCASCADE's Python-binding pythonocc. It may be a factor of the network environment, it fails quite a bit, so it may be better to build from the source code.

conda install -c conda-forge -c oce -c dlr-sc -c ifcopenshell ifcopenshell
conda install -c conda-forge -c dlr-sc -c pythonocc -c oce pythonocc-core==0.17.3

Also, prepare an appropriate IFC file. ifcOpenHouse is good.

Shape acquisition from IFC

The IFC geometry is broken down into complex ʻIfcRepresentationItem`. Specifically, it includes shape representation, style, and texture representation of Surface Model (Tessellatiob, etc.) and Solid Model (CGS, B-reps, sweep, etc.). IfcOpenShell hides this complexity and translates it into all unified boundary representations (B-reps). In addition, it also generates MESH for rendering.

By the way, the actual shape modeling in BIM seems to depend on what is called a "kernel", such as ASIC or Paradsolid. Complex expressions such as cutouts and openings can be easily modeled.

Get MESH

Below is the simplest sample.

import ifcopenshell
from ifcopenshell import geom

def read_geom(ifc_path):
    ifc_file = ifcopenshell.open(ifc_path)
    settings = geom.settings()

    for ifc_entity in ifc_file.by_type("IfcWall"):
        shape = geom.create_shape(settings, ifc_entity)
        #Get the mesh
        ios_vertices = shape.geometry.verts
        ios_edges = shape.geometry.edges
        ios_faces = shape.geometry.faces

        #Try to print normally
        print(ios_vertices)
        print(ios_edges)
        print(ios_faces)

geom.setting () is an option for conversion, which by default creates a mesh of vertices (Vetices), ridges (Edge), and faces (Face). You can get the shape of the IFC element indicated by ifc_entry with geom.create_shape (settings, ifc_entry). Vertices are triplets of x, y, z, ridges connect them, and faces consist of three vertices and ridges.

Handle B-reps directly.

In the above, we got the mesh representation that can be rendered directly, but we can also get the boundary representation B-reps (Boundary representation) directly. Specify the following parameters in geom.setting (). In the following, I am trying to save it to a file.

settings = geom.settings()
settings.set(settings.USE_BREP_DATA, True)
occ_shape = shape.geometry.brep_data

# IfcOpenShell generate an Open Cascade BREP 
with open("IfcOpenShellSamples/brep_data", "w") as file:
    file.write(occ_shape)

The output format seems to be Open Cascade BREP Format.

Draw using pythonOCC as follows. I think the color is strange, but first of all, it's like this.

import ifcopenshell
from ifcopenshell import geom

# Specify to return pythonOCC shapes from ifcopenshell.geom.create_shape()
settings = geom.settings()
settings.set(settings.USE_PYTHON_OPENCASCADE, True)

# Initialize a graphical display window
occ_display = geom.utils.initialize_display()
ifc_file = ifcopenshell.open("ifcOpenHouse.ifc")


products = ifc_file.by_type("IfcProduct")
for product in products:
    if product.is_a("IfcOpeningElement"): continue
    if product.Representation:
        shape = geom.create_shape(settings, product).geometry
        display_shape = geom.utils.display_shape(shape)
        if product.is_a("IfcPlate"):
            # Plates are the transparent parts of the window assembly
            # in the IfcOpenHouse model
            geom.utils.set_shape_transparency(display_shape, 0.8)

#It will disappear soon, so let's loop here
ifcopenshell.geom.utils.main_loop()

image.png

reference

  1. Using IfcOpenShell to parse IFC files with Python
  2. IfcOpenShell – Read geom as mesh
  3. IfcOpenShell – Read geom as brep

Recommended Posts

IfcOpenShell python bindings get started
Get started with Python! ~ ② Grammar ~
started python
Get started with Python! ~ ① Environment construction ~
Link to get started with python
How to get started with Python
Get started with Python in Blender
I started python
Get Started with TopCoder in Python (2020 Edition)
How Python beginners get started with Python with Progete
[Blender x Python] Let's get started with Blender Python !!
Python hand play (let's get started with AtCoder?)
A layman wants to get started with Python
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
Django 1.11 started with Python3.6
[Python] Get environment variables
Get started Jupyter Notebook
1.1 Getting Started with Python
Getting Started with Python
[Python3] Get date diff
Get started with MicroPython
Get date in Python
Get date with python
Get started with Mezzanine
Getting Started with Python
python get current time
I tried to get started with blender python script_Part 01
I tried to get started with blender python script_Part 02
Get YouTube Comments in Python
Getting Started with Python Functions
Get started with Django! ~ Tutorial ⑤ ~
Let's get started with Python ~ Building an environment on Windows 10 ~
Get Twitter timeline with python
Get started with influxDB + Grafana
Minimum knowledge to get started with the Python logging module
Get Youtube data with python
Getting Started with Python Django (1)
Get started with Django! ~ Tutorial ④ ~
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Get started with Django! ~ Tutorial ⑥ ~
Get Terminal size in Python
[Python] Get the previous month
Explicitly get EOF in python
Get thread ID with python
Python started by C programmers
Getting Started with Python Django (6)
[python] Get quotient and remainder
Get stock price with Python
Get Evernote notes in Python
python> Get substring> print serial [5:10] / print serial [5: -1]
Get home directory with python
Get keyboard events with python
Python3 | Getting Started with numpy
(Python) Get AWS billing amount
Python --bitflyer Get chat content
Getting Started with Python Django (5)
Get Alembic information with Python
Python> dictionary> get ()> optional value
Get Japanese synonyms in Python
Get started with the Python framework Django on Mac OS X