5 Replies Latest reply on Nov 12, 2009 3:35 AM by Harbs.

    [JS CS3] Touching Objects

    Stefan Huwiler Level 1

      Hi

       

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

       

       

      Thanks for answers

       

      Stefan Huwiler

        • 1. Re: [JS CS3] Touching Objects
          franzi_ska

          Hi,

           

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

           

          Regards,

           

          Franziska

          1 person found this helpful
          • 2. Re: [JS CS3] Touching Objects
            [Jongware] Most Valuable Participant

            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;
            }
            
            1 person found this helpful
            • 3. Re: [JS CS3] Touching Objects
              Stefan Huwiler Level 1

              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.

              • 4. Re: [JS CS3] Touching Objects
                Marc Autret Level 4

                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

                • 5. Re: [JS CS3] Touching Objects
                  Harbs. Level 6

                  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