Parametric SVG Shapes
Following examples show how to create parametric SVG shapes.
In each example below, you can assign values to variables via parameters dialog
A Pipe
function build() {
return makePipe();
}
function makePipe() {
var height = 30; // or height = params.height
var radius = 5; // or radius = params.radius
var thickness = 1;
var angle = 360;
//make rectangle face to be revolved
var outerRadius = radius+thickness;
var hOffset = height/2;
//define SVG path
var path = "M "+radius+" "+hOffset+" H "+outerRadius+" V "+-hOffset+" H "+radius+" Z";
//Create Face
var face = cadlib.svgPath2Face(path, {x:1, y:1});
//revolve rectangle to make ring
var center = {x:0, y:0, z:0};
var axis = {x:0, y:0, z:1};
//Create solid
var solid = cadlib.revolveFace(face, center, axis, angle);
return solid;
}
A Cup
function build() {
return makeCup();
}
function makeCup() {
var diameter = 20; // or diameter = params.diameter
var wallThickness = 2; // or wallThickness = params.wallThickness
var height = 10;
var baseThickness = 2;
//offset for path so that cylinder is centered on origin
var hOffset = height/2.0;
var outerRadius = diameter/2;
var innerRadius = outerRadius-wallThickness;
//create variables to revolve wall of the cup
var pathWall = "M "+innerRadius+" "+-hOffset+" H " +outerRadius+ " V "+hOffset+" H "+innerRadius+" Z";
var faceWall = cadlib.svgPath2Face(pathWall, {x:1, y:1});
var center = {x:0, y:0, z:0};
var axis = {x:0, y:0, z:1};
var solidWall = cadlib.revolveFace(faceWall, center, axis, 360);
//create variables to revolve base of the cup
var topBase = -hOffset+baseThickness;
var pathBase = "M "+outerRadius+" "+-hOffset+" H 0 V "+topBase+" H "+outerRadius+" Z";
var faceBase = cadlib.svgPath2Face(pathBase, {x:1, y:1});
center = {x:0, y:0, z:(baseThickness-hOffset)/2};
axis = {x:0, y:0, z:1};
var solidBase = cadlib.revolveFace(faceBase, center, axis, 360);
//compound solids for final result
var solid = cadlib.compound([solidWall, solidBase])
return solid;
}
Capped Cone
function build() {
return makeCappedCone();
}
function makeCappedCone() {
var topRadius = 6;
var botRadius = 10;
var height = 20;
//create variables to revolve trapezoid into capped cone
var yPoint = height;
//create SVG path
var path = "M 0 0"+ " H " +botRadius+ " L "+topRadius+" "+yPoint+" H 0 Z";
//Create face
var face = cadlib.svgPath2Face(path, {x:1, y:1});
//define parameters of revolution of above face
var center = {x:0, y:0, z:0};
//axis of rotation
var axis = {x:0, y:0, z:1};
//create solid
var solid = cadlib.revolveFace(face, center, axis, 360);
return solid;
//Note: Edit the SVG path appropriately to rotate around other axis.
}
A box
function build() {
return makeBox();
}
function makeBox() {
let width = 5;
let length = 5;
let height = 50;
//create offset so that the box is centered at the origin
let wOffset = width/2.0
let lOffset = length/2.0
let path = "M" + (-wOffset) + " " + (-lOffset) + "H" + wOffset + "V" + lOffset + "H" + (-wOffset) + "Z";
return extrudeFace(path, height);
}
function extrudeFace(path, height){
let scale = {x:1, y:1};
let face = cadlib.svgPath2Face(path, scale);
let dir = {x:0, y: height, z:0};
let solid = cadlib.extrude(face,dir);
return solid;
}