Is it possible to assign a global variable at run time?

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

Is it possible to assign a global variable at run time?

lar3ry
I'm working on a project that requires passing 6 values to a module, and those 6 values must be passed on to another module, then those same values must be passed on to 18 more modules.

I just figure that a global variable that is assignable at run time would simplify things a lot.

If that can't be done, does anyone have an idea about how best to implement something like it?



Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

nophead
There is no "run time" as it is a description, not an executable program, so I don't understand what you mean.

You can set a global constant and use it in all functions and modules defined in the same file. If you prefix it with $ then it will also be seen by all functions and modules called from the file it is defined in.

If you are passing a lot of variables around perhaps it is better to put them in a list and just pass that. For example when I define something like a parametric box I put the properties in a list and pass it to all functions and modules that draw the box.


On Sat, 8 May 2021 at 16:48, lar3ry <[hidden email]> wrote:
I'm working on a project that requires passing 6 values to a module, and those 6 values must be passed on to another module, then those same values must be passed on to 18 more modules.

I just figure that a global variable that is assignable at run time would simplify things a lot.

If that can't be done, does anyone have an idea about how best to implement something like it?



Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

LenStruttmann
Alternatively, you can create a module that contains a function that returns your six values.  For example, put:

function mySixValues() = [ 1, 2, 3, 5, 6, 7 ];

...in a file called myGlobalValues.scad

Then, in each module that needs those value, put:

//*******************************
use <myGlobalValues.scad>;

myValues = mySixValues();

echo( myValues );




On Sat, May 8, 2021 at 11:09 AM nop head <[hidden email]> wrote:
There is no "run time" as it is a description, not an executable program, so I don't understand what you mean.

You can set a global constant and use it in all functions and modules defined in the same file. If you prefix it with $ then it will also be seen by all functions and modules called from the file it is defined in.

If you are passing a lot of variables around perhaps it is better to put them in a list and just pass that. For example when I define something like a parametric box I put the properties in a list and pass it to all functions and modules that draw the box.


On Sat, 8 May 2021 at 16:48, lar3ry <[hidden email]> wrote:
I'm working on a project that requires passing 6 values to a module, and those 6 values must be passed on to another module, then those same values must be passed on to 18 more modules.

I just figure that a global variable that is assignable at run time would simplify things a lot.

If that can't be done, does anyone have an idea about how best to implement something like it?



Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

lar3ry
In reply to this post by nophead
OK. I really didn't think it could be done, but thought I'd ask anyway.

So I decided to pass the variables as a list, and that's acceptable.

However, I have a problem, related to the whole "variables aren't" thing.
When I test for a valid formula, I can detect whether or not it's valid, but I can't figure out how to pass it out of the for, so I can check for it in the assert(), and use it in the call to calc(formula,given);

Is there some way I can store the true or false externally to the for loop, so I can fetch it later?
Or is there perhaps some way I can break out of the for loop if I find a match?

valid_formula = ([13,23,43,15,25,45,16,26,46,31,32,
                                34,51,52,54,61,62,64,71,72,74]);

triangle (20,30,0,50,0,0); //generates formula 64

module triangle(A,B,C,a,b,c) {
        given = [A,B,C,a,b,c];
        angflag = ((A>0 ? 4 :0) + (B>0 ? 2 :0) + (C>0 ? 1 :0)) * 10;
        sideflag = ((a>0 ? 4 :0) + (b>0 ? 2 :0) + (c>0 ? 1 :0));
        formula = angflag+sideflag;
        for (f = [0:20]) {
                if (formula == valid_formula[f]) {
                        echo ("formula: ", formula, " is valid");
                }
        }
// assert(valid==true, "Invalid input. Must have 2 angles and 1 side, two sides and 1 angle, or three angles and 1 side");
       
echo (given);
// Note that in we only need to solve for side b and side c because once we have those, we can simply close the path
// calc(formula,given);
}



nophead wrote
If you are passing a lot of variables around perhaps it is better to put
them in a list and just pass that. For example when I define something like
a parametric box I put the properties in a list and pass it to all
functions and modules that draw the box.


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

lar3ry
In reply to this post by LenStruttmann
Hmmm... and how would I go about saving the values in a separate file?

LenStruttmann wrote
Alternatively, you can create a module that contains a function that
returns your six values.  For example, put:

function mySixValues() = [ 1, 2, 3, 5, 6, 7 ];

...in a file called myGlobalValues.scad

Then, in each module that needs those value, put:

//*******************************
use <myGlobalValues.scad>;

myValues = mySixValues();

echo( myValues );


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

adrianv
In reply to this post by lar3ry
You just want to know if formula is in the list valid_formula?  Why not use search?  

Something like search([formula], valid_formula)[0] != [].  Might double check syntax, or use BOSL2 in_list() instead to avoid idiotic search() syntax.  

If you really wanted to do it your way (or if the conditional was something else):

match = [for(f=valid_formula) if (f==formula) f];
assert(match != []);


lar3ry wrote
OK. I really didn't think it could be done, but thought I'd ask anyway.

So I decided to pass the variables as a list, and that's acceptable.

However, I have a problem, related to the whole "variables aren't" thing.
When I test for a valid formula, I can detect whether or not it's valid, but
I can't figure out how to pass it out of the for, so I can check for it in
the assert(), and use it in the call to calc(formula,given);

Is there some way I can store the true or false externally to the for loop,
so I can fetch it later?
Or is there perhaps some way I can break out of the for loop if I find a
match?

valid_formula = ([13,23,43,15,25,45,16,26,46,31,32,
                                34,51,52,54,61,62,64,71,72,74]);

triangle (20,30,0,50,0,0); //generates formula 64

module triangle(A,B,C,a,b,c) {
        given = [A,B,C,a,b,c];
        angflag = ((A>0 ? 4 :0) + (B>0 ? 2 :0) + (C>0 ? 1 :0)) * 10;
        sideflag = ((a>0 ? 4 :0) + (b>0 ? 2 :0) + (c>0 ? 1 :0));
        formula = angflag+sideflag;
        for (f = [0:20]) {
                if (formula == valid_formula[f]) {
                        echo ("formula: ", formula, " is valid");
                }
        }
// assert(valid==true, "Invalid input. Must have 2 angles and 1 side, two
sides and 1 angle, or three angles and 1 side");
       
echo (given);
// Note that in we only need to solve for side b and side c because once we
have those, we can simply close the path
// calc(formula,given);
}




nophead wrote
> If you are passing a lot of variables around perhaps it is better to put
> them in a list and just pass that. For example when I define something
> like
> a parametric box I put the properties in a list and pass it to all
> functions and modules that draw the box.





--
Sent from: http://forum.openscad.org/
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

lar3ry
adrianv wrote
You just want to know if formula is in the list valid_formula?  Why not use
search?  
Something like search([formula], valid_formula)[0] != [].  
Ahh. Never have used search. That will do it, but...

Might double check syntax, or use BOSL2 in_list() instead to avoid idiotic search()
syntax.  
That sounds even better.
Thanks Adrian!

oops. forgot to send this.

As you might surmise by my code snippet, I am trying to make an OpenSCAD library that will solve triangle's sides and angles, given three pieces of data. One angle and two sides, or two angles and one side.

I ended up using BOSL2 to error check before passing the given data to a formula. Not knowing anything about trig, I am using the triangle calculator at http://cossincalc.com/

After putting in values of C=50, b=30, and c=60, I got a triangle annotated with angles and sides.

Scrolling down a bit to the Formulae section, and looking at the formula used to calculate B, I run into my first trouble.

What the heck does the '-1' in that formula mean?




Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

nophead
If you mean sin to the -1 it means arcsine, which is asin() in openscad. I.e. the inverse of sin.

On Sun, 9 May 2021 at 05:11, lar3ry <[hidden email]> wrote:
adrianv wrote
You just want to know if formula is in the list valid_formula?  Why not use
search?  
Something like search([formula], valid_formula)[0] != [].  
Ahh. Never have used search. That will do it, but...

Might double check syntax, or use BOSL2 in_list() instead to avoid idiotic search()
syntax.  
That sounds even better.
Thanks Adrian!

oops. forgot to send this.

As you might surmise by my code snippet, I am trying to make an OpenSCAD library that will solve triangle's sides and angles, given three pieces of data. One angle and two sides, or two angles and one side.

I ended up using BOSL2 to error check before passing the given data to a formula. Not knowing anything about trig, I am using the triangle calculator at http://cossincalc.com/

After putting in values of C=50, b=30, and c=60, I got a triangle annotated with angles and sides.

Scrolling down a bit to the Formulae section, and looking at the formula used to calculate B, I run into my first trouble.

What the heck does the '-1' in that formula mean?




Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

lar3ry
Thanks! Got another problem with a formula. Here's a snippet

        if (formula==43) {
                A=given[0]; // 30
                b=given[4]; // 25
                c=given[5]; // 40
                a =sqr(b^2 + c^2 - 2 * b * c * cos(A));
                echo ("line 56 ",A,B,C,a,b,c);

and the result of the echo is:
ECHO: "line 56 ", 30, 0, 0, 242999, 25, 40

The answer should be 22.20 according to the http://cossincalc.com site.

    nophead wrote
    If you mean sin to the -1 it means arcsine, which is asin() in openscad.
    I.e. the inverse of sin.

    > What the heck does the '-1' in that formula mean?

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

mondo

    A= 30;
                b= 25;
                c=  40;
                a =sqrt(b^2 + c^2 - 2 * b * c * cos(A));
                echo ("line 56 ",A,B,C,a,b,c);

works, I guess a given value is wrong.


On 10/05/2021 22:20, lar3ry wrote:
Thanks! Got another problem with a formula. Here's a snippet

        if (formula==43) {
                A=given[0]; // 30
                b=given[4]; // 25
                c=given[5]; // 40
                a =sqr(b^2 + c^2 - 2 * b * c * cos(A));
                echo ("line 56 ",A,B,C,a,b,c);

and the result of the echo is:
ECHO: "line 56 ", 30, 0, 0, 242999, 25, 40

The answer should be 22.20 according to the http://cossincalc.com site.

    nophead wrote
    If you mean sin to the -1 it means arcsine, which is asin() in openscad.
    I.e. the inverse of sin.

    > What the heck does the '-1' in that formula mean?

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

LenStruttmann
The square root function is sqrt().  You've got sqr().

Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

lar3ry
In reply to this post by mondo
Hmm...
A= 30;
      b= 25;
      c=  40; a =sqr(b^2 + c^2 - 2 * b * c * cos(A));
                echo ("line 56 ",A,B,C,a,b,c);

ECHO: "line 56 ", 30, 0, 0, 242999, 25, 40

This is with OpenSCAD version 2021.04.21.ai7788 (git d9d4a9757)

Exactly the same result with OpenSCAD version 2019.05
                A= 30;
                b= 25;
                c= 40;
                a =sqr((b*b) + (c*c) - 2 * b * c * cos(A));
                echo ("line 56 ",A,B,C,a,b,c);

ECHO: "line 56 ", 30, 0, 0, 242999, 25, 40

I am unable to test with 2021.01 because on the OpenSCAD download page,
sudo apt-get install openscad
gives me 2019.05






mondo wrote
    A= 30;
                 b= 25;
                 c=  40;
                 a =sqrt(b^2 + c^2 - 2 * b * c * cos(A));
                 echo ("line 56 ",A,B,C,a,b,c);

works, I guess a given value is wrong.


On 10/05/2021 22:20, lar3ry wrote:

> Thanks! Got another problem with a formula. Here's a snippet
>
>         if (formula==43) {
>                 A=given[0]; // 30
>                 b=given[4]; // 25
>                 c=given[5]; // 40
>                 a =sqr(b^2 + c^2 - 2 * b * c * cos(A));
>                 echo ("line 56 ",A,B,C,a,b,c);
>
> and the result of the echo is:
> ECHO: "line 56 ", 30, 0, 0, 242999, 25, 40
>
> The answer should be 22.20 according to the http://cossincalc.com 
> <http://cossincalc.com> site.
>
>     nophead wrote
>     If you mean sin to the -1 it means arcsine, which is asin() in
> openscad.
>     I.e. the inverse of sin.
>
>     > What the heck does the '-1' in that formula mean?
> ------------------------------------------------------------------------
> Sent from the OpenSCAD mailing list archive
> <http://forum.openscad.org/> at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> To unsubscribe send an email to [hidden email]

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign a global variable at run time?

lar3ry
In reply to this post by LenStruttmann
That loud sound you just heard was my hand slapping my forehead!
Thanks Len!

LenStruttmann wrote
The square root function is sqrt().  You've got sqr().


Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to [hidden email]