Topic Title

The DataCAD Developer Network (DDN) is an online resource for information and support for DCAL® (DataCAD Applications Language) developers as well as anyone interested in creating fonts, toolbars, hatch patterns, or linetypes for use in DataCAD.

Code: Select all{ DCAL program Hello_World is intended to be a simple introduction to Re-entrant State Machines as they pertain to DCAL for Delphi programming. In this example the Re-entrant State Machine is documented in order to illustrate the fundamental structure of a DataCAD macro.

By definition a state machine is a program that stores the status of something at a given time and can operate on input to change the status and/or cause an action or output to take place for any given change.

Programmers originally implemented a 're-entrant' state machine in DataCAD to make it possible to stack commands. For example, if you're in the middle of one command such as drawing a line, you can press a keyboard interrupt such as [/] which will interrupt the line drawing command and bring you to the zoom command. When you exit the zoom command, the re-entrant state machine brings you back to the previous command on the stack; drawing a line.

A hotkey, on the otherhand, breaks out of the current command, clears the command stack, and brings you to a new command. For example, if you're drawing a line and press the [C] key, DataCAD will 'break' out of the line command and bring you to the copy command.

There are four main sections within DataCAD's state machine.

Section 1: AlSize This is where DataCAD's state machine allocates memory for your local variables.

Section 2: Afirst This is where you initialize the local state and other variables. Tasks you define here will only be performed once.

Section 3: Aagain This is where DataCAD returns after it has performed an action for you. The action variable will be equal to Aagain until you set the result to XDone.

Section 4: Alast If your macro has been interrupted by user action such as pressing a hotkey, DataCAD will set the action to Alast to give you the opportunity to clean up any temporary data before exiting to the new command.}

library Hello_World;

uses // Sharemem, { Do not use. All DataCAD parameters are limited to short strings} Dialogs,

// The values stored in the records you define here will be rememberd// between dispatcher calls because they are pushed onto the command stacktype HelloL = record state: asint; case byte of 0: (getp: getpointArg); end;

PHelloL = ^HelloL;

// Main Function (Re-entrant State Machine)function hello_world_main(act: action; pl, pargs: Pointer): wantType;// The values stored in variables you define here will not be retained// between dispatcher calls because they are not pushed onto the stackvar retval: asint; l: PHelloL;begin l := PHelloL(pl);

{ Section 2: Afirst - Tasks to perform only once } if act = Afirst then begin // Initialize local state and other variables wrterr('Hello World Macro - Version 1.0'); l.state := 1; end

{ Section 4: Alast - If your macro has been interrupted by user action } else if act = Alast then begin // If you're going to get blown off the stack... // (i.e.) the user pressed a hotkey, // then this is your last chance to clean up temporary data. end