Welcome! Log In Create A New Profile

# how to use an accumulating variable

Posted by appjaws1
 how to use an accumulating variable January 20, 2016 11:16AM Registered: 10 years ago Posts: 780
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 12:23PM by appjaws1.

appjaws - Core XYUV Duet Ethernet Duex5
firmware 3.1.1 Web Interface 3.1.1
Ormerod 1-converted to laser engraver, Duet wifi
slic3r-1.3.0, Simplify3D 4.1.2, Cura-4.4.1
 Re: how to use an accumulating variable January 21, 2016 03:58PM Registered: 11 years ago Posts: 369
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 05:03PM Registered: 10 years ago Posts: 780
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 - Core XYUV Duet Ethernet Duex5
firmware 3.1.1 Web Interface 3.1.1
Ormerod 1-converted to laser engraver, Duet wifi
slic3r-1.3.0, Simplify3D 4.1.2, Cura-4.4.1
 Re: how to use an accumulating variable January 23, 2016 04:50AM Registered: 9 years ago Posts: 590
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 01:13PM Registered: 10 years ago Posts: 780
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 - Core XYUV Duet Ethernet Duex5
firmware 3.1.1 Web Interface 3.1.1
Ormerod 1-converted to laser engraver, Duet wifi
slic3r-1.3.0, Simplify3D 4.1.2, Cura-4.4.1
 Re: how to use an accumulating variable January 25, 2016 05:11AM Registered: 11 years ago Posts: 369
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.