Version 0.59.0

Multiple Script Files

More complex constructions and the desire to reuse once written 3d model code require the use of multiple script files for one 3d model script. trCAD CAD supports commands for including different scripts or models into other scripts.

The #insert command

The first way of inserting another script is done by the #insert command. (It is a precompiler pragma and has therefore a hash symbol prefix.) The command takes a string as argument that holds the file name of another script. When the #insert command is executed, this script file is inlined into the previous script (see https://en.wikipedia.org/wiki/Inline_expansion ).

Example

#insert( "anotherscript.sps" )

Note

When inlining scripts with the #insert command one should take care of name conflicts that may appear.

The #import command

A second, and maybe more advisable way of including external scripts is given by the #import command. This command imports the second script as a package and not by inlining it. A package has the advantage that it is safe from name conflicts.

Like the #insert command, the #import command takes the name of a script file as only argument, e.g #insert( "myfiles/anotherscript.sps" ). The file is then included as package. This package can be addressed by its identifier that is the file name without directory and postfix (e.g anotherscript). To avoid conficts from importing multiple files with equal names (from different directories), an additional identifier can be appended to the #import command that is then used as package identifier, like in the following example:

Example

#import( "anotherscript.sps" ) mypackage

Accessing objects inside packages

Objects of a package that can be accessed from the importing script must be either declared as open or as public. These variables or functions can be addressed by the package identifier followed by a double colon (::) and the variable name, what is shown in the following example set.

File "anotherscript.sps":

Example

open int i
{
  name = "My Open Parameter"
  descr = "This integer is readable from outside."
  value = 3
}
public string s = "My public string."
public function f()
{
  echo( "This is function f()" )
}

Main script file:

Example

#import ( "anotherscript.sps" )
echo( anotherscript::i )
echo( anotherscript::s )
anotherscript::f()

Output

3
My public string.
This is function f()

Objects in package that are imported over more than one level can be access by the same mechanism:

File "level2.sps":

Example

public string s = "My public string."

File "level1.sps":

Example

#import( "level2.sps" )

Main script file:

Example

#import ( "level1.sps" )
echo( level1::level2::s )

Repeated package import

Sometimes it may be useful to import a single package several times in order to access multiple instances of the package. This is perfectly possible as long as the different instances have unique identifiers.

Example

#import( "anotherscript.sps" )
#import( "anotherscript.sps" ) s2

anotherscript::f()
s2::f()

An alternative way of creating several different instances of the solids generated by an importet packge is described in the section about multiple instances.

🗙

Search results for