Passing parameters to children.

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

Passing parameters to children.

nophead
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

Jesse Guardiani
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

kintel
Administrator
In reply to this post by nophead
On 2013-06-19, at 11:21 , nop head wrote:
>
> Is this something I can rely on to work?
>
I've been waiting for that question ;)
I'm trying to limit the use of it, as it feels wrong to mix these two types of lookups, but we don't have a better mechanism for dealing with such constructs.

Ideas for improving this are welcome!

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

clothbot

On 2013-06-19, at 1:02 PM, Marius Kintel <[hidden email]> wrote:

On 2013-06-19, at 11:21 , nop head wrote:

Is this something I can rely on to work?

I've been waiting for that question ;)
I'm trying to limit the use of it, as it feels wrong to mix these two types of lookups, but we don't have a better mechanism for dealing with such constructs.

Ideas for improving this are welcome!

-Marius

How about always treat the first argument of child() as the index, then strip it off and pass the remaining arguments to the module being child-ed?

Andrew.

--

"The future is already here.  It's just not very evenly distributed" -- William Gibson

Me: http://clothbot.com/wiki/




_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
In reply to this post by kintel
For this case you could allow extra parameter to child() and pass all but the first to the child. The problem is the child that needs the info might not be the top of the tree. For example there might be a translate or a group() in between.


On 19 June 2013 18:02, Marius Kintel <[hidden email]> wrote:
On 2013-06-19, at 11:21 , nop head wrote:
>
> Is this something I can rely on to work?
>
I've been waiting for that question ;)
I'm trying to limit the use of it, as it feels wrong to mix these two types of lookups, but we don't have a better mechanism for dealing with such constructs.

Ideas for improving this are welcome!

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

Jesse Guardiani
In reply to this post by nophead
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

Jesse Guardiani
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
It comes from the fact that openscad cannot pass modules as parameters and then call them. Instead it places them as children in the tree.


On 19 June 2013 19:04, nop head <[hidden email]> wrote:
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

kintel
Administrator
On 2013-06-19, at 14:06 , nop head wrote:

> It comes from the fact that openscad cannot pass modules as parameters and then call them. Instead it places them as children in the tree.
>
Passing modules as parameters (generalized through storing modules in variables) would be a nice feature. It's been discussed a bit here and there, but it's a larger job to make it happen.

As pointed out earlier in this  thread, passing a parameter list to a child() invocation might be a good idea, and basically append these to the child object in question, overriding any defaults which may have been given to it.

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

Jesse Guardiani
In reply to this post by nophead
Can you do something like this?

module draw_fixing_block() {
  // draw stuff here
}

module draw_inverted_fixing_block() {
  rotate(a=180,v=[blah]) draw_fixing_block();
}

module position_fixing_blocks() {
  // upper left
  translate(v=[blah]) draw_fixing_block();
  // upper right
  translate(v=[blah]) draw_fixing_block();  
}

module position_inverted_fixing_blocks() {
  // lower left
  translate(v=[blah]) draw_inverted_fixing_block();
  // upper right
  translate(v=[blah]) draw_inverted_fixing_block();  
}
 


On Wed, Jun 19, 2013 at 2:04 PM, nop head <[hidden email]> wrote:
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
I didn't want to invert the block, just one screw and change the nut trap to a round hole for a washer.

Also I re-use the code that positions the blocks to drill the holes in the panels. In your example it is hard wired to draw the blocks.


On 19 June 2013 20:39, Jesse Guardiani <[hidden email]> wrote:
Can you do something like this?

module draw_fixing_block() {
  // draw stuff here
}

module draw_inverted_fixing_block() {
  rotate(a=180,v=[blah]) draw_fixing_block();
}

module position_fixing_blocks() {
  // upper left
  translate(v=[blah]) draw_fixing_block();
  // upper right
  translate(v=[blah]) draw_fixing_block();  
}

module position_inverted_fixing_blocks() {
  // lower left
  translate(v=[blah]) draw_inverted_fixing_block();
  // upper right
  translate(v=[blah]) draw_inverted_fixing_block();  
}
 


On Wed, Jun 19, 2013 at 2:04 PM, nop head <[hidden email]> wrote:
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
Also the child that drills the holes is passed a cylinder to do the drilling, but also it places the fasteners when it is passed a screw assembly as a child.


On 19 June 2013 20:47, nop head <[hidden email]> wrote:
I didn't want to invert the block, just one screw and change the nut trap to a round hole for a washer.

Also I re-use the code that positions the blocks to drill the holes in the panels. In your example it is hard wired to draw the blocks.


On 19 June 2013 20:39, Jesse Guardiani <[hidden email]> wrote:
Can you do something like this?

module draw_fixing_block() {
  // draw stuff here
}

module draw_inverted_fixing_block() {
  rotate(a=180,v=[blah]) draw_fixing_block();
}

module position_fixing_blocks() {
  // upper left
  translate(v=[blah]) draw_fixing_block();
  // upper right
  translate(v=[blah]) draw_fixing_block();  
}

module position_inverted_fixing_blocks() {
  // lower left
  translate(v=[blah]) draw_inverted_fixing_block();
  // upper right
  translate(v=[blah]) draw_inverted_fixing_block();  
}
 


On Wed, Jun 19, 2013 at 2:04 PM, nop head <[hidden email]> wrote:
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
Thinking more about it, that type of composition of three modules would be harder to do in other languages. The top level parent has no knowledge that its child is also a parent child composition. If that was done with function pointers in something like C/C++ then you would need to write a wrapper/functor to allow the top level module to call the second level passing a pointer to the third level with knowing anything about it.


On 19 June 2013 20:49, nop head <[hidden email]> wrote:
Also the child that drills the holes is passed a cylinder to do the drilling, but also it places the fasteners when it is passed a screw assembly as a child.


On 19 June 2013 20:47, nop head <[hidden email]> wrote:
I didn't want to invert the block, just one screw and change the nut trap to a round hole for a washer.

Also I re-use the code that positions the blocks to drill the holes in the panels. In your example it is hard wired to draw the blocks.


On 19 June 2013 20:39, Jesse Guardiani <[hidden email]> wrote:
Can you do something like this?

module draw_fixing_block() {
  // draw stuff here
}

module draw_inverted_fixing_block() {
  rotate(a=180,v=[blah]) draw_fixing_block();
}

module position_fixing_blocks() {
  // upper left
  translate(v=[blah]) draw_fixing_block();
  // upper right
  translate(v=[blah]) draw_fixing_block();  
}

module position_inverted_fixing_blocks() {
  // lower left
  translate(v=[blah]) draw_inverted_fixing_block();
  // upper right
  translate(v=[blah]) draw_inverted_fixing_block();  
}
 


On Wed, Jun 19, 2013 at 2:04 PM, nop head <[hidden email]> wrote:
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566




_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

Jesse Guardiani
You've lost me. I'd need to see example code of what you're talking about, I think. The one thing the frickin internet lacks is a decent whiteboard.

I wasn't really trying to suggest you invert anything. I was just trying to provide an alternate way to organize your code so you could get around using child(). I'm not convinced child() is necessary.



On Wed, Jun 19, 2013 at 3:59 PM, nop head <[hidden email]> wrote:
Thinking more about it, that type of composition of three modules would be harder to do in other languages. The top level parent has no knowledge that its child is also a parent child composition. If that was done with function pointers in something like C/C++ then you would need to write a wrapper/functor to allow the top level module to call the second level passing a pointer to the third level with knowing anything about it.


On 19 June 2013 20:49, nop head <[hidden email]> wrote:
Also the child that drills the holes is passed a cylinder to do the drilling, but also it places the fasteners when it is passed a screw assembly as a child.


On 19 June 2013 20:47, nop head <[hidden email]> wrote:
I didn't want to invert the block, just one screw and change the nut trap to a round hole for a washer.

Also I re-use the code that positions the blocks to drill the holes in the panels. In your example it is hard wired to draw the blocks.


On 19 June 2013 20:39, Jesse Guardiani <[hidden email]> wrote:
Can you do something like this?

module draw_fixing_block() {
  // draw stuff here
}

module draw_inverted_fixing_block() {
  rotate(a=180,v=[blah]) draw_fixing_block();
}

module position_fixing_blocks() {
  // upper left
  translate(v=[blah]) draw_fixing_block();
  // upper right
  translate(v=[blah]) draw_fixing_block();  
}

module position_inverted_fixing_blocks() {
  // lower left
  translate(v=[blah]) draw_inverted_fixing_block();
  // upper right
  translate(v=[blah]) draw_inverted_fixing_block();  
}
 


On Wed, Jun 19, 2013 at 2:04 PM, nop head <[hidden email]> wrote:
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566




_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
The code is many pages but in essence

module fixing_block_positions() {
   translate() rotate() child(); // ten of these
   ...
}

module fixing_block_screw_positions() {
   translate() rotate() child(); // three of these
   ....
}

module fixing_block_assembly() {
   fixing_block();
   fixing_block_screw_positions()
        screw_and_nut_assembly();
}

To drill the holes in the frame

difference() {
    frame_sheets();
    fixing_block_positions()
        fixing_block_screw_positions()
            cylinder(r = screw_clearance_radius, h = 100, center = true);

}

To place the fixing blocks and their fasteners.

fixing_block_positions()
   fixing_block_assembly();


It could obviously be done without child, but I think the code would be less succinct.


On 19 June 2013 21:25, Jesse Guardiani <[hidden email]> wrote:
You've lost me. I'd need to see example code of what you're talking about, I think. The one thing the frickin internet lacks is a decent whiteboard.

I wasn't really trying to suggest you invert anything. I was just trying to provide an alternate way to organize your code so you could get around using child(). I'm not convinced child() is necessary.



On Wed, Jun 19, 2013 at 3:59 PM, nop head <[hidden email]> wrote:
Thinking more about it, that type of composition of three modules would be harder to do in other languages. The top level parent has no knowledge that its child is also a parent child composition. If that was done with function pointers in something like C/C++ then you would need to write a wrapper/functor to allow the top level module to call the second level passing a pointer to the third level with knowing anything about it.


On 19 June 2013 20:49, nop head <[hidden email]> wrote:
Also the child that drills the holes is passed a cylinder to do the drilling, but also it places the fasteners when it is passed a screw assembly as a child.


On 19 June 2013 20:47, nop head <[hidden email]> wrote:
I didn't want to invert the block, just one screw and change the nut trap to a round hole for a washer.

Also I re-use the code that positions the blocks to drill the holes in the panels. In your example it is hard wired to draw the blocks.


On 19 June 2013 20:39, Jesse Guardiani <[hidden email]> wrote:
Can you do something like this?

module draw_fixing_block() {
  // draw stuff here
}

module draw_inverted_fixing_block() {
  rotate(a=180,v=[blah]) draw_fixing_block();
}

module position_fixing_blocks() {
  // upper left
  translate(v=[blah]) draw_fixing_block();
  // upper right
  translate(v=[blah]) draw_fixing_block();  
}

module position_inverted_fixing_blocks() {
  // lower left
  translate(v=[blah]) draw_inverted_fixing_block();
  // upper right
  translate(v=[blah]) draw_inverted_fixing_block();  
}
 


On Wed, Jun 19, 2013 at 2:04 PM, nop head <[hidden email]> wrote:
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566




_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

Jesse Guardiani
Essentially, child() is doing the job of a callback. Maybe it helps to think of it that way.

On Jun 19, 2013, at 5:14 PM, nop head <[hidden email]> wrote:

The code is many pages but in essence

module fixing_block_positions() {
   translate() rotate() child(); // ten of these
   ...
}

module fixing_block_screw_positions() {
   translate() rotate() child(); // three of these
   ....
}

module fixing_block_assembly() {
   fixing_block();
   fixing_block_screw_positions()
        screw_and_nut_assembly();
}

To drill the holes in the frame

difference() {
    frame_sheets();
    fixing_block_positions()
        fixing_block_screw_positions()
            cylinder(r = screw_clearance_radius, h = 100, center = true);

}

To place the fixing blocks and their fasteners.

fixing_block_positions()
   fixing_block_assembly();


It could obviously be done without child, but I think the code would be less succinct.


On 19 June 2013 21:25, Jesse Guardiani <[hidden email]> wrote:
You've lost me. I'd need to see example code of what you're talking about, I think. The one thing the frickin internet lacks is a decent whiteboard.

I wasn't really trying to suggest you invert anything. I was just trying to provide an alternate way to organize your code so you could get around using child(). I'm not convinced child() is necessary.



On Wed, Jun 19, 2013 at 3:59 PM, nop head <[hidden email]> wrote:
Thinking more about it, that type of composition of three modules would be harder to do in other languages. The top level parent has no knowledge that its child is also a parent child composition. If that was done with function pointers in something like C/C++ then you would need to write a wrapper/functor to allow the top level module to call the second level passing a pointer to the third level with knowing anything about it.


On 19 June 2013 20:49, nop head <[hidden email]> wrote:
Also the child that drills the holes is passed a cylinder to do the drilling, but also it places the fasteners when it is passed a screw assembly as a child.


On 19 June 2013 20:47, nop head <[hidden email]> wrote:
I didn't want to invert the block, just one screw and change the nut trap to a round hole for a washer.

Also I re-use the code that positions the blocks to drill the holes in the panels. In your example it is hard wired to draw the blocks.


On 19 June 2013 20:39, Jesse Guardiani <[hidden email]> wrote:
Can you do something like this?

module draw_fixing_block() {
  // draw stuff here
}

module draw_inverted_fixing_block() {
  rotate(a=180,v=[blah]) draw_fixing_block();
}

module position_fixing_blocks() {
  // upper left
  translate(v=[blah]) draw_fixing_block();
  // upper right
  translate(v=[blah]) draw_fixing_block();  
}

module position_inverted_fixing_blocks() {
  // lower left
  translate(v=[blah]) draw_inverted_fixing_block();
  // upper right
  translate(v=[blah]) draw_inverted_fixing_block();  
}
 


On Wed, Jun 19, 2013 at 2:04 PM, nop head <[hidden email]> wrote:
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566




_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Passing parameters to children.

nophead
Yes but you can't do the double composition with simple callbacks and you can't call functions with unknown numbers of parameters and types both of which happen here.

 fixing_block_positions()
        fixing_block_screw_positions()
            cylinder(r = screw_clearance_radius, h = 100, center = true);

In C it would need some horrible var args stuff and in C++ you would need to use functors to stash the arguments to be passed. Or you need to wrap cylinder in a drill function that has no args and then wrap fixing_block_screw_positions() in a function that calls it with drill as a parameter and then pass that to flixing_block_positions(). All a lot more messy than openscad.



On 19 June 2013 22:25, Jesse Guardiani <[hidden email]> wrote:
Essentially, child() is doing the job of a callback. Maybe it helps to think of it that way.

On Jun 19, 2013, at 5:14 PM, nop head <[hidden email]> wrote:

The code is many pages but in essence

module fixing_block_positions() {
   translate() rotate() child(); // ten of these
   ...
}

module fixing_block_screw_positions() {
   translate() rotate() child(); // three of these
   ....
}

module fixing_block_assembly() {
   fixing_block();
   fixing_block_screw_positions()
        screw_and_nut_assembly();
}

To drill the holes in the frame

difference() {
    frame_sheets();
    fixing_block_positions()
        fixing_block_screw_positions()
            cylinder(r = screw_clearance_radius, h = 100, center = true);

}

To place the fixing blocks and their fasteners.

fixing_block_positions()
   fixing_block_assembly();


It could obviously be done without child, but I think the code would be less succinct.


On 19 June 2013 21:25, Jesse Guardiani <[hidden email]> wrote:
You've lost me. I'd need to see example code of what you're talking about, I think. The one thing the frickin internet lacks is a decent whiteboard.

I wasn't really trying to suggest you invert anything. I was just trying to provide an alternate way to organize your code so you could get around using child(). I'm not convinced child() is necessary.



On Wed, Jun 19, 2013 at 3:59 PM, nop head <[hidden email]> wrote:
Thinking more about it, that type of composition of three modules would be harder to do in other languages. The top level parent has no knowledge that its child is also a parent child composition. If that was done with function pointers in something like C/C++ then you would need to write a wrapper/functor to allow the top level module to call the second level passing a pointer to the third level with knowing anything about it.


On 19 June 2013 20:49, nop head <[hidden email]> wrote:
Also the child that drills the holes is passed a cylinder to do the drilling, but also it places the fasteners when it is passed a screw assembly as a child.


On 19 June 2013 20:47, nop head <[hidden email]> wrote:
I didn't want to invert the block, just one screw and change the nut trap to a round hole for a washer.

Also I re-use the code that positions the blocks to drill the holes in the panels. In your example it is hard wired to draw the blocks.


On 19 June 2013 20:39, Jesse Guardiani <[hidden email]> wrote:
Can you do something like this?

module draw_fixing_block() {
  // draw stuff here
}

module draw_inverted_fixing_block() {
  rotate(a=180,v=[blah]) draw_fixing_block();
}

module position_fixing_blocks() {
  // upper left
  translate(v=[blah]) draw_fixing_block();
  // upper right
  translate(v=[blah]) draw_fixing_block();  
}

module position_inverted_fixing_blocks() {
  // lower left
  translate(v=[blah]) draw_inverted_fixing_block();
  // upper right
  translate(v=[blah]) draw_inverted_fixing_block();  
}
 


On Wed, Jun 19, 2013 at 2:04 PM, nop head <[hidden email]> wrote:
I have a module that places a set of fixing blocks around a frame. To make all the holes on the frame I give it a child that drills holes. To place the actual blocks I give it a child that draws a block and its fasteners. 

Then I decided I want two of the blocks to have the screw going through in the opposite direction so it can fasten something the other side of the panel. In the parent which knows which two blocks are different I need to communicate that fact to the child, but I am not calling the child module directly. I simply call the child() build in module and place the invocation of the child module below the invocation of the parent in the tree. Using a dynamic scoped variable seems to be the only way to do it currently.

If you look at the simple example I posted earlier echo() is the child that takes a parameter but the parent module test() has no way to pass the argument to echo() because it is calling it through child(). The child could have been something like:

union() {
 translate([10, 0, 0]) sphere($i);
 cube($i);
}
  
so the modules getting the parameter are not even the direct descendent, so adding extra parameters to child() does not work unless there is only a single top level child module to receive them. That could be achieved by creating a wrapper.E.g.

module mychild(x) union() {
 translate([10, 0, 0]) sphere(x);
 cube(x);
}

  




On 19 June 2013 18:43, Jesse Guardiani <[hidden email]> wrote:
I'm having trouble visualizing why it would be necessary. Can't you work around it some other way and pass arguments?

(Also, I realize this is the opposite of helpful. If you don't have time to enlighten me, my feelings won't be hurt.)


On Wed, Jun 19, 2013 at 1:26 PM, nop head <[hidden email]> wrote:
No it's something I find I need to do in my design.


On 19 June 2013 18:24, Jesse Guardiani <[hidden email]> wrote:
Is this unit testing you're trying to accomplish? I've been ruminating on how to write unit tests in OpenSCAD for a few days.


On Wed, Jun 19, 2013 at 12:16 PM, nop head <[hidden email]> wrote:
It is a hack but it isn't global, just the scope of the assign and anything below it.

I can't pass an arg to the child() function because that is the index of the child, not things to be passed to the child.


On 19 June 2013 16:51, Jesse Guardiani <[hidden email]> wrote:
ugh. my highly trained programming senses are cringing at the globals I'm seeing here. why can't you pass it as a module argument?


On Wed, Jun 19, 2013 at 11:21 AM, nop head <[hidden email]> wrote:
I found I can do this:

module test() {

     assign($i = 1)

        child();


    assign($i = 2)

        child();

}


test()

    echo($i);


and get


ECHO: 1

ECHO: 2


Is this something I can rely on to work?







_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566




_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
12