BASIC scripts for MD files

You can extend the functionality that is provided for machine design files by accessing the Application Programming Interface (API). This programming environment enables you to customize and enhance the machine design capabilities using the BASIC programming language.

Some functionality that has been added using the API in various situations includes:

Typically, it is fairly simple to program one of these scripts. Often you can copy a similar block of code from another machine and edit it slightly to get it to work on a new machine. In fact, we recommend that you use copy-and-edit for any standard functions.

The BASIC script that is associated with a machine design file is completely managed by FeatureCAM. That is, you do not have a separate file that you can see in the file browser. You can access it only from within FeatureCAM in the following way:

  1. Load your machine design file into FeatureCAM.
  2. Press the Alt+F11 keys on your keyboard. A new window is displayed, containing the BASIC script associated with the current MD file.

Important notes

As a small example of a BASIC script for an MD file, code for opening the main spindle jaws is shown below. Annotations following the code describe some key aspects that you must consider.

Public Sub MachineSim_spindle( Doc As FeatureCAM.MFGDocument, _

ByVal data As Double, _

ByVal Action As FeatureCAM.tagFMMachineSimSpindleActionType, _

ByVal spindle As Long)

Dim Vw As MFGWindow

Dim Err As Long

Dim i As Integer

Dim dOpen As Double

Dim dCls As Double

Set Vw = Doc.ActiveWindow

Err = 0

i = 0

If( Doc.Metric ) Then

dOpen = 4

dCls = -1

Else

dOpen = .25

dCls = -.125

End If

Vw.SimIgnoreClashes(0)

If( Action = eSimAction_Open And spindle = 0 ) Then

Vw.SimIgnoreClashes(1)

While Err = 0 And i<3

Vw.SimCutMove( "main_jaw_1", dOpen, 0, 0 )

Vw.SimCutMove( "main_jaw_2", dOpen, 0, 0 )

Vw.SimCutMove( "main_jaw_3", dOpen, 0, 0 )

Vw.SimPerformCut Err

If( Err = 0 ) Then

Vw.SimUpdateGraphics

End If

i = i + 1

Wend

End If

End Sub

The MachineSim_spindle routine shown is the hook provided for getting control over spindle actions.

Although you define your machine in metric or imperial units, those who use your file may run FeatureCAM documents in either type of units. So the MD file must calculate or convert all units for either system.

The eSimAction_Open enum indicates that the action generated is 'Open'. Other types include eSimAction_Close and eSimAction_Position. See the API guide for a full list.

The main spindle is identified as 0, the sub-spindle as 1. So this block catches events that signal the opening of the main spindle only.

Turns off gouge-checking, which you must handle manually in the script.

Movement is usually done in a loop to break the motion into small increments. In this case the jaws are opened in three steps. Typically, we can use much larger increments for things that are unlikely to collide. When closing jaws, however, you might use 50 steps to stop exactly when the jaws touch the stock.

This is the basic command to move a solid with linear motion. (A similar command, SimCutRotate, is used for rotational motion). All child solids declared in the MD file follow appropriately. The name of the solid is the first argument and must match the name of a solid in the MD file. The motion is with respect to the LCS defined for the solid, or the table coordinate system if no LCS is specified.

Perform all the movements that have been specified, and perform gouge-checking at the same time. Note that the movements are performed simultaneously, not sequentially.

Check whether there was an error. Sometimes it can be useful to disable the error checking for debugging, but you should put the condition back when the model is working.

Update the graphics display only if there was no error.