discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

How to apply changes to all shapes generated in a for loop?

J
JimT
Sat, Mar 24, 2018 2:42 PM

I have a long complicated piece of OpenSCAD code that I am trying to
simplify. The code generates shapes in a for loop, and modifies them. I
tried to use children(), but OpenSCAD unions the shapes generated in for
loops, so that didn't work. Here is a simplified example that doesn't work:

module translate_children() {
for (i=[0:$children-1])
translate([i*15, 0, 0])
children(i);
}

translate_children() {
for (j=[0:8])
cube([10, 10, 5]);
}

I could also simplify the code if OpenSCAD allowed me to pass a reference to a
function, but that isn't supported. Does anyone have any suggestions?

I have a long complicated piece of OpenSCAD code that I am trying to simplify. The code generates shapes in a for loop, and modifies them. I tried to use children(), but OpenSCAD unions the shapes generated in for loops, so that didn't work. Here is a simplified example that doesn't work: module translate_children() { for (i=[0:$children-1]) translate([i*15, 0, 0]) children(i); } translate_children() { for (j=[0:8]) cube([10, 10, 5]); } I could also simplify the code if OpenSCAD allowed me to pass a reference to a function, but that isn't supported. Does anyone have any suggestions?
N
NateTG
Sat, Mar 24, 2018 3:34 PM

Can you  replace cube with mycube() so that you have

module mycube(j,x,y,z) {
foo(j) {
cube([x,y,z]);
}
}

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

Can you replace cube with mycube() so that you have module mycube(j,x,y,z) { foo(j) { cube([x,y,z]); } } -- Sent from: http://forum.openscad.org/
J
jamcultur
Mon, Mar 26, 2018 11:31 PM

NateTG wrote

Can you  replace cube with mycube() so that you have

module mycube(j,x,y,z) {
foo(j) {
cube([x,y,z]);
}
}

What is foo()? When I code it as you have shown it, I get no output.

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

NateTG wrote > Can you replace cube with mycube() so that you have > > module mycube(j,x,y,z) { > foo(j) { > cube([x,y,z]); > } > } What is foo()? When I code it as you have shown it, I get no output. -- Sent from: http://forum.openscad.org/
N
NateTG
Tue, Mar 27, 2018 12:14 AM

I mean putting the function into the loop, rather than the loop output into
the function:

for (j=[0:8])
mycube(j,[10, 10, 5]);

module mycube(i,d) {
translate([i*15,0,0])
cube(d);
}

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

I mean putting the function into the loop, rather than the loop output into the function: for (j=[0:8]) mycube(j,[10, 10, 5]); module mycube(i,d) { translate([i*15,0,0]) cube(d); } -- Sent from: http://forum.openscad.org/
J
jamcultur
Tue, Mar 27, 2018 1:46 AM

NateTG wrote

I mean putting the function into the loop, rather than the loop output
into
the function:

for (j=[0:8])
mycube(j,[10, 10, 5]);

module mycube(i,d) {
translate([i*15,0,0])
cube(d);
}

That's the way it works now and is what I'm hoping to change.

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

NateTG wrote > I mean putting the function into the loop, rather than the loop output > into > the function: > > > for (j=[0:8]) > mycube(j,[10, 10, 5]); > > module mycube(i,d) { > translate([i*15,0,0]) > cube(d); > } That's the way it works now and is what I'm hoping to change. -- Sent from: http://forum.openscad.org/
T
Troberg
Tue, Mar 27, 2018 6:58 AM

What is foo()? When I code it as you have shown it, I get no output.

foo() is a generic placeholder used in examples for "do something here". In
this case, it would do whatever your extra parameter would need to be done.

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

> What is foo()? When I code it as you have shown it, I get no output. foo() is a generic placeholder used in examples for "do something here". In this case, it would do whatever your extra parameter would need to be done. -- Sent from: http://forum.openscad.org/