Written Feb. 16th, 2017 by Johannis Likos (Mech. Eng. & IT Consultant) [likosjo@yahoo.com]
CoffeeSCAD is one of several free web-based environments for programmatically designed 3D objects. The general purpose of using scripts inside a browser is to visualize the virtually created objects before exporting them as STL files for 3D printing.
The Tested 3D Object As test object we take a nut, a fastener element for mechanical engineering, which consists of concentric cylindrical and conic shapes to be merged, subtracted and intersected in order to get the desired result. All below mentioned shapes should be concentric with common Z-axis. Practically should be executed several grouped steps as follows: 1. create the initial main body by merging a. one conic shape on top b. one cylindrical shape in the middle c. one conic shape upside-down at the bottom 2. merge multiple conic shapes for the inner face of the final object a. every cone in odd order (1st, 3rd, 5th) should be upside-down b. add special cones for the entry/exit area of the bolt 3. extrude a 2D hexagonal shape for the outer face of the final object a. in this example are use pre-calculated coordinates b. alternatively could be used a circle with extra parameter $fn:6 4. do the final inner shape a. subtract the threads from the cylindrical main body 5. do the final outer shape a. intersect the extruded hexagonal shape with the cylindrical main body 6. remove all unused shapes
The CoffeeSCAD User Interface The user interface on the browser window consists of a drop-down menu for doing the interactions, an assembly window for listing the objects, an editor window for writing the CoffeeScript code and the 3D space in the background for displaying the resulted object.
The icons on the menu are self-explicable. It is possible to configure some settings in order to make the look-and-feel more customized and user-friendly.
Through the drop-down menu can be accessed ready-made examples to try them out.
In case the interpreter detects an error the particular code line will be marked and by moving the mouse cursor on the red symbol the error explanation will be indicated to the programmer.
Performance Manipulation and Surface Quality
Generally if rotatory shapes, such as circles, discs, cylinder and cones, have higher $fn values, then the surface of the final object will be much smoother, but the calculation time will take much longer, where the browser may alert timeout. On the other hand, if the same rotatory shapes have lower $fn values, then the surface of the final object will be much rougher (coarse), but the calculation time will be faster, to be preferred when prototyping with less accuracy.
The CoffeeScript language The interpreter language CoffeeScript has derived from JavaScript and uses simplified coding rules for variables, function calls, etc. The guidelines for this programming language can be found at the official site: http://coffeescript.org/
The Tested CoffeeScript
It was difficult to use while or for loop, therefore same functions have been repetitively called with slightly different parameters in order to by-pass this bottle-neck. Source code of the nut.coffee file: cylinder1 = cylinder({r:55,h:30,center:[0,0,25]}) cylinder2 = cylinder({r2:55,r1:45,h:10,$fn:128,center:[0,0,5]}) cylinder3 = cylinder({r1:55,r2:45,h:10,$fn:128,center:[0,0,45]}) cylinder1.union(cylinder2) cylinder1.union(cylinder3) cylinder2.subtract(cylinder2) cylinder3.subtract(cylinder3) cylinder4 = cylinder({r:20,h:30,center:[0,0,25]}) cylinder5 = cylinder({r2:20,r1:25,h:10,$fn:128,center:[0,0,5]}) cylinder6 = cylinder({r1:20,r2:25,h:10,$fn:128,center:[0,0,45]}) cylinder11 = cylinder({r1:20,r2:25,h:5,$fn:128,center:[0,0,2.5]}) cylinder21 = cylinder({r2:20,r1:25,h:5,$fn:128,center:[0,0,7.5]}) cylinder12 = cylinder({r1:20,r2:25,h:5,$fn:128,center:[0,0,12.5]}) cylinder22 = cylinder({r2:20,r1:25,h:5,$fn:128,center:[0,0,17.5]}) cylinder13 = cylinder({r1:20,r2:25,h:5,$fn:128,center:[0,0,22.5]}) cylinder23 = cylinder({r2:20,r1:25,h:5,$fn:128,center:[0,0,27.5]}) cylinder14 = cylinder({r1:20,r2:25,h:5,$fn:128,center:[0,0,32.5]}) cylinder24 = cylinder({r2:20,r1:25,h:5,$fn:128,center:[0,0,37.5]}) cylinder15 = cylinder({r1:20,r2:25,h:5,$fn:128,center:[0,0,42.5]}) cylinder25 = cylinder({r2:20,r1:25,h:5,$fn:128,center:[0,0,47.5]}) cylinder4.union(cylinder5) cylinder4.union(cylinder6) cylinder4.union(cylinder11) cylinder4.union(cylinder21) cylinder4.union(cylinder12) cylinder4.union(cylinder22) cylinder4.union(cylinder13) cylinder4.union(cylinder23) cylinder4.union(cylinder14) cylinder4.union(cylinder24) cylinder4.union(cylinder15) cylinder4.union(cylinder25) cylinder5.subtract(cylinder5) cylinder6.subtract(cylinder6) cylinder11.subtract(cylinder11) cylinder21.subtract(cylinder21) cylinder12.subtract(cylinder12) cylinder22.subtract(cylinder22) cylinder13.subtract(cylinder13) cylinder23.subtract(cylinder23) cylinder14.subtract(cylinder14) cylinder24.subtract(cylinder24) cylinder15.subtract(cylinder15) cylinder25.subtract(cylinder25) cylinder1.subtract(cylinder4) cylinder4.subtract(cylinder4) hex_shape2d = CAGBase.fromPoints([ [50, 25],[0, 56],[-50, 25],[-50, -25],[0, -56],[50, -25]]) hex_shape3d = hex_shape2d.extrude({offset:[0,0,50]}) hex_shape3d.color([0.4,0.4,0.4]) cylinder1.intersect(hex_shape3d) cylinder1.color([0.6,0.6,0.6]) hex_shape3d.subtract(hex_shape3d) assembly.add(cylinder1)
The CoffeeSCAD library CoffeeSCAD uses a function library as a superset for handling primitive geometries and Boolean operations. Complex algorithms with encapsulated conditions and loops is difficult to implement and to execute due to slow processing speed of calculations. More details on how to program in the CoffeeSCAD environment, please, visit the Wiki site: https://github.com/kaosat-dev/CoffeeSCad/wiki
The CoffeeSCAD Sources For the further development of CoffeeSCAD its sources can be found at GitHub: https://github.com/kaosat-dev/CoffeeSCad
Reusability and Conclusion This complex 3D object resulted out of the above primitive scripting code, in this example a simple nut representing a fasteners element, which can be saved and loaded. The temporary file storage depends on the preferred browser’s compatibility and behavior. The above described test has been executed on a Windows office notebook by executing the above *.coffee script by opening the web address http://coffeescad.net/online/ inside the Google Chrome browser.
Firefox, Opera or Internet Explorer would be other browser choices on a desktop PC. Alternatively could be used the Safari browser on iMac computers or similar browsers on a Linux workstation. As mobile solution the same test could be executed on an Android tablet or smartphone, but, as mentioned before, be aware of the calculation speed while refreshing the object on the display. -END-