OK, it wrote me the following M6 script that basically uses the pocket number that is referenced to the tool #. I like this alot. super minimal and streamlined.
function m6()
local inst = mc.mcGetInstance()
-- Get the selected tool and current tool
local selectedTool = mc.mcToolGetSelected(inst) -- Tool requested by T# command
local currentTool = mc.mcToolGetCurrent(inst) -- Tool currently in the spindle
-- If the selected tool is the same as the current tool, no tool change is needed
if selectedTool == currentTool then
mc.mcCntlSetLastError(inst, string.format("Tool %d is already loaded.", currentTool))
return
end
-- Get the Pocket # for the selected tool from the Tool Table
local pocket = mc.mcToolGetData(inst, mc.MTOOL_POCKET, selectedTool)
if not pocket or pocket <= 0 then
mc.mcCntlSetLastError(inst, string.format("Error: No pocket assigned for Tool %d.", selectedTool))
return
end
-- Display a message about the tool change
mc.mcCntlSetLastError(inst, string.format("Changing to Tool %d from Pocket %d.", selectedTool, pocket))
-- Move to a safe position for tool change
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 Z0") -- Move Z-axis to machine home (modify as needed)
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X0 Y0") -- Move to tool change position (adjust coordinates)
-- Simulate tool change logic
-- If you have an ATC (Automatic Tool Changer), you would insert code here to control the ATC
-- Example: Send commands to move the tool changer to the correct pocket and load the tool
mc.mcCntlSetLastError(inst, string.format("Loading Tool %d from Pocket %d...", selectedTool, pocket))
-- Set the selected tool as the current tool
mc.mcToolSetCurrent(inst, selectedTool)
end
if (mc.mcInEditor() == 1) then
m6() -- Execute the function if running in the Mach4 script editor
end