Machsupport Forum
Support => Downloads => MACH TOOL BOX => Topic started 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
-
Might want to add an alternative or "M6" search also.
You sure are working hard on your Florida winter retreat.
Have fun.
-
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
-
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
-
Sorry I thought you lived in the northeast
Now that is unless you have messages with a 'T' in the comment. ;)
-
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
-
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"
-
(;-) 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
-
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.
-
'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
-
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:"¤tLine
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
-
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
-
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
-
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
-
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.
-
optimized and cleaned up a tad.
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
-
There goes Ian skinning that cat again.
Cool, I like :')
-
Something does not seem right with it though
I tried a second m6 and never finds it
-
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)
-
here you go - is fixted ;D
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
-
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
-
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.
-
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
-
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
-
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