discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Possible to name a 'chunk' of code, then use its name?

JB
Jordan Brown
Wed, Feb 9, 2022 11:30 PM

[ Nothing to do with Terry's aliasing question.  Further discussion over
on my "Volumetric color" thread, please. ]

On 2/9/2022 2:12 PM, David Schooley wrote:

Since multi-color printing came up recently without me following the
conversation too closely…

Modules can make multi-color printing easy. The part to be done in
color #1 is one module and the part done in color #2 is another
module. Part #2 is subtracted from part #1 to get the stl for the
first color, and part #2 is used by itself to get the stl for the
second color.

The reason that I thought it needed work was that you want to be able to
specify colors arbitrarily deeply nested in the design.  The "head"
module needs to have flesh-colored skin, hair-colored hair, and
eye-colored eyes.  And the "eye" module might have three or more colors
itself.

And then, given the "head" module, you need to extract just the
iris-colored part of the eyes into a separate STL.

[ Nothing to do with Terry's aliasing question.  Further discussion over on my "Volumetric color" thread, please. ] On 2/9/2022 2:12 PM, David Schooley wrote: > Since multi-color printing came up recently without me following the > conversation too closely… > > Modules can make multi-color printing easy. The part to be done in > color #1 is one module and the part done in color #2 is another > module. Part #2 is subtracted from part #1 to get the stl for the > first color, and part #2 is used by itself to get the stl for the > second color. The reason that I thought it needed work was that you want to be able to specify colors arbitrarily deeply nested in the design.  The "head" module needs to have flesh-colored skin, hair-colored hair, and eye-colored eyes.  And the "eye" module might have three or more colors itself. And then, given the "head" module, you need to extract just the iris-colored part of the eyes into a separate STL.
FS
FF Systems
Wed, Feb 9, 2022 11:33 PM

You place this down near the bottom of your text file:

module unique_name(){
<put your text here... it needs to be lines of openSCAD keywords with
proper syntax, just like the lines you are copying from your existing
project>
}

Then you use "unique_name();" to instantiate code in the module.

You don't need to know any more than that, even tho there is more you can
do.

This will NOT do a simple text replacement.  Everything inside the module
needs to be complete openSCAD statements as well as the code that surrounds
the instantiation.

Also, variable handling won't pass into/out of the module like a macro
would allow (in general, openSCAD variables don't behave like they do in a
software program).  Perhaps this is part of why you are resistant?  I admit
that I am having difficulty "seeing" with your eyes regarding this.  This
suggests that there is a disconnect here that has yet to be identified (at
least WRT to my understanding, or lack thereof).

On Wed, Feb 9, 2022 at 4:49 PM jon jon@jonbondy.com wrote:

I keep thinking that you want a module().  I use them all the time, and
they are trivial to write.

On 2/9/2022 5:43 PM, terrypingm@gmail.com wrote:

Yes, that’s the sort of thing I can do with Macro Express Pro. But
like all approaches of this genre it nets down to

  1. Copying the code (the ‘chunk’ of my OP)
  2. Opening the external tool and pasting it in
  3. Assigning a brief name to it (and enduring it's unique)
  4. Returning to OpenSCAD
  5. Using that name to automatically type the original chunk

Which is not what I described. I want
A. The abbreviation to be created in OpenSCAD
B.  Used as an alias for the code, not to reproduce it.


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

You place this down near the bottom of your text file: module unique_name(){ <put your text here... it needs to be lines of openSCAD keywords with proper syntax, just like the lines you are copying from your existing project> } Then you use "unique_name();" to instantiate code in the module. You don't need to know any more than that, even tho there is more you can do. This will NOT do a simple text replacement. Everything inside the module needs to be complete openSCAD statements as well as the code that surrounds the instantiation. Also, variable handling won't pass into/out of the module like a macro would allow (in general, openSCAD variables don't behave like they do in a software program). Perhaps this is part of why you are resistant? I admit that I am having difficulty "seeing" with your eyes regarding this. This suggests that there is a disconnect here that has yet to be identified (at least WRT to my understanding, or lack thereof). On Wed, Feb 9, 2022 at 4:49 PM jon <jon@jonbondy.com> wrote: > I keep thinking that you want a module(). I use them all the time, and > they are trivial to write. > > > On 2/9/2022 5:43 PM, terrypingm@gmail.com wrote: > > Yes, that’s the sort of thing I can do with Macro Express Pro. But > > like all approaches of this genre it nets down to > > 1. Copying the code (the ‘chunk’ of my OP) > > 2. Opening the external tool and pasting it in > > 3. Assigning a brief name to it (and enduring it's unique) > > 4. Returning to OpenSCAD > > 5. Using that name to automatically type the original chunk > > > > Which is not what I described. I want > > A. The abbreviation to be created in OpenSCAD > > B. Used as an alias for the code, not to reproduce it. > > > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Wed, Feb 9, 2022 11:44 PM

On 2/9/2022 2:43 PM, terrypingm@gmail.com wrote:

Which is not what I described. I want
A. The abbreviation to be created in OpenSCAD
B.  Used as an alias for the code, not to reproduce it.

Yep.

That's exactly what modules are.

You have, say:

difference() {
    cylinder(h=10, d=1, center=true);
    cylinder(h=11, d=0.9, center=true);
}

You want to give that a name - say, "pipe".

module pipe() {
    difference() {
        cylinder(h=10, d=1, center=true);
        cylinder(h=11, d=0.9, center=true);
    }
}

You want to use that definition in multiple places:

pipe();
translate([10,0,0]) pipe();

This isn't quite as simple as it can possibly get, but it's close.

Any scheme that would address your need will have a few things:

  • A way to say "I want to make an alias".  That's the "module" keyword.
  • A way to name your "alias".  That's the "pipe" after "module".
  • A way to mark off the operations that will be the content of your
    "alias".  That's the { ... } braces.  (They aren't actually required
    if your object consists of a single operation, as this one does, but
    I'd recommend always including them because it will help to reduce
    errors.)
  • A way to invoke your "alias".  That's the uses of "pipe" in the
    later block.

The only thing that could be simpler would be if the parentheses weren't
required.

Modules can do more than this, of course, but you can use them in this
simple way and it's perfectly OK.

The only thing that I can come up with that modules can't do is to set
variables to be used later, but if you're still struggling with modules
then I don't think that's a problem for you yet.


And you are absolutely correct in not wanting to use an "automatic
typing" answer, because then if you decide that you need to make a
change, you have to somehow hunt down all of the uses and re-generate them.

"Automatic typing" might be useful if you are going to want to edit each
example so that they are different.  Even then, I'd look at a
parameterized module instead.

On 2/9/2022 2:43 PM, terrypingm@gmail.com wrote: > Which is not what I described. I want > A. The abbreviation to be created in OpenSCAD > B.  Used as an alias for the code, not to reproduce it. Yep. That's *exactly* what modules are. You have, say: difference() {     cylinder(h=10, d=1, center=true);     cylinder(h=11, d=0.9, center=true); } You want to give that a name - say, "pipe". module pipe() { difference() {     cylinder(h=10, d=1, center=true);     cylinder(h=11, d=0.9, center=true); } } You want to use that definition in multiple places: pipe(); translate([10,0,0]) pipe(); This isn't quite as simple as it can possibly get, but it's close. Any scheme that would address your need will have a few things: * A way to say "I want to make an alias".  That's the "module" keyword. * A way to name your "alias".  That's the "pipe" after "module". * A way to mark off the operations that will be the content of your "alias".  That's the { ... } braces.  (They aren't actually required if your object consists of a single operation, as this one does, but I'd recommend always including them because it will help to reduce errors.) * A way to invoke your "alias".  That's the uses of "pipe" in the later block. The only thing that could be simpler would be if the parentheses weren't required. Modules can do *more* than this, of course, but you can use them in this simple way and it's perfectly OK. The only thing that I can come up with that modules *can't* do is to set variables to be used later, but if you're still struggling with modules then I don't think that's a problem for you yet. --- And you are absolutely correct in not wanting to use an "automatic typing" answer, because then if you decide that you need to make a change, you have to somehow hunt down all of the uses and re-generate them. "Automatic typing" might be useful if you are going to want to edit each example so that they are different.  Even then, I'd look at a parameterized module instead.