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

Rotation and Translation depending on text inside item

Explorer ,
Dec 04, 2022 Dec 04, 2022

Copy link to clipboard

Copied

Hello everyone.

If the tile of this post isn't clear i'm sorry, let me explain what i'm trying to achieve.

In my document there's 1 layer containing 4 items: text, group1, group2, group3.

My script translates and rotates group1, group2 and group3. (see code below)

I need to make the rotation and translation values depend on the word inside item text. (let's say we have 3 sets of values)

 

Is there a way ti achieve that? Thanks in advance

 

 

 

 

var docRef = app.activeDocument;  
var layers = docRef.layers;  
var myLayer = layers["LAYERNAME"]; //this defines the layer that i want to get the selection from  

docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.  
for(var a=0;a<docRef.groupItems.length;a++){
     if (docRef.groupItems[a].name == "group1"){
     docRef.groupItems[a].selected = true;  
    }
}
docRef.selection[0].translate(678.1357,150.5897);

docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.  
for(var a=0;a<docRef.groupItems.length;a++){
     if (docRef.groupItems[a].name == "group2"){
     docRef.groupItems[a].selected = true;  
    }
}
docRef.selection[0].rotate(-180);
docRef.selection[0].translate(-1826.6742,+29.6273);

docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.  
for(var a=0;a<docRef.groupItems.length;a++){
     if (docRef.groupItems[a].name == "group3"){
     docRef.groupItems[a].selected = true;  

 

TOPICS
Scripting

Views

1.4K

Likes

Translate

Translate

Report

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
Guide ,
Dec 04, 2022 Dec 04, 2022

Copy link to clipboard

Copied

"I need to make the rotation and translation values depend on the word inside item text."

 

Can you give examples of what the words might be and how, depending on the words, you want to rotate/translate? 

Likes

Translate

Translate

Report

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 ,
Dec 04, 2022 Dec 04, 2022

Copy link to clipboard

Copied

Hi Femkeblanco and thanks for replying.

The words would be numbers or words, like these: 164, 152, 128, 116, S, M, L, XL, XXL.

So far i got into the script only one instance of rotate/translate, the one associated to the 116 scenario but basically what will change accordingly to text1 will be just the angle degree/distance values inside this parts of the script:

docRef.selection[0].rotate(-180);
docRef.selection[0].translate(-1826.6742,+29.6273);

 

I may have to specify: i will have x amount of PDF documents in which the items will have the same names but different contents so in one document the text1 item will contain "116", in another it will contain M, in another.. and so on

I want to batch-execute the script with all the combination word/group-modification inside of it so i dont' have to create one script for each documents and manually choose the appropriate one everytime.

 

I hope i explained myself but please ask if you need more info.

 

Thanks!

Likes

Translate

Translate

Report

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
Guide ,
Dec 04, 2022 Dec 04, 2022

Copy link to clipboard

Copied

You could use conditional statements (e.g. switch), or you could put your values in an object, as below.  This snippet targets the group "group1" in the layer "LAYERNAME", and manipulates it depending on the text in the same layer.  E.g. if the text is "164", it will rotate 45 degrees and translate 100 and  -100 points.

 

var values = {
    "164": {angle: 45,  deltaX: 100, deltaY: -100},
    "152": {angle: 90,  deltaX: 200, deltaY: -200},
    "128": {angle: 135, deltaX: 300, deltaY: -300}
};

var myLayer = app.activeDocument.layers["LAYERNAME"];
var text = myLayer.textFrames[0].contents;
myLayer.groupItems["group1"].rotate(values[text].angle);
myLayer.groupItems["group1"].translate(values[text].deltaX, values[text].deltaY);

 

Likes

Translate

Translate

Report

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 ,
Dec 04, 2022 Dec 04, 2022

Copy link to clipboard

Copied

Thank you for your reply. I don't know if it matters but i'll specify that text1 is not the only text item in the document but there will not be any double of those text1 value in the same document. Could it be a problem?

So if i get it right, with "put your values in an object" you are talking about the first part of the script (so "164:" is the object and the rest are the values). Sorry if the question is silly but as you can see i'm new on this topic, i just want to be sure to get it. Angle degrees and translations will be different for each singular group so those values has to be multiply for the number of text1 items and also for the number of group.. right?

The correct way to do it would be something like this?

 "164": {angle1: 45,  deltaX1: 100, deltaY1: -100, angle2: 45,  delta2: 100, deltaY2: -100, angle13: 45,  deltaX3: 100, deltaY3: -100, angle4: 45,  deltaX4: 100, deltaY4: -100},

(by the way, the groups will be more than 4, i reduced to that number just for convenience)

 

Does the name "angle", "deltaX" and "deltaY" mandatory? Or are just placeholder?

 

Sorry again if i wasn't precise in my explanation of the situation.

Thank you in advance for your reply.

Likes

Translate

Translate

Report

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
Guide ,
Dec 04, 2022 Dec 04, 2022

Copy link to clipboard

Copied

"text1 is not the only text item in the document ... Could it be a problem?"

In which case you will need to target it, e.g. by name.  If "text1" is its name, you can target it in the snippet above with

var text = myLayer.textFrames["text1"].contents;

 

"So if i get it right, with 'put your values in an object' you are talking about the first part of the script (so "164:" is the object and the rest are the values)."

An object is just a data structure, to structure your data. One of its benefits is that you can have objects within objects. So "values" is the name of an object; its value is everying between the curly brackets. "164" is the name of an object within the "values" object. "angle" is the name of a property (variable) within the "164" object; it's value is 45.

 

"Does the name "angle", "deltaX" and "deltaY" mandatory? Or are just placeholder?"

Property names are placeholders, but note that if a name is a number, you will not be able to use the dot notation (see below) to access its value.

 

"The correct way to do it would be something like this?"

That looks right.  To create an object (this is called an object literal):

var object1 = {
    "property1": 1,
    property2: 2
};
// To access the values of the properties,
// you can use either the dot or bracket notation:
alert(object1.property1); // 1
alert(object1["property2"]); // 2

A property can be an object, creating an object within an object:

var object1 = {
    object2: {property1: 1, property2: 2},
    object3: {property1: 3, property2: 4}
};
alert(object1.object2); // [object Object], i.e. the value is an object
alert(object1.object2.property1); // 1
alert(object1["object3"].property1); // 3

 

Likes

Translate

Translate

Report

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 ,
Dec 05, 2022 Dec 05, 2022

Copy link to clipboard

Copied

Hi Femkeblanco, thanks for your reply.

I will dig into it asap and let you know my result.

Unfortunately i discovered another issue in one of my previous script that prevent me to proceed with this one.

I'm going creat a new post about that issue (this one), hope you or others can help me with that too.

Thanks and have a nice day.

Likes

Translate

Translate

Report

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 ,
Dec 08, 2022 Dec 08, 2022

Copy link to clipboard

Copied

Hi @femkeblanco , i finally solved the other issue. In Visual Studio Code I tested this script:

 

var docRef = app.activeDocument;  
var layers = docRef.layers;  
var myLayer = layers["dima_var"]; //this defines the layer that you want to get the selection from  

var values = {
    "116": {angle: 45,  deltaX: 100, deltaY: -100},
    "128": {angle: 90,  deltaX: 200, deltaY: -200},
    "M": {angle: 135, deltaX: 300, deltaY: -300}
};

var myLayer = app.activeDocument.layers["dima_var"];
var text = myLayer.textFrames["SIZE"].contents;
myLayer.clippingItems["1"].rotate(values[text].angle);
myLayer.clippingItems["1"].translate(values[text].deltaX, values[text].deltaY);

but it gives me an error on the second-last line saying "undefined is not an object"

 

Note: i changed groupItems to clippingItems cause now the items i want to the modification to be applied to are clipping mask. Is that the proper JS term?

 

Here's a screenshot of my layers panel:

Cattura.JPG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The clipping masks i want to changes in terms of positions and angle are those inside the DESIGN group.

The text inside "SIZE" object is what determine the values of the rotation and translation.

 

Can you tell me what i'm doing wrong?

Thanks!

Likes

Translate

Translate

Report

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
Guide ,
Dec 08, 2022 Dec 08, 2022

Copy link to clipboard

Copied

Clipping masks are groupItems.  Also, you can't use numbers with bracket notation with built-in AI objects like groupItems, because they will be converted to numbers/indices.  So it should be something like 

myLayer.groupItems["DESIGN"].groupItems.getByName("1")....

Likes

Translate

Translate

Report

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 ,
Dec 08, 2022 Dec 08, 2022

Copy link to clipboard

Copied

Thanks @femkeblanco. Basing on your indications i put together this script:

 

var docRef = app.activeDocument;  
var layers = docRef.layers;  
var myLayer = layers["dima_var"]; //this defines the layer that you want to get the selection from  

var values = {
    "116": {modgroup1: {angle: 0, deltaX: 677.9106, deltaY: 152.8782},
            modgroup2: {angle: 180, deltaX: -1826.6732, deltaY: 29.5939},
            modgroup3: {angle: 0, deltaX: 1481.7264, deltaY: 2701.0337},
            modgroup4: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup5: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup6: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup7: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup8: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup9: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup10: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup11: {angle: 0, deltaX: 0, deltaY: -0},},
    "128": {modgroup1: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup2: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup3: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup4: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup5: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup6: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup7: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup8: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup9: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup10: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup11: {angle: 0, deltaX: 0, deltaY: -0},},
    "M": {modgroup1: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup2: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup3: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup4: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup5: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup6: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup7: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup8: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup9: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup10: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup11: {angle: 0, deltaX: 0, deltaY: -0},}
};

var myLayer = app.activeDocument.layers["dima_var"];
var text = myLayer.textFrames["SIZE"].contents;
myLayer.groupItems["DESIGN"].groupItems.getByName("1").rotate(values[text].modgroup1.angle);
myLayer.groupItems["DESIGN"].groupItems.getByName("1").translate(values[text].modgroup1.deltaX, values[text].modgroup1.deltaY);
myLayer.groupItems["DESIGN"].groupItems.getByName("2").rotate(values[text].modgroup2.angle);
myLayer.groupItems["DESIGN"].groupItems.getByName("2").translate(values[text].modgroup2.deltaX, values[text].modgroup2.deltaY);
myLayer.groupItems["DESIGN"].groupItems.getByName("3").rotate(values[text].modgroup3.angle);
myLayer.groupItems["DESIGN"].groupItems.getByName("3").translate(values[text].modgroup3.deltaX, values[text].modgroup3.deltaY);

 

and so far is working!

Maybe you spot some error that i will encounter going ahead?

I have a concern: some file will have 10 groups to be modified, others will have 11. So these lines

myLayer.groupItems["DESIGN"].groupItems.getByName("11").rotate(values[text].modgroup11.angle);
myLayer.groupItems["DESIGN"].groupItems.getByName("11").translate(values[text].modgroup11.deltaX, values[text].modgroup11.deltaY);

 are mandatory of course. But what will happen when the file will not have group 11? Will this invalidate the script or it will just be ignored? I tried deleting group 2 and run the script: it actually gave me error.

Likes

Translate

Translate

Report

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
Guide ,
Dec 08, 2022 Dec 08, 2022

Copy link to clipboard

Copied

If a targeted item does not exist, you will get an error message.  If you want to avoid this, you will need to handle the error with a try...catch statement.  To avoid repetition, you can put all this in a function. 

 

var values = {
    "116": {
        modgroup1: {angle: 0, deltaX: 677.9106, deltaY: 152.8782},
        modgroup2: {angle: 180, deltaX: -1826.6732, deltaY: 29.5939},
        modgroup3: {angle: 0, deltaX: 1481.7264, deltaY: 2701.0337}
    }/*, ...*/
};

var docRef = app.activeDocument;  
var layers = docRef.layers;  
var myLayer = layers["dima_var"];
var text = myLayer.textFrames["SIZE"].contents;
var design = myLayer.groupItems["DESIGN"];

function rotateAndTranslate (name, data) {
    try {
        design.groupItems.getByName(name).rotate(data.angle);
        design.groupItems.getByName(name).translate(data.deltaX, data.deltaY);
    } catch(e) {}
}
rotateAndTranslate("1", values[text].modgroup1);
rotateAndTranslate("2", values[text].modgroup2);
rotateAndTranslate("3", values[text].modgroup3);

 

Likes

Translate

Translate

Report

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 ,
Dec 09, 2022 Dec 09, 2022

Copy link to clipboard

Copied

Hi @femkeblanco , i made it like this

 

var docRef = app.activeDocument;  
var layers = docRef.layers;  
var myLayer = layers["dima_var"]; //this defines the layer that you want to get the selection from  
var text = myLayer.textFrames["SIZE"].contents;
var design = myLayer.groupItems["DESIGN"];

var values = {
    "116": {modgroup1: {angle: 0, deltaX: 677.9106, deltaY: 152.8782},
            modgroup2: {angle: 180, deltaX: -1826.6732, deltaY: 29.5939},
    "128": {modgroup1: {angle: 0, deltaX: 0, deltaY: -0},
    "M": {modgroup1: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup2: {angle: 0, deltaX: 0, deltaY: -0},
          modgroup3: {angle: 0, deltaX: 0, deltaY: -0},
};

function rotateAndTranslate (name, data) {
    try {
        design.groupItems.getByName(name).rotate(data.angle);
        design.groupItems.getByName(name).translate(data.deltaX, data.deltaY);
    } catch(e) {}
}
rotateAndTranslate("1", values[text].modgroup1);
rotateAndTranslate("2",  values[text].modgroup2);
rotateAndTranslate("NAME", values[text].modgroup2);
rotateAndTranslate("3", values[text].modgroup3);

 

...and it worked. But just now i realised i need to translate/rotate also the NAME and NUMBER objects, which are outside DESIGN.

I tried to move NAME and NUMBER inside DESIGN replacing

var design = myLayer.groupItems["DESIGN"];

with:

var num = activeDocument.layers.getByName("NUMBER");
var name = activeDocument.layers.getByName("NAME");

num.move(group, ElementPlacement.PLACEATBEGINNING);
name.move(group, ElementPlacement.PLACEATBEGINNING);

 but it get me error.

 

Then i tried to adjust the function so it takes the objects from the entire layer instead of the DESIGN GROUP doing this:

 

var docRef = app.activeDocument;  
var layers = docRef.layers;  
var myLayer = layers["dima_var"];
var text = myLayer.textFrames["SIZE"].contents;

function rotateAndTranslate (name, data) {
    try {
        myLayer.groupItems.getByName(name).rotate(data.angle);
        myLayer.groupItems.getByName(name).translate(data.deltaX, data.deltaY);
    } catch(e) {}
}
rotateAndTranslate("1", values[text].modgroup1);
rotateAndTranslate("2",  values[text].modgroup2);
rotateAndTranslate("NAME", values[text].modgroup2);
rotateAndTranslate("3", values...

but with no success.

 

Can you please enlighten me? Thanks!

Likes

Translate

Translate

Report

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 ,
Dec 09, 2022 Dec 09, 2022

Copy link to clipboard

Copied

Hi @femkeblanco i finally managed to get NAME and NUMBER inside GROUP (i changed "NAME" with "PLAYER1" to ot create confusion with Jsx terms). I still have problem to make those text items to be included in the function: they don't move nor rotate at all.

 

After some reserach i tried to replace this

var design = myLayer.groupItems["DESIGN"];

with this

var design = myLayer.pageItems["DESIGN"];

but it still doesn't work.

Do you have idea why?

 

Here's the entire script:

 

var docRef = app.activeDocument; 
var layers = docRef.layers;  
var myLayer = layers["dima_var"]; //this defines the layer that you want to get the selection from  
var text = myLayer.textFrames["SIZE"].contents;
var design = myLayer.pageItems["DESIGN"];

var values = {
    "116": {modgroup1: {angle: 0, deltaX: 677.9106, deltaY: 152.8782},
            modgroup2: {angle: 180, deltaX: -1826.6732, deltaY: 29.5939},
            modgroup2: {angle: 180, deltaX: -1826.6732, deltaY: 29.5939},
            modgroup3: {angle: 0, deltaX: 1481.7264, deltaY: 2701.0337},
            modgroup4: {angle: 90, deltaX: 2205.3819, deltaY: 1442.2805},
            modgroup5: {angle: 90, deltaX: 2204.7424, deltaY: 3494.2435},
            modgroup6: {angle: 270, deltaX: 739.01, deltaY: 2519.0489},
            modgroup7: {angle: 90, deltaX: 588.3199, deltaY: 2404.0039},
            modgroup8: {angle: 270, deltaX: 224.7501, deltaY: 2074.0039},
            modgroup9: {angle: 270, deltaX: -1241.7701, deltaY: 1243.62},
            modgroup10: {angle: 270, deltaX: -405.2302, deltaY: 1245.6251},
            modgroup11: {angle: 270, deltaX: 500, deltaY: -0},},
    "128": {modgroup1: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup2: {angle: 0, deltaX: 0, deltaY: -0},
            modgroup3: {angle: 0, deltaX: 0, deltaY: -0},  
};


function rotateAndTranslate (name, data) {
    try {
        design.groupItems.getByName(name).rotate(data.angle);
        design.groupItems.getByName(name).translate(data.deltaX, data.deltaY);
    } catch(e) {}
}
rotateAndTranslate("1", values[text].modgroup1);
rotateAndTranslate("2",  values[text].modgroup2);
rotateAndTranslate("PLAYER1", values[text].modgroup2);
rotateAndTranslate("3", values[text].modgroup3);
rotateAndTranslate("4", values[text].modgroup4);
rotateAndTranslate("5", values[text].modgroup5);
rotateAndTranslate("6", values[text].modgroup6);
rotateAndTranslate("7", values[text].modgroup7);
rotateAndTranslate("8", values[text].modgroup8);
rotateAndTranslate("9", values[text].modgroup9);
rotateAndTranslate("10", values[text].modgroup10);
rotateAndTranslate("11", values[text].modgroup11);

 

Thanks!

 

 

Likes

Translate

Translate

Report

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
Guide ,
Dec 10, 2022 Dec 10, 2022

Copy link to clipboard

Copied

From the screenshot of your layers panel, NAME and NUMBER are not pageItems.  I presume they are textFrames, although they could be pathItems or other items.  All items can be accessed through pageItems. 

 

If you want to use the function to manipulate pageItems that are inside DESIGN, change groupItems to pageItems in the function, like so

 

function rotateAndTranslate (name, data) {
    try {
        design.pageItems.getByName(name).rotate(data.angle);
        design.pageItems.getByName(name).translate(data.deltaX, data.deltaY);
    } catch(e) {}
}

 

 

If you want to use the function to manipulate pageItems that are inside and/or outside DESIGN (but in dima_var),

 

var docRef = app.activeDocument;
var layers = docRef.layers;
var myLayer = layers["dima_var"];
var text = myLayer.textFrames["SIZE"].contents;
var design = myLayer.groupItems["DESIGN"];

var values = {
    "116": {modgroup1: {angle: 0, deltaX: 677.9106, deltaY: 152.8782},
            modgroup2: {angle: 180, deltaX: -1826.6732, deltaY: 29.5939},
            modgroup2: {angle: 180, deltaX: -1826.6732, deltaY: 29.5939},
            // ...
            }
};

function rotateAndTranslate (name, data) {
    try {
        var item = docRef.pageItems.getByName(name);
        if (item.parent == myLayer)
            item = myLayer.pageItems.getByName(name);
        if (item.parent == design)
            item = design.pageItems.getByName(name);
        item.rotate(data.angle);
        item.translate(data.deltaX, data.deltaY);
    } catch(e) {}
}

rotateAndTranslate("1", values[text].modgroup1);
rotateAndTranslate("2", values[text].modgroup2);
rotateAndTranslate("NAME", values[text].modgroup2);
rotateAndTranslate("NUMBER", values[text].modgroup2);

 

 

Likes

Translate

Translate

Report

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 ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Hi @femkeblanco i finally managed to make it work but now i've encountered another issue.

This is a tricky one, hope you can solve the mistery!

I did a test with a different graphic and not all the items got the right move/rotation manipulation.

The problem, since it's the only difference from the previous testing files, has to be related to the graphic or the way it's composed (compound path vs clipping mask vs group...) inside the items subjected to the function (which they all are clipping mask).

 

Here are some file for testing: download

Inside this WeTransfer are: the script i use, the files before running the script and the results

Only those who have "OK" in the name worked as intended.

 

My guess is: even if the graphic is clipped inside the item subjected to the function, if it larger than the clipping mask it mess up the coordinates. Why? How? No idea at all.

 

Thanks in advance and have a nice day!

Cheers.

Likes

Translate

Translate

Report

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
Guide ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

The problem is with rotate(). Clipping masks, being groupItems, are rotated around the centre of the group, not the clipping path or visible artwork. So you will have to translate the difference. This should solve the problem for clipping masks rotated 180 degrees, which seems to be your present problem:

 

function rotateAndTranslate (name, data) {
    try {
       var item = design.groupItems.getByName(name);
       item.translate(data.deltaX, data.deltaY);
       if (data.angle == 180) {
             var x1 = item.pathItems[0].left;
             var y1 = item.pathItems[0].top;
             item.rotate(data.angle);
             var x2 = item.pathItems[0].left;
             var y2 = item.pathItems[0].top;
             item.translate(x1 - x2, y1 - y2);
       } else {
             item.rotate(data.angle);
       }
    } catch(e) {}
}

 

Edit:  I just saw that you do have clipping masks rotated 90 or 270 degrees in the other files; I'll ponder those tomorrow. 

 

 

Likes

Translate

Translate

Report

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
Guide ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

This should solve the problem for clipping masks rotated any degrees.

function rotateAndTranslate (name, data) {
    try {
        var item = design.groupItems.getByName(name);
        item.translate(data.deltaX, data.deltaY);
        var b1 = item.pathItems[0].geometricBounds;
        var x1 = b1[0] + ((b1[2] - b1[0]) / 2);
        var y1 = b1[1] + ((b1[3] - b1[1]) / 2);
        item.rotate(data.angle);
        var b2 = item.pathItems[0].geometricBounds;
        var x2 = b2[0] + ((b2[2] - b2[0]) / 2);
        var y2 = b2[1] + ((b2[3] - b2[1]) / 2);
        item.translate(x1 - x2, y1 - y2);
    } catch(e) {}
}

 

Likes

Translate

Translate

Report

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 ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

Hi @femkeblanco and thanks again for your support.

In my test file i placed only two situations but yes, i actually have several ratoting situations, exactly: 90. 180, 270, 150, 225, 45. No more than those (but if absolutely necessary i could try to avoid the last three).

 

I found online some similar attempts at modification, like "rotateAround on pathItem" or "transform" ...could they work as well? Could they be helpful? I have no idea how to implemen those tho.

 

Waiting for your reply, best regards!

 

 

 

Likes

Translate

Translate

Report

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
Guide ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

We seem to posted almost simultaneously.  Try the above. 

Likes

Translate

Translate

Report

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 ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

Yes we did 😉

I wanted to test it but suddently Illustrator stopped saving any updates on my files. This happens with files stored on google drive (i work directly on that server, so far no problems..). Anyway, i made a new post about this problem.

Damn, everytime there's a new problem... 😞

I'll keep you updated.

Cheers.

Likes

Translate

Translate

Report

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 ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

Hi @femkeblanco i have a question about your last implementation of the script. Does the content of the clipping mask that i want to rotate matter? I mean, if inside that clipping mask are only groups or some groups and other clipping mask or compund paths or groups and compund paths...(you get my point) ...the script works anyway?

This is crucial cause i have to plan very well how to create these clipping mask since the beginning.

Thansk in advance!

Have a nice day!

Likes

Translate

Translate

Report

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
Guide ,
Dec 14, 2022 Dec 14, 2022

Copy link to clipboard

Copied

LATEST

As long as the clipping path (the topmost item in the clipping mask) is a pathItem, which is usually the case, it should work.  

Likes

Translate

Translate

Report

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