Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: compewter_numerical on September 06, 2019, 10:31:13 PM

Title: Load Image Programatically
Post by: compewter_numerical on September 06, 2019, 10:31:13 PM
Hello,

Is there a way to load an image into the screen programatically?

What I am trying to do is load an image for each different gcode file I use. In the first line of the gcode file I have a comment like this: (Image:[imageName.png]).

I created a simple panel with a wxStaticBitmap object and put that into a Lua Panel script in the screen editor.
I am able to parse the filename using string.gmatch, get a pointer to the absolute filepath and load the file into a wxStaticBitmap object. But, Mach4 would crash everytime I would try to use SetImage() in the Lua Panel update event.

The people that use the machine don't know how to use the screen editor and it's not possible to train them how to do it because of...reasons.

I can change the image no problem if I use the built-in image object in the screen editor. I want to be able to load an image into the bitmap object that is in the screen editor but I want to do this without the need to load the image into the screen.

Is this possible? Any help is much appreciated.
Title: Re: Load Image Programatically
Post by: SwiftyJ on September 07, 2019, 04:36:55 AM
Hi, to load an image into a wxStaticBitmap you have to use wxSetBitmap as below

Code: [Select]
wx.wxStaticBitmap:SetBitmap(wx.wxBitmap("imagelocation\\image.jpg", wx.wxBITMAP_TYPE_JPEG))



I tried this in the UI update event, but found that the image flickered. I think it would probably be better to monitor if a new g-code has been loaded in the Update UI event and then run a function once to read the file path from the g-code load the image.
Title: Re: Load Image Programatically
Post by: compewter_numerical on September 08, 2019, 01:53:36 AM
Thanks for the response. I have tried many ways to get this to work but unfortunately have run out of time to keep exploring. I was using SetBitmap originally with no luck I think I just said SetImage in the OP unintentionally because I wasn't looking at the code when I asked the question. I will eventually come back to this in the future but for now I got to put it away. If I get it to work I'll definitely update the forum with the code.

This is what I originally tried and now it won't even load the initial image. It can't even find the file with the file_exists function.
I lost the original code I was working with but I think it will come back to me later.


local inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)

dirPath = path .. "\\GcodeFiles\\img\\"
imgChangeReg = mc.mcRegGetHandle(inst, "gRegs0/ImgChange")
imgNameReg = mc.mcRegGetHandle(inst, "gRegs0/ImgName")

--panel = mcLuaPanelParent
panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxSize(500,300), wx.wxTAB_TRAVERSAL)
gSizer1 = wx.wxGridSizer(0, 0, 0, 0)

mc.mcCntlSetLastError(inst, dirPath)

function file_exists(file)
     local f = io.open(file, "rb")
     if f then f:close() end
     return f ~= nil
end

if not file_exists(dirPath .. "1.png") then
   mc.mcCntlSetLastError(inst, "File exists!")
else
   mc.mcCntlSetLastError(inst, "File does not exist! " .. dirPath .. "1.png")
end

bitmapObj = wx.wxStaticBitmap(panel, wx.wxID_ANY, wx.wxBitmap(dirPath .. "1.png", wx.wxBITMAP_TYPE_ANY), wx.wxDefaultPosition, wx.wxDefaultSize, 0)

gSizer1:Add(bitmapObj, 0, wx.wxALL + wx.wxEXPAND, 0)

panel:SetSizer(gSizer1)
panel:Layout()
panel:Fit()

panel:Connect(wx.wxEVT_UPDATE_UI, function(event)
   local imgChanged = mc.mcRegGetValueLong(imgChangeReg)
   --mc.mcCntlSetLastError(inst, tostring(imgChanged))

   if imgChanged > 0 then
      --mc.mcCntlSetLastError(inst, tostring(imgChanged))
      local imgName = mc.mcRegGetValueString(imgNameReg)
      mc.mcCntlSetLastError(inst, tostring(imgName))
      mc.mcCntlSetLastError(inst, dirPath .. imgName)
      --bitmapObj:SetBitmap(wx.wxBitmap(dirPath .. imgName, wx.wxBITMAP_TYPE_ANY))
      mc.mcRegSetValueLong(imgChangeReg, 0)
   end

   --event:Skip()
end)   


Thanks Again!