3D Central
Importing SVG
Sometimes designing a complicated polygon is not worth the effort in OpenSCAD. It can be far more efficient to make the shape using another program. OpenSCAD (since version 2019.05) can import any shape you can create in a program like Inkscape.
Let's look at a reasonably simple example and take care of the biggest SVG "gotcha" at the same time.
I open up Inkscape (currently using version 1.1) and create a small square. I chose 96px by 96px to make a 1 inch square. When I save the SVG, I can start OpenSCAD (currently version 2021.01) and use the code import("square.svg", convexity=2); Here comes the gotcha. Clicking <F5> to get a preview may or may not reveal the imported square. The problem is that it's a small square and it imports to OpenSCAD based on exactly where we put the square in Inkscape. The default page size of Inkscape is European standard paper page size: A4. If we put the small square in the middle of the page, it will mean the square is not at the center of the OpenSCAD default view. We are effectively zoomed in too close. If we back out with the zoom out control, and there it is, about a hundred units right and 150 units up the x and y scales of OpenSCAD units.
That's not useful to us. A shape is easier to use in OpenSCAD if we can conveniently reposition it where we want it. Starting so many units away from the OpenSCAD origin makes that very difficult.
Let's look closer at the problem.
- First: Location matters
- Inkscape uses the top-left corner as its origin (0,0).
But OpenSCAD expects the bottom-left corner, so we must deal with that.
In this simple pair of images, I've made a 96px square in Inkscape which has its origin at the typical top-left corner.
And, I've made the page just wide enough for the little square (while double its height).
Notice how the OpenSCAD puts it 25.4 units above the origin.
- Second: The shape's corner may not be the best to be at OpenSCAD's corner
- For a simple square, we might expect that putting the shape so it fits just at the bottom-left corner is best. After all the OpenSCAD native square fits that way.
- Third: Easy repositioning
- It is much easier to translate and rotate an object that "starts" centered, the object's center fixed originally at OpenSCAD's origin.
Let's wrap up with a realistic example. You might create a complicated svg image like this one, based on the Free Software Foundation GNU logo.
- The first image is a simplified conversion from the original GNU logo.
- The second is how we want it centered on the page bottom-left corner.
- The third is the easiest way to get the logo centered. You click on the image with the arrow selection tool. Then click again to reveal the corner controls. Then gently tap your arrow keys until you get the side arrows to balance on each of the two sides as shown.
Import to OpenSCAD and then put the logo where you need it for your OpenSCAD 3D project. Increase the convexity value to accomodate the number of potential "edge" crossings. import("gnu3.svg", convexity = 10) worked for me.
Available Files:
gnu-import.scad
gnu3.svg(In most browsers, right click each file you wish to download and select "save-as" from the menu options.)SCAD files for study/modification.
GPL3 License
Please note, the browser preview of the GNU logo only shows the top-right corner because of its position with 75% of the logo off the page.