discuss@lists.openscad.org

OpenSCAD general discussion

View all threads

first class modules WIP

K
kwikius@yahoo.com
Sun, Sep 4, 2022 5:33 PM

I Have now managed to get some actual openscad output using the first class feature into the CSG tree and am sure now this would be a very useful  feature.

The  example demonstrates  putting modules in an array and returning from function.(Full code available to view here https://github.com/kwikius/openscad/blob/WIP-first-class-module/tests/data/scad/experimental/first_class_module/module_array.scad) ( pic of output attached)

// Example put modules in array and return modules from function
geometry = [
    // [rotation , translation , module name ]
    [[0,0,0], [0,0,0], module my_sphere],
    [[0,45,0],[0,2.5 * rscale,0], module my_cylinder],
    [[45,45,0],[0,-2.5 * rscale,0], module my_cube]
];
/*
instantiate the modules defined in the geometry array
*/
for ( i = [0 :1: 2]){
   translate(get_translation(geometry,i)){
      rotate(get_rotation(geometry,i)){
          modalias = get_module(geometry,i);  // module return from function
          modalias(); // instantiate the module
      }
   }
}

The source code for the executable is on the WIP-first-class-module branch if you want to try it:w

https://github.com/kwikius/openscad/tree/WIP-first-class-module

though it won’t use arguments yet, apart from in the way described belo

I found the following syntax useful

my_cube = module cube;  // alias the builtin cube

Where there are no arguments, the arguments in the instantiation call are simply passed through to the module.

my_cube([1,2,3],center = true);

looking to implement parameters and arguments next.

I Have now managed to get some actual openscad output using the first class feature into the CSG tree and am sure now this would be a very useful feature. The example demonstrates putting modules in an array and returning from function.(Full code available to view here https://github.com/kwikius/openscad/blob/WIP-first-class-module/tests/data/scad/experimental/first_class_module/module_array.scad) ( pic of output attached) ``` // Example put modules in array and return modules from function ``` ``` geometry = [ ``` ``` // [rotation , translation , module name ] ``` ``` [[0,0,0], [0,0,0], module my_sphere], ``` ``` [[0,45,0],[0,2.5 * rscale,0], module my_cylinder], ``` ``` [[45,45,0],[0,-2.5 * rscale,0], module my_cube] ``` ``` ]; ``` ``` /* ``` ``` instantiate the modules defined in the geometry array ``` ``` */ ``` ``` for ( i = [0 :1: 2]){ ``` ``` translate(get_translation(geometry,i)){ ``` ``` rotate(get_rotation(geometry,i)){ ``` ``` modalias = get_module(geometry,i); // module return from function ``` ``` modalias(); // instantiate the module ``` ``` } ``` ``` } ``` ``` } ``` The source code for the executable is on the WIP-first-class-module branch if you want to try it:w [https://github.com/kwikius/openscad/tree/WIP-first-class-module](https://github.com/kwikius/openscad/tree/WIP-first-class-module "WIP-first-class-module") though it won’t use arguments yet, apart from in the way described belo I found the following syntax useful ``` my_cube = module cube; // alias the builtin cube ``` Where there are no arguments, the arguments in the instantiation call are simply passed through to the module. ``` my_cube([1,2,3],center = true); ``` looking to implement parameters and arguments next.