discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Copying objects?

T
Troberg
Tue, Nov 21, 2017 7:56 AM

I've made a useful helper function that places a number of wood screws at a
certain distance (I show declaration only, just to show what I'm doing):

module screwseries(screwlength,screweddistance,spacing,center)

It puts a line of screwlength long screws, that is screweddistance long,
with spacing distance between each screw. If center=true, I center it along
the center point od the screwdistance.

Fairly straightforward.

Then, I noticed that I need to do the same with other objects. Metal screws,
nuts, washers, threaded bars and so on. Suddently, I need to duplicate code.

So, I thought, wouldn't it be nice if I could just make a module i call like
this:

objectseries(totallength,spacing,center){
bolt(80,10);
}

It would take the supplied object(s) and make copies of it, distributing
them evenly along that line. No longer would it be restricted to a single
type of object, and I can make neat code by separating placement from object
design.

Possible?

--
Sent from: http://forum.openscad.org/

I've made a useful helper function that places a number of wood screws at a certain distance (I show declaration only, just to show what I'm doing): module screwseries(screwlength,screweddistance,spacing,center) It puts a line of screwlength long screws, that is screweddistance long, with spacing distance between each screw. If center=true, I center it along the center point od the screwdistance. Fairly straightforward. Then, I noticed that I need to do the same with other objects. Metal screws, nuts, washers, threaded bars and so on. Suddently, I need to duplicate code. So, I thought, wouldn't it be nice if I could just make a module i call like this: objectseries(totallength,spacing,center){ bolt(80,10); } It would take the supplied object(s) and make copies of it, distributing them evenly along that line. No longer would it be restricted to a single type of object, and I can make neat code by separating placement from object design. Possible? -- Sent from: http://forum.openscad.org/
NH
nop head
Tue, Nov 21, 2017 8:15 AM

Yes you can do that using children().

objectseries(totallength,spacing,center) {
children();
}

objectseries(100, 20, 0)
bolt(80, 10);

On 21 November 2017 at 07:56, Troberg troberg.anders@gmail.com wrote:

I've made a useful helper function that places a number of wood screws at a
certain distance (I show declaration only, just to show what I'm doing):

module screwseries(screwlength,screweddistance,spacing,center)

It puts a line of screwlength long screws, that is screweddistance long,
with spacing distance between each screw. If center=true, I center it along
the center point od the screwdistance.

Fairly straightforward.

Then, I noticed that I need to do the same with other objects. Metal
screws,
nuts, washers, threaded bars and so on. Suddently, I need to duplicate
code.

So, I thought, wouldn't it be nice if I could just make a module i call
like
this:

objectseries(totallength,spacing,center){
bolt(80,10);
}

It would take the supplied object(s) and make copies of it, distributing
them evenly along that line. No longer would it be restricted to a single
type of object, and I can make neat code by separating placement from
object
design.

Possible?

--
Sent from: http://forum.openscad.org/


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

Yes you can do that using children(). objectseries(totallength,spacing,center) { children(); } objectseries(100, 20, 0) bolt(80, 10); On 21 November 2017 at 07:56, Troberg <troberg.anders@gmail.com> wrote: > I've made a useful helper function that places a number of wood screws at a > certain distance (I show declaration only, just to show what I'm doing): > > module screwseries(screwlength,screweddistance,spacing,center) > > It puts a line of screwlength long screws, that is screweddistance long, > with spacing distance between each screw. If center=true, I center it along > the center point od the screwdistance. > > Fairly straightforward. > > Then, I noticed that I need to do the same with other objects. Metal > screws, > nuts, washers, threaded bars and so on. Suddently, I need to duplicate > code. > > So, I thought, wouldn't it be nice if I could just make a module i call > like > this: > > objectseries(totallength,spacing,center){ > bolt(80,10); > } > > It would take the supplied object(s) and make copies of it, distributing > them evenly along that line. No longer would it be restricted to a single > type of object, and I can make neat code by separating placement from > object > design. > > Possible? > > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
T
Troberg
Tue, Nov 21, 2017 8:50 AM

I'm sorry, but I just don't understand that code.

When I call objectseries, I don't know how many objects I need, as it
depends on totallength and spacing. So, I can't create them before I call
it.

So, the plan was to create one object, then have objectseries create a line
of copies of that object.

So, send it one screw, and it'll create as many screws as needed to make the
line.

As far as i understand your code, I'll have to create all the objects before
I call, then objectseries spaces them out.

My current code (I'll post the simpler version that does less calculations,
for clarity, and since the more advanced version calls this one. Also, sorry
for the Swedish. Also also, I don't actually use Allen screws, they are
crap, I was just too lazy to model Torx, and it's close enough to look
right...):

module skruvrad(length,distance,count,center,offset){
for(n=[0:count-1]){
pos=center?n*distance-(count-1)distance/2:ndistance;
translate([pos,0,0])
traskruv(length,offset);
}
}

module traskruv(length,offset){
color([0.5,0.5,0.5])
translate([0,0,(offset==true)?length+10:0])
difference(){
union(){
translate([0,0,-3.9])
cylinder(d1=3,d2=8,4);
translate([0,0,-length+10])
cylinder(d=3,length-14);
translate([0,0,-length])
cylinder(d1=1,d2=3,10);
}
translate([0,0,-1.9])
cylinder(d=4,3,$fn=6);
}
}

skruvrad(50,50,9,true,false);

//reference plane
color([0,0.4,0],0.5)
translate([-250,-100,-1])
cube([500,200,1]);

As you can see, if I want to draw something else, I'll have to more or less
copy all of skruvrad (and it's companion module skruvlangd), where it would
be easier to just have an:

objectrow(50,9,true){
myamazingthingamajig();
}

or, for the screws:

objectrow(50,9,true){
traskruv(50,false);
}

--
Sent from: http://forum.openscad.org/

I'm sorry, but I just don't understand that code. When I call objectseries, I don't know how many objects I need, as it depends on totallength and spacing. So, I can't create them before I call it. So, the plan was to create one object, then have objectseries create a line of copies of that object. So, send it one screw, and it'll create as many screws as needed to make the line. As far as i understand your code, I'll have to create all the objects before I call, then objectseries spaces them out. My current code (I'll post the simpler version that does less calculations, for clarity, and since the more advanced version calls this one. Also, sorry for the Swedish. Also also, I don't actually use Allen screws, they are crap, I was just too lazy to model Torx, and it's close enough to look right...): module skruvrad(length,distance,count,center,offset){ for(n=[0:count-1]){ pos=center?n*distance-(count-1)*distance/2:n*distance; translate([pos,0,0]) traskruv(length,offset); } } module traskruv(length,offset){ color([0.5,0.5,0.5]) translate([0,0,(offset==true)?length+10:0]) difference(){ union(){ translate([0,0,-3.9]) cylinder(d1=3,d2=8,4); translate([0,0,-length+10]) cylinder(d=3,length-14); translate([0,0,-length]) cylinder(d1=1,d2=3,10); } translate([0,0,-1.9]) cylinder(d=4,3,$fn=6); } } skruvrad(50,50,9,true,false); //reference plane color([0,0.4,0],0.5) translate([-250,-100,-1]) cube([500,200,1]); As you can see, if I want to draw something else, I'll have to more or less copy all of skruvrad (and it's companion module skruvlangd), where it would be easier to just have an: objectrow(50,9,true){ myamazingthingamajig(); } or, for the screws: objectrow(50,9,true){ traskruv(50,false); } -- Sent from: http://forum.openscad.org/
NH
nop head
Tue, Nov 21, 2017 9:35 AM

module skruvrad(distance,count,center){
for(n=[0:count-1]){
pos=center?n*distance-(count-1)distance/2:ndistance;
translate([pos,0,0])
children();
}
}

module traskruv(length,offset){
color([0.5,0.5,0.5])
translate([0,0,(offset==true)?length+10:0])
difference(){
union(){
translate([0,0,-3.9])
cylinder(d1=3,d2=8,4);
translate([0,0,-length+10])
cylinder(d=3,length-14);
translate([0,0,-length])
cylinder(d1=1,d2=3,10);
}
translate([0,0,-1.9])
cylinder(d=4,3,$fn=6);
}
}

skruvrad(50,9,true)
traskruv(50,false);

//reference plane
color([0,0.4,0],0.5)
translate([-250,-100,-1])
cube([500,200,1]);

On 21 November 2017 at 08:50, Troberg troberg.anders@gmail.com wrote:

I'm sorry, but I just don't understand that code.

When I call objectseries, I don't know how many objects I need, as it
depends on totallength and spacing. So, I can't create them before I call
it.

So, the plan was to create one object, then have objectseries create a line
of copies of that object.

So, send it one screw, and it'll create as many screws as needed to make
the
line.

As far as i understand your code, I'll have to create all the objects
before
I call, then objectseries spaces them out.

My current code (I'll post the simpler version that does less calculations,
for clarity, and since the more advanced version calls this one. Also,
sorry
for the Swedish. Also also, I don't actually use Allen screws, they are
crap, I was just too lazy to model Torx, and it's close enough to look
right...):

module skruvrad(length,distance,count,center,offset){
for(n=[0:count-1]){
pos=center?n*distance-(count-1)distance/2:ndistance;
translate([pos,0,0])
traskruv(length,offset);
}
}

module traskruv(length,offset){
color([0.5,0.5,0.5])
translate([0,0,(offset==true)?length+10:0])
difference(){
union(){
translate([0,0,-3.9])
cylinder(d1=3,d2=8,4);
translate([0,0,-length+10])
cylinder(d=3,length-14);
translate([0,0,-length])
cylinder(d1=1,d2=3,10);
}
translate([0,0,-1.9])
cylinder(d=4,3,$fn=6);
}
}

skruvrad(50,50,9,true,false);

//reference plane
color([0,0.4,0],0.5)
translate([-250,-100,-1])
cube([500,200,1]);

As you can see, if I want to draw something else, I'll have to more or less
copy all of skruvrad (and it's companion module skruvlangd), where it would
be easier to just have an:

objectrow(50,9,true){
myamazingthingamajig();
}

or, for the screws:

objectrow(50,9,true){
traskruv(50,false);
}

--
Sent from: http://forum.openscad.org/


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

module skruvrad(distance,count,center){ for(n=[0:count-1]){ pos=center?n*distance-(count-1)*distance/2:n*distance; translate([pos,0,0]) children(); } } module traskruv(length,offset){ color([0.5,0.5,0.5]) translate([0,0,(offset==true)?length+10:0]) difference(){ union(){ translate([0,0,-3.9]) cylinder(d1=3,d2=8,4); translate([0,0,-length+10]) cylinder(d=3,length-14); translate([0,0,-length]) cylinder(d1=1,d2=3,10); } translate([0,0,-1.9]) cylinder(d=4,3,$fn=6); } } skruvrad(50,9,true) traskruv(50,false); //reference plane color([0,0.4,0],0.5) translate([-250,-100,-1]) cube([500,200,1]); On 21 November 2017 at 08:50, Troberg <troberg.anders@gmail.com> wrote: > I'm sorry, but I just don't understand that code. > > When I call objectseries, I don't know how many objects I need, as it > depends on totallength and spacing. So, I can't create them before I call > it. > > So, the plan was to create one object, then have objectseries create a line > of copies of that object. > > So, send it one screw, and it'll create as many screws as needed to make > the > line. > > As far as i understand your code, I'll have to create all the objects > before > I call, then objectseries spaces them out. > > My current code (I'll post the simpler version that does less calculations, > for clarity, and since the more advanced version calls this one. Also, > sorry > for the Swedish. Also also, I don't actually use Allen screws, they are > crap, I was just too lazy to model Torx, and it's close enough to look > right...): > > module skruvrad(length,distance,count,center,offset){ > for(n=[0:count-1]){ > pos=center?n*distance-(count-1)*distance/2:n*distance; > translate([pos,0,0]) > traskruv(length,offset); > } > } > > module traskruv(length,offset){ > color([0.5,0.5,0.5]) > translate([0,0,(offset==true)?length+10:0]) > difference(){ > union(){ > translate([0,0,-3.9]) > cylinder(d1=3,d2=8,4); > translate([0,0,-length+10]) > cylinder(d=3,length-14); > translate([0,0,-length]) > cylinder(d1=1,d2=3,10); > } > translate([0,0,-1.9]) > cylinder(d=4,3,$fn=6); > } > } > > skruvrad(50,50,9,true,false); > > //reference plane > color([0,0.4,0],0.5) > translate([-250,-100,-1]) > cube([500,200,1]); > > As you can see, if I want to draw something else, I'll have to more or less > copy all of skruvrad (and it's companion module skruvlangd), where it would > be easier to just have an: > > objectrow(50,9,true){ > myamazingthingamajig(); > } > > or, for the screws: > > objectrow(50,9,true){ > traskruv(50,false); > } > > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
T
Troberg
Tue, Nov 21, 2017 10:05 AM

Amazing!

However, I still don't understand where the copy happens? Why doesn't it
just move the object with each iteration, instead of creating copies?

(As you may notice, I'm more used to traditional programming languages,
where this code would just have moved the object, and you need to explicitly
make a copy...).

--
Sent from: http://forum.openscad.org/

Amazing! However, I still don't understand where the copy happens? Why doesn't it just move the object with each iteration, instead of creating copies? (As you may notice, I'm more used to traditional programming languages, where this code would just have moved the object, and you need to explicitly make a copy...). -- Sent from: http://forum.openscad.org/
NH
nop head
Tue, Nov 21, 2017 10:40 AM

It builds a big tree of objects and then renders that. If you add a
render() statement to your screw it will calculate the geometry once and
cache it. It will be slower the first time and each time you modify your
screw model but faster to draw the rest of the time.

I render all my vitamins so there will only be one copy of each size of
screw used, etc. In a big model like a 3D printer that has hundreds of
fasteners that is the only way to get reasonable performance.

On 21 November 2017 at 10:05, Troberg troberg.anders@gmail.com wrote:

Amazing!

However, I still don't understand where the copy happens? Why doesn't it
just move the object with each iteration, instead of creating copies?

(As you may notice, I'm more used to traditional programming languages,
where this code would just have moved the object, and you need to
explicitly
make a copy...).

--
Sent from: http://forum.openscad.org/


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

It builds a big tree of objects and then renders that. If you add a render() statement to your screw it will calculate the geometry once and cache it. It will be slower the first time and each time you modify your screw model but faster to draw the rest of the time. I render all my vitamins so there will only be one copy of each size of screw used, etc. In a big model like a 3D printer that has hundreds of fasteners that is the only way to get reasonable performance. On 21 November 2017 at 10:05, Troberg <troberg.anders@gmail.com> wrote: > Amazing! > > However, I still don't understand where the copy happens? Why doesn't it > just move the object with each iteration, instead of creating copies? > > (As you may notice, I'm more used to traditional programming languages, > where this code would just have moved the object, and you need to > explicitly > make a copy...). > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >