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
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
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
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.
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