7 Replies Latest reply on Mar 12, 2013 2:36 PM by Jump_Over

    coordinates as related to the page

    bduffy323 Level 1

      Hello all, I am working in CS 5.5 and need to get the coordinates of a box as it relates to its page. I am having trouble with the case of two pages side by side like this:

      Capture.PNG

      I understand that pageItem.geometricBounds works in its own coordinate space but how can I adjust it to be relative to the page. In this situation I am hoping to get for the top left anchor point -x1,y1 and for the bottom righ anchor point x2,y2. The same that I would expect from this:

      Capture.PNG

      Here is the quick code I wrote to get the geomtric bounds of the selected item. Any explanation would be great!

       

       

      var myItem = app.selection[0]; //just a rectangle that I am moving to different parts of the page
      
      
      var temp = app.activeDocument.viewPreferences.rulerOrigin;
      app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
      
      var currentBounds = myItem.geometricBounds;
      
      //alert(myItem.resolve(AnchorPoint.TOP_LEFT_ANCHOR,CoordinateSpaces.SPREAD_COORDINATES) +"  "+myItem.resolve(AnchorPoint.BOTTOM_RIGHT_ANCHOR,CoordinateSpaces.SPREAD_COORDINATES))
      alert(currentBounds[1]+","+currentBounds[0] + " " + currentBounds[3] +","+currentBounds[2])
      
      
      app.activeDocument.viewPreferences.rulerOrigin = temp;
      
      
        • 1. Re: coordinates as related to the page
          MrTIFF Level 3

          I'll give it a try:  every PageItem is always owned by just one page.  (Or no page, for items entirely on the pasteboard.)

           

          In your top example, the rect that crosses the spine, the rect is owned by the left page. So the GeometricBounds will be in terms of the left page's origin.

           

          Expanding on your "quick code" a bit, this will find all of the top-level pageItems, for each page in the doc:

           

          /**

          * geometric bounds of pageItems

          */

          var doc = app.activeDocument;

          var originalOrigin = doc.viewPreferences.rulerOrigin;

          doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;

          var np = doc.pages.length;

          for (ix = 0; ix < np; ix++)

          {

                    var pg = doc.pages.item(ix);

                    app.activeWindow.activePage = pg;

                    var ni = pg.pageItems.length;

                    for (jx = 0; jx < ni; jx++)

                    {

                              var myItem = pg.pageItems[jx];

                              var gb = myItem.geometricBounds;

                              //alert(myItem.resolve(AnchorPoint.TOP_LEFT_ANCHOR,CoordinateSpaces.SPREAD_COOR DINATES) +"  "+myItem.resolve(AnchorPoint.BOTTOM_RIGHT_ANCHOR,CoordinateSpaces.SPREAD_COORDINATES))

                              alert("pg"+ix+": "+gb[1].toFixed(1) +","+gb[0].toFixed(1) + "  " + gb[3].toFixed(1) +","+gb[2].toFixed(1));

                    }

          }

          doc.viewPreferences.rulerOrigin = originalOrigin;

           

          Cheers,

          • 2. Re: coordinates as related to the page
            bduffy323 Level 1

            What I did was copied your code and then moved my box so that 10% of it was on the left page and 90% of it was on the right page. When I ran your code it said that the box belonged to the right page but still gave me the x coordinate based on the first page. It seems to me that indesign assigns the page item to the page that is majorily on and in your example you must have been mostly on the first page and not the second. I should have been more clear in my example. Try to run your code again with something that looks more like this:

            Capture.PNG

            • 3. Re: coordinates as related to the page
              Jump_Over Level 5

              Hi,

               

              After setting rulerOrigin to Page use:

              app.activeDocument.zeroPoint = [0,0];

               

              This should ensure your page left/up corner is [0,0]

               

              Notice that setting rulerOrigin to Page_Origin doesn't means to set item coordi relation to page. Item geoBound are related to ruler which zeroPoint (document property) can be related to page left/up corner... or not.

               

              On the other hand, you could define an object prototype, i.e:

               

              Object.prototype.myGeoBounds = function () {

                  if(this.parentPage==null) return false;

                  var gb = this.parentPage.bounds;

                  var GB = this.geometricBounds;

                  var my = GB[0] - gb[0];

                  var mx =  GB[1] - gb[1];

                  var mY =  GB[2] - gb[0];

                  var mX =  GB[3] - gb[1];

                  return [my,mx,mY,mX]

                  }

               

              so, pageItem.myGeoBounds() should return page_relative (left top) coorinates regardless of zeroPoint position. (it is false if item is placed on pasteboard).

               

              are you a bit closer?

               

              Jarek

              • 4. Re: coordinates as related to the page
                bduffy323 Level 1

                setting the zeroPoint didn't seem to do the trick, but your prototype on the Object worked as expected! I didn't think of using the page bounds. It seems so simple! Thank you.

                • 5. Re: coordinates as related to the page
                  Jump_Over Level 5

                  bduffy323 wrote:

                   

                  It seems to me that indesign assigns the page item to the page that is majorily on...

                   

                  Agree. It could be seen maybe better with facingPage switched off:

                   

                  screen.png

                  I've met another interesting "page coordi behaviour".

                  FacingPage is OFF.

                  allowPageShuffle is false.

                  There are 2  pages in a spread.

                  Any pageItem.add() on the right page. By default it is placed in left up corner and parentPage is correct (right). But try to assign some custom geobounds to it, let say [10,10,100,100]. This move item to the left side alike item.parentPage would be "left" not "right".

                  I can't find any answer for this...

                   

                  Message was edited by: Jump_Over

                  • 6. Re: coordinates as related to the page
                    bduffy323 Level 1
                    Object.prototype.myGeoBounds = function () {
                        if(this.parentPage==null) return false;
                        var gb = this.parentPage.bounds;
                        var GB = this.geometricBounds;
                        var my = GB[0]- gb[0];
                        var mx =  GB[1] - gb[1];
                        var mY =  GB[2] - gb[0];
                        var mX =  GB[3] - gb[1];
                        return [my,mx,mY,mX]
                        }
                    
                    
                    var myDoc = app.activeDocument;
                    var page1 = app.activeDocument.pages[1];
                    var page2 = app.activeDocument.pages[2];
                    
                    
                    var page1Bounds = page1.bounds;
                    var width = page1Bounds[3]-[1];
                    var myItem = page2.rectangles.add();
                    
                    
                    var destination = [10,10,100,100]
                    //myItem.geometricBounds = [10,10+width,100,100+width]
                    
                    
                    var itemBounds = myItem.myGeoBounds();
                    var offSetX = destination[1] - itemBounds[1];
                    var offSety = destination[0] - itemBounds[0];
                    myItem.move(undefined,[offSetX,offSety])
                    
                    

                     

                     

                    This happens for the same reason I was having an issue. The geometricBounds only take into account the coordinate spaced based off the left hand page. In this code segment I created a new document with 3 pages and went down to the two that are side by side. Using your method I then used the move by instead of just setting the geometric bounds. This way it was all relative to the page you are working in. Does this help you out?

                    • 7. Re: coordinates as related to the page
                      Jump_Over Level 5

                      Hi,

                       

                      Agree, move works. But run this:

                       

                      myDoc = app.documents.add();
                      myDoc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
                      myDoc.documentPreferences.facingPages = true;//false;
                      l=2;
                      if (myDoc.documentPreferences.facingPages)
                      while (l--) myDoc.pages.add();
                      else {
                      MS = myDoc.spreads.add();
                      MS.allowPageShuffle = false;
                      myDoc.spreads[1].pages.add();
                          }
                      myRec = myDoc.pages[2].rectangles.add();
                      myRec.geometricBounds = [10,10,100,100];
                      

                       

                      and compare result if you toggle facingPages true/false (line 3)

                      See the difference? You don't need move() with facingPages; geoBounds works!