Stacking module instances

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

Stacking module instances

Felipe Sanches
I need to stack things. For instance, I may need to design the 3d
model of a sequence of nuts and washers installed in a bolt.

Let's say I have a bolt() module, a washer() module and a nut() module.

today I implement this by doing something like:

bolt();
translate([0,0,-wood_thickness]){
   wood_sheet();

  translate([0,0,-washer_thickness]){
    washer();

    translate([0,0,-nut_thickness])
    nut();
  }
}

I'd love to be able to declare something like this:

object nut(){
  module 3dmodel(){
   //here, the SCAD code for the 3d model of a nut
  }

  thickness = 2;
}

object washer(){
  module 3dmodel(){
   //here, the SCAD code for the 3d model of a washer
  }

  thickness = 1.5;
}

object wood_sheet(){
  module 3dmodel(){
   //here, the SCAD code for the 3d model of the wood sheet where I'll
install the nuts and bolts
  }

  thickness = 6;
}

stack(){
   bolt();
   wood_sheet();
   washer();
   nut();
}
_______________________________________________
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: Stacking module instances

donbright
thats imho equivalent to having a function/module that can return the size of a child object

On Wed, May 22, 2013 at 7:37 PM, Felipe Sanches <[hidden email]> wrote:
I need to stack things. For instance, I may need to design the 3d
model of a sequence of nuts and washers installed in a bolt.

Let's say I have a bolt() module, a washer() module and a nut() module.

today I implement this by doing something like:

bolt();
translate([0,0,-wood_thickness]){
   wood_sheet();

  translate([0,0,-washer_thickness]){
    washer();

    translate([0,0,-nut_thickness])
    nut();
  }
}

I'd love to be able to declare something like this:

object nut(){
  module 3dmodel(){
   //here, the SCAD code for the 3d model of a nut
  }

  thickness = 2;
}

object washer(){
  module 3dmodel(){
   //here, the SCAD code for the 3d model of a washer
  }

  thickness = 1.5;
}

object wood_sheet(){
  module 3dmodel(){
   //here, the SCAD code for the 3d model of the wood sheet where I'll
install the nuts and bolts
  }

  thickness = 6;
}

stack(){
   bolt();
   wood_sheet();
   washer();
   nut();
}
_______________________________________________
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: Stacking module instances

Felipe Sanches
yes, boundingbox support would be great, indeed!

On Wed, May 22, 2013 at 9:38 PM, Don Bright <[hidden email]> wrote:

> thats imho equivalent to having a function/module that can return the size
> of a child object
>
> On Wed, May 22, 2013 at 7:37 PM, Felipe Sanches <[hidden email]>
> wrote:
>>
>> I need to stack things. For instance, I may need to design the 3d
>> model of a sequence of nuts and washers installed in a bolt.
>>
>> Let's say I have a bolt() module, a washer() module and a nut() module.
>>
>> today I implement this by doing something like:
>>
>> bolt();
>> translate([0,0,-wood_thickness]){
>>    wood_sheet();
>>
>>   translate([0,0,-washer_thickness]){
>>     washer();
>>
>>     translate([0,0,-nut_thickness])
>>     nut();
>>   }
>> }
>>
>> I'd love to be able to declare something like this:
>>
>> object nut(){
>>   module 3dmodel(){
>>    //here, the SCAD code for the 3d model of a nut
>>   }
>>
>>   thickness = 2;
>> }
>>
>> object washer(){
>>   module 3dmodel(){
>>    //here, the SCAD code for the 3d model of a washer
>>   }
>>
>>   thickness = 1.5;
>> }
>>
>> object wood_sheet(){
>>   module 3dmodel(){
>>    //here, the SCAD code for the 3d model of the wood sheet where I'll
>> install the nuts and bolts
>>   }
>>
>>   thickness = 6;
>> }
>>
>> stack(){
>>    bolt();
>>    wood_sheet();
>>    washer();
>>    nut();
>> }
>> _______________________________________________
>> 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: Stacking module instances

Whosawhatsis
In reply to this post by Felipe Sanches
Why not nest them? You could do something like this:

bolt() {
wood_sheet() {
washer() {
nut();
}
}
}

Each module would translate it's child node by its own thickness.

On Wednesday, May 22, 2013 at 5:37 PM, Felipe Sanches wrote:

I need to stack things. For instance, I may need to design the 3d
model of a sequence of nuts and washers installed in a bolt.

Let's say I have a bolt() module, a washer() module and a nut() module.

today I implement this by doing something like:

bolt();
translate([0,0,-wood_thickness]){
wood_sheet();

translate([0,0,-washer_thickness]){
washer();

translate([0,0,-nut_thickness])
nut();
}
}

I'd love to be able to declare something like this:

object nut(){
module 3dmodel(){
//here, the SCAD code for the 3d model of a nut
}

thickness = 2;
}

object washer(){
module 3dmodel(){
//here, the SCAD code for the 3d model of a washer
}

thickness = 1.5;
}

object wood_sheet(){
module 3dmodel(){
//here, the SCAD code for the 3d model of the wood sheet where I'll
install the nuts and bolts
}

thickness = 6;
}

stack(){
bolt();
wood_sheet();
washer();
nut();
}
_______________________________________________
OpenSCAD mailing list


_______________________________________________
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: Stacking module instances

Felipe Sanches
That sounds good. I'll try to use that technique in my designs and see
how it works out, thanks for the insight!

On Wed, May 22, 2013 at 9:44 PM, whosawhatsis <[hidden email]> wrote:

> Why not nest them? You could do something like this:
>
> bolt() {
> wood_sheet() {
> washer() {
> nut();
> }
> }
> }
>
> Each module would translate it's child node by its own thickness.
>
> On Wednesday, May 22, 2013 at 5:37 PM, Felipe Sanches wrote:
>
> I need to stack things. For instance, I may need to design the 3d
> model of a sequence of nuts and washers installed in a bolt.
>
> Let's say I have a bolt() module, a washer() module and a nut() module.
>
> today I implement this by doing something like:
>
> bolt();
> translate([0,0,-wood_thickness]){
> wood_sheet();
>
> translate([0,0,-washer_thickness]){
> washer();
>
> translate([0,0,-nut_thickness])
> nut();
> }
> }
>
> I'd love to be able to declare something like this:
>
> object nut(){
> module 3dmodel(){
> //here, the SCAD code for the 3d model of a nut
> }
>
> thickness = 2;
> }
>
> object washer(){
> module 3dmodel(){
> //here, the SCAD code for the 3d model of a washer
> }
>
> thickness = 1.5;
> }
>
> object wood_sheet(){
> module 3dmodel(){
> //here, the SCAD code for the 3d model of the wood sheet where I'll
> install the nuts and bolts
> }
>
> thickness = 6;
> }
>
> stack(){
> bolt();
> wood_sheet();
> washer();
> nut();
> }
> _______________________________________________
> 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: Stacking module instances

Felipe Sanches
doesn't quite work...
WARNING: Ignoring recursive module instantiation of 'translate'.

module thing(){
  height = 13;
  cylinder(r=6, h=height);

  translate([0, 0, height])
  child(0);
}

module another_thing(){
  height=4;
  cube([8,5,height]);

  translate([0, 0, height])
  child(0);
}

thing()
another_thing()
thing()
another_thing();
_______________________________________________
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: Stacking module instances

Whosawhatsis
Are you using the most recent release? It's my understanding that the rules on recursion were loosened in a way that should make that possible...

On Wednesday, May 22, 2013 at 6:02 PM, Felipe Sanches wrote:

doesn't quite work...
WARNING: Ignoring recursive module instantiation of 'translate'.

module thing(){
height = 13;
cylinder(r=6, h=height);

translate([0, 0, height])
child(0);
}

module another_thing(){
height=4;
cube([8,5,height]);

translate([0, 0, height])
child(0);
}

thing()
another_thing()
thing()
another_thing();
_______________________________________________
OpenSCAD mailing list


_______________________________________________
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: Stacking module instances

Triffid Hunter
yep looks like it works with recent git at least :D

I ran into this one myself not too long ago, nice to see it's fixed now!

On 23 May 2013 11:08, whosawhatsis <[hidden email]> wrote:
Are you using the most recent release? It's my understanding that the rules on recursion were loosened in a way that should make that possible...

On Wednesday, May 22, 2013 at 6:02 PM, Felipe Sanches wrote:

doesn't quite work...
WARNING: Ignoring recursive module instantiation of 'translate'.

module thing(){
height = 13;
cylinder(r=6, h=height);

translate([0, 0, height])
child(0);
}

module another_thing(){
height=4;
cube([8,5,height]);

translate([0, 0, height])
child(0);
}

thing()
another_thing()
thing()
another_thing();
_______________________________________________
OpenSCAD mailing list


_______________________________________________
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

scad_stack.png (39K) Download Attachment
Ivo
Reply | Threaded
Open this post in threaded view
|

Re: Stacking module instances

Ivo
In reply to this post by Felipe Sanches
Felipe Sanches wrote
doesn't quite work...
WARNING: Ignoring recursive module instantiation of 'translate'.

module thing(){
  height = 13;
  cylinder(r=6, h=height);

  translate([0, 0, height])
  child(0);
}

module another_thing(){
  height=4;
  cube([8,5,height]);

  translate([0, 0, height])
  child(0);
}

thing()
another_thing()
thing()
another_thing();
I have been using this technique with the latest snapshots for all sorts of suff (chains and assemblies) and it works great. Better than trying to do clever stuff with the bounding box of the child, that assumes the children are convex. In your example, the screw would pass through the nut so knowing the bounding box of the screw won't really help you.

The modules can be parameterised, so you can have "module nut(diameter, thickness) {}" and have not just the nut (and board and washer) be defined by the parameters but also their relative positions.
Reply | Threaded
Open this post in threaded view
|

Re: Stacking module instances

kitwallace
In reply to this post by Felipe Sanches
Just one more approach which I use for column-like things like http://www.thingiverse.com/thing:47274

function v_sum(v,n,k=0) =

k > (n -1) ? 0 : v[k] + v_sum(v,n,k+1);


module stack (separations) {

union() {

for (i = [0:len(separations)-1]) {

assign(offset = v_sum(separations,i)) {

echo("i",i,"offset",offset);

translate ([0,0,offset])

child(i);

}

}

}

}

stack([2,3,4]) {

cube([1,1,2],center=true);

cube([2,2,3],center=true);

cube([3,3,4],center=true);

}


The vector of separations might be considered redundant but it allows offsets which are different for the object height which I sometimes find useful

Chris Wallace



_______________________________________________
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: Stacking module instances

kintel
Administrator
In reply to this post by Felipe Sanches
This inspired by nophead: You can also use a trick by adding a "translate child" construct at the end of each stackable module:

module nut() {
  cylinder(h=6, r=10, $fn=6);
  translate([0,0,6.2]) child(0);
}

module washer() {
  cylinder(h=2, r=10);
  translate([0,0,2.2]) child(0);
}

module screw() {
  cylinder(r=8, h=6);
  translate([0,0,2.9]) cylinder(r=3, h=20);
  translate([0,0,6.2]) child(0);
}


screw() washer() nut() washer();

 -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: Stacking module instances

nophead
This technique now gives me "WARNING:Child index(0) out of bounds (0 children)" wherever I have a washer with no children.

Do I need to add if($children) child(0);   ?


On 23 May 2013 15:49, Marius Kintel <[hidden email]> wrote:
This inspired by nophead: You can also use a trick by adding a "translate child" construct at the end of each stackable module:

module nut() {
  cylinder(h=6, r=10, $fn=6);
  translate([0,0,6.2]) child(0);
}

module washer() {
  cylinder(h=2, r=10);
  translate([0,0,2.2]) child(0);
}

module screw() {
  cylinder(r=8, h=6);
  translate([0,0,2.9]) cylinder(r=3, h=20);
  translate([0,0,6.2]) child(0);
}


screw() washer() nut() washer();

 -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: Stacking module instances

kintel
Administrator
On 2013-05-29, at 05:24 , nop head wrote:

> This technique now gives me "WARNING:Child index(0) out of bounds (0 children)" wherever I have a washer with no children.
>
> Do I need to add if($children) child(0);   ?
>
It's just a warning, meaning if you know what you're doing, it's ok.
Adding an "if" would make your design intention more explicit.

 -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: Stacking module instances

nophead
Yes that seems a better way to do it.


On 29 May 2013 15:26, Marius Kintel <[hidden email]> wrote:
On 2013-05-29, at 05:24 , nop head wrote:

> This technique now gives me "WARNING:Child index(0) out of bounds (0 children)" wherever I have a washer with no children.
>
> Do I need to add if($children) child(0);   ?
>
It's just a warning, meaning if you know what you're doing, it's ok.
Adding an "if" would make your design intention more explicit.

 -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
Ivo
Reply | Threaded
Open this post in threaded view
|

Re: Stacking module instances

Ivo
In reply to this post by nophead
i define an "empty" module to avoid the warning and make the emptyness explicit.

module empty() {
}

Then you can do :

screw() washer() nut() washer() empty();