discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Saving and retrieving objects

JW
Joe Weinpert
Thu, May 5, 2022 11:33 PM

I will be needing to make objects as displayed in the picture.  Many
different shapes.  These will be used throughout different projects for
building things.

The object you see was created with an inner and outer array of X,Y vectors
each having variable names (i.e. shapeOuter, shapeInner).  Each of these
were used with the difference() and offset_sweep() functions commands to
create the object.

How can I create callable SCADs for this and other shaped objects so that
they may be included into different projects without the need to recreate
each object in every project that receives it?  In other words, just
including or calling or using them.

Their use will be in different positions in different projects so they will
need to be called into use and positioned correctly.

How do I avoid overwriting variable names that created them with this
procedure?

[image: example.png]

I will be needing to make objects as displayed in the picture. Many different shapes. These will be used throughout different projects for building things. The object you see was created with an inner and outer array of X,Y vectors each having variable names (i.e. shapeOuter, shapeInner). Each of these were used with the difference() and offset_sweep() functions commands to create the object. How can I create callable SCADs for this and other shaped objects so that they may be included into different projects without the need to recreate each object in every project that receives it? In other words, just including or calling or using them. Their use will be in different positions in different projects so they will need to be called into use and positioned correctly. How do I avoid overwriting variable names that created them with this procedure? [image: example.png]
JB
Jordan Brown
Fri, May 6, 2022 2:03 AM

[ Punched the wrong button and sent this privately first.  Joe, sorry
for the duplicate. ]

There are three related parts of doing this sort of thing.

First, you probably want to put each of your things into a module.

module mySphereThing(d=10) {
    difference() {
        sphere(d=d);
        cube([d/2, d/2, d*2], center=true);
    }
}

module myCubeThing(d=10) {
    difference() {
        cube(d, center=true);
        cylinder(h=d*2, d=d/2, center=true);
    }
}

and invoke them like so:

translate([-20,0,0]) mySphereThing();
translate([20,0,0]) myCubeThing(15);

Second, one of two similar options.  Both start with putting your
definitions into a separate file - call it myThings.scad.

You can refer to a file using either

include <myThings.scad>

or

use <myThings.scad>

In either case, you can then refer to your modules from the calling file

  • call it main.scad.  With "include", it is exactly as if the text of
    myThing.scad had been inserted in main.scad.  With "use", each file has
    its own set of global variables - main.scad cannot see the global
    variables from myThings.scad, and myThings.scad cannot see the global
    variables from main.scad.  With "use", the only interaction available is
    that main.scad can call modules and functions defined by myThings.scad.

Which of the two styles is right depends on your needs.  If you need to
share variables, you need to use "include".  If you need to keep them
separate, you need to use "use".

The general intent is that "use" is good for libraries.  It allows those
library files to be independently executable - since their top level
geometry is not executed at "use" time, they can include demonstration /
test invocations.  It avoids polluting the caller's namespace with the
library's variables.  It avoids having the library accidentally call the
main program's functions or modules.

In complex cases "use" has performance problems, and its interactions
with global-level $ variables are ... subtle.  However, for simple cases
neither of those is a significant factor.

Where should you put your library file myThings.scad?  There are, I
believe, four options.  The details depend on your platform, but they
are basically:

  • In the same directory as the main file(s).
  • In the directory or directories named by the OPENSCADPATH
    environment variable.
  • In the per-user library directory.  See File / Show Library Folder. 
    On Windows, this is <user>\Documents\OpenSCAD\libraries.
  • In the system-wide library directory.  On Windows, this is <OpenSCAD install directory>\libraries.
[ Punched the wrong button and sent this privately first.  Joe, sorry for the duplicate. ] There are three related parts of doing this sort of thing. First, you probably want to put each of your things into a module. module mySphereThing(d=10) { difference() { sphere(d=d); cube([d/2, d/2, d*2], center=true); } } module myCubeThing(d=10) { difference() { cube(d, center=true); cylinder(h=d*2, d=d/2, center=true); } } and invoke them like so: translate([-20,0,0]) mySphereThing(); translate([20,0,0]) myCubeThing(15); Second, one of two similar options.  Both start with putting your definitions into a separate file - call it myThings.scad. You can refer to a file using either include <myThings.scad> or use <myThings.scad> In either case, you can then refer to your modules from the calling file - call it main.scad.  With "include", it is exactly as if the text of myThing.scad had been inserted in main.scad.  With "use", each file has its own set of global variables - main.scad cannot see the global variables from myThings.scad, and myThings.scad cannot see the global variables from main.scad.  With "use", the only interaction available is that main.scad can call modules and functions defined by myThings.scad. Which of the two styles is right depends on your needs.  If you need to share variables, you need to use "include".  If you need to keep them separate, you need to use "use". The general intent is that "use" is good for libraries.  It allows those library files to be independently executable - since their top level geometry is not executed at "use" time, they can include demonstration / test invocations.  It avoids polluting the caller's namespace with the library's variables.  It avoids having the library accidentally call the main program's functions or modules. In complex cases "use" has performance problems, and its interactions with global-level $ variables are ... subtle.  However, for simple cases neither of those is a significant factor. Where should you put your library file myThings.scad?  There are, I believe, four options.  The details depend on your platform, but they are basically: * In the same directory as the main file(s). * In the directory or directories named by the OPENSCADPATH environment variable. * In the per-user library directory.  See File / Show Library Folder.  On Windows, this is <user>\Documents\OpenSCAD\libraries. * In the system-wide library directory.  On Windows, this is <OpenSCAD install directory>\libraries.
PS
Peter-Frank Spierenburg
Fri, May 6, 2022 1:10 PM

Have you looked into modules?

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Modules

Peter-Frank Spierenburg

Sent from Outlookhttp://aka.ms/weboutlook


From: Joe Weinpert joe.weinpert@gmail.com
Sent: May 5, 2022 5:33 PM
To: OpenSCAD general discussion discuss@lists.openscad.org
Subject: [OpenSCAD] Saving and retrieving objects

I will be needing to make objects as displayed in the picture.  Many different shapes.  These will be used throughout different projects for building things.

The object you see was created with an inner and outer array of X,Y vectors each having variable names (i.e. shapeOuter, shapeInner).  Each of these were used with the difference() and offset_sweep() functions commands to create the object.

How can I create callable SCADs for this and other shaped objects so that they may be included into different projects without the need to recreate each object in every project that receives it?  In other words, just including or calling or using them.

Their use will be in different positions in different projects so they will need to be called into use and positioned correctly.

How do I avoid overwriting variable names that created them with this procedure?

[example.png]

Have you looked into modules? https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Modules Peter-Frank Spierenburg Sent from Outlook<http://aka.ms/weboutlook> ________________________________ From: Joe Weinpert <joe.weinpert@gmail.com> Sent: May 5, 2022 5:33 PM To: OpenSCAD general discussion <discuss@lists.openscad.org> Subject: [OpenSCAD] Saving and retrieving objects I will be needing to make objects as displayed in the picture. Many different shapes. These will be used throughout different projects for building things. The object you see was created with an inner and outer array of X,Y vectors each having variable names (i.e. shapeOuter, shapeInner). Each of these were used with the difference() and offset_sweep() functions commands to create the object. How can I create callable SCADs for this and other shaped objects so that they may be included into different projects without the need to recreate each object in every project that receives it? In other words, just including or calling or using them. Their use will be in different positions in different projects so they will need to be called into use and positioned correctly. How do I avoid overwriting variable names that created them with this procedure? [example.png]