-
Notifications
You must be signed in to change notification settings - Fork 114
/
SubtractTopPath.jsx
269 lines (222 loc) · 6.73 KB
/
SubtractTopPath.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
/*
SubtractTopPath.jsx for Adobe Illustrator
Description: Subtracts the top path from all those below it
Date: April, 2022
Author: Sergey Osokin, email: hi@sergosokin.ru
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1 Initial version
0.2 Added subtraction from stroked paths
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
// Main function
function main() {
var CFG = {
isRmvTop: true, // Remove the top item when finished
isUseFS: false // Switch to full screen. Set it to true if script is slow
};
if (!documents.length) {
alert('Error\nOpen a document and try again');
return;
}
if (!selection.length || selection.typename === 'TextRange') {
alert('Error\nPlease, select two or more paths');
return;
}
var doc = activeDocument,
docView = doc.views[0].screenMode,
cutter = selection[0];
cutter.selected = false;
var paths = getPaths(selection);
if (CFG.isUseFS && paths.length > 10)
doc.views[0].screenMode = ScreenMode.FULLSCREEN;
for (var i = paths.length - 1; i >= 0; i--) {
process(cutter, paths[i]);
}
if (CFG.isRmvTop) cutter.remove();
selection = null;
redraw();
doc.views[0].screenMode = docView;
}
/**
* Get single paths
* @param {(Object|Array)} collection - Set of items
* @return {Array} out - Single paths
*/
function getPaths(collection) {
var out = [];
for (var i = 0; i < collection.length; i++) {
var item = collection[i];
if (item.pageItems && item.pageItems.length) {
out = [].concat(out, getPaths(item.pageItems));
} else if (/compound/i.test(item.typename) && item.pathItems && !item.pathItems[0].clipping) {
out.push(item);
} else if (/pathitem/i.test(item.typename) && !item.clipping) {
out.push(item);
}
}
return out;
}
/**
* Process the bottom item
* @param {Object} a - top item
* @param {Object} b - bottom item
*/
function process(a, b) {
var tmp = (/compound/i.test(b.typename)) ? b.pathItems[0] : b;
if (tmp.filled && tmp.stroked && !tmp.closed)
return; // Not work with filled open paths
selection = null;
var cutter = a.duplicate();
cutter.move(b, ElementPlacement.PLACEBEFORE);
b.selected = true;
cutter.selected = true;
redraw();
if (tmp.stroked && !tmp.closed) {
if (isOverlap(cutter, b)) subtractLine();
else cutter.remove();
} else {
subtractShape();
removeContained(selection);
}
}
/**
* Test the overlapping with the line
* @param {Object} a - Top item
* @param {Object} b - Bottom item
* @return {boolean}
*/
function isOverlap(a, b) {
var dupA = a.duplicate(),
dupB = b.duplicate();
selection = null;
dupA.selected = true;
dupB.selected = true;
app.executeMenuCommand('group');
app.executeMenuCommand('Live Pathfinder Divide');
app.executeMenuCommand('expandStyle');
app.executeMenuCommand('ungroup');
var len = selection.length;
for (var i = selection.length - 1; i >= 0; i--) {
selection[i].remove();
}
a.selected = true;
b.selected = true;
return len > 1;
}
// Subtract the top selected shape from the open path
function subtractLine() {
app.executeMenuCommand('Make Planet X');
app.executeMenuCommand('Expand Planet X');
if (selection[0].pageItems.length === 1) app.executeMenuCommand('ungroup');
selection[0].groupItems[selection[0].groupItems.length - 1].remove();
app.executeMenuCommand('ungroup');
}
// Subtract the top selected shape from the bottom shape
function subtractShape() {
app.executeMenuCommand('group');
app.executeMenuCommand('Live Pathfinder Subtract');
app.executeMenuCommand('expandStyle');
app.executeMenuCommand('ungroup');
}
/**
* Remove item overlapped by the top item
* @param {(Object|Array)} collection - Set of items
*/
function removeContained(collection) {
if (collection.length === 1) return;
var path0 = collection[0],
path1 = collection[1],
type0 = path0.typename,
type1 = path1.typename;
if (/compound/i.test(type0)) path0 = path0.pathItems[0];
if (/compound/i.test(type1)) path1 = path1.pathItems[0];
try {
if (/group/i.test(type0) || /group/i.test(type1) ||
!isEqualColor(path0.fillColor, path1.fillColor)) {
for (var i = collection.length - 1; i >= 0; i--) {
collection[i].remove();
}
}
} catch (e) {}
}
/**
* Compare the colors of two items
* @param {Object} a - Color of the top item
* @param {Object} b - Color of the bottom item
* @return {boolean}
*/
function isEqualColor(a, b) {
if (a.typename !== b.typename) return false;
if (a == '[NoColor]' && b == '[NoColor]') return true;
var colArrA = (a.typename == 'PatternColor') ? [a.pattern] : getColorValues(a);
var colArrB = (b.typename == 'PatternColor') ? [b.pattern] : getColorValues(b);
if (isEqualArr(colArrA, colArrB)) return true;
return false;
}
/**
* Get an array of all color values
* @param {Object} color
* @return {Array} out - Array of color channel values
*/
function getColorValues(color) {
var out = [];
if (color.typename) {
switch (color.typename) {
case 'CMYKColor':
out.push(color.cyan, color.magenta, color.yellow, color.black);
break;
case 'RGBColor':
out.push(color.red, color.green, color.blue);
break;
case 'GrayColor':
out.push(color.gray, color.gray, color.gray);
break;
case 'LabColor':
out.push(color.a, color.b, color.l);
break;
case 'SpotColor':
out = [].concat(out, getColorValues(color.spot.color));
break;
case 'GradientColor':
for (var i = 0; i < color.gradient.gradientStops.length; i++) {
out = [].concat(out, getColorValues(color.gradient.gradientStops[i].color));
}
break;
}
}
return out;
}
/**
* Compare arrays
* @param {Array} a
* @param {Array} b
* @return {boolean}
*/
function isEqualArr(a, b) {
if (a.length !== 0 && a.length === b.length) {
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
return false;
}
// Run script
try {
main();
} catch (e) {}