discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Parsing values to children

MA
Martin Axelsen
Wed, Sep 20, 2023 4:02 PM

It is nice that we have the ability to override the built-in modules on
OpenSCAD, however it seems to elude me how to parse any values the the
children of a module as is the case with the built-in for() loop where for
instance the variable i is available for the children of for(i=[1:5])
One way to come around this is to specify the iterator as a global variable
like in children($angle=angle) as I have done in the code snippet below
below, but this is not how for() loops work.

The reason I came about this is that I need the iterator of the
rotate_extrude() angle parameter to be exposed within its child matrix.

Consider the code below

//prototype for alternative rotate_extrude() exposing $angle as iterator
variable - segments are not joined
module alt_rotate_extrude(angle=360, convexity=2){
$fn=($fn==undef||$fn==0)?10:$fn;
echo($fn, angle)
for(angle=[0:360/$fn:angle]){
rotate([0, 0, angle])
rotate_extrude(angle=360/$fn, convexity = convexity)
children($angle=angle);
}
}

// snail shell (approximated seashell surface)
snail_shell();
module snail_shell(){
let(phi=(sqrt(5)+1)/2)
alt_rotate_extrude(angle=900, convexity=2, $fn=50)
let(v=pow(phi,$angle/200))
translate([v, 2.2v])
difference(){
circle(r=v, $fn=$fn);
circle(r=v
0.9, $fn=$fn);
}
}

If one were to write their own for() module that is compatible with
existing code how would this be done/what would the code be like?

If this is indeed possible it would be really nice to have such reference
code snippets in the documentation along the built-ins.

Kharar

It is nice that we have the ability to override the built-in modules on OpenSCAD, however it seems to elude me how to parse any values the the children of a module as is the case with the built-in for() loop where for instance the variable i is available for the children of for(i=[1:5]) One way to come around this is to specify the iterator as a global variable like in children($angle=angle) as I have done in the code snippet below below, but this is not how for() loops work. The reason I came about this is that I need the iterator of the rotate_extrude() angle parameter to be exposed within its child matrix. Consider the code below //prototype for alternative rotate_extrude() exposing $angle as iterator variable - segments are not joined module alt_rotate_extrude(angle=360, convexity=2){ $fn=($fn==undef||$fn==0)?10:$fn; echo($fn, angle) for(angle=[0:360/$fn:angle]){ rotate([0, 0, angle]) rotate_extrude(angle=360/$fn, convexity = convexity) children($angle=angle); } } // snail shell (approximated seashell surface) snail_shell(); module snail_shell(){ let(phi=(sqrt(5)+1)/2) alt_rotate_extrude(angle=900, convexity=2, $fn=50) let(v=pow(phi,$angle/200)) translate([v, 2.2*v]) difference(){ circle(r=v, $fn=$fn); circle(r=v*0.9, $fn=$fn); } } If one were to write their own for() module that is compatible with existing code how would this be done/what would the code be like? If this is indeed possible it would be really nice to have such reference code snippets in the documentation along the built-ins. Kharar
AM
Adrian Mariano
Wed, Sep 20, 2023 8:37 PM

If I understand what you're trying to do (which is not at all certain) then
you just need to change

children($angle=angle);

into

$angle=angle;
children();

On Wed, Sep 20, 2023 at 12:05 PM Martin Axelsen kharar@gmail.com wrote:

It is nice that we have the ability to override the built-in modules on
OpenSCAD, however it seems to elude me how to parse any values the the
children of a module as is the case with the built-in for() loop where for
instance the variable i is available for the children of for(i=[1:5])
One way to come around this is to specify the iterator as a global
variable like in children($angle=angle) as I have done in the code snippet
below below, but this is not how for() loops work.

The reason I came about this is that I need the iterator of the
rotate_extrude() angle parameter to be exposed within its child matrix.

Consider the code below

//prototype for alternative rotate_extrude() exposing $angle as iterator
variable - segments are not joined
module alt_rotate_extrude(angle=360, convexity=2){
$fn=($fn==undef||$fn==0)?10:$fn;
echo($fn, angle)
for(angle=[0:360/$fn:angle]){
rotate([0, 0, angle])
rotate_extrude(angle=360/$fn, convexity = convexity)
children($angle=angle);
}
}

// snail shell (approximated seashell surface)
snail_shell();
module snail_shell(){
let(phi=(sqrt(5)+1)/2)
alt_rotate_extrude(angle=900, convexity=2, $fn=50)
let(v=pow(phi,$angle/200))
translate([v, 2.2v])
difference(){
circle(r=v, $fn=$fn);
circle(r=v
0.9, $fn=$fn);
}
}

If one were to write their own for() module that is compatible with
existing code how would this be done/what would the code be like?

If this is indeed possible it would be really nice to have such reference
code snippets in the documentation along the built-ins.

Kharar


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

If I understand what you're trying to do (which is not at all certain) then you just need to change children($angle=angle); into $angle=angle; children(); On Wed, Sep 20, 2023 at 12:05 PM Martin Axelsen <kharar@gmail.com> wrote: > It is nice that we have the ability to override the built-in modules on > OpenSCAD, however it seems to elude me how to parse any values the the > children of a module as is the case with the built-in for() loop where for > instance the variable i is available for the children of for(i=[1:5]) > One way to come around this is to specify the iterator as a global > variable like in children($angle=angle) as I have done in the code snippet > below below, but this is not how for() loops work. > > The reason I came about this is that I need the iterator of the > rotate_extrude() angle parameter to be exposed within its child matrix. > > Consider the code below > > //prototype for alternative rotate_extrude() exposing $angle as iterator > variable - segments are not joined > module alt_rotate_extrude(angle=360, convexity=2){ > $fn=($fn==undef||$fn==0)?10:$fn; > echo($fn, angle) > for(angle=[0:360/$fn:angle]){ > rotate([0, 0, angle]) > rotate_extrude(angle=360/$fn, convexity = convexity) > children($angle=angle); > } > } > > // snail shell (approximated seashell surface) > snail_shell(); > module snail_shell(){ > let(phi=(sqrt(5)+1)/2) > alt_rotate_extrude(angle=900, convexity=2, $fn=50) > let(v=pow(phi,$angle/200)) > translate([v, 2.2*v]) > difference(){ > circle(r=v, $fn=$fn); > circle(r=v*0.9, $fn=$fn); > } > } > > If one were to write their own for() module that is compatible with > existing code how would this be done/what would the code be like? > > If this is indeed possible it would be really nice to have such reference > code snippets in the documentation along the built-ins. > > > Kharar > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
MA
Martin Axelsen
Wed, Sep 20, 2023 8:47 PM

Not so much, I guess I just want to avoid using global variables because
that is considered good practice in most programming languages.

I believe

children($angle=angle);

and

$angle=angle;
children();

is going to have the same effect in my case.

On Wed, 20 Sept 2023 at 22:37, Adrian Mariano avm4@cornell.edu wrote:

If I understand what you're trying to do (which is not at all certain)
then you just need to change

children($angle=angle);

into

$angle=angle;
children();

On Wed, Sep 20, 2023 at 12:05 PM Martin Axelsen kharar@gmail.com wrote:

It is nice that we have the ability to override the built-in modules on
OpenSCAD, however it seems to elude me how to parse any values the the
children of a module as is the case with the built-in for() loop where for
instance the variable i is available for the children of for(i=[1:5])
One way to come around this is to specify the iterator as a global
variable like in children($angle=angle) as I have done in the code snippet
below below, but this is not how for() loops work.

The reason I came about this is that I need the iterator of the
rotate_extrude() angle parameter to be exposed within its child matrix.

Consider the code below

//prototype for alternative rotate_extrude() exposing $angle as iterator
variable - segments are not joined
module alt_rotate_extrude(angle=360, convexity=2){
$fn=($fn==undef||$fn==0)?10:$fn;
echo($fn, angle)
for(angle=[0:360/$fn:angle]){
rotate([0, 0, angle])
rotate_extrude(angle=360/$fn, convexity = convexity)
children($angle=angle);
}
}

// snail shell (approximated seashell surface)
snail_shell();
module snail_shell(){
let(phi=(sqrt(5)+1)/2)
alt_rotate_extrude(angle=900, convexity=2, $fn=50)
let(v=pow(phi,$angle/200))
translate([v, 2.2v])
difference(){
circle(r=v, $fn=$fn);
circle(r=v
0.9, $fn=$fn);
}
}

If one were to write their own for() module that is compatible with
existing code how would this be done/what would the code be like?

If this is indeed possible it would be really nice to have such reference
code snippets in the documentation along the built-ins.

Kharar


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Not so much, I guess I just want to avoid using global variables because that is considered good practice in most programming languages. I believe *children($angle=angle);* and *$angle=angle;* *children();* is going to have the same effect in my case. On Wed, 20 Sept 2023 at 22:37, Adrian Mariano <avm4@cornell.edu> wrote: > If I understand what you're trying to do (which is not at all certain) > then you just need to change > > children($angle=angle); > > into > > $angle=angle; > children(); > > > On Wed, Sep 20, 2023 at 12:05 PM Martin Axelsen <kharar@gmail.com> wrote: > >> It is nice that we have the ability to override the built-in modules on >> OpenSCAD, however it seems to elude me how to parse any values the the >> children of a module as is the case with the built-in for() loop where for >> instance the variable i is available for the children of for(i=[1:5]) >> One way to come around this is to specify the iterator as a global >> variable like in children($angle=angle) as I have done in the code snippet >> below below, but this is not how for() loops work. >> >> The reason I came about this is that I need the iterator of the >> rotate_extrude() angle parameter to be exposed within its child matrix. >> >> Consider the code below >> >> //prototype for alternative rotate_extrude() exposing $angle as iterator >> variable - segments are not joined >> module alt_rotate_extrude(angle=360, convexity=2){ >> $fn=($fn==undef||$fn==0)?10:$fn; >> echo($fn, angle) >> for(angle=[0:360/$fn:angle]){ >> rotate([0, 0, angle]) >> rotate_extrude(angle=360/$fn, convexity = convexity) >> children($angle=angle); >> } >> } >> >> // snail shell (approximated seashell surface) >> snail_shell(); >> module snail_shell(){ >> let(phi=(sqrt(5)+1)/2) >> alt_rotate_extrude(angle=900, convexity=2, $fn=50) >> let(v=pow(phi,$angle/200)) >> translate([v, 2.2*v]) >> difference(){ >> circle(r=v, $fn=$fn); >> circle(r=v*0.9, $fn=$fn); >> } >> } >> >> If one were to write their own for() module that is compatible with >> existing code how would this be done/what would the code be like? >> >> If this is indeed possible it would be really nice to have such reference >> code snippets in the documentation along the built-ins. >> >> >> Kharar >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> >
AM
Adrian Mariano
Wed, Sep 20, 2023 10:26 PM

Ah, that turns out to be version dependent.  In the stable version
(2021.01) passing it as an arg to children() gives a warning and doesn't
propagate the parameter.  In the dev version they seem to be equivalent.

There is no other mechanism for passing information into children from the
parent other than $ variables.

On Wed, Sep 20, 2023 at 4:48 PM Martin Axelsen kharar@gmail.com wrote:

Not so much, I guess I just want to avoid using global variables because
that is considered good practice in most programming languages.

I believe

children($angle=angle);

and

$angle=angle;
children();

is going to have the same effect in my case.

On Wed, 20 Sept 2023 at 22:37, Adrian Mariano avm4@cornell.edu wrote:

If I understand what you're trying to do (which is not at all certain)
then you just need to change

children($angle=angle);

into

$angle=angle;
children();

On Wed, Sep 20, 2023 at 12:05 PM Martin Axelsen kharar@gmail.com wrote:

It is nice that we have the ability to override the built-in modules on
OpenSCAD, however it seems to elude me how to parse any values the the
children of a module as is the case with the built-in for() loop where for
instance the variable i is available for the children of for(i=[1:5])
One way to come around this is to specify the iterator as a global
variable like in children($angle=angle) as I have done in the code snippet
below below, but this is not how for() loops work.

The reason I came about this is that I need the iterator of the
rotate_extrude() angle parameter to be exposed within its child matrix.

Consider the code below

//prototype for alternative rotate_extrude() exposing $angle as iterator
variable - segments are not joined
module alt_rotate_extrude(angle=360, convexity=2){
$fn=($fn==undef||$fn==0)?10:$fn;
echo($fn, angle)
for(angle=[0:360/$fn:angle]){
rotate([0, 0, angle])
rotate_extrude(angle=360/$fn, convexity = convexity)
children($angle=angle);
}
}

// snail shell (approximated seashell surface)
snail_shell();
module snail_shell(){
let(phi=(sqrt(5)+1)/2)
alt_rotate_extrude(angle=900, convexity=2, $fn=50)
let(v=pow(phi,$angle/200))
translate([v, 2.2v])
difference(){
circle(r=v, $fn=$fn);
circle(r=v
0.9, $fn=$fn);
}
}

If one were to write their own for() module that is compatible with
existing code how would this be done/what would the code be like?

If this is indeed possible it would be really nice to have such
reference code snippets in the documentation along the built-ins.

Kharar


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Ah, that turns out to be version dependent. In the stable version (2021.01) passing it as an arg to children() gives a warning and doesn't propagate the parameter. In the dev version they seem to be equivalent. There is no other mechanism for passing information into children from the parent other than $ variables. On Wed, Sep 20, 2023 at 4:48 PM Martin Axelsen <kharar@gmail.com> wrote: > Not so much, I guess I just want to avoid using global variables because > that is considered good practice in most programming languages. > > > I believe > > *children($angle=angle);* > > and > > > *$angle=angle;* > *children();* > > is going to have the same effect in my case. > > > > On Wed, 20 Sept 2023 at 22:37, Adrian Mariano <avm4@cornell.edu> wrote: > >> If I understand what you're trying to do (which is not at all certain) >> then you just need to change >> >> children($angle=angle); >> >> into >> >> $angle=angle; >> children(); >> >> >> On Wed, Sep 20, 2023 at 12:05 PM Martin Axelsen <kharar@gmail.com> wrote: >> >>> It is nice that we have the ability to override the built-in modules on >>> OpenSCAD, however it seems to elude me how to parse any values the the >>> children of a module as is the case with the built-in for() loop where for >>> instance the variable i is available for the children of for(i=[1:5]) >>> One way to come around this is to specify the iterator as a global >>> variable like in children($angle=angle) as I have done in the code snippet >>> below below, but this is not how for() loops work. >>> >>> The reason I came about this is that I need the iterator of the >>> rotate_extrude() angle parameter to be exposed within its child matrix. >>> >>> Consider the code below >>> >>> //prototype for alternative rotate_extrude() exposing $angle as iterator >>> variable - segments are not joined >>> module alt_rotate_extrude(angle=360, convexity=2){ >>> $fn=($fn==undef||$fn==0)?10:$fn; >>> echo($fn, angle) >>> for(angle=[0:360/$fn:angle]){ >>> rotate([0, 0, angle]) >>> rotate_extrude(angle=360/$fn, convexity = convexity) >>> children($angle=angle); >>> } >>> } >>> >>> // snail shell (approximated seashell surface) >>> snail_shell(); >>> module snail_shell(){ >>> let(phi=(sqrt(5)+1)/2) >>> alt_rotate_extrude(angle=900, convexity=2, $fn=50) >>> let(v=pow(phi,$angle/200)) >>> translate([v, 2.2*v]) >>> difference(){ >>> circle(r=v, $fn=$fn); >>> circle(r=v*0.9, $fn=$fn); >>> } >>> } >>> >>> If one were to write their own for() module that is compatible with >>> existing code how would this be done/what would the code be like? >>> >>> If this is indeed possible it would be really nice to have such >>> reference code snippets in the documentation along the built-ins. >>> >>> >>> Kharar >>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >>