General HydroStatics
Ship Stability Software
Command of the Week
(New or interesting aspects of GHS that you may not know about)

MACRO


It has been said that the MACRO command is the best thing about GHS.

MACRO has often appeared in COW examples without being explained. The time has come to milk this one.

MACRO is a multi-line command: it starts with MACRO and ends with a slash.

But is MACRO really a command? That's debatable. There are multi-line commands in Part Maker that do something. The MACRO command does nothing.

That's because "macro command" has a double meaning. The MACRO command proper, as described above, does nothing immediately, but it defines a "large command" that may be used later to do almost anything.

When you write,

MACRO CALC
    SOLVE
    ANGLES *
    RA
/

you have defined your own "macro" or "large" command. You now have a "macro command" named CALC.

The EXECUTE command executes macros. You cannot say,

CALC

because CALC is not a regular command. You have to say,

EXECUTE CALC

There is a shortcut syntax for EXECUTE:

.CALC is equivalent to EXECUTE CALC.

What .CALC does is exactly the same as if you had issued the commands in its body directly.

You can see that what we are dealing with is text substitution. It's really simple. There are no complicated rules to learn. When you define a macro, the lines of text in it can be anything: the syntax is not checked, and the lines are not checked for being legal commands. You can put anything in the body of a macro because the only thing the MACRO command does is store the lines of text under the given name. That's it! Yes, the slash at the end is removed as well as any slash that appears at the beginning of an internal line.

When you execute a macro, the lines are taken to be commands and processed in sequence. If there is anything that is not a valid GHS command at that time, you get an error report just as you would if the offending text were issued directly.

Now there is one rule: if a percent sign followed by a digit is found, it disappears. For example, if a macro looked like,

MACRO CALC
    SOLVE
    ANGLES *
    RA %1
/

and it was executed with,

.CALC

then it would be as if the %1 did not exist.

But if you executed it with

.CALC "/LIM"

the "/LIM" would replace the %1.

Or you might do

.CALC "/LIM /FSM"

Or if the macro was defined as

MACRO CALC
    SOLVE
    ANGLES *
    RA %1 %2
/

Then you could do,

.CALC "/LIM", "/FSM"

You can have as many as nine of these "parameters" which substitute for %1, %2, %3, etc. wherever they occur in the body of the macro. It's very simple but very powerful.

Let's suppose you have put some LIMIT commands in macros. For example,

MACRO LIM1
UNITS MT
    LIM area from abs 0 to abs 30 > 0.055
    LIM area from abs 0 to abs 40 or fld > 0.09
    LIM area from abs 30 to abs 40 or fld >0.03
    LIM ra at max to abs 30 > 0.20
    LIM abs angle at max > 25
    LIM gm upright > 0.15
/

MACRO LIM2
UNITS MT
    LIM area from abs 0 to max at abs 15 > 0.070
    LIM area from abs 0 to max at abs 30 > 0.055
    LIM area from abs 30 to abs 40 or fld > 0.03
    LIM ra at max to abs 30 > 0.20
    LIM abs angle at max > 15
    LIM gm upright > 0.15
/

MACRO LIMIT
    UNIT SAVE
    LIM OFF
    .LIM%1
    UNIT REST
/

MACRO CALC
    .LIMIT%1
    SOLVE
    ANGLES *
    RA /LIM %2
/

.CALC 1
.CALC 2, "/FSM"

Just remember "text substitution" and you can easily reason out the example above.

If a COW can do it anyone can.

(Macro mavens will have noticed the error in CALC.)

Questions, comments, or requests?
Contact Creative Systems, Inc.

support@ghsport.com

USA phone: 360-385-6212 Fax: 360-385-6213
Office hours: 7:00 am - 4:00 pm Pacific Time, Monday - Friday

Mailing address:
PO Box 1910
Port Townsend, WA 98368 USA

www.ghsport.com

Click here for an index to this and previous COWs