Welcome! Log In Create A New Profile

Advanced

how to use an accumulating variable

Posted by appjaws1 
how to use an accumulating variable
January 20, 2016 08:16AM
Hi,
I have the following lines as an example.

translate([0,0,0]) cylinder(d=holdupdia, h=holdupht);
acc=acc+holdht;
translate([0,0,acc]) cylinder(d=holdshaftdia, h=holdshaftupperht);
acc=acc+holdshaftupperht;
translate([0,0,acc]) cylinder(d=holdmiddia, h=holdmidht);
acc=acc+holdmidht;
translate([0,0,acc]) cylinder(d=holdshaftdia, h=holdshaftmidht);

acc just shows the last value assigned to it, I need it to accumulate all of the values in the section

Any ideas on how to achieve this?

Edited 1 time(s). Last edit at 01/20/2016 09:23AM by appjaws1.

appjaws - Ormerod 1, core XY
Duet wifi, duex5
firmware 1.18
Web Interface 1.15a
OpenSCAD 2015.03
slic3r-1.33.8-prusa3d and/or Simplify3D 3.1.1
Re: how to use an accumulating variable
January 21, 2016 12:58PM
OpenSCAD is a functional language, so declarative constructs like acc = acc + holdht don't do what you think they do.

For the example shown, you can substitute the actual calculations directly into the translate() function:

translate([0,0,0]) cylinder(d=holdupdia, h=holdupht); 
//I'm assuming acc = 0 at this point
//acc=acc+holdht;
translate([0,0,holdht]) cylinder(d=holdshaftdia, h=holdshaftupperht);
//acc=acc+holdshaftupperht;
translate([0,0,holdht+holdshaftupperht]) cylinder(d=holdmiddia, h=holdmidht); 
//acc=acc+holdmidht;
translate([0,0,holdht+holdshaftupperht+holdmidht]) cylinder(d=holdshaftdia, h=holdshaftmidht);

Without knowing what else you're trying to do I'm not sure if this answers your question.
Re: how to use an accumulating variable
January 21, 2016 02:03PM
Thank you, nothing is easy is it.

All I want to do is increase "Z" by the height of the previous item, so then I could have a routine that just took the heights as parameters. I wanted to parameterise this so that I could use it for different item heights and diameters in the future

Of course the added problem is the different diameters of the cylinders.

I wonder if I should use an array but I think I would have to calculate all values of Z, so I may as well enter them in the script.


appjaws - Ormerod 1, core XY
Duet wifi, duex5
firmware 1.18
Web Interface 1.15a
OpenSCAD 2015.03
slic3r-1.33.8-prusa3d and/or Simplify3D 3.1.1
Re: how to use an accumulating variable
January 23, 2016 01:50AM
You could use something like module multicyl() below which creates a stack of cylinders using an array of [d,h] entries, in your case:
multicyl([[holdupdia,holdupht],[holdshaftdia,holdshaftupperht],[holdmiddia,holdmidht],[holdshaftdia,holdshaftmidht]]);
I also included multicone() which allows the same kind of stacking for cones:
// stack of multiple cylinders defined by an array of [d,h] entries
module multicyl( dh , index=0) {
   if(len(dh)>index){
      cylinder(d=dh[index][0],h=dh[index][1]);
      translate([0,0,dh[index][1]])multicyl(dh,index+1);
   }
}

// stack of multiple cones defined by an array of [r1,r2,d] entries
module multicone( rrh , index=0) {
   if(len(rrh)>index){
      cylinder(r1=rrh[index][0],r2=rrh[index][1],h=rrh[index][2]);
      translate([0,0,rrh[index][2]])multicone(rrh,index+1);
   }
}

translate([0,-30,0])
   multicyl(dh=[[50,2],[20,10],[30,5],[10,11],[50,3]]);

translate([0,30,0])
   multicone(rrh=[[25,25,2],[25,20,10],[20,10,5],[10,10,11],[20,22,3]]);
And here the output of OpenScad for the above example:

Re: how to use an accumulating variable
January 24, 2016 10:13AM
Thank you, I think this is the only solution. Use arrays, but the problem is that they are difficult to change.
What is so hard to include a=a+1 in the openscad language?


appjaws - Ormerod 1, core XY
Duet wifi, duex5
firmware 1.18
Web Interface 1.15a
OpenSCAD 2015.03
slic3r-1.33.8-prusa3d and/or Simplify3D 3.1.1
Re: how to use an accumulating variable
January 25, 2016 02:11AM
Quote
appjaws1
Thank you, I think this is the only solution. Use arrays, but the problem is that they are difficult to change.
What is so hard to include a=a+1 in the openscad language?

OpenSCAD is a declarative language, whereas we are mostly taught (academically or through self-discovery) imperative or procedural languages because they're easier to understand.

CSG trees are a known quantity - you know all the dimensions of all the constituent parts beforehand, even if you have to calculate them along the way. There is no interaction with the user during the 'program' so your variable 'a' should always be known - it's just a case of working backwards through your dependency lists and substituting the real values into the calculations. You can abstract your arrays into functions to help readability and re-usability, but ultimately you are simply describing a bunch of static 3D shapes whose dimensions were set the moment you started writing the code.
Sorry, only registered users may post in this forum.

Click here to login