JSCAD User Group
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Openscad to JSCAD Refinement

    Scheduled Pinned Locked Moved Development Discussions
    3 Posts 2 Posters 1.5k Views 2 Watching
    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.
    • drewpearD Offline
      drewpear
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • gilboonetG Offline
        gilboonet
        last edited by gilboonet

        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.

        drewpearD 1 Reply Last reply Reply Quote 0
        • drewpearD Offline
          drewpear @gilboonet
          last edited by drewpear

          @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' },
          ];
          }

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          Powered by NodeBB | Contributors