discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

use of for loop in function

JJ
Johan Jonker
Sat, Feb 13, 2016 9:36 PM

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,bti/16,0,r)],
[for (i=[1:8]) ct(rb,-bb
i/16,-h-rb,0,1)],
[-h,0],
[for (i=[8:1]) ct(rb,bbi/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.

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.
TP
Torsten Paul
Sat, Feb 13, 2016 9:50 PM

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.

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.
A
Adam
Sun, Feb 14, 2016 1:25 AM

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 Paulmailto:Torsten.Paul@gmx.de

To: discuss@lists.openscad.orgmailto:discuss@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.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

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:Torsten.Paul@gmx.de> To: discuss@lists.openscad.org<mailto:discuss@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. _______________________________________________ OpenSCAD mailing list Discuss@lists.openscad.org http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
JJ
Johan Jonker
Sun, Feb 14, 2016 6:59 AM

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.

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.
JJ
Johan Jonker
Sun, Feb 14, 2016 7:07 AM

I also tried this one:

function chamber_(r,rb,h,bt,bb,pc) = [
concat([0,0],
[for (i=[1:1:8]) ct(r,bti/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,bbi/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.

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.
JJ
Johan Jonker
Sun, Feb 14, 2016 7:38 AM

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.

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.
JJ
Johan Jonker
Sun, Feb 14, 2016 8:29 AM

I decide to solve it in another way:

function drawroom(i,r,rb,h,bt,bb,pc) =
(i<= $fn/8)? ct(r,-bti/($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.

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.
TP
Torsten Paul
Sun, Feb 14, 2016 8:43 AM

On 02/14/2016 07:59 AM, Johan Jonker wrote:

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.

On 02/14/2016 07:59 AM, Johan Jonker wrote: > 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.
JJ
Johan Jonker
Sun, Feb 14, 2016 11:44 AM

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.

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.