discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

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

L
lar3ry
Sat, May 8, 2021 3:46 PM

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: http://forum.openscad.org/

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: http://forum.openscad.org/
NH
nop head
Sat, May 8, 2021 4:08 PM

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.

See
https://github.com/nophead/NopSCADlib/blob/master/printed/printed_box.scad
for an example.

On Sat, 8 May 2021 at 16:48, lar3ry lar3ry@sasktel.net 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 http://forum.openscad.org/
at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

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. See https://github.com/nophead/NopSCADlib/blob/master/printed/printed_box.scad for an example. On Sat, 8 May 2021 at 16:48, lar3ry <lar3ry@sasktel.net> 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 <http://forum.openscad.org/> > at Nabble.com. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
LM
Leonard Martin Struttmann
Sat, May 8, 2021 8:18 PM

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 nop.head@gmail.com 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.

See
https://github.com/nophead/NopSCADlib/blob/master/printed/printed_box.scad
for an example.

On Sat, 8 May 2021 at 16:48, lar3ry lar3ry@sasktel.net 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 http://forum.openscad.org/
at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

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 <nop.head@gmail.com> 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. > > See > https://github.com/nophead/NopSCADlib/blob/master/printed/printed_box.scad > for an example. > > > > On Sat, 8 May 2021 at 16:48, lar3ry <lar3ry@sasktel.net> 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 <http://forum.openscad.org/> >> at Nabble.com. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
L
lar3ry
Sat, May 8, 2021 8:50 PM

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.

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/
L
lar3ry
Sat, May 8, 2021 8:53 PM

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 );

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: http://forum.openscad.org/
A
adrianv
Sat, May 8, 2021 9:11 PM

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

discuss-leave@.openscad

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 > discuss-leave@.openscad -- Sent from: http://forum.openscad.org/
L
lar3ry
Sun, May 9, 2021 4:11 AM

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: http://forum.openscad.org/

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: http://forum.openscad.org/
NH
nop head
Sun, May 9, 2021 8:11 AM

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 lar3ry@sasktel.net 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 http://forum.openscad.org/
at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

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 <lar3ry@sasktel.net> 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 <http://forum.openscad.org/> > at Nabble.com. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
L
lar3ry
Mon, May 10, 2021 9:11 PM

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 * cos(A));
	echo ("line 56 ",A,B,C,a,b,c);

and the result of the echo is:
ECHO: "line 56 ", 30, 0, 0, 4.75981e+6, 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?

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 * cos(A)); echo ("line 56 ",A,B,C,a,b,c); and the result of the echo is: ECHO: "line 56 ", 30, 0, 0, 4.75981e+6, 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: http://forum.openscad.org/
L
lar3ry
Mon, May 10, 2021 9:20 PM

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?

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: http://forum.openscad.org/