-
Notifications
You must be signed in to change notification settings - Fork 63
/
notches.jsx
429 lines (370 loc) · 13.3 KB
/
notches.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
// notches.jsx
// draws sewing notches along the selected segments.
// USAGE: select the segments of paths and run this script.
// test env: Adobe Illustrator CC2014 (Win/Mac)
// Copyright(c) 2014 Hiroyuki Sato
// https://github.com/shspage
// This script is distributed under the MIT License.
// See the LICENSE file for details.
// Mon, 22 Sep 2014 23:53:27 +0900
// Sat, 26 Nov 2016 19:13:07 +0900
// -- modify not to activate textedit (fix a problem which forces an extra click after closing the dialog)
// 2018.07.20, modified to ignore locked/hidden objects in a selected group
function main(){
var script_name = "Notches";
if(documents.length < 1) return;
var paths = [];
getPathItemsInSelection(1, paths);
if(paths.length < 1){
alert( script_name + ": select path(s) before run this script");
return;
}
// dialog
var win = new Window("dialog", script_name);
win.alignChildren = "right";
var edits = {
div_count : new EditTextWithLabel(win, "div count:", 4),
notch_length : new EditTextWithLabel(win, "notch length(mm):", 10)
};
//edits.div_count.activate();
addOkCancelButtons(win, (function(){
var opts = {
div_count : parseFloat( edits.div_count.getValue() ),
notch_length : mm2pt(parseFloat( edits.notch_length.getValue() ))
};
var ok = true;
if(opts.div_count <= 1 ){
alert("The value for the div count is invalid");
ok = false;
} else if(opts.notch_length <= 0 ){
alert("The value for the notch length is invalid");
ok = false;
} else {
drawNotches(paths, opts);
}
return ok;
}));
win.show();
}
// -----------------------------------------------
// ScriptUI utility
var EditTextWithLabel = function(win, label, defaultvalue){
var gr = win.add("group");
gr.add("statictext", undefined, label);
this.et = gr.add("edittext", undefined, defaultvalue);
this.et.characters = 4;
//this.et.active = true;
}
EditTextWithLabel.prototype = {
getValue : function(){
var v = parseFloat(this.et.text);
return isNaN(v) ? 0 : v;
},
activate : function(){
this.et.active = true;
}
}
// -----------------------------------------------
// ScriptUI utility
function addOkCancelButtons(win, func){
var gr = win.add("group");
var btn_ok = gr.add("button", undefined, "OK");
var btn_cancel = gr.add("button", undefined, "Cancel");
btn_ok.onClick = function(){
if( func() ) win.close();
};
}
// -----------------------------------------------
// core function
function drawNotches( paths, opts ){
for( var sIdx = 0, sEnd = paths.length; sIdx < sEnd; sIdx++){
var path = paths[sIdx];
var pts = path.pathPoints;
// curves is a set of arrays of a series of Curve instances.
// curves = [ [ new Curve(pts, 0, 1), new Curve(pts, 1, 2) ],
// [ new Curve(pts, 3, 4), ...
var curves = [];
var cv; // for Curve instance
// searches selected segments
// and creates sets of Curve instances for them
for( var pIdx = 0, pEnd = pts.length; pIdx < pEnd; pIdx++){
var pNext = parseIdx( pts, pIdx + 1 );
if( pNext < 0 ) break;
if( isSegmentSelected(pts, pIdx)){
cv = new Curve( path, pIdx, pNext );
if( curves.length < 1 ){
curves.push([ cv ]);
} else {
var r = curves[ curves.length - 1 ];
if( r[ r.length - 1 ].idx2 == pIdx ){
r.push( cv );
} else {
curves.push([ cv ]);
}
}
}
}
if( curves.length < 1 ) continue;
// If the selected segments stride the first anchor,
// It needs to concat the first and the last set of Curves.
if( path.closed && curves.length > 1 ){
var rFirst = curves[0];
var rLast = curves[ curves.length - 1 ];
if( rFirst[0].idx1 == 0
&& rLast[ rLast.length - 1 ].idx2 == 0){
curves[0] = curves.pop().concat(curves[0]);
}
}
// draws notches for each of the set of Curve.
for( var cIdx = 0, cEnd = curves.length; cIdx < cEnd; cIdx++ ){
var cvs = curves[ cIdx ];
// defines the length between notches
var d = getCurvesLength( cvs ) / opts.div_count;
var spec = { ts:[], d:d, ini_d:d, count:opts.div_count - 1 };
// every set of notches are put into a group.
var grp = path.layer.groupItems.add();
for( var cvIdx = 0, cvEnd = cvs.length; cvIdx < cvEnd; cvIdx++){
cv = cvs[ cvIdx ];
cv.getEquallySpacedParameters( spec );
cv.drawNotches( spec, opts.notch_length, grp );
if( spec.count < 1 ) break;
spec.ts = [];
}
}
}
}
// -----------------------------------------------
function getCurvesLength( cvs ){
var len = 0;
for(var i = 0, iEnd = cvs.length; i < iEnd; i++){
len += cvs[i].getLength(1);
}
return len;
}
// -----------------------------------------------
function isSegmentSelected(pts, pIdx){
var s = pts[pIdx].selected;
return ! (s == PathPointSelection.NOSELECTION
|| s == PathPointSelection.LEFTDIRECTION);
}
// -----------------------------------------------
var Point = function(){
this.x = 0;
this.y = 0;
}
Point.prototype = {
set : function(x, y){
this.x = x;
this.y = y;
return this;
},
setr : function(xy){ // set with an array
this.x = xy[0];
this.y = xy[1];
return this;
},
setp : function(p){ // set with a Point
this.x = p.x;
this.y = p.y;
return this;
},
addp : function(p){
return new Point().set( this.x + p.x, this.y + p.y );
},
subp : function(p){
return new Point().set( this.x - p.x, this.y - p.y );
},
mul : function(m){
return new Point().set( this.x * m, this.y * m );
},
rotate : function(rad){
var s = Math.sin(rad);
var c = Math.cos(rad);
return new Point().set( this.x * c - this.y * s, this.x * s + this.y * c );
},
getAngle : function(){
return Math.atan2( this.y, this.x ); // radian
},
normalize : function(){
var d = Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
var p = new Point();
if( d == 0 ){
p.set(0,0);
} else {
p.set(this.x / d, this.y / d);
}
return p;
},
toArray : function(){
return [this.x, this.y];
}
}
// -----------------------------------------------
var Curve = function(path, idx1, idx2){
var pts = path.pathPoints;
this.path = path;
this.idx1 = idx1;
this.idx2 = idx2;
this.p1 = new Point().setr(pts[idx1].anchor);
this.rdir = new Point().setr(pts[idx1].rightDirection);
this.ldir = new Point().setr(pts[idx2].leftDirection);
this.p2 = new Point().setr(pts[idx2].anchor);
this.q = [this.p1, this.rdir, this.ldir, this.p2];
this.params = null;
this.dm = null;
this.dn = null;
this.length = null;
}
Curve.prototype = {
bezier : function(t){
var u = 1 - t;
return new Point().set(
u*u*u * this.p1.x + 3*u*t*(u* this.rdir.x + t* this.ldir.x) + t*t*t * this.p2.x,
u*u*u * this.p1.y + 3*u*t*(u* this.rdir.y + t* this.ldir.y) + t*t*t * this.p2.y);
},
setParams : function(){
var m = [this.p2.x - this.p1.x + 3 * (this.rdir.x - this.ldir.x),
this.p1.x - 2 * this.rdir.x + this.ldir.x,
this.rdir.x - this.p1.x];
var n = [this.p2.y - this.p1.y + 3 * (this.rdir.y - this.ldir.y),
this.p1.y - 2 * this.rdir.y + this.ldir.y,
this.rdir.y - this.p1.y];
this.params = [ m[0] * m[0] + n[0] * n[0],
4 * (m[0] * m[1] + n[0] * n[1]),
2 * ((m[0] * m[2] + n[0] * n[2]) + 2 * (m[1] * m[1] + n[1] * n[1])),
4 * (m[1] * m[2] + n[1] * n[2]),
m[2] * m[2] + n[2] * n[2]];
this.dm = [m[0], 2 * m[1], m[2]];
this.dn = [n[0], 2 * n[1], n[2]];
},
getLength : function(t){
if( !this.params ) this.setParams();
var k = this.params;
var h = t / 128;
var hh = h * 2;
var fc = function(t, k){
return Math.sqrt(t * (t * (t * (t * k[0] + k[1]) + k[2]) + k[3]) + k[4]) || 0 };
var total = (fc(0, k) - fc(t, k)) / 2;
for(var i = h; i < t; i += hh){
total += 2 * fc(i, k) + fc(i + h, k);
}
return total * hh;
},
getTforLength : function(len){
//if( !this.params ) this.setParams();
var k = this.params;
//if( !this.length) this.length = this.getLength(1);
if(len <= 0){
return 0;
} else if(len > this.length){
return -1;
}
var t, d;
var t0 = 0;
var t1 = 1;
var torelance = 0.001;
for(var h = 1; h < 30; h++){
t = t0 + (t1 - t0) / 2;
d = len - this.getLength(t);
if(Math.abs(d) < torelance) break;
else if(d < 0) t1 = t;
else t0 = t;
}
return Math.min(1, t);
},
getEquallySpacedParameters : function( spec ){
// spec = { ts:[], d:d, ini_d:0 }
if( !this.params ) this.setParams();
if( !this.length ) this.length = this.getLength(1);
var total_d = spec.ini_d;
var t;
if( total_d < this.length ){
while( 1 ){
t = this.getTforLength( total_d );
if( t < 0 ) break;
spec.ts.push( t );
total_d += spec.d;
}
spec.ini_d = total_d - this.length;
} else {
spec.ini_d -= this.length;
}
},
drawNotches : function(spec, notch_length, grp){
var ts = spec.ts;
if(ts.length < 1) return;
//if( !this.params ) this.setParams();
var hpi = Math.PI / 2;
for(var ti = 0, tiEnd = ts.length; ti < tiEnd; ti++){
var t = ts[ti];
var dx = t * (t * this.dm[0] + this.dm[1]) + this.dm[2];
var dy = t * (t * this.dn[0] + this.dn[1]) + this.dn[2];
var pnt = this.bezier(t);
var rad = Math.atan2( dy, dx ) + hpi;
var xoffset = notch_length * Math.cos(rad);
var yoffset = notch_length * Math.sin(rad);
var p1 = [pnt.x + xoffset, pnt.y + yoffset];
var p2 = [pnt.x - xoffset, pnt.y - yoffset];
var line = this.path.duplicate();
line.closed = false;
line.filled = false;
line.stroked = true;
line.setEntirePath([p1, pnt.toArray(), p2]);
line.move(grp, ElementPlacement.PLACEATEND);
spec.count--;
if( spec.count < 1 ) break;
}
}
}
// ------------------------------------------------
// convert millimeter to PostScript point
function mm2pt(n){
return n * 2.83464567;
}
// ----------------------------------------------
// return the index of pathpoint. when the argument is out of bounds,
// fixes it if the path is closed (ex. next of last index is 0),
// or return -1 if the path is not closed.
function parseIdx(p, n){ // PathPoints, number for index
var len = p.length;
if( p.parent.closed ){
return n >= 0 ? n % len : len - Math.abs(n % len);
} else {
return (n < 0 || n > len - 1) ? -1 : n;
}
}
// ------------------------------------------------
// extract PathItems from the selection which length of PathPoints
// is greater than "n"
function getPathItemsInSelection(n, paths){
if(documents.length < 1) return;
var s = activeDocument.selection;
if (!(s instanceof Array) || s.length < 1) return;
extractPaths(s, n, paths);
}
// --------------------------------------
// 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);
}
}
}
main();