-
Notifications
You must be signed in to change notification settings - Fork 63
/
softgel.jsx
433 lines (370 loc) · 13 KB
/
softgel.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// softgel
// When you want to create a softgel capsule like shape, this script may
// help you.
// USAGE : Draw circles and select them, then run this script. Adjust
// options in the dialog. then click OK.
// (This script doesn't check whether each path is really a circle.)
// Note : Combining the shapes using Pathfinder may results several
// overlapping anchor points on the path. if it occurs, it may help to
// solve it to use my another script "Merge Overlapped Anchors.js
// (http://shspage.com/aijs/en/#merge)
// This script merges overlapping anchors on the path.
// test env: Adobe Illustrator CC (Win/Mac)
// Copyright(c) 2013 Hiroyuki Sato
// https://github.com/shspage
// This script is distributed under the MIT License.
// See the LICENSE file for details.
// Tue, 07 Jul 2015 20:17:27 +0900
// Sat, 26 Nov 2016 19:15:51 +0900
// -- add try...finally statement around parts changing win.enabled property.
// 2018.07.20, modified to ignore locked/hidden objects in a selected group
main();
function main(){
var conf = {
center_angle : 90,
slider_defaultvalue : 90,
slider_minvalue : 1,
slider_maxvalue : 180,
extra_anchor : "auto",
errmsg : ""
}
if (documents.length<1) return;
var s = activeDocument.selection;
if (!(s instanceof Array) || s.length<1) return;
var sp = [];
extractPaths(s, 1, sp);
if(sp.length < 2) return;
activateEditableLayer(sp[0]);
var preview_paths = [];
var previewed = false;
var clearPreview = function(){
if( previewed ){
try{
undo();
redraw(); // A.J.Hammerschmidt : fixed the problem with undo() crashing.
} catch(e){
alert(e);
} finally {
preview_paths = [];
previewed = false;
}
}
}
var drawPreview = function(){
try{
var shape, j;
for(var i = sp.length - 1; i >= 1; i--){
for(j = i - 1; j >= 0; j--){
shape = softgel(sp[i], sp[j], conf);
if(shape != null) preview_paths.push( shape );
}
}
} finally {
previewed = true;
}
}
// show a dialog
var win = new Window("dialog", "Softgel");
win.orientation = "column";
win.alignChildren = "fill";
win.anglePanel = win.add("panel", [15, 15, 240, 61], "center angle");
win.anglePanel.orientation = "row";
win.anglePanel.angleSlider = win.anglePanel.add("slider", [15, 10, 165, 27],
conf.slider_defaultvalue, conf.slider_minvalue, conf.slider_maxvalue);
win.anglePanel.txtBox = win.anglePanel.add("edittext", [175, 14, 220, 34], 90);
win.anglePanel.txtBox.justify = "right";
win.anglePanel.txtBox.helpTip = "hit TAB to set the input value temporarily";
win.radioPanel = win.add("panel", [15, 76, 215, 122], "extra anchor");
win.radioPanel.orientation = "row";
win.radioPanel.autoRb = win.radioPanel.add("radiobutton", undefined, "auto");
win.radioPanel.alwaysRb = win.radioPanel.add("radiobutton", undefined, "always");
win.radioPanel.neverRb = win.radioPanel.add("radiobutton", undefined, "never");
win.radioPanel.autoRb.value = true;
win.chkGroup = win.add("group");
win.chkGroup.previewChk = win.chkGroup.add("checkbox", undefined, "preview");
win.btnGroup = win.add("group");
win.btnGroup.okBtn = win.btnGroup.add("button", undefined, "OK");
win.btnGroup.cancelBtn = win.btnGroup.add("button", undefined, "Cancel");
var getValues = function(){
conf.center_angle = win.anglePanel.txtBox.text;
if(win.radioPanel.alwaysRb.value){
conf.extra_anchor = "always";
} else if(win.radioPanel.neverRb.value){
conf.extra_anchor = "never";
} else {
conf.extra_anchor = "auto";
}
}
var processPreview = function( is_preview ){
if( ! is_preview || win.chkGroup.previewChk.value){
try{
win.enabled = false;
getValues();
clearPreview();
drawPreview();
if( is_preview ) redraw();
} catch(e){
alert(e);
} finally{
win.enabled = true;
}
}
}
win.anglePanel.txtBox.onChange = function(){
var v = parseFloat(this.text);
if(isNaN(v)){
v = conf.slider_defaultvalue;
} else if(v < conf.slider_minvalue){
v = conf.slider_minvalue;
} else if(v > conf.slider_maxvalue){
v = conf.slider_maxvalue;
}
this.text = v;
win.anglePanel.angleSlider.value = v;
processPreview( true );
}
win.anglePanel.angleSlider.onChanging = function(){
win.anglePanel.txtBox.text = Math.round(this.value);
}
win.anglePanel.angleSlider.onChange = function(){
win.anglePanel.txtBox.text = Math.round(this.value);
processPreview( true );
}
win.chkGroup.previewChk.onClick = function(){
if( this.value ){
processPreview( true );
} else {
if( previewed ){
clearPreview();
redraw();
}
}
}
win.btnGroup.okBtn.onClick = function(){
processPreview( false );
win.close();
}
win.btnGroup.cancelBtn.onClick = function(){
try{
win.enabled = false;
clearPreview();
} catch(e){
alert(e);
} finally{
win.enabled = true;
}
win.close();
}
win.show();
if( previewed ) activeDocument.selection = sp.concat( preview_paths );
// if( previewed ) activeDocument.selection = preview_paths; // works, omits original selection items
if( conf.errmsg != "") alert( conf.errmsg );
}
// ---------------------------------------------
function softgel(s0, s1, conf){
var mpi = Math.PI;
var hpi = mpi/2;
var arr = getGBCenterWidth(s0);
var o1 = arr[0];
var r1 = arr[1] / 2;
arr = getGBCenterWidth(s1);
var o2 = arr[0];
var r2 = arr[1] / 2;
if(r1 == 0 || r2 == 0) return;
var d = dist(o1, o2);
if(d <= Math.abs(r1 - r2)) return;
// if center_angle is 180, simply draw a circle.
if(conf.center_angle == 180){
return (function(){
var d1 = (d + r2 - r1) / d;
var d2 = (d + r1 - r2) / d;
var center = [(o1[0] * d2 + o2[0] * d1) / 2,
(o1[1] * d2 + o2[1] * d1) / 2];
var radius = (d + r1 + r2) / 2;
var circle = activeDocument.activeLayer.pathItems.ellipse(
center[1] + radius,
center[0] - radius,
radius * 2, radius * 2
);
var cp = circle.pathPoints;
var pitem = s0.duplicate();
var p = pitem.pathPoints;
while(p.length < 4) p.add();
while(p.length > 4) p[p.length - 1].remove();
var copyPathPoint = function(p1, p2){
p2.anchor = p1.anchor;
p2.rightDirection = p1.rightDirection;
p2.leftDirection = p1.leftDirection;
p2.pointType = p1.pointType;
};
for(var i = 0; i < 4; i++){
copyPathPoint(cp[i], p[i]);
}
circle.remove();
return pitem;
})();
}
var ot1 = getRad(o1, o2);
var ot2 = ot1 + mpi;
var t = conf.center_angle * mpi / 180; // center_angle as radian
var cost = Math.cos( t );
// r : radius of the arc
var r = equation2_custom( 2 * cost - 2,
(2 - 2 * cost) * (r1 + r2),
2 * cost * r1 * r2 - r1 * r1 - r2 * r2 + d * d );
if(r == null){
conf.errmsg = "there're errors in calcuration of the radius";
return;
}
var a = r - r2;
var b = r - r1;
// calcurates angles using the law of cosines
var t_a = Math.acos((b * b + d * d - a * a) / (2 * b * d));
if(isNaN(t_a)) return;
var t_b = Math.acos((d * d + a * a - b * b) / (2 * d * a));
if(isNaN(t_b)) return;
// adds an extra anchor for each arc, if the central angle of
// the arc is greater than 90 degree.
var add_extra_anc = (t > hpi || conf.extra_anchor == "always");
if( conf.extra_anchor == "never" ) add_extra_anc = false;
if( add_extra_anc ){
t /= 2;
}
// length of the handles for arc
var h = 4 * Math.tan( t / 4 ) / 3 * r;
var shape = s0.duplicate();
with(shape){
var p = pathPoints;
// adjusts the number of the pathPoints
while(p.length < 4) p.add();
while(p.length > 4) p[p.length - 1].remove();
if( add_extra_anc ){
p.add();
p.add();
}
var idx = 0;
with(p[idx]){
anchor = setPnt(o2, ot1 + t_b, r2);
leftDirection = anchor;
rightDirection = setPnt(anchor, ot1 + t_b + hpi, h);
}
idx += 1;
if( add_extra_anc ){
with(p[idx]){
anchor = setPnt(setPnt(o1, ot1 - t_a, r - r1), ot1 + t_b + t, r);
rightDirection = setPnt(anchor, ot1 + t_b + t + hpi, h);
leftDirection = setPnt(anchor, ot1 + t_b + t - hpi, h);
}
idx += 1;
}
with(p[idx]){
anchor = setPnt(o1, ot2 - t_a, r1);
leftDirection = setPnt(anchor, ot2 - t_a - hpi, h);
rightDirection = anchor;
}
idx += 1;
with(p[idx]){
anchor = setPnt(o1, ot2 + t_a, r1);
leftDirection = anchor;
rightDirection =setPnt(anchor, ot2 + t_a + hpi, h);
}
idx += 1;
if( add_extra_anc ){
with(p[idx]){
anchor = setPnt(setPnt(o1, ot1 + t_a, r - r1), ot1 - t_b - t, r);
rightDirection = setPnt(anchor, ot1 - t_b - t + hpi, h);
leftDirection =setPnt(anchor, ot1 - t_b - t - hpi, h);
}
idx += 1;
}
with(p[idx]){
anchor = setPnt(o2, ot1 - t_b, r2);
leftDirection = setPnt(anchor, ot1 - t_b - hpi, h);
rightDirection = anchor;
}
}
return shape;
}
// ------------------------------------------------
function getGBCenterWidth(pi){
var gb = pi.geometricBounds; // left, top, right, bottom
return [[(gb[0] + gb[2]) / 2, (gb[1] + gb[3]) / 2], gb[2] - gb[0]];
}
// ------------------------------------------------
function setPnt(pnt, rad, dis){
return [pnt[0] + Math.cos(rad) * dis,
pnt[1] + Math.sin(rad) * dis];
}
// ------------------------------------------------
function dist(p1, p2) {
return Math.sqrt(Math.pow(p1[0] - p2[0],2) + Math.pow(p1[1] - p2[1],2));
}
// ------------------------------------------------
function getRad(p1,p2) {
return Math.atan2(p2[1] - p1[1], p2[0] - p1[0]);
}
// --------- -------------------------------------
function equation2_custom(a,b,c) {
var s;
if(a == 0){
if(b == 0){
return null;
} else {
s = -c / b;
return s > 0 ? s : null;
}
}
a *= 2;
var d = b * b - 2 * a * c;
if(d < 0){
return null;
}
var rd = Math.sqrt(d);
if(d > 0){
var s1 = (-b + rd) / a;
var s2 = (-b - rd) / a;
if( s1 > 0 && s2 > 0){
// I'm not sure if it's ok
return Math.max( s1, s2 );
} else if( s1 > 0 ){
return s1;
} else if( s2 > 0 ){
return s2;
} else {
return null;
}
} else {
s = -b / a;
return s > 0 ? s : null;
}
}
// --------------------------------------
// extract PathItems from "s" (Array of PageItems -- ex. selection),
// and put them into an Array "paths". If "pp_length_limit" is specified,
// this function extracts PathItems which PathPoints length is greater
// than this number.
function extractPaths(s, pp_length_limit, paths){
for(var i = 0; i < s.length; i++){
if(s[i].locked || s[i].hidden){
continue;
} else if(s[i].typename == "PathItem"){
if((pp_length_limit && s[i].pathPoints.length <= pp_length_limit)
|| s[i].guides || s[i].clipping){
continue;
}
paths.push(s[i]);
} else if(s[i].typename == "GroupItem"){
// search for PathItems in GroupItem, recursively
extractPaths(s[i].pageItems, pp_length_limit, paths);
} else if(s[i].typename == "CompoundPathItem"){
// searches for pathitems in CompoundPathItem, recursively
// ( ### Grouped PathItems in CompoundPathItem are ignored ### )
extractPaths(s[i].pathItems, pp_length_limit , paths);
}
}
}
// ----------------------------------------------
function activateEditableLayer(pi){
var lay = activeDocument.activeLayer;
if(lay.locked || !lay.visible) activeDocument.activeLayer = pi.layer;
}