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

[JS CS3] Touching Objects

New Here ,
Nov 11, 2009 Nov 11, 2009

Copy link to clipboard

Copied

Hi

Is there a way to obtain all objects who are touching a certain frame on a page?

Thanks for answers

Stefan Huwiler

TOPICS
Scripting

Views

1.5K

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

correct answers 1 Correct answer

Guide , Nov 11, 2009 Nov 11, 2009

Stefan Huwiler wrote:

I was thinking about such a way too, but was interested if there is maybe another way.

I thought InDesign internally knows what objects are overlapping or upon another.

Thanks für the answers.

It's not so simple. The 'bounds' property deals with bounding box disregarding the inner path of the object (polygons, ovals, rotated or sheared frames).

1) If 2 bounding box have no intersection, you are sure that the 2 underlying objects don't intersect.

but

2) If 2 bounding box intersect,

...

Votes

Translate

Translate
Guest
Nov 11, 2009 Nov 11, 2009

Copy link to clipboard

Copied

Hi,

if the objects are rectengular, you could try to compare the geometric bounds of the objects with the frame.

Regards,

Franziska

Votes

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
Community Expert ,
Nov 11, 2009 Nov 11, 2009

Copy link to clipboard

Copied

That's the easy case -- see below. "Touching" is a bit .. touchy. If you place two rectangles side-by-side on the same guideline, this script will not always sense the touch. Perhaps the positions differ in the 4th decimal.

However, giving a little leeway in the "touches" function below is not a problem (deciding how much is).

This script deselects your current selection and instead selects all objects that "touch" it.

frame = app.selection[0];
frame.select (SelectionOptions.REMOVE_FROM);

page = frame.parent;
if (!(page instanceof Page))
{
alert ("That's not on a page..");
exit();
}

for (allitems = 0; allitems<page.pageItems.length; allitems++)
if (frame != page.pageItems[allitems] && touches (frame.geometricBounds, page.pageItems[allitems].geometricBounds))
  page.pageItems[allitems].select (SelectionOptions.ADD_TO);

// geometricBounds: [ty lx by rx]
function touches(bounds1, bounds2)
{
if (bounds1[2] < bounds2[0]) return false;
if (bounds1[0] > bounds2[2]) return false;
if (bounds1[1] > bounds2[3]) return false;
if (bounds1[3] < bounds2[1]) return false;
return true;
}

Votes

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
New Here ,
Nov 11, 2009 Nov 11, 2009

Copy link to clipboard

Copied

I was thinking about such a way too, but was interested if there is maybe another way.

I thought InDesign internally knows what objects are overlapping or upon another.

Thanks für the answers.

Votes

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 ,
Nov 11, 2009 Nov 11, 2009

Copy link to clipboard

Copied

Stefan Huwiler wrote:

I was thinking about such a way too, but was interested if there is maybe another way.

I thought InDesign internally knows what objects are overlapping or upon another.

Thanks für the answers.

It's not so simple. The 'bounds' property deals with bounding box disregarding the inner path of the object (polygons, ovals, rotated or sheared frames).

1) If 2 bounding box have no intersection, you are sure that the 2 underlying objects don't intersect.

but

2) If 2 bounding box intersect, you are not sure that the 2 underlying objects intersect.

In a recent project I had to check for touching polygons.

The 'bounds' approach provides a quick diagnostic when the conclusion is "no intersection":

/*bool*/ Polygon.prototype.collideBounds = function(/*Polygon*/ _poly)
//--------------------------------------------------------------------
// Ret. TRUE if the bounding boxes intersect, else return FALSE

// quick method, but TRUE does not mean the shapes actually intersect
{
var b = this.visibleBounds;
var _b = _poly.visibleBounds;
return( !((b[0]>_b[2]) || (b[2]<_b[0]) || (b[1]>_b[3]) || (b[3]<_b[1])) );
}

When p1.collideBounds(p2) is TRUE, I need to invoke another method to refine the test. Then I use the intersectPath method, which is dramatically slower:

/*bool*/ Polygon.prototype.collideShapes = function(/*Polygon*/ _poly)
//--------------------------------------------------------------------
// Ret. TRUE if the shapes intersect

// slow method

{
try     {
     this.intersectPath(_poly);
     app.activeDocument.undo();
     return(true);
     }
catch(ex)
     {
     return(false);
     }
}

As shown above, the point is that p1.intersectPath(p2) throws an error when the 2 shapes don't intersect.

Hope it could help you.

@+

Marc

Votes

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
LEGEND ,
Nov 12, 2009 Nov 12, 2009

Copy link to clipboard

Copied

LATEST

Yes. Objects of arbitrary shape can be pretty hard to check.

If you need a quicker method than intersectPath, you can compare them

using the entirePath property of the paths of the two objects, but

that's not a job for the faint hearted or someone who dislikes

math...

Harbs

Votes

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