OFF TOPIC - openscad help January 21, 2016 11:22AM |
Registered: 10 years ago Posts: 782 |
Re: OFF TOPIC - openscad help January 21, 2016 01:35PM |
Registered: 9 years ago Posts: 106 |
Quote
appjaws1
I am changing some parts on my ormerod and am using openscad. I have posted on that forum but have had no replies as yet, so thought I would try the experts on this forum
Is it possible to use an accumulating variable?
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?
acc=...; // initial definition of acc acc1=acc+holdht; acc2=acc1+holdshaftupperht; acc3=acc2+holdmidht;...then the series of "translate ([0,0,acc/acc1/acc2/acc3]) cylinder()" bits. You would still have to have four of these because the variables used in "cylinder" are different in each instance.
increment=[init,holdht,holdshaftupperht,holdmidht]; function acc(n)=(n>0)?acc(n-1)+increment[n]:increment[n];
increment=[init,holdht,holdshaftupperht,holdmidht]; function acc(n)=(n>0)?acc(n-1)+increment[n]:increment[n]; diameter=[holdupdia,holdshaftdia,holdmiddia,holdshaftdia]; height=[holdupht,holdshaftupperht,holdmidht,holdshaftmidht]; for (loop=[0:1:3]) { translate ([0,0,acc(loop)]) cylinder (d=diameter[loop],h=height[loop]); }
Re: OFF TOPIC - openscad help January 21, 2016 01:50PM |
Registered: 10 years ago Posts: 782 |
Re: OFF TOPIC - openscad help January 21, 2016 04:01PM |
Registered: 9 years ago Posts: 106 |
Quote
appjaws1
Thank you Bart, 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.
acc=initial value translate ([0,0,acc]) union () { cylinder (d=holdupdia,h=holdupht); translate ([0,0,holdht]) union () { cylinder (d=holdshaftdia,h=holdshaftupperht); translate ([0,0,holdshaftupperht]) union () { cylinder (d=holdmiddia,h=holdmidht); translate ([0,0,holdmidht]) union () { cylinder (d=holdshaftdia,h=holdshaftmidht); } } } }The last union() is not necessary but is added here for consistency with the preceding code. This is a bit like a series of recursive function calls (in terms of the z coordinate in "translate") but written out in its entirety. Each translate builds on the previous one.
Re: OFF TOPIC - openscad help January 21, 2016 06:12PM |
Registered: 9 years ago Posts: 106 |
sections=4; // number of cylindrical sections heights=[3,5,3,2]; // heights of consecutive sections diameters=[5,2,8,7]; // diameters of consecutive sections module draw_section (iter=sections) { cylinder (h=heights[sections-iter],d=diameters[sections-iter]); if (iter>1) translate ([0,0,heights[sections-iter]]) draw_section (iter-1); } draw_section ();
Re: OFF TOPIC - openscad help January 22, 2016 06:05AM |
Registered: 10 years ago Posts: 782 |