QQ登录

只需一步,快速开始

查看: 3715|回复: 7
收起左侧

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

[复制链接]
累计签到:407 天
连续签到:1 天
发表于 2017-3-7 18:32:53 | 显示全部楼层 |阅读模式
1印币
如:路径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[h].pathPoints;
   
    for(i = 0; i < p.length; i++){
      j = parseIdx(p, i + 1);
      if(j < 0) break;
      if(! sideSelection(p, p[j])) continue;
      
      ar = ;
      q = [p.anchor, p.rightDirection,
           p[j].leftDirection, p[j].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[h], pnts, false);
  }
  activeDocument.selection = pathes;
}


// ----------------------------------------------
// addPnts: adds anchors to pathitem "pi"
// an example of usage:
/* var pi=activeDocument.selection[0];
var pnts = [ [0,  0.3,0.8,0.5],
             [3,  0.5] ];  // [ [i, t,t,t,t...],[i, t,t..
addPnts(pi,pnts,true);
*/
function addPnts(pi, pnts, need2sort){ // target pathItem, list, need to sort the parameters
  // pnts = [ [index,  t,t,t,t...],[index,  t,t..
  // items must be ordered by index
  // an item without t ([index]) 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[pnts.length-1][0] == p.length - 1) ? 0 : pnts[0][0];
  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[pnts2.length-1][2] = 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[ar.length-1];
        }
        pnts2[pnts2.length - 1][1] = adjHanP(p, 1, ar[0]),
        ar.unshift(0);
        ar.push(1);
        nxi = parseIdx(p,i + 1);
        if(nxi < 0) break;
        q = [p.anchor, p.rightDirection,
             p[nxi].leftDirection, p[nxi].anchor];
        if(arrEq(q[0], q[1]) && arrEq(q[2], q[3])){
          for(j = 1; j < ar.length - 1; j++){
            pnt = bezier(q, ar[j]);
            pnts2.push( [pnt, pnt, pnt, PointType.CORNER] );
          }
        } else {
          for(j = 1; j < ar.length - 1; j++){
            pnts2.push( getDivPnt(q, ar[j-1], ar[j], ar[j+1]) );
          }
        }
         adjNextLDir = ar[ar.length - 2];
      } else {
        adjNextLDir = 0;
      }
      if(pnts.length > 0){
        ar = pnts.shift();
        nidx = ar.shift();
      }
    } else {
      adjNextLDir = 0;
    }
  }
  if(adjFirstLDir > 0) pnts2[0][2] = adjHanP(p[0], 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[0] ];
  var torelance = 0.01;
  for(var i = 1; i < ar.length; i++){
    if(ar - ar2[ ar2.length - 1 ] > torelance) ar2[ar2.length] = ar;
  }
  return ar2;
}
// ----------------------------------------------
// returns an array for properties of a pathpoint
function getDat(p){ // pathPoint
  with(p) return [anchor, rightDirection, leftDirection, pointType];
}
// ----------------------------------------------
// 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 [anchor[0] + (d[0] - anchor[0]) * m,
            anchor[1] + (d[1] - anchor[1]) * 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 [x, y] 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[n][0] - 2 * q[n+1][0] + q[n+2][0]) + 2 * (q[n+1][0] - q[n][0])) + q[n][0],
              t * (t * (q[n][1] - 2 * q[n+1][1] + q[n+2][1]) + 2 * (q[n+1][1] - q[n][1])) + q[n][1]];
  return [anc[0] + (dir[0] - anc[0]) * m,
          anc[1] + (dir[1] - anc[1]) * m];
}
// ----------------------------------------------
// return the [x, y] coordinate on the bezier curve
// that corresponds to the paramter "t"
function bezier(q, t) {
  var u = 1 - t;
  return [u*u*u * q[0][0] + 3*u*t*(u* q[1][0] + t* q[2][0]) + t*t*t * q[3][0],
          u*u*u * q[0][1] + 3*u*t*(u* q[1][1] + t* q[2][1]) + t*t*t * q[3][1]];
}
// ----------------------------------------------
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[0];
      rightDirection = pnts[1];
      leftDirection  = pnts[2];
      pointType      = pnts[3];
    }
  }
}
// ----------------------------------------------
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[p.length-1].remove();
  }


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


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


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


华印网相关搜索

累计签到:354 天
连续签到:1 天
发表于 2017-3-7 19:31:47 | 显示全部楼层
编程的问题不该在这问吧,这是印刷论坛啊
回复 送花

使用道具 举报

累计签到:351 天
连续签到:0 天
发表于 2017-3-7 21:55:58 | 显示全部楼层
这是什么代码啊有大神能根据这代码修改成指定数值剪断路径么?{tag}(1)有大神能根据这代码修改成指定数值剪断路径么?{tag}(2)有大神能根据这代码修改成指定数值剪断路径么?{tag}(3)
回复 送花

使用道具 举报

累计签到:407 天
连续签到:1 天
 楼主| 发表于 2017-3-7 22:36:35 | 显示全部楼层
AI的曲线等分代码有大神能根据这代码修改成指定数值剪断路径么?{tag}(4)有大神能根据这代码修改成指定数值剪断路径么?{tag}(5)
回复 送花

使用道具 举报

累计签到:635 天
连续签到:2 天
发表于 2019-8-19 18:24:56 | 显示全部楼层
这个好像是 c++,唯一高级语言,历害啊。这个可能没几个人会的。也没几个人懂的吧,可能等魔镜的h大专家,帮你解答吧,他应该很懂这东西来的。  。 有大神能根据这代码修改成指定数值剪断路径么?{tag}(6)有大神能根据这代码修改成指定数值剪断路径么?{tag}(7)
回复 送花

使用道具 举报

累计签到:315 天
连续签到:1 天
发表于 2019-8-24 15:01:04 | 显示全部楼层
你这应该是从AI动作里复制出来的原代码吧!
回复 送花

使用道具 举报

累计签到:407 天
连续签到:1 天
 楼主| 发表于 2019-8-24 21:09:23 | 显示全部楼层
94444199 发表于 2019-8-24 15:01
你这应该是从AI动作里复制出来的原代码吧!

不是,已经解决了
回复 送花

使用道具 举报

您需要登录后才可以回帖 登录 | 注册帐号

本版积分规则

关闭

注意注意注意:必看上一条 /1 下一条

华印网 - 华印社区
Share More 周一至周日:09:00 - 21:00
华印网旗下的设计印刷制作类专业技术站点
请勿发布违反国家法律法规的内容,会员观点不代表本站立场
企鹅群号:119572101

华印网汇集印前印后技术、PDF拼版、防伪包装、数码印刷、合版印刷、图文设计、平面设计、数码印刷及CTP等最新印刷技术,提供软件汉化、插件汉化、cdr插件、ai插件、ps插件、pdf插件、印刷流程、ctp输出、印刷软件、印能捷、esko、CorelDRAW、InDesign、Illustrator、CTP、CDR以及PDF软件下载的综合性印刷论坛社区!

Powered by Discuz! X3.4 © 2001-2021,Tencent Cloud.

站点地图|小黑屋|手机版|Archiver|华印 ( 粤ICP备19020152号-1 )

GMT+8, 2024-12-22 22:15 , Processed in 0.098691 second(s), 27 queries , Gzip On, Yac On.

快速回复 返回顶部 返回列表