discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Re: how to use IF ... else ...

AM
Adrian Mariano
Fri, Apr 22, 2022 1:32 PM

There are two mechanisms for conditionals in OpenSCAD. One is for
conditional geometry and the other for conditional variable
assignment.  Below you are doing variable assignment but using the
if....else construction, which is for geometry.  If you make your
geometry inside the if statements it will work.  That is:

if (mountSide=="left"){
side="0";
exitDegree=-1;
/// make geometry here
echo(side,mountSide,exitDegree);  // This is stand-in for geometry.
Must be inside the scope
}

The echo statement is a proxy for making geometry.  Variable
assignments don't escape their scope, so they stay within the {}
region where they are defined.

Atlernatively if that doesn't fit your needs (which aren't clear from
this message) then you can use the ? ... : method for variable
assignment.  That would like this:

side = mountSide=="left" ? "0" : "1";
exitDegree = mountside=="left" ? 1 : -1;

Note here I assume that if mountSide is not "left" then it is "right".
You can cascade it and do

side = mountSide == "left" ? "0"
: mountSide == "right" ? "1"
: mountside == "top"? "some other value"
: assert(false,"invalid moutSide value");  // print error
message if no case worked

This second approach will conditionally assign your variables so that
you can then use them to create the geometry.  Your echo statement in
your example will work, and you can make the geometry afterwords based
on the the side and exitDegree values.

On Fri, Apr 22, 2022 at 8:46 AM Jan Öhman via Discuss
discuss@lists.openscad.org wrote:

---------- Forwarded message ----------
From: "Jan Öhman" jan_ohman@yahoo.com
To: OpenSCAD General Discussion discuss@lists.openscad.org
Cc:
Bcc:
Date: Fri, 22 Apr 2022 12:47:41 +0000 (UTC)
Subject: [OpenSCAD] how to use IF ... else ...
Works on a box that should be left or right configured.
by entering e.g. "Left" or "Right" to the module
Thought to use mirror ()
and the other thing that needs to be changed is an angle of a tube (ex. Left = 30degree and Right = -30degree)

I have now understand that the suggestion below doesn't work.
(but i have no idea how to solve this)

=======

module box(mountSide="left")
{
if (mountSide == "right")
{ side="1";
exitDegree=1;
}
if (mountSide == "left")
{  side="0";
exitDegree=-1;
}
echo(side, mountSide, exitDegree);
....

=======

---------- Forwarded message ----------
From: "Jan Öhman via Discuss" discuss@lists.openscad.org
To: OpenSCAD General Discussion discuss@lists.openscad.org
Cc: "Jan Öhman" jan_ohman@yahoo.com
Bcc:
Date: Fri, 22 Apr 2022 12:47:41 +0000 (UTC)
Subject: [OpenSCAD] how to use IF ... else ...


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

There are two mechanisms for conditionals in OpenSCAD. One is for conditional geometry and the other for conditional variable assignment. Below you are doing variable assignment but using the if....else construction, which is for geometry. If you make your geometry inside the if statements it will work. That is: if (mountSide=="left"){ side="0"; exitDegree=-1; /// make geometry here echo(side,mountSide,exitDegree); // This is stand-in for geometry. Must be inside the scope } The echo statement is a proxy for making geometry. Variable assignments don't escape their scope, so they stay within the {} region where they are defined. Atlernatively if that doesn't fit your needs (which aren't clear from this message) then you can use the ? ... : method for variable assignment. That would like this: side = mountSide=="left" ? "0" : "1"; exitDegree = mountside=="left" ? 1 : -1; Note here I assume that if mountSide is not "left" then it is "right". You can cascade it and do side = mountSide == "left" ? "0" : mountSide == "right" ? "1" : mountside == "top"? "some other value" : assert(false,"invalid moutSide value"); // print error message if no case worked This second approach will conditionally assign your variables so that you can then use them to create the geometry. Your echo statement in your example will work, and you can make the geometry afterwords based on the the side and exitDegree values. On Fri, Apr 22, 2022 at 8:46 AM Jan Öhman via Discuss <discuss@lists.openscad.org> wrote: > > > > > ---------- Forwarded message ---------- > From: "Jan Öhman" <jan_ohman@yahoo.com> > To: OpenSCAD General Discussion <discuss@lists.openscad.org> > Cc: > Bcc: > Date: Fri, 22 Apr 2022 12:47:41 +0000 (UTC) > Subject: [OpenSCAD] how to use IF ... else ... > Works on a box that should be left or right configured. > by entering e.g. "Left" or "Right" to the module > Thought to use mirror () > and the other thing that needs to be changed is an angle of a tube (ex. Left = 30degree and Right = -30degree) > > I have now understand that the suggestion below doesn't work. > (but i have no idea how to solve this) > > ======= > > module box(mountSide="left") > { > if (mountSide == "right") > { side="1"; > exitDegree=1; > } > if (mountSide == "left") > { side="0"; > exitDegree=-1; > } > echo(side, mountSide, exitDegree); > .... > > ======= > > > > ---------- Forwarded message ---------- > From: "Jan Öhman via Discuss" <discuss@lists.openscad.org> > To: OpenSCAD General Discussion <discuss@lists.openscad.org> > Cc: "Jan Öhman" <jan_ohman@yahoo.com> > Bcc: > Date: Fri, 22 Apr 2022 12:47:41 +0000 (UTC) > Subject: [OpenSCAD] how to use IF ... else ... > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org