discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Accumulating variable in openSCAD

PJ
Philip Jones
Mon, Oct 22, 2018 8:49 AM

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

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
MS
Mark Schafer
Mon, Oct 22, 2018 9:24 AM

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:

<!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0cm; margin-bottom:.0001pt; font-size:11.0pt; font-family:"Calibri","sans-serif";} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:purple; text-decoration:underline;} span.EmailStyle17 {mso-style-type:personal-compose; font-family:"Calibri","sans-serif"; color:windowtext;} .MsoChpDefault {mso-style-type:export-only;} @page WordSection1 {size:612.0pt 792.0pt; margin:72.0pt 72.0pt 72.0pt 72.0pt;} div.WordSection1 {page:WordSection1;} -->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

<pre wrap="">_______________________________________________
OpenSCAD mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Discuss@lists.openscad.org">Discuss@lists.openscad.org</a>
<a class="moz-txt-link-freetext" href="http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org">http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org</a>
P
PhilipJ
Mon, Oct 22, 2018 9:47 AM

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/

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/
P
PhilipJ
Mon, Oct 22, 2018 1:45 PM

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/

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/
R
runsun
Mon, Oct 22, 2018 8:36 PM

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 ); $ Tips ; $ Snippets

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

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/
P
PhilipJ
Tue, Oct 23, 2018 7:52 AM

Hey runsun thanks,

that worked perfectly :-)

PhilipJ

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

Hey runsun thanks, that worked perfectly :-) PhilipJ -- Sent from: http://forum.openscad.org/