illustrator是矢量编辑软件,画板是绘制处理的重要容器,在印刷方面的一个重要功能就是绘制外箱,开发一个绘制外箱功能,以下功能仅用于学习交流,请勿用于非法用途和商业用途,源代码如下所示:
var myDoc = activeDocument;
var pro1 = prompt("请依照以下模式输入长宽高 (单位:mm)", "500x400x300", undefined);
var ldh = new String(pro1);
var array2 = ldh.split(",");
var nums2 = [];
for (var i = 0; i < array2.length; i += 1) {
nums2.push(array2[i]);
}
for (var k = 0; k < nums2.length; k += 1) {
var array = nums2[k].split("x");
var nums = [];
for (var i = 0; i < array.length; i += 1) {
nums.push(parseInt(array[i]));
}
var l = nums[0];
var d = nums[1];
var h = nums[2];
var tomm = 2.834646;
var ll = l * tomm;
var dd = d * tomm;
var hh = h * tomm;
var xx = 0 * tomm;
var yy = 0 * tomm;
myDoc.defaultStroked = true;
myDoc.defaultFilled = false;
var group1 = myDoc.groupItems.add();
var rect = group1.pathItems.rectangle(yy + (dd / 2), xx, ll, -hh);
var rect = group1.pathItems.rectangle(yy + (dd / 2), xx + ll, dd, -hh);
var rect = group1.pathItems.rectangle(yy + (dd / 2), xx + ll + dd, ll, -hh);
var rect = group1.pathItems.rectangle(yy + (dd / 2), xx + ll + ll + dd, dd, -hh);
var rect = group1.pathItems.rectangle(yy + hh + (dd / 2), xx, ll, -dd / 2);
var rect = group1.pathItems.rectangle(yy + hh + (dd / 2), xx + ll, dd, -dd / 2);
var rect = group1.pathItems.rectangle(yy + hh + (dd / 2), xx + ll + dd, ll, -dd / 2);
var rect = group1.pathItems.rectangle(yy + hh + (dd / 2), xx + ll + ll + dd, dd, -dd / 2);
var rect = group1.pathItems.rectangle(yy, xx, ll, -dd / 2);
var rect = group1.pathItems.rectangle(yy, xx + ll, dd, -dd / 2);
var rect = group1.pathItems.rectangle(yy, xx + ll + dd, ll, -dd / 2);
var rect = group1.pathItems.rectangle(yy, xx + ll + ll + dd, dd, -dd / 2);
var textRef1 = group1.textFrames.add();
textRef1.contents = "纸箱尺寸:" + l + "x" + d + "x" + h + " mm";
textRef1.top = yy + hh + dd + 100;
textRef1.left = xx + 0;
textRef1.textRange.characterAttributes.size = 100;
newCMYKColor = new CMYKColor();
newCMYKColor.cyan = 0;
newCMYKColor.magenta = 100;
newCMYKColor.yellow = 100;
newCMYKColor.black = 0;
textRef1.textRange.characterAttributes.fillColor = newCMYKColor;
group1.selected = true;
app.executeMenuCommand("Fit Artboard to artwork bounds");
center();
}
function center() {
if (documents.length > 0) {
if (activeDocument.selection.length > 0) {
mySelection = activeDocument.selection;
if (mySelection instanceof Array) {
initBounds = mySelection[0].visibleBounds;
ul_x = initBounds[0];
ul_y = initBounds[1];
lr_x = initBounds[2];
lr_y = initBounds[3];
for (var i = 1; i < mySelection.length; i += 1) {
groupBounds = mySelection[i].visibleBounds;
if (groupBounds[0] < ul_x) {
ul_x = groupBounds[0];
}
if (groupBounds[1] > ul_y) {
ul_y = groupBounds[1];
}
if (groupBounds[2] > lr_x) {
lr_x = groupBounds[2];
}
if (groupBounds[3] < lr_y) {
lr_y = groupBounds[3];
}
}
}
activeDocument.views[0].zoom = 1;
ScreenSize = activeDocument.views[0].bounds;
ScreenWidth = ScreenSize[2] - ScreenSize[0];
ScreenHeight = ScreenSize[1] - ScreenSize[3];
screenProportion = ScreenHeight / ScreenWidth;
cntrPos = [ul_x, ul_y];
mySelWidth = lr_x - ul_x;
mySelHeight = ul_y - lr_y;
cntrPos[0] = ul_x + (mySelWidth / 2);
cntrPos[1] = ul_y - (mySelHeight / 2);
activeDocument.views[0].centerPoint = cntrPos;
zoomFactorW = ScreenWidth / mySelWidth;
zoomFactorH = ScreenHeight / mySelHeight;
if ((mySelWidth * screenProportion) >= mySelHeight) {
zF = zoomFactorW;
} else {
zF = zoomFactorH;
}
activeDocument.views[0].zoom = zF * 0.85;
} else {
activeDocument.activeView.zoom = 1;
}
}
}
合理的脚本代码可以有效的提高工作效率,减少重复劳动。
文章引用至 作者知了-联系方式1
文章引用至 作者知了-联系方式2