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

Charu Rajput
Community Expert
Community Expert
June 15, 2020

Hi, Did you try the solution from the following thread

https://community.adobe.com/t5/illustrator/reverse-path-direction/td-p/3298182?page=1

 

This script also works when you select the path Items, Here is the updated version of the same script that works for all pathItems with no selection.

 

// A Javascript for Adobe Illustrator
// by Wolfgang Reszel (ai-js@rumborak.de)


// -------------------------------------------------------------------

var language="en";   // "de" fuer Deutsch

// -------------------------------------------------------------------

var WR="WR-reversePathDirection v0.2\n\n";

if (language == "de") {

  var MSG_noobjects = WR+"Bitte w\xE4hle vorher einige Objekte aus.";
  var MSG_nodocs = WR+"Kein Dokument ge\xF6ffnet.";

} else {

  var MSG_noobjects = WR+"Please select some objects.";
  var MSG_nodocs = WR+"You have no open document."

}

var error=0;

if (documents.length<1) {
  error++;
  alert(MSG_nodocs);
}
if (error == 0) {
  var theObjects= app.activeDocument.pathItems; //selection;

  if (theObjects.length<1 && error == 0) {
    error++;
    alert(MSG_noobjects);
  }
}
if (error < 1) {
  reversePaths(theObjects);
}

function reversePaths(theItems) {
  if (theItems.typename == "TextPath") {
    pathLen = theItems.pathPoints.length;

    for ( k = 0; k < pathLen/2; k++ ) {
      h = pathLen-k-1;

      HintenAnchor = theItems.pathPoints[h].anchor;
      HintenLeft = theItems.pathPoints[h].leftDirection;
      HintenType = theItems.pathPoints[h].pointType;
      HintenRight = theItems.pathPoints[h].rightDirection;

      theItems.pathPoints[h].anchor = theItems.pathPoints[k].anchor;
      theItems.pathPoints[h].leftDirection = theItems.pathPoints[k].rightDirection;
      theItems.pathPoints[h].pointType = theItems.pathPoints[k].pointType;
      theItems.pathPoints[h].rightDirection = theItems.pathPoints[k].leftDirection;
      theItems.pathPoints[k].anchor = HintenAnchor;
      theItems.pathPoints[k].leftDirection = HintenRight;
      theItems.pathPoints[k].pointType = HintenType;
      theItems.pathPoints[k].rightDirection = HintenLeft;

    }
  }

  for (var i = 0 ; i < theItems.length; i++)
  {
    if (theItems[i].typename == "GroupItem" || theItems[i].typename == "CompoundPathItem" ) {
      reversePaths(theItems[i].pathItems);
      try {reversePaths(theItems[i].compoundPathItems)} catch (e) {};
    }
    if (theItems[i].typename == "TextFrame" ) {reversePaths(theItems[i].textPath);}
    if (theItems[i].typename == "TextArtItem" ) {reversePaths(theItems[i].textPaths);}
    if ( theItems[i].typename == "PathItem" && !theItems[i].locked && !theItems[i].parent.locked && !theItems[i].layer.locked ) {

      pathLen = theItems[i].pathPoints.length;

      for ( k = 0; k < pathLen/2; k++ ) {
        h = pathLen-k-1;

        HintenAnchor = theItems[i].pathPoints[h].anchor;
        HintenLeft = theItems[i].pathPoints[h].leftDirection;
        HintenType = theItems[i].pathPoints[h].pointType;
        HintenRight = theItems[i].pathPoints[h].rightDirection;

        theItems[i].pathPoints[h].anchor = theItems[i].pathPoints[k].anchor;
        theItems[i].pathPoints[h].leftDirection = theItems[i].pathPoints[k].rightDirection;
        theItems[i].pathPoints[h].pointType = theItems[i].pathPoints[k].pointType;
        theItems[i].pathPoints[h].rightDirection = theItems[i].pathPoints[k].leftDirection;
        theItems[i].pathPoints[k].anchor = HintenAnchor;
        theItems[i].pathPoints[k].leftDirection = HintenRight;
        theItems[i].pathPoints[k].pointType = HintenType;
        theItems[i].pathPoints[k].rightDirection = HintenLeft;
      }
    }
    if ( theItems[i].typename == "TextPath" && !theItems[i].locked && !theItems[i].parent.locked && !theItems[i].parent.layer.locked ) {

      pathLen = theItems[i].textPathObject.pathPoints.length;

      for ( k = 0; k < pathLen/2; k++ ) {
        h = pathLen-k-1;

        HintenAnchor = theItems[i].textPathObject.pathPoints[h].anchor;
        HintenLeft = theItems[i].textPathObject.pathPoints[h].leftDirection;
        HintenType = theItems[i].textPathObject.pathPoints[h].pointType;
        HintenRight = theItems[i].textPathObject.pathPoints[h].rightDirection;

        theItems[i].textPathObject.pathPoints[h].anchor = theItems[i].textPathObject.pathPoints[k].anchor;
        theItems[i].textPathObject.pathPoints[h].leftDirection = theItems[i].textPathObject.pathPoints[k].rightDirection;
        theItems[i].textPathObject.pathPoints[h].pointType = theItems[i].textPathObject.pathPoints[k].pointType;
        theItems[i].textPathObject.pathPoints[h].rightDirection = theItems[i].textPathObject.pathPoints[k].leftDirection;
        theItems[i].textPathObject.pathPoints[k].anchor = HintenAnchor;
        theItems[i].textPathObject.pathPoints[k].leftDirection = HintenRight;
        theItems[i].textPathObject.pathPoints[k].pointType = HintenType;
        theItems[i].textPathObject.pathPoints[k].rightDirection = HintenLeft;
      }
    }
  }
}

 

 

Let us know if this works for you.

Best regards
christype
christypeAuthor
Known Participant
June 15, 2020

Yeah thanks I had. It just reverses all the paths. I need to set all paths to the same direction. 

 

Tried hacking away at the script to set all pathPoints to right direction, but I don't know what I'm doing.

 

 

Charu Rajput
Community Expert
Community Expert
June 15, 2020

Hi,

Could you post Before and After screenshots or share ai file. I am not sure how direction will have effect on your paths. So, first I would like to see what you are trying to achieve here.

Best regards