Using Macros |
Top Previous Next |
FreeFlyer supports the use of Macros to provide "find-and-replace" functionality, allowing users to create generic sets of FreeFlyer script, using arguments to specify identifiers that will be replaced when the Macro is instantiated by the Call command. While Procedures require the user to declare the type for each argument passed to the Procedure, Macros allow users to pass any identifier without specifying the argument type. Examples of "identifiers" include FreeFlyer objects, object properties or methods, functions, variable values, and string values.
Syntax
Details on Macro Implementation
Find-and-Replace ExampleThe example below defines a Macro which will substitute "Spacecraft1" in every instance of the characters "SC", "ObjectCreatedInMacro" for "name", and "Spacecraft.E" for "ecc". When the Macro is instantiated using the Call command, it will change the values of Spacecraft1.A and ObjectCreatedBeforeMacro, and create a new Variable with the name "ObjectCreatedInMacro".
Note that the "ObjectCreatedBeforeMacro" Variable was not passed as an argument, nor is it a Global object, but it can still be accessed within the Macro.
Create Object Example This simple Macro allows the user to create an object, using the Call command to specify the type of object to create (String or Variable), the name of the object, and the value to assign to the object.
Call Function Example This Macro allows the user to specify the math function to perform in a calculation. In this example, the identifier "abs" is substituted wherever "function" appears in the Macro.
Macro Functions__macro_quote__ and __macro_single_quote__These Macro functions allow users to obtain a quoted string containing the value passed by the Call command into the specified identifier. Simply placing quotation marks around an identifier will return the literal string without substituting the value (find-and-replace is not performed). The example below illustrates how the quoted string "SC" will be reported as "SC", while __macro_quote__(SC) performs the find-and-replace and is reported as "Spacecraft1". __macro_single_quote__(SC) is reported as 'Spacecraft1'.
__macro_rest_cdl__Macros allow users to specify extra arguments when calling the Macro in addition to the arguments that were specified when the Macro was defined. The __macro_rest_cdl__ function allows users to access these additional arguments as a comma-delimited list (hence, "cdl"). In the example below, the Macro is defined with zero arguments, but three arguments are passed using the Call command. The example shows how the function __macro_rest_cdl__ can be used to capture the extra string arguments, which can be concatenated into a single string.
In this example, the Macro is defined with one argument, but four arguments are passed using the Call command. The example shows how the function __macro_rest_cdl__ can be used to capture the extra variable arguments as a comma-delimited string, which can be used to set properties such as Spacecraft1.Position.
This Macro creates an object and sets its name, using a combination of the __macro_rest_cdl__ and __macro_quote__ functions.
See Also•Define Macro Command •Include Command •Call Command
|