Hello Guest it is April 19, 2024, 11:44:24 PM

Author Topic: Button Script to search through active Gcode File for NEXT Tool change  (Read 19208 times)

0 Members and 1 Guest are viewing this topic.

Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #10 on: March 10, 2013, 10:25:56 PM »
I've used this one for years attached to a button.

Dim currentLine As String
Dim currentfile As String
Dim filesys, filetxt, lastline, oldline As String

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set filesys = CreateObject("Scripting.FileSystemObject")
currentLine = GetOEMDRO(816)
Found = False
currentfile = FileName
Set filetxt = filesys.OpenTextFile(currentfile, ForReading, True)

Dim i As Integer
i = 0

Do While filetxt.AtEndOfStream <> true
  linestring = UCase(filetxt.ReadLine()) & " "
  If linestring <> " " Then
    i = i+1
    If i > currentLine Then
      If ((InStr(linestring,"M6") >0 Or InStr(linestring,"M06")>0)) Then
        SetOEMDRO(816,i)
        Found = True
        MsgBox "File has been set to line:"& GetOEMDRO(816)&" Your next toolchange position.Your previous line was:"&currentLine
        Exit Do
      End If
    End If
   End If
Loop

If Found = False Then
  MsgBox "No M6/M06 Found after LINE:"& currentLine
End If

filetxt.Close
Set filesys = Nothing
Set filetxt = Nothing

Offline BR549

*
  •  6,965 6,965
    • View Profile
Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #11 on: March 10, 2013, 11:23:45 PM »
OK not sure how your example works YET ,and I will figure it out how  it works,  but it does.

Thank You

Your button code seems a bit more eloquent(;-) AND I spied the way you did the M6 or M06  for the string search(;-).

Sorry but I have a defective Programmers GENE.

(;-) TP

Offline BR549

*
  •  6,965 6,965
    • View Profile
Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #12 on: March 11, 2013, 12:38:05 AM »
OK  a TOTALLY unrelated question if you will. IS it possible to change the FONT size in a ListBox.  In the Load from Image function I have a list box BUT the font is fairly small, not good for a touchscreen.

Any ideas, (;-) TP

Offline Dan13

*
  •  1,208 1,208
    • View Profile
    • DY Engineering
Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #13 on: March 11, 2013, 03:33:04 AM »
Yes, Craig kindly sent me this code a couple of days ago and it works well and I even like the way it sets next line and reports the new with the previous one. But like you, Terry, I haven't yet figured how exactly it works.

That idea of looking for a "(" is nice, but sometimes I am lazy when adding comments manually so use the ";" character instead. Should I be able to look for it as well and then for a CR to reset the flag?

Dan
Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #14 on: March 11, 2013, 08:10:50 AM »
Dan no reason you should not be able to search for ";"
as soon as it sees a ";" it just skips the whole line and goes to the next line.

I've added the comments feature to my post processors, so it spits out the (added information).

And the VB button code I use I'd found posted several years ago, so I can not take the credit.
Think it was the first time I learned to attach code to a button.

Terry, I'll look into the font control, it would be nice.

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #15 on: March 11, 2013, 09:45:25 AM »
optimized and cleaned up a tad.

Code: [Select]
Option Explicit

'define the strings we're looking for - add/change as you see fit
Dim needles(2) As String
needles(0)="M6"
needles(1)="M06"
needles(2)="M 6"

Dim currentLine As Integer
Dim thisLine As Integer
Dim fileSys As Object
Dim fileTxt As Object
Dim haystack As String
Dim needle As String
Dim matchFound As Boolean
Const forReading = 1

currentLine = GetOEMDRO(816) 'note where Mach currently is in the gcode file
matchFound = False

'open the file in cb
Set fileSys = CreateObject("scripting.fileSystemObject")
Set fileTxt = fileSys.openTextFile(fileName, forReading, true)

'no point in reading all the lines we're already past so skip 'em
For thisLine = 0 To currentLine - 1
  fileTxt.skipLine
Next
thisLine = thisLine - 1

'plough through the rest of the file looking for our needles in the haystack
'we'll either get to the end or we'll find our match
While Not fileTxt.atEndOfStream And Not matchFound
  haystack = UCase(fileTxt.readLine()) 'get the whole line
  If haystack <> "" Then 'check it's not empty if it is just skip it
    thisLine = thisLine + 1
    For Each needle In needles 'look through the line for our required strings
      If InStr(haystack, needle) Then
        SetOEMDRO 816,thisLine
        matchFound = true
        Exit For 'no point in checking the rest of the line
      End If
    Next
  End If
Wend

If matchFound Then
  MsgBox "File has been set to line:" & thisLine & _
         "your Next toolchange position." & Chr(13) & Chr(10) & _
         "Your previous line was:" & currentLine
Else
  Dim msg As String
 
  For Each needle In needles
    msg=msg & needle & " "
  Next
  MsgBox "No " & msg & " found after line:" & currentLine
End If

fileTxt.Close
Set fileSys = Nothing
Set fileTxt = Nothing


Ian
Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #16 on: March 11, 2013, 11:25:36 AM »
There goes Ian skinning that cat again.

Cool, I like :')
Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #17 on: March 11, 2013, 11:46:05 AM »
Something does not seem right with it though

I tried a second m6 and never finds it
« Last Edit: March 11, 2013, 12:00:56 PM by Ya-Nvr-No »

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #18 on: March 11, 2013, 12:14:01 PM »
Indeed you are correct sir - BUT - the only reason your code finds the next ones is because it doesn't actually do what it says on the tin - it actually stops on the line AFTER the toolchange. If you want mine to do that then I can soon unfix it  ;D
(or you could just scroll down to the line after the toolchange it's found in the gcode display and then hit the button)

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: Button Script to search through active Gcode File for NEXT Tool change
« Reply #19 on: March 11, 2013, 02:08:06 PM »
here you go - is fixted  ;D

Code: [Select]
Option Explicit

'define the strings we're looking for - add/change as you see fit
Dim needles(2) As String
needles(0)="M6"
needles(1)="M06"
needles(2)="M 6"

Dim currentLine As Integer
Dim thisLine As Integer
Dim fileSys As Object
Dim fileTxt As Object
Dim haystack As String
Dim needle As String
Dim matchFound As Boolean
Const forReading = 1

currentLine = GetOEMDRO(816) 'note where Mach currently is in the gcode file
matchFound = False

'open the file in cb
Set fileSys = CreateObject("scripting.fileSystemObject")
Set fileTxt = fileSys.openTextFile(fileName, forReading, true)

'no point in reading all the lines we're already past so skip 'em
For thisLine = 0 To currentLine
  fileTxt.skipLine
Next

'plough through the rest of the file looking for our needles in the haystack
'we'll either get to the end or we'll find our match
While Not fileTxt.atEndOfStream And Not matchFound
  haystack = UCase(fileTxt.readLine()) 'get the whole line
  If haystack <> "" Then 'check it's not empty if it is just skip it
    For Each needle In needles 'look through the line for our required strings
      If InStr(haystack, needle) Then
        SetOEMDRO 816,thisLine
        matchFound = true
        Exit For 'no point in checking the rest of the line
      End If
    Next
    thisLine = thisLine + 1
  End If
Wend

If matchFound Then
  MsgBox "File has been set to line:" & thisLine-1 & _
         "your Next toolchange position." & Chr(13) & Chr(10) & _
         "Your previous line was:" & currentLine
Else
  Dim msg As String
 
  For Each needle In needles
    msg=msg & needle & " "
  Next
  MsgBox "No " & msg & " found after line:" & currentLine
End If

fileTxt.Close
Set fileSys = Nothing
Set fileTxt = Nothing

Ian