-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathDivideBottomPath.jsx
311 lines (275 loc) · 8 KB
/
DivideBottomPath.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
DivideBottomPath.jsx for Adobe Illustrator
Description: Divide the bottom path by the intersection points with the top paths
Date: February, 2023
Author: Sergey Osokin, email: [email protected]
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1 Initial version
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
NOTICE:
Tested with Adobe Illustrator CC 2019-2025 (Mac/Win).
This script is provided "as is" without warranty of any kind.
Free to use, not for sale
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Check my other scripts: https://github.com/creold
*/
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
function main() {
var isRmvTop = true, // Remove top paths
isRndColor = false; // Random stroke color
if (!isCorrectEnv('version:16', 'selection')) return;
var paths = getPaths(selection);
if (!paths.length) return;
addIntersectPoints();
if (selection.length !== paths.length) {
selection = null;
app.redraw();
alert('The paths problem\n'
+ 'Due to the Illustrator error, resulting paths get split.\n'
+ 'Please try to change or move the paths slightly before run script.', 'Script Error');
app.undo();
return;
}
var last = selection.length - 1, // Bottom shape index
lastPath = selection[last],
arr = get(selection),
i = 0;
arr.splice(last, 1);
var otherPts = getPoints(arr);
var newPts = getNewPoints(lastPath, otherPts);
selectPoints(lastPath, newPts);
divideShape(lastPath);
// Recolor strokes
if (isRndColor) {
var isRGB = activeDocument.documentColorSpace == DocumentColorSpace.RGB;
for (i = selection.length - 1; i >=0; i--) {
if (selection[i].stroked) {
selection[i].strokeColor = generateColor(isRGB);
}
}
}
// Remove top paths
if (isRmvTop) {
for (i = arr.length - 1; i >=0; i--) {
arr[i].remove();
}
}
}
// Check the script environment
function isCorrectEnv() {
var args = ['app', 'document'];
args.push.apply(args, arguments);
for (var i = 0; i < args.length; i++) {
var arg = args[i].toString().toLowerCase();
switch (true) {
case /app/g.test(arg):
if (!/illustrator/i.test(app.name)) {
alert('Wrong application\nRun script from Adobe Illustrator', 'Script error');
return false;
}
break;
case /version/g.test(arg):
var rqdVers = parseFloat(arg.split(':')[1]);
if (parseFloat(app.version) < rqdVers) {
alert('Wrong app version\nSorry, script only works in Illustrator v.' + rqdVers + ' and later', 'Script error');
return false;
}
break;
case /document/g.test(arg):
if (!documents.length) {
alert('No documents\nOpen a document and try again', 'Script error');
return false;
}
break;
case /selection/g.test(arg):
if (!selection.length || selection.typename === 'TextRange') {
alert('Nothing selected\nPlease, select two or more paths', 'Script error');
return false;
}
break;
}
}
return true;
}
// Get paths from selection
function getPaths(coll) {
var out = [];
for (var i = 0; i < coll.length; i++) {
var item = coll[i];
if (isType(item, 'group') && item.pageItems.length) {
out = [].concat(out, getPaths(item.pageItems));
} else if (isType(item, '^path')) {
if (!item.stroked) {
item.stroked = true;
item.strokeWidth = 1;
item.strokeColor = generateColor();
}
if (item.stroked && item.strokeWidth > 0) {
item.filled = false;
out.push(item);
} else {
item.selected = false;
}
} else {
item.selected = false;
}
}
return out;
}
// Check the item typename by short name
function isType(item, type) {
var regexp = new RegExp(type, 'i');
return regexp.test(item.typename);
}
// Convert collection into standard Array
function get(coll) {
var out = [];
for (var i = 0, len = coll.length; i < len; i++) {
out.push(coll[i]);
}
return out;
}
// Get path anchors
function getPoints(coll) {
var out = [];
for (var i = 0, len = coll.length; i < len; i++) {
var item = coll[i];
if (!isType(item, '^path')) continue;
var pp = item.pathPoints;
for (var j = 0, jlen = pp.length; j < jlen; j++) {
out.push(pp[j].anchor);
}
}
return out;
}
// Add intersection points to lines
function addIntersectPoints() {
app.executeMenuCommand('group');
app.executeMenuCommand('Make Planet X');
selection[0].translate(0, 0); // Update view
app.executeMenuCommand('Expand Planet X');
try {
app.executeMenuCommand('ungroup');
app.executeMenuCommand('ungroup');
} catch (err) {}
}
// Remove start and end points
function getNewPoints(item, pts) {
if (!isType(item, '^path')) return;
var len = item.pathPoints.length,
newPP = [],
i = 0;
for (i = 0; i < len; i++) {
var pt = item.pathPoints[i];
if (pts.length && compareCoord(pt.anchor, pts)) {
newPP.push(i); // Add new point index
}
}
return newPP;
}
// Find coordinates match
function compareCoord(p, coord) {
for (var i = 0, len = coord.length; i < len; i++) {
if (p[0].toFixed(2) == coord[i][0].toFixed(2)
&& p[1].toFixed(2) == coord[i][1].toFixed(2)) {
coord.splice(i, 1);
return true;
}
}
return false;
}
function selectPoints(path, pts) {
selection = [];
var idx = 0;
for (var i = 0, len = pts.length; i < len; i++) {
idx = pts[i];
path.pathPoints[idx].selected = PathPointSelection.ANCHORPOINT;
}
}
// Cut At Selected Anchors
// Hiroyuki Sato, https://github.com/shspage
function divideShape(path) {
if (path == undefined) return;
var i = 0,
j = 0,
pp = path.pathPoints,
firstAnchSel = isSelected(pp[0]),
idxs = [[0]],
ary,
ancs;
for (i = 1; i < pp.length; i++) {
idxs[idxs.length - 1].push(i);
if (isSelected(pp[i])) idxs.push([i]);
}
if (idxs.length < 2 && !(firstAnchSel && path.closed)) {
return;
}
// Adjust the array (closed path)
if (path.closed) {
if (firstAnchSel) {
idxs[idxs.length - 1].push(0);
} else {
ary = idxs.shift();
idxs[idxs.length - 1] = idxs[idxs.length - 1].concat(ary);
}
}
// Duplicate the path and apply the data of the array
for (i = 0; i < idxs.length; i++) {
ary = idxs[i];
ancs = [];
for (j = ary.length - 1; j >= 0; j--) {
ancs.unshift(pp[ary[j]].anchor);
}
with(path.duplicate()) {
closed = false;
setEntirePath(ancs);
for(j = pathPoints.length - 1; j >= 0; j--){
with(pathPoints[j]) {
rightDirection = pp[ary[j]].rightDirection;
leftDirection = pp[ary[j]].leftDirection;
pointType = pp[ary[j]].pointType;
}
}
selected = true;
}
}
// Remove the original path
path.remove();
}
// Check the point is selected
function isSelected(p) {
return p.selected == PathPointSelection.ANCHORPOINT;
}
// Add new RGB or CMYK color
function generateColor(isRGB) {
if (!arguments.length || !isRGB) isRGB = true;
var c = isRGB ? new RGBColor() : new CMYKColor();
if (isRGB) {
c.red = Math.min(rndInt(0, 255, 8), 255);
c.green = Math.min(rndInt(0, 255, 8), 255);
c.blue = Math.min(rndInt(0, 255, 8), 255);
} else {
c.cyan = rndInt(0, 100, 5);
c.magenta = rndInt(0, 100, 5);
c.yellow = rndInt(0, 100, 5);
c.black = 0;
}
return c;
}
// Get random integer number in range
function rndInt(min, max, step) {
var rand = min - 0.5 + Math.random() * (max - min + 1)
return Math.round(rand / step) * step;
}
// Run script
try {
main();
} catch (err) {}