UI = {} --A convenient way to store all of the information the window/dialog creates. local inst = mc.mcGetInstance() -- create FixtureOffsetDialog UI.FixtureOffsetDialog = wx.wxDialog (wx.NULL, wx.wxID_ANY, "Fixture Offsets", wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxDEFAULT_DIALOG_STYLE )--create and name the window with desired settings UI.FixtureOffsetDialog:SetSizeHints( wx.wxDefaultSize, wx.wxDefaultSize ) UI.bSizer4 = wx.wxBoxSizer( wx.wxVERTICAL )--create a sizer to place elements in UI.m_FixtureOffsetGrid = wx.wxGrid( UI.FixtureOffsetDialog, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, 0 ) --create a grid for the various offsets. -------------------------GRID SETTINGS------------------------------------------ -- Grid UI.m_FixtureOffsetGrid:CreateGrid( 255, 6 ) UI.m_FixtureOffsetGrid:EnableDragGridSize( False ) UI.m_FixtureOffsetGrid:SetMargins( 0, 0 ) -- Columns UI.m_FixtureOffsetGrid:SetColSize( 0, 60 ) UI.m_FixtureOffsetGrid:SetColSize( 1, 60 ) UI.m_FixtureOffsetGrid:SetColSize( 2, 60 ) UI.m_FixtureOffsetGrid:SetColSize( 3, 60 ) UI.m_FixtureOffsetGrid:SetColSize( 4, 60 ) UI.m_FixtureOffsetGrid:SetColSize( 5, 60 ) UI.m_FixtureOffsetGrid:SetColSize( 6, 60 ) UI.m_FixtureOffsetGrid:EnableDragColSize( True ) UI.m_FixtureOffsetGrid:SetColLabelSize( 30 ) UI.m_FixtureOffsetGrid:SetColLabelAlignment( wx.wxALIGN_CENTRE, wx.wxALIGN_CENTRE ) -- Rows UI.m_FixtureOffsetGrid:AutoSizeRows() UI.m_FixtureOffsetGrid:EnableDragRowSize( True ) UI.m_FixtureOffsetGrid:SetRowLabelSize( 80 ) UI.m_FixtureOffsetGrid:SetRowLabelAlignment( wx.wxALIGN_CENTRE, wx.wxALIGN_CENTRE ) -- Label Appearance -- Cell Defaults UI.m_FixtureOffsetGrid:SetDefaultCellAlignment( wx.wxALIGN_LEFT, wx.wxALIGN_TOP ) UI.m_FixtureOffsetGrid:SetMinSize( wx.wxSize( 700,300 ) ) UI.m_FixtureOffsetGrid:SetMaxSize( wx.wxSize( -1,750 ) ) -------------------------END GRID SETTINGS------------------------------------------ UI.bSizer4:Add( UI.m_FixtureOffsetGrid, 0, wx.wxALL + wx.wxALIGN_CENTER_HORIZONTAL, 5 )--Add the grid to the dialog. UI.m_sdbSizer2 = wx.wxStdDialogButtonSizer()--Add an "OK" button UI.m_sdbSizer2OK = wx.wxButton( UI.FixtureOffsetDialog, wx.wxID_OK, "" ) UI.m_sdbSizer2:AddButton( UI.m_sdbSizer2OK ) UI.m_sdbSizer2:Realize(); UI.bSizer4:Add( UI.m_sdbSizer2, 1, wx.wxALIGN_CENTER_HORIZONTAL, 5 ) UI.FixtureOffsetDialog:SetSizer( UI.bSizer4 ) UI.FixtureOffsetDialog:Layout() UI.bSizer4:Fit( UI.FixtureOffsetDialog ) UI.FixtureOffsetDialog:Centre( wx.wxBOTH ) -- Connect Events lastrow = 0 lastcol = 0 UI.m_FixtureOffsetGrid:Connect( wx.wxEVT_GRID_CELL_CHANGE, function(event) --This is an event handler that happens when the user changes data in a cell. row = event:GetRow() col = event:GetCol() event:Skip() rc = UI.m_FixtureOffsetGrid:GetCellValue(row, col) --We get the value of the cell, and then format it, and set the offset value. rc = round (tonumber(rc), 4) --rounds rc to 4 digits. param = GetID(row,col) er = mc.mcCntlSetPoundVar(inst,param, rc); end ) UI.m_FixtureOffsetGrid:Connect( wx.wxEVT_GRID_CELL_LEFT_CLICK, function(event) --This is an event handler that happens when the user clicks in a cell. local rownum = event:GetRow() local colnum = event:GetCol() if(lastrow ~= rownum or lastcol ~= colnum) then UI.m_FixtureOffsetGrid:SetCellBackgroundColour(tonumber(rownum), tonumber(colnum), wx.wxColour(wx.wxGREEN))--This code changes the new cell to green UI.m_FixtureOffsetGrid:SetCellBackgroundColour(tonumber(lastrow), tonumber(lastcol), wx.wxColour(wx.wxWHITE))--This cell changes the old green cell to white. lastrow = rownum--update the "old cell" variables lastcol = colnum UI.m_FixtureOffsetGrid:ForceRefresh() end event:Skip() end ) function SetupLabels() UI.m_FixtureOffsetGrid:SetColLabelValue(0, "X") UI.m_FixtureOffsetGrid:SetColLabelValue(1, "Y") UI.m_FixtureOffsetGrid:SetColLabelValue(2, "Z") UI.m_FixtureOffsetGrid:SetColLabelValue(3, "A") UI.m_FixtureOffsetGrid:SetColLabelValue(4, "B") UI.m_FixtureOffsetGrid:SetColLabelValue(5, "C") UI.m_FixtureOffsetGrid:SetRowLabelValue(0, "Work Shift") UI.m_FixtureOffsetGrid:SetRowLabelValue(1, "Head Shift") UI.m_FixtureOffsetGrid:SetRowLabelValue(2, "G54") UI.m_FixtureOffsetGrid:SetRowLabelValue(3, "G55") UI.m_FixtureOffsetGrid:SetRowLabelValue(4, "G56") UI.m_FixtureOffsetGrid:SetRowLabelValue(5, "G57") UI.m_FixtureOffsetGrid:SetRowLabelValue(6, "G58") UI.m_FixtureOffsetGrid:SetRowLabelValue(7, "G59") local numrows = 254 local numcols = 6 parameter = 3 for i = 8, numrows do --since we are starting on the eigth row, and it is indexed at 0, we start this for loop at 8. local labelstring = "G54.1 P" UI.m_FixtureOffsetGrid:SetRowLabelValue(i, labelstring..tostring(parameter)) parameter = parameter + 1 end end function GetID(offset, axis)--This returns the #variable that is associated with a given row of the offset table. index = 0; if(offset == 0) then index = mc.SV_WORK_SHIFT_X + axis--any mc.ALL_CAPS are #define variables in the MachAPI.h, they are constant values that allow us to start indexing at the correct value. elseif(offset == 1) then index = mc.SV_HEAD_SHIFT_X + axis else index = (mc.MC_FIXTURES_START + axis + ((offset - 2) * mc.MC_FIXTURES_INC)); end return (index); end function SetupFixtureTable() local numrows = 254 local numcols = 6 for i = 0, numrows do--Loops through the number of rows for j = 0, numcols do--loops through the number of columns param = GetID(i, j);--Gets the #variable associated with a given cell. (i being row, j being column) local rc, er = mc.mcCntlGetPoundVar(inst, param) local tmpstring if(rc ~= nil) then--If the value is not nil, then we format it rc = round (rc, 4) --rounds rc to 4 digits. tmpstring = tostring(rc) else--otherwise, we just use 0 tmpstring = "0" end UI.m_FixtureOffsetGrid:SetCellValue(i,j,tmpstring)--Update the cell with the value in the #variable. end end end function round(num, numdigits) local mult = 10^(numdigits or 0) return math.floor(num * mult + 0.5) / mult end SetupLabels() SetupFixtureTable() UI.FixtureOffsetDialog:Show()--Shows the dialog to the user. --wx.wxGetApp():MainLoop()