ALL DONE....
NOTE if you get a Z value of 1000 then a z value was never set.
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
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.