Hello Guest it is April 29, 2024, 12:44:28 AM

Author Topic: Auto Load Last GCode File on Startup  (Read 161 times)

0 Members and 1 Guest are viewing this topic.

Auto Load Last GCode File on Startup
« on: March 05, 2024, 10:42:40 AM »
Is there any way on startup to automatically open the G-Code file that was open when Mach 4 was last closed?  In understand this is not a huge detail, but in my case it would be quite helpful.  Thanks in advance!

Offline cncmagic

*
  •  63 63
  • what me worry? heck...it ain't my machine anyway
    • View Profile
Re: Auto Load Last GCode File on Startup
« Reply #1 on: March 08, 2024, 12:02:27 AM »
you can probably write some code in the screen load script to do this. I wouldn't expect it to be too difficult. You'll need some experience with coding using LUA (C is close) and you'll need to search out how to locate the last file loaded, or write some code to store the filename and then retrieve it. To get to the screen load script before you enable Mach4, click on the Operator in the top menu.. then click Edit Screen. You'll now see the screen object tree on the left side of the screen. Click on 'WX4' which is most likely the screen set you are using. Then click the the lightning bolt in the Properties window below. Now you should see the selections for the screen load script, screen unload script, plc script, message script and timer script. Click on the box to the right of the 'SCREEN LOAD SCRIPT'  text.. then click on the small button on the right of the box you just clicked in. That will bring up the screen load script in the editor. Rest is up to you. There is useful info in the forum and the documentation that came with Mach4. Be careful.. you can make changes that will cause the program to either stop, fault, or lock up. Make a copy of the WX4.set file in the screens sub directory. If  you make changes and Mach4 won't load or run, replace the WX4 file in the sub-directory with the original. You might also want to look into the plc script.
Remember.. never take candy from a stranger.. nor advice.  :o
any semblance of information posted to anything remotely  close to accuracy is merely coincidence. Use at you own discretion.. or play the lottery.. same odds
Re: Auto Load Last GCode File on Startup
« Reply #2 on: March 09, 2024, 05:16:12 AM »
Put this function in your screenload script
Code: [Select]
function LoadLastGCode()
local inst = mc.mcGetInstance()

--This prevents a crash if there is already a loaded file e.g. when coming out of the screen editor
local curFile = mc.mcCntlGetGcodeFileName(inst)
if (curFile ~= "") then
do return end
end

--Mach4 already stores last loaded file in the machine.ini, this is used for the 'Load Recent File' button
--So we'll just read the file name and path from here
local fileName = mc.mcProfileGetString(inst, 'Preferences', 'MRU1', 'NO FILE')

--Load the file and check for errors
if (fileName ~= "NO FILE") then
local rc = mc.mcCntlLoadGcodeFile(inst, fileName)
if (rc ~= mc.MERROR_NOERROR) then
mc.mcCntlSetLastError(inst, "Error loading last g-code file")
else
mc.mcCntlSetLastError(inst, "Last G-Code file successfully loaded")
end
else
mc.mcCntlSetLastError(inst, "Last loaded g-code file not found")
end
end

and then at the bottom of the PLC script there is a 'PLC First Run' section. It will look something like this, you'll have some other functions in there as well to do with the keyboard. Just add the call to the function within the 'if' statement.
Code: [Select]
-------------------------------------------------------
--  PLC First Run
-------------------------------------------------------
if (testcount == 1) then
    LoadLastGCode() --Call the function here

...

[/cpde]