Quantcast
Channel: Adobe Community : Popular Discussions - Illustrator Scripting
Viewing all articles
Browse latest Browse all 12845

Auto-arrange objects by rounded Y position

$
0
0

Hi! Can someone help me? I want to sort objects on airboard from bottom to top by rounded Y for simplify sorting process. Rounding will create virtual rows (different color on pic below) highest value goes first (Top object in the layer list). Then each row I want to sort by X, from right to the left (so high value goes first - revers order)

autosave_CUT.jpg

Here is example

Bars+1_cut.jpg

Result of arranging objects in the layer should be (from top to down): 1,2,3,4,5,6 and etc

This is like sorting 2 dimensional array in revers order:

X,Y array

myobjlist= [[124,2],[23,2],[60,1],[150,1],[12,2],[20,0],[50,1],[40,0],[70,0]]

sorted(myobjlist)

will create next array
[[124,2] [23,2][12,2] [150,1] [60,1] [50,1] [70,0] [40,0] [20,0]]

 

Object with[124,2] coordinates will be first in layers list and [20,0] - last

 

Example script that doing this but with some mistakes

// JavaScript Document for Illustrator  
var dec = 20;  
if (app.documents.length > 0) {    var docRef = app.activeDocument;    var selectedItems = selection;    var nbObjSelect = selectedItems.length;      if (nbObjSelect) {        var rep, iObj, newtop;        var TabTop = [];            rep = prompt("Tolérance ?", dec);            dec= rep*1;              for (var i = 0; i < nbObjSelect; i++) {                iObj = selectedItems[i];                TabTop.push([iObj.top,i])              }            TabTop.sort();            TabTop.reverse(); //alert(TabTop.join("\r"));            newtop =  TabTop[0][0];            docRef.layers.add();            nbLayers = docRef.layers.length;              for(i = 0, k = nbLayers-1; i < nbObjSelect; i++) {                if (newtop > TabTop[i][0]-dec && newtop < TabTop[i][0]+dec) {                  iObj = selectedItems[TabTop[i][1]];                  iObj.move(docRef.layers[0],ElementPlacement.PLACEATEND);                  docRef.layers[0].name = "Rang "+k;                }                else {                  newtop = TabTop[i][0]; k++; i--;                  docRef.layers.add();                }              }        var layerAct, nbObjLayer, n, nObj, j, jObj;        var TabLeft = [];            for(i = 0; i < k; i++) {              layerAct = docRef.layers[i];              nbObjLayer = layerAct.pageItems.length;                for(n = 0; n < nbObjLayer; n++) {                  nObj = layerAct.pageItems[n];                  TabLeft.push([nObj.left,nObj]);                }              TabLeft.sort(); //TabLeft.revers(); alert(TabLeft.join("\r"));                for(j = 0; j < nbObjLayer; j++) {                  jObj = TabLeft[j][1];                  jObj.zOrder(ZOrderMethod.SENDTOBACK);                }              TabLeft = [];            }      }      else alert("Vous n'avez rien sélectionné !","De Elleere");  
}  
else alert("Pour l'exécution de ce sript un document doit être ouvert !","Script Alerte de Elleere !");

Thanks to renél80416020

Tolerance value is fine. But there is some problems:

  1. It creates last layer (virtual row) in top of the list.
  2. Each object in the row should be sorted from right to left
  3. it creates layers with objects. In finale will be good if all sorted objects will be in one layer

see pic below

Test1-Simple.jpg


Viewing all articles
Browse latest Browse all 12845

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>