A-Ming 发表于 2017-3-7 18:32:53

有大神能根据这代码修改成指定数值剪断路径么?

如:路径100MM输入25MM就能剪断成一条25MM的路径


// Divide (length)


// 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.


// Copyright(c) 2006-2009 SATO Hiroyuki
// http://park12.wakwak.com/~shp/lc/et/en_aics_script.html


// 2006-06-30
// 2009-05-23 some refinements


var ver10 = (version.indexOf('10') == 0);


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.pathPoints;
   
    for(i = 0; i < p.length; i++){
      j = parseIdx(p, i + 1);
      if(j < 0) break;
      if(! sideSelection(p, p)) continue;
      
      ar = ;
      q = .anchor, p.rightDirection,
         p.leftDirection, p.anchor];
      
      len = getT4Len(q, 0) / n;
      if(len <= 0) continue;


      redrawflg = true;


      for(k = 1; k < n; k++){
      ar.push( getT4Len(q, len * k) );
      }
      pnts.push(ar);
    }
    if(redrawflg) addPnts(pathes, pnts, false);
}
activeDocument.selection = pathes;
}


// ----------------------------------------------
// addPnts: adds anchors to pathitem "pi"
// an example of usage:
/* var pi=activeDocument.selection;
var pnts = [ ,
             ];// [ ,
addPnts(pi,pnts,true);
*/
function addPnts(pi, pnts, need2sort){ // target pathItem, list, need to sort the parameters
// pnts = [ ,
// items must be ordered by index
// an item without t () is acceptable.an empty allay is not acceptable.
// an index out of bounds is acceptable.the range of t is 0 < t < 1.
var p = pi.pathPoints;
var pnts2 = [];
var adjNextLDir= 0;
var adjFirstLDir = 0;
var idx = (pi.closed && pnts == p.length - 1) ? 0 : pnts;
var ar = pnts.shift();
var nidx = ar.shift();
var j, pnt, q;
for(var i = idx; i < p.length; i++){
    pnts2.push( getDat(p) );
    if(adjNextLDir > 0){
      pnts2 = adjHanP(p,0,1-adjNextLDir);
    }
    if(nidx == i){
      if(ar.length > 0){
      if(need2sort){
          ar.sort();
          ar = getUniq(ar);
      }
      if(i == p.length - 1 && idx == 0){
          adjFirstLDir = ar;
      }
      pnts2 = adjHanP(p, 1, ar),
      ar.unshift(0);
      ar.push(1);
      nxi = parseIdx(p,i + 1);
      if(nxi < 0) break;
      q = .anchor, p.rightDirection,
             p.leftDirection, p.anchor];
      if(arrEq(q, q) && arrEq(q, q)){
          for(j = 1; j < ar.length - 1; j++){
            pnt = bezier(q, ar);
            pnts2.push( );
          }
      } else {
          for(j = 1; j < ar.length - 1; j++){
            pnts2.push( getDivPnt(q, ar, ar, ar) );
          }
      }
         adjNextLDir = ar;
      } else {
      adjNextLDir = 0;
      }
      if(pnts.length > 0){
      ar = pnts.shift();
      nidx = ar.shift();
      }
    } else {
      adjNextLDir = 0;
    }
}
if(adjFirstLDir > 0) pnts2 = adjHanP(p, 0, 1 - adjFirstLDir);
if(pnts2.length > 0) applyData2AfterIdx(p, pnts2, idx - 1);
}
// ----------------------------------------------
function getUniq(ar){ // Array (sorted)
if(ar.length < 2) return ar;
var ar2 = [ ar ];
var torelance = 0.01;
for(var i = 1; i < ar.length; i++){
    if(ar - ar2[ ar2.length - 1 ] > torelance) ar2 = ar;
}
return ar2;
}
// ----------------------------------------------
// returns an array for properties of a pathpoint
function getDat(p){ // pathPoint
with(p) return ;
}
// ----------------------------------------------
// magnifies a handle by m
function adjHanP(p, n, m){ // p=pathpoint, n=0:leftDir, n=1:rightDir, m=magnification rate
with(p){
    var d = (n == 1 ? rightDirection : leftDirection);
    return + (d - anchor) * m,
            anchor + (d - anchor) * m];
}
}
// ----------------------------------------------
// returns an array for properties of a pathpoint
// that corresponds to the parameter "t1"
// q=4 points, t0-2=parameters, anc=coordinate of anchor
function getDivPnt(q, t0, t1, t2, anc){
if(!anc) anc = bezier(q, t1);
var r = defDir(q,1, t1, anc, (t2 - t1) / (1 - t1));
var l = defDir(q,0, t1, anc, (t1 - t0) / t1);
return [ anc, r, l, PointType.SMOOTH ];
}
// ----------------------------------------------
// returns the coordinate of the handle of the point on the bezier curve
// that corresponds to the parameter "t"
// q=4 points, t=paramter, anc=coordinate of anchor, m=magnification ratio
function defDir(q, n, t, anc, m){ // n=0:ldir, n=1:rdir
var dir = [ t * (t * (q - 2 * q + q) + 2 * (q - q)) + q,
            t * (t * (q - 2 * q + q) + 2 * (q - q)) + q];
return + (dir - anc) * m,
          anc + (dir - anc) * m];
}
// ----------------------------------------------
// return the coordinate on the bezier curve
// that corresponds to the paramter "t"
function bezier(q, t) {
var u = 1 - t;
return + 3*u*t*(u* q + t* q) + t*t*t * q,
          u*u*u * q + 3*u*t*(u* q + t* q) + t*t*t * q];
}
// ----------------------------------------------
function applyData2Path(p, pnts){ // pathpoint, list
// (format:[[ anchor, rightDirection, leftDirection, poinType ],...]
if(pnts.length<1) return;


var pt;


while(p.length > pnts.length){
    p[ p.length - 1 ].remove();
}

for(var i in pnts){
    pt = i < p.length ? p : p.add();
    with(pt){
      anchor         = pnts;
      rightDirection = pnts;
      leftDirection= pnts;
      pointType      = pnts;
    }
}
}
// ----------------------------------------------
function applyData2AfterIdx(p, pnts, idx){ // pathpoint, list, index
if(idx == null || idx < 0){
    applyData2Path(p, pnts);
    return;
}
var pt;


while(p.length-1 > idx){
    p.remove();
}


for(var i = 0; i < pnts.length; i++){
    pt = p.add();
    with(pt){
      anchor         = pnts;
      rightDirection = pnts;
      leftDirection= pnts;
      pointType      = pnts;
    }
}
}


// ------------------------------------------------
// 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 * (q - q),
         q - 2 * q + q,
         q - q];
var n = - q + 3 * (q - q),
         q - 2 * q + q,
         q - q];
var k = [ m * m + n * n,
            4 * (m * m + n * n),
            2 * ((m * m + n * n) + 2 * (m * m + n * n)),
            4 * (m * m + n * n),
            m * m + n * n];

   var fullLen = getLength(k, 1);


if(len == 0){
    return fullLen;
   
} else if(len < 0){
    len += fullLen;
    if(len < 0) return 0;
   
} else if(len > fullLen){
    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 - getLength(k, t);
   
    if(Math.abs(d) < torelance) break;
    else if(d < 0) t1 = t;
    else t0 = t;
}

return t;
}


// ------------------------------------------------
// 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;

var fc = function(t, k){
    return Math.sqrt(t * (t * (t * (t * k + k) + k) + k) + k) || 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;
}


// ------------------------------------------------
// 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;
}
}


noWAR 发表于 2017-3-7 19:31:47

编程的问题不该在这问吧,这是印刷论坛啊

冰晶 发表于 2017-3-7 21:55:58

这是什么代码啊:o:o:o

A-Ming 发表于 2017-3-7 22:36:35

AI的曲线等分代码:lol:lol

彩广告 发表于 2019-8-19 18:24:56

这个好像是 c++,唯一高级语言,历害啊。这个可能没几个人会的。也没几个人懂的吧,可能等魔镜的h大专家,帮你解答吧,他应该很懂这东西来的。。 {:4_313:}{:4_313:}

94444199 发表于 2019-8-24 15:01:04

你这应该是从AI动作里复制出来的原代码吧!

A-Ming 发表于 2019-8-24 21:09:23

94444199 发表于 2019-8-24 15:01
你这应该是从AI动作里复制出来的原代码吧!

不是,已经解决了
页: [1]
查看完整版本: 有大神能根据这代码修改成指定数值剪断路径么?