Skip to content

Instantly share code, notes, and snippets.

@hrgdavor
Last active March 17, 2025 22:39
Show Gist options
  • Select an option

  • Save hrgdavor/a8069ce654f4484866c8b61af70b46bd to your computer and use it in GitHub Desktop.

Select an option

Save hrgdavor/a8069ce654f4484866c8b61af70b46bd to your computer and use it in GitHub Desktop.
jscad multifile project combining parameters
const { sphere, cube } = require('@jscad/modeling').primitives;
const { subtract } = require('@jscad/modeling').booleans;
const { translate } = require('@jscad/modeling').transforms;
const { addParamsTo, getPrefix } = require('./param.util.js');
const childShape = require('/model1.js');
const childShape2 = require('/model2.js');
function main(params) {
const mainCube = cube({ size: params.size });
const mainSphere = sphere({ radius: params.radius });
const mainShape = subtract(mainCube, mainSphere);
const translatedMainShape = translate([0, 30, 0], mainShape);
//An error occurs here. model1.js need params
const childShapeResult = subtract(
childShape.main(getPrefix(params,'model1')),
childShape2.main(getPrefix(params,'model2')),
);
return [childShapeResult, translatedMainShape];
}
function getParameterDefinitions() {
const out = [
{
name: 'size',
caption: 'Cube Size',
type: 'float',
initial: 10,
min: 1,
max: 100,
step: 0.1
},
{
name: 'radius',
caption: 'Sphere Radius',
type: 'number',
initial: 5.5,
min: 1,
max: 50,
step: 0.1
}
];
addParamsTo(childShape.getParameterDefinitions(),'model1', 'Model 1 options', out, 'closed')
addParamsTo(childShape2.getParameterDefinitions(),'model2', 'Model 2 options', out)
return out
}
module.exports = { main, getParameterDefinitions };
const { cube } = require('@jscad/modeling').primitives;
function main(params) {
return cube({ size: params.size });
}
function getParameterDefinitions() {
return [
{
name: 'size',
caption: 'Cube Size',
type: 'number',
initial: 5,
min: 1,
max: 100,
step: 0.1
}
];
}
module.exports = { main, getParameterDefinitions };
const { sphere } = require('@jscad/modeling').primitives;
function main(params) {
return sphere({ radius: params.radius });
}
function getParameterDefinitions() {
return [
{
name: 'radius',
caption: 'Sphere Radius',
type: 'number',
initial: 3,
min: 1,
max: 50,
step: 0.1
}
];
}
module.exports = { main, getParameterDefinitions };
function addParamsTo(params, prefix, caption, to, initial= 'open'){
if(!params?.length) return
to.push({ name: prefix, type: 'group', caption, initial })
params.forEach(p=>{
to.push({...p, name: prefix+p.name})
})
}
function getPrefix(params, prefix){
const out = {}
for(let p in params){
if(p.startsWith(prefix)) out[p.substring(prefix.length)] = params[p]
}
console.warn('params for prefix',prefix, out)
return out
}
module.exports = {addParamsTo, getPrefix}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment