discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

simple loop question

F
fred_dot_u
Sun, Dec 20, 2015 3:24 PM

I did a search of the forum for "multiple operations in for loop" and came up
empty. Just picking the correct phrase is a challenge, I've found.

It's been decades since I've had any real exposure to coding. Even though
this portion of code works, I feel it's inelegant.

Is there a simple, cleaner way to generate translations in opposite
directions along one axis? This particular project will expand incrementally
upward as well as along the x and y axes, generating a hexagonal conic shape
of sorts

I'm going to make a guess that vector loops (?) would be a better choice,
but I understand that even less than this simple stuff.

thanks

fred

--
View this message in context: http://forum.openscad.org/simple-loop-question-tp15245.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

I did a search of the forum for "multiple operations in for loop" and came up empty. Just picking the correct phrase is a challenge, I've found. It's been decades since I've had any real exposure to coding. Even though this portion of code works, I feel it's inelegant. Is there a simple, cleaner way to generate translations in opposite directions along one axis? This particular project will expand incrementally upward as well as along the x and y axes, generating a hexagonal conic shape of sorts I'm going to make a guess that vector loops (?) would be a better choice, but I understand that even less than this simple stuff. thanks fred -- View this message in context: http://forum.openscad.org/simple-loop-question-tp15245.html Sent from the OpenSCAD mailing list archive at Nabble.com.
TP
Torsten Paul
Sun, Dec 20, 2015 3:34 PM

On 12/20/2015 04:24 PM, fred_dot_u wrote:

Is there a simple, cleaner way to generate translations in opposite
directions along one axis? This particular project will expand incrementally
upward as well as along the x and y axes, generating a hexagonal conic shape
of sorts

One option would be to use a module that generates both the x and -x
child module:

$fn=120;
high=1;
dia=1.25;

module mirrored_translate(x) {
translate([x, 0, 0]) children();
if (x > 0.001) translate([-x, 0, 0]) children();
}

for (i=[0:1:5])
mirrored_translate(i*1.25)
cylinder(high,d=dia);

Note that your code generated 2 identical overlapping cylinders at
the center which can cause issues later on.

ciao,
Torsten.

On 12/20/2015 04:24 PM, fred_dot_u wrote: > Is there a simple, cleaner way to generate translations in opposite > directions along one axis? This particular project will expand incrementally > upward as well as along the x and y axes, generating a hexagonal conic shape > of sorts > One option would be to use a module that generates both the x and -x child module: $fn=120; high=1; dia=1.25; module mirrored_translate(x) { translate([x, 0, 0]) children(); if (x > 0.001) translate([-x, 0, 0]) children(); } for (i=[0:1:5]) mirrored_translate(i*1.25) cylinder(high,d=dia); Note that your code generated 2 identical overlapping cylinders at the center which can cause issues later on. ciao, Torsten.
JD
Jerry Davis
Sun, Dec 20, 2015 5:11 PM

On Sun, Dec 20, 2015 at 8:34 AM, Torsten Paul Torsten.Paul@gmx.de wrote:

$fn=120;
high=1;
dia=1.25;

module mirrored_translate(x) {
translate([x, 0, 0]) children();
if (x > 0.001) translate([-x, 0, 0]) children();
}

for (i=[0:1:5])
mirrored_translate(i*1.25)
cylinder(high,d=dia);

or simply, without using the module, (although that is coolness), just put
braces around what you want in your for loop (to form a block), as in:

for (i=[0:1:5]) {
translate([i*(1.25), 0, 0])
cylinder(high,d=dia);
translate([-(i*1.25), 0, 0])
cylinder(high,d=dia);
}

--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer

The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

I
*f you give someone a program, you will frustrate them for a day; if you
teach them how to program, you will frustrate them for a lifetime. *-
Anonymous

If writing good code requires very little comments, then writing really
excellent code requires no comments at all!
- Ken Thompson

On Sun, Dec 20, 2015 at 8:34 AM, Torsten Paul <Torsten.Paul@gmx.de> wrote: > $fn=120; > high=1; > dia=1.25; > > module mirrored_translate(x) { > translate([x, 0, 0]) children(); > if (x > 0.001) translate([-x, 0, 0]) children(); > } > > for (i=[0:1:5]) > mirrored_translate(i*1.25) > cylinder(high,d=dia); > > or simply, without using the module, (although that is coolness), just put braces around what you want in your for loop (to form a block), as in: for (i=[0:1:5]) { translate([i*(1.25), 0, 0]) cylinder(high,d=dia); translate([-(i*1.25), 0, 0]) cylinder(high,d=dia); } -- Extra Ham Operator: K7AZJ Registered Linux User: 275424 Raspberry Pi and Arduino developer *The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".*- Isaac. Asimov *I* *f you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. *- Anonymous *If writing good code requires very little comments, then writing really excellent code requires no comments at all!*- Ken Thompson
F
fred_dot_u
Sun, Dec 20, 2015 5:32 PM

I think the braces might be the answer I seek, although I agree that the
module method is elegant. The braces method does not remove the duplicate
central cylinder, although this is not a print project, it could become one,
and the duplicate should be avoided.

I've run into a problem with the module method though. I don't understand
clearly the concept of children() in this context. Well, I don't understand
the concept within programming in general, so within OpenSCAD it gets more
challenging.

I did a search of the wiki page and did not get clarification enough to
understand this simple code segment. This is not a good sign.

My next segment will be to create another row of cylinders offset to the +/-
direction, cylinder count minus one and offset the appropriate distance x to
create tangent contact. I'd prefer to understand the code prior to expanding
this effort.

To complicate matters, eventually the central cylinder will be z'd up while
the outermost ring will be z=0 and the intermediate cylinders will be, well,
intermediate height.

I think I'm going about this in an incorrect manner and perhaps should thing
from the center outward, rather than striping the cylinders, causing coding
problems later.

--
View this message in context: http://forum.openscad.org/simple-loop-question-tp15245p15250.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

I think the braces might be the answer I seek, although I agree that the module method is elegant. The braces method does not remove the duplicate central cylinder, although this is not a print project, it could become one, and the duplicate should be avoided. I've run into a problem with the module method though. I don't understand clearly the concept of children() in this context. Well, I don't understand the concept within programming in general, so within OpenSCAD it gets more challenging. I did a search of the wiki page and did not get clarification enough to understand this simple code segment. This is not a good sign. My next segment will be to create another row of cylinders offset to the +/- direction, cylinder count minus one and offset the appropriate distance x to create tangent contact. I'd prefer to understand the code prior to expanding this effort. To complicate matters, eventually the central cylinder will be z'd up while the outermost ring will be z=0 and the intermediate cylinders will be, well, intermediate height. I think I'm going about this in an incorrect manner and perhaps should thing from the center outward, rather than striping the cylinders, causing coding problems later. -- View this message in context: http://forum.openscad.org/simple-loop-question-tp15245p15250.html Sent from the OpenSCAD mailing list archive at Nabble.com.
TP
Torsten Paul
Sun, Dec 20, 2015 8:39 PM

On 12/20/2015 06:32 PM, fred_dot_u wrote:

I've run into a problem with the module method though. I don't understand
clearly the concept of children() in this context. Well, I don't understand
the concept within programming in general, so within OpenSCAD it gets more
challenging.

Try to forget programming for a bit :-). Just think real
objects and lets define recipes how to create those...

So we have a red cube of size 10 - in OpenSCAD that's:

color("red") cube(10, center = true);

Now we introduce modules which are basically just a shortcut
for what's inside:

module red_cube() {
color("red") cube(10, center = true);
}

red_cube();

We simply gave the recipe to produce a red cube a special name
using the module definition which by itself does nothing. But
we do can use the "red_cube()" now which creates the object we
defined in that recipe.

Then there's modules that can affect other things, for example
translate() which moves it's child objects.

translate([20, 20, 0]) red_cube();

Simply moving that into a module, for example with a single
parameter for the x part of the translation gives a recipe
for a red_cube() that can be moved in x direction using the
x parameter for the recipe (so we have to give that parameter
when using the recipe):

module my_translate(x) {
translate([x, 20, 0]) red_cube();
}

my_translate(x = 20);

Now using the special children() module we can substitute the
object that is translated and leave that for later, similar to
the built-in translate(). Effectively that children() will be
now replaced with whatever is written after the actual use of
my_translate() - e.g. once the red_cube() and once a "normal"
cube().
So basically we leave the part that describes the translation
in the my_translate() module but don't force it to be applied
only to red_cube(). Now it can be applied to any child object.

module my_translate(x) {
translate([x, 20, 0]) children();
}

my_translate(x = 20) red_cube();
my_translate(x = -20) sphere(5);

Some more examples:
http://files.openscad.org/examples/Basics/children.html
http://files.openscad.org/examples/Basics/children_indexed.html

ciao,
Torsten.

On 12/20/2015 06:32 PM, fred_dot_u wrote: > I've run into a problem with the module method though. I don't understand > clearly the concept of children() in this context. Well, I don't understand > the concept within programming in general, so within OpenSCAD it gets more > challenging. > Try to forget programming for a bit :-). Just think real objects and lets define recipes how to create those... So we have a red cube of size 10 - in OpenSCAD that's: color("red") cube(10, center = true); Now we introduce modules which are basically just a shortcut for what's inside: module red_cube() { color("red") cube(10, center = true); } red_cube(); We simply gave the recipe to produce a red cube a special name using the module definition which by itself does nothing. But we do can use the "red_cube()" now which creates the object we defined in that recipe. Then there's modules that can affect other things, for example translate() which moves it's child objects. translate([20, 20, 0]) red_cube(); Simply moving that into a module, for example with a single parameter for the x part of the translation gives a recipe for a red_cube() that can be moved in x direction using the x parameter for the recipe (so we have to give that parameter when using the recipe): module my_translate(x) { translate([x, 20, 0]) red_cube(); } my_translate(x = 20); Now using the special children() module we can substitute the object that is translated and leave that for later, similar to the built-in translate(). Effectively that children() will be now replaced with whatever is written after the actual use of my_translate() - e.g. once the red_cube() and once a "normal" cube(). So basically we leave the part that describes the translation in the my_translate() module but don't force it to be applied only to red_cube(). Now it can be applied to any child object. module my_translate(x) { translate([x, 20, 0]) children(); } my_translate(x = 20) red_cube(); my_translate(x = -20) sphere(5); Some more examples: http://files.openscad.org/examples/Basics/children.html http://files.openscad.org/examples/Basics/children_indexed.html ciao, Torsten.
F
fred
Mon, Dec 21, 2015 9:46 PM

I've collected the resources you've provided and am going to have to ponder for a while, experiment and attempt to understand clearly the mechanics of the children() feature. Would it be practical to create a short bit of code in "longhand" that represents what the children() feature performs? I'm nearing the end of my 60th year, having started my life in my zero-th year, so the brain cells don't function well later in the day. If the earlier part of the day involves strenuous activity, the brain cells are also kaput for the day.

On Sunday, December 20, 2015 3:40 PM, Torsten Paul <Torsten.Paul@gmx.de> wrote:

On 12/20/2015 06:32 PM, fred_dot_u wrote:

I've run into a problem with the module method though. I don't understand
clearly the concept of children() in this context. Well, I don't understand
the concept within programming in general, so within OpenSCAD it gets more
challenging.

Try to forget programming for a bit :-). Just think real
objects and lets define recipes how to create those...

So we have a red cube of size 10 - in OpenSCAD that's:

  color("red") cube(10, center = true);

Now we introduce modules which are basically just a shortcut
for what's inside:

  module red_cube() {
    color("red") cube(10, center = true);
  }

  red_cube();

We simply gave the recipe to produce a red cube a special name
using the module definition which by itself does nothing. But
we do can use the "red_cube()" now which creates the object we
defined in that recipe.

Then there's modules that can affect other things, for example
translate() which moves it's child objects.

  translate([20, 20, 0]) red_cube();

Simply moving that into a module, for example with a single
parameter for the x part of the translation gives a recipe
for a red_cube() that can be moved in x direction using the
x parameter for the recipe (so we have to give that parameter
when using the recipe):

  module my_translate(x) {
    translate([x, 20, 0]) red_cube();
  }

  my_translate(x = 20);

Now using the special children() module we can substitute the
object that is translated and leave that for later, similar to
the built-in translate(). Effectively that children() will be
now replaced with whatever is written after the actual use of
my_translate() - e.g. once the red_cube() and once a "normal"
cube().
So basically we leave the part that describes the translation
in the my_translate() module but don't force it to be applied
only to red_cube(). Now it can be applied to any child object.

  module my_translate(x) {
    translate([x, 20, 0]) children();
  }

  my_translate(x = 20) red_cube();
  my_translate(x = -20) sphere(5);

Some more examples:
http://files.openscad.org/examples/Basics/children.html
http://files.openscad.org/examples/Basics/children_indexed.html

ciao,
  Torsten.


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

I've collected the resources you've provided and am going to have to ponder for a while, experiment and attempt to understand clearly the mechanics of the children() feature. Would it be practical to create a short bit of code in "longhand" that represents what the children() feature performs? I'm nearing the end of my 60th year, having started my life in my zero-th year, so the brain cells don't function well later in the day. If the earlier part of the day involves strenuous activity, the brain cells are also kaput for the day. On Sunday, December 20, 2015 3:40 PM, Torsten Paul <Torsten.Paul@gmx.de> wrote: On 12/20/2015 06:32 PM, fred_dot_u wrote: > I've run into a problem with the module method though. I don't understand > clearly the concept of children() in this context. Well, I don't understand > the concept within programming in general, so within OpenSCAD it gets more > challenging. > Try to forget programming for a bit :-). Just think real objects and lets define recipes how to create those... So we have a red cube of size 10 - in OpenSCAD that's:   color("red") cube(10, center = true); Now we introduce modules which are basically just a shortcut for what's inside:   module red_cube() {     color("red") cube(10, center = true);   }   red_cube(); We simply gave the recipe to produce a red cube a special name using the module definition which by itself does nothing. But we do can use the "red_cube()" now which creates the object we defined in that recipe. Then there's modules that can affect other things, for example translate() which moves it's child objects.   translate([20, 20, 0]) red_cube(); Simply moving that into a module, for example with a single parameter for the x part of the translation gives a recipe for a red_cube() that can be moved in x direction using the x parameter for the recipe (so we have to give that parameter when using the recipe):   module my_translate(x) {     translate([x, 20, 0]) red_cube();   }   my_translate(x = 20); Now using the special children() module we can substitute the object that is translated and leave that for later, similar to the built-in translate(). Effectively that children() will be now replaced with whatever is written after the actual use of my_translate() - e.g. once the red_cube() and once a "normal" cube(). So basically we leave the part that describes the translation in the my_translate() module but don't force it to be applied only to red_cube(). Now it can be applied to any child object.   module my_translate(x) {     translate([x, 20, 0]) children();   }   my_translate(x = 20) red_cube();   my_translate(x = -20) sphere(5); Some more examples: http://files.openscad.org/examples/Basics/children.html http://files.openscad.org/examples/Basics/children_indexed.html ciao,   Torsten. _______________________________________________ OpenSCAD mailing list Discuss@lists.openscad.org http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
NH
nop head
Tue, Dec 22, 2015 6:02 PM

There isn't a long hand version of children(), you simple can't do what it
does any other way. The children of a module are the modules below it in
the tree. For example the children of translate are the things you place
after it that you want translated. The children() module gets replaced by
all the children of the module it is contained in. For example:

module rotate90() {
rotate([0, 0, 90])
children();
}

rotate90()
cube([1,10,1]);

Becomes: -

rotate([0, 0, 90])
cube([1, 10, 1]);

In this case there is only one child but if you enclose several things in
braces you can have multiple children() and you can use children(0) to
refer to the first one, children(1) the second and so on.

On 21 December 2015 at 21:46, fred fred_dot_u@yahoo.com wrote:

I've collected the resources you've provided and am going to have to
ponder for a while, experiment and attempt to understand clearly the
mechanics of the children() feature. Would it be practical to create a
short bit of code in "longhand" that represents what the children() feature
performs? I'm nearing the end of my 60th year, having started my life in my
zero-th year, so the brain cells don't function well later in the day. If
the earlier part of the day involves strenuous activity, the brain cells
are also kaput for the day.

On Sunday, December 20, 2015 3:40 PM, Torsten Paul Torsten.Paul@gmx.de
wrote:

On 12/20/2015 06:32 PM, fred_dot_u wrote:

I've run into a problem with the module method though. I don't understand
clearly the concept of children() in this context. Well, I don't

understand

the concept within programming in general, so within OpenSCAD it gets

more

challenging.

Try to forget programming for a bit :-). Just think real
objects and lets define recipes how to create those...

So we have a red cube of size 10 - in OpenSCAD that's:

color("red") cube(10, center = true);

Now we introduce modules which are basically just a shortcut
for what's inside:

module red_cube() {
color("red") cube(10, center = true);
}

red_cube();

We simply gave the recipe to produce a red cube a special name
using the module definition which by itself does nothing. But
we do can use the "red_cube()" now which creates the object we
defined in that recipe.

Then there's modules that can affect other things, for example
translate() which moves it's child objects.

translate([20, 20, 0]) red_cube();

Simply moving that into a module, for example with a single
parameter for the x part of the translation gives a recipe
for a red_cube() that can be moved in x direction using the
x parameter for the recipe (so we have to give that parameter
when using the recipe):

module my_translate(x) {
translate([x, 20, 0]) red_cube();
}

my_translate(x = 20);

Now using the special children() module we can substitute the
object that is translated and leave that for later, similar to
the built-in translate(). Effectively that children() will be
now replaced with whatever is written after the actual use of
my_translate() - e.g. once the red_cube() and once a "normal"
cube().
So basically we leave the part that describes the translation
in the my_translate() module but don't force it to be applied
only to red_cube(). Now it can be applied to any child object.

module my_translate(x) {
translate([x, 20, 0]) children();
}

my_translate(x = 20) red_cube();
my_translate(x = -20) sphere(5);

Some more examples:
http://files.openscad.org/examples/Basics/children.html
http://files.openscad.org/examples/Basics/children_indexed.html

ciao,
Torsten.


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


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

There isn't a long hand version of children(), you simple can't do what it does any other way. The children of a module are the modules below it in the tree. For example the children of translate are the things you place after it that you want translated. The children() module gets replaced by all the children of the module it is contained in. For example: module rotate90() { rotate([0, 0, 90]) children(); } rotate90() cube([1,10,1]); Becomes: - rotate([0, 0, 90]) cube([1, 10, 1]); In this case there is only one child but if you enclose several things in braces you can have multiple children() and you can use children(0) to refer to the first one, children(1) the second and so on. On 21 December 2015 at 21:46, fred <fred_dot_u@yahoo.com> wrote: > I've collected the resources you've provided and am going to have to > ponder for a while, experiment and attempt to understand clearly the > mechanics of the children() feature. Would it be practical to create a > short bit of code in "longhand" that represents what the children() feature > performs? I'm nearing the end of my 60th year, having started my life in my > zero-th year, so the brain cells don't function well later in the day. If > the earlier part of the day involves strenuous activity, the brain cells > are also kaput for the day. > > > On Sunday, December 20, 2015 3:40 PM, Torsten Paul <Torsten.Paul@gmx.de> > wrote: > > > On 12/20/2015 06:32 PM, fred_dot_u wrote: > > I've run into a problem with the module method though. I don't understand > > clearly the concept of children() in this context. Well, I don't > understand > > the concept within programming in general, so within OpenSCAD it gets > more > > challenging. > > > Try to forget programming for a bit :-). Just think real > objects and lets define recipes how to create those... > > So we have a red cube of size 10 - in OpenSCAD that's: > > color("red") cube(10, center = true); > > Now we introduce modules which are basically just a shortcut > for what's inside: > > module red_cube() { > color("red") cube(10, center = true); > } > > red_cube(); > > We simply gave the recipe to produce a red cube a special name > using the module definition which by itself does nothing. But > we do can use the "red_cube()" now which creates the object we > defined in that recipe. > > Then there's modules that can affect other things, for example > translate() which moves it's child objects. > > translate([20, 20, 0]) red_cube(); > > Simply moving that into a module, for example with a single > parameter for the x part of the translation gives a recipe > for a red_cube() that can be moved in x direction using the > x parameter for the recipe (so we have to give that parameter > when using the recipe): > > module my_translate(x) { > translate([x, 20, 0]) red_cube(); > } > > my_translate(x = 20); > > Now using the special children() module we can substitute the > object that is translated and leave that for later, similar to > the built-in translate(). Effectively that children() will be > now replaced with whatever is written after the actual use of > my_translate() - e.g. once the red_cube() and once a "normal" > cube(). > So basically we leave the part that describes the translation > in the my_translate() module but don't force it to be applied > only to red_cube(). Now it can be applied to any child object. > > module my_translate(x) { > translate([x, 20, 0]) children(); > } > > my_translate(x = 20) red_cube(); > my_translate(x = -20) sphere(5); > > Some more examples: > http://files.openscad.org/examples/Basics/children.html > http://files.openscad.org/examples/Basics/children_indexed.html > > ciao, > Torsten. > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
F
fred_dot_u
Wed, Dec 23, 2015 1:26 AM

The latest reply has been helpful. I'd like to see an example with more than
one object to the children() aspect, if that's the correct terminology. I
think I have a couple examples provided earlier, so I'll read those over
too.

I'm not sure yet when I'll make use of this feature. I hope when it is
needed, it "jumps out" at me and is recognized as such.

Thank you

--
View this message in context: http://forum.openscad.org/simple-loop-question-tp15245p15274.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

The latest reply has been helpful. I'd like to see an example with more than one object to the children() aspect, if that's the correct terminology. I think I have a couple examples provided earlier, so I'll read those over too. I'm not sure yet when I'll make use of this feature. I hope when it is needed, it "jumps out" at me and is recognized as such. Thank you -- View this message in context: http://forum.openscad.org/simple-loop-question-tp15245p15274.html Sent from the OpenSCAD mailing list archive at Nabble.com.