{"id":124,"date":"2018-03-19T18:04:09","date_gmt":"2018-03-19T18:04:09","guid":{"rendered":"http:\/\/www.vandelle.com\/blog\/3dprinting\/?p=124"},"modified":"2018-03-19T18:04:09","modified_gmt":"2018-03-19T18:04:09","slug":"modelize-your-first-3d-shape-with-openscad-4","status":"publish","type":"post","link":"https:\/\/www.vandelle.com\/blog\/3dprinting\/2018\/03\/19\/modelize-your-first-3d-shape-with-openscad-4\/","title":{"rendered":"Modelize your first 3D shape with openSCAD #4"},"content":{"rendered":"<h2>4. Good practice<\/h2>\n<p>Like in all programing languages there is good practice you must do to ensure that everyone can understand your code and also to be easier to reread. In openSCAD it\u2019s not so essential to do some of this good practice because the code is quite easy to understand but I definitely recommend you to do it.<\/p>\n<h3><\/h3>\n<h3>a. Commentary<\/h3>\n<p>To do a commentary in openSCAD you can use \/\/ to comment 1 line or \/* at the start and *\/ at end to comment an entire block of code.<\/p>\n<pre>\/\/this is a commentary\n\nCube(10) \/\/this is a cube<\/pre>\n<pre>\/*\n\nhull() {\n\n\u00a0\u00a0 cube([20,20,1],center=true);\n\n\u00a0\u00a0 translate([0,0,20]) cylinder(1,8,8);\n\n}*\/<\/pre>\n<p>Commentary can be used to document your code or to comment code you don\u2019t need for the moment.<\/p>\n<h3>b. Indentation<\/h3>\n<p>Indentation is when you do space in your code. Indentation is essential to have a clear code:<\/p>\n<pre>\nhull() {\n\n<span style=\"color: #ff0000\">\u2026\u2026\u2026\u2026\u2026\u2026.<\/span>cube([20,20,1],center=true);\n\n<span style=\"color: #ff0000\">\u2026\u2026\u2026\u2026\u2026\u2026.<\/span>translate([0,0,20]) cylinder(1,8,8);\n\n\u00a0\u00a0\u00a0 }\n\n<\/pre>\n<pre>scale([1.3,0.9,1])\n\nlinear_extrude(height=2)\n\n\u00a0     difference() {\n\n\u00a0           difference() {\n\n\u00a0\u00a0\u00a0                circle(r=18);\n\n\u00a0\u00a0\u00a0                union() {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0                   translate([0,2,0]) circle(r=14);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0                   translate([-60,0,0]) square([60,60]);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0                   translate([-40,-40,0]) rotate([0,0,55]) square([60,60]);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0                   translate([-9,-14.2,0]) rotate([0,0,-20]) square([7,3]);\n\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0             }\n\n             }\n\n}<\/pre>\n<h3>c. Global variables<\/h3>\n<p>Global variables are in programming when you give at the beginning of the program a value that can be reused everywhere in your code. To do this you need to use the \u201c=\u201d operator with the syntax: name = value; .<\/p>\n<p>Example:<\/p>\n<pre>my_height = 20;\n\nmy_radius = 5;\n\ncylinder(my_height,19,19);\n\nsphere(my_radius)\n\n<\/pre>\n<p>Global variables are very useful when you want for example the same height for your cylinder in your code and if one day you want to change it because your cylinders are to small, you just need to change the value of the variable and not of each cylinder.<\/p>\n<h3>d. Colors<\/h3>\n<p>One last good practice I like to do is to put colors on each of my objects to see them clearly in my view. The syntax for color is color(&#8220;your color&#8221;). Basic color like black, green, red\u2026 exist yet in the openSCAD kit but if you need more colors you can use RGB with this syntax : <a href=\"https:\/\/en.wikibooks.org\/wiki\/OpenSCAD_User_Manual\/Transformations#color\" target=\"_blank\" rel=\"noopener\">color<\/a>([r,g,b,a]).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-70\" src=\"http:\/\/www.vandelle.com\/blog\/3dprinting\/wp-content\/uploads\/sites\/6\/2018\/06\/Lars2018-300x245.png\" alt=\"\" width=\"320\" height=\"261\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>4. Good practice Like in all programing languages there is good practice you must do to ensure that everyone can understand your code and also to be easier to reread. In openSCAD it\u2019s not so essential to do some of this good practice because the code is quite easy to understand but I definitely recommend [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":88,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/wp\/v2\/posts\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":0,"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vandelle.com\/blog\/3dprinting\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}