call module based on parameter value

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

call module based on parameter value

fractorr
Is there way to call a module based on the value of a parameter other than doing what I have below?  This method works but was wondering if there was a way to eliminate the callmod module?


mod = "modA";  // [modA,modB,modC]

cube([50, 50, 10]);
callmod(mod);

module callmod(modname) {
    if      (modname == "modA") modA();
    else if (modname == "modB") modB();
    else if (modname == "modC") modC();
}

module modA() {
    translate([50, 50, 0])
        cube([50, 50, 10]);
}

module modB() {
    translate([-100, -100, 0])
        cube([100, 100, 10]);
}

module modC() {
    translate([50, -200, 0])
        cube([200, 200, 10]);
}


Sent from the OpenSCAD mailing list archive at Nabble.com.

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

Re: call module based on parameter value

JordanBrown
On 3/7/2021 10:18 AM, fractorr wrote:
Is there way to call a module based on the value of a parameter other than doing what I have below?  This method works but was wondering if there was a way to eliminate the callmod module?

There are any number of other styles for what you're doing, but they all end up the same:  no, you cannot take a string and have it be interpreted as a module name.  One way or another, you'll need to compare that string and explicitly call the desired module.


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