discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

First-class modules?

PW
Philip White
Fri, Apr 22, 2022 7:07 PM

Hello,

I am a beginner at OpenSCAD, though a big fan already. I use it to develop keys for low-security locks.

I am a software engineer by trade, so I really appreciate OpenSCAD’s code-first philosophy. As I am familiar with other programming languages, I try to apply their techniques to OpenSCAD -- sometimes successfully, sometimes not.

One challenge I ran into is modularizing my code. I have a model that has a set of interchangeable parts. I represent these parts as modules. I want to produce a model with part A, and a model with part B, so I’d like to pass module A or module B into the overall model. But this does not seem to be possible. Here’s an example:

// interchangeable parts start
module inner_cylinder_large() {
cylinder(20, 7, 7);
}

module inner_cylinder_small() {
cylinder(20, 3, 3);
}
// interchangeable parts end

// overall shape does not care which inner part is used
module overall_shape(inner_cylinder) {
cylinder(10, 10, 10);
inner_cylinder();
}

// render the whole shape, plugging in whatever inner part you want
overall_shape(inner_cylinder_small);

Is there a way to make this work, whether through modules, functions, or some other approach?

Thank you.


Philip

Hello, I am a beginner at OpenSCAD, though a big fan already. I use it to develop keys for low-security locks. I am a software engineer by trade, so I really appreciate OpenSCAD’s code-first philosophy. As I am familiar with other programming languages, I try to apply their techniques to OpenSCAD -- sometimes successfully, sometimes not. One challenge I ran into is modularizing my code. I have a model that has a set of interchangeable parts. I represent these parts as modules. I want to produce a model with part A, and a model with part B, so I’d like to pass module A or module B into the overall model. But this does not seem to be possible. Here’s an example: // interchangeable parts start module inner_cylinder_large() { cylinder(20, 7, 7); } module inner_cylinder_small() { cylinder(20, 3, 3); } // interchangeable parts end // overall shape does not care which inner part is used module overall_shape(inner_cylinder) { cylinder(10, 10, 10); inner_cylinder(); } // render the whole shape, plugging in whatever inner part you want overall_shape(inner_cylinder_small); Is there a way to make this work, whether through modules, functions, or some other approach? Thank you. — Philip
MM
Michael Möller
Fri, Apr 22, 2022 7:21 PM

My approach :

module choose(PartId) {
if ( PartId=="large") cylinder(d=10,h=20);
if ( PartId=="small") cylinder(d=5,h=20);
}

/* main */
difference() {
cylinder(d=30,h=15);
choose("large") ;
}

You cant pass modulenames as arguments, AFAIK

On Fri, 22 Apr 2022 at 21:07, Philip White philip@mailworks.org wrote:

Hello,

I am a beginner at OpenSCAD, though a big fan already. I use it to develop
keys for low-security locks.

I am a software engineer by trade, so I really appreciate OpenSCAD’s
code-first philosophy. As I am familiar with other programming languages, I
try to apply their techniques to OpenSCAD -- sometimes successfully,
sometimes not.

One challenge I ran into is modularizing my code. I have a model that has
a set of interchangeable parts. I represent these parts as modules. I want
to produce a model with part A, and a model with part B, so I’d like to
pass module A or module B into the overall model. But this does not seem to
be possible. Here’s an example:

// interchangeable parts start
module inner_cylinder_large() {
cylinder(20, 7, 7);
}

module inner_cylinder_small() {
cylinder(20, 3, 3);
}
// interchangeable parts end

// overall shape does not care which inner part is used
module overall_shape(inner_cylinder) {
cylinder(10, 10, 10);
inner_cylinder();
}

// render the whole shape, plugging in whatever inner part you want
overall_shape(inner_cylinder_small);

Is there a way to make this work, whether through modules, functions, or
some other approach?

Thank you.


Philip


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

My approach : module choose(PartId) { if ( PartId=="large") cylinder(d=10,h=20); if ( PartId=="small") cylinder(d=5,h=20); } /* main */ difference() { cylinder(d=30,h=15); choose("large") ; } You cant pass modulenames as arguments, AFAIK On Fri, 22 Apr 2022 at 21:07, Philip White <philip@mailworks.org> wrote: > Hello, > > I am a beginner at OpenSCAD, though a big fan already. I use it to develop > keys for low-security locks. > > I am a software engineer by trade, so I really appreciate OpenSCAD’s > code-first philosophy. As I am familiar with other programming languages, I > try to apply their techniques to OpenSCAD -- sometimes successfully, > sometimes not. > > One challenge I ran into is modularizing my code. I have a model that has > a set of interchangeable parts. I represent these parts as modules. I want > to produce a model with part A, and a model with part B, so I’d like to > pass module A or module B into the overall model. But this does not seem to > be possible. Here’s an example: > > // interchangeable parts start > module inner_cylinder_large() { > cylinder(20, 7, 7); > } > > module inner_cylinder_small() { > cylinder(20, 3, 3); > } > // interchangeable parts end > > // overall shape does not care which inner part is used > module overall_shape(inner_cylinder) { > cylinder(10, 10, 10); > inner_cylinder(); > } > > // render the whole shape, plugging in whatever inner part you want > overall_shape(inner_cylinder_small); > > Is there a way to make this work, whether through modules, functions, or > some other approach? > > Thank you. > > — > Philip > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
AC
A. Craig West
Fri, Apr 22, 2022 7:37 PM

Modules get passed as chil;dren, instead of as arguments. so:

// interchangeable parts start
module inner_cylinder_large() {
cylinder(20, 7, 7);
}

module inner_cylinder_small() {
cylinder(20, 3, 3);
}
// interchangeable parts end

// overall shape does not care which inner part is used
module overall_shape() {
cylinder(10, 10, 10);
children();
}

// render the whole shape, plugging in whatever inner part you want
overall_shape() {
inner_cylinder_small();
}

On Fri, Apr 22, 2022 at 3:07 PM Philip White philip@mailworks.org wrote:

Hello,

I am a beginner at OpenSCAD, though a big fan already. I use it to develop
keys for low-security locks.

I am a software engineer by trade, so I really appreciate OpenSCAD’s
code-first philosophy. As I am familiar with other programming languages, I
try to apply their techniques to OpenSCAD -- sometimes successfully,
sometimes not.

One challenge I ran into is modularizing my code. I have a model that has
a set of interchangeable parts. I represent these parts as modules. I want
to produce a model with part A, and a model with part B, so I’d like to
pass module A or module B into the overall model. But this does not seem to
be possible. Here’s an example:

// interchangeable parts start
module inner_cylinder_large() {
cylinder(20, 7, 7);
}

module inner_cylinder_small() {
cylinder(20, 3, 3);
}
// interchangeable parts end

// overall shape does not care which inner part is used
module overall_shape(inner_cylinder) {
cylinder(10, 10, 10);
inner_cylinder();
}

// render the whole shape, plugging in whatever inner part you want
overall_shape(inner_cylinder_small);

Is there a way to make this work, whether through modules, functions, or
some other approach?

Thank you.


Philip


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

Modules get passed as chil;dren, instead of as arguments. so: // interchangeable parts start module inner_cylinder_large() { cylinder(20, 7, 7); } module inner_cylinder_small() { cylinder(20, 3, 3); } // interchangeable parts end // overall shape does not care which inner part is used module overall_shape() { cylinder(10, 10, 10); children(); } // render the whole shape, plugging in whatever inner part you want overall_shape() { inner_cylinder_small(); } On Fri, Apr 22, 2022 at 3:07 PM Philip White <philip@mailworks.org> wrote: > Hello, > > I am a beginner at OpenSCAD, though a big fan already. I use it to develop > keys for low-security locks. > > I am a software engineer by trade, so I really appreciate OpenSCAD’s > code-first philosophy. As I am familiar with other programming languages, I > try to apply their techniques to OpenSCAD -- sometimes successfully, > sometimes not. > > One challenge I ran into is modularizing my code. I have a model that has > a set of interchangeable parts. I represent these parts as modules. I want > to produce a model with part A, and a model with part B, so I’d like to > pass module A or module B into the overall model. But this does not seem to > be possible. Here’s an example: > > // interchangeable parts start > module inner_cylinder_large() { > cylinder(20, 7, 7); > } > > module inner_cylinder_small() { > cylinder(20, 3, 3); > } > // interchangeable parts end > > // overall shape does not care which inner part is used > module overall_shape(inner_cylinder) { > cylinder(10, 10, 10); > inner_cylinder(); > } > > // render the whole shape, plugging in whatever inner part you want > overall_shape(inner_cylinder_small); > > Is there a way to make this work, whether through modules, functions, or > some other approach? > > Thank you. > > — > Philip > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
TP
Torsten Paul
Fri, Apr 22, 2022 8:25 PM

On 22.04.22 21:07, Philip White wrote:

I want to produce a model with part A, and a model with
part B, so I’d like to pass module A or module B into > the overall model. But this does not seem to be possible.

Currently there's only first-class functions, but not
modules.

Modules may happen at some point too, but that's likely
going to take some considerable time unless someone finds
time to dive into the topic.

Some discussion on that in:
https://github.com/openscad/openscad/pull/3087

The way to pass modules right now is a bit restricted
and uses the special children() module.

It's not as general but in some cases allows quite nice
composition of models.

Simple example:

$fa = 4; $fs = 0.4;

module base(h) {
cylinder(h = h, r = 10);
color("red")
translate([0, 0, h])
children();
}

module topA(r) {
translate([0, 0, r]) sphere(r);
}

module topB(w, h) {
linear_extrude(h) square(w, center = true);
}

translate([-12, 0, 0]) base(5) topA(3);

translate([12, 0, 0]) base(5) topB(3, 10);

ciao,
Torsten.

On 22.04.22 21:07, Philip White wrote: > I want to produce a model with part A, and a model with > part B, so I’d like to pass module A or module B into > the overall model. But this does not seem to be possible. Currently there's only first-class functions, but not modules. Modules may happen at some point too, but that's likely going to take some considerable time unless someone finds time to dive into the topic. Some discussion on that in: https://github.com/openscad/openscad/pull/3087 The way to pass modules right now is a bit restricted and uses the special children() module. It's not as general but in some cases allows quite nice composition of models. Simple example: $fa = 4; $fs = 0.4; module base(h) { cylinder(h = h, r = 10); color("red") translate([0, 0, h]) children(); } module topA(r) { translate([0, 0, r]) sphere(r); } module topB(w, h) { linear_extrude(h) square(w, center = true); } translate([-12, 0, 0]) base(5) topA(3); translate([12, 0, 0]) base(5) topB(3, 10); ciao, Torsten.
PW
Philip White
Tue, Apr 26, 2022 11:14 PM

Thanks, everyone. This has been very helpful! I was able to refactor my code to be much more pleasant. :)

On Apr 22, 2022, at 13:25, Torsten Paul Torsten.Paul@gmx.de wrote:

On 22.04.22 21:07, Philip White wrote:

I want to produce a model with part A, and a model with
part B, so I’d like to pass module A or module B into > the overall model. But this does not seem to be possible.

Currently there's only first-class functions, but not
modules.

Modules may happen at some point too, but that's likely
going to take some considerable time unless someone finds
time to dive into the topic.

Some discussion on that in:
https://github.com/openscad/openscad/pull/3087

The way to pass modules right now is a bit restricted
and uses the special children() module.

It's not as general but in some cases allows quite nice
composition of models.

Simple example:

$fa = 4; $fs = 0.4;

module base(h) {
cylinder(h = h, r = 10);
color("red")
translate([0, 0, h])
children();
}

module topA(r) {
translate([0, 0, r]) sphere(r);
}

module topB(w, h) {
linear_extrude(h) square(w, center = true);
}

translate([-12, 0, 0]) base(5) topA(3);

translate([12, 0, 0]) base(5) topB(3, 10);

ciao,
Torsten.


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

Thanks, everyone. This has been very helpful! I was able to refactor my code to be much more pleasant. :) > On Apr 22, 2022, at 13:25, Torsten Paul <Torsten.Paul@gmx.de> wrote: > > On 22.04.22 21:07, Philip White wrote: >> I want to produce a model with part A, and a model with >> part B, so I’d like to pass module A or module B into > the overall model. But this does not seem to be possible. > > Currently there's only first-class functions, but not > modules. > > Modules may happen at some point too, but that's likely > going to take some considerable time unless someone finds > time to dive into the topic. > > Some discussion on that in: > https://github.com/openscad/openscad/pull/3087 > > The way to pass modules right now is a bit restricted > and uses the special children() module. > > It's not as general but in some cases allows quite nice > composition of models. > > Simple example: > > > $fa = 4; $fs = 0.4; > > module base(h) { > cylinder(h = h, r = 10); > color("red") > translate([0, 0, h]) > children(); > } > > module topA(r) { > translate([0, 0, r]) sphere(r); > } > > module topB(w, h) { > linear_extrude(h) square(w, center = true); > } > > translate([-12, 0, 0]) base(5) topA(3); > > translate([12, 0, 0]) base(5) topB(3, 10); > > > ciao, > Torsten. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org