ScriptUO

Casa de TrailMyx => Programming / Operating Systems => Topic started by: Khameleon on December 31, 2010, 06:10:46 AM

Title: text file scanner
Post by: Khameleon on December 31, 2010, 06:10:46 AM
I'm not really sure how to go about this.. so I'll just tell you guys what I'm looking for and maybe you guys can point me in the right direction.

a little background history... I work on a CNC machine which is coded by a McMaster Cam design program the file format is .nc which can be read in text file format.  what I'm looking for is a program that reads the whole text file and reports each tool # and the Farthest Z Depth. I'm looking for a text file that I can attach to show you what the file looks like...
Title: Re: text file scanner
Post by: Masscre on January 01, 2011, 11:09:06 AM
There are a ton of file parsing tools out there and reporting tools most are free. Get me a copy of one of th efiles and I can prolly get you a link to one or even help you right one quickly in c or pascal.
Title: Re: text file scanner
Post by: Khameleon on January 01, 2011, 05:34:04 PM
thanks a lot.. I'll have to wait till Monday before I can snag a file from work... I've been reading the program we use is way out dated but for small business's it does what we need.  I'm sure its gotta be sometime simple for you guys that write programs all the time...
from what I can remember is you will see the start of a tool would be something like
T1 [Tool 1]
a few lines of code then you will start to see the z values somethine like
Z-1.125
ect ect ect.

sometimes the z will have multiple depths for each tool I would like to find the deepest depth for each Tool Number.
Title: Re: text file scanner
Post by: Khameleon on January 05, 2011, 03:16:45 PM
here's the type of code that i'm looking to scan...
%
O0000
(PROGRAM NAME - 13430 PUNCH GEO)
(DATE=DD-MM-YY - 05-01-11 TIME=HH:MM - 18:04)
N100G20
N102G0G17G40G49G80G90
( 1/2 CENTERDRILL TOOL - 1 DIA. OFF. - 1 LEN. - 1 DIA. - .5)  Beginning of First tool
N104T1M6
N106G0G90G54X-2.5289Y11.8305A0.S2500M3
N108G43H1Z.1M8
N110G99G81Z-.26R.1F40.   Right here is the first Z depth -.26
N112X2.3831
N114X-12.3794Y11.837
N116X-7.4582
N118X7.3056
N120X12.3523Y11.8412
N122Y15.8335
N124X-15.6538Y15.837
N126X-7.1548
N128G80
N130M5
N132G91G28Z0.M9
N134G28X0.Y0.A0.
N136M01 M01 is the End of the first tool
( 1/2 FLAT ENDMILL TOOL - 2 DIA. OFF. - 2 LEN. - 2 DIA. - .5) Switching to 2nd Tool
N138T2M6
N140G0G90G54X-2.1541Y18.587A0.S3056M3
N142G43H2Z-.25  Here we have the z depth again at -.25
N144G1Y18.087F24.45
N146G3X-1.6541Y17.587R.5
N148G1X12.8459
N150G2X13.0959Y17.337R.25
N152G1Y10.587
N154G2X12.8459Y10.337R.25
N156G1X-16.1541
N158G2X-16.4041Y10.587R.25
N160G1Y17.337
N162G2X-16.1541Y17.587R.25
N164G1X-1.6541
N166G3X-1.1541Y18.087R.5
N168G1Y18.587
N170Z.35F6.42
N172M5
N174G91G0G28Z0.
N176G28X0.Y0.A0.
N178M01  M01 again is the end of the tool
( 1/2 FLAT ENDMILL TOOL - 3 DIA. OFF. - 3 LEN. - 3 DIA. - .5)  Starting Tool 3
N180T3M6
N182G0G90G54X-2.5289Y11.8305A0.S1833M3
N184G43H3Z.1M8
N186G99G83Z-.25R.1Q.1F14.66 here we have a depth of -.25
N188X2.3831
N190X-12.3794Y11.837
N192X-7.4582
N194X7.3056
N196X12.3523Y11.8412
N198Y15.8335
N200X-15.6538Y15.837
N202X-7.1548
N204G80
N206M5
N208G91G28Z0.M9
N210G28X0.Y0.A0.
N212M02 and now we are at the end of the program noted by M02
%

Sometimes tools are pulled up multiple times at different z depths I don't know if there is a way to scan for T1M6 (T1 = Tool Change to Tool 1) scan the whole section for Z-(what ever this is) until it hits M01 then scan for the next tool T2M6 all the way till the next M01 and so on... then create some sort of Tool List with the farthest Z depth? 


if I confused you please ask me to help me to help you help me :P
Title: Re: text file scanner
Post by: Endless Night on April 08, 2011, 07:39:42 AM
you get this all sorted Kham ?
Title: Re: text file scanner
Post by: Khameleon on April 08, 2011, 04:02:04 PM
no :(

I would like to we use this code at work, and my moron programer doesn't ever write down tool depths and I have to look through the whole program so I don't smash a tool into my vises...
Title: Re: text file scanner
Post by: Scrripty on April 08, 2011, 04:06:34 PM
no :(

I would like to we use this code at work, and my moron programer doesn't ever write down tool depths and I have to look through the whole program so I don't smash a tool into my vises...

Sounds like your tool programmer needs to be readjusted... :)
Title: Re: text file scanner
Post by: UOMaddog on April 09, 2011, 08:06:54 PM
Yea, set the x/y coordinates for a stick equal to the x/y coordinate of his @$$, then set the z axis coordinate to -10....

Anyways...you might be able to use Notepad++ to find each of those lines (I believe it has some decent macro functionality). I could also whip up a java program that would look for specific strings, but we'd need to clarify a few things (will a line with a Z in it always be a z-coordinate? does every new tool start with an open parentheses and is that the only thing that does?). It's not too hard for me to whip some things like that up, so let me know!
Title: Re: text file scanner
Post by: Khameleon on April 10, 2011, 04:15:00 AM
the Z will always be a Negative we never change the Z Starting point.. the only other time z won't be a negative is it will be 0. I can get a basic program file so you can see in multiple tool changes so you can see what it looks like in the program. but the example I put up  a above is a basic program much doesn't change.
Title: Re: text file scanner
Post by: UOMaddog on April 10, 2011, 08:38:07 AM
if you can post up a legit one, I'll whip up a regex-based Java program that should do exactly what you want!
Title: Re: text file scanner
Post by: Khameleon on April 10, 2011, 09:21:55 AM
I'll get a file on monday and post it up.
Title: Re: text file scanner
Post by: Khameleon on April 13, 2011, 12:11:35 PM
Here's that File you Requested.. please let me know if there is any other information you need...
you can open the file in Notepad to read the file.
Title: Re: text file scanner
Post by: UOMaddog on April 13, 2011, 03:04:21 PM
Does your computer that you will be running this program on have Java installed on it? Because that's more than likely what I'll use to write it, and I want you to be able to run it! If not, let me know how you would ideally like it to work!
Title: Re: text file scanner
Post by: Khameleon on April 13, 2011, 04:34:21 PM
honestly I doubt it.. its a real basic computer with windows xp installed. the computer is dedicated to send the programs out to the CNC machines.. no internet either ;(
Title: Re: text file scanner
Post by: UOMaddog on April 13, 2011, 04:44:45 PM
Hmm, you really know how to shoot down people's hopes and dreams!  ;)

(I was honestly thinking of setting up a website where you could load the .NC files and have it parse them since you probably didn't have Java installed, but since you don't have internet access, that makes it a little trickier!)

I'll see what I can think up!
Title: Re: text file scanner
Post by: UOMaddog on April 13, 2011, 04:47:55 PM
I'm scared to ask, but does it have the .NET framework installed on it?
Title: Re: text file scanner
Post by: Khameleon on April 13, 2011, 05:28:57 PM
honestly couldn't tell you the truth.  I highly doubt it. ;(
Title: Re: text file scanner
Post by: Endless Night on April 13, 2011, 05:57:35 PM
could always write it in lua... :)  and install a version of openEUO on his work machine.  lol

I joke but it would work perfect.

Title: Re: text file scanner
Post by: Khameleon on April 13, 2011, 05:59:56 PM
Does lua work without UO installed :P
Title: Re: text file scanner
Post by: TrailMyx on April 14, 2011, 07:14:08 AM
It sure does.  Lua is a general purpose scripting language used in other places in the world.  I actually have used it a few times stand-alone to parse data for my job.
Title: Re: text file scanner
Post by: Endless Night on April 14, 2011, 08:40:11 AM
It would be/should be pretty easy to knock out a file parser in lua.

If i understood what the hell you where trying to do .. Id help.  Heres some code to get you going

Code: [Select]
function LoadData(fn)    -- by Cheffe
  local f,e = openfile(fn,"r")          --r means read-only (default)
  if f then                             --anything other than nil/false evaluates to true
    local s = f:read("*a")              --*a means read all
    f:close()
    return s
  else                                  --if openfile fails it returns nil plus an error message
    error(e,2)                          --stack level 2, error should be reported
  end                                   --from where LoadData() was called!
end

function EN_StringToStringTable(s)   -- imperfect code but suffices
  local t = {}
  local NewLineAt = 0
  local line = ''  
  local EOL = string.char(10)
  s = s..EOL..'**THEEND**'
  --print(string.byte(string.sub(s,22,22)))
  repeat
    NewLineAt = string.find(s, EOL)
    line = string.sub(s, 1 , NewLineAt - 1)    
    table.insert(t, { Line=line })
    s = string.sub(s, NewLineAt + 1, string.len(s))
    --print(line)
  until s == '**THEEND**'
  return t
end

------
local sFile = LoadData('filename')  -- one big string of the files contents.
local sStringTable = EN_StringToStringTable(sFile)  --

for k=1,table.maxn(sStringTable) do
   print(sStringTable[k].Line)

Of course i didnt test any of the above... but should open file load as a string, convert to a table/array of strings/lines. Print each line.

Title: Re: text file scanner
Post by: UOMaddog on April 14, 2011, 09:39:12 AM
Well while EN's on a roll, here's what I can tell from these files EN:

-Each time you have a line that contains a "(" and ")" it means a new tool, so print it out
-Each time you have a line that contains a "Z" it means a Z-coordinate (haven't seen any other Zs in other lines and oh well if you have a few extra lines), so print it out

I believe that's all he's looking for, but I'll read over his initial post again
Title: Re: text file scanner
Post by: Khameleon on April 14, 2011, 11:33:28 AM
ya that's the Jist of it.. I'd like to Output something like this

Code: [Select]
Tool 1: Z 0
Tool 2: Z -4.5
Tool 3: z -.5
Ect.

in the case there are Multiple Z depths print the Farthest Z Depth.


So Far when I run the Script In LUA I get an error before I can do anything.
!C:\OpenEUO\scripts\cnc z depth.lua:34: 'end' expected (to close 'for' at line 33) near '<eof>'
Title: Re: text file scanner
Post by: Endless Night on April 14, 2011, 12:45:41 PM
Quote from: Khameleon link=topic=6725.msg63864#msg63864
in the case there are Multiple Z depths print the Farthest Z Depth.
[/quote

As in the LOWEST NUMBER ??
Title: Re: text file scanner
Post by: Khameleon on April 14, 2011, 12:47:54 PM
right. z 0 would be the the TOP of a part, as Z -2.000 means the Tool, usually a Drill will drill a hole -2.000 inches so to answer your question I'm looking for the Deepest Z per tool.
Title: Re: text file scanner
Post by: Endless Night on April 14, 2011, 01:10:40 PM
Right before i go any further... the code below makes the below output .. please examine the output and tell me if its

(a) Picking up the tools correctly
(b) Picking up all the Zs for a certain tool

Note: Right now it just prints all the z lines and Tool lines it doesnt parse out the values yet.

Code: [Select]
function LoadData(fn)    -- by Cheffe
  local f,e = openfile(fn,"r")          --r means read-only (default)
  if f then                             --anything other than nil/false evaluates to true
    local s = f:read("*a")              --*a means read all
    f:close()
    return s
  else                                  --if openfile fails it returns nil plus an error message
    error(e,2)                          --stack level 2, error should be reported
  end                                   --from where LoadData() was called!
end

function EN_StringToStringTable(s)   -- imperfect code but suffices
  local t = {}
  local NewLineAt = 0
  local line = ''  
  local EOL = string.char(10)
  s = s..EOL..'**THEEND**'
  --print(string.byte(string.sub(s,22,22)))
  repeat
    NewLineAt = string.find(s, EOL)
    line = string.sub(s, 1 , NewLineAt - 1)    
    table.insert(t, line )
    s = string.sub(s, NewLineAt + 1, string.len(s))
    --print(line)
  until s == '**THEEND**'
  return t
end

function DoIt(fn)
  local sFile = LoadData(fn)  -- one big string of the files contents.
  local sT = EN_StringToStringTable(sFile)  

  local tools = 0
  local z = {}
  for k=1,table.maxn(sT) do
     s = sT[k]
--     print(s)

     if string.match(s,'TOOL T')  then      
         tools = tools + 1
         print('TOOL '..tools..':= '..s)
         end
     if string.match(s,'Z') then
        print('         z:= '..s)
        z[tools] = tools
        end            
     end
    
--  for k=1,tools do
--     print('Tool '..k..': Z '..z[k])  
--     end
    
--  for k=1,table.maxn(sT) do print(sT[k]) end  
end

---------------
DoIt('cnc.nc')

Output from running against your file.
Code: [Select]
TOOL 1:= N13T1M06  *(---- TOOL T1  OFFSET H1---)
         z:= N16G43H1Z.1M07
         z:= N18G1Z0.F20.
         z:= N46G0Z.1
         z:= N48G0H0Z0.M09
TOOL 2:= N49T2M06  *---- (TOOL T2  OFFSET H2---)
         z:= N53G43H2Z.1M07
         z:= N55G1Z-.05F20.
         z:= N62G0Z.1
         z:= N65G1Z-.1F20.
         z:= N72G0Z.1
         z:= N75G1Z-.15F20.
         z:= N82G0Z.1
         z:= N85G1Z-.2F20.
         z:= N92G0Z.1
         z:= N95G1Z-.25F20.
         z:= N102G0Z.1
         z:= N105G1Z-.3F20.
         z:= N112G0Z.1
         z:= N115G1Z-.35F20.
         z:= N122G0Z.1
         z:= N125G1Z-.4F20.
         z:= N132G0Z.1
         z:= N135G1Z-.05F20.
         z:= N141G0Z.1
         z:= N144G1Z-.1F20.
         z:= N150G0Z.1
         z:= N153G1Z-.15F20.
         z:= N159G0Z.1
         z:= N162G1Z-.2F20.
         z:= N168G0Z.1
         z:= N171G1Z-.25F20.
         z:= N177G0Z.1
         z:= N180G1Z-.3F20.
         z:= N186G0Z.1
         z:= N189G1Z-.35F20.
         z:= N195G0Z.1
         z:= N198G1Z-.4F20.
         z:= N204G0Z.1
         z:= N206G0H0Z0.M09
TOOL 3:= N207T3M06  *---- (TOOL T3  OFFSET H3---)
         z:= N211G43H3Z.1M07
         z:= N213G1Z-.4F20.
         z:= N220G0Z.1
         z:= N223G1Z-.4F20.
         z:= N230G0Z.1
         z:= N233G1Z-.4F20.
         z:= N240G0Z.1
         z:= N242G0H0Z0.M09
TOOL 4:= N243T4M06  *---- (TOOL T4  OFFSET H4---)
         z:= N247G43H4Z.1M07
         z:= N249G99G81X-1.625Y-1.75Z-.23R0.1F6.
         z:= N252G0G80Z.1
         z:= N254G0H0Z0.M09
TOOL 5:= N255T5M06  *---- (TOOL T5  OFFSET H5---)
         z:= N259G43H5Z.1M07
         z:= N261G99G83X-1.625Y-1.75Z-.6R0.1Q.04F5.
         z:= N264G0G80Z.1
         z:= N266G0H0Z0.M09
TOOL 6:= N267T6M06  *---- (TOOL T6  OFFSET H6---)
         z:= N271G43H6Z.1M07
         z:= N273G99G83X-1.625Y-1.75Z-.3R0.1Q.04F4.
         z:= N276G0G80Z.1
         z:= N278G0H0Z0.M09
TOOL 7:= N279T7M06  *---- (TOOL T7  OFFSET H7---)
         z:= N283G43H7Z.1M07
         z:= N285G99G81X-1.625Y-.75Z-.11R0.1F6.
         z:= N289G0G80Z.1
         z:= N291G0H0Z0.M09
TOOL 8:= N292T8M06  *---- (TOOL T8  OFFSET H8---)
         z:= N296G43H8Z.1M07
         z:= N298G99G83X-1.625Y-.75Z-.6R0.1Q.04F5.
         z:= N302G0G80Z.1
         z:= N304G0H0Z0.M09
TOOL 9:= N305T9M06  *---- (TOOL T9  OFFSET H9---)
         z:= N309G43H9Z.5M07
         z:= N311G99G84X-1.625Y-.75Z-.5R0.5F350.Q.0313
         z:= N315G0G80Z.5
         z:= N317G0H0Z0.M09
TOOL 10:= N318T10M06  *---- (TOOL T10  OFFSET H10---)
         z:= N322G43H10Z.1M07
         z:= N325G1Z-.012F5.
         z:= N327G0Z.1
         z:= N329G0H0Z0.M09
TOOL 11:= N330T11M06  *---- (TOOL T11  OFFSET H11---)
         z:= N334G43H11Z.1M07
         z:= N336G1Z-.015F5.
         z:= N338G0Z.1
         z:= N341G1Z-.015
         z:= N347G0Z.1
         z:= N349G1Z-.015
         z:= N355G0Z.1
         z:= N358G1Z-.015
         z:= N360G0Z.1
         z:= N363G1Z-.015
         z:= N374G0Z.1
         z:= N377G1Z-.015
         z:= N388G0Z.1
         z:= N391G1Z-.015
         z:= N393G0Z.1
         z:= N395G0H0Z0.M09

Title: Re: text file scanner
Post by: Endless Night on April 14, 2011, 02:05:20 PM
ALL DONE....
NOTE if you get a Z value of 1000 then a z value was never set.


Code: [Select]
function LoadData(fn)    -- by Cheffe
  local f,e = openfile(fn,"r")          --r means read-only (default)
  if f then                             --anything other than nil/false evaluates to true
    local s = f:read("*a")              --*a means read all
    f:close()
    return s
  else                                  --if openfile fails it returns nil plus an error message
    error(e,2)                          --stack level 2, error should be reported
  end                                   --from where LoadData() was called!
end

function EN_StringToStringTable(s)   -- imperfect code but suffices
  local t = {}
  local NewLineAt = 0
  local line = ''  
  local EOL = string.char(10)
  s = s..EOL..'**THEEND**'
  --print(string.byte(string.sub(s,22,22)))
  repeat
    NewLineAt = string.find(s, EOL)
    line = string.sub(s, 1 , NewLineAt - 1)    
    table.insert(t, line )
    s = string.sub(s, NewLineAt + 1, string.len(s))
    --print(line)
  until s == '**THEEND**'
  return t
end

function DoIt(fn)
  local sFile = LoadData(fn)  -- one big string of the files contents.
  local sT = EN_StringToStringTable(sFile)  

  local tools = 0
  local t = {}
  local z = {}
  for k=1,table.maxn(sT) do
     s = sT[k]
     -- Strip Comments
     if string.match(s, '*') then  s = string.sub(s, 1 , string.find(s, '*') - 1) end    

     if string.match(s,'M06')  then
         tools = tools + 1
         t[tools] = string.sub(s , string.find(s,'T') + 1, string.find(s,'M06')-1 ) -- Tool Number
         z[tools] = 1000
         end
     if string.match(s,'Z') then
        -- extact Z Value
        s = string.sub(s, string.find(s, 'Z') + 1, string.len(s))
        if string.find(s, "%a+") then  s = string.sub(s, 1, string.find(s, "%a+") - 1 ) end
        local y = tonumber(s)    
        if y < z[tools] then z[tools] = y end
        end            
     end
    
   for k=1,tools do
      print('Tool '..t[k]..': Z '..z[k])  
      end
    
--  for k=1,table.maxn(sT) do print(sT[k]) end  
end

---------------
DoIt('cnc.nc')

Output from above using test file
Code: [Select]
Tool 1: Z 0
Tool 2: Z -0.4
Tool 3: Z -0.4
Tool 4: Z -0.23
Tool 5: Z -0.6
Tool 6: Z -0.3
Tool 7: Z -0.11
Tool 8: Z -0.6
Tool 9: Z -0.5
Tool 10: Z -0.012
Tool 11: Z -0.015


Id advise that you test this against alot of test file to determine if works correctly in all circumstances... but it works correctly for the one test file i tried it against.
Title: Re: text file scanner
Post by: Khameleon on April 14, 2011, 02:16:19 PM
awesome.. that works great... I had to be a bug, but is there a way select a file sorta like an open file?
Title: Re: text file scanner
Post by: Endless Night on April 14, 2011, 02:18:16 PM
awesome.. that works great... I had to be a bug, but is there a way select a file sorta like an open file?

Glad it works great...   My skill in Lua sadly hasnt stretched to the GUI yet...  if i get time tommorrow ill take a look unless someone else cares to jump in at this point.