9 Replies Latest reply: May 23, 2011 6:16 PM by Big RA RSS

    How to construct a Rectangle element

    Big RA Community Member

      I would like to use the Rectangle object described in the help to ExtendScript CS5:

       

      >  Rectangle
      >  Adobe Illustrator CS5 Type Library
      >  Describes a rectangle. This class is also a four-element collection.

       

      It has nine properties and methods.

       

      This is not the same as PathItems.rectangle().

       

      How can I construct a var of type Rectangle?

       

                      var rect = new Rectangle

       

      results in the error, "Rectangle does not have a constructor"

       

      How can I create a var of type Rectangle and then set the values?  I would like to store it in an Array.

        • 1. Re: How to construct a Rectangle element
          Ten A Community Member

          Hi, try below

           

          var myRectangle = app.activeDocument.pathItems.rectangle(-100,100,150,150);

           

          You can add rectangles use pathItems rectangle method.

           

          PathItem PathItems.rectangle (top: number, left: number, width: number, height: number[, reversed: bool=false])

           

          Ten.

          • 2. Re: How to construct a Rectangle element
            Big RA Community Member

            I am looking for a Rectangle that I can store in an array, not one in a document.  Maybe such a thing doesn't exist.  Specifically, I am trying to center text labels in circles when they are already in a circle.  I can get the geometricBounds or visibleBounds of each, but I don't know how to get that in an array.  Placing a Rectangle in the array would satisfy the need.  It would be nice to get the functionality of width, height, relative to top, bottom, left, right, that a Rectangle offers.  Oddly enough, text objects have different parameters of these available than do circles.  I would like a class Rectangle that tracks all these when I set them and ask for them.

            • 3. Re: How to construct a Rectangle element
              Ten A Community Member

              Here is a sample code store rectangles in an Array.

               

              var myRectangle = new Array();

               

              for (i=0;i<10;i++){
                  myRectangle[i] = app.activeDocument.pathItems.rectangle(-100,i*15+30,10,10);
                  }

               

              var c = new CMYKColor;
              c.cyan= 100;
              c.magenta = 0;
              c.yellow = 0;
              c.black = 0;

               

              myRectangle[4].fillColor = c;

               

               

               

               

              Ten

              • 4. Re: How to construct a Rectangle element
                [Jongware] Community Member

                Despite your insistence, RA isn't referring to physically drawn rectangles *at all*.

                 

                Theoretically, a "rectangle class" would allow one to construct a rectangle in several ways -- origin, size, top left and bottom right points, etc. It seems the built-in "Rect" class is just a thin wrapper around a basic array, so that's not going to work.

                 

                Perhaps you could build your own. Javascript allows one to create a new class from scratch and define all of the methods and properties you might need.

                • 5. Re: How to construct a Rectangle element
                  [Jongware] Community Member
                  I can get the geometricBounds or visibleBounds of each, but I don't know how to get that in an array.

                   

                  ... Maybe you want to expand on that. Both *are* returned as a simple 4-element array.

                  • 6. Re: How to construct a Rectangle element
                    Big RA Community Member

                    True, I could store the geometricBounds or visibleBounds, but these don't have the methods for top, bottom, right, left, width, height.  I tried to make a class, but ExtendScript tells me I'm making "illegal use of reserved word 'class,'"  so I haven't pursued that further yet.

                     

                    I would like to write a generic "align" routine that would align two objects, but some items have top and height while others have top and bottom, so the procedure to align depends on which kind of object is passed.  Putting a class wrapper around geometricBounds would do it since they all seem to have geometricBounds, but I haven't figured out how to do that either.  I could also use "geometricBounds[0]" etc, but that makes the code big and obscure.

                     

                    As you can tell, I'm new to javascript and this is my learning process.  Extendscript seems to differ somewhat from conventional javascript, making the effort a bit more difficult.

                    • 7. Re: How to construct a Rectangle element
                      chris gebab Community Member

                      hi,

                       

                      you cant use class, try something like that and search for javascript prototyping.

                       

                      function Point(x,y){

                      this.x=x;

                      this.y=y

                       

                      }

                       

                       

                       

                      function MyRect(bounds){

                      this.width= bounds[?]-bounds[?];

                      and so on

                      dont forget:

                      this.center = new Point(a,b);

                      }

                      MyRect.prototype.doubleWidth=function(){

                      return this.width * 2

                      }

                       

                      var rect= new MyRect( Item.someBounds);

                       

                      happy scripting

                      chris

                      • 8. Re: How to construct a Rectangle element
                        [Jongware] Community Member

                        Ah yes -- I didn't remember where I saw this before and had to ask Marc Autret first

                         

                        Check this page of his: http://www.indiscripts.com/post/2010/05/operator-overloading-with-extendscript

                        • 9. Re: How to construct a Rectangle element
                          Big RA Community Member

                          Thanks.  I guess I'll have to try something like that.