• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Projecting cap to Butt cap via scripting

Explorer ,
Dec 05, 2016 Dec 05, 2016

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Dec 11, 2016 Dec 11, 2016

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.

...

Votes

Translate

Translate
Adobe
Participant ,
Dec 06, 2016 Dec 06, 2016

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 11, 2016 Dec 11, 2016

Copy link to clipboard

Copied

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.typename == "PathItem") {

            paths.push(s);

        } else if (s.typename == "GroupItem") {

            // search for PathItems in GroupItem, recursively

            extractPaths(s.pageItems, paths);

        } else if (s.typename == "CompoundPathItem") {

            // searches for pathitems in CompoundPathItem, recursively

            // ( ### Grouped PathItems in CompoundPathItem are ignored ### )

            extractPaths(s.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) {

            ps.unshift([anchor, leftDirection, rightDirection, pointType]);

        }

        i > 0 && p.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[0];

            leftDirection = ps[1];

            rightDirection = ps[2];

            pointType = ps[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);

    }

}

main();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 11, 2016 Dec 11, 2016

Copy link to clipboard

Copied

LATEST

Wow! It's works! Thanks, moluapple.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines