illustrator是矢量编辑软件,画板是绘制处理的重要容器,在印刷方面的一个重要功能就是标注尺寸,开发一个尺寸标注功能,以下功能仅用于学习交流,请勿用于非法用途,源代码如下所示:
if (app.documents.length > 0) {
var doc = activeDocument;
var selectedItems = parseInt(doc.selection.length, 10) || 0;
var setUnits = true;
var defaultUnits = $.getenv("Specify_defaultUnits") ? convertToBoolean($.getenv("Specify_defaultUnits")) : setUnits;
var setFontSize = 10;
var defaultFontSize = $.getenv("Specify_defaultFontSize") ? convertToUnits($.getenv("Specify_defaultFontSize")).toFixed(3) : setFontSize;
var setRed = 255;
var defaultColorRed = $.getenv("Specify_defaultColorRed") ? $.getenv("Specify_defaultColorRed") : setRed;
var setGreen = 0;
var defaultColorGreen = $.getenv("Specify_defaultColorGreen") ? $.getenv("Specify_defaultColorGreen") : setGreen;
var setBlue = 0;
var defaultColorBlue = $.getenv("Specify_defaultColorBlue") ? $.getenv("Specify_defaultColorBlue") : setBlue;
var setDecimals = 2;
var defaultDecimals = $.getenv("Specify_defaultDecimals") ? $.getenv("Specify_defaultDecimals") : setDecimals;
var specifyDialogBox = new Window("dialog", "尺寸标注");
specifyDialogBox.alignChildren = "left";
dimensionPanel = specifyDialogBox.add("panel", undefined, "选择需标注的位置");
dimensionPanel.orientation = "column";
dimensionPanel.alignment = "left";
dimensionPanel.margins = [20, 10, 35, 10];
dimensionGroup = dimensionPanel.add("group");
dimensionGroup.orientation = "row";
topCheckbox = dimensionGroup.add("checkbox", undefined, "上").helpTip = "标注对象的上边.";
topCheckbox.value = false;
rightCheckbox = dimensionGroup.add("checkbox", undefined, "右").helpTip = "标注对象的右边.";
rightCheckbox.value = false;
bottomCheckbox = dimensionGroup.add("checkbox", undefined, "下").helpTip = "标注对象的下边.";
bottomCheckbox.value = false;
leftCheckbox = dimensionGroup.add("checkbox", undefined, "左").helpTip = "标注对象的左边.";
leftCheckbox.value = false;
selectAllGroup = dimensionPanel.add("group");
selectAllGroup.orientation = "row";
selectAllCheckbox = selectAllGroup.add("checkbox", undefined, "标注四边").helpTip = "标注对象的四边.";
selectAllCheckbox.value = false;
selectAllCheckbox.onClick = function() {
if (selectAllCheckbox.value) {
topCheckbox.value = true;
topCheckbox.enabled = false;
rightCheckbox.value = true;
rightCheckbox.enabled = false;
bottomCheckbox.value = true;
bottomCheckbox.enabled = false;
leftCheckbox.value = true;
leftCheckbox.enabled = false;
} else {
topCheckbox.value = false;
topCheckbox.enabled = true;
rightCheckbox.value = false;
rightCheckbox.enabled = true;
bottomCheckbox.value = false;
bottomCheckbox.enabled = true;
leftCheckbox.value = false;
leftCheckbox.enabled = true;
}
};
optionsPanel = specifyDialogBox.add("panel", undefined, "选项");
optionsPanel.orientation = "column";
optionsPanel.margins = 10;
optionsPanel.alignChildren = "left";
units = optionsPanel.add("checkbox", undefined, "标注时标上单位标签").helpTip = "选中时,将标签旁边插入\n输出尺寸.\n例: 220 mm";
units.value = defaultUnits;
if (selectedItems == 2) {
between = optionsPanel.add("checkbox", undefined, "对象间隔间尺寸").helpTip = "选中时,对象间隔的距离\n所选尺寸的2个对象.";
between.value = false;
}
fontGroup = optionsPanel.add("group");
fontGroup.orientation = "row";
fontLabel = fontGroup.add("statictext", undefined, "字体大小:");
fontSizeInput = fontGroup.add("edittext", undefined, defaultFontSize).helpTip = "输入尺寸标签所需的字体大小.\n默认: " setFontSize;
fontUnitsLabelText = "";
switch (doc.rulerUnits) {
case RulerUnits.Picas:
fontUnitsLabelText = "pc";
break;
case RulerUnits.Inches:
fontUnitsLabelText = "in";
break;
case RulerUnits.Millimeters:
fontUnitsLabelText = "mm";
break;
case RulerUnits.Centimeters:
fontUnitsLabelText = "cm";
break;
case RulerUnits.Pixels:
fontUnitsLabelText = "px";
break;
default:
fontUnitsLabelText = "pt";
}
fontUnitsLabel = fontGroup.add("statictext", undefined, fontUnitsLabelText);
fontSizeInput.characters = 5;
fontSizeInput.onActivate = function() {
restoreDefaultsButton.enabled = true;
};
colorGroup = optionsPanel.add("group");
colorGroup.orientation = "row";
colorLabel = colorGroup.add("statictext", undefined, "标签颜色 (RGB):");
colorInputRed = colorGroup.add("edittext", undefined, defaultColorRed).helpTip = "输入要用于尺寸标签的RGB红色值.\n默认: " setRed;
colorInputRed.characters = 3;
colorInputGreen = colorGroup.add("edittext", undefined, defaultColorGreen).helpTip = "输入用于尺寸标签的RGB绿色颜色值.\n默认: " setGreen;
colorInputGreen.characters = 3;
colorInputBlue = colorGroup.add("edittext", undefined, defaultColorBlue).helpTip = "输入要用于尺寸标签的RGB蓝色颜色值.\n默认: " setBlue;
colorInputBlue.characters = 3;
colorInputRed.onActivate = function() {
restoreDefaultsButton.enabled = true;
};
colorInputGreen.onActivate = function() {
restoreDefaultsButton.enabled = true;
};
colorInputBlue.onActivate = function() {
restoreDefaultsButton.enabled = true;
};
decimalPlacesGroup = optionsPanel.add("group");
decimalPlacesGroup.orientation = "row";
decimalPlacesLabel = decimalPlacesGroup.add("statictext", undefined, "显示小数点:");
decimalPlacesInput = decimalPlacesGroup.add("edittext", undefined, defaultDecimals).helpTip = "输入所需的数量的小数点后\n显示在标签的尺寸.\n默认: " setDecimals;
decimalPlacesInput.characters = 4;
decimalPlacesInput.onActivate = function() {
restoreDefaultsButton.enabled = true;
};
infoText = optionsPanel.add("statictext", undefined, "尺寸标注");
infoText.margins = 0;
infoText.enabled = false;
infoText = optionsPanel.add("statictext", undefined, "广州知了软件有限公司");
infoText.margins = 0;
infoText.enabled = false;
infoText = optionsPanel.add("statictext", undefined, "18928899728");
infoText.margins = 0;
infoText.enabled = false;
restoreDefaultsButton = optionsPanel.add("button", undefined, "恢复默认值");
restoreDefaultsButton.alignment = "left";
restoreDefaultsButton.enabled = setFontSize != defaultFontSize || setRed != defaultColorRed || setGreen != defaultColorGreen || setBlue != defaultColorBlue || setDecimals != defaultDecimals ? true : false;
restoreDefaultsButton.onClick = function() {
restoreDefaults();
};
function restoreDefaults() {
units.value = setUnits;
fontSizeInput.text = setFontSize;
colorInputRed.text = setRed;
colorInputGreen.text = setGreen;
colorInputBlue.text = setBlue;
decimalPlacesInput.text = setDecimals;
restoreDefaultsButton.enabled = false;
$.setenv("Specify_defaultUnits", "");
$.setenv("Specify_defaultFontSize", "");
$.setenv("Specify_defaultColorRed", "");
$.setenv("Specify_defaultColorGreen", "");
$.setenv("Specify_defaultColorBlue", "");
$.setenv("Specify_defaultDecimals", "");
}
buttonGroup = specifyDialogBox.add("group");
buttonGroup.orientation = "row";
buttonGroup.alignment = "right";
buttonGroup.margins = [0, 0, 0, 0];
cancelButton = buttonGroup.add("button", undefined, "关闭");
cancelButton.size = [50, 30];
cancelButton.onClick = function() {
specifyDialogBox.close();
};
specifyButton = buttonGroup.add("button", undefined, "标注对象");
specifyButton.size = [160, 30];
specifyDialogBox.defaultElement = specifyButton;
specifyButton.onClick = function() {
startSpec();
};
try {
var specsLayer = doc.layers.SPECS;
} catch (err) {
var specsLayer = doc.layers.add();
specsLayer.name = "标注";
}
var color = new RGBColor();
var gap = 4;
var size = 6;
function startSpec() {
var objectsToSpec = new Array();
for (var index = doc.selection.length - 1; index >= 0; index--) {
objectsToSpec[index] = doc.selection[index];
}
var top = topCheckbox.value;
var left = leftCheckbox.value;
var right = rightCheckbox.value;
var bottom = bottomCheckbox.value;
fontSizeInput.active = false;
var validFontSize = /^[0-9]{1,3}(\.[0-9]{1,3})?$/.test(fontSizeInput.text);
var validRedColor = /^[0-9]{1,3}$/.test(colorInputRed.text);
var validGreenColor = /^[0-9]{1,3}$/.test(colorInputGreen.text);
var validBlueColor = /^[0-9]{1,3}$/.test(colorInputBlue.text);
if (validRedColor && validGreenColor && validBlueColor) {
color.red = colorInputRed.text;
color.green = colorInputGreen.text;
color.blue = colorInputBlue.text;
$.setenv("Specify_defaultColorRed", color.red);
$.setenv("Specify_defaultColorGreen", color.green);
$.setenv("Specify_defaultColorBlue", color.blue);
}
var validDecimalPlaces = /^[0-4]{1}$/.test(decimalPlacesInput.text);
if (validDecimalPlaces) {
decimals = decimalPlacesInput.text;
$.setenv("Specify_defaultDecimals", decimals);
}
if (selectedItems < 1) {
beep();
alert("请至少选择1个对象,然后重试.");
specifyDialogBox.close();
} else if (!top && !left && !right && !bottom) {
beep();
alert("请选择至少1边.");
} else if (!validFontSize) {
beep();
alert("请输入有效的字体大小. \n0.002 - 999.999");
fontSizeInput.active = true;
fontSizeInput.text = setFontSize;
} else if (parseFloat(fontSizeInput.text, 10) <= 0.001) {
beep();
alert("字体大小必须大于 0.001.");
fontSizeInput.active = true;
} else if (!validRedColor || !validGreenColor || !validBlueColor) {
beep();
alert("请输入有效的RGB颜色.");
colorInputRed.active = true;
colorInputRed.text = defaultColorRed;
colorInputGreen.text = defaultColorGreen;
colorInputBlue.text = defaultColorBlue;
} else if (!validDecimalPlaces) {
beep();
alert("小数位必须在 0~4之间.");
decimalPlacesInput.active = true;
decimalPlacesInput.text = setDecimals;
} else if (selectedItems == 2 && between.value) {
if (top) {
specDouble(objectsToSpec[0], objectsToSpec[1], "Top")
}
if (left) {
specDouble(objectsToSpec[0], objectsToSpec[1], "Left")
}
if (right) {
specDouble(objectsToSpec[0], objectsToSpec[1], "Right")
}
if (bottom) {
specDouble(objectsToSpec[0], objectsToSpec[1], "Bottom")
}
specifyDialogBox.close();
} else {
for (var objIndex = objectsToSpec.length - 1; objIndex >= 0; objIndex--) {
if (top) {
specSingle(objectsToSpec[objIndex].geometricBounds, "Top")
}
if (left) {
specSingle(objectsToSpec[objIndex].geometricBounds, "Left")
}
if (right) {
specSingle(objectsToSpec[objIndex].geometricBounds, "Right")
}
if (bottom) {
specSingle(objectsToSpec[objIndex].geometricBounds, "Bottom")
}
}
specifyDialogBox.close();
}
}
function specSingle(bound, where) {
specsLayer.locked = false;
var w = bound[2] - bound[0];
var h = bound[1] - bound[3];
var a = bound[0];
var b = bound[2];
var c = bound[1];
var xy = "x";
var dir = 1;
switch (where) {
case "Top":
a = bound[0];
b = bound[2];
c = bound[1];
xy = "x";
dir = 1;
break;
case "Right":
a = bound[1];
b = bound[3];
c = bound[2];
xy = "y";
dir = 1;
break;
case "Bottom":
a = bound[0];
b = bound[2];
c = bound[3];
xy = "x";
dir = -1;
break;
case "Left":
a = bound[1];
b = bound[3];
c = bound[0];
xy = "y";
dir = -1;
break;
}
var lines = new Array();
if (xy == "x") {
lines[0] = new Array(new Array(a, c (gap * dir)));
lines[0].push(new Array(a, c ((gap size) * dir)));
lines[1] = new Array(new Array(b, c (gap * dir)));
lines[1].push(new Array(b, c ((gap size) * dir)));
lines[2] = new Array(new Array(a, c ((gap (size / 2)) * dir)));
lines[2].push(new Array(b, c ((gap (size / 2)) * dir)));
if (where == "Top") {
var t = specLabel(w, (a b) / 2, lines[0][1][1], color);
t.top = t.height;
} else {
var t = specLabel(w, (a b) / 2, lines[0][0][1], color);
t.top -= size;
}
t.left -= (t.width / 2);
} else {
lines[0] = new Array(new Array(c (gap * dir), a));
lines[0].push(new Array(c ((gap size) * dir), a));
lines[1] = new Array(new Array(c (gap * dir), b));
lines[1].push(new Array(c ((gap size) * dir), b));
lines[2] = new Array(new Array(c ((gap (size / 2)) * dir), a));
lines[2].push(new Array(c ((gap (size / 2)) * dir), b));
if (where == "Left") {
var t = specLabel(h, lines[0][1][0], (a b) / 2, color);
t.left -= t.width;
t.rotate(90, true, false, false, false, Transformation.BOTTOMRIGHT);
t.top = t.width;
t.top = (t.height / 2);
} else {
var t = specLabel(h, lines[0][1][0], (a b) / 2, color);
t.rotate(-90, true, false, false, false, Transformation.BOTTOMLEFT);
t.top = t.width;
t.top = (t.height / 2);
}
}
var specgroup = new Array(t);
for (var i = 0; i < lines.length; i = 1) {
var p = doc.pathItems.add();
p.setEntirePath(lines[i]);
p.strokeDashes = [];
setLineStyle(p, color);
specgroup.push(p);
}
group(specsLayer, specgroup);
specsLayer.locked = true;
}
function specDouble(item1, item2, where) {
var bound = new Array(0, 0, 0, 0);
var a = item1.geometricBounds;
var b = item2.geometricBounds;
if (where == "Top" || where == "Bottom") {
if (b[0] > a[0]) {
if (b[0] > a[2]) {
bound[0] = a[2];
bound[2] = b[0];
} else {
bound[0] = b[0];
bound[2] = a[2];
}
} else {
if (a[0] >= b[0]) {
if (a[0] > b[2]) {
bound[0] = b[2];
bound[2] = a[0];
} else {
bound[0] = a[0];
bound[2] = b[2];
}
}
}
bound[1] = Math.max(a[1], b[1]);
bound[3] = Math.min(a[3], b[3]);
} else {
if (b[3] > a[3]) {
if (b[3] > a[1]) {
bound[3] = a[1];
bound[1] = b[3];
} else {
bound[3] = b[3];
bound[1] = a[1];
}
} else {
if (a[3] >= b[3]) {
if (a[3] > b[1]) {
bound[3] = b[1];
bound[1] = a[3];
} else {
bound[3] = a[3];
bound[1] = b[1];
}
}
}
bound[0] = Math.min(a[0], b[0]);
bound[2] = Math.max(a[2], b[2]);
}
specSingle(bound, where);
}
function specLabel(val, x, y, color) {
var t = doc.textFrames.add();
if (parseFloat(fontSizeInput.text) > 0) {
labelFontSize = parseFloat(fontSizeInput.text);
} else {
labelFontSize = defaultFontSize;
}
var labelFontInUnits = convertToPoints(labelFontSize);
$.setenv("Specify_defaultFontSize", labelFontInUnits);
t.textRange.characterAttributes.size = labelFontInUnits;
t.textRange.characterAttributes.alignment = StyleRunAlignmentType.center;
t.textRange.characterAttributes.fillColor = color;
var displayUnitsLabel = units.value;
$.setenv("Specify_defaultUnits", displayUnitsLabel);
var v = val;
var unitsLabel = "";
switch (doc.rulerUnits) {
case RulerUnits.Picas:
v = new UnitValue(v, "pt").as("pc");
var vd = v - Math.floor(v);
vd = 12 * vd;
v = Math.floor(v) "p" vd.toFixed(decimals);
break;
case RulerUnits.Inches:
v = new UnitValue(v, "pt").as("in");
v = v.toFixed(decimals);
unitsLabel = " in";
break;
case RulerUnits.Millimeters:
v = new UnitValue(v, "pt").as("mm");
v = v.toFixed(decimals);
unitsLabel = " mm";
break;
case RulerUnits.Centimeters:
v = new UnitValue(v, "pt").as("cm");
v = v.toFixed(decimals);
unitsLabel = " cm";
break;
case RulerUnits.Pixels:
v = new UnitValue(v, "pt").as("px");
v = v.toFixed(decimals);
unitsLabel = " px";
break;
default:
v = new UnitValue(v, "pt").as("pt");
v = v.toFixed(decimals);
unitsLabel = " pt";
}
if (displayUnitsLabel) {
t.contents = v unitsLabel;
} else {
t.contents = v;
}
t.top = y;
t.left = x;
return t;
}
function convertToBoolean(string) {
switch (string.toLowerCase()) {
case "true":
return true;
break;
case "false":
return false;
break;
}
}
function setLineStyle(path, color) {
path.filled = false;
path.stroked = true;
path.strokeColor = color;
path.strokeWidth = 0.5;
return path;
}
function group(layer, items, isDuplicate) {
var gg = layer.groupItems.add();
for (var i = items.length - 1; i >= 0; i--) {
if (items[i] != gg) {
if (isDuplicate) {
newItem = items[i].duplicate(gg, ElementPlacement.PLACEATBEGINNING);
} else {
items[i].move(gg, ElementPlacement.PLACEATBEGINNING);
}
}
}
return gg;
}
function convertToPoints(value) {
switch (doc.rulerUnits) {
case RulerUnits.Picas:
value = new UnitValue(value, "pc").as("pt");
break;
case RulerUnits.Inches:
value = new UnitValue(value, "in").as("pt");
break;
case RulerUnits.Millimeters:
value = new UnitValue(value, "mm").as("pt");
break;
case RulerUnits.Centimeters:
value = new UnitValue(value, "cm").as("pt");
break;
case RulerUnits.Pixels:
value = new UnitValue(value, "px").as("pt");
break;
default:
value = new UnitValue(value, "pt").as("pt");
}
return value;
}
function convertToUnits(value) {
switch (doc.rulerUnits) {
case RulerUnits.Picas:
value = new UnitValue(value, "pt").as("pc");
break;
case RulerUnits.Inches:
value = new UnitValue(value, "pt").as("in");
break;
case RulerUnits.Millimeters:
value = new UnitValue(value, "pt").as("mm");
break;
case RulerUnits.Centimeters:
value = new UnitValue(value, "pt").as("cm");
break;
case RulerUnits.Pixels:
value = new UnitValue(value, "pt").as("px");
break;
default:
value = new UnitValue(value, "pt").as("pt");
}
return value;
}
switch (selectedItems) {
case 0:
beep();
alert("请至少选择1个对象,然后重试.");
break;
default:
specifyDialogBox.show();
break;
}
} else {
alert("没有要指定的对象. \n请打开文档继续.");
}
2.作者答疑
如有疑问,请留言。
提示: 作者知了-联系方式1
提示: 作者知了-联系方式2