discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Looking for equivalent of python/perl exec()

DP
Dan Perry
Tue, Nov 19, 2024 9:47 AM

I have a scad file with 50 modules, each one imports a different svg file,
each svg file needs different scaling and X-Y translation.

With python or perl exec(), iirc, I can pass a string to my parent module
that could be used to call the correct svg import module, like this if
exec() existed in OpenSCAD:

module parent(var) { exec(str("import_", var));}

I'm looking for something more clever than a 50 line conditional statement.

Dan

I have a scad file with 50 modules, each one imports a different svg file, each svg file needs different scaling and X-Y translation. With python or perl exec(), iirc, I can pass a string to my parent module that could be used to call the correct svg import module, like this if exec() existed in OpenSCAD: module parent(var) { exec(str("import_", var));} I'm looking for something more clever than a 50 line conditional statement. Dan
TP
Torsten Paul
Tue, Nov 19, 2024 9:52 AM

On 19.11.24 10:47, Dan Perry via Discuss wrote:

I'm looking for something more clever than a 50 line conditional statement.

From that snippet it's not clear why you would need something
like exec. It seems just the variable is fine.

Example:
https://files.openscad.org/advent-calendar-2023/Lamp-Part6/Lamp.scad

ciao,
Torsten.

On 19.11.24 10:47, Dan Perry via Discuss wrote: > I'm looking for something more clever than a 50 line conditional statement. From that snippet it's not clear why you would need something like exec. It seems just the variable is fine. Example: https://files.openscad.org/advent-calendar-2023/Lamp-Part6/Lamp.scad ciao, Torsten.
ML
Math Lover <
Tue, Nov 19, 2024 10:01 AM

I sometimes do it the other way around:

If I have data in a database or other system that I want to use in
OpenSCAD, I generate an OpenSCAD file from that data with a script or query.

In my main OpenSCAD file, I then include the generated data file.

On 19-11-2024 10:52, Torsten Paul via Discuss wrote:

On 19.11.24 10:47, Dan Perry via Discuss wrote:

I'm looking for something more clever than a 50 line conditional
statement.

From that snippet it's not clear why you would need something
like exec. It seems just the variable is fine.

Example:
https://files.openscad.org/advent-calendar-2023/Lamp-Part6/Lamp.scad

ciao,
  Torsten.


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

I sometimes do it the other way around: If I have data in a database or other system that I want to use in OpenSCAD, I generate an OpenSCAD file from that data with a script or query. In my main OpenSCAD file, I then include the generated data file. On 19-11-2024 10:52, Torsten Paul via Discuss wrote: > On 19.11.24 10:47, Dan Perry via Discuss wrote: >> I'm looking for something more clever than a 50 line conditional >> statement. > > From that snippet it's not clear why you would need something > like exec. It seems just the variable is fine. > > Example: > https://files.openscad.org/advent-calendar-2023/Lamp-Part6/Lamp.scad > > ciao, >   Torsten. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
WF
William F. Adams
Tue, Nov 19, 2024 1:40 PM

On Tuesday, November 19, 2024 at 05:02:00 AM EST, Math Lover < via Discuss discuss@lists.openscad.org wrote:

I sometimes do it the other way around:

If I have data in a database or other system that I want to use in
OpenSCAD, I generate an OpenSCAD file from that data with a script or query.

In my main OpenSCAD file, I then include the generated data file.

You could use a different programming system (a Make file?) to control OpenSCAD, or you could use the Python-enabled version OpenPythonSCAD:

https://pythonscad.org/

William

On Tuesday, November 19, 2024 at 05:02:00 AM EST, Math Lover < via Discuss <discuss@lists.openscad.org> wrote: >I sometimes do it the other way around: >If I have data in a database or other system that I want to use in >OpenSCAD, I generate an OpenSCAD file from that data with a script or query. >In my main OpenSCAD file, I then include the generated data file. You could use a different programming system (a Make file?) to control OpenSCAD, or you could use the Python-enabled version OpenPythonSCAD: https://pythonscad.org/ William
JB
Jordan Brown
Tue, Nov 19, 2024 3:41 PM

On 11/19/2024 1:47 AM, Dan Perry via Discuss wrote:

I have a scad file with 50 modules, each one imports a different svg
file, each svg file needs different scaling and X-Y translation.  

With python or perl exec(), iirc, I can pass a string to my parent
module that could be used to call the correct svg import module, like
this if exec() existed in OpenSCAD:

 module parent(var) { exec(str("import_", var));}

I'm looking for something more clever than a 50 line conditional
statement.

Just to confirm what you're looking for:

You have a string that has a module name, and you want to call that
module.  Right?

There's currently no way to do that.

For your particular case, where you have n SVG files with n scale
factors and n translations, you could put all of that in an array,
something like:

NAME = 0;
SCALE = 1;
TRANSLATE = 2;
list = [
    [ "aaa", 1.0, [3,4] ],
    [ "bbb", 1.2, [0,0] ],
    [ "ccc", [1, 2], [-5, -5] ],
    ...
];

for (e = list) {
    translate(e[TRANSLATE]) scale(e[SCALE]) import(str(e[NAME], ".svg"));
}

... but there's nothing that will help you with the general case of
dispatching to modules.

PR#4478 has a couple of features that might be helpful, but has no ETA.

Module references:

list = [
    module () { translate([3,4]) import("aaa.svg"); },
    module () { scale(1.2) import("bbb.svg"); },
    module () { translate([-5,-5]) scale([1,2]) import("ccc.svg"); },
    ...
];

for (m = list) m();

Geometry values:

list = [
    {{ translate([3,4]) import("aaa.svg"); }},
    {{ scale(1.2) import("bbb.svg"); }},
    {{ translate([-5,-5]) scale([1,2]) import("ccc.svg"); }},
    ...
];

Objects/dictionaries/associative arrays:

list = {
    aaa: { t: [3,4] },
    bbb: { s: 1.2 },
    ccc: { t: [-5,-5], s: [1,2]),
    ...
};

for (n = list) {
    e = list[n];
    translate(e.t ? e.t : [0,0])
        scale(e.s ? e.s : 1)
        import(str(n, ".svg"));
}

and of course objects/dictionaries/associative arrays can contain module
references and geometry values.

It doesn't have "given a string, call the named module", though I think
that would be pretty straightforward:  there could be a function that,
given a string, would look it up and return a module reference.

On 11/19/2024 1:47 AM, Dan Perry via Discuss wrote: > I have a scad file with 50 modules, each one imports a different svg > file, each svg file needs different scaling and X-Y translation.   > > With python or perl exec(), iirc, I can pass a string to my parent > module that could be used to call the correct svg import module, like > this if exec() existed in OpenSCAD: > > module parent(var) { exec(str("import_", var));} > > > I'm looking for something more clever than a 50 line conditional > statement. Just to confirm what you're looking for: You have a string that has a module name, and you want to call that module.  Right? There's currently no way to do that. For your particular case, where you have n SVG files with n scale factors and n translations, you could put all of that in an array, something like: NAME = 0; SCALE = 1; TRANSLATE = 2; list = [ [ "aaa", 1.0, [3,4] ], [ "bbb", 1.2, [0,0] ], [ "ccc", [1, 2], [-5, -5] ], ... ]; for (e = list) { translate(e[TRANSLATE]) scale(e[SCALE]) import(str(e[NAME], ".svg")); } ... but there's nothing that will help you with the general case of dispatching to modules. PR#4478 has a couple of features that might be helpful, but has no ETA. Module references: list = [ module () { translate([3,4]) import("aaa.svg"); }, module () { scale(1.2) import("bbb.svg"); }, module () { translate([-5,-5]) scale([1,2]) import("ccc.svg"); }, ... ]; for (m = list) m(); Geometry values: list = [ {{ translate([3,4]) import("aaa.svg"); }}, {{ scale(1.2) import("bbb.svg"); }}, {{ translate([-5,-5]) scale([1,2]) import("ccc.svg"); }}, ... ]; Objects/dictionaries/associative arrays: list = { aaa: { t: [3,4] }, bbb: { s: 1.2 }, ccc: { t: [-5,-5], s: [1,2]), ... }; for (n = list) { e = list[n]; translate(e.t ? e.t : [0,0]) scale(e.s ? e.s : 1) import(str(n, ".svg")); } and of course objects/dictionaries/associative arrays can contain module references and geometry values. It doesn't have "given a string, call the named module", though I think that would be pretty straightforward:  there could be a function that, given a string, would look it up and return a module reference.