forked from topstar210/illustrator-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeCAiScript(v3).js
746 lines (664 loc) · 21.6 KB
/
MakeCAiScript(v3).js
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Setup:
// Define Constant: SegmentLength=1
// Step 1:
// -Verify that selection contains:
// 1) Only closed paths
// 2) All the same color stroke
// 3) No fill
// 4) All the same stroke width
// -If not, display error message, make each path red color if error, return
// Step 2:
// -Move selection into new layer called "CLOSED DIE LINES (WORKING FILE)"
// -Copy selection into new layer called “OPEN DIE LINES (FOR PRINT)”
// -Change stroke color of all paths in layer "CLOSED DIE LINES (WORKING FILE)" to ORANGE
// -Lock and make invisible layer "CLOSED DIE LINES (WORKING FILE)"
// Step 3:
// -Work on EACH path in layer “OPEN DIE LINES (FOR PRINT)”:
// -Find CENTER of path (X & Y coord exactly between left side, right side, top and bottom)
// -Find point on path that is FURTHEST POINT away from center.
// -Break (cut path at anchor point) curve at FURTHEST POINT.
// Step 4
// -create new line segment, connected at Path Start, length of SegmentLength in mm, angled directly away from CENTER.
// -create new line segment, connected at Path END, length of SegmentLength in mm, angled directly away from CENTER.
// Step 5...
// List of colors appears off artboard of every color, strokes and fills..
// Step 6...
// 1) Rotate one segment 5 degrees...
// 2) Then rotate other segment -5 degrees.
// 3) Make sure lines are crossing, not diverging.
// Step 5
// -Display message: "OPEN Die lines layer created. Please check it carefully BEFORE saving."
(function(){
var myDoc = app.activeDocument;
var pathes = [];
// selected pathes
getPathItemsInSelection(1, pathes);
// Step 1:
if (app.documents.length == 0 || app.selection.length < 1){
alert ("Please make sure to have something useful selected"); return;
}
if(checkClosedPath(pathes)) return;
if(checkSameStroke(pathes)) return;
if(checkSameColor(pathes)) return;
// Step 2:
var CLOSED_layer = "CLOSED DIE LINES";
var OPEN_layer = "OPEN DIE LINES";
var closedLayer = myDoc.layers.add();
closedLayer.name = CLOSED_layer;
var openLayer = myDoc.layers.add();
openLayer.name = OPEN_layer;
for(var i = pathes.length - 1; i >= 0; i--){
var op = pathes[i];
op.duplicate(openLayer);
op.strokeColor = makeColor(255, 165, 0);
op.move(closedLayer, ElementPlacement.PLACEATEND);
}
myDoc.layers.getByName(CLOSED_layer).visible = false;
myDoc.layers.getByName(CLOSED_layer).locked = true;
// Step 3:
myDoc.activeLayer = myDoc.layers.getByName(OPEN_layer);
var activeLayer = myDoc.activeLayer;
var s_Paths = activeLayer.pathItems;
// is it in a shape?
var cc_Ary = calcContainableFuncs(s_Paths);
// draw the segment SegmentLength = 1 mm
var tempLayer = myDoc.layers.add();
tempLayer.name = 'sengmemts';
var segment = tempLayer.pathItems.add();
var sp1 = segment.pathPoints.add();
sp1.anchor = sp1.rightDirection = sp1.leftDirection = [0,0];
var sp2 = segment.pathPoints.add();
sp2.anchor = sp2.rightDirection = sp2.leftDirection = [7, 0];
for(var i = s_Paths.length - 1; i >= 0; i--){
var s_op = s_Paths[i];
var b = s_op.geometricBounds
var c_xy = [(b[0] + b[2]) / 2, (b[1] + b[3]) / 2];
var result = furthestSet (s_op, c_xy);
if (result.length == 2)
{
var f_pos = result[1];
/* result[1] is point #1, result[2] = center position #2 */
/* now calculate angle and rotate */
var disty = f_pos[0] - c_xy[0];
var distx = f_pos[1] - c_xy[1];
var angle = -Math.atan2 (disty, distx) - Math.PI/2;
angle = angle*180.0/Math.PI;
if (angle <= -180) angle += 180;
if (angle >= 180) angle -= 180;
var s_path = segment.duplicate();
s_path.translate(f_pos[0], f_pos[1]);
s_path.rotate(angle, true, true, true, true, Transformation.LEFT);
// alert("width:"+c_xy[0]+" height:"+f_pos[0]);
// alert(angle);
// is contained?
var isContained = cc_Ary["cc_" + i]?true:false;
if(isContained){
if(c_xy[0] > f_pos[0] && c_xy[1] > f_pos[1])
s_path.translate(s_path.width*-1, s_path.height*-1);
else if(c_xy[0] > f_pos[0] && c_xy[1] < f_pos[1])
s_path.translate(s_path.width*-1, s_path.height);
else if(c_xy[0] < f_pos[0] && c_xy[1] < f_pos[1])
s_path.translate(s_path.width, s_path.height);
else if(c_xy[0] > f_pos[0] && c_xy[1] == f_pos[1])
s_path.translate(s_path.width*-1, 0);
else if(c_xy[0] == f_pos[0] && c_xy[1] < f_pos[1])
s_path.translate(0, s_path.height);
} else {
if(c_xy[0] <= f_pos[0] && c_xy[1] >= f_pos[1])
s_path.translate(s_path.width*-1, s_path.height);
}
s_path.strokeWidth = s_op.strokeWidth;
s_path.strokeColor = s_op.strokeColor;
// connected at Path Start, angled directly away from CENTER
sg_points = s_path.pathPoints;
overlayPoints(s_op, sg_points, activeLayer, isContained);
s_path.remove();
s_op.remove();
}
}
segment.remove();
tempLayer.remove();
// step 5:
displayColorOfPath(s_Paths, activeLayer);
alert("OPEN Die lines layer created. Please check it carefully BEFORE saving.");
})()
// calculate the relationships of shapes
function calcContainableFuncs(_pathes){
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
var len = o.length >>> 0;
if (len === 0) {
return -1;
}
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
if (n >= len) {
return -1;
}
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (k in o && o[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
};
function foreach (arr, func) {
for (var i = 0; i < arr.length; i++) {
func(arr[i]);
}
}
function marker (xy, container) {
var container = container || doc;
var z = container.pathItems.ellipse(xy[1] + 2, xy[0] - 2, 4, 4);
z.stroked = false;
z.filled = true;
z.fillColor = cclr;
}
function getPathPoints (path) { // collects pathPoints of a path into segment arrays of 2 anchors & 2 controls between them
var arr = [];
for (var i = 0, ln = path.pathPoints.length; i < ln; i++) { // designed to work on closed paths!
if (i < ln - 1) {
var p = path.pathPoints[i], pnext = path.pathPoints[i + 1];
} else {
var p = path.pathPoints[i], pnext = path.pathPoints[0];
}
arr.push([p.anchor, p.rightDirection, pnext.leftDirection, pnext.anchor]);
}
return arr;
}
function getBezierSegment (segArr) { // turns a pathPoint segment (gotten with getPathPoints) into bezier interpolated points
var anch2 = segArr[3], cont2 = segArr[2], cont1 = segArr[1], anch1 = segArr[0];
var resultPts = [];
var a = anch2[0] - 3 * (cont2[0]) + 3 * (cont1[0]) - anch1[0];
var b = 3 * (cont2[0]) - 6 * (cont1[0]) + 3 * anch1[0];
var c = 3 * (cont1[0]) - 3 * (anch1[0]);
var d = anch1[0];
var e = anch2[1] - 3 * (cont2[1]) + 3 * (cont1[1]) - anch1[1];
var f = 3 * (cont2[1]) - 6 * (cont1[1]) + 3 * anch1[1];
var g = 3 * (cont1[1]) - 3 * (anch1[1]);
var h = anch1[1];
var inc = 0.1;
for (var t = 0; t < 1; t += inc) {
resultPts.push([
a * Math.pow(t, 3) + b * Math.pow(t, 2) + (c * t) + d,
e * Math.pow(t, 3) + f * Math.pow(t, 2) + (g * t) + h
]);
}
return resultPts;
}
function getBezierPath (pathPts) { // turns entire series of segments (gotten with getPathPoints) into a complete set of interpolated bezier points
var pathArr = [];
for (var i = 0; i < pathPts.length; i++) {
var thisPtSet = pathPts[i];
var seg = getBezierSegment(thisPtSet);
if (
i > 0 && (seg[0][0] == pathArr[pathArr.length - 1][0] &&
seg[0][1] == pathArr[pathArr.length - 1][1])
) {
seg.splice(0, 1);
}
pathArr = pathArr.concat(seg);
}
return pathArr;
}
function markBezierPoints (seg) {
var grp = doc.groupItems.add();
grp.name = "MyGroup";
for (var i = 0; i < seg.length; i++) {
marker(seg, grp);
}
}
function makePolygon (pts) {
var pth = doc.pathItems.add();
pth.setEntirePath(pts);
pth.filled = false;
pth.stroked = true;
pth.strokeColor = cclr;
pth.strokeWidth = 1;
}
var RayCaster = { // the whole raycasting through polygon algorithm from http://rosettacode.org/wiki/Ray-casting_algorithm
// evaluates a single point to see if it's inside a polygon.
Point : function (x, y) {
this.x = x;
this.y = y;
},
pointInPoly : function (point, poly) {
var index, intersected, pointA, pointB, segment, segments;
segments = (function () {
var _i, _len, _results;
_results = [];
for (index = 0, _len = poly.length; index < _len; index++) {
pointA = poly[index];
pointB = poly[(index + 1) % poly.length];
_results.push([new RayCaster.Point(pointA[0], pointA[1]), new RayCaster.Point(pointB[0], pointB[1])]);
}
return _results;
})();
intersected = (function () {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = segments.length; _i < _len; _i++) {
segment = segments[_i];
if (
RayCaster.rayIntersectsSegment(new RayCaster.Point(point[0], point[1]), segment)
) {
_results.push(segment);
}
}
return _results;
})();
return intersected.length % 2 !== 0;
},
rayIntersectsSegment : function (p, segment) {
var a, b, mAB, mAP, p1, p2, _ref;
p1 = segment[0], p2 = segment[1];
_ref = p1.y < p2.y ? [p1, p2] : [p2, p1], a = _ref[0], b = _ref[1];
if (p.y === b.y || p.y === a.y) {
p.y += Number.MIN_VALUE;
}
if (p.y > b.y || p.y < a.y) {
return false;
} else if (p.x > a.x && p.x > b.x) {
return false;
} else if (p.x < a.x && p.x < b.x) {
return true;
} else {
mAB = (b.y - a.y) / (b.x - a.x);
mAP = (p.y - a.y) / (p.x - a.x);
return mAP > mAB;
}
}
};
function pathAnchorsInPoly (myPath, polyPts) {
var flag = false;
var all = [];
foreach(myPath.pathPoints, function (arg) {
if (RayCaster.pointInPoly(arg.anchor, polyPts) === false) {
all.push(false);
}
all.push(true);
});
flag = (all.indexOf(false) != -1) ? false : true;
if (flag === false) {
// alert("NOT All Path Anchors are inside containing ellipse.");
} else {
// alert("Yay! All Path Anchors are INSIDE the containing ellipse!");
}
return flag; // for use later (?)
}
function testContained(container, checkme){
var cclr = new CMYKColor();
cclr.cyan = 0;
cclr.magenta = 100;
cclr.yellow = 100;
cclr.black = 0;
// var m = doc.pathItems.getByName('mycir'); // my circle
// var mp = doc.pathItems.getByName('mypth'); // my test path
var pts = getPathPoints(container);
var bezPts = getBezierPath(pts);
var chkRes = pathAnchorsInPoly(checkme, bezPts);
return chkRes;
}
// ccAry
var ccAry = {};
for (var i = _pathes.length - 1; i >= 0; i--) {
var container = _pathes[i];
if(ccAry['cc_'+i] > -1) continue;
ccAry['cc_'+i] = 1;
container.rotate('0.01', true, true, true, true, Transformation.LEFT);
for (var j = 0; j < _pathes.length; j++) {
var checkme = _pathes[j];
if(testContained(container, checkme)) {
// alert('cc_'+j);
ccAry['cc_'+j] = 0;
}
}
container.rotate('-0.01', true, true, true, true, Transformation.LEFT);
}
return ccAry;
}
// distance of two positions
function dist2(p1, p2) {
return Math.pow(p1[0] - p2[0], 2)
+ Math.pow(p1[1] - p2[1], 2);
}
// re-arrange the point and redraw from re-points
function overlayPoints(path, sg_points, activeLayer, inoutFlag){
var pts = path.pathPoints,
tmpancs = [],
tmpancsPp = [],
ancs = [],
ancsPp = [],
minDist = 0.15,
minFlag = true,
stopFlag = false,
startPos = sg_points[0],
secondPos = 0;
for(var k = 0; k < pts.length; k++) {
// if(secondPos!=0 && dist2(secondPos.anchor, pts[k].anchor) < minDist) {
// stopFlag = true;
// ancs.push(pts[k].anchor);
// ancsPp.push(pts[k]);
// }
// if(stopFlag) continue;
if(dist2(sg_points[0].anchor, pts[k].anchor) < minDist && minFlag){
minFlag = false;
startPos = sg_points[1];
secondPos = pts[k];
}
if(dist2(sg_points[1].anchor, pts[k].anchor) < minDist && minFlag) {
minFlag = false;
startPos = sg_points[0];
secondPos = pts[k];
}
if(secondPos!=0){
ancs.push(pts[k].anchor);
ancsPp.push(pts[k]);
} else {
tmpancs.push(pts[k].anchor);
tmpancsPp.push(pts[k]);
}
}
ancs = ancs.concat(tmpancs);
ancsPp = ancsPp.concat(tmpancsPp);
ancs.push(ancs[0]);
ancsPp.push(ancsPp[0]);
var s_e_posAry = getStartAndEndPos(activeLayer, secondPos.anchor, startPos.anchor);
var standardP, stInd;
var tempPos = [];
standardP = ancsPp[0].rightDirection;
if(dist2(standardP, s_e_posAry[0].anchor) > dist2(standardP, s_e_posAry[1].anchor)){
tempPos['R'] = [0, dist2(standardP, s_e_posAry[0].anchor).toFixed(1)];
} else {
tempPos['R'] = [1, dist2(standardP, s_e_posAry[1].anchor).toFixed(1)];
}
standardP = ancsPp[0].leftDirection;
if(dist2(standardP, s_e_posAry[0].anchor) > dist2(standardP, s_e_posAry[1].anchor)){
tempPos['L'] = [0, dist2(standardP, s_e_posAry[0].anchor).toFixed(1)];
} else {
tempPos['L'] = [1, dist2(standardP, s_e_posAry[1].anchor).toFixed(1)];
}
// alert(tempPos['L'][1]+" === "+tempPos['R'][1]);
if(tempPos['L'][1] > tempPos['R'][1]){
stInd = tempPos['L'][0];
} else if(tempPos['L'][1] == tempPos['R'][1]){
standardP = ancsPp[1].anchor;
if(dist2(standardP, s_e_posAry[0].anchor) > dist2(standardP, s_e_posAry[1].anchor))
stInd = 0
else stInd = 1;
} else {
stInd = tempPos['R'][0];
}
var enInd = stInd?0:1;
ancsPp.unshift(s_e_posAry[stInd]);
ancsPp.push(s_e_posAry[enInd]);
ancs.unshift(s_e_posAry[stInd].anchor);
ancs.push(s_e_posAry[enInd].anchor);
var dppath = path.duplicate();
dppath.closed = false;
dppath.setEntirePath(ancs);
var dpPoint = dppath.pathPoints;
for (var k = 0; k < dpPoint.length-1; k++) {
if(k == dpPoint.length - 2) {
if(secondPos.anchor) dpPoint[k].rightDirection = secondPos.anchor;
dpPoint[k].leftDirection = ancsPp[k].leftDirection;
dpPoint[k].pointType = ancsPp[k].pointType;
} else if(k == 1){
dpPoint[k].rightDirection = ancsPp[k].rightDirection;
if(secondPos.anchor) dpPoint[k].leftDirection = secondPos.anchor;
dpPoint[k].pointType = ancsPp[k].pointType;
} else {
dpPoint[k].rightDirection = ancsPp[k].rightDirection;
dpPoint[k].leftDirection = ancsPp[k].leftDirection;
dpPoint[k].pointType = ancsPp[k].pointType;
}
}
}
// calculate the start and end pos by 5 degrees
function getStartAndEndPos(myDoc, o_s_pos, o_e_pos){
var myLine = myDoc.pathItems.add();
myLine.setEntirePath([o_s_pos, o_e_pos]);
var d5ch1 = myLine.duplicate();
d5ch1.rotate(10, true, true, true, true, Transformation.CENTER);
var d5ch2 = myLine.duplicate();
d5ch2.rotate(-10, true, true, true, true, Transformation.CENTER);
var firstPosOfPath = d5ch1.pathPoints[1];
var endPosOfPath = d5ch2.pathPoints[1];
myLine.remove();
d5ch1.remove();
d5ch2.remove();
return [firstPosOfPath, endPosOfPath];
}
// check
// color closed paths
function checkClosedPath(pathes){
var errFlag = false;
for(var i = pathes.length - 1; i >= 0; i--){
var op = pathes[i];
// op.closed = true;
// op.filled = true;
// op.stroked = true;
if(!op.closed || op.filled) {
errFlag = true;
op.strokeColor = makeColor(255,0,0);
continue;
}
}
errFlag && alert("Closed Or No Fill Error!");
return errFlag;
}
// check same stroke path
function checkSameStroke(pathes){
var errFlag = false;
var wdAry = [];
var wdObj = {};
for(var i = pathes.length - 1; i >= 0; i--){
var op = pathes[i];
var wd = (op.strokeWidth).toFixed(3);
wdAry[i] = wd;
wdObj[wd] = wdObj[wd]?wdObj[wd]+1:1;
}
var mw = 0;
var maxWd = "";
for(var mk in wdObj){
if(wdObj[mk]> mw){
mw = wdObj[mk];
maxWd= mk
}
}
for(var i = pathes.length - 1; i >= 0; i--){
var op = pathes[i];
var wd = (op.strokeWidth).toFixed(3);
if(maxWd != wd) {
errFlag = true;
op.strokeColor = makeColor(255,0,0);
}
}
errFlag && alert("Incorrect Width Of Stroke!");
return errFlag;
}
function checkSameColor(pathes){
var errFlag = false;
var clrAry = [];
var clrObj = {};
for(var i = pathes.length - 1; i >= 0; i--){
var op = pathes[i];
if(op.stroked && op.strokeColor.spot){
clrAry[i] = op.strokeColor.spot.name;
var clrName = op.strokeColor.spot.name;
} else {
var colorSel = getColor(op);
clrAry[i] = colorSel;
var clrName = getColorName(colorSel);
}
clrObj[clrName] = clrObj[clrName]?clrObj[clrName]+1:1;
}
var mc = 0;
var maxClr = "";
for(var mk in clrObj){
if(clrObj[mk]> mc){
mc = clrObj[mk];
maxClr= mk
}
}
for(var i = pathes.length - 1; i >= 0; i--){
var op = pathes[i];
if(op.stroked && op.strokeColor.spot){
var clrName = op.strokeColor.spot.name;
} else {
var colorSel = getColor(op);
var clrName = getColorName(colorSel);
}
if(maxClr != clrName) {
errFlag = true;
op.strokeColor = makeColor(255,0,0);
}
}
errFlag && alert("Incorrect Color Of Stroke");
return errFlag;
}
// defined unique color name
function getColorName(clr){
return "c_"+(clr.cyan).toFixed(2)+"_m_"+(clr.magenta).toFixed(2)+"_y_"+(clr.yellow).toFixed(2)+"_b_"+(clr.black).toFixed(2);
}
// get color
function getColor(obj) {
var colorSel = new CMYKColor;
colorSel.cyan = obj.strokeColor.cyan;
colorSel.magenta = obj.strokeColor.magenta;
colorSel.yellow = obj.strokeColor.yellow;
colorSel.black = obj.strokeColor.black;
return colorSel;
}
// make color
function makeColor(r,g,b){
var testColor = new RGBColor();
testColor.red = r;
testColor.green = g;
testColor.blue = b;
return testColor;
}
// display the colors of selected paths
function displayColorOfPath(pathes, activeLayer){
var obj = pathes[pathes.length-1];
var maxLeftPos = 0;
var maxTopPos = Math.pow(10,5) *-1;
for(var i = 0; i < pathes.length; i++){
var p = pathes[i].geometricBounds;
if(maxLeftPos < p[2]) maxLeftPos = p[2]
if(maxTopPos < p[1]) maxTopPos = p[1]
}
if(obj.stroked && obj.strokeColor.spot){
var clrName = obj.strokeColor;
drawColorName(activeLayer, clrName,maxTopPos,maxLeftPos,clrName,"type1");
// itext.strokeColor.spot.name = clrName;
} else {
var colorSel = getColor(obj);
var clrName = "CMYB("+(colorSel.cyan).toFixed(1) +", "
+ (colorSel.magenta).toFixed(1) +", "
+ (colorSel.yellow).toFixed(1) +", "
+ (colorSel.black).toFixed(1)+")";
drawColorName(activeLayer, clrName,maxTopPos,maxLeftPos,colorSel,"type2");
}
}
function drawColorName(activeLayer,cName,mt,ml,colorSel, type) {
var itext = activeLayer.textFrames.add();
var rect = activeLayer.pathItems.rectangle (mt, ml+8, 10, 10);
rect.strokeColor = rect.fillColor = colorSel;
if(type == "type1"){
cName = colorSel.spot.name;
}
itext.contents = cName;
itext.textRange.characterAttributes.fillColor = colorSel;
itext.top = mt;
itext.left = ml + 20;
itext.stroked = false;
itext.filled = false;
}
// ------------------------------------------------
// extract PathItems from the selection which length of PathPoints
// is greater than "n"
function getPathItemsInSelection(n, pathes){
if(documents.length < 1) return;
var s = activeDocument.selection;
if (!(s instanceof Array) || s.length < 1) return;
extractPathes(s, n, pathes);
}
// --------------------------------------
// extract PathItems from "s" (Array of PageItems -- ex. selection),
// and put them into an Array "pathes". If "pp_length_limit" is specified,
// this function extracts PathItems which PathPoints length is greater
// than this number.
function extractPathes(s, pp_length_limit, pathes){
for(var i = 0; i < s.length; i++){
if(s[i].typename == "PathItem" && !s[i].guides && !s[i].clipping){
if(pp_length_limit && s[i].pathPoints.length <= pp_length_limit){
continue;
}
pathes.push(s[i]);
} else if(s[i].typename == "GroupItem"){
// search for PathItems in GroupItem, recursively
extractPathes(s[i].pageItems, pp_length_limit, pathes);
} else if(s[i].typename == "CompoundPathItem"){
// searches for pathitems in CompoundPathItem, recursively
// ( ### Grouped PathItems in CompoundPathItem are ignored ### )
extractPathes(s[i].pathItems, pp_length_limit , pathes);
}
}
}
function distanceFromPointToPoint (A, B)
{
/* since we only need to know what point is furthest, the squared result is okay as well */
/* return Math.sqrt ( ((A[0]-B[0]) * (A[0]-B[0])) + ((A[1]-B[1]) * (A[1]-B[1])) ); */
return ((A[0]-B[0]) * (A[0]-B[0])) + ((A[1]-B[1]) * (A[1]-B[1]));
}
function pathToArray (obj)
{
var pt;
var flatpath = [];
if (!obj.hasOwnProperty ("pathPoints"))
return null;
for (pt=0; pt<obj.pathPoints.length; pt++)
{
flatpath.push (obj.pathPoints[pt].anchor);
}
/* once more for good luck */
flatpath.push (obj.pathPoints[0].anchor);
return flatpath;
}
function furthestSet (obj, c_xy)
{
var flatpath = [], i, d, distance = -1, result = [];
if (obj.constructor.name == "CompoundPathItem")
{
for (p=0; p<obj.pathItems.length; p++)
{
flatpath = flatpath.concat(pathToArray (obj.pathItems[p]));
}
} else
{
flatpath = pathToArray (obj);
}
if (flatpath == [])
return [0, [0,0], [0,0]];
for (i=0; i < flatpath.length-1; i++)
{
d = distanceFromPointToPoint (flatpath[i], c_xy);
if (d > distance)
{
distance = d;
result = [d, flatpath[i]];
}
}
return result;
}