Trying to add an option for units to a file using the Customizer

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

Trying to add an option for units to a file using the Customizer

William Adams-2
but it's not working.

This code:

//Units
Units="0"; // [0:Inches, 1:Millimeters]
if (Units=="0") {conversionfactor=25.4;} else {conversionfactor=1;};
/* [Size] */
//Length
length = 6;
//Width
width = 3;
//Height
height = 4;
clength = length*conversionfactor;
cwidth = width*conversionfactor;
cheight = height*conversionfactor;
cube(size = [clength,cwidth,cheight], center = false);

results in the error: WARNING: Ignoring unknown variable 'conversionfactor'.  appearing 3 times in the Console.

What is not right and is there some other way I should go about this?

William


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

Re: Trying to add an option for units to a file using the Customizer

JordanBrown
On 5/24/2018 7:55 AM, William Adams wrote:
but it's not working.

This code:

//Units
Units="0"; // [0:Inches, 1:Millimeters]
if (Units=="0") {conversionfactor=25.4;} else {conversionfactor=1;};
/* [Size] */
//Length
length = 6;
//Width
width = 3;
//Height
height = 4;
clength = length*conversionfactor;
cwidth = width*conversionfactor;
cheight = height*conversionfactor;
cube(size = [clength,cwidth,cheight], center = false);

results in the error: WARNING: Ignoring unknown variable 'conversionfactor'.  appearing 3 times in the Console.

What is not right and is there some other way I should go about this?

This might well be the most common OpenSCAD question.

OpenSCAD variables... aren't variables.  They're constants, sort of, with the last assignment winning and with inner scopes able to override values from outer scopes.  You can't change them, or at least not exactly, and you especially can't change them inside an "if".

x=1;
echo(x=x);
x=2;
echo(x=x);

produces

ECHO: x = 2
ECHO: x = 2

and

x=1;
echo(x=x);
if (true) {
    x=2;
    echo (x=x);
}
echo (x=x);

produces

ECHO: x = 1
ECHO: x = 2
ECHO: x = 1

If you need to set a value conditionally, you need to use the ? : operator, e.g.

conversionfactor = (Units=="0") ? 25.4 : 1;

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

Re: Trying to add an option for units to a file using the Customizer

William Adams-2
Thanks.

My apologies for not twigging to that. I actually saw that in the manual, but it didn't make sense to me.

William


On Thu, May 24, 2018 at 11:16 AM, Jordan Brown <[hidden email]> wrote:
On 5/24/2018 7:55 AM, William Adams wrote:
but it's not working.

This code:

//Units
Units="0"; // [0:Inches, 1:Millimeters]
if (Units=="0") {conversionfactor=25.4;} else {conversionfactor=1;};
/* [Size] */
//Length
length = 6;
//Width
width = 3;
//Height
height = 4;
clength = length*conversionfactor;
cwidth = width*conversionfactor;
cheight = height*conversionfactor;
cube(size = [clength,cwidth,cheight], center = false);

results in the error: WARNING: Ignoring unknown variable 'conversionfactor'.  appearing 3 times in the Console.

What is not right and is there some other way I should go about this?

This might well be the most common OpenSCAD question.

OpenSCAD variables... aren't variables.  They're constants, sort of, with the last assignment winning and with inner scopes able to override values from outer scopes.  You can't change them, or at least not exactly, and you especially can't change them inside an "if".

x=1;
echo(x=x);
x=2;
echo(x=x);

produces

ECHO: x = 2
ECHO: x = 2

and

x=1;
echo(x=x);
if (true) {
    x=2;
    echo (x=x);
}
echo (x=x);

produces

ECHO: x = 1
ECHO: x = 2
ECHO: x = 1

If you need to set a value conditionally, you need to use the ? : operator, e.g.

conversionfactor = (Units=="0") ? 25.4 : 1;


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