JSCAD User Group

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    Multipart project template others could find useful

    Development Discussions
    1
    1
    49
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • hrgdavor
      hrgdavor last edited by

      copy pasta from https://github.com/jscad/OpenJSCAD.org/discussions/1141

      Sharing this idea and also calling to discuss improvements to it.
      Even ideas how jscad could be improved to better facilitate use case like this are welcome.

      Very often when creating thins for 3d print I have multiple parts, so in time I have created a sort of template I like to use. Even if I have single piece I tend to split design in few parts to more easily handle it.

      This is the latest iteration that solves few issues for me

      • parameter definition to select a part is generated automatically
      • no need to declare configurable parameters in each function ( func that creates a part)
      • can reuse one part in another part
      const jscad = require('@jscad/modeling')
      const {sphere, cube} = jscad.primitives 
      const {translate} = jscad.transforms
      
      // all of the functions that generate parts will see the parameters without declaring them explicitly
      const main = ({//@jscad-params
        size=10, // {type:'slider'}
        part,
      }, getParams)=>{
      
        // UTILITY placeholder for part generator functions
        const parts = {}
      
        // CTRL+R in vscode works just fine
        parts.Sample_Cube = ()=>cube({size})
      
        parts.Sample_Sphere = ()=>{
          return sphere({radius:size/2})
        }
      
        // parts can easily be combined
        parts.Assembly = ()=>([
          // jump to definition in vscode (ALT+click) works
          parts.Sample_Cube(),
          translate([size+5,0,0], parts.Sample_Sphere()),
        ])
      
        /*********************** UTILITY below is just utility code. do not change **************** */
      
        // we were called by getParameterDefinitions so we need to provide list of parts
        if(getParams === true){
          const values = Object.keys(parts)
          return {values, initial: values[0]}
        }
      
        // make sure we always call one of the functions
        if(!parts[part]) part = Object.keys(parts)[0]
        return parts[part]()
      }
      
      const getParameterDefinitions = ()=>[{ name: 'part', caption:'Part', type: 'choice', ...main({}, true)}]
      
      module.exports = {main, getParameterDefinitions}
      

      image

      1 Reply Last reply Reply Quote 1
      • First post
        Last post
      Powered by NodeBB | Contributors