4 Replies Latest reply on Dec 11, 2016 7:43 AM by victorcarbajo

    Projecting cap to Butt cap via scripting

    victorcarbajo

      Hello. I need to change all Projecting cap strokes to Butt cap strokes, but extending half the line width beyond the end of the line.

       

      but cap.png

        • 1. Re: Projecting cap to Butt cap via scripting
          zertle Level 2

          The pseudo-code would look something like this:

           

          loop through pathItems {
               
               get current path
               
               if currentPath.closed == true {
                    get stroke width of current pathItem * 0.5
                    change pathItem.strokeCap to StrokeCap.BUTTENDCAP
                    
                    add pathPoint half the stroke width in the direction of the last pathPoint's leftDirection
                    
                    add pathPoint half the stroke width in the direction of the first pathPoint's rightDirection
               }
          }
          

           

          Something along these lines, you might need to flip the leftDirection and rightDirection, as well as double check that the anchor point isn't a corner. If I have some time later I'll see if I can put together something more concrete, this seems like a pretty fun puzzle!

           

          Good luck!

          • 2. Re: Projecting cap to Butt cap via scripting
            victorcarbajo Level 1

            Thanks, zertle, but I don't know how to complete the code.

            • 3. Re: Projecting cap to Butt cap via scripting
              moluapple Level 4

              This code is modified from s.h's page : Scripts for Adobe Illustrator CS , please select some path items and test.

              // Thanks to Hiroyuki Sato
              // -----------------------------------------------------------------------
              // codes copy from: http://shspage.com/aijs/en/#reverse
              // return the angle of the line drawn from p1 to p2
              function getRad(p1, p2) {
                  return Math.atan2(p2[1] - p1[1],
                      p2[0] - p1[0]);
              }
              
              function getPnt(pnt, rad, dis) {
                  return [pnt[0] + Math.cos(rad) * dis,
                      pnt[1] + Math.sin(rad) * dis
                  ];
              }
              
              function extractPaths(s, paths) {
                  for (var i = 0; i < s.length; i++) {
                      if (s[i].typename == "PathItem") {
                          paths.push(s[i]);
              
                      } else if (s[i].typename == "GroupItem") {
                          // search for PathItems in GroupItem, recursively
                          extractPaths(s[i].pageItems, paths);
              
                      } else if (s[i].typename == "CompoundPathItem") {
                          // searches for pathitems in CompoundPathItem, recursively
                          // ( ### Grouped PathItems in CompoundPathItem are ignored ### )
                          extractPaths(s[i].pathItems, paths);
                      }
                  }
                  return paths
              }
              // -----------------------------------------------------------------------------
              
              function isEqual(p1, p2) {
                  return p1[0] === p2[0] && p1[1] === p2[1]
              }
              
              function addPointBS(p, beginPoint, endPoint) {
                  var ps = [],
                      end,
                      i;
              
                  for (i = p.length - 1; i >= 0; i--) {
                      with(p[i]) {
                          ps.unshift([anchor, leftDirection, rightDirection, pointType]);
                      }
                      i > 0 && p[i].remove();
                  }
                  // set begin point
                  p[0].rightDirection = p[0].leftDirection = p[0].anchor = beginPoint;
                  // reset original points
                  for (i = 0; i < ps.length; i++) {
                      with(p.add()) {
                          anchor = ps[i][0];
                          leftDirection = ps[i][1];
                          rightDirection = ps[i][2];
                          pointType = ps[i][3];
                      }
                  }
                  // modify original first and last points
                  p[1].leftDirection = p[1].anchor;
                  p[p.length - 1].rightDirection = p[p.length - 1].anchor;
                  // set end point
                  end = p.add();
                  end.rightDirection = end.leftDirection = end.anchor = endPoint;
              }
              
              function Projecting2Butt(pth) {
                  var p = pth.pathPoints,
                      begin = p[0],
                      end = p[p.length - 1],
                      beginAnchor = begin.anchor,
                      beginRD = begin.rightDirection,
                      endAnchor = end.anchor,
                      endLD = end.leftDirection,
                      rad,
                      rad2,
                      beginPoint,
                      endPoint;
              
                  if (isEqual(beginRD, beginAnchor)) {
                      rad = getRad(p[1].leftDirection, beginAnchor);
                  } else {
                      rad = getRad(beginRD, beginAnchor);
                  }
              
                  if (isEqual(endLD, endAnchor)) {
                      rad2 = getRad(p[p.length - 2].rightDirection, endAnchor);
                  } else {
                      rad2 = getRad(endLD, endAnchor);
                  }
              
                  beginPoint = getPnt(beginAnchor, rad, pth.strokeWidth / 2);
                  endPoint = getPnt(endAnchor, rad2, pth.strokeWidth / 2);
              
                  addPointBS(p, beginPoint, endPoint);
              }
              
              function main() {
                  var paths = extractPaths(app.selection, []),
                      i = 0;
              
                  for (; i < paths.length; i++) {
                      Projecting2Butt(paths[i]);
                  }
              }
              
              main();
              
              • 4. Re: Projecting cap to Butt cap via scripting
                victorcarbajo Level 1

                Wow! It's works! Thanks, moluapple.