{"id":3631,"date":"2012-09-10T00:49:20","date_gmt":"2012-09-10T07:49:20","guid":{"rendered":"http:\/\/www.iearobotics.com\/blog\/?p=3631"},"modified":"2013-01-28T08:20:57","modified_gmt":"2013-01-28T15:20:57","slug":"enhancing-openscad-with-the-attach-library","status":"publish","type":"post","link":"http:\/\/www.iearobotics.com\/blog\/2012\/09\/10\/enhancing-openscad-with-the-attach-library\/","title":{"rendered":"Enhancing Openscad with the attach library"},"content":{"rendered":"<p><a href=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/blog-attach-01.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/blog-attach-01.png\" alt=\"\" title=\"blog-attach-01\" width=\"500\" height=\"446\" class=\"aligncenter size-full wp-image-3632\" srcset=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/blog-attach-01.png 500w, http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/blog-attach-01-300x267.png 300w\" sizes=\"auto, (max-width: 500px) 85vw, 500px\" \/><\/a><\/p>\n<p><H2> Looking into the future <\/h2>\n<p>  <a href=\"http:\/\/www.openscad.org\/\">OpenScad<\/a> is one of those tools that opens your mind. It falls into the category of <strong>mechanical description languages<\/strong>. Instead of the typical Graphical User Interface (GUI) for designing parts, OpenScad generates the parts from the code typed by the user. This way, <strong>the mechanical parts are now pieces of code<\/strong> that can be easily documented, shared among users, uploaded into repositories and so forth.<\/p>\n<p>  One of the advantages of this approach is that the code can also be generated by other programs, opening the doors to <strong>future design tools that can create physical models from high level specifications<\/strong>. Imagine a software tool able to create robots from the user requests: &#8220;I want a mobile robot with tracks, a size lower that xxxx &#8230;&#8221;. The tool will be in charge of calculating everything and finally offering the user the prototypes that best fit its needs. These tools will also be able to use evolutionary algorithms to build optimized robots or parts.<\/p>\n<p><a href=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/ooml-logo.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/ooml-logo.jpg\" alt=\"\" title=\"ooml-logo\" width=\"400\" height=\"186\" class=\"aligncenter size-full wp-image-3689\" srcset=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/ooml-logo.jpg 400w, http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/ooml-logo-300x139.jpg 300w\" sizes=\"auto, (max-width: 400px) 85vw, 400px\" \/><\/a><\/p>\n<p>  For programming such tools, we need to combine the power of a standard programming language with the ability to create physical parts. That is one of the goal of the <a href=\"http:\/\/iearobotics.com\/oomlwiki\/doku.php?id=start\">Object Oriented Mechanical Library<\/a> (OOML): applying the object oriented paradigm for designing parts (in C++). It is a <strong>research project for developing new paradigm for designing mechanics<\/strong>.<\/p>\n<h2> Back to earth <\/h2>\n<p>In the meanwhile, OpenScad is a great tool (and very extended) for developing mechanical parts. There are <a href=\"http:\/\/www.thingiverse.com\/tag:openscad\">thousands of designs at thingiverse<\/a> (tagged with the word &#8220;openscad&#8221;). Although it is rather easy to design with it, sometimes understanding how others have implemented their parts is a complex task. Even if the code is made by yourself, after some time, it is rather complicated to understand it.  There are plenty of <em>translate<\/em>, <em>rotate<\/em> and other operators nested in deep blocks of code.  Many times is easier to start from scratch than to re-use some code made by others.<\/p>\n<p><a href=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/Miniskybot-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/Miniskybot-1.png\" alt=\"\" title=\"Miniskybot-1\" width=\"400\" height=\"370\" class=\"aligncenter size-full wp-image-3696\" srcset=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/Miniskybot-1.png 400w, http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/Miniskybot-1-300x277.png 300w\" sizes=\"auto, (max-width: 400px) 85vw, 400px\" \/><\/a><\/p>\n<p>  In order to make the code more readable and reusable, I am working on small tools for enhancing openscad designs. One of them is the <a href=\"http:\/\/www.thingiverse.com\/thing:30014\">vector library<\/a>. Now I cannot live without it \ud83d\ude42  The new tool I am presenting here is the <strong><a href=\"http:\/\/www.thingiverse.com\/thing:30136\">attach library<\/a><\/strong>.<\/p>\n<h2>Attaching parts<\/h2>\n<p>When designing is rather common to design separated sub-parts as modules that are connected together later. In complex multipart-objects, such as robots, it is also useful to connect all the parts to see how the final design looks.<\/p>\n<p>It is not difficult to do such operations in openscad by means of the translate and rotate operators&#8230; but <strong>it generates a very difficult to understand code<\/strong>. Here comes the <a href=\"http:\/\/www.thingiverse.com\/thing:30136\">attach operator<\/a> in our help.<\/p>\n<h2>Connectors<\/h2>\n<p>In order to write re-usable and understandable code, we have to <strong>separated the part data from the code<\/strong>. The first give us the information of the part: size, drills &#8230;  The second tell us how the object is built from the data (algorithms or approaches). One can choose different algorithms or approaches for building the object from the same data. Separating the data from code makes it possible for others to re-implement the algorithms, therefore improving the parts.<\/p>\n<p>  One way of achieving it (for attaching parts) is using <strong>connectors<\/strong>, defined as a list of 3 elements: <em>the attachment point<\/em>, <em>the attachment axis<\/em> and the <em>roll<\/em> angle.<\/p>\n<h2>Example: defining and using connectors<\/h2>\n<p><a href=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-02.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-02.png\" alt=\"\" title=\"attach-02\" width=\"500\" height=\"446\" class=\"aligncenter size-full wp-image-3650\" srcset=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-02.png 500w, http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-02-300x267.png 300w\" sizes=\"auto, (max-width: 500px) 85vw, 500px\" \/><\/a><br \/>\nLet&#8217;s work on a simple example. Imagine we have a main part in which we want to connect more parts. For simplification, let&#8217;s assume the main part is a cube. The first thing to do is to <strong>define the connectors<\/strong>. We want to have one on the top and another on the left. Given the cube size by:<br \/>\n<code><br \/>\nsize = [20,20,20];<br \/>\n<\/code><br \/>\nthe two connectors can be declared like this:<br \/>\n<code><br \/>\n\/\/--     att. point     att. axis    roll<br \/>\nc1 = [ [0,0,size[2]\/2],  [0,0,1],     20 ];  \/\/-- Top<br \/>\nc2 = [ [-size[0]\/2,0,0], [-1,0,0],    -30 ]; \/\/-- Left<br \/>\n<\/code><br \/>\nummm&#8230; not very clear up to now&#8230; But here comes the <strong>connector()<\/strong> module in our help: we can see the cube with its connectors on the screen very easily (See the above image):<\/p>\n<p><code><br \/>\nconnector(c1); \/\/-- Con. 1<br \/>\nconnector(c2); \/\/-- Con. 2<br \/>\nframe(l=8);    \/\/-- See the frame of reference<br \/>\n\/\/-- Draw the Main part (transparent)<br \/>\ncolor(\"Yellow\",0.5)<br \/>\n  cube(size,center=true);<br \/>\n<\/code><br \/>\nNow we can see the main part (a transparent cube) along with its frame of reference (module frame()) and the two connectors in gray. The graphical connectors represent their three components with different objects: a <strong>sphere<\/strong> on the base for the <em>attachment point<\/em>, a <strong>vector<\/strong> for the <em>attachment axis<\/em>, and a <strong>square mark<\/strong> on the vector head for the <em>rolling<\/em> angle.<br \/>\n<a href=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-03.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-03.png\" alt=\"\" title=\"attach-03\" width=\"400\" height=\"357\" class=\"aligncenter size-full wp-image-3654\" srcset=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-03.png 400w, http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-03-300x267.png 300w\" sizes=\"auto, (max-width: 400px) 85vw, 400px\" \/><\/a><br \/>\nLet&#8217;s build the part we want to attach to the main body. We write it like a module, so that the part can be re-used. Again, it is a simplified part, just to show you a simple example. First the part data: <em>size<\/em> and one <em>connector<\/em>:<\/p>\n<p><code><br \/>\nasize = [10,40,3];<br \/>\n\/\/-- Connector<br \/>\na = [ [0, asize[1]\/2-3,-asize[2]\/2], [0,0,1],  0];<br \/>\n<\/code><\/p>\n<p>the module:<\/p>\n<p><em>module arm(debug=false)<br \/>\n{<br \/>\n&nbsp; \/\/&#8211; Debug mode: show the connector and frame of ref<br \/>\n&nbsp;  if (debug) {<br \/>\n &nbsp;&nbsp;   frame(l=10);<br \/>\n &nbsp;&nbsp;  connector(a);<br \/>\n&nbsp;  }<\/p>\n<p> &nbsp; color(&#8220;Brown&#8221;,0.5)<br \/>\n &nbsp; difference() {<br \/>\n &nbsp;&nbsp;  cube(asize,center=true);<\/p>\n<p> &nbsp;&nbsp;  translate([0, -asize[1]\/2,0])<br \/>\n &nbsp;&nbsp;cube([asize[0]\/2, asize[1]\/3, asize[2]+1],center=true);<br \/>\n &nbsp;}<br \/>\n}<\/em><\/p>\n<p>Notice that the module has a <strong>debug flag<\/strong>, so that when activated the frame of reference and connector are shown:<br \/>\n<code><br \/>\narm(debug=true);<br \/>\n<\/code><\/p>\n<h2>One, two, three&#8230;Attach!<\/h2>\n<p><a href=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-04.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-04.png\" alt=\"\" title=\"attach-04\" width=\"500\" height=\"446\" class=\"aligncenter size-full wp-image-3672\" srcset=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-04.png 500w, http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-04-300x267.png 300w\" sizes=\"auto, (max-width: 500px) 85vw, 500px\" \/><\/a><br \/>\nAttaching the parts is now a piece of cake:<br \/>\n<code><br \/>\nattach(c1,a) arm(debug=true);<br \/>\nattach(c2,a) arm(debug=true);<br \/>\n<\/code><\/p>\n<p>The meaning of the first sentence is pretty clear:  Attach the connector <em>a<\/em> of the <em>arm<\/em> part to the connector <strong>c1<\/strong> of the main part.<\/p>\n<p>The arm parts are rotated  a &#8220;roll&#8221; angle when attaching. This was the third parameter of the connector. Just changing it, the arms will point to a different place:<br \/>\n<code><br \/>\n\/\/--     att. point     att. axis    roll<br \/>\nc1 = [ [0,0,size[2]\/2],  [0,0,1],     <strong>80<\/strong> ];<br \/>\nc2 = [ [-size[0]\/2,0,0], [-1,0,0],    <strong>90<\/strong> ];<br \/>\n<\/code><br \/>\n<a href=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-05.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-05.png\" alt=\"\" title=\"attach-05\" width=\"500\" height=\"446\" class=\"aligncenter size-full wp-image-3675\" srcset=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-05.png 500w, http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-05-300x267.png 300w\" sizes=\"auto, (max-width: 500px) 85vw, 500px\" \/><\/a><\/p>\n<p>Imagine that a user wants to attach a different part. It is straightforward now to understand the code&#8230; just change the arm module by the new part:<br \/>\n<code><br \/>\nattach(c1,b) new_part();<br \/>\n<\/code><\/p>\n<p>where b is the connector of the new part.<\/p>\n<h2>Limitations<\/h2>\n<p><a href=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-061.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-061.png\" alt=\"\" title=\"attach-06\" width=\"500\" height=\"446\" class=\"aligncenter size-full wp-image-3719\" srcset=\"http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-061.png 500w, http:\/\/www.iearobotics.com\/blog\/wp-content\/uploads\/2012\/09\/attach-061-300x267.png 300w\" sizes=\"auto, (max-width: 500px) 85vw, 500px\" \/><\/a><\/p>\n<p>That is how the part looks once you turn off the debug flag. <\/p>\n<p>  The attach operator seems to be very powerful and useful.. and it is.. but with limitations.  Unfortunately <strong>Openscad does not allow defining recursive modules<\/strong> (At least with the 2012-02-22 release).. so when you try to use it on a module that already uses the attach operator&#8230;. <strong>you will get a warning and your part will not be made&#8230; <\/strong><\/p>\n<p>   but &#8230;hey! It is <strong>opensource software!<\/strong> Yes! it is! So, if the attach operator is useful and the community start using it, it can be implemented natively as an openscad operator&#8230;  or it can be implemented in higher level mechanical description languages, like ooml (which already include it).<\/p>\n<p>Happy mechanics geeking!<\/p>\n<p><a href=\"http:\/\/www.iearobotics.com\/wiki\/index.php?title=Juan_Gonzalez:Main\">Obijuan<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking into the future OpenScad is one of those tools that opens your mind. It falls into the category of mechanical description languages. Instead of the typical Graphical User Interface (GUI) for designing parts, OpenScad generates the parts from the code typed by the user. This way, the mechanical parts are now pieces of code &hellip; <a href=\"http:\/\/www.iearobotics.com\/blog\/2012\/09\/10\/enhancing-openscad-with-the-attach-library\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Enhancing Openscad with the attach library&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,19,10],"tags":[],"class_list":["post-3631","post","type-post","status-publish","format-standard","hentry","category-plastic-valley","category-printbots","category-reprap"],"_links":{"self":[{"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/posts\/3631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/comments?post=3631"}],"version-history":[{"count":86,"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/posts\/3631\/revisions"}],"predecessor-version":[{"id":3811,"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/posts\/3631\/revisions\/3811"}],"wp:attachment":[{"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/media?parent=3631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/categories?post=3631"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.iearobotics.com\/blog\/wp-json\/wp\/v2\/tags?post=3631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}