Hello Guest it is November 08, 2024, 09:59:00 PM

Author Topic: Calling Multiple Subroutines Not Working as Expected  (Read 2769 times)

0 Members and 1 Guest are viewing this topic.

Calling Multiple Subroutines Not Working as Expected
« on: April 06, 2023, 06:06:23 PM »
I'm trying to create a single file that lets me make whatever parts I need.  It's mostly working in that I can call a G-code subroutine, but if I try to call multiple subroutines it does not work as expected.  It will run the last subroutine the number of times of the total number of subroutines.  For example, if I tell it to run the MakeD20.tap and the MakeD12.tap it will run the MakeD12.tap twice.  Any help would be much appreciated.

The code:
Code: [Select]
'====== Dice Vending Machine ======
'M301
'Last edited 4/6/2023 by Aaron Potts

Sub Main

    'Last argument for Dialog is not asigning an id, it is specifying the
    'function that will handle interactions with the dialog elements.
    Begin Dialog DiceVendingMachine 200, 150, "Dice Vending Machine v1.0", .DialogHandler
        OKButton      10, 130, 40, 15, .ok
        CancelButton 152, 130, 40, 15, .cancel

        OptionGroup .diceChoiceGroup
        OptionButton  20, 10, 40, 10, "Full Poly", .fullPoly
        OptionButton  75, 10, 50, 10, "Poly, No D6", .polyNoD6

        CheckBox 140, 10, 40, 10, "Custom", .custom

        GroupBox 10, 30, 180, 90, "Custom Run:", .customGroup
        CheckBox 40, 45, 40, 10, "D20", .d20Check
        Text     90, 45, 40, 10, "How Many?", .d20Text
        TextBox 135, 45, 20, 10, .d20Qty

        CheckBox 40, 57, 40, 10, "D12", .d12Check
        Text     90, 57, 40, 10, "How Many?", .d12Text
        TextBox 135, 57, 20, 10, .d12Qty

        CheckBox 40, 69, 40, 10, "D10", .d10Check
        Text     90, 69, 40, 10, "How Many?", .d10Text
        TextBox 135, 69, 20, 10, .d10Qty

        CheckBox 40, 81, 40, 10, "D8", .d8Check
        Text     90, 81, 40, 10, "How Many?", .d8Text
        TextBox 135, 81, 20, 10, .d8Qty

        CheckBox 40, 93, 40, 10, "D6", .d6Check
        Text     90, 93, 40, 10, "How Many?", .d6Text
        TextBox 135, 93, 20, 10, .d6Qty

        CheckBox 40, 105, 40, 10, "D4", .d4Check
        Text     90, 105, 40, 10, "How Many?", .d4Text
        TextBox 135, 105, 20, 10, .d4Qty
    End Dialog

    'Initialize a dialog object.
    Dim DVM As DiceVendingMachine

    'Store the value returned from the dialog:
    '-1 = OK
    '0 = Cancel
    buttonPressed = Dialog(DVM) 'This line is necessary.

    If DVM.diceChoiceGroup = 0 And DVM.custom = 0 Then
        Code "M98 (Automated Dice Set 4D6 Rotary.TAP)"
    End If
    If DVM.diceChoiceGroup = 1 And DVM.custom = 0 Then
        Code "M98 (Automated Dice Set No D6.TAP)"
    End If
    If DVM.d20Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D20.tap) L" & DVM.d20Qty
    End If
    If DVM.d12Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D12.tap) L" & DVM.d12Qty
    End If
    If DVM.d10Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D10.tap) L" & DVM.d10Qty
    End If
    If DVM.d8Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D8.tap) L" & DVM.d8Qty
    End If
    If DVM.d6Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D6.tap) L" & DVM.d6Qty
    End If
    If DVM.d4Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D4.tap) L" & DVM.d4Qty
    End If
End Sub

'A function is required to control visibility of dialog elements.
Function DialogHandler(ControlID$, Action%, SuppValue%)

    'Toggle visibility of custom dice elements.
    Select Case Action%
        Case 1 'Action% = 1 when the dialog is first called.
            DlgEnable "customGroup", 0

            DlgVisible "d20Check", 0
            DlgVisible "d20Text", 0
            DlgVisible "d20Qty", 0
            DlgEnable "d20Text", 0
            DlgEnable "d20Qty", 0

            DlgVisible "d12Check", 0
            DlgVisible "d12Text", 0
            DlgVisible "d12Qty", 0
            DlgEnable "d12Text", 0
            DlgEnable "d12Qty", 0

            DlgVisible "d10Check", 0
            DlgVisible "d10Text", 0
            DlgVisible "d10Qty", 0
            DlgEnable "d10Text", 0
            DlgEnable "d10Qty", 0

            DlgVisible "d8Check", 0
            DlgVisible "d8Text", 0
            DlgVisible "d8Qty", 0
            DlgEnable "d8Text", 0
            DlgEnable "d8Qty", 0

            DlgVisible "d6Check", 0
            DlgVisible "d6Text", 0
            DlgVisible "d6Qty", 0
            DlgEnable "d6Text", 0
            DlgEnable "d6Qty", 0

            DlgVisible "d4Check", 0
            DlgVisible "d4Text", 0
            DlgVisible "d4Qty", 0
            DlgEnable "d4Text", 0
            DlgEnable "d4Qty", 0
        Case 2 'Action% = 2 anytime there is an interaction with an element other than a TextBox or ComboBox.
            If ControlID$ = "custom" Then
                'If no 0 or 1 after, then toggles elements.
                DlgEnable "fullPoly"
                DlgEnable "polyNoD6"

                DlgEnable "customGroup"
               
                DlgVisible "d20Check"
                DlgVisible "d20Text"
                DlgVisible "d20Qty"

                DlgVisible "d12Check"
                DlgVisible "d12Text"
                DlgVisible "d12Qty"

                DlgVisible "d10Check"
                DlgVisible "d10Text"
                DlgVisible "d10Qty"

                DlgVisible "d8Check"
                DlgVisible "d8Text"
                DlgVisible "d8Qty"

                DlgVisible "d6Check"
                DlgVisible "d6Text"
                DlgVisible "d6Qty"

                DlgVisible "d4Check"
                DlgVisible "d4Text"
                DlgVisible "d4Qty"
            End If
            If ControlID$ = "d20Check" Then
                DlgEnable "d20Text"
                DlgEnable "d20Qty"
            End If
            If ControlID$ = "d12Check" Then
                DlgEnable "d12Text"
                DlgEnable "d12Qty"
            End If
            If ControlID$ = "d10Check" Then
                DlgEnable "d10Text"
                DlgEnable "d10Qty"
            End If
            If ControlID$ = "d8Check" Then
                DlgEnable "d8Text"
                DlgEnable "d8Qty"
            End If
            If ControlID$ = "d6Check" Then
                DlgEnable "d6Text"
                DlgEnable "d6Qty"
            End If
            If ControlID$ = "d4Check" Then
                DlgEnable "d4Text"
                DlgEnable "d4Qty"
            End If
            If ControlID$ = "ok" Then
                Exit Function 'By default, OK buton doesn't do anything.
            End If
    End Select
End Function

Offline TPS

*
  •  2,528 2,528
Re: Calling Multiple Subroutines Not Working as Expected
« Reply #1 on: April 07, 2023, 03:09:59 AM »
you can try to use the teachfile for this (not tested!!):

Code: [Select]
'====== Dice Vending Machine ======
'M301
'Last edited 4/6/2023 by Aaron Potts

Sub Main

    'Last argument for Dialog is not asigning an id, it is specifying the
    'function that will handle interactions with the dialog elements.
    Begin Dialog DiceVendingMachine 200, 150, "Dice Vending Machine v1.0", .DialogHandler
        OKButton      10, 130, 40, 15, .ok
        CancelButton 152, 130, 40, 15, .cancel

        OptionGroup .diceChoiceGroup
        OptionButton  20, 10, 40, 10, "Full Poly", .fullPoly
        OptionButton  75, 10, 50, 10, "Poly, No D6", .polyNoD6

        CheckBox 140, 10, 40, 10, "Custom", .custom

        GroupBox 10, 30, 180, 90, "Custom Run:", .customGroup
        CheckBox 40, 45, 40, 10, "D20", .d20Check
        Text     90, 45, 40, 10, "How Many?", .d20Text
        TextBox 135, 45, 20, 10, .d20Qty

        CheckBox 40, 57, 40, 10, "D12", .d12Check
        Text     90, 57, 40, 10, "How Many?", .d12Text
        TextBox 135, 57, 20, 10, .d12Qty

        CheckBox 40, 69, 40, 10, "D10", .d10Check
        Text     90, 69, 40, 10, "How Many?", .d10Text
        TextBox 135, 69, 20, 10, .d10Qty

        CheckBox 40, 81, 40, 10, "D8", .d8Check
        Text     90, 81, 40, 10, "How Many?", .d8Text
        TextBox 135, 81, 20, 10, .d8Qty

        CheckBox 40, 93, 40, 10, "D6", .d6Check
        Text     90, 93, 40, 10, "How Many?", .d6Text
        TextBox 135, 93, 20, 10, .d6Qty

        CheckBox 40, 105, 40, 10, "D4", .d4Check
        Text     90, 105, 40, 10, "How Many?", .d4Text
        TextBox 135, 105, 20, 10, .d4Qty
    End Dialog

    'Initialize a dialog object.
    Dim DVM As DiceVendingMachine

    'Store the value returned from the dialog:
    '-1 = OK
    '0 = Cancel
    buttonPressed = Dialog(DVM) 'This line is necessary.


   'Create a new Teach File in Mach3\Gcode
   MyTeachFile = "TeachMe.nc"
   Err = OpenTeachFile(MyTeachFile)
   If Err <> 0 Then

    If DVM.diceChoiceGroup = 0 And DVM.custom = 0 Then
        Code "M98 (Automated Dice Set 4D6 Rotary.TAP)"
    End If
    If DVM.diceChoiceGroup = 1 And DVM.custom = 0 Then
        Code "M98 (Automated Dice Set No D6.TAP)"
    End If
    If DVM.d20Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D20.tap) L" & DVM.d20Qty
    End If
    If DVM.d12Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D12.tap) L" & DVM.d12Qty
    End If
    If DVM.d10Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D10.tap) L" & DVM.d10Qty
    End If
    If DVM.d8Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D8.tap) L" & DVM.d8Qty
    End If
    If DVM.d6Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D6.tap) L" & DVM.d6Qty
    End If
    If DVM.d4Check = 1 And DVM.custom = 1 Then
        Code "M98 (Make D4.tap) L" & DVM.d4Qty
    End If

    'Close the Teach file
    Code "M30"
    CloseTeachFile()
    'Now load the teach file for execution
    Call LoadTeachFile()
   Else
    'OpenTeachFile failed
    Message "Unable to open Teach File"
   End If   
End Sub

'A function is required to control visibility of dialog elements.
Function DialogHandler(ControlID$, Action%, SuppValue%)

    'Toggle visibility of custom dice elements.
    Select Case Action%
        Case 1 'Action% = 1 when the dialog is first called.
            DlgEnable "customGroup", 0

            DlgVisible "d20Check", 0
            DlgVisible "d20Text", 0
            DlgVisible "d20Qty", 0
            DlgEnable "d20Text", 0
            DlgEnable "d20Qty", 0

            DlgVisible "d12Check", 0
            DlgVisible "d12Text", 0
            DlgVisible "d12Qty", 0
            DlgEnable "d12Text", 0
            DlgEnable "d12Qty", 0

            DlgVisible "d10Check", 0
            DlgVisible "d10Text", 0
            DlgVisible "d10Qty", 0
            DlgEnable "d10Text", 0
            DlgEnable "d10Qty", 0

            DlgVisible "d8Check", 0
            DlgVisible "d8Text", 0
            DlgVisible "d8Qty", 0
            DlgEnable "d8Text", 0
            DlgEnable "d8Qty", 0

            DlgVisible "d6Check", 0
            DlgVisible "d6Text", 0
            DlgVisible "d6Qty", 0
            DlgEnable "d6Text", 0
            DlgEnable "d6Qty", 0

            DlgVisible "d4Check", 0
            DlgVisible "d4Text", 0
            DlgVisible "d4Qty", 0
            DlgEnable "d4Text", 0
            DlgEnable "d4Qty", 0
        Case 2 'Action% = 2 anytime there is an interaction with an element other than a TextBox or ComboBox.
            If ControlID$ = "custom" Then
                'If no 0 or 1 after, then toggles elements.
                DlgEnable "fullPoly"
                DlgEnable "polyNoD6"

                DlgEnable "customGroup"
               
                DlgVisible "d20Check"
                DlgVisible "d20Text"
                DlgVisible "d20Qty"

                DlgVisible "d12Check"
                DlgVisible "d12Text"
                DlgVisible "d12Qty"

                DlgVisible "d10Check"
                DlgVisible "d10Text"
                DlgVisible "d10Qty"

                DlgVisible "d8Check"
                DlgVisible "d8Text"
                DlgVisible "d8Qty"

                DlgVisible "d6Check"
                DlgVisible "d6Text"
                DlgVisible "d6Qty"

                DlgVisible "d4Check"
                DlgVisible "d4Text"
                DlgVisible "d4Qty"
            End If
            If ControlID$ = "d20Check" Then
                DlgEnable "d20Text"
                DlgEnable "d20Qty"
            End If
            If ControlID$ = "d12Check" Then
                DlgEnable "d12Text"
                DlgEnable "d12Qty"
            End If
            If ControlID$ = "d10Check" Then
                DlgEnable "d10Text"
                DlgEnable "d10Qty"
            End If
            If ControlID$ = "d8Check" Then
                DlgEnable "d8Text"
                DlgEnable "d8Qty"
            End If
            If ControlID$ = "d6Check" Then
                DlgEnable "d6Text"
                DlgEnable "d6Qty"
            End If
            If ControlID$ = "d4Check" Then
                DlgEnable "d4Text"
                DlgEnable "d4Qty"
            End If
            If ControlID$ = "ok" Then
                Exit Function 'By default, OK buton doesn't do anything.
            End If
    End Select
End Function


« Last Edit: April 07, 2023, 03:18:42 AM by TPS »
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: Calling Multiple Subroutines Not Working as Expected
« Reply #2 on: April 07, 2023, 02:40:45 PM »
TPS, thank you for your help!  I was not able to get a teach file working, but it started me in the right direction.

For future searchers, here's the VBScript I was able to get working with Mach3 on Windows 10 to run multiple G-code subroutines:
Code: [Select]
'Just an easy place to put the file path:
customSubroutine = "C:\Mach3\Subroutines\Make Custom.tap"

'Creates a new file or overwrites an existing file.
Open customSubroutine For Output As #1

'Each Print statement writes a new line to the file:
Print #1, "%" '<-- Subroutine boilerplate.
Print #1, "M98 (SomeSubroutine.tap)"
Print #1, "M98 (SomeOtherSubroutine.tap)"
Print #1, "M99" '<-- Subroutine boilerplate.
Print #1, "%" '<-- Subroutine boilerplate.

'Close the file:
Close #1

'Call the new subroutine file:
Code "M98 (Make Custom.tap)"