Untitled
unknown
plain_text
a year ago
3.6 kB
13
Indexable
// Bounding Box with Dimension Lines and Labels
// Works in Adobe Illustrator
(function() {
if (app.documents.length === 0) {
alert("No document open.");
return;
}
var doc = app.activeDocument;
if (doc.selection.length === 0) {
alert("Please select a group or object first.");
return;
}
var sel = doc.selection[0];
var bounds = sel.visibleBounds; // [left, top, right, bottom]
var left = bounds[0], top = bounds[1], right = bounds[2], bottom = bounds[3];
var width = right - left;
var height = top - bottom;
// --- Create bounding box ---
var rect = doc.pathItems.rectangle(top, left, width, height);
rect.stroked = true;
rect.filled = false;
rect.strokeWidth = 1;
// Use safe black color
var black;
try {
black = doc.swatches["Black"].color;
} catch (e) {
black = new RGBColor();
black.red = black.green = black.blue = 0;
}
rect.strokeColor = black;
rect.zOrder(ZOrderMethod.SENDTOBACK);
// --- Corner points ---
var topLeft = [left, top];
var topRight = [right, top];
var bottomLeft = [left, bottom];
var bottomRight = [right, bottom];
// --- Dimension Lines ---
var offset = 15; // distance from box for dimension lines
// Horizontal line (width)
var hLine = doc.pathItems.add();
hLine.setEntirePath([[left, bottom - offset], [right, bottom - offset]]);
hLine.stroked = true;
hLine.strokeWidth = 0.5;
hLine.strokeColor = black;
hLine.filled = false;
// Vertical line (height)
var vLine = doc.pathItems.add();
vLine.setEntirePath([[left - offset, bottom], [left - offset, top]]);
vLine.stroked = true;
vLine.strokeWidth = 0.5;
vLine.strokeColor = black;
vLine.filled = false;
// --- Arrowheads function ---
function createArrow(point1, point2, size) {
var dx = point2[0] - point1[0];
var dy = point2[1] - point1[1];
var angle = Math.atan2(dy, dx);
var arrow1 = doc.pathItems.add();
var arrow2 = doc.pathItems.add();
var arrowLength = size;
// Left/top side
arrow1.setEntirePath([
point1,
[point1[0] + arrowLength * Math.cos(angle + Math.PI * 3/4),
point1[1] + arrowLength * Math.sin(angle + Math.PI * 3/4)]
]);
arrow1.stroked = true;
arrow1.strokeWidth = 0.5;
arrow1.strokeColor = black;
arrow1.filled = false;
// Right/bottom side
arrow2.setEntirePath([
point2,
[point2[0] + arrowLength * Math.cos(angle - Math.PI * 3/4),
point2[1] + arrowLength * Math.sin(angle - Math.PI * 3/4)]
]);
arrow2.stroked = true;
arrow2.strokeWidth = 0.5;
arrow2.strokeColor = black;
arrow2.filled = false;
}
// Create arrowheads for horizontal and vertical lines
createArrow([left, bottom - offset], [right, bottom - offset], 5);
createArrow([left - offset, bottom], [left - offset, top], 5);
// --- Labels ---
var widthLabel = doc.textFrames.add();
widthLabel.contents = width.toFixed(2) + " px";
widthLabel.position = [left + width / 2, bottom - offset - 10];
widthLabel.textRange.characterAttributes.size = 10;
var heightLabel = doc.textFrames.add();
heightLabel.contents = height.toFixed(2) + " px";
heightLabel.position = [left - offset - 10, bottom + height / 2];
heightLabel.textRange.characterAttributes.size = 10;
heightLabel.rotate(-90);
})();Editor is loading...
Leave a Comment