#target illustrator if (app.documents.length === 0) { alert("No document open"); exit(); } var doc = app.activeDocument; // ================= UI ================= var win = new Window("dialog", "CTP Sheet Optimizer"); win.alignChildren = "left"; // Sheet Panel var pSheet = win.add("panel", undefined, "Sheet Size"); pSheet.orientation = "grid"; pSheet.add("statictext", undefined, "Width:"); var sheetW = pSheet.add("edittext", undefined, "23"); sheetW.characters = 6; pSheet.add("statictext", undefined, "Height:"); var sheetH = pSheet.add("edittext", undefined, "36"); sheetH.characters = 6; // Design Source var pSource = win.add("panel", undefined, "Design Size From"); pSource.orientation = "column"; var rSel = pSource.add("radiobutton", undefined, "Selected Object"); var rArt = pSource.add("radiobutton", undefined, "Artboard"); rSel.value = true; // Spacing var pSpace = win.add("panel", undefined, "Margin / Gap (mm)"); pSpace.orientation = "grid"; pSpace.add("statictext", undefined, "Margin:"); var margin = pSpace.add("edittext", undefined, "10"); margin.characters = 6; pSpace.add("statictext", undefined, "Gap:"); var gap = pSpace.add("edittext", undefined, "5"); gap.characters = 6; // Rotation var rotateChk = win.add("checkbox", undefined, "Allow 90° Rotation"); rotateChk.value = true; // Result var pResult = win.add("panel", undefined, "Result"); pResult.orientation = "column"; var resultTxt = pResult.add("statictext", undefined, "", {multiline:true}); resultTxt.preferredSize.width = 260; resultTxt.preferredSize.height = 80; // Buttons var btnGroup = win.add("group"); var calcBtn = btnGroup.add("button", undefined, "Calculate"); btnGroup.add("button", undefined, "Cancel", {name:"cancel"}); // ================= LOGIC ================= calcBtn.onClick = function () { var sheetWidth = parseFloat(sheetW.text); var sheetHeight = parseFloat(sheetH.text); var m = parseFloat(margin.text) * 2.8346; // mm to pt var g = parseFloat(gap.text) * 2.8346; var dW, dH; if (rSel.value) { if (doc.selection.length === 0) { alert("Please select an object"); return; } var b = doc.selection[0].visibleBounds; dW = b[2] - b[0]; dH = b[1] - b[3]; } else { var ab = doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect; dW = ab[2] - ab[0]; dH = ab[1] - ab[3]; } // inch to pt sheetWidth *= 72; sheetHeight *= 72; var usableW = sheetWidth - m; var usableH = sheetHeight - m; var col1 = Math.floor((usableW + g) / (dW + g)); var row1 = Math.floor((usableH + g) / (dH + g)); var total1 = col1 * row1; var total2 = 0; if (rotateChk.value) { var col2 = Math.floor((usableW + g) / (dH + g)); var row2 = Math.floor((usableH + g) / (dW + g)); total2 = col2 * row2; } var best = Math.max(total1, total2); resultTxt.text = "Normal Fit : " + total1 + " pcs\n" + "Rotated Fit: " + total2 + " pcs\n\n" + "BEST FIT : " + best + " pcs per sheet"; }; win.center(); win.show();