local f = {} local sec = "FontEngraving" local inst = 0 local post = nil local function LeadingZeros(num, digits) --send in text and number of leading zeros --output string with leading zeros local lz = cbLeadZero:GetValue() if not lz then digits = 0 end txt = string.format("%0" .. tostring(digits) .. "d", num) return txt end local function StrVarReplace(str) --Replace variables in string with values local tblVars = { [1] = "SN", [2] = "DATE", [3] = "TIME" } local tblVals = { ["SN"] = LeadingZeros(txtSN:GetValue(), txtNumDigits:GetValue()), ["DATE"] = os.date("%x"), ["TIME"] = os.date("%X") } for _,v in ipairs(tblVars) do rplc = tostring(tblVals[v]) if (v == "SN") and (cbLeadZero:GetValue()) then digits = txtNumDigits:GetValue() rplc = string.format("%0"..digits.."d", rplc) end var = "<" .. tostring(v) .. ">" str = str:gsub(var, rplc) end return str end local function GetStringGcode(festr, xstart, ystart) post.ClearSettings(xstart, ystart) local strcode = "" local strl = festr:len() local i = 0; for i=1,strl,1 do local character = string.sub(festr, i, i) local ascii = string.byte(festr, i) func = tostring(character) if (_G[func] == nil) then func = "ascii" .. tostring(ascii) if(_G[func] == nil) then func = "ascii32" end end local lettercode = _G[func]() if (func ~= "ascii32") then strcode = strcode .. lettercode .. "\n" end end return strcode end local function GenGcode(input) local MachDirectory = mc.mcCntlGetMachDir(inst) local Profile = mc.mcProfileGetName(inst) local ScriptPath = MachDirectory .. "\\Modules\\AddOns\\FontEngraving\\?.lua;" local fontpath = mc.mcProfileGetString(inst, sec, "FONT", MachDirectory .. "\\Modules\\AddOns\\FontEngraving\\Fonts\\ScriptFont.lua") local path, file, ext = string.match(fontpath, "(.-)([^\\/]-%.?)([^%.\\/]*)$") local file = string.match(file, "[^%.]*") local ScriptPath = path .. "?." .. ext -- look for this path in the existing package.path. local spos = string.find(package.path, ScriptPath) if (spos == nil) then -- The path is not there, append it. package.path = package.path .. ";" .. ScriptPath end mcStraightFont = require (file) _G = mcStraightFont --Gcode string local gcode = "" --Check if input is a table or string. if it is a string add it to the txt table at index 0 local txt = {} if (input == "string") then txt[1] = input else txt = input end local numlines = #txt local index = 1 local gc = {} local ll = {} local align = rbAlignment:GetSelection() local xpos = tonumber(txtXPos:GetValue()) local ypos = tonumber(txtYPos:GetValue()) --y pos of first line and increment value local yinc = post.height * 1.25 local halflines = numlines / 2 if (halflines < 1) then ypos = ypos - (post.height / 2) else ypos = ypos + (yinc * (halflines - 1)) + (post.height * .125) end for _,r in ipairs(txt) do --gencode for each line gc[index] = GetStringGcode(r) ll[index] = post.letterStartX - (.25 * post.height) --set xpos local xpos2 = xpos if (align == 1) then xpos2 = xpos - ll[index]/2 elseif (align == 2) then xpos2 = xpos - ll[index] end gc[index] = GetStringGcode(r, xpos2, ypos) gcode = gcode .. gc[index] --increment ypos ypos = ypos - yinc --increment line count index = index + 1 end --var for character function local func = "" --gcode header gcode = post.ProgStart() .. gcode --gcode footer gcode = gcode .. post.ProgEnd() return gcode, ll end local function AssembleLines() local lcount = txtEngrave:GetNumberOfLines() local txt = {} local sn = txtSN:GetValue() local digits = txtNumDigits:GetValue() sn = LeadingZeros(sn, digits) for i = 0, (lcount - 1) do j = i + 1 txt[j] = txtEngrave:GetLineText(i) txt[j] = StrVarReplace(txt[j]) if (j == 1) then if (snPos == -1) then local spaces = spacePrefix:GetValue() if (tonumber(spaces) > 0) then for i = 1, tonumber(spaces) do sn = sn .. " " end end txt[j] = sn .. txt[j] elseif (snPos == 1) then local spaces = spaceSuffix:GetValue() if (tonumber(spaces) > 0) then for i = 1, tonumber(spaces) do sn = " " .. sn end end txt[j] = txt[j] .. sn end end end return txt end local function LoadParams(retonly) local zpos = mc.mcProfileGetString(inst, sec, "ZPOS", "0.0000") local depth = mc.mcProfileGetString(inst, sec, "DEPTH", "0.0080") local rapidh = mc.mcProfileGetString(inst, sec, "RAPIDHEIGHT", "0.250") local feedh = mc.mcProfileGetString(inst, sec, "FEEDHEIGHT", "0.0000") local plungef = mc.mcProfileGetString(inst, sec, "PLUNGEFEED", "30") local cutf = mc.mcProfileGetString(inst, sec, "CUTFEED", "30") local rpm = mc.mcProfileGetString(inst, sec, "RPM", "10000") local font = mc.mcProfileGetString(inst, sec, "FONT", "C:\\Mach4Hobby\\Modules\\AddOns\\FontEngraving\\Fonts\\StraightFont.lua") if not retonly then txtZPosition:SetValue(zpos) txtZDepth:SetValue(depth) txtRapidHeight:SetValue(rapidh) txtFeedHeight:SetValue(feedh) txtPlungeFeed:SetValue(plungef) txtCutFeed:SetValue(cutf) txtRPM:SetValue(rpm) fileFont:SetPath(font) end return zpos, depth, rapidh, feedh, plungef, cutf, rpm, font end local function SetVals() post.ClearSettings() local height = txtHeight:GetValue() local xpos = txtXPos:GetValue() local ypos = txtYPos:GetValue() post.setHeight(tonumber(height)) post.setXStart(tonumber(xpos)) post.setYStart(tonumber(ypos)) local zpos, depth, rapidh, feedh, plungef, cutf, rpm = LoadParams(true) post.setRapidHeight(zpos + rapidh) post.setFeedHeight(zpos + feedh) post.setDepth(zpos - depth) post.setFeedPlung(plungef) post.setFeedCut(cutf) post.setRPM(rpm) return true end local function Update() local txt = AssembleLines() SetVals() local gcode, ll = GenGcode(txt) local length = 0 for _,l in ipairs (ll) do if (l > length) then length = l end end txtWidth:SetValue(string.format("%.4f", length)) end local function LoadFEPanel() local snPos = mc.mcProfileGetString(inst, sec, "SNPOS", "0") if (snPos == "-1") then rbSNPre:SetValue(true) elseif (snPos == "1") then rbSNSuf:SetValue(true) else rbSNNone:SetValue(true) end local spacepre = mc.mcProfileGetString(inst, sec, "SPACEPREFIX", "0") local spacesuf = mc.mcProfileGetString(inst, sec, "SPACESUFFIX", "0") local text = mc.mcProfileGetString(inst, sec, "TEXT", "Serial number \nDate at