3D part design with Inkscape and OpenSCAD #68: Using SVG layers with the customizer.

An example of how to import layers from an Inkscape SVG file and make a 3d object with OpenSCAD

6 min read
By Bob
3D part design with Inkscape and OpenSCAD #68: Using SVG  layers with the customizer.

One of the excellent capabilities of OpenScad that I noticed I hadn't posted about was using layers with Inkscape svg files. This is a very fast and easy way to build multi layer models.

I used the code I had already made for DXF import and modified it for SVG import, here is the modified code:

/*[SVG] */ 
Path_to_SVG_File="Yourfile.svg";

/* [SVG_Layer 1] */ 
ShowLayer1 = true;
Layer1_name="layer1";
Linear_Extrude_Layer1 = 0.1; //[0.1:0.01:400]
Layer1_twist = 0; //[-360:0.01:360]
Layer1_slices = 1; //[0:1:400]
Layer1_transparency = 1;//[0:.01:1]
Layer1_red = 255;//[0:255]
Layer1_green = 255;//[0:255]
Layer1_blue = 0;//[0:255]
Rotate_Layer1_X = 0; //[0:.01:360]
Rotate_Layer1_Y = 0; //[0:.01:360]
Rotate_Layer1_Z = 0; //[0:.01:360]
Move_Layer1_X = 0; //[-400:0.01:400] 
Move_Layer1_Y = 0; //[-400:0.01:400]
Move_Layer1_Z = 0; //[-400:0.01:400]
Scale_Layer1_X = 1; //[1:0.01:100]
Scale_Layer1_Y = 1; //[1:0.01:100]
Scale_Layer1_Z = 1; //[1:0.01:100]

/* [SVG_Layer 2] */ 
ShowLayer2 = true;
Layer2_name="layer2";
Linear_Extrude_Layer2 = 0.1; //[0.1:0.01:400]
Layer2_twist = 0; //[-360:0.01:360]
Layer2_slices = 1; //[0:1:400]
Layer2_transparency = 1;//[0:.01:1]
Layer2_red = 255;//[0:255]
Layer2_green = 255;//[0:255]
Layer2_blue = 0;//[0:255]
Rotate_Layer2_X = 0; //[0:.01:360]
Rotate_Layer2_Y = 0; //[0:.01:360]
Rotate_Layer2_Z = 0; //[0:.01:360]
Move_Layer2_X = 0; //[-400:0.01:400] 
Move_Layer2_Y = 0; //[-400:0.01:400]
Move_Layer2_Z = 0; //[-400:0.01:400]
Scale_Layer2_X = 1; //[1:0.01:100]
Scale_Layer2_Y = 1; //[1:0.01:100]
Scale_Layer2_Z = 1; //[1:0.01:100]

/* [SVG_Layer 3] */ 
ShowLayer3 = true;
Layer3_name="layer3";
Linear_Extrude_Layer3 = 0.1; //[0.1:0.01:400]
Layer3_twist = 0; //[-360:0.01:360]
Layer3_slices = 1; //[0:1:400]
Layer3_transparency = 1;//[0:.01:1]
Layer3_red = 255;//[0:255]
Layer3_green = 255;//[0:255]
Layer3_blue = 0;//[0:255]
Rotate_Layer3_X = 0; //[0:.01:360]
Rotate_Layer3_Y = 0; //[0:.01:360]
Rotate_Layer3_Z = 0; //[0:.01:360]
Move_Layer3_X = 0; //[-400:0.01:400] 
Move_Layer3_Y = 0; //[-400:0.01:400]
Move_Layer3_Z = 0; //[-400:0.01:400]
Scale_Layer3_X = 1; //[1:0.01:100]
Scale_Layer3_Y = 1; //[1:0.01:100]
Scale_Layer3_Z = 1; //[1:0.01:100]

/* [SVG_Layer 4] */ 
ShowLayer4 = true;
Layer4_name="layer4";
Linear_Extrude_Layer4 = 0.1; //[0.1:0.01:400]
Layer4_twist = 0; //[-360:0.01:360]
Layer4_slices = 1; //[0:1:400]
Layer4_transparency = 1;//[0:.01:1]
Layer4_red = 255;//[0:255]
Layer4_green = 255;//[0:255]
Layer4_blue = 0;//[0:255]
Rotate_Layer4_X = 0; //[0:.01:360]
Rotate_Layer4_Y = 0; //[0:.01:360]
Rotate_Layer4_Z = 0; //[0:.01:360]
Move_Layer4_X = 0; //[-400:0.01:400] 
Move_Layer4_Y = 0; //[-400:0.01:400]
Move_Layer4_Z = 0; //[-400:0.01:400]
Scale_Layer4_X = 1; //[1:0.01:100]
Scale_Layer4_Y = 1; //[1:0.01:100]
Scale_Layer4_Z = 1; //[1:0.01:100]

/* [SVG_Layer 5] */ 
ShowLayer5 = true;
Layer5_name="layer5";
Linear_Extrude_Layer5 = 0.1; //[0.1:0.01:400]
Layer5_twist = 0; //[-360:0.01:360]
Layer5_slices = 1; //[0:1:400]
Layer5_transparency = 1;//[0:.01:1]
Layer5_red = 255;//[0:255]
Layer5_green = 255;//[0:255]
Layer5_blue = 0;//[0:255]
Rotate_Layer5_X = 0; //[0:.01:360]
Rotate_Layer5_Y = 0; //[0:.01:360]
Rotate_Layer5_Z = 0; //[0:.01:360]
Move_Layer5_X = 0; //[-400:0.01:400] 
Move_Layer5_Y = 0; //[-400:0.01:400]
Move_Layer5_Z = 0; //[-400:0.01:400]
Scale_Layer5_X = 1; //[1:0.01:100]
Scale_Layer5_Y = 1; //[1:0.01:100]
Scale_Layer5_Z = 1; //[1:0.01:100]

/* [Rotate Extrude SVG] */
ShowExtrudeLayer = false;
Rotate_Extrude_Layer_Name = "";
Rotate_Extrude_Degrees = 0.1; //[0.1:.01:360]
Rotate_Extrude_Facets = 3; //[3:1:400]
Extrude_Layer_transparency = 1;//[0:.01:1]
Extrude_Layer_red = 255;//[0:255]
Extrude_Layer_green = 255;//[0:255]
Extrude_Layer_blue = 0;//[0:255]
RotateX_Extrude_Layer = 0; //[0:0.01:360]
RotateY_Extrude_Layer = 0; //[0:0.01:360]
RotateZ_Extrude_Layer = 0; //[0:0.01:360]
MoveX_ExtrudeLayer = 0; //[-400:0.01:400] 
MoveY_ExtrudeLayer = 0; //[-400:0.01:400]
MoveZ_ExtrudeLayer = 0; //[-400:0.01:400]
ScaleX_Extrude_Layer = 1; //[1:0.01:100]
ScaleY_Extrude_Layer = 1; //[1:0.01:100]
ScaleZ_Extrude_Layer = 1; //[1:0.01:100] 


module Layer1(){

if (ShowLayer1)
color([Layer1_red/255,Layer1_green/255,Layer1_blue/255],Layer1_transparency)
rotate(a = [Rotate_Layer1_X,0,0])
rotate(a = [0,Rotate_Layer1_Y,0])
rotate(a = [0,0,Rotate_Layer1_Z])
translate([Move_Layer1_X,0,0])
translate([0,Move_Layer1_Y,0])
translate([0,0,Move_Layer1_Z])
scale([Scale_Layer1_X,1,1])
scale([1,Scale_Layer1_Y,1])
scale([1,1,Scale_Layer1_Z])
linear_extrude(height = Linear_Extrude_Layer1, twist = Layer1_twist, slices = Layer1_slices)
import(Path_to_SVG_File, layer = Layer1_name);
}

module Layer2(){

if (ShowLayer2)
color([Layer2_red/255,Layer2_green/255,Layer2_blue/255],Layer2_transparency)
rotate(a = [Rotate_Layer2_X,0,0])
rotate(a = [0,Rotate_Layer2_Y,0])
rotate(a = [0,0,Rotate_Layer2_Z])
translate([Move_Layer2_X,0,0])
translate([0,Move_Layer2_Y,0])
translate([0,0,Move_Layer2_Z])
scale([Scale_Layer2_X,1,1])
scale([1,Scale_Layer2_Y,1])
scale([1,1,Scale_Layer2_Z])
linear_extrude(height = Linear_Extrude_Layer2, twist = Layer2_twist, slices = Layer2_slices)
import(Path_to_SVG_File, layer = Layer2_name);
}
 
module Layer3(){

if (ShowLayer3)
color([Layer3_red/255,Layer3_green/255,Layer3_blue/255],Layer3_transparency)
rotate(a = [Rotate_Layer3_X,0,0])
rotate(a = [0,Rotate_Layer3_Y,0])
rotate(a = [0,0,Rotate_Layer3_Z])
translate([Move_Layer3_X,0,0])
translate([0,Move_Layer3_Y,0])
translate([0,0,Move_Layer3_Z])
scale([Scale_Layer3_X,1,1])
scale([1,Scale_Layer3_Y,1])
scale([1,1,Scale_Layer3_Z])
linear_extrude(height = Linear_Extrude_Layer3, twist = Layer3_twist, slices = Layer3_slices)
import(Path_to_SVG_File, layer = Layer3_name);
} 

module Layer4(){

if (ShowLayer4)
color([Layer4_red/255,Layer4_green/255,Layer4_blue/255],Layer4_transparency)
rotate(a = [Rotate_Layer4_X,0,0])
rotate(a = [0,Rotate_Layer4_Y,0])
rotate(a = [0,0,Rotate_Layer4_Z])
translate([Move_Layer4_X,0,0])
translate([0,Move_Layer4_Y,0])
translate([0,0,Move_Layer4_Z])
scale([Scale_Layer4_X,1,1])
scale([1,Scale_Layer4_Y,1])
scale([1,1,Scale_Layer4_Z])
linear_extrude(height = Linear_Extrude_Layer4, twist = Layer4_twist, slices = Layer4_slices)
import(Path_to_SVG_File, layer = Layer4_name);
} 

module Layer5(){

if (ShowLayer5)
color([Layer5_red/255,Layer5_green/255,Layer5_blue/255],Layer5_transparency)
rotate(a = [Rotate_Layer5_X,0,0])
rotate(a = [0,Rotate_Layer5_Y,0])
rotate(a = [0,0,Rotate_Layer5_Z])
translate([Move_Layer5_X,0,0])
translate([0,Move_Layer5_Y,0])
translate([0,0,Move_Layer5_Z])
scale([Scale_Layer5_X,1,1])
scale([1,Scale_Layer5_Y,1])
scale([1,1,Scale_Layer5_Z])
linear_extrude(height = Linear_Extrude_Layer5, twist = Layer5_twist, slices = Layer5_slices)
import(Path_to_SVG_File, layer = Layer5_name);
} 

module RotateExtrude(){
if (ShowExtrudeLayer)
color([Extrude_Layer_red/255,Extrude_Layer_green/255,Extrude_Layer_blue/255],Extrude_Layer_transparency)     
rotate_extrude(angle=Rotate_Extrude_Degrees, convexity=10,$fn=Rotate_Extrude_Facets)
rotate(a = [RotateX_Extrude_Layer,0,0])
rotate(a = [0,RotateY_Extrude_Layer,0])
rotate(a = [0,0,RotateZ_Extrude_Layer])
translate([MoveX_ExtrudeLayer,0,0])
translate([0,MoveY_ExtrudeLayer,0])
translate([0,0,MoveZ_ExtrudeLayer])
scale([ScaleX_Extrude_Layer,1,1])
scale([1,ScaleY_Extrude_Layer,1])
scale([1,1,ScaleZ_Extrude_Layer])
 import(Path_to_SVG_File, layer = Rotate_Extrude_Layer_Name);
}

Layer1();
Layer2();
Layer3();
Layer4();
Layer5();
RotateExtrude();

As you can see you can easily copy the customizer block and module, then change the number to the next in sequence to import as many layers as you need. I made a main file import and then individual layer name import for the layers.

OpenSCAD User Manual/SVG Import - Wikibooks, open books for an open world

If you add more nodes and convert them to lines you can make very smooth circles and curves, here is some text:

The more nodes you add the finer the details will be, then convert the nodes to lines:

And your import into OpenSCAD will be super smooth:

here's a circle before adding nodes:

And after:

Here's an example of why I added twist to the layers:

And Rotate extrude:

By converting the stroke to path and object to path you can move them to separate layers and make pretty much any type of odd shaped box or object outline you can imagine:

Using svg layers you can build some complex models very quickly.