Skip to main content
tt27079448
Inspiring
January 7, 2017
Answered

How to get the angle of an object ?

  • January 7, 2017
  • 5 replies
  • 7388 views

How to get the angle of an object ?

This topic has been closed for replies.
Correct answer Silly-V

If you are dealing with most types of art , you can check to see if the automatically-added "BBAccumRotation" tag is at work. This 'tag' is a scripting object that's not accessible from the UI, and likely serves some sort of Illustrator purpose. It is automatically added to art items when the rotation tools are used within AI to rotate them.

#target illustrator

function test(){

    var doc = app.activeDocument;

    var sel = doc.selection[0];

    if(sel.tags.length > 0 && sel.tags[0].name == "BBAccumRotation"){

      alert(sel.tags[0].value); //in radians

    }

};

test();

5 replies

Loic.Aigon
Legend
January 30, 2018

Hello,

Après une analyse plus profonde, il s'avère que l'objet en question était de type "GroupItem" bien qu'il ne soit qu'un tracé unique. En le dégroupant, l'objet PathItem intrinsèque dispose bien du tag BBAccumRotation et sa valeur est cohérente avec l'angle de l'objet.

Donc infine, ton code est probablement très fonctionnel pour autant que l'on s'adresse au bon objet

renél80416020
Inspiring
January 30, 2018

Hello, Un petite erreur dans le deuxième script que j'ai corrigée, (voir plus haut) ligne 13,39,56 accolade déplacée en 70. On a souvent des surprises avec les pdf non hybrides. J'ai à ce propos posté un nouveau sujet sur Mediabox page Illustrator Sélections différentes ? LR

Loic.Aigon
Legend
January 29, 2018

Oui je te confirme que même dernier snippet échoue (mais merci d'avoir proposé).

Ceci dit ela me fait penser que si je récupère les points et génère un nouvel objet à l'identique, peut-être ton snippet fonctionnera.

Loic.Aigon
Legend
January 24, 2018

Hi all,

It seems that in some cases, a rotated item ( a path item in my case ) won't host a BBAccumRotation tag. So all you have left is geometry).

I did write a function to retrieve angle for those shapes but it only work with rectangles as I rely on the 4 points to compute angles. I have absolutely no clue how an angle could be calculated with a non regular shape.

Hoping some brilliant math mind will hang around…

FWIW, my func for rectangle angle computation:

function getPathItemProperties ( pathitem )

{

var x1, x2, y1, y2, x4,y4, w2,w, h2,h, angleH, cos, factor, angle;

while ( pathitem.typename != "PathItem" )

{

pathitem = pathitem.pageItems[0];

}

x1 = pathitem.pathPoints[0].anchor[0];

y1 = pathitem.pathPoints[0].anchor[1];

x2 = pathitem.pathPoints[1].anchor[0];

y2 = pathitem.pathPoints[1].anchor[1];

x4 = pathitem.pathPoints[3].anchor[0];

y4 = pathitem.pathPoints[3].anchor[1];

w2 = Math.pow (  Math.abs ( x2 - x1 ), 2 )  + Math.pow (  Math.abs ( y2 - y1 ), 2 );

w = Math.sqrt (w2);

h2 = Math.pow (  Math.abs ( x4 - x1 ), 2 )  + Math.pow (  Math.abs ( y4 - y1 ), 2 );

h = Math.sqrt (h2);

angleH = Math.abs ( y2 - y1 );

cos = angleH/w;

factor = ( y2 > y1 )? -1 : 1;

angle = factor * ( Math.acos (cos)*180/Math.PI );

return { w:w*0.3528, h:h*0.3528, r:angle};

}

renél80416020
Inspiring
January 29, 2018

Bonjour à tous,
Pour Loïc :
Le fameux tag de nom "BBAccumRotation" est opérant sur tous type d'objet.
Sauf que pour certains objets comme une image placée par copier coller, un rectangle qui n'a pas encore subit de rotation...
Dans ces cas là, il suffit de créer le tag par tags.add(), le script qui suit fait ce travail si besoin est.
L'angle est orienté, il est donné par rapport à la direction verticale, dans l'exemple qui suit, l'objet est une instance de symbole, on obtient le même résultat après un "Rompre le lien avec le symbole" (objet groupés).
Cet angle est mis à jour à chaque nouvelle rotation (AccumRotation).

exemple :

Grand merci à Sily pour avoir abordé ce sujet, car moi aussi je me suis cassé la tète sur ce problème d'orientation sans y parvenir

//

// JavaScript Document for Illustrator
// Finds the tags of name "BBAccumRotation" associated with the selected art item,
// angle to the vertical direction
// of  Sily V modif elleere
#target illustrator

function getAngle(){
    var doc = activeDocument;

    var angle;
    if (selection.length == 0) return null;
    var sel = doc.selection[0];

    var nb = sel.tags.length; //alert(nb)
    if (!nb) {
      if (!confirm ("Create a new Tag with the name BBAccumRotation y/n ?",false,"De Elleere")) return null;
      var BBAccumRotationTAG = sel.tags.add();
          BBAccumRotationTAG.name = "BBAccumRotation";
          BBAccumRotationTAG.value = 0;
          nb = 1;
    }

      do {
          if (sel.tags[nb-1].name == "BBAccumRotation" ) {
            angle = sel.tags[nb-1].value*1; //in radians
            angleRad = angle.toFixed(5).replace(/.00000/,"");;
            angleDeg = angle*180/Math.PI;  //in degrees
            angleDeg = angleDeg.toFixed(2).replace(/.00/,"");
            alert("méthode tags\nangleRad = "+angleRad+" rd\nangleDeg = "+angleDeg+"°");
            nb = 0
          } else{nb--;}
      }
      while (nb);
      return angle;
}

  getAngle();

Loic.Aigon
Legend
January 29, 2018

Hi,

renél80416020 

In this snippet, you assume the object isn't rotated explaining the lack of a BBAccumRotation tag. So you add a tag with a 0 rotation angle.

My problem is that when you work with PDF files, even "rotated" object may not have such a tag. See attached screenshots:

Which is wrong obviously.

Unless I missed something?

Silly-V
Silly-VCorrect answer
Legend
January 7, 2017

If you are dealing with most types of art , you can check to see if the automatically-added "BBAccumRotation" tag is at work. This 'tag' is a scripting object that's not accessible from the UI, and likely serves some sort of Illustrator purpose. It is automatically added to art items when the rotation tools are used within AI to rotate them.

#target illustrator

function test(){

    var doc = app.activeDocument;

    var sel = doc.selection[0];

    if(sel.tags.length > 0 && sel.tags[0].name == "BBAccumRotation"){

      alert(sel.tags[0].value); //in radians

    }

};

test();

tt27079448
Inspiring
January 8, 2017

thanks very much

function test(){

    var doc = app.activeDocument;

    var sel = doc.selection[0];

    if(sel.tags.length > 0 && sel.tags[0].name == "BBAccumRotation"){

      alert("Angle : "+180 *sel.tags[0].value/Math.PI); //in radians

    }

};

test();

Inspiring
January 7, 2017

For objects which have "matrix" property(images, textframes), the math can be:

var matrix = app.selection[0].matrix;

alert(180/Math.PI * Math.atan2(matrix.mValueC, matrix.mValueD)); 

tt27079448
Inspiring
January 8, 2017

thanks very much