Just copy & paste in the mcLua editor and Run or use it in a button script. Couldn't get the .mcs file attached.
Demonstrates variable scoping, using 'for' loops, return codes, and 'goto' statement.
Run it first. Then deref your axis and run it again to see if the script picks up that your axes are not homed. Then read the code remarks.
The functions are only examples of what we can do. I just did this as a learning process.
--josh
--comments on--
--local variables are associated within this script only
--you can define global variables in the Screen Load Up script
--variables pass through all functions in this script
--Define local variables and initialize
local inst = mc.mcGetInstance ()
local rc = 0
local remark = ""
--using a 'for' loop to find what axes are enables
--this example is just written a different way, it is harder to read but faster to write
function countEnabledAxis ()
local j = 0; for i=0, 11 do; if mc.mcAxisIsEnabled (inst, i) == 1 then j = j + 1; end; end
wx.wxMessageBox ("You have " ..j.." axes enabled."); return j
end
--using a 'for' loop to make sure the axes are homed before executing movement
--the number of iterations or loops is equal to the number of enabled axes only
--we then check to see if the number of axes is homed is equal to number of axis enabled, kinda like a signal verification check
function safetyCheck1 ()
local var = 0
var = countEnabledAxis ()
local j = 0
for i=0, var do
if mc.mcAxisIsHomed(inst,i) == 1 then
j = j + 1
wx.wxMessageBox ("Axis " ..j-1 .." is homed")
end
end
wx.wxMessageBox (var .." axes enabled\n" ..j .." axes homed")
if j ~= var then
return "Not all Axes are homed.", false
end
end
--if applicable above functions can be loaded into the Screen Load Up script to return a value to your button script
--functions in the Screen Load Up script are accessible to all button scripts
--visualize function main(), call for your defined global safetyCheck function and then execute main function in your button
function main()
wx.wxMessageBox ("All is good to execute some screwie louie custom button script.")
end
--this is the point at which the script actuall starts
--the first thing is to start a safety check, this check can be anything you want it to be
--if the machine is not where you want it to be the safety check will return false
wx.wxMessageBox ("Being safety check.")
remark, rc = safetyCheck1 ()
local var = rc
--safety check returns a message 'remark' and a return code value
--just want to demonstrate what we can use return codes for and the 'goto' statement
--if the machine is not where we want it, skip executing the main function of our button script and goto finish line
if rc == false then wx.wxMessageBox (tostring(remark)) mc.mcCntlEnable (inst, 0) end goto finish
main ()
::finish::
--this let's use execute our current script in the editor enviroment for debugging puposes
if mc.mcInEditor () == 1 then main() end
--comments off--
local inst = mc.mcGetInstance ()
local rc = 0
local remark = ""
function countEnabledAxis ()
local j = 0; for i=0, 11 do; if mc.mcAxisIsEnabled (inst, i) == 1 then j = j + 1; end; end
wx.wxMessageBox ("You have " ..j.." axes enabled."); return j
end
function safetyCheck1 ()
local var = 0
var = countEnabledAxis ()
local j = 0
for i=0, var do
if mc.mcAxisIsHomed(inst,i) == 1 then
j = j + 1
wx.wxMessageBox ("Axis " ..j-1 .." is homed")
end
end
wx.wxMessageBox (var .." axes enabled\n" ..j .." axes homed")
if j ~= var then
return "Not all Axes are homed.", false
end
end
function main()
wx.wxMessageBox ("All is good to execute some screwie louie custom button script.")
end
wx.wxMessageBox ("Being safety check.")
remark, rc = safetyCheck1 ()
local var = rc
if rc == false then wx.wxMessageBox (tostring(remark)) mc.mcCntlEnable (inst, 0) end goto finish
main ()
::finish::
if mc.mcInEditor () == 1 then main() end