simple accumulator question....

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

simple accumulator question....

macdarren
This post was updated on .
I have model in which I create multiple patterns of drill holes.

The pattern and thus the number of holes is optimized for the model size and
"drill size" and other parameters.

The final number of holes drilled is based on a nested group of operations
each fitting in the best pattern.  Each hole is separately positioned and
differenced out.

What I want to do is have an accumulator such that each time a cylinder
difference is created to 'drill' a hole I want accumulate the volume of
material removed or at least the area that is cut.

Traditionally this would be done something like:

                 cut_volume = cut_volume + volume_of_new_cut

Now as I understand it this won't work in OpenSCAD....or maybe I am wrong...

Any suggestions about how to implement this...I don't really want to change
the mechanism for calculating the holes, I just want to add some code to
keep track and echo the result.



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

_______________________________________________
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: simple accumulator question....

Troberg
That would work, as long as the variable is declared on a global scope. I do
it to calculate cost and weight.



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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: simple accumulator question....

macdarren
This post was updated on .
If this works for you, I must be doing something wrong.....

I get only one accumulation then despite multiple adds the value never changes.
Based on my understanding one Scad this is basically what I expected....so there must be
a trick here I don't grasp.

I declared/initialized the total outside any code (I also tried it inside various places) but if I echo after each add it never changes.

after the loops that drill the object the value echoed is the initial value...like the loops had their own scope...which makes some sense but still I can't accumulate any data even just trying to do accumulations at the global level fails.

Could you post a small example snippet that shows how this works for you?

Thanks










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

_______________________________________________
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: simple accumulator question....

nophead
You can't accumulate in openscad variables, I don't know why Troberg said you can.



On Wed, 10 Apr 2019 at 16:40, macdarren <[hidden email]> wrote:
I must be doing something wrong.....

I get only one accumulation them despite multiple adds the value never
changes.
Based on my understanding one Scad this is how it works....so there must be
a trick here I don't grasp.





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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: simple accumulator question....

doug.moen
In reply to this post by macdarren
You were right the first time.
   cut_volume = cut_volume + volume_of_new_cut
does not work in OpenSCAD. It doesn't work in any context.

On Wed, Apr 10, 2019, at 11:40 AM, macdarren wrote:

> I must be doing something wrong.....
>
> I get only one accumulation them despite multiple adds the value never
> changes.
> Based on my understanding one Scad this is how it works....so there must be
> a trick here I don't grasp.
>
>
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: simple accumulator question....

nophead
In reply to this post by nophead
You can accumulate in C style for loops in list comprehensions.

echo ([for(i = 0, t = 0; i < 10; t = t + i, i = i + 1) if(i == 9) t][0]);

ECHO: 36




On Wed, 10 Apr 2019 at 16:46, nop head <[hidden email]> wrote:
You can't accumulate in openscad variables, I don't know why Troberg said you can.



On Wed, 10 Apr 2019 at 16:40, macdarren <[hidden email]> wrote:
I must be doing something wrong.....

I get only one accumulation them despite multiple adds the value never
changes.
Based on my understanding one Scad this is how it works....so there must be
a trick here I don't grasp.





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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: simple accumulator question....

macdarren
Hmm that looks promising...I use for loops for each section so if I can even
get subsection totals that would be a big improvement...I will try to
implement this and see what happens.

Thanks



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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: simple accumulator question....

adrianv
Nobody has mentioned the normal way of accumulating, which is to use
recursion.  I don't know if you can apply this to your situation, though.  

function sumsqr(n,total=0) = n==0 ? total : sumsqr(n-1,total+n*n);





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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: simple accumulator question....

Troberg
In reply to this post by nophead
nophead wrote
> You can't accumulate in openscad variables, I don't know why Troberg said
> you can.

You are right, I just checked my code. I log the numbers, and then add stuff
from the log in a separate program. Sorry!




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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org