discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

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

WA
William Adams
Thu, May 24, 2018 2:55 PM

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 = widthconversionfactor;
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

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
JB
Jordan Brown
Thu, May 24, 2018 3:16 PM

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 = widthconversionfactor;
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;
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;
WA
William Adams
Thu, May 24, 2018 3:35 PM

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 <
openscad@jordan.maileater.net> 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 = widthconversionfactor;
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;

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 < openscad@jordan.maileater.net> 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; > >