Discussion:
[OpenSCAD] use of for loop in function
Johan Jonker
2016-02-13 21:36:09 UTC
Permalink
Hello,

I tried to generate an array in a function using a for loop, but it does not
work.
What am I doing wrong.

I tried to do something like this:
The ct function generate a two dimensional vector.

function chamber_(r,rb,h,bt,bb,pc) = [
[0,0],
[for (i=[1:8]) ct(r,bt*i/16,0,r)],
[for (i=[1:8]) ct(rb,-bb*i/16,-h-rb,0,1)],
[-h,0],
[for (i=[8:1]) ct(rb,bb*i/16,-h-rb,0,1)],
[for (i=[8:1]) ct(r,bt*i/16,0,r)],
[0,0],
];




--
View this message in context: http://forum.openscad.org/use-of-for-loop-in-function-tp16116.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Torsten Paul
2016-02-13 21:50:40 UTC
Permalink
You probably need to remove the [ ] around the "for" calls:

p = [
[ 0, 0 ],
for (a = [ 1 : 9 ]) [ a, a ],
[ 10, 10 ],
for (a = [ 9 : -1 : 1 ]) [ a, a ],
[ 0, 0 ]
];

echo(p);

Also please use the 3 value variant for ranges that decrement
the value, e.g. [ 9 : -1 : 1 ].

Due to backward compatibility, defining [8:1] will NOT produce
the expected order, which is also mentioned in the warning:

DEPRECATED: Using ranges of the form [begin:end] with begin value greater than the end value is deprecated.

ciao,
Torsten.
Adam
2016-02-14 01:25:32 UTC
Permalink
Hi Paul,

Thank you for the info.

I am not at all certain what you are talking about because I do not know the language that OpenScad uses. I am a BASIC and Fortran guy.

I do understand "For Loops" etc but I need a reference for this language and more I need to understand the algorithm being used. I am accustomed to "Top Down algorythmic design."


----- Original Message -----

From: Torsten Paul<mailto:***@gmx.de>

To: ***@lists.openscad.org<mailto:***@lists.openscad.org>

Sent: Saturday, February 13, 2016 3:50 PM

Subject: Re: [OpenSCAD] use of for loop in function



You probably need to remove the [ ] around the "for" calls:

p = [
[ 0, 0 ],
for (a = [ 1 : 9 ]) [ a, a ],
[ 10, 10 ],
for (a = [ 9 : -1 : 1 ]) [ a, a ],
[ 0, 0 ]
];

echo(p);

Also please use the 3 value variant for ranges that decrement
the value, e.g. [ 9 : -1 : 1 ].

Due to backward compatibility, defining [8:1] will NOT produce
the expected order, which is also mentioned in the warning:

DEPRECATED: Using ranges of the form [begin:end] with begin value greater than the end value is deprecated.

ciao,
Torsten.
Johan Jonker
2016-02-14 06:59:23 UTC
Permalink
Thanks for reacting Torsten,

I made the modificatinos you suggested but get an error indicated after the
for statement when I remove the [].


ERROR: Parser error in line 410: syntax error
ERROR: Compilation failed!



--
View this message in context: http://forum.openscad.org/use-of-for-loop-in-function-tp16116p16119.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Johan Jonker
2016-02-14 07:07:15 UTC
Permalink
I also tried this one:

function chamber_(r,rb,h,bt,bb,pc) = [
concat([0,0],
[for (i=[1:1:8]) ct(r,bt*i/16,0,r)],
[for (i=[1:1:8]) ct(rb,-bb*i/16,-h-rb,0,1)],
[-h,0],
[for (i=[8:-1:1]) ct(rb,bb*i/16,-h-rb,0,1)],
[for (i=[8:-1:1]) ct(r,bt*i/16,0,r)],
[0,0])
];

It compiles but gives:
WARNING: PolySet has degenerate polygons



--
View this message in context: http://forum.openscad.org/use-of-for-loop-in-function-tp16116p16120.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Johan Jonker
2016-02-14 07:38:21 UTC
Permalink
As soon as I combine the [0,0] with the for statement it requires [] around
the for statement to compile.

This
ECHO: [[0, 0], [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0,
0]]]

is the result from this:

echo( newchamber_(0,0,0,0,0,0));
function newchamber_(r,rb,h,bt,bb,pc) = [
[0,0],
[for (i=[1:1:8]) ct(r,i*bt/16,0,r)]
];




and this
ECHO: [[0, 0, [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0,
0]]]

from this:
function newchamber_(r,rb,h,bt,bb,pc) = [
concat([0,0],
[for (i=[1:1:8]) ct(r,i*bt/16,0,r)])
];



--
View this message in context: http://forum.openscad.org/use-of-for-loop-in-function-tp16116p16121.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Johan Jonker
2016-02-14 08:29:47 UTC
Permalink
I decide to solve it in another way:

function drawroom(i,r,rb,h,bt,bb,pc) =
(i<= $fn/8)? ct(r,-bt*i/($fn/4),0,r):
(i<= 3*$fn/8)? csl(r,rb,h,bt,bb, (i-$fn/8)/($fn/4)):
(i<= 4*$fn/8) ? ct(rb,-bb*(4*$fn/8-i)/($fn/4),-h-rb,0,1):
(i<= 5*$fn/8) ? ct(rb,bb*(5*$fn/8-i)/($fn/4),-h-rb,0,1):
(i<= 7*$fn/8) ? csr(r,rb,h,bt,bb, (7*$fn/8-i)/($fn/4)):
ct(r,bt*(8*$fn/8-i),0,r);

function newchamber_(r,rb,h,bt,bb,pc) = [
for (i=[0:1:$fn-1]) drawroom(i,r,rb,h,bt,bb,pc)
];

There are still some bugs, but it compiles and it draws.



--
View this message in context: http://forum.openscad.org/use-of-for-loop-in-function-tp16116p16122.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Torsten Paul
2016-02-14 08:43:43 UTC
Permalink
Post by Johan Jonker
I made the modificatinos you suggested but get an error indicated
after the for statement when I remove the [].
ERROR: Parser error in line 410: syntax error
ERROR: Compilation failed!
This feature to have multiple "for" expressions in a single
list is just a couple of days old. If you want to use that,
you'll need the latest snapshot versions, it will not work
with the 2015.03 release.

It is possible to get the same result with the release
version using concat(), e.g.

echo([
for (a = [0 : 9]) [ a, 0 ],
for (a = [9 : -1 : 0]) [ a, 1 ],
[0, 0]
]);

is the same the following statement using concat() in the
release (note that this requires the double [] for the
[0, 0].

echo(concat(
[ for (a = [0 : 9]) [ a, 0 ] ],
[ for (a = [9 : -1 : 0]) [ a, 1 ] ],
[ [0, 0] ]));

ciao,
Torsten.
Johan Jonker
2016-02-14 11:44:56 UTC
Permalink
Ah, good to hear that openscad is evolving!



--
View this message in context: http://forum.openscad.org/use-of-for-loop-in-function-tp16116p16124.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Continue reading on narkive:
Loading...