Attribute VB_Name = "ModVerificaAssi"

Public FuocoUp As Long
Public FuocoDown As Long
Public lngPosizioneInizialeX As Long
Public lngPosizioneInizialeY As Long
Public lngPosizioneProbeInizialeX As Long
Public lngPosizioneProbeInizialeY As Long
Public lngPosizioneProbeInizialeZ As Long
Public lngPosizioneProbeInizialeW As Long

Sub JogXYProbeVerificaAssi()
    If flagpuntali% = 2 Then Exit Sub
    'Call UpdatePosXYProbe
    If MovingProbe > 0 Then Exit Sub
    Static enabler%
    Static enabler1%
    Static enabler2%
    Static ocmd$
    Static ocmd2$
    Static tcmd2$
    Static spxx&
    Static spyy&
    Static clk%
    Static crk%
    Static cuk%
    Static cdk%
    Static ths%
    Static dt&
    Static a&
    Static dx&
    Static dY&
    Static accx$
    Static accy$
    Static spx&
    Static spy&
    Static intProbe1 As Integer
    Static intProbe2 As Integer

    ths% = 15
    dt& = timeGetTime()
    a& = GetCursorPos&(pt)
    thexx& = Screen.Width / (2 * Screen.TwipsPerPixelX)
    theyy& = Screen.Height / (2 * Screen.TwipsPerPixelY)
    dx& = pt.x - thexx&
    dY& = theyy& - pt.y
    'accelerazioni assi
    accx$ = AccMaxProbe
    accy$ = AccMaxProbe
    spx& = Int((mult& * dx&) / (dt& - odt&))
    spy& = Int((mult& * dY&) / (dt& - odt&))
    Select Case spx&
    Case Is < -SpeedMaxProbe
        spx& = -SpeedMaxProbe
    Case Is > SpeedMaxProbe
        spx& = SpeedMaxProbe
    End Select
    Select Case spy&
    Case Is < -SpeedMaxProbe
        spy& = -SpeedMaxProbe
    Case Is > SpeedMaxProbe
        spy& = SpeedMaxProbe
    End Select
    ck% = GetAsyncKeyState%(speedkey%) 'ctrl key
    lk% = GetAsyncKeyState%(&H25) 'left key
    uk% = GetAsyncKeyState%(&H26) 'up key
    rk% = GetAsyncKeyState%(&H27) 'right key
    dk% = GetAsyncKeyState%(&H28) 'down key
    esk% = GetAsyncKeyState%(27) 'escape key
    enk% = GetAsyncKeyState%(13) 'enter key
    EnablePulsProbe = GetAsyncKeyState%(&H1)
'    lmk% = GetAsyncKeyState%(&H1) 'left mouse button
'    rmk% = GetAsyncKeyState%(&H2) 'right mouse button
    shk% = GetAsyncKeyState%(&H10) 'shift key
    intProbe1 = GetAsyncKeyState(49)
    intProbe2 = GetAsyncKeyState(50)
    FuocoUp = GetAsyncKeyState(33)
    FuocoDown = GetAsyncKeyState(34)
    
    
    If (InJogCheProbe <> 1 And intProbe1 = 1) Then
        InJogCheProbe = 1
        BistPulsProbe = 1
    End If
    
    If (InJogCheProbe <> 2 And intProbe2 = 1) Then
        InJogCheProbe = 2
        BistPulsProbe = 0
    End If

    Select Case shk%
    Case 0
        enabler2% = enabler2% Or 1
    Case Else
        If enabler2% = 1 Then
            'ho spinto shift
            enabler2% = 0
            If dispstep% = 0 Then
                dispstep% = 1
            Else
                dispstep% = 0
            End If
        End If
    End Select

    Select Case esk%
    Case 0
        enabler% = enabler% Or 1
    Case Else
        If enabler% = 15 Then
            'ho spinto esc
            escjog% = 1
            enabler% = 0
        End If
    End Select

    Select Case enk%
    Case 0
        enabler% = enabler% Or 2
    Case Else
        If enabler% = 15 Then
            getv% = 1
            escjog% = 1
            enabler% = 0
            'ho spinto enter
        End If
    End Select
    
    Select Case lmk%
    Case 0
        enabler% = enabler% Or 4
    Case Else
        If enabler% = 15 Then
            getv% = 1
            escjog% = 1
            enabler% = 0
            'ho spinto il tasto sinistro
        End If
    End Select
    
    Select Case rmk%
    Case 0
        enabler% = enabler% Or 8
    Case Else
        If enabler% = 15 Then
            getv% = 1
            escjog% = 1
            enabler% = 0
            'ho spinto il tasto destro
        End If
    End Select
    
    Select Case lk%
    Case 0
        'non sto spigendo
        clk% = 0
        enabler1% = enabler1% Or 1
        If spxx& < 0 Then
            spxx& = 0
            kk% = 1
        End If
    Case Else
        If enabler1% = 15 Then
            clk% = clk% + 1
            If lk% = 1 Then clk% = 1
            If clk% > ths% Then
                clk% = ths%
                spxx& = -SpeedMaxProbe
            Else
                spxx& = -(SpeedMaxProbe / ths%) * clk%
            End If
            'sto spingendo
            kk% = 1
            If ck% <> 0 Then
                acx% = 1
                spxx& = 2 * spxx&
            End If
        Else
            clk% = 0
        End If
    End Select
    
    Select Case rk%
    Case 0
        'non sto spigendo
        crk% = 0
        enabler1% = enabler1% Or 2
        If spxx& > 0 Then
            spxx& = 0
            kk% = 1
        End If
    Case Else
        If enabler1% = 15 Then
            'sto spingendo
            crk% = crk% + 1
            If rk% = 1 Then crk% = 1
            If crk% > ths% Then
                crk% = ths%
                spxx& = SpeedMaxProbe
            Else
                spxx& = (SpeedMaxProbe / ths%) * crk%
            End If
            kk% = 1
            If ck% <> 0 Then
                spxx& = 2 * spxx&
                acx% = 1
            End If
        Else
            crk% = 0
        End If
    End Select
    
    Select Case uk%
    Case 0
        'non sto spigendo
        cuk% = 0
        enabler1% = enabler1% Or 4
        If spyy& > 0 Then
            spyy& = 0
            kk% = 1
        End If
    Case Else
        If enabler1% = 15 Then
            'sto spingendo
            cuk% = cuk% + 1
            If uk% = 1 Then cuk% = 1
            If cuk% > ths% Then
                cuk% = ths%
                spyy& = SpeedMaxProbe
            Else
                spyy& = (SpeedMaxProbe / ths%) * cuk%
            End If
            kk% = 1
            If ck% <> 0 Then
                spyy& = 2 * spyy&
                acy% = 1
            End If
        Else
            cuk% = 0
        End If
    End Select

    Select Case dk%
    Case 0
        'non sto spigendo
        cdk% = 0
        enabler1% = enabler1% Or 8
        If spyy& < 0 Then
            spyy& = 0
            kk% = 1
        End If
    Case Else
        'sto spingendo
        If enabler1% = 15 Then
            cdk% = cdk% + 1
            If dk% = 1 Then cdk% = 1
            If cdk% > ths% Then
                cdk% = ths%
                spyy& = -SpeedMaxProbe
            Else
                spyy& = -(SpeedMaxProbe / ths%) * cdk%
            End If
            kk% = 1
            If ck% <> 0 Then
                spyy& = 2 * spyy&
                acy% = 1
            End If
        Else
            cdk% = 0
        End If
    End Select
    
    If kk% = 1 Then
        accx$ = accmin$
        If acx% = 1 Then accx$ = AccMaxProbe
            accy$ = accmin$
            If acy% = 1 Then accy$ = AccMaxProbe
            spx& = spxx&
            spy& = spyy&
        End If
        
        If FuocoUp <> 0 And FuocoDown = 0 Then
           tcmd2$ = "JG ,,," + Str(-SpeedMaxFuoco / 4) + ";BG W"
        Else
           If FuocoDown = 0 Then
                tcmd2$ = "ST W"
           End If
        End If
        
        If FuocoDown <> 0 And FuocoUp = 0 Then
           tcmd2$ = "JG ,,," + Str(SpeedMaxFuoco / 4) + ";BG W"
        Else
           If FuocoUp = 0 Then
                tcmd2$ = "ST W"
           End If
        End If
        
        If odt& <> 0 And (spx& <> 0 Or spy& <> 0) Then
            Select Case InJogCheProbe
                Case 1
                    tcmd$ = "JG " + Str$(spx&) + "," + Str$(spy&) + ";" + "BG XY"
                Case 2
                    tcmd$ = "JG,, " + "-" + Str$(spx&) + ",-" + Str$(spy&) + ";" + "BG ZW"
            End Select
        Else
            Select Case InJogCheProbe
                Case 1
                    tcmd$ = "ST XY;SP" + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe)
                Case 2
                    tcmd$ = "ST ZW;SP,," + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe)
            End Select
        End If
        If escjog% = 1 Then
            Select Case InJogCheProbe
                Case 1
                    tcmd$ = "ST XY;SP" + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe) + ";AC " + accx$ + "," + accy$
                Case 2
                    tcmd$ = "ST ZW;SP,," + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe) + ";AC,," + accx$ + "," + accy$
            End Select
            tcmd2 = "ST W"
        End If
        Select Case InJogCheProbe
            Case 1
                tcmd$ = tcmd$ + ";AC" + accx$ + "," + accy$
            Case 2
                tcmd$ = tcmd$ + ";AC,," + accx$ + "," + accy$
        End Select
        If ocmd$ <> tcmd$ Then
            'Debug.Print tcmd$
            Call GalilCmdProbe(tcmd$, res$, ermsg$)
        End If
        ocmd$ = tcmd$
        
        If ocmd2$ <> tcmd2$ Then
           Call galilcmd(tcmd2$, res$, ermsg$)
        End If
        ocmd2$ = tcmd2$
        
        a& = SetCursorPos(thexx&, theyy&)
        odt& = dt&

        If escjog% = 1 Then
            If getv% = 1 Then
                Select Case InJogCheProbe
                    Case 1
                        Call GalilCmdProbe("TP XY", res$, ermsg$)
                    Case 2
                        Call GalilCmdProbe("TP ZW", res$, ermsg$)
                End Select
                cm% = InStr(res$, ",")
                If cm% > 0 Then
                    thex& = Val(Trim$(Left$(res$, cm% - 1)))
                    they& = Val(Trim$(Mid$(res$, cm% + 1)))
                End If
            End If
            Do
                esk% = GetAsyncKeyState%(27) 'escape key
                enk% = GetAsyncKeyState%(13) 'enter key
                lmk% = GetAsyncKeyState%(&H1) 'left mouse button
                rmk% = GetAsyncKeyState%(&H2) 'right mouse button
                tk% = esk% + enk% + lmk% + rmk%
                DoEvents
            Loop While tk% <> 0
            Call offjog
            enabler% = 0
            enabler1% = 0
            ocmd$ = ""
            ocmd2$ = ""
            'Select Case InJogCheProbe
            '    Case 1
            '        statjog% = 60
            '    Case 2
            '        statjog% = 61
 '           End Select
            
  '          Static GapPuntaliX As Long
   '         Static GapPuntaliY As Long
            Select Case InJogCheProbe
                    Case 1
                        Call GalilCmdProbe("TP XY", res$, ermsg$)
                    Case 2
                        Call GalilCmdProbe("TP ZW", res$, ermsg$)
            End Select
            cm% = InStr(res$, ",")
            If cm% > 0 Then
                thex& = Val(Trim$(Left$(res$, cm% - 1)))
                they& = Val(Trim$(Mid$(res$, cm% + 1)))
            End If
            Select Case statjog%
            Case 80
                lngPosizioneProbeInizialeX = thex&
                lngPosizioneProbeInizialeY = they&
                frmVerificaAssi.Visible = True
            Case 81
                lngPosizioneProbeInizialeZ = thex&
                lngPosizioneProbeInizialeW = they&
                frmVerificaAssi.Visible = True
     '           Case 60
       '             If getv% = 1 Then
      '                  'Posizione Probe A
        '                GapPuntaliX = thex& - Val(Frmedit.TxtRefXProbeA)
         '               GapPuntaliY = they& - Val(Frmedit.TxtRefYProbeA)
          '              PositionProbeAX = GapPuntaliX
           '             Frmedit.txtProbeAX.Tag = Trim(Str(PositionProbeAX))
            '            Frmedit.txtProbeAX.Text = formatter$(step2mm#(PositionProbeAX, stepxy!), 3, 2)
             '           PositionProbeAY = GapPuntaliY
              '          Frmedit.txtProbeAY.Tag = Trim(Str(PositionProbeAY))
               '         Frmedit.txtProbeAY.Text = formatter$(step2mm#(PositionProbeAY, stepxy!), 3, 2)
                '    End If
                  '  statjog% = 0
                 '   Frmedit.ZOrder 0
                   ' Frmedit.Visible = True

'                Case 61
 '                   If getv% = 1 Then
                        'Posizione Probe B
  '                      GapPuntaliX = thex& - Val(Frmedit.TxtRefXProbeB)
   '                     GapPuntaliY = they& - Val(Frmedit.TxtRefYProbeB)
    '                    PositionProbeBX = GapPuntaliX
     '                   Frmedit.txtProbeBX.Tag = Trim(Str(PositionProbeBX))
      '                  Frmedit.txtProbeBX.Text = formatter$(step2mm#(PositionProbeBX, stepxy!), 3, 2)
       '                 PositionProbeBY = GapPuntaliY
        '                Frmedit.txtProbeBY.Tag = Trim(Str(PositionProbeBY))
         '               Frmedit.txtProbeBY.Text = formatter$(step2mm#(PositionProbeBY, stepxy!), 3, 2)
          '          End If
           '         statjog% = 0
            '        Frmedit.ZOrder 0
             '       Frmedit.Visible = True
'
 '               Case 62
  '                  If getv% = 1 Then
   '                     'Reference Probe A
    '                    MasterRefXProbeA = thex&
     '                   Frmedit.TxtRefXProbeA.Tag = Trim(Str(MasterRefXProbeA))
      '                  Frmedit.TxtRefXProbeA.Text = formatter$(step2mm#(thex&, stepxy!), 3, 2)
       '                 MasterRefYProbeA = they&
        '                Frmedit.TxtRefYProbeA.Tag = Trim(Str(MasterRefYProbeA))
         '               Frmedit.TxtRefYProbeA.Text = formatter$(step2mm#(they&, stepxy!), 3, 2)
          '              OffSetPuntaliX = MasterRefXProbeB + MasterRefXProbeA
           '             OffSetPuntaliY = MasterRefYProbeB + MasterRefYProbeA
            '        End If
             '       statjog% = 0
              '      Frmedit.ZOrder 0
               '     Frmedit.Visible = True
                '
'                Case 63
 '                   If getv% = 1 Then
                        'Reference Probe B
  '                      MasterRefXProbeB = thex&
   '                     Frmedit.TxtRefXProbeB.Tag = Trim(Str(MasterRefXProbeB))
    '                    Frmedit.TxtRefXProbeB.Text = formatter$(step2mm#(thex&, stepxy!), 3, 2)
     '                   MasterRefYProbeB = they&
      '                  Frmedit.TxtRefYProbeB.Tag = Trim(Str(MasterRefYProbeB))
       '                 Frmedit.TxtRefYProbeB.Text = formatter$(step2mm#(they&, stepxy!), 3, 2)
        '                OffSetPuntaliX = MasterRefXProbeB + MasterRefXProbeA
         '               OffSetPuntaliY = MasterRefYProbeB + MasterRefYProbeA
          '          End If
           '         statjog% = 0
            '        Frmedit.ZOrder 0
             '       Frmedit.Visible = True
              '
            End Select
         
        End If
End Sub
