JSCAD User Group

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

    Question about arrays.

    General Discussions
    3
    3
    356
    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.
    • J
      jarshvor last edited by

      What is the proper way to work with arrays that have been generated with a for loop and push?
      Excuse if my terminology is not correct. I'm fairly new to openjscad and js as a whole.

      In essence I have an array of positions and want to punch holes in a plate at those positions.

      I am trying with the following:

      function pcb () {
          pcb_width = 25;
          pcb_height = 3;
          pcb_thickness = 50;
          pcb_dimmensions = [(pcb_width), (pcb_thickness), (pcb_height)];
          
          return cube(pcb_dimmensions);
      }
      
      function holes1 () {
          var array = [];
          array.push(translate([10,10,0],
                          cylinder({h: 20})));
          array.push(translate([5,10,0],
                          cylinder({h: 20})));
          return array;
      }
      
      function holes2 () {
          var holes = [];
          var positions = [
              [4, 12, -1],
              [8, 12, -1],
              [8, 18, -1],
              [12, 18, -1],
              ];
          
          for (i=0; i <= positions.length; i++) {
              holes.push(translate(positions[i],
                      cylinder({h: 10})))
          }
          return holes
      }
      
      function main () {
          //return holes2 ();
          //why does return [pcb()] work but not return [holes1(), or holes2()] ?
          //return [pcb(),holes1()] //does not work
          //return difference (pcb(),holes1());      // does not works
          return union (pcb(),holes1());      //works
          //return union (pcb(),holes2());    //does not work
      }
      

      Questions:

      1. Why can I not return an array consisting of the pcb and the holes-array (cylinders)? Is nesting a problem here?
      2. Union of pcb() and holes1() works, but not if holes2() is used. The function which includes a for loop.
      3. Difference of pcb() and holes() works for neither.

      As a side note, I am assuming the community hangs here and not on IRC or such?
      Looking for a place to quickly solve some doubts to help me get on my feet.

      1 Reply Last reply Reply Quote 0
      • z3dev
        z3dev @gilboonet last edited by z3dev

        @jarshvor another way to look at this is the return types...

        pcb() returns an object (1)
        holes1() returns an array of objects (2)
        holes2() returns an array of objects (4)

        V1 OpenJSCAD.org requires main() to return an ARRAY of objects or a single object.
        (FYI, V2 automatically flattens everything into an array.)

        @gilboonet is correct in the example provided. By using '...holes1()' syntax, an array is constructed from [ pcb(), holes1[0], holes1[1] ]

        Hang in there. You'll get it soon. 🙂

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

          I recently had same trouble with arrays. If you want to return your values without being forced to union them, you can use ... on those who are iterable (mainly arrays).

          pcb() returns a value, so it is not iterable, you return it directly
          holes1() returns an array so you can return ...holes1()

          put together that makes

          return [pcb(), ...holes1()]

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