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

Confused on creating a rectangle and validating its size & position afterwards

Participant ,
Apr 10, 2015 Apr 10, 2015

If one creates a rectangle in a layer like this:

var childLayer = parentLayer.pathItems.rectangle(top, left, someWidth, someHeight);

childLayer.stroked = false;

childLayer.filled = true;

After creating the rectangle, would one read it back like this:

var bounds = childLayer.geometricBounds;

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]);

or should I use visibleBounds, or controlBounds? Or something else?

I know if stroke not used or can exclude can use geometric bounds? Or rather all 3 bounds could be the same in that case?

I'm confused right now because I'm assuming the bounds represent the four corners of the rectangle's position in the document and when read back out, the values (bounds[0] and bounds[1]) don't match against the top & left values originally passed in to create the rectangle. Shouldn't they be the same?

If not, how am I supposed to validate that the layer/object uses the same top & left values for it's position as when the rectangle was created (for say validation purposes)?

Also I assume I should be able to fetch the position of one corner using childLayer.top & childLayer.left as well right? For some reason, that doesn't match with top & left either.

TOPICS
Scripting
883
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
Adobe
Guide ,
Apr 10, 2015 Apr 10, 2015

it all worked fine for me...

I tested with this.

var doc = app.activeDocument;

var Top = 0;

var Left = 50;

var Width = 20;

var Height = 100;

var rect = doc.pathItems.rectangle(Top,Left,Width,Height);

rect.stroked = false;

rect.filled = true;

var bounds = rect.geometricBounds;

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]);

the alert reports: "bounds: 50,0,70,-100"

so your bounds are as follows:

bounds[0] = Left = 50

bounds[1] = Top = 0

bounds[2] = Right = 70 (Left + Width)

bounds[3] = Bottom = -100 (Top - Height)

if this is not the case for you I would like to know what version your are using.

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
Guide ,
Apr 10, 2015 Apr 10, 2015

as far as geometric vs visible vs control...

var doc = app.activeDocument; 

var Top = 0; 

var Left = 50; 

var Width = 20; 

var Height = 100; 

var rect = doc.pathItems.rectangle(Top,Left,Width,Height); 

rect.stroked = true;

rect.strokeWidth = 10;

rect.filled = true; 

var bounds = rect.geometricBounds;  

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]); 

var bounds = rect.visibleBounds; 

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]); 

var bounds = rect.controlBounds;  

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]); 

you can see that geometric does not include stroke while visible does.

Control measures the control box.

although that is not what the documentation says...

controlBounds

array of 4 numbers Read-only.

The bounds of the object including stroke width and controls.

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
Participant ,
Apr 13, 2015 Apr 13, 2015

Using your code snippet, I get same values. I'm using Adobe Illustrator CS6 (64-bit).

But using a slightly different snippet, adapted from original script code I'm working with:

var doc = app.activeDocument;

var finishedWidth = 6.74 * 72;

var finishedHeight = 4.84 * 72;

var top = (362.88 - finishedHeight) / 2;

var left = (502.56 - finishedWidth) / 2;

alert("orig left: "+left+", top: "+top+", width: "+finishedWidth+", height: "+finishedHeight);

var trimLayer = doc.pathItems.rectangle(top, left, finishedWidth, finishedHeight);

alert("done left: "+trimLayer.left+", top: "+trimLayer.top);

var bounds = trimLayer.geometricBounds;

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]);

the results are off regarding trimLayer.left & trimLayer.right compared to the original top & left values passed in. But the bounds values are correct. From your code snippet, I checked the rect.left and rect.top & it's still correct.

Here's the output of the alerts (with some rounding of values as opposed to what was displayed):

orig left: 8.64, top: 7.19, width: 485.28, height: 348.48

done left: 8.14, top: 7.7

bounds: 8.64,7.2,493.92,-341.28

and debugging the original script code, the script I'm working with, (the rect.left, rect.top and) the bounds don't match up, with a difference from 0.14 to 0.42 to the actual value it's supposed to be. Extracting the area of code that I'm debugging out (for generalization and sharing code) results in the snippet above, and for some reason there, outside the context of the full script, the bounds are correct but not the rect.left & rect.top. Unfortunately, can't share the rest of the code at present.

Guess I'll have to investigate further, with help from my team's developers. At the moment I'm perplexed by these variations. Logically, from reading the code & scripting reference, I would have expected the values to match up in all cases.

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
Participant ,
Apr 13, 2015 Apr 13, 2015

Figured the problem with rect.left and rect.top. I forgot to specifically set stroke value to false, and looks like default is true, so that threw off those values, which account for stroke if it exists I guess.


Still need to figure out the issue in the original script though. Thanks for the help thus far.

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
Guide ,
Apr 13, 2015 Apr 13, 2015
LATEST

Remember that it does not measure Clipped size but the whole thing

clip.JPG

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