Addin That Makes Toolbar Button That Creates New Document for Indexing



Your Ad Here

This is an addin example that creates a toolbar button and uses a dialog box.  It is a complete example, and is probably a little too complex if you are just learning.

When you press the button, the macro creates a new file, sets stock dimensions and centers the stock for indexing.  When you load this addin, it makes a toolbar button for you.  When you press the toolbar button, you see the dialog box below.

Option Explicit
' this routine is called when the addin is loaded
' when the addin is loaded, we want to add the button to the Standard toolbar
Public Sub AddIn_OnConnect(ByVal flags As FeatureCAM.tagFMAddInFlags)
    Dim bars As FMCmdBars
    Dim bar As FMCmdBar
    Dim ctrl As FMCmdBarBtn

    Set bars = Application.CommandBars ' get access to all of the tool bars
    Set bar = bars("Standard") ' get access to the Standard toolbar
    Set ctrl = bar.Controls("MyNewFile") ' get access to the MyNewFile button
    If ctrl Is Nothing Then
	' the button did not exist, so add it
        Set ctrl = bar.Controls.Add( ,, "MyNewFile")
	' get the button image
        ctrl.FaceId = 15
    End If
End Sub

' this routine is called when the addin is unloaded
' when the addin is unloaded, we want to remove the button from the toolbar
Public Sub AddIn_OnDisConnect(ByVal flags As FeatureCAM.tagFMAddInFlags)
    Dim bars As FMCmdBars
    Dim bar As FMCmdBar
    Dim ctrl As FMCmdBarCtrl

    Set bars = Application.CommandBars ' get access to all of the tool bars
    Set bar = bars("Standard") ' get access to the Standard toolbar
    Set ctrl = bar.Controls( "MyNewFile") ' get access to the MyNewFile button
    If Not ctrl Is Nothing Then
	' the button exists, so delete it
        ctrl.Delete
    End If
End Sub

Public Sub MyNewFile
    Begin Dialog UserDialog 410,154,"Create new FM file",.StockDlg ' %GRID:10,7,1,1
    Text 20,14,90,14,"Stock Width",.Text1
    Text 20,42,90,14,"Stock Length",.Text2
    Text 20,70,120,14,"Stock Thickness",.Text3
    TextBox 160,14,90,21,.Width
    TextBox 160,42,90,21,.Length
    TextBox 160,70,90,21,.Thickness
    CheckBox 30,112,90,14,"Indexed",.index
    OKButton 300,14,90,21
    CancelButton 300,42,90,21
    End Dialog
    Dim dlg As UserDialog
    dlg.Width = "4"
    dlg.Length = "6"
    dlg.Thickness = "2"
    Dim rc
    rc = Dialog( dlg)
    If( rc = -1) Then
        Dim Doc As FMDocument
        Dim Stock As FMStock
        Dim Setup As FMSetup
        Dim Ucs As FMUcs
        Dim dWidth, dThick, dLen
        Dim x As Double, y As Double, z As Double

        Set Doc = Application.Documents.AddFM( True, True)
        dWidth = CDbl( dlg.Width)
        dLen = CDbl( dlg.Length)
        dThick = CDbl( dlg.Thickness)
        Set Stock = Doc.Stock
        Stock.SetDimensions eST_Block, dLen, dWidth, dThick,,,,,
        If dlg.index = 1 Then
            x = 0.0
            y = dWidth/-2.0
            z = dThick/2.0
            Stock.SetLocation x,y,z
            Stock.IndexType = eIT_4thAxisX
            Doc.AddLine2Points (-1.0,0,0,dLen+1.0,0,0)
            Doc.AddLine2Points (dLen+.5,0,.25,dLen+1.0,0,0)
        Else
            Stock.SetLocation 0.0,0.0,0.0
        End If
        Set Setup = Doc.ActiveSetup
        Set Ucs = Setup.ucs
        Ucs.Align eUA_StockTopUL,,,
     End If
End Sub

Rem See DialogFunc help topic for more information.
Private Function StockDlg(DlgItem$, Action%, SuppValue&) As Boolean
    Select Case Action%
    Case 1 ' Dialog box initialization
    Case 2 ' Value changing or button pressed
    Rem StockDlg = True ' Prevent button press from closing the dialog box
    Case 3 ' TextBox or ComboBox text changed
    Case 4 ' Focus changed
    Case 5 ' Idle
    Rem Wait .1 : StockDlg = True ' Continue getting idle actions
    Case 6 ' Function key
    End Select
End Function

Return to FeatureCAM Index


Your Ad Here