We’ll see how to create an STL model of a stylized filament reel with a logo from SVG and some text without clicking the mouse. Yes we’ll write it procedurally in OpenSCAD, use templating and automate the whole thing. We’ll see how OpenSCAD is a really natural tool for programmer and it very easy to start playing with and get impressive results. <div style=”clear:both”></div>
Our today’s goal is to prepare around 20 3D-printed things representing filament reels, each with a inset logo and a short name of the material. This little weekend project was done for Svět 3D Tisku (3D Print World), a local 3D print company ran by my parents, for 3D Expo Prague 2016. Imagine a big poster to present a collection of filament materials and for each there’s a nice small tangible artifact made of that material.
OpenSCAD - Constructive Solid Geometry
Since I like programming more than GUI modeling tools and mouse clicking, I decided to try OpenSCAD. It allows to make 3D models using the Constructive Solid Geometry (CSG) and procedural modeling paradigms. Ie. we the model from primitive parts (cube, 2D polygon, linear extrusion), by applying transformations (traslate, scale, rotate) and set operations (union, intersection, difference). The model is described by a custom programming language. This is a different to the triangle mesh representation where only the border, not the solid itself is described.
For programmers the whole concept is much more intuitive and natural, eg. when compared to Blender with its overly complex UI with hard learning curve.
After I finished this project it felt vary natual and familiar. Then I realized I’ve already done something similar a few years ago in open-source RenderMan implementation Pixie for the university projected on the Shading Languages course.
Please refer to the OpenSCAD User Manual and the example models for the basics of OpenSCAD. Basically you write your code in one pane and see the model in the other. Useful keyboard shortcuts and commands.
F5- refresh the preview
F6- render the model (do this before saving to STL)
Export to STL
Comments are C-like, ie.
// ... and
/* ... */
Creating a model - step by step
We would like to get a shape that roughly looks like a filament reel but is not too complex. On top of it we would like to put a logo and some abbreviation of the material name. We can model it in the dimensions of a real reel and than scale it to the final model size.
Real world example (colorFabb COPPERFILL Ø 2,85 m / 4m):
There are example snippets of code throughout the article. The complete source code is available in a git repo linked at the end of the article. You can clone it and play with it yourself. It’s fully working.
Basic reel shape
The basic shape is a cylinder, ie. an extruded circle. The
$fn argument makes the circle smoother - the circle is represented as a regular polygon and
$fn specifies the number of its edges. Eg. cylinder of 190mm diameter, 20 mm high to represent the inner reel:
1 2 3
In the final model it is shown in red.
Functions, set operations
Let’s add two outer reels and a hole. As I said the model is really stylized and so the proportions do not match excatly a real-world reel. We can transform the primitives and operate on them with set operations. We can also extract constants to variables and refactor parts into functions (modules) which also can be parameterized.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Tip - visual debugging
A very useful way to ensure you are making the correct operations is visual debugging. Just add a
# character before any part and it is displayed in transparent red. This way you can be sure what you are intersecting and diffing.
Beveled side + ribs
Let’s add some rim by beveling the side and add some ribs just to make it look a bit more like a real reel.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Originally I though it would be nice to make some thin flat outer circular parts of the reel (that holds the filament from sides). When we printed it I realized it needs supports, otherwise the printer just extrudes into air. To avoid using supports and save some priting time we modified the models to make some small supports directly in the model. Just add some conic sections that smoothly interpolate betweeen the inner and outer diameter.
Fortunately OpenSCAD provides this functionality right in the
linear_extrude primitive, just and the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
It worked like charm and the models was printed well.
Logo - from SVG
Now when we have a nice reel let’s enhance it by adding our logo. Our logo is some vector graphics, so a DXF extrusion is an option.
I’ve extracted just a part of the logo from an AI (Illustrator) file and saved it as SVG. OpenSCAD can only open 2D vector files from DXF (Autocad), not SVG, so we have to first convert the SVG to DXF.
I’ve tried a few options and only this Inkscape plugin for export the DXF worked well: Inkscape-OpenSCAD-DXF-Export. Just download the repo and copy the dir and
~/.config/inkscape/extensions (on Mac) and than start Inkscape. Then just open the SVG and
File -> Save copy -> OpenSCAD DXF Output.
Then in OpenSCAD we import it into a 2D object:
In order to use it in CSG operations we have to extrude it:
1 2 3 4 5 6
In case nothing appears, make sure you select the default layer or the proper one. Look for
AcDbLayerTableRecord in the DXF file. I once copied an example from the docs and it selected some other layer and I though the DXF file is not compatible. Also the model from DXF might have to be translated to the original of coordinates.
Let’s inscribe the logo in the upper reel part:
1 2 3 4 5 6 7 8 9 10
The logo itself is a bit thicker and if we make it as thick as is lowered we may get into numerical problems. The solution is easy, just make it overlap a bit (by a small epsilon). If you’ve ever came across computational geometry or numerical programming this should have been deeply rooted in your blood :). Otherwide, you’ve just learned. Tip for reading: Numerical Robustness (for Geometric Calculations).
Then we’d like to add some short description text inscriped in the the upper side similarly as the logo. OpenSCAD directly supports 2D text primitives which can be then extruded. That’s exactly what we need.
1 2 3
We can set the text size, alignment and font to customize the text for aesthetics and legibility:
We have texts of various length like “ABS”, “BRASS”, “PETG ESD” so we need to customize the font size and and Y position individually for a good git.
In order to use the space well without compromizing legibility we can split the text into multiple lines (in this case usually two). Since OpenSCAD’s
text() only supports a single line, we need to make more translated
text() objects and union them. We’re not the first to come across this problem and fortunately other people managed to get a working solution. So we’ll use a snippet by user iggy_koopa.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
The same epsilon trick as with the logo still holds.
Scaling the whole model
The basic units in OpenSCAD are millimeters. Since we used dimensions of a real reel (with 20 cm diameter) but want our models smaller (eg. 8 cm), we can just scale the shole model.
1 2 3 4 5 6 7 8 9 10 11 12 13
Exporting to STL
Now we have single reel model and it’s time to export an STL. In the UI it is quite simple:
F6- render the model
- File -> Export -> Export to STL
In order to automate the things it would be nice to call OpenSCAD via command line. Fortunately, it supports it!
On Mac the binary is buried in
/Applications, so to use it a little bit more comfortable we can link it to our
~/bin. This is optional.
1 2 3 4
Then we can produce STL from an SCAD file this way:
It is quite slow. This model took about 1:15 min.
Exporting to PNG
Similarly we can export a thumbnail to PNG:
Also we can set the camera position and orientation:
Refer to the docs for details.
It is nice that we were able to create one model but we want a models for each of ~ 20 materials. Well, we could copy a model, change something and save as another file. But such approach get too cumbersome when we want to change something and propagate the change into all the instnaces. Better, we can try to automate the whole process.
So we can make a parameterized model with variable text, font size and text position and then use it to make multiple instances. We want a separate STL for each model, so we have to create mulitple separate SCAD files.
An easy approach is to use eg. Python as the templating engine, store the parameters in a table (a CSV file) and generate multiple SCAD files. Then for each SCAD file we’ll generate the STL and PNG thumbnail.
We store the parameterized model in the
reel.scad file and provide a function:
1 2 3
Then we can include this file and call the
reel() function to instantiate the model with given parameters and store it to other file:
Automating the whole thing
Python template generator
We store the definitions into a CSV file
reel_texts.csv. The text column is split on whitespace into multiple lines. The size is text size in mm, the y_pos is the relative Y position.
1 2 3 4 5
To read the CSV we use
pandas. Then we transform each row into a SCAD source code and store it into a file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Converting to STL in batch
Let’s convert all the generated SCAD files to STL files and store them to another directory. For ~ 20 files it took about half an hour. Right now I haven’t tried to optimize it.
1 2 3 4 5 6 7 8
Converting to PNG in batch
We can do similar thing to obtain PNG thumbnails:
1 2 3 4 5 6
Bonus - array of thumbnails
It might be nice to see all the thumbnails in a single image. Python, numpy and matplotlib can save us and to the job in a few lines of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
The code - git repository
The complete code, including the scripts and graphics is available in a git repository. The license code the code (except for the logo) it MIT open-source, so feel free to use it.
About the company
Svět 3D tisku (3D Print World) is a Czech boutique e-shop and store providing broad range of fillaments, 3D printers and custom 3D print services for Central European customers (local companies, schools, hobbyists). The company is ran by by mum and dad and I’m trying to help them if time permits.
In this small weekend project we’ve learned the basics of OpenSCAD and were able to create some nice small but not completely trivial model. We’ve seem how to use a hierarchy of parameterized models, how to use text, import vector graphics logo from SVG and use it as an inscription. In order to avoid repetitive work we tried to automate all the steps from generating SCAD code, STL and PNG file up to making a summary table of thumbnails.
I hope this article has shown you that it is very easy to start with OpenSCAD (at least for programmers) and inspired you to make your own projects. If you have any questions, you can search the docs or drop some comment here. Thanks and enjoy!