-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCut At Selected Anchors.jsx
112 lines (90 loc) · 3.14 KB
/
Cut At Selected Anchors.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
// Cut At Selected Anchors
// cuts selected puthes at each selected anchor
// test env: Adobe Illustrator CS3, CS6 (Windows)
// Copyright(c) 2005-2013 Hiroyuki Sato
// https://github.com/shspage
// This script is distributed under the MIT License.
// See the LICENSE file for details.
// Wed, 30 Jan 2013 07:04:30 +0900
main();
function main(){
var sp = [];
getPathItemsInSelection(2, sp);
if(sp.length<1) return;
var j, k, p;
var first_anchor_selected, idxs, ary, ancs;
for(var i=0; i<sp.length; i++){
p = sp[i].pathPoints;
idxs = [[0]];
first_anchor_selected = isSelected(p[0]);
for(j = 1; j < p.length; j++){
idxs[idxs.length - 1].push(j);
if(isSelected(p[j])) idxs.push([j]);
}
if(idxs.length < 2 && !(first_anchor_selected && sp[i].closed)) continue;
// adjust the array (closed path)
if(sp[i].closed){
if(first_anchor_selected){
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(j = 0; j < idxs.length; j++){
ary = idxs[j];
ancs = [];
for(k=ary.length - 1; k >= 0; k--) ancs.unshift(p[ary[k]].anchor);
with(sp[i].duplicate()){
closed = false;
setEntirePath(ancs);
for(k = pathPoints.length - 1; k >= 0; k--){
with(pathPoints[k]){
rightDirection = p[ary[k]].rightDirection;
leftDirection = p[ary[k]].leftDirection;
pointType = p[ary[k]].pointType;
}
}
}
}
sp[i].remove(); // remove the original path
}
}
// ------------------------------------------------
// 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].typename == "PathItem"
&& !s[i].guides && !s[i].clipping){
if(pp_length_limit
&& s[i].pathPoints.length <= pp_length_limit){
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 isSelected(p){
return p.selected == PathPointSelection.ANCHORPOINT;
}