discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Questions about include statements

L
LenStruttmann
Sat, Apr 17, 2021 2:36 PM

Yet more newbie questions: I'm lovin' OpenSCAD and have progressed from
single parts, through simple assemblies, and now I am attempting a complex
assembly with multiple dependencies and I'm having trouble.  So, I'm
stepping back to create an organized structure and a set of best practices.

I develop each part in its own file which contains the part's module,
dimensional constants, and functions for computed values. My top level
assembly file references all of the part files using include statements and
puts it all together.

  1. My first question is about include statements:  How do I conditionally
    "include" library files?

Each part and the assembly needs a common library, for example,
metric_screws.scad. As I develop each part, each part file needs to have a
"include <metric_screws.scad>;".

The top level assembly includes metric_screws.scad, which then gets included
again as each part file is included.

So, how do I avoid these multiple includes of the same librry from the
top-level assembly?  Or, how do I conditionally execute include statements
in the part files?

  1. When would you use a "use" statement rather than an "include" statement?

Thanks

--
Sent from: http://forum.openscad.org/

Yet more newbie questions: I'm lovin' OpenSCAD and have progressed from single parts, through simple assemblies, and now I am attempting a complex assembly with multiple dependencies and I'm having trouble. So, I'm stepping back to create an organized structure and a set of best practices. I develop each part in its own file which contains the part's module, dimensional constants, and functions for computed values. My top level assembly file references all of the part files using include statements and puts it all together. 1. My first question is about include statements: How do I conditionally "include" library files? Each part and the assembly needs a common library, for example, metric_screws.scad. As I develop each part, each part file needs to have a "include <metric_screws.scad>;". The top level assembly includes metric_screws.scad, which then gets included again as each part file is included. So, how do I avoid these multiple includes of the same librry from the top-level assembly? Or, how do I conditionally execute include statements in the part files? 2. When would you use a "use" statement rather than an "include" statement? Thanks -- Sent from: http://forum.openscad.org/
NH
nop head
Sat, Apr 17, 2021 2:46 PM

You can't conditionally include files so I do end up with the same file
included over and over again.

The best I can do is to only put constants I need to make global in my
include files and a use of a second file that has all the functions and
modules. Used files are cached and only parsed once. For example I have a
screws.scad that I include and it uses screw.scad.

On Sat, 17 Apr 2021 at 15:36, LenStruttmann LenStruttmann@gmail.com wrote:

Yet more newbie questions: I'm lovin' OpenSCAD and have progressed from
single parts, through simple assemblies, and now I am attempting a complex
assembly with multiple dependencies and I'm having trouble.  So, I'm
stepping back to create an organized structure and a set of best practices.

I develop each part in its own file which contains the part's module,
dimensional constants, and functions for computed values. My top level
assembly file references all of the part files using include statements and
puts it all together.

  1. My first question is about include statements:  How do I conditionally
    "include" library files?

Each part and the assembly needs a common library, for example,
metric_screws.scad. As I develop each part, each part file needs to have a
"include <metric_screws.scad>;".

The top level assembly includes metric_screws.scad, which then gets
included again as each part file is included.

So, how do I avoid these multiple includes of the same librry from the
top-level assembly?  Or, how do I conditionally execute include statements
in the part files?

  1. When would you use a "use" statement rather than an "include"
    statement?

Thanks


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


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

You can't conditionally include files so I do end up with the same file included over and over again. The best I can do is to only put constants I need to make global in my include files and a use of a second file that has all the functions and modules. Used files are cached and only parsed once. For example I have a screws.scad that I include and it uses screw.scad. On Sat, 17 Apr 2021 at 15:36, LenStruttmann <LenStruttmann@gmail.com> wrote: > Yet more newbie questions: I'm lovin' OpenSCAD and have progressed from > single parts, through simple assemblies, and now I am attempting a complex > assembly with multiple dependencies and I'm having trouble. So, I'm > stepping back to create an organized structure and a set of best practices. > > I develop each part in its own file which contains the part's module, > dimensional constants, and functions for computed values. My top level > assembly file references all of the part files using include statements and > puts it all together. > > 1. My first question is about include statements: How do I conditionally > "include" library files? > > Each part and the assembly needs a common library, for example, > metric_screws.scad. As I develop each part, each part file needs to have a > "include <metric_screws.scad>;". > > The top level assembly includes metric_screws.scad, which then gets > included again as each part file is included. > > So, how do I avoid these multiple includes of the same librry from the > top-level assembly? Or, how do I conditionally execute include statements > in the part files? > > 2. When would you use a "use" statement rather than an "include" > statement? > > Thanks > > > ------------------------------ > Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> > at Nabble.com. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
AC
A. Craig West
Sat, Apr 17, 2021 2:58 PM

I usually split up constants into a screws.h which is included, and modules
in screws.scad which I 'use'

On Sat, 17 Apr 2021, 10:46 nop head, nop.head@gmail.com wrote:

You can't conditionally include files so I do end up with the same file
included over and over again.

The best I can do is to only put constants I need to make global in my
include files and a use of a second file that has all the functions and
modules. Used files are cached and only parsed once. For example I have a
screws.scad that I include and it uses screw.scad.

On Sat, 17 Apr 2021 at 15:36, LenStruttmann LenStruttmann@gmail.com
wrote:

Yet more newbie questions: I'm lovin' OpenSCAD and have progressed from
single parts, through simple assemblies, and now I am attempting a complex
assembly with multiple dependencies and I'm having trouble.  So, I'm
stepping back to create an organized structure and a set of best practices.

I develop each part in its own file which contains the part's module,
dimensional constants, and functions for computed values. My top level
assembly file references all of the part files using include statements and
puts it all together.

  1. My first question is about include statements:  How do I conditionally
    "include" library files?

Each part and the assembly needs a common library, for example,
metric_screws.scad. As I develop each part, each part file needs to have a
"include <metric_screws.scad>;".

The top level assembly includes metric_screws.scad, which then gets
included again as each part file is included.

So, how do I avoid these multiple includes of the same librry from the
top-level assembly?  Or, how do I conditionally execute include statements
in the part files?

  1. When would you use a "use" statement rather than an "include"
    statement?

Thanks


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


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


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

I usually split up constants into a screws.h which is included, and modules in screws.scad which I 'use' On Sat, 17 Apr 2021, 10:46 nop head, <nop.head@gmail.com> wrote: > You can't conditionally include files so I do end up with the same file > included over and over again. > > The best I can do is to only put constants I need to make global in my > include files and a use of a second file that has all the functions and > modules. Used files are cached and only parsed once. For example I have a > screws.scad that I include and it uses screw.scad. > > > > > > On Sat, 17 Apr 2021 at 15:36, LenStruttmann <LenStruttmann@gmail.com> > wrote: > >> Yet more newbie questions: I'm lovin' OpenSCAD and have progressed from >> single parts, through simple assemblies, and now I am attempting a complex >> assembly with multiple dependencies and I'm having trouble. So, I'm >> stepping back to create an organized structure and a set of best practices. >> >> I develop each part in its own file which contains the part's module, >> dimensional constants, and functions for computed values. My top level >> assembly file references all of the part files using include statements and >> puts it all together. >> >> 1. My first question is about include statements: How do I conditionally >> "include" library files? >> >> Each part and the assembly needs a common library, for example, >> metric_screws.scad. As I develop each part, each part file needs to have a >> "include <metric_screws.scad>;". >> >> The top level assembly includes metric_screws.scad, which then gets >> included again as each part file is included. >> >> So, how do I avoid these multiple includes of the same librry from the >> top-level assembly? Or, how do I conditionally execute include statements >> in the part files? >> >> 2. When would you use a "use" statement rather than an "include" >> statement? >> >> Thanks >> >> >> ------------------------------ >> Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> >> at Nabble.com. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
L
LenStruttmann
Sat, Apr 17, 2021 5:31 PM

But, then you are still including screws.h multiple times, right?

I'm kinda surprised that this isn't more of an issue.

--
Sent from: http://forum.openscad.org/

But, then you are still including screws.h multiple times, right? I'm kinda surprised that this isn't more of an issue. -- Sent from: http://forum.openscad.org/
A
adrianv
Sat, Apr 17, 2021 5:41 PM

You can define the same function or module multiple times.  Later definitions
replace earlier ones.  So including the same file repeatedly doesn't break
anything.  Even definitions of global constants are just replaced if they
happen in include files.

Be aware if you use "use" that any variable assignments at the top level
will be executed any time you call a function in your file, which can result
in huge slow downs if you call functions a lot and have something slow to
compute in those top level assignments.

LenStruttmann wrote

But, then you are still including screws.h multiple times, right?

I'm kinda surprised that this isn't more of an issue.

--
Sent from: http://forum.openscad.org/


OpenSCAD mailing list
To unsubscribe send an email to

discuss-leave@.openscad

You can define the same function or module multiple times. Later definitions replace earlier ones. So including the same file repeatedly doesn't break anything. Even definitions of global constants are just replaced if they happen in include files. Be aware if you use "use" that any variable assignments at the top level will be executed any time you call a function in your file, which can result in huge slow downs if you call functions a lot and have something slow to compute in those top level assignments. LenStruttmann wrote > But, then you are still including screws.h multiple times, right? > > I'm kinda surprised that this isn't more of an issue. > > > > -- > Sent from: http://forum.openscad.org/ > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to > discuss-leave@.openscad -- Sent from: http://forum.openscad.org/
L
LenStruttmann
Sat, Apr 17, 2021 7:32 PM

/You can define the same function or module multiple times.  Later
definitions replace earlier ones.  So including the same file repeatedly
doesn't break anything.  Even definitions of global constants are just
replaced if they happen in include files. /

That's good to know.  I still find it odd.  If my assembly and my parts all
include:

include <BOSL2/std.scad>;
include <BOSL2/metric_screws.scad>;

... it seems to me that there would be a performance hit to re-read those
files for each part.  Then again, after the first file read the OS will have
all (or most) of the file contents cached. I suppose I'm just showing my
age, coming from a programming background where file reads were enormously
expensive.  I guess that this behavior is not much different from a C
compiler including .h files.

/Be aware if you use "use" that any variable assignments at the top level
will be executed any time you call a function in your file, which can result
in huge slow downs if you call functions a lot and have something slow to
compute in those top level assignments./

Thanks!  These are the types of performance hits I'd like to avoid.

--
Sent from: http://forum.openscad.org/

/You can define the same function or module multiple times. Later definitions replace earlier ones. So including the same file repeatedly doesn't break anything. Even definitions of global constants are just replaced if they happen in include files. / That's good to know. I still find it odd. If my assembly and my parts all include: include <BOSL2/std.scad>; include <BOSL2/metric_screws.scad>; ... it seems to me that there would be a performance hit to re-read those files for each part. Then again, after the first file read the OS will have all (or most) of the file contents cached. I suppose I'm just showing my age, coming from a programming background where file reads were enormously expensive. I guess that this behavior is not much different from a C compiler including .h files. /Be aware if you use "use" that any variable assignments at the top level will be executed any time you call a function in your file, which can result in huge slow downs if you call functions a lot and have something slow to compute in those top level assignments./ Thanks! These are the types of performance hits I'd like to avoid. -- Sent from: http://forum.openscad.org/
NH
nop head
Sat, Apr 17, 2021 8:43 PM

To be clear I generally avoid including the same file twice in the same
scope but screws for example are included in most sub assemblies that are
themselves used, so they get included into multiple scopes as each used
file has its own variables.

The fact that variables are evaluated when a module of function in a used
file is called really slows down my builds and I think sets a finite limit
to the complexity of the design. I am struggling with my latest 3D printed.
I don't think I could model a car or an airliner in OpenSCAD. The time to
instantiate the variables would tend towards infinity. It already takes
minutes.

For most of my designs that only have one main file or perhaps two or three
it isn't a problem but when there are 30 files it gets very slow. I think
100 would be impossible, whereas a C++ program with 100 files in it is not
really a problem. There isn't really a limit because compile time is linear
with the number of files, whereas with OpenSCAD it increases exponentially.

On Sat, 17 Apr 2021 at 20:32, LenStruttmann LenStruttmann@gmail.com wrote:

*You can define the same function or module multiple times.  Later
definitions replace earlier ones.  So including the same file repeatedly
doesn't break anything.  Even definitions of global constants are just
replaced if they happen in include files. *

That's good to know.  I still find it odd.  If my assembly and my parts
all include:

include <BOSL2/std.scad>;
include <BOSL2/metric_screws.scad>;

... it seems to me that there would be a performance hit to re-read those
files for each part.  Then again, after the first file read the OS will
have all (or most) of the file contents cached. I suppose I'm just showing
my age, coming from a programming background where file reads were
enormously expensive.  I guess that this behavior is not much different
from a C compiler including .h files.

Be aware if you use "use" that any variable assignments at the top level
will be executed any time you call a function in your file, which can
result in huge slow downs if you call functions a lot and have something
slow to compute in those top level assignments.

Thanks!  These are the types of performance hits I'd like to avoid.


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


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

To be clear I generally avoid including the same file twice in the same scope but screws for example are included in most sub assemblies that are themselves used, so they get included into multiple scopes as each used file has its own variables. The fact that variables are evaluated when a module of function in a used file is called really slows down my builds and I think sets a finite limit to the complexity of the design. I am struggling with my latest 3D printed. I don't think I could model a car or an airliner in OpenSCAD. The time to instantiate the variables would tend towards infinity. It already takes minutes. For most of my designs that only have one main file or perhaps two or three it isn't a problem but when there are 30 files it gets very slow. I think 100 would be impossible, whereas a C++ program with 100 files in it is not really a problem. There isn't really a limit because compile time is linear with the number of files, whereas with OpenSCAD it increases exponentially. On Sat, 17 Apr 2021 at 20:32, LenStruttmann <LenStruttmann@gmail.com> wrote: > > *You can define the same function or module multiple times. Later > definitions replace earlier ones. So including the same file repeatedly > doesn't break anything. Even definitions of global constants are just > replaced if they happen in include files. * > > That's good to know. I still find it odd. If my assembly and my parts > all include: > > include <BOSL2/std.scad>; > include <BOSL2/metric_screws.scad>; > > ... it seems to me that there would be a performance hit to re-read those > files for each part. Then again, after the first file read the OS will > have all (or most) of the file contents cached. I suppose I'm just showing > my age, coming from a programming background where file reads were > enormously expensive. I guess that this behavior is not much different > from a C compiler including .h files. > > *Be aware if you use "use" that any variable assignments at the top level > will be executed any time you call a function in your file, which can > result in huge slow downs if you call functions a lot and have something > slow to compute in those top level assignments.* > > Thanks! These are the types of performance hits I'd like to avoid. > > > > ------------------------------ > Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> > at Nabble.com. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
L
LenStruttmann
Sat, Apr 17, 2021 9:48 PM

So, would a "best practice" be:

(1) Assuming that libraries are well-designed with a minimum number of
instantiated variables,
(2) Part files include any libraries they need, but never instantiate global
variables. Instead, they expose all values only via functions.
(3) The top level assembly only "use"s part files, accessing everything as a
module or a function.

?

--
Sent from: http://forum.openscad.org/

So, would a "best practice" be: (1) Assuming that libraries are well-designed with a minimum number of instantiated variables, (2) Part files include any libraries they need, but never instantiate global variables. Instead, they expose all values only via functions. (3) The top level assembly only "use"s part files, accessing everything as a module or a function. ? -- Sent from: http://forum.openscad.org/
NH
nop head
Sat, Apr 17, 2021 9:59 PM

Yes but that would mean wrapping all my objects with functions.

The problem remains with complex machines that  their used files need
variables to calculate positions, etc. If you wrap all those expressions in
functions you end up with an exploding tree of function calls.

On Sat, 17 Apr 2021 at 22:49, LenStruttmann LenStruttmann@gmail.com wrote:

So, would a "best practice" be:

(1) Assuming that libraries are well-designed with a minimum number of
instantiated variables,
(2) Part files include any libraries they need, but never instantiate
global variables. Instead, they expose all values only via functions.
(3) The top level assembly only "use"s part files, accessing everything as
a module or a function.

?


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


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

Yes but that would mean wrapping all my objects with functions. The problem remains with complex machines that their used files need variables to calculate positions, etc. If you wrap all those expressions in functions you end up with an exploding tree of function calls. On Sat, 17 Apr 2021 at 22:49, LenStruttmann <LenStruttmann@gmail.com> wrote: > So, would a "best practice" be: > > (1) Assuming that libraries are well-designed with a minimum number of > instantiated variables, > (2) Part files include any libraries they need, but never instantiate > global variables. Instead, they expose all values only via functions. > (3) The top level assembly only "use"s part files, accessing everything as > a module or a function. > > ? > > ------------------------------ > Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> > at Nabble.com. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
AC
A. Craig West
Sat, Apr 17, 2021 10:03 PM

In general, in files that I am using, I never define top level variables at
all, which works well with my general aversion to global variables. Pretty
much all variables are declared inside modules, except the $ variables,
which are special. Everything else is passed in as arguments. It seems to
help

On Sat, 17 Apr 2021, 18:00 nop head, nop.head@gmail.com wrote:

Yes but that would mean wrapping all my objects with functions.

The problem remains with complex machines that  their used files need
variables to calculate positions, etc. If you wrap all those expressions in
functions you end up with an exploding tree of function calls.

On Sat, 17 Apr 2021 at 22:49, LenStruttmann LenStruttmann@gmail.com
wrote:

So, would a "best practice" be:

(1) Assuming that libraries are well-designed with a minimum number of
instantiated variables,
(2) Part files include any libraries they need, but never instantiate
global variables. Instead, they expose all values only via functions.
(3) The top level assembly only "use"s part files, accessing everything
as a module or a function.

?


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


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


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

In general, in files that I am using, I never define top level variables at all, which works well with my general aversion to global variables. Pretty much all variables are declared inside modules, except the $ variables, which are special. Everything else is passed in as arguments. It seems to help On Sat, 17 Apr 2021, 18:00 nop head, <nop.head@gmail.com> wrote: > Yes but that would mean wrapping all my objects with functions. > > The problem remains with complex machines that their used files need > variables to calculate positions, etc. If you wrap all those expressions in > functions you end up with an exploding tree of function calls. > > On Sat, 17 Apr 2021 at 22:49, LenStruttmann <LenStruttmann@gmail.com> > wrote: > >> So, would a "best practice" be: >> >> (1) Assuming that libraries are well-designed with a minimum number of >> instantiated variables, >> (2) Part files include any libraries they need, but never instantiate >> global variables. Instead, they expose all values only via functions. >> (3) The top level assembly only "use"s part files, accessing everything >> as a module or a function. >> >> ? >> >> ------------------------------ >> Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> >> at Nabble.com. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >