Machsupport Forum

Support => Downloads => MACH TOOL BOX => Topic started by: BR549 on March 08, 2013, 12:33:50 PM

Title: Button Script to search through active Gcode File for NEXT Tool change
Post by: BR549 on March 08, 2013, 12:33:50 PM
Hi ALL, This is a button script to allow you to search through a loaded Gcode file. It advances to the next M6 it finds in the current file. If no file is loaded then it errors OUT and ends.

(;-)TP

'Macro for  GcodeFile string search
Dim sLine As String
Dim i As Long
Dim lFoundLine As Long

Fname = FileName

If Fname = "No FIle Loaded." Then
MsgBox" There is NO File Currently Loaded in Mach3"
End
Else

Open FIlename For Input As #1

Do Until EOF(1)
  lFoundLine = lFoundLine + 1
  Line Input #1, sLine
  If InStr(1, sLine, "M06") > 0 And lfoundline > GetDro(16) Then
    Exit Do
  End If
Loop
Close #1
Nline = (lfoundline -1)
MsgBox""&Nline
Setdro(16,Nline)
End If
End
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No on March 10, 2013, 09:02:32 AM
Might want to add an alternative or "M6" search also.

You sure are working hard on your Florida winter retreat.
Have fun.
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Dan13 on March 10, 2013, 12:41:12 PM
Hey Terry,

Thanks for the code!

Doing for Turn, what if we OR gated the following in the If statement: "T0" OR "T1" OR "T2". True, not an elegant way, but it will do what's needed. And I don't have tools on the lathe past 29. If I had I could just add another one to the OR: "T3" and so on...

Dan
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: BR549 on March 10, 2013, 01:29:26 PM
Good Idea about teh M06 Most use M6 just change out the text to M6

For turn just change out the M6 to the "T" , or  "T?? yes it could pickup the letters in a (message) but just push the button again to advance to the next T??.

Not really a retreat only lived here for 59 years so far(;-)

(;-) TP
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No on March 10, 2013, 08:23:14 PM
Sorry I thought you lived in the northeast

Now that is unless you have messages with a 'T' in the comment.   ;)

Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: BR549 on March 10, 2013, 08:46:36 PM
I am working on fixing the "T" deal but I don't have a clue yet as HOW to ignore strings inside the ( ) . Some things are obvious that one is not(;-).

At least to my little pea brain, (;-) TP
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No on March 10, 2013, 09:12:04 PM
Use a flag that when you see a "(" you set the flag to 1 keep searching until you see a")" reset the flag to 0. Than your back to looking for a "T"
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: BR549 on March 10, 2013, 09:36:21 PM
(;-) IF you can fix it please DO.  It may take me DAYS to figure out HOW to make it work as I am not a programmer but a code assembler(;-).

There is also another error in the code I just saw.

 If InStr(1, sLine, "M06") > 0 And lfoundline > GetDro(16) Then

Should read as

 If InStr(1, sLine, "M06") > 0 And lfoundline > (GetDro(16)+1) Then

Other wise it will not search beyond the same line number. It just repeats the same line number.

(;-) TP
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No on March 10, 2013, 09:50:12 PM
See... you can do it, you have been a machine at pumping out useful code. Just need to push your talents a little, always been fun for me to learn new things. I think your like that too.
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No on March 10, 2013, 10:13:04 PM
'gives you the idea.


Do Until EOF(1)
  lFoundLine = lFoundLine + 1
  Line Input #1, sLine
  If InStr(1, sLine, "(") > 0 And lfoundline > GetDro(16)+1 Then
    flag=1
  End If
  If flag=0 Then
    If InStr(1, sLine, "T") > 0 And lfoundline > GetDro(16)+1 Then
      Exit Do
     End If
  End If
  flag=0
Loop


Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No 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
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: BR549 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

Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: BR549 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
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Dan13 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
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No 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.
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: stirling 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
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No on March 11, 2013, 11:25:36 AM
There goes Ian skinning that cat again.

Cool, I like :')
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No 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
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: stirling 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)
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: stirling 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
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: BR549 on March 11, 2013, 04:25:52 PM
He said WHAT??? I will take me a while to work this one out(;-).


Now the next idea I had was to be able to RUN the old DXFImporter that was in mach1/2 for use with plasma . It was simple and to the point. IF it could be ran it would help simply the plasma process.

Just a thought, (;-) TP

Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Ya-Nvr-No on March 11, 2013, 07:25:34 PM
Ian "that's the ticket" :')

I just cleaned this section, for my analness (is that really a word, lol)

  MsgBox "File has been set to line #: " & thisLine-1 & Chr(13) & Chr(10) & _
         "which is your next toolchange position." & Chr(13) & Chr(10) & _
         "Your previous line # was: " & currentLine

Your the "VB Man"
Thanks again.

Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: stirling on March 12, 2013, 05:34:14 AM
I just cleaned this section, for my analness (is that really a word, lol)
;D When an engineer polishes out the tool marks on a milled engine block they're congratulated for their attention to detail.
When a luthier (Brett click here (http://lmgtfy.com/?q=luthier)) brings a guitar to a highly polished finish they're congratulated for their craftmanship.
When a coder (spit!) finesses their work they're anal. "WTF - it works - well kinda anyway - who cares?"

Ah well - 'tis our lot. ;D
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: Chaoticone on March 12, 2013, 10:33:11 AM
You got me Ian...................  ;D

I just had to clcik it........  I did so with a stretched out arm and a jump back though.................... just in case. 

Brett
Title: Re: Button Script to search through active Gcode File for NEXT Tool change
Post by: BR549 on March 12, 2013, 09:04:50 PM
GUYS forget the idea for a MACH3 DXF importer (;-) HECK we have SheetCam what more could I want. I don't even know how that got put on my list of things to do. I think my daughter (a crackerjack metal artist and plasma cutter) has been adding to my list AGAIN.

Programmers  what are you to do with them??? ,

Thanks Guys, (;-) TP