discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

passing geometry or functions into a module()

JB
Jon Bondy
Thu, Jan 16, 2025 1:27 PM

OpenSCAD is complex enough that I usually stick to a subset of all of
the features.  I'm sure this has been discussed in the past, but I
ignored it, since it was unnecessary at the time. Times change.

I want to create 3 variants on some geometry.  The key difference
between the 3 variants lies in 3 different 2D shapes that are
rotate_extruded inside the main module().  At the moment, I have three
lines of code, and I carefully comment out 2 of the 3 each time I run
the program.  It works but is awkward.

I tried passing the 2D geometry in as a parameter, but that failed.  I
am pretty sure that passing in modules that generate the 2D geometry
will also fail.

What is the best current approach please?

Thanks!

Jon

--
This email has been checked for viruses by AVG antivirus software.
www.avg.com

OpenSCAD is complex enough that I usually stick to a subset of all of the features.  I'm sure this has been discussed in the past, but I ignored it, since it was unnecessary at the time. Times change. I want to create 3 variants on some geometry.  The key difference between the 3 variants lies in 3 different 2D shapes that are rotate_extruded inside the main module().  At the moment, I have three lines of code, and I carefully comment out 2 of the 3 each time I run the program.  It works but is awkward. I tried passing the 2D geometry in as a parameter, but that failed.  I am pretty sure that passing in modules that generate the 2D geometry will also fail. What is the best current approach please? Thanks! Jon -- This email has been checked for viruses by AVG antivirus software. www.avg.com
GS
Guenther Sohler
Thu, Jan 16, 2025 1:48 PM

In OpensCAD geometries are no 1st class objects, yo cannot pass geometry as
arguments, but there is a workaround

you can do:

module mymodule(a,b)
{
square(a,b);
child(1); // accessing 1st child here
}

mymodule(a,b) {
circle(1); // geometry passed as child
}

hope that helps

On Thu, Jan 16, 2025 at 2:27 PM Jon Bondy via Discuss <
discuss@lists.openscad.org> wrote:

OpenSCAD is complex enough that I usually stick to a subset of all of
the features.  I'm sure this has been discussed in the past, but I
ignored it, since it was unnecessary at the time. Times change.

I want to create 3 variants on some geometry.  The key difference
between the 3 variants lies in 3 different 2D shapes that are
rotate_extruded inside the main module().  At the moment, I have three
lines of code, and I carefully comment out 2 of the 3 each time I run
the program.  It works but is awkward.

I tried passing the 2D geometry in as a parameter, but that failed.  I
am pretty sure that passing in modules that generate the 2D geometry
will also fail.

What is the best current approach please?

Thanks!

Jon

--
This email has been checked for viruses by AVG antivirus software.
www.avg.com


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

In OpensCAD geometries are no 1st class objects, yo cannot pass geometry as arguments, but there is a workaround you can do: module mymodule(a,b) { square(a,b); child(1); // accessing 1st child here } mymodule(a,b) { circle(1); // geometry passed as child } hope that helps On Thu, Jan 16, 2025 at 2:27 PM Jon Bondy via Discuss < discuss@lists.openscad.org> wrote: > OpenSCAD is complex enough that I usually stick to a subset of all of > the features. I'm sure this has been discussed in the past, but I > ignored it, since it was unnecessary at the time. Times change. > > I want to create 3 variants on some geometry. The key difference > between the 3 variants lies in 3 different 2D shapes that are > rotate_extruded inside the main module(). At the moment, I have three > lines of code, and I carefully comment out 2 of the 3 each time I run > the program. It works but is awkward. > > I tried passing the 2D geometry in as a parameter, but that failed. I > am pretty sure that passing in modules that generate the 2D geometry > will also fail. > > What is the best current approach please? > > Thanks! > > Jon > > > -- > This email has been checked for viruses by AVG antivirus software. > www.avg.com > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Thu, Jan 16, 2025 2:29 PM

On 1/16/2025 5:48 AM, Guenther Sohler via Discuss wrote:

In OpensCAD geometries are no 1st class objects, yo cannot pass
geometry as arguments, but there is a workaround

It's not a workaround.  It's a designed feature for achieving this exact
goal.  It's just not using geometry-as-data-values.

And functions are first-class objects, or close to it.  A function
declared with

function foo(arg) = ...;

can't be passed around as data, but a function expression can:

myfunc = function (args) ...;

Modules are not (yet) first class.  PR#4478 has first-class geometry and
modules.

   child(1); // accessing 1st child here

child() is deprecated; children() is the preferred name.

On 1/16/2025 5:48 AM, Guenther Sohler via Discuss wrote: > In OpensCAD geometries are no 1st class objects, yo cannot pass > geometry as arguments, but there is a workaround It's not a workaround.  It's a designed feature for achieving this exact goal.  It's just not using geometry-as-data-values. And functions *are* first-class objects, or close to it.  A function declared with function foo(arg) = ...; can't be passed around as data, but a function expression can: myfunc = function (args) ...; Modules are not (yet) first class.  PR#4478 has first-class geometry and modules. >    child(1); // accessing 1st child here child() is deprecated; children() is the preferred name.