Openscad to JSCAD Refinement



  • howdy all,

    i host a few pages where folks can determine the diameter of sphere to resonate a given frequency and then 3d print them if they wish using openjscad. i originally wrote some code with openscad before i was aware of openjscad. i have been able to port the code over from openscad, however there is some functionality i haven't been able convert from openscad. as shown below, with the openscad code i was able to provide an area where visitors are able to enter the variables and then the remainder of the code processes those variables. with jscad, i have been unable to provide the same functionality and therefor visitors must edit throughout the code directly with whatever instructions i can provide to make it not too confusing. there are two sphere types. one has a sound hole and the other has a tube elongating the sound hole. below is what i have done for each type.

    Sphere with a sound hole -

    OPENSCAD:

    // Example A 440Hz

    // fragment number

    $fn=100;

    // Enter sphere diameter

    sd=143.38;

    // Enter wall thickness

    wt=2;

    // Enter sound hole diameter.

    sh=25;

    difference() {
    sphere(d=sd+(2wt));
    sphere(d=sd);
    translate([0,0,-sd/2-wt])cylinder(d=sh,h=wt
    2);
    }

    JSCAD:

    // Example A 440Hz

    // BEGINNING OF CODE

    function
    main() {
    return [

    // Enter radius of sphere plus 2 for r:
    difference(sphere({r: 47, center:true, fn: 100}),

    // Enter radius of sphere for r:
    sphere({r: 45, center:true, fn: 100}),

    // Enter radius of sound hole for r: and h= -radius of first sphere
    cylinder({r: 12.5, h: -47})),

    ];
    }

    // END OF CODE

    Sphere with tube -

    OPENSCAD:

    // Example: A 220Hz

    // fragment number

    $fn=100;

    // Enter sphere diameter.

    sd=132.47;

    // Enter Sound Hole Diameter.

    sh=25;

    // Enter length of neck

    ln=25;

    // Enter wall Thickness

    wt=2;

    // SPHERE

    difference() {
    sphere(d=sd+(2wt));
    sphere(d=sd);
    translate([0,0,-(sd/2)-wt])cylinder(d=sh,h=wt
    2);
    }

    // NECK

    difference() {
    translate([0,0,-ln-(sd/2)+wt])cylinder(d=sh+(2*wt),h=ln);
    translate([0,0,-ln-(sd/2)+wt])cylinder(d=sh,h=ln);
    }

    JSCAD:

    // Example: A 440Hz

    // BEGINNING OF CODE

    function
    main() {
    return [

    // Sphere1. Enter radius of sphere +2 for r:

    difference(sphere({r: 35, center:true, fn: 100}),

    // Sphere2. Enter radius of sphere for r:

    sphere({r: 33, center:true, fn: 100}),

    // Enter radius of sound hole for r: and radius of sphere1 for h:

    cylinder({r: 12.5, h: -35, fn: 100})),

    // Enter radius of sound hole +2 for r:
    // Enter length of neck for h:
    // Enter radius of sphere2 plus height of neck
    // minus 2x wall thickness(2x2=4 in this example) for .translate

    difference(cylinder({r: 14.5, h: 25, fn: 100}).translate([0,0,-54]),

    // Enter radius of sound hole for r:
    // Enter length of neck for h:
    // Enter radius of sphere2 plus height of neck
    // minus 2x wall thickness(2x2=4 in this example) for .translate

    cylinder({r: 12.5, h: 25, fn: 100}).translate([0,0,-54])),

    ];
    }

    // END OF CODE

    i would love for visitors to be able to simply define the variables of sphere radius or diameter, sound hole diameter, tube length, wall thickness, and fragment number and then let the code do the rest.

    thank you for any insights you can offer.



  • @gilboonet
    thank you for that.
    after a few mis-steps and with the guidance of editor prompts, i came up with this which seems to work for the sphere with a sound hole.
    i'm tickled pink because i had wanted to provide form fields for the parameters also, but due to my newbieness, thought it would be additional html/javascripting.


    function
    main(params) {

     var sr = params.sphereradius;
     var sh = params.soundhole;
     var wt = params.wallthickness;
     var fn = params.fragmentnumber;
      
     return [	  
        difference(sphere({r:sr+wt, center:true, fn:fn}),
        sphere({r:sr, center:true, fn:fn}),
        cylinder ({r:sh, h:-(sr+wt)}))
    ]; 
    

    }

    function getParameterDefinitions() {
    return [
    { name: 'sphereradius', type: 'number', initial: 47, caption: 'Sphere Radius' },
    { name: 'soundhole', type: 'number', initial: 15, caption: 'Sound Hole Radius' },
    { name: 'wallthickness', type: 'number', initial: 2, caption: 'Wall Thickness' },
    { name: 'fragmentnumber', type: 'number', initial: 100, caption: 'Fragment Number' },
    ];
    }



  • Hello, you can use design parameters, it is documented here : https://openjscad.org/dokuwiki/doku.php?id=quick_reference_parameters. You add a function called getParametersDefinition() where you describe your parameters, and you add a parameter variable to your main() so it become main(params). Then you can use params.myParam1.value and so on into your code.


Log in to reply