Skip to content

Instantly share code, notes, and snippets.

@briped
Created August 23, 2025 00:08
Show Gist options
  • Select an option

  • Save briped/ca63ae3736cb451e2bc7ef1ae2adaf8a to your computer and use it in GitHub Desktop.

Select an option

Save briped/ca63ae3736cb451e2bc7ef1ae2adaf8a to your computer and use it in GitHub Desktop.
const jscad = require('@jscad/modeling');
const { polyhedron, cylinder } = jscad.primitives;
const { subtract } = jscad.booleans;
const getParameterDefinitions = () => [
{ name: 'baseGroup', type: 'group', caption: 'Base' },
{ name: 'width', type: 'number', caption: 'Width:', initial: 20.00, min: 20.00, max: 200.00, step: 5.00 },
{ name: 'depth', type: 'number', caption: 'Depth:', initial: 20.00, min: 20.00, max: 200.00, step: 5.00 },
{ name: 'height', type: 'number', caption: 'Height:', initial: 3.20, min: 1.00, max: 10.00, step: 0.10 },
{ name: 'taper', type: 'number', caption: 'Taper:', initial: -2.00, min: -10.00, max: 10.00, step: 0.10 },
{ name: 'walls', type: 'number', caption: 'Walls:', initial: 0.60, min: 0.60, max: 5.00, step: 0.10 },
{ name: 'percent', type: 'number', caption: 'Percent:', initial: 100.00, min: 10.00, max: 500.00, step: 1.00 },
{ name: 'magnetGroup', type: 'group', caption: 'Magnet' },
{ name: 'diameter', type: 'number', caption: 'Diameter:', initial: 3.00, min: 1.00, max: 10.00, step: 0.10 },
{ name: 'thickness', type: 'number', caption: 'Thickness:', initial: 1.00, min: 1.00, max: 10.00, step: 0.10 },
{ name: 'tolerance', type: 'number', caption: 'Tolerance:', initial: 0.10, min: 0.00, max: 1.00, step: 0.05 },
]
const main = (params) => {
const scale = params.percent/100;
// Calculate the minimum values
const minimumWidth = params.diameter+params.tolerance+params.walls*2;
const minimumDepth = minimumWidth;
const minimumHeight = params.thickness+params.tolerance+params.walls;
// Calculate the tapered/top values
const taperedWidth = params.width+params.taper;
const taperedDepth = params.depth+params.taper;
// Calculate the scaled values
const scaledWidth = params.width*scale;
const scaledDepth = params.depth*scale;
const scaledHeight = params.height*scale;
const scaledTaperWidth = taperedWidth*scale;
const scaledTaperDepth = taperedDepth*scale;
//if (scaledTaperWidth >= minimumWidth && scaledTaperDepth >= minimumDepth && scaledWidth >= minimumWidth && scaledDepth >= minimumDepth && scaledHeight >= minimumHeight) {}
const magnet = cylinder({ radius: params.diameter/2+params.tolerance, height: params.thickness+params.tolerance, center: [0, 0, (params.thickness+params.tolerance)/2] });
const points = [
// Bottom
[-scaledWidth/2, -scaledDepth/2, 0], // [0] Front left
[ scaledWidth/2, -scaledDepth/2, 0], // [1] Front right
[ scaledWidth/2, scaledDepth/2, 0], // [2] Rear right
[-scaledWidth/2, scaledDepth/2, 0], // [3] Rear left
// Top
[-scaledTaperWidth/2, -scaledTaperDepth/2, scaledHeight], // [4] Front left
[ scaledTaperWidth/2, -scaledTaperDepth/2, scaledHeight], // [5] Front right
[ scaledTaperWidth/2, scaledTaperDepth/2, scaledHeight], // [6] Rear right
[-scaledTaperWidth/2, scaledTaperDepth/2, scaledHeight] // [7] Rear left
];
const faces = [
[3,2,1,0], // Bottom
[4,5,6,7], // Top
[0,1,5,4], // Front
[1,2,6,5], // Right
[2,3,7,6], // Rear
[3,0,4,7] // Left
];
const base = polyhedron({ points: points, faces: faces });
return subtract(base, magnet);
}
module.exports = { main, getParameterDefinitions }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment