Attribute VB_Name = "Modpsi"
Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetAsyncKeyState% Lib "user32" (ByVal vKey As Long)
Private Declare Function timeGetTime Lib "winmm.dll" () As Long

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Global Const EM_GETLINE = &HC4
Global Const EM_GETLINECOUNT = 186
Global Const EM_LINEFROMCHAR = &HC9
Global Const EM_LINELENGTH = &HC1
'Global Const EM_LINELENGTH = &HFFFF
Global Const EM_LINEINDEX = &HBB
Global oldline& 'vecchia linea puntata
Global newline& 'nuova linea puntata
Global flagdopsi% 'se =1 devo eseguire le istruzioni del psi
Global palletStartPos$ 'posizione di start del pallet (in passi)
Global palletN% 'numero di pezzi sul pallet
Global palletcurn% 'numero step corrente
Global palletGap$ 'interasse pallet
Global pressesc%
Public thematrix$(0 To 26000)
'Public Amatrix!(0 To 1000) 'primo insieme di variabili single A
'Public Bmatrix!(0 To 1000) 'secondo insieme di variabili single B
'Public Cmatrix!(0 To 1000) 'terzo insieme di variabili single C
'Public Smatrix$(0 To 1000) 'variabili stringa S
Public pcounter&
Public printcolor&
Public printx& 'coord X di stampa
Public printy& ' coord y di stampa
Public stackmatrix&() 'matrice di stack coi puntatori di ritorno
Public stackindex% 'dice quanti valori ci sono nello stack
Public lasttable% '0 tavola A, 1 tavola B
Public intPsiNSerp As Integer

Sub colorline(thel&, thecolor&, startfrom&, endto&)
        startline& = startfrom&
        linelen& = startfrom& - endto& + 1
        If startfrom& = 0 Then
            startline& = SendMessage(Frminter.txtinter.hwnd, EM_LINEINDEX, thel&, 0&)
            linelen& = SendMessage(Frminter.txtinter.hwnd, EM_LINELENGTH, startline&, 0&)
        End If
        If startline& < 0 Then startline& = 0
        Frminter.txtinter.SelStart = startline&
        Frminter.txtinter.SelLength = linelen&
        Frminter.txtinter.SelColor = thecolor&
        Frminter.txtinter.SelLength = 0
End Sub
Sub psiliner(cl$, thel&, md%, thelines As RichTextBox)
        If cl$ = "" Then
            startline& = SendMessage(thelines.hwnd, EM_LINEINDEX, thel&, 0&)
            If startline& = -1 Then
                myline& = theline&
                If md% = 1 Then pcounter& = -1
                Exit Sub
            End If
            'line len
            linelen& = SendMessage(thelines.hwnd, EM_LINELENGTH, startline&, 0&)
            cl$ = Mid$(thelines.Text, startline& + 1, linelen&)
            'added for full line load
            If Mid$(thelines.Text, startline& + 1, 1) = "D" Then
                inline& = startline& + 1 + linelen&
                endline = Chr$(13) + Chr$(10)
                While Mid$(thelines.Text, inline&, 2) <> endline
                    cl$ = cl$ + Mid$(thelines.Text, inline&, 1)
                    inline& = inline& + 1
                Wend
                
            End If
            If md% = 1 Then
                'sono in esecuzione
                'aggiorno il program counter
                pcounter& = thel& + 1
            End If
        End If
        If cl$ = "" Then Exit Sub
        thecomment$ = ""
        cm% = InStr(cl$, "'")
        If cm% > 0 Then
            For i% = cm% - 1 To 1 Step -1
                ch$ = Mid$(cl$, i%, 1)
                Select Case ch$
                Case " "
                Case Else
                    cm% = i% + 1
                    Exit For
                End Select
            Next i%
            thecomment$ = Mid$(cl$, cm%)
            cl$ = Left$(cl$, cm% - 1)
            
        End If
        
        If md% = 0 Then
            op& = thelines.SelStart
            If startline& < 0 Then startline& = 0
            thelines.SelStart = startline&
            thelines.SelLength = linelen&
            thelines.SelText = ""
        End If
        acl$ = cl$ + "|"
        offset& = 0
        offset1& = 0
        Do
            Col% = InStr(acl$, "|")
            If Col% > 0 Then
                'pcl1$ = Left$(acl$, col% - 1)
                pcl$ = RTrim$(Left$(acl$, Col% - 1))
                'acl1$ = Mid$(acl$, col% + 1)
                acl$ = LTrim$(Mid$(acl$, Col% + 1))
                'gap1% = Len(pcl1$) - Len(pcl$)
                'gap2% = Len(acl1$) - Len(acl$)
                'Select Case op&
                    'Case Is > Len(pcl1$) + offset&
                        'op& = op& - gap1% - gap2% + 2
                'End Select
            End If
            If pcl$ <> "" Then
                If md% = 0 Then thelines.SelColor = 0
                Call execline(pcl$, fm&, tto&, clr&, thelines)
                thesel$ = pcl$
                If acl$ <> "" Then
                    thesel$ = thesel$ + " | "
                End If
                If md% = 0 Then
                    If startline& + offset& < 0 Then
                        startline& = 0
                        offset& = 0
                    End If
                    thelines.SelStart = startline& + offset&
                    thelines.SelText = thesel$
                    thelines.SelStart = startline& + offset& + fm& - 1
                    thelines.SelLength = tto& - fm& + 1
                    thelines.SelColor = clr&
                    thelines.SelLength = 0
                End If
                offset& = offset& + Len(thesel$)
            End If
        Loop While acl$ <> ""
        If thecomment$ <> "" And md% = 0 Then
            startline& = SendMessage(thelines.hwnd, EM_LINEINDEX, thel&, 0&)
            linelen& = SendMessage(thelines.hwnd, EM_LINELENGTH, startline&, 0&)
            If startline& + linelen& < 0 Then
                startline& = 0
                linelen& = 0
            End If
            thelines.SelStart = startline& + linelen&
            thelines.SelText = thecomment$
            thelines.SelStart = startline& + linelen&
            thelines.SelLength = Len(thecomment$)
            thelines.SelColor = RGB(0, 128, 0)
            thelines.SelLength = 0
        End If
        'Frminter.txtinter.SelStart = op&
        'Frminter.txtinter.SelLength = 0
        'Frminter.txtinter.SelColor = 0
End Sub
Sub loadpsi(fname$, mytext As RichTextBox)
mytext.Text = ""
un% = FreeFile
term$ = Chr$(13) + Chr$(10)
Open fname$ For Input As #un%
lc% = 0
While Not EOF(un%)
    lc% = lc% + 1
    Line Input #un%, cl$
    mytext.SelStart = 65535
    mytext.SelText = cl$ + term$
Wend
mytext.SetFocus
mytext.SelStart = 0
flagdopsi% = 0
For i% = 1 To lc%
    SendKeys ("{Down}")
    DoEvents
Next i%
mytext.SelStart = 0
Close #un%
End Sub
Sub savepsi(fname$, mytext As RichTextBox)
un% = FreeFile
term$ = Chr$(13) + Chr$(10)
Open fname$ For Output As #un%
Print #un%, mytext.Text;
Close #un%
End Sub
Sub openfile(fname$, myform As Form)
Call fileopendlg(myform, fname$)
If fname$ <> "" Then
    If Right$(fname$, 1) = Chr$(0) Then fname$ = Left$(fname$, Len(fname$) - 1)
    If InStr(fname$, "\") <= 0 Then fname$ = App.Path + "\" + fname$
    If UCase$(Right$(fname$, 4)) <> ".PSI" Then fname$ = fname$ + ".PSI"
End If
End Sub
Sub savefile(fname$, myform As Form)
Call filesavedlg(fname$, myform)
If fname$ <> "" Then
    If Right$(fname$, 1) = Chr$(0) Then fname$ = Left$(fname$, Len(fname$) - 1)
    If InStr(fname$, "\") <= 0 Then fname$ = App.Path + "\" + fname$
    If UCase$(Right$(fname$, 4)) <> ".PSI" Then fname$ = fname$ + ".PSI"
End If
End Sub
Sub fileopendlg(myform As Form, fname$)
    Dim ofn As OPENFILENAME
    fname1$ = Trim$(fname$) + Space$(255 - Len(Trim$(fname$)))
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = myform.hwnd
    ofn.hInstance = App.hInstance
    ofn.lpstrFilter = "Interpreter Files (*.psi)" + Chr$(0) + "*.psi" + Chr$(0)
    ofn.nMaxFile = 256
    ofn.lpstrFile = fname1$
    ofn.nMaxFileTitle = 256
    ofn.lpstrFileTitle = Space$(255)
    ofn.lpstrInitialDir = App.Path
    ofn.lpstrTitle = "Apri file"
    ofn.lpstrDefExt = "psi"
    ofn.Flags = 0
    ret& = GetOpenFileName(ofn)
    If ret& <> 0 Then
            fname$ = Trim$(ofn.lpstrFile)
    Else
            fname$ = ""
    End If
End Sub
Sub execline(cl$, fm&, tto&, clr&, thelines As RichTextBox)
'for read and analyze command laser file
'tipo: del parametro:
'1 variabile numerica
'2 variabile stringa
'3 costante numerica
'4 costante stringa
    Dim parmatrix$()
    Dim typematrix%()
    Dim indexmatrix!()
    ok% = 1
    fm& = 1
    tto& = Len(cl$)
    ccl$ = cl$
    ccl$ = LTrim$(ccl$)
    gp% = Len(cl$) - Len(ccl$)
    If ccl$ = "" Then Exit Sub
    fcl$ = Left$(ccl$, 1)
    Select Case fcl$
    Case "'"
            'commento
            clr& = RGB(0, 128, 0)
            fm& = 1
            tto& = Len(cl$)
            Exit Sub
    Case "#"
            'label
            ocl$ = Trim$(Mid$(cl$, 2))
            If ocl$ <> "" Then
                For i% = 1 To Len(ocl$)
                    lch$ = Mid$(ocl$, i%, 1)
                    Select Case lch$
                    Case "0" To "9"
                    Case Else
                        ok% = 0
                        Exit For
                    End Select
                Next i%
            Else
                    ok% = 0
            End If
            cl$ = "#" + ocl$
            clr& = RGB(0, 0, 255)
            fm& = 1
            tto& = Len(cl$)
            If ok% = 1 Then Exit Sub
    Case Else
        'istruzione generica
        If flagdopsi% = 1 Then
            If debugging% = 1 Then Call updatedebug(ccl$)
        End If
        bl% = InStr(ccl$, " ")
        If bl% <= 0 Then bl% = Len(ccl$) + 1
        If bl% > 0 Then
            tcmd$ = UCase$(Left$(ccl$, bl% - 1))
            Select Case tcmd$
            
            'Case "FOCUS"
            '    npar% = 1
            '    Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '    If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If typematrix%(1) = 2 Or typematrix%(1) = 4 Then: ok% = 0: coderr% = 1: GoTo endpoint
            '    If flagdopsi% > 0 Then
            '        Call gotoposw(parmatrix$(1))
            '    End If
            
            'Case "ADJAUTOFOCUS"
            '    If flagdopsi% > 0 Then
            '        Call AdjFocusPos
            '    End If
            
            'Case "ADJREF"
            '    'Aggiustamento ottico del reference
            '    npar% = 1
            '    Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '    If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If typematrix%(1) = 2 Or typematrix%(1) = 4 Then: ok% = 0: coderr% = 1: GoTo endpoint
            '    If flagdopsi% > 0 Then
            '        Call AdjRefVis
            '        thematrix$(indexmatrix!(1)) = Trim(Str(AdjRefVis))
            '    End If
           '
            Case "ADD"
                'somma stringhe
                npar% = 2
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% > 0 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 2 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If typematrix%(2) <> 2 And typematrix%(2) <> 4 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% = 1 Then
                    thev1$ = parmatrix$(1)
                    thev2$ = parmatrix$(2)
                    thev3$ = thev1$ + thev2$
                    thematrix$(indexmatrix!(1)) = thev3$
                End If
                
            'Case "ADDITEMREPORT"
            '    npar% = 5
            '    Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '    If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If flagdopsi% > 0 Then
            '        Call frmReport.AddItemReport(Trim(parmatrix(1)), Trim(Str(Round(Val(parmatrix(2)), 1))), Trim(Str(Round(Val(parmatrix(3)), 1))), Trim(Str(Round(Val(parmatrix(4)), 1))), Trim(Str(Round(Val(parmatrix(5)), 1))))
            '    End If
            
            'Case "ADDITEMREPORTTEST"
            '    npar% = 5
            '    Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '    If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If flagdopsi% > 0 Then
            '        Call frmReport.AddItemReport(Trim(parmatrix(1)), Trim(Str(Round(Val(parmatrix(2)), 4))), Trim(parmatrix(3)), Trim(parmatrix(4)), Trim(parmatrix(5)))
            '    End If
           '
            Case "BEEP"
                npar% = 0
                If flagdopsi% > 0 Then
                    Beep
                End If
                      
            Case "CLS"
                npar% = 0
                If flagdopsi% > 0 Then
                    Frmprint.Picprint.Cls
                    printx& = 0
                    printy& = 0
                End If
            
            Case "COLOR"
                'colore del carattere di stampa
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                Select Case UCase$(parmatrix$(1))
                Case "BLUE"
                    mprintcolor& = RGB(0, 0, 256)
                Case "GREEN"
                    mprintcolor& = RGB(0, 128, 0)
                Case "RED"
                    mprintcolor& = RGB(256, 0, 0)
                Case Else
                    ok% = 0: coderr% = 1: GoTo endpoint
                End Select
                If flagdopsi% > 0 Then
                    printcolor& = mprintcolor&
                End If
           
           Case "D"
                'read code from command file
                'plan change to -1 in future, minimal is 1 paramets
                npar% = -1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                'error check
                If typematrix%(1) = 2 Or typematrix%(1) = 4 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                    If ((UBound(parmatrix$) - LBound(parmatrix$) + 1) = 1) Then
                        cutn% = Val(parmatrix$(1))
                        'lasttable changing numerink laser move
                        lt% = lasttable%
                        'realize cut
                        Call executd2700(cutn%, lt%, 1, cuterr%, 1, parmatrix$)
                    Else
                        'now, it is not easy cut
                        
                        'check cuts types
                        b_ok = True
                        For n = LBound(parmatrix$) To UBound(parmatrix$) Step 1
                            cutn% = Val(parmatrix$(n))
                            If cutn% <= UBound(TrimMatrix) Then
                                myrecord = TrimMatrix(cutn%)
                                
                                If Val(cleanval(myrecord.cutType)) <> 7 Then
                                    b_ok = False
                                End If
                            Else
                                b_ok = False
                            End If
                        Next n
                        
                        'secundary test of tie in lines
                        'this is tested in GerberConvertor first
                        If b_ok = True Then
                            cutn% = Val(parmatrix$(1))
                            'myrecordB = TrimMatrix(cutn%)
                            For n = LBound(parmatrix$) + 1 To UBound(parmatrix$) Step 1
                                'myrecordA = myrecordB
                                
                                cutn% = Val(parmatrix$(n))
                                'myrecordB = TrimMatrix(cutn%)
                                
                                If TrimMatrix(cutn% - 1).stopx <> TrimMatrix(cutn%).startx Then
                                    b_ok = False
                                End If
                                If TrimMatrix(cutn% - 1).stopy <> TrimMatrix(cutn%).starty Then
                                    b_ok = False
                                End If
                            Next n
                        End If
                        
                        If b_ok = True Then
                            'call edited executd2700 for cutting
                            cutn% = Val(parmatrix$(1))
                            lt% = lasttable%
                            Call executd2700(cutn%, lt%, 1, cuterr%, 1, parmatrix$)
                        Else
                            Dim byone$(1)
                            For n = LBound(parmatrix$) To UBound(parmatrix$) Step 1
                                cutn% = Val(parmatrix$(n))
                                byone$(1) = parmatrix$(n)
                                lt% = lasttable%
                                Call executd2700(cutn%, lt%, 1, cuterr%, 1, byone$)
                            Next n
                        End If
                    End If
                End If
            
            Case "D?"
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) = 2 Or typematrix%(1) = 4 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                    cutn% = Val(parmatrix$(1))
                    lt% = lasttable%
                    Call executd2700(cutn%, lt%, 0, cuterr%, 1, parmatrix$)
                    'Call executc(cutn%, 1, cuterr%)
                End If
            
            'Case "DELAYATRIM"
            '    'setta il valore del DelayAfterTrim
            '    npar% = 1
            '    Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '    If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
            '    If flagdopsi% > 0 Then
            '       DelayAfterTrim = Val(parmatrix$(1))
            '    End If
            
            Case "DIV", "MUL", "SUM", "SUB"
                npar% = 3
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If typematrix%(2) <> 1 And typematrix%(2) <> 3 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If typematrix%(3) <> 1 And typematrix%(3) <> 3 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                    theval1! = Val(parmatrix$(2))
                    theval2! = Val(parmatrix$(3))
                    op% = 1
                    If tcmd$ = "MUL" Then op% = 2
                    If tcmd$ = "SUM" Then op% = 3
                    If tcmd$ = "SUB" Then op% = 4
                    Select Case op%
                    Case 1
                        'divisione
                        If theval2! = 0 Then ok% = 0: coderr% = 10: GoTo endpoint
                        theval3! = theval1! / theval2!
                    Case 2
                        'molt.
                        theval3! = theval1! * theval2!
                    Case 3
                        'somma
                        theval3! = theval1! + theval2!
                    Case 4
                        'sottraz.
                        theval3! = theval1! - theval2!
                    End Select
                    thematrix$(indexmatrix!(1)) = Trim$(Str(theval3!))
                End If
            
            Case "DOOR"
                'attende che il portello anteriore venga aperto
                npar% = 0
                If flagdopsi% > 0 Then
                    Do
                        DoEvents
                        ek% = GetAsyncKeyState%(27) 'esc key
                        If ek% <> 0 Then
                            pressesc% = 1
                            Exit Do
                        End If
                        Call readdevice(0, 0, 5)
                        If d0.I_Switch_Portello_Ant_Open = 1 Then Exit Do
                    Loop
                End If
                
                
            Case "DSLOPE"
            npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                   If parmatrix$(1) = "DOWN" Then
                      DSlope = 0
                   End If
                   If parmatrix$(1) = "UP" Then
                      DSlope = 1
                   End If
                End If
            
            Case "DVOLTAGE"
            npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                   DVoltage = CSng(Val(parmatrix$(1)))
                End If
            
            Case "DOHM"
            'setta il valore del comparatore per taglio resistivo tramite PSI
            npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                   DResistenza = CSng(Val(parmatrix$(1)))
                End If
                            
            Case "EQU"
                npar% = 2
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                Select Case typematrix%(1)
                Case 1
                    Select Case typematrix%(2)
                      Case 1, 3
                        op% = 1
                    Case Else
                        ok% = 0: coderr% = 1: GoTo endpoint
                    End Select
                Case 2
                    Select Case typematrix%(2)
                    Case 2, 4
                        op% = 2
                    Case Else
                        ok% = 0: coderr% = 1: GoTo endpoint
                    End Select
                Case Else
                    ok% = 0: coderr% = 1: GoTo endpoint
                End Select
                 If flagdopsi% > 0 Then
                    Select Case op%
                    Case 1
                        thematrix$(indexmatrix!(1)) = Trim$(Str(Val(parmatrix$(2))))
                    Case 2
                        thematrix$(indexmatrix!(1)) = parmatrix$(2)
                    End Select
                End If
                
            Case "GOTO", "GOSUB"
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                Select Case typematrix(1)
                Case 1, 3
                Case Else
                        coderr% = 1: ok% = 0: GoTo endpoint
                End Select
                If flagdopsi% > 0 Then
                    lbl$ = "#" + parmatrix$(1)
                    labelok& = InStr(thelines.Text, lbl$)
                    'meglio se
                    'labelok& = InStr(chr$(10)+thelines.Text,chr$(10)+ lbl$)
                    If labelok& > 0 Then
                        If tcmd$ = "GOSUB" Then
                            stackindex% = stackindex% + 1
                            ReDim Preserve stackmatrix&(1 To stackindex%)
                            stackmatrix&(stackindex%) = pcounter&
                        End If
                        'meglio se
                        'pcounter& = SendMessage(thelines.hwnd, EM_LINEFROMCHAR, labelok&+1, 0&) + 1
                        pcounter& = SendMessage(thelines.hwnd, EM_LINEFROMCHAR, labelok&, 0&) + 1
                    Else
                        'label not found
                        pcounter& = -2
                        ok% = 0: coderr = 20: GoTo endpoint
                    End If
                End If
            
            'Case "INPUT"
            '    'attende input da utente e lo assegna alla variabile
            '    npar% = 1
            '    Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '    If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
            '    If flagdopsi% > 0 Then
            '        pmpt$ = Trim$(Mid$(ccl$, bl% + 1)) + "=? Type a value and press Enter"
            '        ai$ = InputBox$(pmpt$)
            '        If ai$ <> "" Then
            '            thematrix$(indexmatrix!(1)) = ai$
            '        End If
            '    End If
            
            Case "JLESS", "JGREAT", "JEQUAL"
                op% = 1 'greater
                If tcmd$ = "JLESS" Then op% = 2
                If tcmd$ = "JEQUAL" Then op% = 3
                npar% = 3
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                typeok% = 1
                For i% = 1 To 3
                    Select Case typematrix(i%)
                    Case 1, 3
                    Case Else
                        typeok% = 0
                        Exit For
                    End Select
                Next i%
                If typeok% = 0 Then ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                    istrue% = 0
                    theval1! = Val(parmatrix(1))
                    theval2! = Val(parmatrix(2))
                    Select Case op%
                    Case 1
                            If theval1! > theval2! Then istrue% = 1
                    Case 2
                            If theval1! < theval2! Then istrue% = 1
                    Case 3
                            If theval1! = theval2! Then istrue% = 1
                    End Select
                    If istrue% = 1 Then
                        'condizione verificata
                        lbl$ = "#" + parmatrix$(3)
                        labelok& = InStr(Chr$(10) + thelines.Text, Chr$(10) + lbl$)
                        If labelok& > 0 Then
                            pcounter& = SendMessage(thelines.hwnd, EM_LINEFROMCHAR, labelok&, 0&) + 1
                            'meglio se
                            'pcounter& = SendMessage(thelines.hwnd, EM_LINEFROMCHAR, labelok&+1, 0&) + 1
                        
                        Else
                            'label not found
                            pcounter& = -2
                            ok% = 0: coderr = 20: GoTo endpoint
                        End If
                    End If
                End If
            
            Case "KEY"
                'attesa pressione tasto SpaceBar
                npar% = 0
                If flagdopsi% > 0 Then
                    Call checksafety
                    ent% = GetAsyncKeyState(32)
                    ent% = GetAsyncKeyState(32)
                    ent1% = GetAsyncKeyState(27)
                    ent1% = GetAsyncKeyState(27)
                    'Frminter.txtimm.SelStart = 65535
                    'Frminter.txtimm.SelText = "Press the Spacebar Key" + Chr$(13) + Chr$(10)
                    'Frminter.txtimm.SelStart = 65535
                    Call showstatus("Press the spacebar key to continue")
                    Do
                        DoEvents
                        ent% = GetAsyncKeyState(32)
                        ent1% = GetAsyncKeyState(27)
                        If ent% <> 0 Then Exit Do
                        If ent1% <> 0 Then
                            pressesc% = 1
                            Exit Do
                        End If
                    Loop
                    Do
                        DoEvents
                        ent% = GetAsyncKeyState(32)
                        If ent% = 0 Then Exit Do
                    Loop
                End If
            
            Case "KEY1"
                'attesa pressione tasto SpaceBar
                npar% = 0
                If flagdopsi% > 0 Then
                    Call checksafety
                    ent% = GetAsyncKeyState(32)
                    ent% = GetAsyncKeyState(32)
                    ent1% = GetAsyncKeyState(27)
                    ent1% = GetAsyncKeyState(27)
                    'Frminter.txtimm.SelStart = 65535
                    'Frminter.txtimm.SelText = "Press the Spacebar Key" + Chr$(13) + Chr$(10)
                    'Frminter.txtimm.SelStart = 65535
                    Do
                        DoEvents
                        ent% = GetAsyncKeyState(32)
                        ent1% = GetAsyncKeyState(27)
                        If ent% <> 0 Then Exit Do
                        If ent1% <> 0 Then
                            pressesc% = 1
                            Exit Do
                        End If
                    Loop
                    Do
                        DoEvents
                        ent% = GetAsyncKeyState(32)
                        If ent% = 0 Then Exit Do
                    Loop
                End If
            
            'Case "KILLF"
            '    'kills work file #n
            '    npar% = 1
            '    Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '    If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If typematrix%(1) <> 1 And typematrix%(1) <> 3 Then coderr% = 1: ok% = 0: GoTo endpoint
            '    If flagdopsi% > 0 Then
            '        psifile$ = Trim$(Str$(Val(parmatrix(1))))
            '        psifile$ = thepath$ + "WORK\work" + psifile$ + ".txt"
            '        un% = FreeFile
            '        Open psifile$ For Output As #un%
            '        Close #un%
            '    End If
            
            'Case "LOAD_TABLE"
            'Dim intCont As Integer
            'npar% = 0
            '     Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '      If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '      If flagdopsi% > 0 Then
            '        intCont = 1
            '        Call ProbeUp
             '       Delay (300)
            '        Call gotoposz(Str(PositionTableIn))
            '        Do While moving% <> 0
            '           Delay (300)
            '           intCont = intCont + 1
             '          If intCont >= 100 Then
             '             Call galilcmd("ST Z", res$, ermsg$)
              '            MsgBox "Errore: posizionamento tavola non riuscito", vbExclamation
              '            Exit Do
              '         End If
             '       Loop
             '       Delay (500)
             '     End If
                  
            
            
            
            Case "MESSAGE"
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    If typematrix%(1) = 1 Or typematrix%(1) = 3 Then
                        theprint$ = Trim$(Str(Val(parmatrix$(1))))
                    Else
                        theprint$ = parmatrix$(1)
                    End If
                    Call showstatus(theprint$)
                End If
            
            Case "MSG"
                'message box su video
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    If typematrix%(1) = 1 Or typematrix%(1) = 3 Then
                        theprint$ = Trim$(Str(Val(parmatrix$(1))))
                    Else
                        theprint$ = parmatrix$(1)
                    End If
                    MsgBox (theprint$)
                End If
                
            Case "PRINT"
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    If typematrix%(1) = 1 Or typematrix%(1) = 3 Then
                        printval! = Int(Val(parmatrix$(1)) * 100)
                        printval! = printval! / 100
                        theprint$ = Trim$(Str(printval!))
                        ptt% = InStr(theprint$, ".")
                        If ptt% > 0 Then
                            theprint$ = theprint$ + "00"
                            theprint$ = Left$(theprint$, ptt% + 2)
                        Else
                            theprint$ = theprint$ + ".00"
                        End If
                    Else
                        theprint$ = parmatrix$(1)
                    End If
                    Frmprint.Show
                    Frmprint.Picprint.ForeColor = printcolor&
                    chw& = Frmprint.Picprint.TextWidth("A")
                    chh& = Frmprint.Picprint.TextHeight("A")
                    If printx& = 0 Then printx& = chw& / 2
                    If printy& = 0 Then printy& = chh& / 2
                    thex1& = printx&
                    they1& = printy&
                    thex2& = printx& + (Len(theprint$) * chw&)
                    they2& = printy& + chh&
                    Frmprint.Picprint.Line (thex1&, they1&)-(thex2&, they2&), Frmprint.Picprint.BackColor, BF
                    Frmprint.Picprint.CurrentX = printx&
                    Frmprint.Picprint.CurrentY = printy&
                    Frmprint.Picprint.Print theprint$
                    'printx& = chw& / 2
                    'printy& = chh& / 2
                    printx& = Frmprint.Picprint.CurrentX
                    printy& = Frmprint.Picprint.CurrentY
                    printcolor& = 0
                    Frmprint.Refresh
                    Frmprint.ZOrder 0
                
                End If
            
            Case "PRINTA"
                '5 decimali
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    If typematrix%(1) = 1 Or typematrix%(1) = 3 Then
                        printval! = CInt(Val(parmatrix$(1)) * 100000)
                        printval! = printval! / 100000
                        theprint$ = Trim$(Str(printval!))
                        ptt% = InStr(theprint$, ".")
                        If ptt% > 0 Then
                            theprint$ = theprint$ + "00000"
                            theprint$ = Left$(theprint$, ptt% + 5)
                        Else
                            theprint$ = theprint$ + ".00000"
                        End If
                    Else
                        theprint$ = parmatrix$(1)
                    End If
                    Frmprint.Show
                    Frmprint.Picprint.ForeColor = printcolor&
                    chw& = Frmprint.Picprint.TextWidth("A")
                    chh& = Frmprint.Picprint.TextHeight("A")
                    If printx& = 0 Then printx& = chw& / 2
                    If printy& = 0 Then printy& = chh& / 2
                    thex1& = printx&
                    they1& = printy&
                    thex2& = printx& + (Len(theprint$) * chw&)
                    they2& = printy& + chh&
                    Frmprint.Picprint.Line (thex1&, they1&)-(thex2&, they2&), Frmprint.Picprint.BackColor, BF
                    Frmprint.Picprint.CurrentX = printx&
                    Frmprint.Picprint.CurrentY = printy&
                    Frmprint.Picprint.Print theprint$
                    'printx& = chw& / 2
                    'printy& = chh& / 2
                    printx& = Frmprint.Picprint.CurrentX
                    printy& = Frmprint.Picprint.CurrentY
                    printcolor& = 0
                    Frmprint.Refresh
                    Frmprint.ZOrder 0
                End If
            
            Case "PRINTB"
                'assoluto
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    If typematrix%(1) = 1 Or typematrix%(1) = 3 Then
                        printval! = Val(parmatrix$(1))
                        theprint$ = Trim$(Str(printval!))
                    Else
                        theprint$ = parmatrix$(1)
                    End If
                    Frmprint.Show
                    Frmprint.Picprint.ForeColor = printcolor&
                    chw& = Frmprint.Picprint.TextWidth("A")
                    chh& = Frmprint.Picprint.TextHeight("A")
                    If printx& = 0 Then printx& = chw& / 2
                    If printy& = 0 Then printy& = chh& / 2
                    thex1& = printx&
                    they1& = printy&
                    thex2& = printx& + (Len(theprint$) * chw&)
                    they2& = printy& + chh&
                    Frmprint.Picprint.Line (thex1&, they1&)-(thex2&, they2&), Frmprint.Picprint.BackColor, BF
                    Frmprint.Picprint.CurrentX = printx&
                    Frmprint.Picprint.CurrentY = printy&
                    Frmprint.Picprint.Print theprint$
                    'printx& = chw& / 2
                    'printy& = chh& / 2
                    printx& = Frmprint.Picprint.CurrentX
                    printy& = Frmprint.Picprint.CurrentY
                    printcolor& = 0
                    Frmprint.Refresh
                    Frmprint.ZOrder 0
                End If
                
            Case "PRINTF"
                'prints on work file #n (if "" print crlf)
                npar% = 2
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 And typematrix%(1) <> 3 Then coderr% = 1: ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    psifile$ = Trim$(Str$(Val(parmatrix(1))))
                    psifile$ = thepath$ + "WORK\work" + psifile$ + ".txt"
                    un% = FreeFile
                    Open psifile$ For Append As #un%
                        If parmatrix(2) = "" Then
                                Print #un%, Chr$(13) + Chr$(10);
                        Else
                            Select Case Trim$(UCase$(parmatrix(2)))
                            Case "DATE"
                                Print #un%, Date$;
                            Case "TIME"
                                Print #un%, Time$;
                            Case "TIMER"
                                Print #un%, Timer;
                            Case Else
                                Print #un%, parmatrix(2);
                            End Select
                        End If
                    Close #un%
                    
                End If
                
            Case "PRINTR"
                'prints on work file #n (if "" print crlf)
                npar% = 6
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 And typematrix%(1) <> 3 Then coderr% = 1: ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    psifile$ = Trim$(Str$(Val(parmatrix(1))))
                    psifile$ = thepath$ + "WORK\Report" + psifile$ + ".txt"
                    un% = FreeFile
                    Open psifile$ For Append As #un%
                        Print #un%, UCase$(parmatrix(2)) + ";" + UCase$(parmatrix(3)) + ";" + UCase$(parmatrix(4)) + ";" + UCase$(parmatrix(5)) + ";" + UCase$(parmatrix(6))
                    Close #un%
                    
                End If
            
            'Case "REFPOS"
            '    npar% = 2
            '    Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '    If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
            '    If typematrix%(2) <> 1 And typematrix%(2) <> 3 Then: ok% = 0: coderr% = 1: GoTo endpoint
            '    If flagdopsi% > 0 Then
            '        PsiTLasReferenceX = Str(Val(parmatrix$(1)))
            '        PsiTLasReferenceY = Str(Val(parmatrix$(2)))
            '        '''mario''' Call AdjRefVis
            '        If Val(PsiTLasReferenceX) <> 0 And Val(PsiTLasReferenceY) <> 0 Then
            '            Static GapPsiRefX As Long
            '            Static GapPsiRefY As Long
       '
        '                GapPsiRefX = Val(PsiTLasReferenceX) - Val(TLasReferenceX$)
         '               GapPsiRefY = Val(PsiTLasReferenceY) - Val(TLasReferenceY$)
        '
         '               TLasReferenceX$ = PsiTLasReferenceX
          '              TLasReferenceY$ = PsiTLasReferenceY
           '             PsiTLasReferenceX = ""
            '            PsiTLasReferenceY = ""
        '
         '               RefX& = Val(TLasReferenceX$)
          '              RefY& = Val(TLasReferenceY$)
           '             Frmedit.txtRefX.Tag = TLasReferenceX$
            '            Frmedit.txtRefX = formatter$(step2mm#(Val(TLasReferenceX$), stepxy!), 3, 2)
             '           Frmedit.txtRefY.Tag = TLasReferenceY$
              '          Frmedit.txtRefY = formatter$(step2mm#(Val(TLasReferenceY$), stepxy!), 3, 2)
        '
         '               '''mario''' Correzzione Puntali
          '              MasterRefXProbeA = MasterRefXProbeA + GapPsiRefX
           '             Frmedit.TxtRefXProbeA.Tag = Str(MasterRefXProbeA)
            '            Frmedit.TxtRefXProbeA.TabIndex = formatter(step2mm(MasterRefXProbeA, stepxy!), 3, 2)
        '
         '               MasterRefYProbeA = MasterRefYProbeA + GapPsiRefY
          '              Frmedit.TxtRefYProbeA.Tag = Str(MasterRefYProbeA)
           '             Frmedit.TxtRefYProbeA.TabIndex = formatter(step2mm(MasterRefYProbeA, stepxy!), 3, 2)
        '
         '               MasterRefXProbeB = MasterRefXProbeB - GapPsiRefX
          '              Frmedit.TxtRefXProbeB.Tag = Str(MasterRefXProbeB)
           '             Frmedit.TxtRefXProbeB.TabIndex = formatter(step2mm(MasterRefXProbeB, stepxy!), 3, 2)
        '
            '            MasterRefYProbeB = MasterRefYProbeB - GapPsiRefY
         '               Frmedit.TxtRefYProbeB.Tag = Str(MasterRefYProbeB)
          '              Frmedit.TxtRefYProbeB.TabIndex = formatter(step2mm(MasterRefYProbeB, stepxy!), 3, 2)
           '             Call gotoposxy(TLasReferenceX$, TLasReferenceY$)
             '           Do
              '              DoEvents
               '         Loop While moving% > 0
                '    End If
                'End If
        
            Case "PROBE_DOWN"   'abbassa i puntali rosa
          
                 npar% = 0
                 Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                  If flagexit% = 1 Then ok% = 0: GoTo endpoint
                  If flagdopsi% > 0 Then
                     Call ProbeDown
                  End If
            
            Case "PROBE_UP"   'alza i puntali rosa
          
                 npar% = 0
                 Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                  If flagexit% = 1 Then ok% = 0: GoTo endpoint
                  If flagdopsi% > 0 Then
                     Call ProbeUp
                  End If
            
            Case "READ_RES" 'legge il valore di una resistenza
            npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                   Call rr2700(theval!, 0, 0, 0, 6)
                   thematrix$(indexmatrix!(1)) = Str(theval!)
                End If
            
            Case "READ_CURR" 'legge la corrente
            npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                   Call ReadCurr(theval!)
                   thematrix$(indexmatrix!(1)) = Str(theval!)
                End If
            
            Case "READ_VOLT" 'legge la tensione
            npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                   Call rv2700(theval!, 0, 0, 6)
                   thematrix$(indexmatrix!(1)) = Str(theval!)
                End If
               
            'Case "READ_AVER" 'legge una tensione media
            'npar% = 1
            '    Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '    If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
             '   If flagdopsi% > 0 Then
              '     Call ReadAverage(theval!)
           '        thematrix$(indexmatrix!(1)) = Str(theval!)
            '    End If
             '
            Case "RETURN"
            npar% = 0
            If flagdopsi% > 0 Then
                If stackindex% = 0 Then ok% = 0: coderr% = 30: GoTo endpoint
                pcounter& = stackmatrix&(stackindex%)
                stackindex% = stackindex% - 1
            End If
                     
            'Case "REPORT_OFF"
             '   npar% = 0
              '  If flagdopsi% > 0 Then
               '     Unload frmReport
                'End If
                
'            Case "REPORT_ON"
 '               npar% = 0
  '              If flagdopsi% > 0 Then
   '                 Load frmReport
    '                frmReport.Visible = True
     '           End If
                
      '      Case "RIPRISTINA_REF"
       '         npar% = 0
        '        If flagdopsi% > 0 Then
         '           Call RipristinaReference
          '      End If
                
           ' Case "SALVA_REF"
            '    npar% = 0
             '   If flagdopsi% > 0 Then
              '      Call SalvaReference
               ' End If
            
'            Case "STEP"
 '               npar% = 0
  '              If flagdopsi% > 0 Then
   '                 Call ProbeUp
    '                intContatoreTotale = intContatoreTotale + 1
     '               Call AssegnaValoreVariabile("E10", CLng(intContatoreTotale))
      '              If intContatoreTotale <= intNumStepX * intNumStepY Then
       '                 If (intContatoreColonna Mod 2) <> 0 Then
        '                    intContatoreRiga = intContatoreRiga + 1
         '                   Call AssegnaValoreVariabile("E11", CLng(intContatoreRiga))
          '                  If intContatoreRiga <= intNumStepX Then
           '                     TLasReferenceX$ = Str(Val(TLasReferenceX$) + lngDeltaStepX)
            '                    MasterRefXProbeA = Str(Val(MasterRefXProbeA) + lngDeltaStepX)
             '                   MasterRefXProbeB = Str(Val(MasterRefXProbeB) - lngDeltaStepX)
              '              Else
               '                 intContatoreRigaRitorno = 1
                '                Call AssegnaValoreVariabile("E13", CLng(intContatoreRigaRitorno))
                 '               intContatoreColonna = intContatoreColonna + 1
                  '              Call AssegnaValoreVariabile("E12", CLng(ntContatoreColonna))
                   '             If intContatoreColonna <= intNumStepY Then
                    '                TLasReferenceY$ = Str(Val(TLasReferenceY$) + lngDeltaStepY)
                     '               MasterRefYProbeA = Str(Val(MasterRefYProbeA) + lngDeltaStepY)
                      '              MasterRefYProbeB = Str(Val(MasterRefYProbeB) - lngDeltaStepY)
                       '         End If
                        '    End If
'                        Else
 '                           intContatoreRigaRitorno = intContatoreRigaRitorno + 1
  '                          Call AssegnaValoreVariabile("E13", CLng(intContatoreRigaRitorno))
   '                         If intContatoreRigaRitorno <= intNumStepX Then
    '                            TLasReferenceX$ = Str(Val(TLasReferenceX$) - lngDeltaStepX)
     '                           MasterRefXProbeA = Str(Val(MasterRefXProbeA) - lngDeltaStepX)
      '                          MasterRefXProbeB = Str(Val(MasterRefXProbeB) + lngDeltaStepX)
       '                     Else
        '                        intContatoreRiga = 1
         '                       Call AssegnaValoreVariabile("E11", CLng(intContatoreRiga))
          '                      intContatoreColonna = intContatoreColonna + 1
           '                     Call AssegnaValoreVariabile("E12", CLng(intContatoreColonna))
            '                    If intContatoreColonna <= intNumStepY Then
             '                       TLasReferenceY$ = Str(Val(TLasReferenceY$) + lngDeltaStepY)
              '                      MasterRefYProbeA = Str(Val(MasterRefYProbeA) + lngDeltaStepY)
               '                     MasterRefYProbeB = Str(Val(MasterRefYProbeB) - lngDeltaStepY)
                '                End If
                 '           End If
                  '      End If
                   ' Else
                    '    Call AssegnaValoreVariabile("E14", 1)
'                    End If
                    'Delay (1000)
 '               End If
            
            Case "STOP", "END"
                npar% = 0
                If flagdopsi% > 0 Then
                    pcounter& = -1
                End If
            
            Case "STR"
                'trasforma un numero in stringa
                npar% = 2
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 2 Then coderr% = 1: ok% = 0: GoTo endpoint
                If typematrix%(2) <> 1 And typematrix%(2) <> 3 Then coderr% = 1: ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    thematrix$(indexmatrix!(1)) = parmatrix$(2)
                End If
            
'            Case "TABLE_IN"
 '               npar% = 0
  '              If flagdopsi% > 0 Then
   '                 'Call SalvaReference
    '                Call ProbeUp
     '               Call gotoposz(Str(PositionTableIn))
      '              Do While Not moving% = 0
       '                 DoEvents
        '            Loop
         '           FlagTavolaInOut = 1
          '          FrmEuro.Toolbar1.Buttons(10).value = tbrPressed
           '     End If
            '
'            case "TABLE_OUT"
 '               npar% = 0
  '              If flagdopsi% > 0 Then
   '                 Call ProbeUp
    '                 'Call RipristinaReference
     '                Call gotoposz(Str(PositionTableOut))
      '               Do While Not moving% = 0
       '                 DoEvents
        '             Loop
         '            FlagTavolaInOut = 0
          '           FrmEuro.Toolbar1.Buttons(10).value = tbrUnpressed
           '     End If
            '
'            Case "TABLE_POS"
'                npar% = 1
'                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
'                If flagexit% = 1 Then ok% = 0: GoTo endpoint
'                If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
'                If flagdopsi% > 0 Then
'
'                End If
            
            Case "U"
                'unconditonal trim per marcatura
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) = 2 Or typematrix%(1) = 4 Then: ok% = 0: coderr% = 1: GoTo endpoint
                If flagdopsi% > 0 Then
                    cutn% = Val(parmatrix$(1))
                    lt% = lasttable%
                    Call executu(cutn%, lt%, 1, cuterr%)
                End If
            
'            Case "UNLOAD_TABLE"
 '           Dim intCont2 As Integer
  '          npar% = 0
   '              Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
    '              If flagexit% = 1 Then ok% = 0: GoTo endpoint
     '             If flagdopsi% > 0 Then
      '              intCont2 = 1
       '             Call ProbeUp
        '            Delay (300)
         '           Call gotoposz(Str(PositionTableOut))
          '          Do While moving% <> 0
           '            Delay (300)
            '           intCont2 = intCont2 + 1
             '          If intCont2 >= 100 Then
              '            Call galilcmd("ST Z", res$, ermsg$)
               '           MsgBox "Errore: posizionamento tavola non riuscito", vbExclamation
                '          Exit Do
                 '      End If
                  '  Loop
'                  End If
 '
  '
            Case "VAL"
                'trasforma una stringa in numero
                npar% = 2
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                If typematrix%(1) <> 1 Then coderr% = 1: ok% = 0: GoTo endpoint
                If typematrix%(2) <> 2 And typematrix%(2) <> 4 Then coderr% = 1: ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    thematrix$(indexmatrix!(1)) = parmatrix$(2)
                End If
            
            'Case "VUOTO_ON"
            'npar% = 0
            '     Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '      If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '      If flagdopsi% > 0 Then
            '        Call InizializzaVariabiliPerTaratura
            '         Call setoutput(0, d0.O_Vuoto_Tavola)
            '         FrmEuro.Toolbar1.Buttons(16).value = tbrPressed
            '         Delay (500)
            '         If d0.I_Sensore_Vuoto_Tavola = 0 Then
            '            MsgBox "ERRORE: non è stato possibile creare correttamente il vuoto", vbExclamation
            '            Call resetoutput(0, d0.O_Vuoto_Tavola)
            '            FrmEuro.Toolbar1.Buttons(16).value = tbrUnpressed
            '            pressesc% = 1
            '         End If
            '      End If
            
            'Case "VUOTO_OFF"
            'npar% = 0
            '     Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
            '      If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '      If flagdopsi% > 0 Then
            '         Call resetoutput(0, d0.O_Vuoto_Tavola)
            '         FrmEuro.Toolbar1.Buttons(16).value = tbrUnpressed
            '         Delay (500)
            '         If d0.I_Sensore_Vuoto_Tavola = 1 Then
            '            MsgBox "ERRORE: non è stato possibile togliere il vuoto", vbExclamation
            '            pressesc% = 1
            '            FrmEuro.Toolbar1.Buttons(16).value = tbrPressed
            '         End If
            '      End If
           '
            Case "WAIT"
                'attende n millisecondi
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                TDelay& = Val(parmatrix$(1))
                If TDelay& > 60000 Then: coderr% = 1: ok% = 0: GoTo endpoint
                If flagdopsi% > 0 Then
                    dt& = timeGetTime&
                    Do
                        DoEvents
                    Loop While timeGetTime& - dt& < TDelay&
                End If
            
            Case "WINPOS", "WINSIZE"
                npar% = 2
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                For i% = 1 To 2
                    Select Case typematrix(i%)
                    Case 1, 3
                    Case Else
                        coderr% = 1: ok% = 0: GoTo endpoint
                    End Select
                Next i%
                If flagdopsi% > 0 Then
                    If tcmd$ = "WINPOS" Then
                        Frmprint.Left = Int(Val(parmatrix$(1)))
                        Frmprint.Top = Int(Val(parmatrix$(2)))
                    Else
                        ncharx& = Int(Val(parmatrix$(1))) + 1
                        nchary& = Int(Val(parmatrix$(2))) + 1
                        gapy& = Frmprint.Height - Frmprint.ScaleHeight
                        gapx& = Frmprint.Width - Frmprint.ScaleWidth
                        chh& = Frmprint.Picprint.TextHeight("A")
                        chw& = Frmprint.Picprint.TextWidth("A")
                        Frmprint.Height = gapy& + (nchary& * chh&)
                        Frmprint.Width = gapx& + (ncharx& * chw&)
                    End If
                End If
            
            Case "WINON"
                npar% = 0
                If flagdopsi% > 0 Then
                    Frmprint.Show
                End If
            
            Case "WINOFF"
                npar% = 0
                If flagdopsi% > 0 Then
                    Frmprint.Visible = False
                End If
            
            Case "WINLOC"
                'posizione stampa n caratteri x ncaratteri y
                npar% = 2
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                For i% = 1 To 2
                    Select Case typematrix(i%)
                    Case 1, 3
                    Case Else
                        coderr% = 1: ok% = 0: GoTo endpoint
                    End Select
                Next i%
                If flagdopsi% > 0 Then
                    chh& = Frmprint.Picprint.TextHeight("A")
                    chw& = Frmprint.Picprint.TextWidth("A")
                    printx& = ((Val(parmatrix$(1)) - 1) * chw&) + chw& / 2
                    printy& = ((Val(parmatrix$(2)) - 1) * chh&) + chh& / 2
                    If printx& = 0 Then printx& = chw& / 2
                    If printy& = 0 Then printx& = chh& / 2
                End If
            
            Case "WINTITLE"
                npar% = 1
                Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
                If flagexit% = 1 Then ok% = 0: GoTo endpoint
                Select Case typematrix(1)
                Case 2, 4
                Case Else
                    coderr% = 1: ok% = 0: GoTo endpoint
                End Select
                If flagdopsi% > 0 Then
                    Frmprint.Caption = parmatrix$(1)
                End If
               
'                Case "SEND_GPIB"    'permette di inviare un comando tramite la scheda
 '                                   'GPIB; 3 argomanti: il primo è lo stato della risposta
  '                                  'il secondo è l'indirizzo, il terzo è la stringa che si
   '                                 'che si desidera inviare
    '            Dim intStatus As Integer
     '           Dim strComando As String
      '          Dim strTempor As String
       '         Dim intTempor As Integer
        '
         '       npar% = 3
          '      Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
           '     If flagexit% = 1 Then ok% = 0: GoTo endpoint
            '    If flagdopsi% > 0 Then
             '   intTempor = InStr(1, parmatrix$(3), "$")
              '  If intTempor <> 0 Then
'                             strComando = Trim(parmatrix$(3))
 '                  intTempor = InStr(1, strComando, "$")
  '                 strTempor = Mid(strComando, 1, intTempor - 1) + "'"
   '                strComando = strTempor + Mid(strComando, intTempor + 1)
    '               intTempor = InStr(1, strComando, "$")
     '              strTempor = Mid(strComando, 1, intTempor - 1) + "'"
      '             strTempor = strTempor + Mid(strComando, intTempor + 1)
       '            Call send(Val(parmatrix$(2)), strTempor, intStatus)
        '           'Exit Sub
         '       Else
          '         Call send(Val(parmatrix$(2)), Trim(parmatrix$(3)), intStatus)
           '     End If
            '       thematrix$(indexmatrix!(1)) = intStatus
             '   End If
              '
'                Case "ENTER_GPIB"   'permette di ricevere un valore precedentemente
 '                                   'letto dallo strumento specificato;
  '                                  'primo argomento è il valore letto, il secondo è lo stato,
   '                                 'il terzo è l'indirizzo
    '            Dim strValueRead As String
     '           Dim intLength As Integer
      '          Dim intStatusEnter As Integer
       '         npar% = 3
        '        Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
         '       If flagexit% = 1 Then ok% = 0: GoTo endpoint
          '      If flagdopsi% > 0 Then
           '        strValueRead = Space$(255)
            '       Call enter(strValueRead, 255, intLength, CInt(Trim(parmatrix$(3))), intStatusEnter)
             '      thematrix$(indexmatrix!(1)) = Str(Val(strValueRead))
              '     thematrix$(indexmatrix!(2)) = Str(intStatusEnter)
               ' End If
                
'            Case "MOVEPROBE"
 '               npar% = 4
  '              Call extractpar(Trim$(Mid$(ccl$, bl% + 1)), npar%, flagexit%, parmatrix(), typematrix(), indexmatrix())
   '             If flagexit% = 1 Then ok% = 0: GoTo endpoint
    '            If typematrix%(1) <> 1 Then: ok% = 0: coderr% = 1: GoTo endpoint
     '           If typematrix%(2) <> 1 And typematrix%(2) <> 3 Then: ok% = 0: coderr% = 1: GoTo endpoint
      '          If typematrix%(3) <> 1 And typematrix%(3) <> 3 Then: ok% = 0: coderr% = 1: GoTo endpoint
       '         If typematrix%(4) <> 1 And typematrix%(4) <> 3 Then: ok% = 0: coderr% = 1: GoTo endpoint
        '        If flagdopsi% > 0 Then
         '           Call MoveProbe(Str(Val(parmatrix$(1))), Str(Val(parmatrix$(2))), Str(Val(parmatrix$(3))), Str(Val(parmatrix$(4))), 1)
          '      End If
           '
            Case Else
                ok% = 0
            End Select
            
        End If
    End Select
    If flagdopsi% > 0 And tcmd$ <> "MESSAGE" Then
        oldmsg$ = ""
        Call showstatus("")
    End If

endpoint:
    erstring$ = ""
    Select Case coderr%
    Case 0
    Case 1
        erstring$ = "Parameter type mismatch"
    Case 10
        erstring$ = "Division by zero"
    Case 20
        erstring$ = "Label " + lbl$ + " not found"
    Case 30
        erstring$ = "RETURN without GOSUB"
    Case Else
        erstring$ = "Generic Error"
    End Select
    If erstring$ <> "" Then MsgBox (erstring$)
    If (flagdopsi% > 0) And (erstring$ <> "") Then pcounter& = -2
    If ok% = 0 Then
        clr& = RGB(255, 0, 0)
        fm& = 1
        tto& = Len(cl$)
    Else
        clr& = RGB(0, 0, 255)
        fm& = 1 + gp%
        tto& = Len(tcmd$) + gp%
        Mid$(cl$, 1 + gp%, Len(tcmd$)) = UCase$(Mid$(cl$, 1 + gp%, Len(tcmd$)))
    End If
End Sub
Sub extractpar(myl$, npar%, flagexit%, ByRef parmatrix$(), ByRef typematrix%(), ByRef indexmatrix!())
'read paramets from line
'tipo: del parametro:
'1 variabile numerica
'2 variabile stringa
'3 costante numerica
'4 costante stringa
flagexit% = 0
Erase parmatrix$()
Erase typematrix%()
Erase indexmatrix!()
ReDim parmatrix$(1 To 1)
ReDim typematrix%(1 To 1)
ReDim indexmatrix!(1 To 1)
'parmatrix(1) = "Ciao"
'typematrix(1) = -1
'indexmatrix(1) = -1
'Exit Sub
'find paramets
cmyl$ = LTrim$(myl$ + " ")
Do
    bl% = InStr(cmyl$, " ")
    If bl% > 0 Then
        Item$ = Trim$(Left$(cmyl$, bl% - 1))
        If Left$(Item$, 1) = Chr$(34) Then
            fq% = InStr(cmyl$, Chr$(34))
            sq% = InStr(fq% + 1, cmyl$, Chr$(34) + " ")
            If sq% > 0 And fq% > 0 Then
                bl% = sq% + 1
                Item$ = Mid$(cmyl$, fq%, sq% - fq% + 1)
            Else
                Item$ = ""
            End If
        End If
        If Item$ <> "" Then
            paramerr% = 0
            Call anitem(Item$, itemtype%, itemindex%, paramerr%)
            If paramerr% = 1 Then
                flagexit% = 1
                MsgBox ("Parameter error")
                Exit Sub
            End If
            ic% = ic% + 1
            'add parameter to output
            ReDim Preserve parmatrix$(1 To ic%)
            ReDim Preserve typematrix%(1 To ic%)
            ReDim Preserve indexmatrix!(1 To ic%)
            parmatrix$(ic%) = Item$
            typematrix%(ic%) = itemtype%
            If itemtype% = 1 Or itemtype% = 2 Then
                indexmatrix!(ic%) = itemindex%
            Else
                indexmatrix!(ic%) = -1
            End If
        End If
    End If
    cmyl$ = LTrim$(Mid$(cmyl$, bl% + 1))
Loop While cmyl$ <> ""
'controllo numero parametri
'check paramets number
If npar% >= 0 Then
    Select Case ic%
    Case Is < npar%
        'added support from undefined number of paramets
        flagexit% = 1
        MsgBox ("Missing Parameters")
    Case Is > npar%
        flagexit% = 1
        MsgBox ("Too Many Parameters")
    End Select
Else
    'negative number is used as minimum number of parameters
    If ic% < -npar% Then
        flagexit% = 1
        MsgBox ("Missing Parameters")
    End If
End If
End Sub
Sub filesavedlg(fname$, myform As Form)
Dim ofn As OPENFILENAME
fname1$ = Trim$(fname1$) + Space$(255 - Len(Trim$(fname1$)))
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = myform.hwnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "Program Files (*.psi)" + Chr$(0) + "*.psi" + Chr$(0)
ofn.lpstrFile = fname1$
ofn.nMaxFile = 256
ofn.nMaxFileTitle = 256
ofn.lpstrFileTitle = Space$(255)
ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "Salva File"
ofn.Flags = 6
ofn.lpstrDefExt = "psi"
ret& = GetSaveFileName(ofn)
If ret& <> 0 Then
           fname$ = Trim$(ofn.lpstrFile)
Else
           fname$ = ""
End If
End Sub
Sub updateline(thel&)
Frminter.Lblline.Caption = Trim$(Str(thel&))
End Sub

Sub anitem(MyItem$, MyType%, MyIndex%, paramerr%)
'load parametr from set number/string or varible name
'tipo: del parametro:
'1 variabile numerica
'2 variabile stringa
'3 costante numerica
'4 costante stringa
    firstchar$ = UCase$(Left$(MyItem$, 1))
    Select Case firstchar$
    Case Chr$(34)
        'costante stringa
        MyItem$ = Mid$(MyItem$, 2)
        lastchar$ = Right$(MyItem$, 1)
        If lastchar$ = Chr$(34) Then
            MyItem$ = Left$(MyItem$, Len(MyItem$) - 1)
            MyType% = 4
        Else
            GoTo parerr
        End If
    Case "A" To "Z"
        'variabile numerica o stringa
        theindex$ = Mid$(MyItem$, 2)
        If theindex$ <> "" Then
            fc$ = Left$(theindex$, 1)
            lc$ = Right$(theindex$, 1)
            Select Case fc$
            Case "("
                If lc$ <> ")" Then GoTo parerr
                isindex% = 1
            Case "["
                If lc$ <> "]" Then GoTo parerr
                isindex% = 1
            Case "0" To "9"
                isindex% = 1
                theindex$ = "(" + theindex$ + ")"
            Case Else
                GoTo parerr
            End Select
            
        End If
        If isindex% = 1 Then
            theindex$ = Mid$(theindex$, 2)
            theindex$ = Left$(theindex$, Len(theindex$) - 1)
            For i% = 1 To Len(theindex$)
                thech$ = Mid$(theindex$, i%, 1)
                Select Case thech$
                Case "0" To "9"
                Case Else
                    GoTo parerr
                End Select
            Next i%
        End If
        If Val(theindex$) > 999 Then GoTo parerr
        MyIndex% = Val(theindex$) + (1000 * (Asc(firstchar$) - 65))
        MyItem$ = thematrix$(MyIndex%)
        MyType% = 1
        If firstchar$ = "S" Then MyType% = 2
    Case "+", "-", ".", "0" To "9"
        'costante numerica
        foundp% = 0
        If firstchar$ = "." Then foundp% = 1
        theindex$ = Mid$(MyItem$, 2)
        If theindex$ <> "" Then
            For i% = 1 To Len(theindex$)
                thech$ = Mid$(theindex$, i%, 1)
                Select Case thech$
                Case "."
                    If foundp% = 1 Then GoTo parerr
                    foundp% = 1
                Case "0" To "9"
                Case Else
                    GoTo parerr
                End Select
            Next i%
        End If
        MyItem$ = Trim$(Str(Val(MyItem$)))
        MyType% = 3
    End Select
Exit Sub
parerr:
paramerr% = 1
Exit Sub
End Sub
