Sorting the order of points in a pathPoints array


okay, why want this? well, i've got areatext rectangles created various means, , 0 index top left, other times bottom right. need lot of resizing/aligning/spacing/sizing. script movement of top left point of each (or point matter), i'm trying clean sequence of points in each objects array of pathpoints can move pathpoint[0].anchor each rectangle , sure top left moving, not other random corner.

 

so, testing in script alone, , came following (don't laugh @ how called out array), works great in extendscript:

 

var testarray = new array;

testarray.push({xval: 5, yval:0});

testarray.push({xval: 0, yval:-2});

testarray.push({xval: 5, yval:-2});

testarray.push({xval: 0, yval:0});

 

function primarysort(){  testarray.sort(function (a,b){return b.yval-a.yval});   }

 

function secondarysort(){

    //reorder first pair of points  based on xval

       if (number(testarray[0].xval) > number(testarray[1].xval)){

           moveme = testarray[1];

           testarray.splice(1,1);

           testarray.unshift(moveme);}

       // reorder second pair of points  based on xval

              if (number(testarray[2].xval) < number(testarray[3].xval)){

           moveme = testarray[2];

           testarray.splice(2,1);

           testarray.push(moveme);}    }

 

function printarray(){

for (i =0; i<testarray.length; i++){

   var writeme = testarray[i].xval.tostring()+ "," + testarray[i].yval.tostring() ;

   $.writeln (writeme);  }}


primarysort();

printarray();

$.writeln (".........");

secondarysort();

printarray();

 

so, tried same illustrator:

 

var objarray = app.activedocument.textframes;

var objpoints = new array;

 

for (i=0; i<objarray.length ; i++){

var pathobj = objarray[i].textpath;

objpoints = pathobj.pathpoints;

xypairsort();}

     

function xypairsort(){

   

    //writes current sequence of pathpoint anchor x y coordinates console

for (i =0; i<objpoints.length; i++){

var writeme = objpoints[i].anchor[0].tostring()+ "," + objpoints[i].anchor[1].tostring() + "," + ;

   $.writeln (writeme);}

   

    //sort 4 points top , bottom pairs

    objpoints.sort(function (a,b){return b.anchor[1]-a.anchor[1]});

   

    //reorder first pair of points  based on xval

       if (number(objpoints[0].anchor[0]) > number(objpoints[1].anchor[0])){

           moveme = objpoints[1];

           objpoints.splice(1,1);

           objpoints.unshift(moveme);}

       // reorder second pair of points  based on xval

    if (number(objpoints[2].anchor[0]) < number(objpoints[3].anchor[0])){

           moveme = objpoints[2];

           objpoints.splice(2,1);

           objpoints.push(moveme);}

      

       //writes new sequence of pathpoint anchor x y coordinates console

   (i =0; i<objpoints.length; i++){

   var writeme = objpoints[i].anchor[0].tostring()+ "," + objpoints[i].anchor[1].tostring() + "," + ;

   $.writeln (writeme);}

        }

 

which throws error "objpoints.sort not function"

unlike js version, ai script trying sort of points of of of textareas, i'll fix shortly sorts 4 points of each object. problem script isn't sorting points, providing helpful error message instead.

 

any ideas? internet searches error don't explain why happens, other ways of doing sorts.

where function objpoints.sort() ?



More discussions in Illustrator Scripting


adobe

Comments

Popular posts from this blog

How to set the order of FAQs instead of alphabetical

Thread: Get UK Keyboard working

how do I change the e-mail address for my merchant account