3D part design with Inkscape and OpenSCAD #73: automating difference() with the customizer.

6 min read
By Bob
3D part design with Inkscape and OpenSCAD #73: automating difference() with the customizer.

Being able to click a box and  add or subtract an object from a project is pretty handy, and combined with the svg layer import template it increases the speed of the workflow quite a bit. Here is an addition I made to the svg layer import code so I can just click a box and automatically perform difference() to the individual layers.

Here is the module I added for difference():

Here is the modified code:


/*[SVG] */ 
Path_to_SVG_File="/home/none3/Desktop/drawing.svg";

/* [SVG_Layer 1] */ 
Layer1_name="layer1";
ShowLayer1 = true;
Difference1=true;
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; //[-360:.01:360]
Rotate_Layer1_Y = 0; //[-360:.01:360]
Rotate_Layer1_Z = 0; //[-360:.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; //[-100:0.01:100]
Scale_Layer1_Y = 1; //[-100:0.01:100]
Scale_Layer1_Z = 1; //[-100:0.01:100]

/*[SVG_Layer 2]*/
ShowLayer2=true;
Difference2=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; //[-360:.01:360]
Rotate_Layer2_Y = 0; //[-360:.01:360]
Rotate_Layer2_Z = 0; //[-360:.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; //[-100:0.01:100]
Scale_Layer2_Y = 1; //[-100:0.01:100]
Scale_Layer2_Z = 1; //[-100:0.01:100]

/* [SVG_Layer 3] */ 
ShowLayer3 = true;
Difference3=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; //[-360:.01:360]
Rotate_Layer3_Y = 0; //[-360:.01:360]
Rotate_Layer3_Z = 0; //[-360:.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; //[-100:0.01:100]
Scale_Layer3_Y = 1; //[-100:0.01:100]
Scale_Layer3_Z = 1; //[-100:0.01:100]

/* [SVG_Layer 4] */ 
ShowLayer4 = true;
Difference4=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; //[360:.01:360]
Rotate_Layer4_Y = 0; //[360:.01:360]
Rotate_Layer4_Z = 0; //[360:.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; //[-100:0.01:100]
Scale_Layer4_Y = 1; //[-100:0.01:100]
Scale_Layer4_Z = 1; //[-100:0.01:100]

/* [SVG_Layer 5] */ 
ShowLayer5 = true;
Difference5=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; //[-360:.01:360]
Rotate_Layer5_Y = 0; //[-360:.01:360]
Rotate_Layer5_Z = 0; //[-360:.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; //[-100:0.01:100]
Scale_Layer5_Y = 1; //[-100:0.01:100]
Scale_Layer5_Z = 1; //[-100: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; //[-360:0.01:360]
RotateY_Extrude_Layer = 0; //[-360:0.01:360]
RotateZ_Extrude_Layer = 0; //[-360: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; //[-100:0.01:100]
ScaleY_Extrude_Layer = 1; //[-100:0.01:100]
ScaleZ_Extrude_Layer = 1; //[-100:0.01:100] 

module Layer1(){
if(ShowLayer1)
color([Layer1_red/255,Layer1_green/255,Layer1_blue/255],Layer1_transparency)
rotate(a=[Rotate_Layer1_X,Rotate_Layer1_Y,Rotate_Layer1_Z])
translate([Move_Layer1_X,Move_Layer1_Y,Move_Layer1_Z])
scale([Scale_Layer1_X,Scale_Layer1_Y,Scale_Layer1_Z])
linear_extrude(height=Linear_Extrude_Layer1,twist=Layer1_twist,slices=Layer1_slices,convexity=3)
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,Rotate_Layer2_Y,Rotate_Layer2_Z])
translate([Move_Layer2_X,Move_Layer2_Y,Move_Layer2_Z])
scale([Scale_Layer2_X,Scale_Layer2_Y,Scale_Layer2_Z])
linear_extrude(height = Linear_Extrude_Layer2,twist=Layer2_twist,slices=Layer2_slices,convexity=3)
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,Rotate_Layer3_Y,Rotate_Layer3_Z])
translate([Move_Layer3_X,Move_Layer3_Y,Move_Layer3_Z])
scale([Scale_Layer3_X,Scale_Layer3_Y,Scale_Layer3_Z])
linear_extrude(height=Linear_Extrude_Layer3,twist=Layer3_twist,slices=Layer3_slices,convexity=3)
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,Rotate_Layer4_Y,Rotate_Layer4_Z])
translate([Move_Layer4_X,Move_Layer4_Y,Move_Layer4_Z])
scale([Scale_Layer4_X,Scale_Layer4_Y,Scale_Layer4_Z])
linear_extrude(height = Linear_Extrude_Layer4,twist=Layer4_twist,slices=Layer4_slices,convexity=3)
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,Rotate_Layer5_Y,Rotate_Layer5_Z])
translate([Move_Layer5_X,Move_Layer5_Y,Move_Layer5_Z])
scale([Scale_Layer5_X,Scale_Layer5_Y,Scale_Layer5_Z])
linear_extrude(height = Linear_Extrude_Layer5,twist=Layer5_twist,slices=Layer5_slices,convexity=3)
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,RotateY_Extrude_Layer,RotateZ_Extrude_Layer])
translate([MoveX_ExtrudeLayer,MoveY_ExtrudeLayer,MoveZ_ExtrudeLayer])
scale([ScaleX_Extrude_Layer,ScaleY_Extrude_Layer,ScaleZ_Extrude_Layer])
 import(Path_to_SVG_File,layer=Rotate_Extrude_Layer_Name);
}

module Difference(){
if(Difference1){
difference(){
Layer1();
Layer2();
Layer3();
Layer4();
Layer5();
}}
if(Difference2){
difference(){
Layer2();
Layer3();
Layer4();
Layer5();
Layer1();
}}
if(Difference3){
difference(){
Layer3();
Layer1();
Layer2();
Layer4();
Layer5();
}}
if(Difference4){
difference(){
Layer4();
Layer1();
Layer2();
Layer3();
Layer5();
}}
if(Difference5){
difference(){
Layer5();
Layer1();
Layer2();
Layer3();
Layer4();
}}
}



Difference();
RotateExtrude();





Now I can design the  objects on different layers and add or subtract them in OpenSCAD much easier, here is an example design with objects on 5 different layers:

Here I have imported the svg file into OpenSCAD with the difference option selected (off)

and with it turned on:

And different layers with it set and unset:

And with the object turned off:

As you can see being able to add or subtract the different objects on the fly saves a bunch of time, alternately  you could cut holes in inkscape before you import the file but then you can't move them around, scale and rotate them.

here's a design that would have taken a while to model and only took a few minutes using this method:

There is one problem that I still need to fix though, if you have objects that you don't want to subtract from the other layers  there will be an area where they overlap and they will subtract from each other leaving a hole:

when you try to slice the object it will cause a non manifold error because there is a hole,it's pretty simple to fix the error by just commenting out the layer you don't want to subtract from but that kind of detracts from the ease of use of just selecting a check box. I'm sure there is a simple solution I will just have to figure it out.

Linux tip: To get the path of the file you want to import just highlight the file and click cntrl-c this will copy the path and then you can paste it into the import box: