#target illustrator if (app.documents.length === 0) { alert("No document open"); exit(); } var doc = app.activeDocument; // ================= UI ================= var win = new Window("dialog", "CTP Optimizer v2 - Multi Size"); win.alignChildren = "fill"; // Sheet var pSheet = win.add("panel", undefined, "Sheet Size"); pSheet.orientation = "row"; pSheet.add("statictext", undefined, "Sheet:"); var sheetInput = pSheet.add("edittext", undefined, "11x14 in"); sheetInput.characters = 15; // Gap var pGap = win.add("panel", undefined, "Spacing"); pGap.orientation = "row"; pGap.add("statictext", undefined, "Gap (mm):"); var gapInput = pGap.add("edittext", undefined, "5"); gapInput.characters = 6; // Quantity var pQty = win.add("panel", undefined, "Size - Quantity"); pQty.orientation = "column"; var qtyInput = pQty.add("edittext", undefined, "S-50\nM-20\nL-30\nXL-50", {multiline:true}); qtyInput.preferredSize.height = 100; // Result var pRes = win.add("panel", undefined, "Result"); var resTxt = pRes.add("edittext", undefined, "", {multiline:true, readonly:true}); resTxt.preferredSize.height = 160; // Buttons var btns = win.add("group"); var calcBtn = btns.add("button", undefined, "Calculate"); btns.add("button", undefined, "Cancel", {name:"cancel"}); // ================= LOGIC ================= function parseSheetSize(str) { var unit = "in"; if (str.match(/mm/i)) unit = "mm"; if (str.match(/cm/i)) unit = "cm"; var nums = str.match(/([\d\.]+)\s*x\s*([\d\.]+)/i); if (!nums) return null; var w = parseFloat(nums[1]); var h = parseFloat(nums[2]); if (unit === "mm") { w *= 2.8346; h *= 2.8346; } else if (unit === "cm") { w *= 28.346; h *= 28.346; } else { w *= 72; h *= 72; } return {w:w, h:h}; } calcBtn.onClick = function () { if (doc.selection.length === 0) { alert("Please select one design object"); return; } var sheet = parseSheetSize(sheetInput.text); if (!sheet) { alert("Invalid sheet size format"); return; } var gap = parseFloat(gapInput.text) * 2.8346; var b = doc.selection[0].visibleBounds; var dW = b[2] - b[0]; var dH = b[1] - b[3]; var col = Math.floor((sheet.w + gap) / (dW + gap)); var row = Math.floor((sheet.h + gap) / (dH + gap)); var perSheet = col * row; if (perSheet <= 0) { alert("Design too big for sheet"); return; } var lines = qtyInput.text.split("\n"); var out = ""; var totalPlates = 0; for (var i = 0; i < lines.length; i++) { if (!lines[i]) continue; var p = lines[i].split("-"); if (p.length !== 2) continue; var size = p[0]; var qty = parseInt(p[1], 10); var sheets = Math.ceil(qty / perSheet); totalPlates += sheets; out += size + " : " + perSheet + " pcs/sheet | Qty " + qty + " | Sheets " + sheets + "\n"; } out += "\nTOTAL PLATES: " + totalPlates; resTxt.text = out; }; win.center(); win.show();