Discussion:
[OpenSCAD] Configuration selection
Dan Christian
2018-09-13 19:16:09 UTC
Permalink
I want to select between multiple different configurations and want to
understand how to do that in a functional language.

In an imperative language I would do:
model = 'A';
if (model == 'A') {
dim_a = 1;
dim_b = 2;
} else if (model == 'B') {
dim_a = 3;
dim_b = 4;
} else { // default configuration
dim_a = 5;
dim_b = 6;
}

I may have many configurations and variables for each one. This simple
example could be done with ? :, but that gets super messy as things get
more complex.

What's the best way to do this in OpenScad?

Dan
Michael Frey
2018-09-13 20:20:34 UTC
Permalink
Hi Dan,

I see two practical approaches:

*Approach 1:*
If you donÂŽt mind Development snapshoots, try our customizer.
You can get those snapshoots from http://www.openscad.org/downloads.html
You also have to go into the preferences and enable the customzier.

In the customizer you can handle your configurations as so called presets.

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/WIP#Customizer

*Approach 2:*
If you want to stay on the release line of openscad:
I would write a "base_model.scad" (or something like that), which is let
us say model default:
    dim_a = 5;
    dim_b = 6;
    //lots of code that creates the actual model

For model b, I would create a file called "model_b.scad" and then simply
    include <base_model.scad>;
    dim_a = 1;
    dim_b = 2;

For model b, I would create a file called "model_b.scad" and then simply
    include <base_model.scad>;
    dim_a = 3;
    dim_b = 4;

The trick is, that the last assignment overwrites any previous
assignments to a variable.

Every model has then its own file.
Most of the code being in base_model.scad.
The other files only need to overwrite the variables.

Depending on what you really want/need, use might also be an option.
But when you use "use", you need a bit more code for each configuration.

see also
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Include_Statement

With Kind regards,
Michael Frey
Dan Christian
2018-09-13 23:16:56 UTC
Permalink
Thanks!

Approach 2 should work for me.

Dan
Post by Michael Frey
Hi Dan,
*Approach 1:*
If you donÂŽt mind Development snapshoots, try our customizer.
You can get those snapshoots from http://www.openscad.org/downloads.html
You also have to go into the preferences and enable the customzier.
In the customizer you can handle your configurations as so called presets.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/WIP#Customizer
*Approach 2:*
I would write a "base_model.scad" (or something like that), which is let
dim_a = 5;
dim_b = 6;
//lots of code that creates the actual model
For model b, I would create a file called "model_b.scad" and then simply
include <base_model.scad>;
dim_a = 1;
dim_b = 2;
For model b, I would create a file called "model_b.scad" and then simply
include <base_model.scad>;
dim_a = 3;
dim_b = 4;
The trick is, that the last assignment overwrites any previous assignments
to a variable.
Every model has then its own file.
Most of the code being in base_model.scad.
The other files only need to overwrite the variables.
Depending on what you really want/need, use might also be an option.
But when you use "use", you need a bit more code for each configuration.
see also
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Include_Statement
With Kind regards,
Michael Frey
_______________________________________________
OpenSCAD mailing list
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
TLC123
2018-09-14 08:09:36 UTC
Permalink
Assign values to dim_a and dim_b separately.
Use the Ternary conditional operator - ?:
usage:
/condition expression/ ? /expression if true /: /expression if false/ ;

In this case one ternary operation is nested inside one other.

model = 'A';

dim_a = (model == 'A') ? 1:(model == 'B')?3:5;
dim_b = (model == 'A') ? 2:(model == 'B')?4:6;

// "let" dim_a= "if"(model == 'A')? "then" 1 : "else if"(model == 'B')?
"then" 3: "else" 5



--
Sent from: http://forum.openscad.org/
Ronaldo
2018-09-14 15:44:12 UTC
Permalink
​If you have many parameters for each alternative model, it is a good idea to
collect them in a list for each model.

modelA = [ ​1, // dim_a
2, // dim_b
[1,2] // vec_a // some vector parameter
...
];
modelB = [ ​3, // dim_a
4, // dim_b
[2,3] // vec_a
...
];
modelC = [ ​5, // dim_a
6, // dim_b
[3,4] // vec_a
...
];

Then, select the parameter set with just one :? operator

modelset = model=='A' ? modelA : model=='B' ? modelB : modelC;

and retrieve the parameters from modelset:

dim_a = modelset[0];
dim_b = modelset[1];
vec_a = modelset[2];

With a few changes, you will be able to add more parameters without touching
the selection operation.

If the selection key 'model' is an integer instead of a char, you can even
avoid the :? operator at all.

modellist = [modelA, modelB, modelC];
...
modelset = modellist[model];




--
Sent from: http://forum.openscad.org/
runsun
2018-09-14 20:45:11 UTC
Permalink
The associative array
<https://github.com/runsun/OpenSCAD_Tips/blob/master/snippets.md#hash> *
might be an option:

models = [ "A", [ dim_a,1, dim_b, 2 ]
, "B", [ dim_a,3, dim_b, 4 ]
, ...
];

data = hash( models, "A");
dim_a = hash( data, "dim_a");
dim_b = hash( data, "dim_b");




-----

$ Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

--
Sent from: http://forum.openscad.org/

Loading...