// divides each selected segment into specified number. based on the length.
// Length of each segment in each divided segments is equal.
// JavaScript Script for Adobe Illustrator CS3
// Tested with Adobe Illustrator CS3 13.0.3, Windows XP SP2 (Japanese version).
// This script provided "as is" without warranty of any kind.
// Free to use and distribute.
mAIn();
function main(){
var pathes = [];
getPathItemsInSelection(n, pathes);
if(pathes.length < 1) return;
// Settings ====================
var n = 2; // default dividing number
// =============================
// not ver.10 : input a number with a prompt box
if(! ver10){
n = prompt("divide each selected segment into ... (based on its length)", n);
if(! n){
return;
} else if(isNaN(n) || n < 2){
alert("Please input a number greater than 1 with 1 byte characters.");
return;
}
n = parseInt(n);
}
var i, j, k, p, q;
var pnts, len, ar, redrawflg;
for(var h = 0; h < pathes.length; h++){
redrawflg = false;
pnts = [];
p = pathes[h].pathPoints;
// ------------------------------------------------
// returns true, if a segment between pathpoints ps1 and ps2 is selected
function sideSelection(ps1, ps2) {
return (ps1.selected != PathPointSelection.NOSELECTION
&& ps1.selected != PathPointSelection.LEFTDIRECTION
&& ps2.selected != PathPointSelection.NOSELECTION
&& ps2.selected != PathPointSelection.RIGHTDIRECTION);
}
// ------------------------------------------------
// if the contents of both arrays are equal, return true (lengthes must be same)
function arrEq(arr1, arr2) {
for(var i in arr1){
if (arr1 != arr2){
return false;
}
}
return true;
}
// ------------------------------------------------
// return the bezier curve parameter "t"
// at the point which the length of the bezier curve segment
// (from the point start drawing) is "len"
// when "len" is 0, return the length of whole this segment.
function getT4Len(q, len){
var m = [q[3][0] - q[0][0] + 3 * (q[1][0] - q[2][0]),
q[0][0] - 2 * q[1][0] + q[2][0],
q[1][0] - q[0][0]];
var n = [q[3][1] - q[0][1] + 3 * (q[1][1] - q[2][1]),
q[0][1] - 2 * q[1][1] + q[2][1],
q[1][1] - q[0][1]];
var k = [ 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]];
// ------------------------------------------------
// return the length of bezier curve segment
// in range of parameter from 0 to "t"
// "m" and "n" are coefficients.
function getLength(k, t){
var h = t / 128;
var hh = h * 2;
for(var i = h; i < t; i += hh){
total += 2 * fc(i, k) + fc(i + h, k);
}
return total * hh;
}
// ------------------------------------------------
// 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.typename == "PathItem"){
if(pp_length_limit
&& s.pathPoints.length <= pp_length_limit){
continue;
}
pathes.push(s);
} else if(s.typename == "GroupItem"){
// search for PathItems in GroupItem, recursively
extractPathes(s.pageItems, pp_length_limit, pathes);
} else if(s.typename == "CompoundPathItem"){
// searches for pathitems in CompoundPathItem, recursively
// ( ### Grouped PathItems in CompoundPathItem are ignored ### )
extractPathes(s.pathItems, pp_length_limit , pathes);
}
}
}
// ----------------------------------------------
// return pathpoint's index. 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;
}
}