Skip to main content
christype
Known Participant
June 15, 2020
Answered

How to make all paths the same direction

  • June 15, 2020
  • 11 replies
  • 2366 views

I have a whole lot of parallel paths that have different directions (some up, some down). "Reverse path direction" is not helpful unless I select manually, which is super time consuming.

 

Wondering if there is a way to quickly align all path directions? 

 

This topic has been closed for replies.
Correct answer CarlosCanto

try this script, select your items before running

// equalizePathDirection,jsx
// CarlosCanto // 06/15/2020
// https://community.adobe.com/t5/illustrator/how-to-make-all-paths-the-same-direction/td-p/11207971?page=1

function main() {
    var idoc = app.activeDocument;
    
    var sel = idoc.selection;
    var pathitems = [];
    
    if (sel.length>1) { 
        for (var a = 0; a<sel.length; a++) {
            var pgItem = sel[a];
            if (pgItem.typename == 'PathItem' && !pgItem.closed) {
                pathitems.push(pgItem);
            }
        }
        selection = null;
        equalizeDirection(pathitems);
        
    }
    else {
        alert('select two or more path items and try again');
    }
}

main ();

function equalizeDirection(pa /*pathItems array*/) {
    var p0, p1, pp;
    
    for (var p in pa) {
        pp = pa[p].pathPoints;
        p0 = pp[0].anchor;
        p1 = pp[pp.length-1].anchor;
        if (p0[1]<p1[1]) {
            pa[p].selected = true;
        }
    }

    app.executeMenuCommand('Reverse Path Direction');
}

11 replies

Kurt Gold
Community Expert
Community Expert
June 16, 2020

I think an action that would first do the (dirty) tiny rotation and then executes the script may be used as a bumpy workaround to solve the "horizontal issue". Just an idea. No proper one, I know.

 

Have a good day (and evening).

CarlosCanto
Community Expert
Community Expert
June 16, 2020

noted, thanks have a good one as well.

CarlosCanto
Community Expert
Community Expert
June 16, 2020

hahaha it is not as simple Kurt, as of now, all paths are evaluated as if they're vertical. Even almost horizontal lines would get their Y position tested. 

 

A more complete solution would be to treat lines less than 45 degrees as horizontal and over 45 as vertical. Then we could process both vertical and horizontal lines more precisely.

Kurt Gold
Community Expert
Community Expert
June 16, 2020

A quick fix for the horizontal lines issue would be to clandestinely rotate the current selection 0,0001° as an evil mischief inside your script code.

 

Probably no one will ever notice that trick.

 

😎

CarlosCanto
Community Expert
Community Expert
June 16, 2020

Hi Kurt, thanks for the detailed report.

 

I see what happened, and I get the same results you did.

 

the truth is, the script is not very robust. The OP requested to reverse vertical paths, so that's all I did. I did't have time to make a "production" ready script covering all possible scenarios.

 

as usual thanks for checking so diligently!!

 

Carlos

Kurt Gold
Community Expert
Community Expert
June 16, 2020

Yes, Carlos, the one liner script does work (of course).

 

I did some more testing and it turned out that the issue with your script is a special (and somehow beautiful) one.

 

My tests yesterday were based on horizontal straight lines (with different path directions). You may try the following test: With the line tool draw a dozen of horizontal lines and give them different path directions. Now execute your script. Does it actually work? No, at least not for me.

 

Do the same with a couple of vertical lines. Does it work? Yes, at least for me.

 

The same goes for straight lines drawn at other angles (other than horizontal). The script will work.

 

There seem to be some issues with curvy paths drawn with the Pen tool for example, but I would have to do some further tests in order to describe them in detail.

CarlosCanto
Community Expert
Community Expert
June 15, 2020

ok, see you tomorrow. Have a good night

Kurt Gold
Community Expert
Community Expert
June 15, 2020

Thanks, Carlos.

 

No, my test was based on open paths. I did not use any closed path.

 

Will do some more experiments tomorrow. Now it's time to say ...

 

Good night (well, at least over here).

CarlosCanto
Community Expert
Community Expert
June 15, 2020

Hello Kurt, thanks for letting me know. Are you trying to reverse Closed Shapes? the script only works with Open Paths, I excluded closed shapes on purpose.

 

Would you mind doint a test for me?

 

can you run this one liner script, this is the command responsible for reversing. Select open or closed paths before running.

 

app.executeMenuCommand('Reverse Path Direction');

 

thanks

Carlos

Kurt Gold
Community Expert
Community Expert
June 15, 2020

Good evening, Carlos.

 

I'd like to inform you that your script doesn't seem to work as expected in my environment.

 

Actually, it doesn't do anything. It only deselects the current selection.

 

Tested with Illustrator 24.1.3 on Macintosh OS 10.14.6

 

Just thought that might be worth of a note for you.

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
June 15, 2020

try this script, select your items before running

// equalizePathDirection,jsx
// CarlosCanto // 06/15/2020
// https://community.adobe.com/t5/illustrator/how-to-make-all-paths-the-same-direction/td-p/11207971?page=1

function main() {
    var idoc = app.activeDocument;
    
    var sel = idoc.selection;
    var pathitems = [];
    
    if (sel.length>1) { 
        for (var a = 0; a<sel.length; a++) {
            var pgItem = sel[a];
            if (pgItem.typename == 'PathItem' && !pgItem.closed) {
                pathitems.push(pgItem);
            }
        }
        selection = null;
        equalizeDirection(pathitems);
        
    }
    else {
        alert('select two or more path items and try again');
    }
}

main ();

function equalizeDirection(pa /*pathItems array*/) {
    var p0, p1, pp;
    
    for (var p in pa) {
        pp = pa[p].pathPoints;
        p0 = pp[0].anchor;
        p1 = pp[pp.length-1].anchor;
        if (p0[1]<p1[1]) {
            pa[p].selected = true;
        }
    }

    app.executeMenuCommand('Reverse Path Direction');
}

christype
christypeAuthor
Known Participant
June 15, 2020

@CarlosCanto

That's great thanks. Nailed it.

CarlosCanto
Community Expert
Community Expert
June 15, 2020

you're welcome!