Accumulating variable in openSCAD

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Accumulating variable in openSCAD

PhilipJ

Hi,

I’m new here, so thanks for adding me to the list.

 

I would like some advice on how to achieve something in openSCAD programming language. I want to put a row of holes into something where each hole is larger than the last and with the holes spaced apart by the diameter of the previous hole (basically a drill size guide).

 

Something like:

difference()

{

  cube([90, 30, 5]);

  offset = 0;

  for (a = [0: 1: 20])

  {

    dia = 1.0+(a/10.0);

    offset = 4 + (2 * dia);

    translate([offset, 4, -1])

    {

      cylinder(h=7, d=dia);

    }

  }

}

 

This produces a row of holes of increasing size, but the increasing offset doesn’t work. I think I read that autoSCAD doesn’t have “variables” like other programming languages (clearly ‘offset’ isn’t working how I am expecting it to) but not being an expert (except perhaps in ‘C’ which I’ve used for about 30 years) I don’t really know what that means or how to write something that would work.

 

Any advice, or code example would be gratefully received.

 

PhilipJ

 


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Accumulating variable in openSCAD

Neon22

Try two passes. First generate a list of the dia and offset as pairs.
Then consume the list while making the objects.

dims = [ for (a=[0:20]) [1+a/10, 4+(1+a/10)*2]  ];
echo(dims);
difference() {
    cube([90, 30, 5]);
    //
    for (d = dims) {
        #translate([d[1], 4, -1])
            cylinder(h=7, d=d[0]);
    }
}


On 10/22/2018 9:49 PM, Philip Jones wrote:

Hi,

I’m new here, so thanks for adding me to the list.

 

I would like some advice on how to achieve something in openSCAD programming language. I want to put a row of holes into something where each hole is larger than the last and with the holes spaced apart by the diameter of the previous hole (basically a drill size guide).

 

Something like:

difference()

{

  cube([90, 30, 5]);

  offset = 0;

  for (a = [0: 1: 20])

  {

    dia = 1.0+(a/10.0);

    offset = 4 + (2 * dia);

    translate([offset, 4, -1])

    {

      cylinder(h=7, d=dia);

    }

  }

}

 

This produces a row of holes of increasing size, but the increasing offset doesn’t work. I think I read that autoSCAD doesn’t have “variables” like other programming languages (clearly ‘offset’ isn’t working how I am expecting it to) but not being an expert (except perhaps in ‘C’ which I’ve used for about 30 years) I don’t really know what that means or how to write something that would work.

 

Any advice, or code example would be gratefully received.

 

PhilipJ

 



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Accumulating variable in openSCAD

PhilipJ
Neon22 wrote

> Try two passes. First generate a list of the dia and offset as
>       pairs.
>       Then consume the list while making the objects.
>     dims = [ for (a=[0:20]) [1+a/10, 4+(1+a/10)*2]  ];
>       echo(dims);
>       difference() {
>           cube([90, 30, 5]);
>           //
>           for (d = dims) {
>               #translate([d[1], 4, -1])
>                   cylinder(h=7, d=d[0]);
>           }
>       }

Thanks for that, I'm always learning something :-)
PhilipJ




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Accumulating variable in openSCAD

PhilipJ
In reply to this post by Neon22
Hi again,

I tried your code suggestion and it worked as it was supposed to but it made
me realize I need a more complex set of offsets. Basically the offset needs
to be:
     the offset of the previous element + 2 * dia of the hole.

I'm not familiar with how to manipulate 'lists' so can you suggest how I
could do something like:

   dims[i] = dims[i-1] + 2 * dia;

I only know how to show this in 'C' so it's probably nonsense in OpenSCAD
language.
I have looked through the online documentation but I can't find anything
that specifically explains this usage of creating a list or of the
for(d=dims) usage. Is this a general scripting form ? will I find it in
documentation for Python or Lua ?

Sorry for using up your time like this, I appreciate the help
PhilipJ




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Accumulating variable in openSCAD

runsun
In reply to this post by Neon22
Since Neon22 already figured out the formula to generate the offset (in the
dims), which is the critical part, there's actually no need to have 2
for-loops:

difference() {
    cube([90, 30, 5]);    
    for (a = [0: 1: 20]) {
        translate([4+(1+a/10)*2, 4, -1])
            cylinder(h=7, d=1+a/10);
    }
}

If the formula is too complicated to figure out (or you are too lazy), an
alternative is to use recursive module:

module holes(offset=0, d=d0, _i=0)
{
    // Draw the cylinder

    translate( [offset,4,-1] ) cylinder( ... );
   
    // Draw next one with new variable values

    if(_i< number_of_holes)
        holes( offset= offset + ...
               ,  d = d + ...
               , _i = _i+1
               );
}

In this approach you only need to worry about the relationship between the
current iteration and the one right before it.



-----

$  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets
Reply | Threaded
Open this post in threaded view
|

Re: Accumulating variable in openSCAD

PhilipJ
Hey runsun thanks,

that worked perfectly :-)

PhilipJ




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org