307
« on: February 09, 2018, 03:42:12 PM »
Since I don’t use a CAM program often I find myself writing macros and always need to know what settings are current.
I’m not very good at remembering which parameters and letters are supposed to be sent for macros that I created awhile ago. You know, m6875 D.25 L1.5 P15…which did d, l and P stand for? And in your macro having to grab the values from the param list.
if (hVars ~= nil) then
local DFlag = mc.mcCntlGetLocalVarFlag(inst, hVars, mc.SV_D)
if(DFlag == 1) then
local dia = mc.mcCntlGetLocalVar(inst, hVars, mc.SV_D)
end
local LFlag = mc.mcCntlGetLocalVarFlag(inst, hVars, mc.SV_L)
if(LFlag == 1) then
local length = mc.mcCntlGetLocalVar(inst, hVars, mc.SV_L)
end
local DFlag = mc.mcCntlGetLocalVarFlag(inst, hVars, mc.SV_P)
if(PFlag == 1) then
local DOC= mc.mcCntlGetLocalVar(inst, hVars, mc.SV_P)
end
end
Wouldn’t it be nice if you could just read the values from the panel dro’s, led’s,… from your macro
function m6875()
local dia=myModule.getProperty(‘droDia’,’Value’)
local length=myModule.getProperty(‘txtLength’,’Value’)
local DOC=myModule.getProperty(‘droDia’,’Value’)
…
end
With the following additions you can get/set screen properties. Concept:
Create 4 gRegs0 registers:
gRegs0:gpFlag
gRegs0:gpComponentName
gRegs0:gpComponentAttribute
gRegs0:gpComponentResult
Macro calls for getting/setting a component’s property through a module function that sets registers and sets a register flag to request the action. Then waits for the flag to indicate that it is finished.
PLC monitors a register to determine if a get or set property has been called by a macro, when requested, calls the function in the Load Script
Load script function reads the component name and component property from registers and either gets/sets the property. Posts the result in a register and sets the flag that it is finished.
PLC monitor
------------------------------------------------------------------
--PLC script addition
------------------------------------------------------------
-- checks to see if macro/module has requested a screen property --myChangeProperty()
------------------------------------------------------------
local hReg,rc= mc.mcRegGetHandle(inst,'gRegs0/gpFlag')
if hReg~=nil then
local flag=mc.mcRegGetValue(hReg)
if flag==0 then return end --no request
if flag==1 then --new request to get a property
mc.mcRegSetValueLong(hReg,2) --set flag to processing request
myGetProperty() --get the requested data and set result and set flag to 0
end
if flag==3 then --new request to set a property
mc.mcRegSetValueLong(hReg,2) --set flag to processing request
mySetProperty() --set the requested property and set result and set flag to 0
end
end
Screen Load Functions
------------------------------------------------
--Add to Screen Load Script
------------------------------------------------
function myGetProperty()
local hflag=mc.mcRegGetHandle(inst,'gRegs0/gpFlag')
local hcmp=mc.mcRegGetHandle(inst,'gRegs0/gpComponentName')
local hattr=mc.mcRegGetHandle(inst,'gRegs0/gpComponentAttribute')
local hrslt=mc.mcRegGetHandle(inst,'gRegs0/gpComponentResult')
local Cname=mc.mcRegGetValueString(hcmp)
local Cattr=mc.mcRegGetValueString(hattr)
local Rslt,rc=scr.GetProperty(Cname,Cattr)
if (rc == mc.MERROR_NOERROR) then
mc.mcRegSetValueString(hrslt,tostring(Rslt))
mc.mcRegSetValueString(hcmp,'')
mc.mcRegSetValueString(hattr,'')
mc.mcRegSetValue(hflag,0)
elseif rc~=nil then
mc.mcRegSetValueString(hrslt,'')
mc.mcRegSetValue(hflag,rc)
end
end
function mySetProperty()
local hflag=mc.mcRegGetHandle(inst,'gRegs0/gpFlag')
local hcmp=mc.mcRegGetHandle(inst,'gRegs0/gpComponentName')
local hattr=mc.mcRegGetHandle(inst,'gRegs0/gpComponentAttribute')
local hrslt=mc.mcRegGetHandle(inst,'gRegs0/gpComponentResult')
local Cname=mc.mcRegGetValueString(hcmp)
local Cattr=mc.mcRegGetValueString(hattr)
local Rslt=mc.mcRegGetValueString(hrslt)
local rc=scr.SetProperty(Cname,Cattr,Rslt)
if (rc == mc.MERROR_NOERROR) then
mc.mcRegSetValueString(hrslt,Rslt)
mc.mcRegSetValueString(hcmp,'')
mc.mcRegSetValueString(hattr,'')
mc.mcRegSetValue(hflag,0)
elseif rc~=nil then
mc.mcRegSetValueString(hrslt,'')
mc.mcRegSetValueString(hflag,rc)
end
end
Module Code
------------------------------------------------------------------
--Module definition
------------------------------------------------------------------
local rtModule = {}
function rtModule.rtChangeProperty(cName,cAttr,getSet,vlu)
local hflag=mc.mcRegGetHandle(inst,'gRegs0/gpFlag')
local hcmp=mc.mcRegGetHandle(inst,'gRegs0/gpComponentName')
local hattr=mc.mcRegGetHandle(inst,'gRegs0/gpComponentAttribute')
local hrslt=mc.mcRegGetHandle(inst,'gRegs0/gpComponentResult')
mc.mcRegSetValueString(hcmp,cName)
mc.mcRegSetValueString(hattr,cAttr)
mc.mcRegSetValue(hflag,getSet)
mc.mcRegSetValueString(hrslt,tostring(vlu))
wx.wxMilliSleep(200)
while mc.mcRegGetValue(hflag)==2 do
wx.wxMilliSleep(100)
end
local Rslt,rc=mc.mcRegGetValueString(hrslt)
return Rslt
end
function rtModule.rtGetProperty(cName,cAttr)
return rtModule.rtChangeProperty(cName,cAttr,1)
end;
function rtModule.rtSetProperty(cName,cAttr,vlu)
if vlu==nil then wx.wxMessageBox('Invalid Value') return end
return rtModule.rtChangeProperty(cName,cAttr,3,vlu)
end;
return rtModule
Macro
-------------------------------------------------------------------------
--Macro for testing
--------------------------------------------------------------------------
function m995()
-- local rslt=rt.rtGetProperty('ledMeasOnly','Value')
-- local rslt=rt.rtSetProperty('ledMeasOnly','Value',1)
-- local rslt=rt.rtSetProperty('btn(70)','Label','new test')
-- local rslt=rt.rtSetProperty('droProbeDia','Value',0.665)
wx.wxMessageBox('In 995: '..tostring(rslt))
end
Obviously this can be used for many other tasks beside an alternate way of getting macro parameters.
HTH