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

Auto-arrange objects by rounded Y position

Explorer ,
Jan 23, 2019 Jan 23, 2019

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

              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[0]-dec && newtop < TabTop[0]+dec) { 

                iObj = selectedItems[TabTop[1]]; 

                iObj.move(docRef.layers[0],ElementPlacement.PLACEATEND); 

                docRef.layers[0].name = "Rang "+k; 

              } 

              else { 

                newtop = TabTop[0]; k++; i--; 

                docRef.layers.add(); 

              } 

            } 

      var layerAct, nbObjLayer, n, nObj, j, jObj; 

      var TabLeft = []; 

          for(i = 0; i < k; i++) { 

            layerAct = docRef.layers

            nbObjLayer = layerAct.pageItems.length; 

              for(n = 0; n < nbObjLayer; n++) { 

                nObj = layerAct.pageItems

                TabLeft.push([nObj.left,nObj]); 

              } 

            TabLeft.sort(); //TabLeft.revers(); alert(TabLeft.join("\r")); 

              for(j = 0; j < nbObjLayer; j++) { 

                jObj = TabLeft[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

TOPICS
Scripting
2.4K
Translate
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

Advocate , Jan 24, 2019 Jan 24, 2019

Bonjour Dever

J'ai essayé toutes les adresses mail que tu m'as transmises sans résultat.

Mon adresse est sous ma photo.

Dans le script, c'est juste la fonction sort() qui n'est pas adaptée.

Le Cloud d'Orange

René

Translate
Adobe
Advocate ,
Jan 24, 2019 Jan 24, 2019

Bonjour Dever

J'ai essayé toutes les adresses mail que tu m'as transmises sans résultat.

Mon adresse est sous ma photo.

Dans le script, c'est juste la fonction sort() qui n'est pas adaptée.

Le Cloud d'Orange

René

Translate
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 ,
Jan 25, 2019 Jan 25, 2019

I have modified it little and it works! Thank you

Translate
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 ,
Jan 30, 2019 Jan 30, 2019

Bonjour renél80416020

If I have other layer in document script freezes (Ai crushes)

I have added "put back to original Layer" function and removing all Rang layers

How to fix it to work only with selected layer in the document?

// JavaScript Document for Illustrator

// tab Top.js

// contitution de lignes du bas vers le haut et trie par ligne de la droite vers la gauche

var dec = 30; // Tolérance de position (top) pour appartenir à une ligne

var ognlayerName;

if (app.documents.length > 0) {

    var docRef = app.activeDocument;

    ognlayerName = new String(docRef.activeLayer.name);

    //alert(ognlayerName);

    var selectedItems = selection;

    var nbObjSelect = selectedItems.length;

    if (nbObjSelect) {

        var rep, iObj, newtop;

        var TabTop = [];

        rep = prompt("Tolerance?", dec);

        dec = rep * 1;

        for (var i = 0; i < nbObjSelect; i++) {

            iObj = selectedItems;

            TabTop.push([iObj.top, i])

        }

        bsort(TabTop, 0, TabTop.length - 1);

        //alert(TabTop.join("\r"));

        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[0] - dec && newtop < TabTop[0] + dec) {

                iObj = selectedItems[TabTop[1]];

                iObj.move(docRef.layers[0], ElementPlacement.PLACEATEND);

                docRef.layers[0].name = "Rang " + k;

            } else {

                newtop = TabTop[0];

                k++;

                i--;

                docRef.layers.add();

            }

        }

        var layerAct, nbObjLayer, n, nObj, j, jObj;

        var TabLeft = [];

        for (i = 0; i < k; i++) {

            layerAct = docRef.layers;

            nbObjLayer = layerAct.pageItems.length;

            for (n = 0; n < nbObjLayer; n++) {

                nObj = layerAct.pageItems;

                TabLeft.push([nObj.left, nObj]);

            }

            bsort(TabLeft, 0, TabLeft.length - 1); //alert(TabLeft.join("\r"));

            for (j = 0; j < nbObjLayer; j++) {

                jObj = TabLeft[1];

                jObj.zOrder(ZOrderMethod.BRINGTOFRONT);

            }

            TabLeft = [];

        }

        count = 0;

        if (docRef.pathItems.length > 0) {

            thePaths = docRef.pathItems;

            numPaths = thePaths.length;

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

                pathArt = docRef.pathItems;

                if (pathArt.parent.typename != "CompoundPathItem") {

                    pathArt.selected = true;

                    count++;

                }

            }

        }

        //DESCRIPTION: move sorted to original layer

        var docSelected = app.activeDocument.selection;

        var topLayer = app.activeDocument.layers[ognlayerName];

        //alert(topLayer);

        //var topLayer = app.activeDocument.layers.add();

        //topLayer.name = "Sorted Bleed";

        for (s = 0; s < docSelected.length; s++) {

            myPath = docSelected;

            myPath.move(topLayer, ElementPlacement.PLACEATEND);

        }

        //DESCRIPTION: remove sorted Rang layers

        var layersDeleted = 0;

        var targetDocument = app.activeDocument;

        var layerCount = targetDocument.layers.length;

        // Loop through layers from the back, to preserve index

        // of remaining layers when we remove one

        for (var ii = layerCount - 1; ii >= 0; ii--) {

            targetLayer = targetDocument.layers[ii];

            var layerName = new String(targetLayer.name);

            if (layerName.indexOf("Rang") == 0) {

                targetDocument.layers[ii].remove();

                layersDeleted++;

            }

        }

    } else alert("You have not selected anything!", "Warning");

} else alert("For execution of this script a document must be opened! ", " Script Alert!");

//------

function bsort(Tab, debut, fin) { // sort an ascending order array from start to end

    var i, k, buffer;

    for (i = fin; i != 0; i--) {

        for (k = debut; k < i; k++) {

            if (Tab[0] > Tab[k + 1][0]) {

                buffer = Tab;

                Tab = Tab[k + 1];

                Tab[k + 1] = buffer;

            }

        }

    }

}

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

thanks in advance

Translate
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
Advocate ,
Feb 01, 2019 Feb 01, 2019

Bonjour Dever,

J'aimerais connaître le processus de création des tracés et comment les tracés classés sont exploitée par la suite ?

cela peu aider pour trouver des solutions pour des cas plus complexes.

Bien ton script fonctionne, je donne ma version...

Le Cloud d'Orange

de René landry

elleere

PS essai de me joindre par mail.

Translate
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 ,
Feb 01, 2019 Feb 01, 2019

your version works, but with two layers in document it will stuck.

Screenshot_1.png

PS

PM me your email PLEASE. I'm not seeing any contact information in your profile

Thanks in advance!

Translate
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
Advocate ,
Feb 02, 2019 Feb 02, 2019

Bonjour dever,

Oui effectivement, je m'étais basé que sur ton exemple

Il te suffit de modifier la ligne 30 k =1 cela devrait te convenir.

for(i = 0, k = 1; i < nbObjSelect; i++) {

Pour l'adresse mail (passe le cuseur de la souris sur le portrait (René Landry)

Cordialement René

Translate
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 ,
Feb 03, 2019 Feb 03, 2019

Bonjour, Rene

thanks, now works

PS

Only you are seeing detail info in your profile page

Screenshot_1.png

Translate
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
Advocate ,
Feb 07, 2019 Feb 07, 2019

Bonjour Dever,

Moi j'ai çà ?

Translate
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
Community Expert ,
Feb 07, 2019 Feb 07, 2019
LATEST

Dever  schrieb

Only you are seeing detail info in your profile page

Dever​ is absolutly right.

This depends on your own privacy settings in your fora profile.

Standard setting is: only you see this private info.

Real name: only you can see

Email address: only you can see

For changing:

Click on the little arrow right beside your avatar (at the top on this page, near: search).

Choose: edit profile

Choose: the third register (privacy)

There are your privacy settings.

Translate
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