{"id":822,"date":"2019-07-28T09:00:35","date_gmt":"2019-07-28T08:00:35","guid":{"rendered":"https:\/\/pythoncvc.net\/?p=822"},"modified":"2019-08-01T10:58:28","modified_gmt":"2019-08-01T09:58:28","slug":"ifcopenshell-read-geom-as-mesh","status":"publish","type":"post","link":"https:\/\/pythoncvc.net\/?p=822","title":{"rendered":"IfcOpenShell &#8211; Read geom as mesh"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/ifcopenshell.org\/\">IfcOpenShell<\/a> is a library used to work with <a href=\"https:\/\/technical.buildingsmart.org\/standards\/ifc\/\">IFC<\/a> schema. This library is licensed under LGPL 3.0 (libre and open source).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Some features included :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Parse ifc<\/li><li>Create geom from ifc representation<\/li><li>Display geom using pythonOCC<\/li><li>Ifc importer for Blender<\/li><li>Ifc importer for 3ds Max<\/li><li>Convert geometry to many formats<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Some projects using IfcOpenShell :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.freecadweb.org\/\">FreeCAD<\/a> : parametric CAD modeler including a BIM workbench<\/li><li><a href=\"http:\/\/bimserver.org\/\">BIMserver<\/a> : Multi-user self-hostable BIM platform with a plugin ecosystem to view, analyse, merge etc\u2026<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In this article we will use a function which generate a mesh from an ifc entity in our case an IfcWall and see what we get.<br>A simple wall created with FreeCAD and exported to .ifc :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"486\" height=\"427\" src=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/07\/image.png\" alt=\"\" class=\"wp-image-830\" srcset=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/07\/image.png 486w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/07\/image-300x264.png 300w\" sizes=\"auto, (max-width: 486px) 100vw, 486px\" \/><figcaption>Base wall created in FreeCAD<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Prerequisite : IfcOpenShell installed. Version used here : 0.6.0a1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First we need to import ifcopenshell and open the ifc file :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import ifcopenshell\nfrom ifcopenshell import geom\n\n\ndef read_geom(ifc_path):\n\n    ifc_file = ifcopenshell.open(ifc_path)\n\n    settings = geom.settings()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>geom.settings<\/code> is used to set conversion options. By default, ifcopenshell generate a mesh with vertices, edges and faces. <code>ifc_file.by_type(\"IfcClass\")<\/code> is a very handy way to get all element of a chosen class (including subclass). So if for example you <a href=\"https:\/\/standards.buildingsmart.org\/IFC\/DEV\/IFC4_2\/FINAL\/HTML\/link\/ifcbuildingelement.htm\">IfcBuildingElement<\/a> it will also include IfcWall, IfcWindow, IfcSlab, IfcBeam etc\u2026<br><code>geom.create_shape(settings, ifc_entity)<\/code> is the function which convert the ifc entity into a mesh. We can observe that vertices are stored in a single tuple not by xyz triplet. Same for edges and faces.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    for ifc_entity in ifc_file.by_type(\"IfcWall\"):\n        shape = geom.create_shape(settings, ifc_entity)\n        # ios stands for IfcOpenShell\n        ios_vertices = shape.geometry.verts\n        ios_edges = shape.geometry.edges\n        ios_faces = shape.geometry.faces\n\n        # IfcOpenShell store vertices in a single tuple, same for edges and faces\n        print(ios_vertices)\n        print(ios_edges)\n        print(ios_faces)\n        \"\"\" Above will result in :\n(0.0, 0.0, 0.0, 0.0, 0.0, 3.0, 10.0, 0.0, 0.0, 10.0, 0.0, 3.0, 10.0, 0.2, 0.0, 10.0, 0.2, 3.0, 0.0, 0.2, 0.0, 0.0, 0.2, 3.0)\n(0, 1, 1, 3, 0, 2, 2, 3, 2, 3, 2, 4, 3, 5, 4, 5, 4, 5, 5, 7, 4, 6, 6, 7, 6, 7, 0, 6, 1, 7, 0, 1, 0, 6, 0, 2, 4, 6, 2, 4, 1, 7, 1, 3, 5, 7, 3, 5)\n(1, 0, 3, 3, 0, 2, 3, 2, 4, 5, 3, 4, 5, 4, 7, 7, 4, 6, 7, 6, 0, 1, 7, 0, 0, 6, 2, 2, 6, 4, 3, 7, 1, 5, 7, 3)\n\"\"\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It is obvious that vertices are x,y,z triplet one by one. But how are defined edges and faces ? An edge is a line bounded by 2 vertices but values we see are not vertices. A face in a mesh is a triangle surface bounded by 3 vertices and 3 edges. If we make a set of edges and faces values we get a set of length 8.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">        print(set(ios_edges))\n        print(set(ios_faces))\n        \"\"\" Above will result in :\n{0, 1, 2, 3, 4, 5, 6, 7}\n{0, 1, 2, 3, 4, 5, 6, 7}\n\"\"\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If we group vertices by by 3 values (x,y,z), edges by 2 (vertex1, vertex2), and faces by 3 (3 vertices or 3 edges) we see that our wall geometry is defined by 8 vertices, 24 edges and 12 faces. Edges and faces values are both referring vertices indexes.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">        # Let's parse it and prepare it for FreeCAD import\n        vertices = [\n            FreeCAD.Vector(ios_vertices[i : i + 3])\n            for i in range(0, len(ios_vertices), 3)\n        ]\n        edges = [ios_edges[i : i + 2] for i in range(0, len(ios_edges), 2)]\n        faces = [tuple(ios_faces[i : i + 3]) for i in range(0, len(ios_faces), 3)]\n\n        print(\n            f\"This {ifc_entity.is_a()} has been defined by {len(vertices)} vertices, {len(edges)} edges and {len(faces)} faces\"\n        )\n        print(vertices)\n        print(edges)\n        print(faces)\n        \"\"\" Above will result in :\nThis IfcWall has been defined by 8 vertices, 24 edges and 12 faces\n[(0.0, 0.0, 0.0), (0.0, 0.0, 3.0), (10.0, 0.0, 0.0), (10.0, 0.0, 3.0), (10.0, 0.2, 0.0), (10.0, 0.2, 3.0), (0.0, 0.2, 0.0), (0.0, 0.2, 3.0)]\n[(0, 1), (1, 3), (0, 2), (2, 3), (2, 3), (2, 4), (3, 5), (4, 5), (4, 5), (5, 7), (4, 6), (6, 7), (6, 7), (0, 6), (1, 7), (0, 1), (0, 6), (0, 2), (4, 6), (2, 4), (1, 7), (1, 3), (5, 7), (3, 5)]\n[(1, 0, 3), (3, 0, 2), (3, 2, 4), (5, 3, 4), (5, 4, 7), (7, 4, 6), (7, 6, 0), (1, 7, 0), (0, 6, 2), (2, 6, 4), (3, 7, 1), (5, 7, 3)]\n\"\"\"\n        return {\"vertices\": vertices, \"edges\": edges, \"faces\": faces}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Of course FreeCAD already has a better way to import an IfcWall but let&rsquo;s use our mesh to generate a geometry :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import FreeCAD\nimport FreeCADGui\nimport Mesh\n\nif __name__ == \"__main__\":\n    mesh_values = read_geom(\n        \"\/home\/cyril\/git\/pythoncvc.net\/IfcOpenShellSamples\/Wall.ifc\"\n    )\n\n    # Create a FreeCAD geometry. A FreeCAD can take vertices and faces as input\n    mesh = Mesh.Mesh((mesh_values[\"vertices\"], mesh_values[\"faces\"]))\n    # Ifc lenght internal unit : meter. FreeCAD internal unit : mm.\n    scale_factor = 1000\n    matrix = FreeCAD.Matrix()\n    matrix.scale(scale_factor, scale_factor, scale_factor)\n    mesh.transform(matrix)\n\n    # Allow you to embed FreeCAD in python https:\/\/www.freecadweb.org\/wiki\/Embedding_FreeCAD\n    FreeCADGui.showMainWindow()\n    doc = FreeCAD.newDocument()\n\n    # Add geometry to FreeCAD scenegraph (Coin)\n    fc_mesh = doc.addObject(\"Mesh::Feature\", \"IfcMesh\")\n    fc_mesh.Mesh = mesh\n\n    # Set Draw Style to display mesh edges. Orient view and fit to wall\n    FreeCADGui.runCommand(\"Std_DrawStyle\",1) \n    FreeCADGui.Selection.addSelection(fc_mesh)\n    FreeCADGui.activeView().viewIsometric()\n    FreeCADGui.SendMsgToActiveView(\"ViewSelection\")\n    \n    FreeCADGui.exec_loop()<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"575\" src=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/07\/image-2-1024x575.png\" alt=\"\" class=\"wp-image-833\" srcset=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/07\/image-2-1024x575.png 1024w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/07\/image-2-300x168.png 300w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/07\/image-2-768x431.png 768w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/07\/image-2-620x348.png 620w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/07\/image-2.png 1873w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Resulting geometry in FreeCAD<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Mesh is fast to display but it is usually not what you want to use in a BIM authoring software. So next time we will see how to generate a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Boundary_representation\">boundary representation<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Full source code is available <a href=\"https:\/\/github.com\/CyrilWaechter\/pythoncvc.net\/tree\/master\/IfcOpenShellSamples\">here<\/a>.<br>But it will be moved soon out of github due to recent discrimination toward some country : <a href=\"https:\/\/github.com\/1995parham\/github-do-not-ban-us\">https:\/\/github.com\/1995parham\/github-do-not-ban-us<\/a><br>Star the repo and share !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IfcOpenShell is a library used to work with IFC schema. This library is licensed under LGPL 3.0 (libre and open source). Some features included : Parse ifc Create geom from ifc representation Display geom using pythonOCC Ifc importer for Blender Ifc importer for 3ds Max Convert geometry to many formats Some projects using IfcOpenShell : [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[203,183,80],"tags":[193,192,194,82],"class_list":["post-822","post","type-post","status-publish","format-standard","hentry","category-freecad","category-ifcopenshell","category-python","tag-freecad","tag-ifcopenshell","tag-mesh","tag-python"],"_links":{"self":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts\/822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=822"}],"version-history":[{"count":12,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts\/822\/revisions"}],"predecessor-version":[{"id":837,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts\/822\/revisions\/837"}],"wp:attachment":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}