Debugging Scripts

Old Content - visit altium.com/documentation

Parent page: Scripting

The Altium Designer Scripting system offers a full featured debugging environment.

The Scripting Debugger helps you to identify and resolve errors in scripts by providing a range of automated tools and information panels.

Script errors

Common errors include invalid methods, misplaced grouping operators, mismatched types, logic errors and typographical errors. When the scripting system encounters an error, the debugger will pause the script and display an error dialog indicating the problem.

The below example shows a script with a missing terminating character and the matching error dialog message.


The debugger will pause a script in response to compile or run time errors.

Once the error dialog is dismissed the script can be stopped using the Stop command (Run » StopCtrl+F3) or button , then the code corrected and the script re-run.

There are situations when an error can cause a script to be locked in an infinite loop and Altium Designer becomes non-responsive. To overcome this problem, press Ctrl+Pause/Break shortcut keys to break the script's loop.

Script debugging

There are a range of tools in the scripting system to help in debugging scripts. These include applying multiple break points in a script, using the Watch List panel to monitor the value of variables, using the bookmarks to jump around more efficiently, and using step into and over facilities to trace through scripts.

For example, the following features can be used when checking the values of variables and expressions in a scripts:

  • Breakpoints
  • Expression Evaluation tooltips
  • Insight tool tips
  • Evaluate dialog

Tracing

To help find and resolve an unknown error in a script, a series of breakpoints can be inserted into a script to pause the code at multiple points while the values of variables and expressions are checked.

At any point when the script is paused, however, the script can also be advanced one line at a time using the Debugger's tracing/step features. In this way a breakpoint can be used to initially pause the script in the area of interest then advanced one line at a time using the tracing feature. The process of executing a script one line at a time is referred to as tracing or script stepping.

The Script debugger provides commands related to tracing: stepping into (Run » Step Into) and stepping over (Run » Step Over). This allows you to trace the script process and analyze its behavior when debugging the script code.


The Debugging commands available in the Run menu.

Step Into Command

The Step Into process executes script one statement at a time. The command is available from the Run menu, its associated button  or the F7 shortcut key.

If the statement being executed calls another procedure, stepping into that statement transfers control to the first line in the called procedure. The blue line on a script indicates the current line where the script is up to.

Step Over Command

The Step Over process is similar to Step Into, except that if the current statement is a call to another procedure, the entire called procedure is executed without stopping rather than tracing through the called procedure. The command is available from the Run menu, its associated button  or the F8 shortcut key.

This command is useful when it has been determined that a called procedure is not the cause of the problem in the current procedure, and avoids having to needlessly trace through the called procedure line by line — that procedure is 'stepped over'.

Use the Run To Cursor command to execute a script up to the cursor location. The script pauses at that line as if it has encountered a breakpoint, where it can be run or stepped from that point. Select Run » Run To Cursor from the main menu, or use its toolbar button  or the Ctrl+F9 shortcut keys.

Exceptions and Messages

As tends to be the case with all programming projects, developing and debugging the script code does not necessarily stop with a 'working' script. It's very likely that in practical use a script will encounter unforeseen errors, or may not deliver the expected results.

Fortunately there are a number of ways the scripting system can be used to handle execution exceptions and provide user feedback about the script operation.

Exception Handlers

In DelphiScript, the try keyword introduces a try-except statement or a try-finally statement. These two statements are related but serve different purposes.

Try Finally

In a try-finally handler, the statements in the finally block are always executed — regardless of whether an exception error occurred in in try block. Use try-finally block to free temporary objects or other resources, and to perform clean up activities.

 Reset(F); Try   // process file F Finally   CloseFile(F); End;

Try Except

Use the try-except statement to catch raised exceptions and execute related error handling code. For example, a specific exception can be caught and a log updated or a descriptive error dialog box opened. A code exception in the try block causes the except block to be executed.

In the below example, the ApplyOffset procedure is called, with suitable parameters, from the DefineOffset procedure. The procedure makes a simple division calculation (on the Ydim and Zdim variables) and reports the result (Offset) in a standard dialog box.

 Procedure ApplyOffset(Ydim, Zdim); Var    Offset; Begin;    Try       Offset := Ydim div Zdim;       ShowMessage('Calculated Offset = ' + IntToStr(Offset));    Except       ShowWarning('Entered Z dimension must be greater than zero.');    End; End;  Procedure DefineOffset; Begin;    ApplyOffset(32, 4); End;

For the ApplyOffset parameters shown above (32,4), the executed script will show the calculated result using the ShowMessage procedure, as shown below.

Alternatively, changing the passed parameters to 32,0 (ApplyOffset(32, 0);) induces a divide-by-zero exception in the Offset calculation, triggering the Except handler block. This uses the ShowWarning procedure to display a relevant Warning dialog box.

In the script editor's debug mode, an exception will pause the script execution when the Error dialog is dismissed. Use the Run or Step Into command to continue.

Raise

The assumption in the above simple error handling procedure is that the exception is raised in response to a divide by zero error, but this means that other types of exceptions will not be accurately reported by the warning dialog text. In this example however, it is the most likely exception error that will be encountered.

Some form of specific error detection is nevertheless desirable from a user perspective. In DelphiScript the the Delphi On keyword (to catch a specified exception type) is not supported, but the Raise statement can used inside an Except block to raise the most recent exception (whatever type it may have been).

 Try    Offset := Ydim div Zdim;    ShowMessage('Calculated Offset = ' + IntToStr(Offset)); Except    Raise; End;

In this way, when the bulk of a procedure's code exists inside the try block of a try-except statement, all exceptions can be reported by the Raise statement. Note that exception dialog will only report generic error messages, rather than information specific to the script.

   

The Raise can be used without parameters to re-raise the last exception, or with a string parameter to deliver a specific error message.

Raise can also be used to induce an exception in response to specific tests, for example by applying simple if-then or case statements.

 Procedure ApplyOffset(Ydim, Zdim); Var    Offset; Begin;    if Zdim > Ydim then       Begin;          Raise('Z dimension must be smaller than Y dimension.');       End;    Offset := Ydim div Zdim;    ShowMessage('Calculated Offset = ' + IntToStr(Offset)); End;

Here, if the above example's Zdim parameter is larger than the Ydim parameter an exception (with message) is raised.

Errors in VB Scripts

The VBScript implementation in the Altium Designer scripting engine has built in functions that can be used in VBScript script projects, such as the MsgBox function.

The On Error statement can be used when trapping errors in VB Scripts. Consult the Microsoft VBScript documentation for more information on trapping errors.

Messages

In general, messages are the primary way to enhance script operation from a user perspective.

Messages can provide alerts for exceptions or indicate running information and warnings, and can be inserted in a script routines that deal with input data, calculations, file operations or any procedure where a procedural insight would be beneficial.

Message Dialogs

Along with the basic message dialog available in the scripting system (ShowMessage), a range of specific dialogs are available to provide tailored user feedback.

These include message procedures such as:

  • ShowError
  • ShowException
  • ShowInfo
  • ShowWarning
  • and more..

Messages Panel

Altium Designer's Messages panel can be a useful debugging tool for scripting, but can also be applied to provide a running stream of useful message information for the user.

The Messages panel is a Workspace Manager object that accessed from the Message Manager interface. The Messages panel can be used in a script to show the states of variables and properties (or indeed any other data) for both debugging and user feedback, as outlined in the example below.

 Procedure CreateMessages; var    WS : IWorkSpace;    MM : IMessagesManager; Begin    WS := GetWorkSpace;           // obtain Workspace Manager interface    MM := WS.DM_MessagesManager;  // obtain Message Manager interface        MM.ClearMessages;             // initialize     MM.BeginUpdate;    // add relevant messages here using MM.AddMesssage procedures    MM.EndUpdate;     WS.DM_ShowMessageView;         // display messages End;

 

You are reporting an issue with the following selected text and/or image within the active document: