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 1: | Line 1: | ||
{{Template:Start coding}} | {{Template:Start coding}} | ||
− | + | ||
Before getting started, you may be interested in [[Using the Python console with BlenderBIM Add-on]], to quickly install all the software and development environment you need for writing and running code, without any administrator privileges required. You may also find it useful watching [https://www.youtube.com/watch?v=WZPNaAM9ZuQ this video] which is complementary. | Before getting started, you may be interested in [[Using the Python console with BlenderBIM Add-on]], to quickly install all the software and development environment you need for writing and running code, without any administrator privileges required. You may also find it useful watching [https://www.youtube.com/watch?v=WZPNaAM9ZuQ this video] which is complementary. | ||
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> | </syntaxhighlight> | ||
Line 199: | Line 191: | ||
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. | ||
− | + | Note: this article is a work in progress. | |
+ | |||
+ | TODO: talk about [https://github.com/IfcOpenShell/IfcOpenShell/issues/866 naming in ids]. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
+ | import multiprocessing | ||
import ifcopenshell | import ifcopenshell | ||
import ifcopenshell.geom | import ifcopenshell.geom | ||
ifc_file = ifcopenshell.open('/path/to/your/file.ifc') | ifc_file = ifcopenshell.open('/path/to/your/file.ifc') | ||
− | |||
− | |||
− | |||
settings = ifcopenshell.geom.settings() | settings = ifcopenshell.geom.settings() | ||
− | + | iterator = ifcopenshell.geom.iterator(settings, ifc_file, multiprocessing.cpu_count()) | |
− | + | valid_file = iterator.initialize() | |
− | + | if valid_file: | |
− | + | while True: | |
− | + | shape = iterator.get() | |
− | + | element = ifc_file.by_guid(shape.guid) | |
− | + | if not iterator.next(): | |
− | + | break | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
=IFC Query Syntax= | =IFC Query Syntax= | ||
Line 319: | Line 273: | ||
# This gets all walls and slabs in a particular building storey | # This gets all walls and slabs in a particular building storey | ||
elements = selector.parse(ifc, '@ #0ehnsYoIDA7wC8yu69IDjv & ( .IfcWall | .IfcSlab )') | elements = selector.parse(ifc, '@ #0ehnsYoIDA7wC8yu69IDjv & ( .IfcWall | .IfcSlab )') | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 387: | Line 325: | ||
<entity IfcFlowSegment> | <entity IfcFlowSegment> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Get subtypes : | Get subtypes : | ||
Line 413: | Line 335: | ||
<entity IfcPipeSegment>) | <entity IfcPipeSegment>) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
=Property and quantity sets (Pset and Qto)= | =Property and quantity sets (Pset and Qto)= | ||
Line 467: | Line 388: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=Date and time in IFC= | =Date and time in IFC= | ||
Date and time conversion can be error prone. Fortunately IFC has chosen wide used definitions so standard python [https://docs.python.org/3/library/datetime.html datetime] and [https://docs.python.org/3/library/time.html time] modules have tools to handle most of them. | Date and time conversion can be error prone. Fortunately IFC has chosen wide used definitions so standard python [https://docs.python.org/3/library/datetime.html datetime] and [https://docs.python.org/3/library/time.html time] modules have tools to handle most of them. | ||
Line 500: | Line 399: | ||
# To convert it to datetime (UTC+1 in this example) | # To convert it to datetime (UTC+1 in this example) | ||
>>> import datetime | >>> import datetime | ||
− | >>> datetime | + | >>> datetime.fromtimestamp(0) |
− | datetime. | + | datetime.datetime(1970, 1, 1, 1, 0) |
− | >>> datetime | + | >>> datetime.fromtimestamp(1610829915) |
− | datetime. | + | datetime.datetime(2021, 1, 16, 21, 45, 15) |
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
==IfcDateTime== | ==IfcDateTime== | ||
[https://standards.buildingsmart.org/IFC/RELEASE/IFC4/ADD2_TC1/HTML/link/ifcdatetime.htm IfcDateTime] is stored in ISO 8601 format : YYYY-MM-DDThh:mm:ss | [https://standards.buildingsmart.org/IFC/RELEASE/IFC4/ADD2_TC1/HTML/link/ifcdatetime.htm IfcDateTime] is stored in ISO 8601 format : YYYY-MM-DDThh:mm:ss | ||
Line 558: | Line 456: | ||
!Additional infos | !Additional infos | ||
|- | |- | ||
− | | [ | + | | [http://academy.ifcopenshell.org/optimize-ifc-files/ http://academy.ifcopenshell.org/optimize-ifc-files/] |
| python | | python | ||
| 0.6.0-0d93633 | | 0.6.0-0d93633 | ||
|- | |- | ||
− | | [ | + | | [http://academy.ifcopenshell.org/calculate-differences-of-ifc-files-with-hashing/ Calculate Differences of IFC files with Hashing] |
| python | | python | ||
| 0.6.0-0d93633 | | 0.6.0-0d93633 | ||
Line 584: | Line 482: | ||
| FreeCAD 0.18 git | | FreeCAD 0.18 git | ||
|- | |- | ||
− | | [ | + | | [http://academy.ifcopenshell.org/using-ifcopenshell-and-c-to-generate-alignments-through-the-ifc-4x1-schema/ Using IfcOpenShell and C++ to generate Alignments through the IFC 4×1 schema] |
| python/C++ | | python/C++ | ||
|- | |- | ||
− | | [ | + | | [http://academy.ifcopenshell.org/creating-a-simple-wall-with-property-set-and-quantity-information/ Creating a simple wall with property set and quantity information] |
| python | | python | ||
|- | |- | ||
− | | [ | + | | [http://academy.ifcopenshell.org/using-ifcopenshell-and-pythonocc-to-generate-cross-sections-directly-from-an-ifc-file/ Using IfcOpenshell and pythonOCC to generate cross sections directly from an IFC file] |
| python | | python | ||
| <= 0.6 ? | | <= 0.6 ? | ||
| pythonOCC 0.16.0 ? | | pythonOCC 0.16.0 ? | ||
|- | |- | ||
− | |[ | + | |[http://academy.ifcopenshell.org/using-the-parsing-functionality-of-ifcopenshell-interactively/ Using the parsing functionality of IfcOpenShell interactively] |
| python | | python | ||
|- | |- | ||
− | |[ | + | |[http://academy.ifcopenshell.org/using-ifcopenshell-and-pythonocc-to-construct-new-geometry/ Using IfcOpenShell and pythonOCC to construct new geometry] |
| python | | python | ||
| <= 0.6 | | <= 0.6 |