-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundCoordinatesToValue.jsx
157 lines (130 loc) · 4.59 KB
/
RoundCoordinatesToValue.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
RoundCoordinatesToValue.jsx for Adobe Illustrator
Description: Rounds the coordinates of anchors and/or handles to a specified level of precision.
Author: organizedslop
Release notes:
0.1 Initial version
*/
// Creates dialog window
var dialog = new Window("dialog");
dialog.text = "Round Values";
dialog.preferredSize.width = 200;
dialog.orientation = "column";
dialog.alignChildren = ["center", "top"];
dialog.spacing = 10;
dialog.margins = 16;
// Creates selection panel
var panelS = dialog.add("panel", undefined, undefined, {name: "panelS"});
panelS.text = "Apply to";
panelS.preferredSize.width = 110;
panelS.orientation = "column";
panelS.alignChildren = ["left", "center"];
panelS.spacing = 10;
panelS.margins = 10;
var radiobuttonAll = panelS.add("radiobutton", undefined, undefined, {name: "radiobuttonAll"});
radiobuttonAll.text = "All";
radiobuttonAll.value = true;
var radiobuttonSel = panelS.add("radiobutton", undefined, undefined, {name: "radiobuttonSel"});
radiobuttonSel.text = "Selected";
var divider = panelS.add("panel", undefined, undefined, {name: "divider"});
divider.alignment = "fill";
var checkboxAnchors = panelS.add("checkbox", undefined, undefined, {name: "checkboxAnchors"});
checkboxAnchors.text = "Anchors";
checkboxAnchors.value = true;
var checkboxHandles = panelS.add("checkbox", undefined, undefined, {name: "checkboxHandles"});
checkboxHandles.text = "Handles";
// Creates precision panel
var panelP = dialog.add("panel", undefined, undefined, {name: "panelP"});
panelP.text = "Precision (" + getUnits() + ")";
panelP.preferredSize.width = 110;
panelP.orientation = "column";
panelP.alignChildren = ["center", "center"];
panelP.spacing = 10;
panelP.margins = 10;
var precisionInput = panelP.add('edittext {justify: "center", properties: {name: "precisionInput"}}');
precisionInput.text = "1";
precisionInput.preferredSize.width = 50;
// Creates OK and Cancel buttons
var groupConfirm = dialog.add("group"),
buttonOk = groupConfirm.add("button", undefined, "OK"),
buttonCancel = groupConfirm.add("button", undefined, "Cancel");
buttonOk.onClick = function(){
main();
dialog.close();
};
// Returns abbreviated document units
function getUnits(){
var units = app.activeDocument.XMPString.match(/<stDim:unit>.*<\/stDim:unit>/).toString().slice(12, -13);
var unitAbbreviations = {"Pixels":"px", "Points":"pt", "Picas":"pc", "Inches":"in",
"Feet":"ft", "Yards":"yd", "Millimeters":"mm", "Centimeters":"cm",
"Meters":"m"};
return unitAbbreviations[units];
}
dialog.show();
// =============================================================================
function main() {
if (!documents.length)
return;
var doc = app.activeDocument,
roundTo = precisionInput.text,
selPaths = [],
selPoints = [];
getPaths(doc.pageItems, selPaths);
getPoints(selPaths, selPoints);
for (var i = 0; i < selPoints.length; i++) {
if (checkboxAnchors.value) {
selPoints[i].anchor = round(selPoints[i].anchor, roundTo);
}
if (checkboxHandles.value) {
selPoints[i].leftDirection = round(selPoints[i].leftDirection, roundTo);
selPoints[i].rightDirection = round(selPoints[i].rightDirection, roundTo);
}
}
}
// Rounds coordinates to the given precision
function round(point, roundTo) {
return [Math.round(point[0] / roundTo) * roundTo,
Math.round(point[1] / roundTo) * roundTo];
}
// Gets paths from selection
function getPaths(items, arr) {
for (var i = 0; i < items.length; i++) {
var currItem = items[i];
try {
if (currItem.typename == "GroupItem") {
getPaths(currItem.pageItems, arr);
}
else if (currItem.typename == "PathItem") {
arr.push(currItem);
}
else if (currItem.typename == "CompoundPathItem") {
getPaths(currItem.pathItems, arr);
}
else {
currItem.selected = false;
}
} catch (error){}
}
return arr;
}
// Gets selected points on paths
function getPoints(items, arr) {
for (var i = 0; i < items.length; i++) {
if (items[i].pathPoints.length > 1) {
var points = items[i].pathPoints;
for (var j = 0; j < points.length; j++) {
if (radiobuttonSel.value == true) {
if (isSelected(points[j])) arr.push(points[j]);
} else arr.push(points[j]);
}
}
}
}
// Check if current point is selected
function isSelected(point) {
return point.selected == PathPointSelection.ANCHORPOINT;
}
// =============================================================================
try {
main();
} catch(error) {}