{"id":851,"date":"2019-08-13T09:42:18","date_gmt":"2019-08-13T08:42:18","guid":{"rendered":"https:\/\/pythoncvc.net\/?p=851"},"modified":"2019-08-13T09:42:19","modified_gmt":"2019-08-13T08:42:19","slug":"ifcopenshell-placement","status":"publish","type":"post","link":"https:\/\/pythoncvc.net\/?p=851","title":{"rendered":"IfcOpenShell &#8211; Placement"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If <a href=\"https:\/\/pythoncvc.net\/?p=839\">previous article<\/a> we used IfcOpenShell\u2019s (IOS) to read an ifc geometry and convert it to a brep. When we read wikilab.ifc everything seemed to be at the right place but was it really ? When you use BIM in your project coordinates is always a subject to correctly discuss. In that matter I recommend you to read Dion Moult&rsquo;s article <a href=\"https:\/\/thinkmoult.com\/ifc-coordinate-reference-systems-and-revit.html\">IFC Coordinate Reference Systems and Revit<\/a> and references cited in the article.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IfcOpenShell version used : 0.6.0a1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Placement in IFC schema<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A geometry generally has its own Local Coordinate System (LCS). Why ? Take for example an air terminal. You often use a few types of air terminal in a project. The same air terminal geometry is replicated multiple time in each space. If your geometry is defined with World Coordinate System (WCS) you need to define a new geometry for each one. If your geometry is defined in its own LCS you can use same geometry everywhere and give its placement relative to another reference.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In ifc schema, an air terminal is a <a href=\"https:\/\/standards.buildingsmart.org\/IFC\/RELEASE\/IFC4_1\/FINAL\/HTML\/link\/ifcairterminal.htm\">IfcAirTerminal<\/a> (IFC4) or an <a href=\"https:\/\/standards.buildingsmart.org\/IFC\/RELEASE\/IFC2x3\/FINAL\/HTML\/ifcsharedbldgserviceelements\/lexical\/ifcflowterminal.htm\">IfcFlowTerminal<\/a> (IFC2x3). Both are inherited from <a href=\"https:\/\/standards.buildingsmart.org\/IFC\/RELEASE\/IFC4_1\/FINAL\/HTML\/link\/ifcproduct.htm\">IfcProduct<\/a> (as walls, windows, ducts, pipes etc\u2026) which has an ObjectPlacement attribute of type <a href=\"https:\/\/standards.buildingsmart.org\/IFC\/RELEASE\/IFC4_1\/FINAL\/HTML\/schema\/ifcgeometricconstraintresource\/lexical\/ifcobjectplacement.htm\">IfcObjectPlacement<\/a>. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Placement of the product in space, the placement can either be absolute (relative to the world coordinate system), relative (relative to the object placement of another product), or constraint (e.g. relative to grid axes). It is determined by the various subtypes of IfcObjectPlacement, which includes the axis placement information to determine the transformation for the object coordinate system.<\/p><cite><a href=\"https:\/\/standards.buildingsmart.org\/IFC\/RELEASE\/IFC4_1\/FINAL\/HTML\/link\/ifcproduct.htm\">ObjectPlacement<\/a> attribute definition from IFC documentation<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/standards.buildingsmart.org\/IFC\/RELEASE\/IFC4_1\/FINAL\/HTML\/link\/ifclocalplacement.htm\">IfcLocalPlacement<\/a> subtype, which I think is the most common one, has 2 attributes : <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>#1 PlacementRelTo of type <a href=\"https:\/\/standards.buildingsmart.org\/IFC\/RELEASE\/IFC4_1\/FINAL\/HTML\/schema\/ifcgeometricconstraintresource\/lexical\/ifcobjectplacement.htm\">IfcObjectPlacement<\/a>. If filled placement is relative. If empty placement use WCS.<\/li><li>#2 RelativePlacement is basically a 2D or 3D coordinate.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Relative placement can be chained eg. AirTerminal &lt;&lt; space &lt;&lt; buildingstorey &lt;&lt; building &lt;&lt; Site. To retrieve an object placement relative to WCS you need to transform your first relative placement through the complete chain. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Placement in IfcOpenShell<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">IfcOpenShell can be used as a parser to get placement exactly as defined in IfcSchema. However it also has handy tools to avoid crawling the whole relative placement chain. The option to look at to understand this is <code><a href=\"https:\/\/github.com\/IfcOpenShell\/IfcOpenShell\/blob\/8c0bcc4ce75e933b11a8fdd460c339a42ade0a3b\/src\/ifcgeom\/IfcGeomIteratorSettings.h#L41\">USE_WORLD_COORDS<\/a><\/code> :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\/ Specifies whether to apply the local placements of building elements\n\/\/\/ directly to the coordinates of the representation mesh rather than\n\/\/\/ to represent the local placement in the 4x3 matrix, which will in that\n\/\/\/ case be the identity matrix.\nUSE_WORLD_COORDS = 1 &lt;&lt; 1,<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you display geom as we did in previous articles coordinates were given using the geometry LCS. It was not visible as our wall placement was at coordinates <code>0,0,0<\/code>. Let&rsquo;s now use the script from ifcopenshell academy <a href=\"http:\/\/academy.ifcopenshell.org\/creating-a-simple-wall-with-property-set-and-quantity-information\/\">Creating a simple wall with property set and quantity information<\/a> to generate a new wall with a placement at <code>20,10,3<\/code>. By default, script also generate a wall at <code>0,0,0<\/code>. To modify it&rsquo;s relative placement we need to modify following line :<\/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=\"\">wall_placement = create_ifclocalplacement(ifcfile, relative_to=storey_placement)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Considering function definition we need to replace this line with :<\/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=\"\">wall_placement = create_ifclocalplacement(ifcfile, point=(20., 10., 3.), relative_to=storey_placement)\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now if you try to load geometry from this newly created <code>hello_wall.ifc<\/code> according to our <a href=\"https:\/\/pythoncvc.net\/?p=839\">previous script<\/a> you will see that the wall is still at <code>0,0,0<\/code> which is wrong.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"508\" src=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-3-1024x508.png\" alt=\"\" class=\"wp-image-857\" srcset=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-3-1024x508.png 1024w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-3-300x149.png 300w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-3-768x381.png 768w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-3-620x308.png 620w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-3.png 1400w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Wall incorrectly placed<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To place it correctly we have 2 option using matrix transformation and <code>USE_WORLD_COORDINATES<\/code> settings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using placement matrix<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As stated in its source code when you create a geom. Its location is given by a 4&#215;3 matrix. But care, when displayed as a tuple IfcOpenShell matrix and FreeCAD matrix are transposed :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>IfcOpenShell matrix values is a tuple of 4 consecutive vector&rsquo;s xyz :<br>format : (v1.x, v1.y, v1.z, v2.x, v2.y \u2026 , v4.z)<\/li><li>FreeCAD matrix constructor takes up to 16 float. 4 vectors grouped by x, y, z values :<br>format : (v1.x, v2.x, v3.x, v4.x, v1.y, \u2026 , v4.z, 0, 0, 0, 1)<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">About the last <code>0, 0, 0, 1<\/code> of FreeCAD matrix. As  greatly stated in matrices chapter from the <a href=\"http:\/\/www.opengl-tutorial.org\/beginners-tutorials\/tutorial-3-matrices\/\">OpenGL tutorial<\/a>  :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>This will be more clear soon, but for now, just remember this :<br>   If w == 1, then the vector (x,y,z,1) is a position in space.<br>   If w == 0, then the vector (x,y,z,0) is a direction. <br> (In fact, remember this forever.)<\/p><cite>http:\/\/www.opengl-tutorial.org\/beginners-tutorials\/tutorial-3-matrices\/<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">So we&rsquo;ll use a function to transpose an IfcOpenShell matrix to a FreeCAD matrix :<\/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=\"\">def ios_to_fc_matrix(ios_matrix):\n    m_l = list()\n    for i in range(3):\n        line = list(ios_matrix[i::3])\n        line[-1] *= SCALE\n        m_l.extend(line)\n    return FreeCAD.Matrix(*m_l)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After our shape creation we call then call it to give our object a placement :<\/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=\"\">    # Create FreeCAD shape from Open Cascade BREP\n    fc_shape = Part.Shape()\n    fc_shape.importBrepFromString(occ_shape)\n    \n    # Ifc lenght internal unit : meter. FreeCAD internal unit : mm.\n    fc_shape.scale(SCALE)\n    # Shape scale must be applied before shape placement else placement scale would be doubled\n    fc_shape.Placement = ios_shape_to_fc_placement(ios_shape)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This way we get a correctly placed wall :<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"508\" src=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-4-1024x508.png\" alt=\"\" class=\"wp-image-858\" srcset=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-4-1024x508.png 1024w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-4-300x149.png 300w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-4-768x381.png 768w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-4-620x308.png 620w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-4.png 1401w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Wall placement using matrix transformation<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">USE_WOLD_COORDINATES<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To define settings still the same process :<\/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=\"\">settings.set(settings.USE_WORLD_COORDS, True)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can keep the same code base as it will apply the identity matrix which doesn&rsquo;t change anything. By running the script you&rsquo;ll get again a correctly placed wall but this time position will still be <code>0, 0, 0<\/code> which means that the geometry vertices coordinates are defined with their absolute coordinates :<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"509\" src=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-5-1024x509.png\" alt=\"\" class=\"wp-image-860\" srcset=\"https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-5-1024x509.png 1024w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-5-300x149.png 300w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-5-768x382.png 768w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-5-620x308.png 620w, https:\/\/pythoncvc.net\/wp-content\/uploads\/2019\/08\/image-5.png 1401w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Wall placement using <code>USE_WORLD_COORDINATES<\/code> setting<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For reason explained in first part I tend to say that this solution is not the best choice for CAD\/BIM work in HVAC domain. Currently FreeCAD import and export ifc this way I will investigate to see why and maybe launch a this topic on forum. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Full source code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Source code is available in <code><a href=\"https:\/\/framagit.org\/pythonhvac\/pythoncvc-net\/blob\/master\/IfcOpenShellSamples\/ifc_placement.py\">ifc_placement.py<\/a><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If previous article we used IfcOpenShell\u2019s (IOS) to read an ifc geometry and convert it to a brep. When we read wikilab.ifc everything seemed to be at the right place but was it really ? When you use BIM in your project coordinates is always a subject to correctly discuss. In that matter I recommend [&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],"tags":[193,192,271,269,268,270],"class_list":["post-851","post","type-post","status-publish","format-standard","hentry","category-freecad","category-ifcopenshell","tag-freecad","tag-ifcopenshell","tag-lcs","tag-matrix","tag-placement","tag-wcs"],"_links":{"self":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts\/851","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=851"}],"version-history":[{"count":9,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts\/851\/revisions"}],"predecessor-version":[{"id":863,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts\/851\/revisions\/863"}],"wp:attachment":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}