AutoHotkey Community

It is currently May 27th, 2012, 9:03 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: October 17th, 2011, 5:03 pm 
Offline

Joined: August 25th, 2011, 10:56 pm
Posts: 10
Location: texas
I'm pretty new to prgramming, but ahk has been a great help with some repetitive tasks.

I'm trying to figure out how to extract some numbers out of a cnc program text file.
the idea is to copy some code to the clipboard and then read a line at a time, only writing
the numbers after the x and y coordinates to another text file. It will also need to ignore
some lines that begin with ( for comments, etc.

the cnc code looks like:
Code:
(comments)
N01 x1.0 Y1.0 T01
N02 x2.0
N03 y2.0
etc.

m30 (end of program)


Desired output:

1.0,1.0
2.0,1.0
2.0,2.0


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2011, 5:22 pm 
Offline

Joined: March 5th, 2011, 3:43 pm
Posts: 20
Maybe try not to copy file to clipboard but use LoopReadFile.

Then use RegExMatch on each line (may took a while depending on txt file size) to extract coordinates. Check if both coordinates exist in line, if not use one from previous line.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2011, 7:01 pm 
Offline

Joined: August 25th, 2011, 10:56 pm
Posts: 10
Location: texas
loop read file is probably a good idea,

I looked at regexmatch but i'm not sure how to go about it.

not too worried about file size, they are not too big (old machine=not much ram )


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2011, 7:26 pm 
I don't know anything about CNC files, but by the looks of it:

You only define a change of X or Y otherwise keep the same X or Y so it has to remember the coordinate between lines?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2011, 7:55 pm 
Offline

Joined: December 18th, 2009, 6:08 pm
Posts: 63
maybe one of the pros will chime in here, but heres my attempt.

Code:
filename = test.txt
outfile = out.txt
loop, read, %filename%
   {
      if RegExMatch(A_LoopReadline,"N0\d+")
         {
            if RegExMatch(A_LoopReadLine, "i).*?x(\d+.\d)",x)
                  x := x1, px := x
            else
                  x := px
            if RegExMatch(A_LoopReadLine, "i).*?y(\d+.\d)",y)
                  y := y1, py := y
            else
                  y := py
            line = x%x%,y%y%`r`n
            FileAppend, %line%,%outfile%
         }
   }
ExitApp


double check, i am not responsible for crashing machine :?


Last edited by ton80 on October 17th, 2011, 8:12 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2011, 7:56 pm 
Offline

Joined: August 25th, 2011, 10:56 pm
Posts: 10
Location: texas
yes, that is how the machine works, it stays where it's at until you tell it otherwise.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2011, 8:12 pm 
Offline

Joined: March 5th, 2011, 3:43 pm
Posts: 20
Generally above code should work but... it depends on what kind of NC files you want to analyze.
Do you just want to get all coordinates from any NC file? If yes then be careful with this code because if in NC file are also functions like G2, G3, G54 and so on then X and Y won't be 'normal coordinates'. It depends on files you have.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2011, 8:49 pm 
Offline

Joined: December 18th, 2009, 6:08 pm
Posts: 63
sqblak wrote:
Generally above code should work but... it depends on what kind of NC files you want to analyze.
Do you just want to get all coordinates from any NC file? If yes then be careful with this code because if in NC file are also functions like G2, G3, G54 and so on then X and Y won't be 'normal coordinates'. It depends on files you have.



you may want to read the ops post/request.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2011, 9:56 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
sqblak has a point. It's unusual for a line to specify N (A line number) without a G at some point to define the function (Typically G1 or G01)

The posted code should work whether G codes are used are not, so all should be well. in the future, it might be smart to filter lines with codes that are not G1, G2, or G3 (or G01, G02, G03)

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 18th, 2011, 1:05 pm 
Offline

Joined: August 25th, 2011, 10:56 pm
Posts: 10
Location: texas
thanks ton80 i'll give this a try and see how it works.

good observations about the G codes, the files i'm analyzing are for a punch press so not nearly as many codes as a mill or something.

at this point I am trying to take some old NC programs and use the coordinates to send to a drawing program so I can update/modify them, so the G codes are not really important, i.e the post processor puts them back in where they should go after i'm done.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 18th, 2011, 2:09 pm 
Offline

Joined: August 25th, 2011, 10:56 pm
Posts: 10
Location: texas
It works! I had to put just N instead of N0 to get it to go past line 9 of the NC program.

I also added a plus sign to get more than one decimal place in both X and Y.

Here is the code with the changes I made:
Code:



filename = test.txt
outfile = out.txt
loop, read, %filename%
   {
      if RegExMatch(A_LoopReadline,"N\d+")
         {
            if RegExMatch(A_LoopReadLine, "i).*?x(\d+.\d+)",x)
                  x := x1, px := x
           

       else
                  x := px
            if RegExMatch(A_LoopReadLine, "i).*?y(\d+.\d+)",y)
                  y := y1, py := y
            else
                  y := py
            line = %x%,%y%`r`n
            FileAppend, %line%,%outfile%
         }
   }
ExitApp

How would I make it skip any line with a G in it?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 18th, 2011, 2:17 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
Code:
filename = test.txt
outfile = out.txt
loop, read, %filename%
   {
      if Instr(A_LoopReadline, "G")
         Continue

      if RegExMatch(A_LoopReadline,"N\d+")
         {
            if RegExMatch(A_LoopReadLine, "i).*?x(\d+.\d+)",x)
                  x := x1, px := x
           

       else
                  x := px
            if RegExMatch(A_LoopReadLine, "i).*?y(\d+.\d+)",y)
                  y := y1, py := y
            else
                  y := py
            line = %x%,%y%`r`n
            FileAppend, %line%,%outfile%
         }
   }
ExitApp

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 21st, 2011, 5:31 pm 
Offline

Joined: August 25th, 2011, 10:56 pm
Posts: 10
Location: texas
thanks, that works great! starting to understand the regex subpatterns too!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, hyper_, iBob35555VR, tomoe_uehara and 69 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group