11 Replies Latest reply on Mar 24, 2015 9:23 PM by CarlosCanto

    Resize then check collide and repeat untill max

    Zantcor Level 1

      I've got a trapezoid that is the guide box and artwork that needs to always be maxed size to fit it.  I've wrote a script that will place the artwork bottom center then move it up 3points so that it isn't touching.  I need the script to loop through and resize by 101% then redo the center function and check collide again.  I'm not even sure if there is a .collide or how to go about that but I went ahead and wrote the rest of the script.  The part that reads "arttouchguide.collide" on line 07 is the part that I'm having problems with and it is just a place holder as I know that it doesn't work right now.  Below the code is a .jpg of what I'm working with.

       

      #target illustrator
      
        var selecteditem = app.activeDocument.groupItems[0];
        
        
        
        for ( arttouchguide.collide = false)
        {
         selecteditem.resize(101,101);
         centerArt()
      }
      
      function centerArt()
         {   
             var thisdoc = app.activeDocument;  
              var selecteditem = app.activeDocument.groupItems[0];
              selecteditem.selected = true;
              var hasDocCoords = app.coordinateSystem == CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
              
              
              var p = thisdoc.pageItems;  
              for (var i = 0, l = p.length; i < l; i++) {  
                  var pID = p[i];  
                  if (pID.guides == true) {  
                      var guideBox = pID  
                  }  
              }   
              var wNum = guideBox.width;  
              var hNum = guideBox.height;
              var swNum = selecteditem.width;
              var shNum = selecteditem.height;
              
              var sxOffset = (swNum/2);
              var syOffset = (shNum);
              var xOffset = (wNum/2);
              var yOffset = (hNum-3);
              
              
              var xNum = guideBox.position[0]+xOffset;  
              var yNum = guideBox.position[1]-yOffset; 
              
              var guideXCenter = xNum-sxOffset;
              var guideYCenter = yNum+(syOffset);
              
              var sxNum = selecteditem.position[0]+sxOffset;
              var syNum = selecteditem.position[1]-syOffset;
              
              //to test your script for position un annotate the two alerts below
              
                    // alert("GuideBox Data:\nWidth: "+wNum+"\nHeight: "+hNum+"\nX Position: "+xNum+"\nY Position: "+yNum); 
                    // alert ("Selected object Data:\nWidth: "+swNum+"\nHeight: "+shNum+"\nX Position: "+sxNum+"\nY Position: "+syNum);
                     
                     
              //changes the position of the selected artwork to the center of the guide box.
              selecteditem.position = [guideXCenter, guideYCenter]    
              
          }//end centerart
      

       

      Umbrella Resize Test.jpg

       

      The red line is the guide box, the lime part is the artwork.  It will increase the size of the art, recenter it, move it up 3 points and check for overlap/collide and repeat till it is touching.  This is the only way that I can think of to max out the artwork inside a trapezoid.

        • 1. Re: Resize then check collide and repeat untill max
          CarlosCanto Adobe Community Professional & MVP

          I've got it!...it seems, it worked on paper with sample values, I just wanted to share the Formula and the Method for now...script to come...

           

          this formula (#8) solves for the max width of the art (e)

          20150320_200155.jpg

           

          1 to 4 was first attempt, didn't work, ignore that...second try starts with 5, sample values in #4

          • 2. Re: Resize then check collide and repeat untill max
            CarlosCanto Adobe Community Professional & MVP

            hmm...funny, the scripted version is not working with the sample values.

             

            [edit] found the problem, syntax error, we're back on track.

            • 3. Re: Resize then check collide and repeat untill max
              Qwertyfly... Level 4

              Would this script have the answer to this.

              I'm sure it could be dissected to achieve just collision detection.

               

              http://www.wundes.com/JS4AI/groupOverlappingObjects.js

              • 4. Re: Resize then check collide and repeat untill max
                CarlosCanto Adobe Community Professional & MVP

                problably, I should try the original request next, scale 101, check collision, repeat.

                 

                I went with the math approach...it's done, just need to make it presentable, I'll post it tonight after work.

                • 5. Re: Resize then check collide and repeat untill max
                  Zantcor Level 1

                  I had been working on this since I posted it and between the two posts below I had come up with the following.  Funny thing is one of them is yours Carlos.

                   

                  How to select only objects within a selection marquee?

                   

                  Re: How to check if a path fits inside of another path?

                   

                  There was one more but I failed to bookmark it.  It took the artwork that was selected, rasterized it, live traced it so that you had one path going along the outside of the groupitem.

                   

                  Here is the code that I have so far;

                   

                  #target illustrator
                  
                    var selecteditem = app.activeDocument.groupItems[0];
                    
                    var artworkmax = false 
                    
                    //to count the number of tries
                    var trycount = 0;
                    
                  if (app.activeDocument.selection.length > 0)
                  {
                      findguide ()
                      
                    if (guidetoggle == false)
                    {
                        alert ("There is not a guide box to size to")
                        }//end if
                    
                    
                    for (a=0;artworkmax == false || a>1000; a++)
                    {
                     selecteditem.resize(102,102);
                     centerArt()
                     merge()
                     findguide()
                     trycount++
                     
                            if (artworkmax == true)
                            {
                              alert ("Script tried " + trycount + " number of times")
                              }
                     
                  }
                  }/*end if anything open check*/ else
                  {
                      alert ("Nothing selected")
                      }
                  
                  function centerArt()
                     {   
                         var thisdoc = app.activeDocument;  
                          var selecteditem = app.activeDocument.groupItems[0];
                          selecteditem.selected = true;
                          var hasDocCoords = app.coordinateSystem == CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
                          
                          
                          var p = thisdoc.pageItems;  
                          for (var i = 0, l = p.length; i < l; i++) {  
                              var pID = p[i];  
                              if (pID.guides == true) {  
                                  var guideBox = pID  
                              }  
                          }   
                          var wNum = guideBox.width;  
                          var hNum = guideBox.height;
                          var swNum = selecteditem.width;
                          var shNum = selecteditem.height;
                          
                          var sxOffset = (swNum/2);
                          var syOffset = (shNum);
                          var xOffset = (wNum/2);
                          var yOffset = (hNum-3);
                          
                          
                          var xNum = guideBox.position[0]+xOffset;  
                          var yNum = guideBox.position[1]-yOffset; 
                          
                          var guideXCenter = xNum-sxOffset;
                          var guideYCenter = yNum+(syOffset);
                          
                          var sxNum = selecteditem.position[0]+sxOffset;
                          var syNum = selecteditem.position[1]-syOffset;
                          
                          //to test your script for position un annotate the two alerts below
                          
                                // alert("GuideBox Data:\nWidth: "+wNum+"\nHeight: "+hNum+"\nX Position: "+xNum+"\nY Position: "+yNum); 
                                // alert ("Selected object Data:\nWidth: "+swNum+"\nHeight: "+shNum+"\nX Position: "+sxNum+"\nY Position: "+syNum);
                                 
                                 
                          //changes the position of the selected artwork to the center of the guide box.
                          selecteditem.position = [guideXCenter, guideYCenter]    
                          
                      }//end centerart
                  
                  function findguide()
                  {
                      
                      
                  var thisdoc = app.activeDocument;  
                  var p = thisdoc.pageItems;
                  var guidetoggle = false
                  
                  
                          for (var i = 0, l = p.length; i < l; i++) 
                          {  
                              var pID = p[i];  
                              if (pID.guides == true) 
                              {  
                                  var guideBox = pID
                                  var guidetoggle = true;
                                  
                                  }/*end if*/
                              
                              }//end for
                          
                          if (guidetoggle == true)
                          {
                              //alert ("Found Guides")
                              }/*end if*/ else
                          {
                             // alert ("No Guides")
                              artworkmax = true
                              selecteditem.resize(99,99);
                              
                              }//end else
                  
                   }//end function
                  
                  function merge()
                  {
                  //function to merge both art and guide box
                  //when they are merged if the art overlaps with anything turned to guides
                  //it makes the guides disappear. So after you do this, run FundGuide if guidetoggle = False
                  //then the art and the guide overlapped
                  
                  if (guidetoggle = true)
                  {              
                      var thisdoc = app.activeDocument;
                      var p = thisdoc.pageItems; 
                              for (var i = 0, l = p.length; i < l; i++) 
                          {  
                              var pID = p[i];  
                              if (pID.guides == true) 
                              {  
                                  var guideBox = pID                
                                  }/*end if*/
                              
                              }//end for
                  
                  
                    var selecteditem = app.activeDocument.groupItems[0];
                    
                    //var artguidegroup = selecteditem && guidebox
                    
                    //var artguidegroup.selected = true;
                    
                    app.executeMenuCommand ('Live Pathfinder Merge');
                    
                    }//end if guides are present then run merge
                  
                    }//end merge function
                  
                    
                  function wait10()  
                  {
                    while(a<100)   
                  {    
                      a++; 
                        
                      if (a>99) break; // guard against infinite loop  
                  
                    }
                  }
                  
                  

                   

                   

                  I had to up it from the 101% increase as it was bogging down the section if there were a lot of points but when I did it wasn't as accurate.  I'm going to set it up to enlarge by 5 percent - Collide then resize by "95.24%" then do it again at 101%

                   

                  I had started on your path but my brain died when I got to around your number 7 so I started fishing for codes.

                   

                  Thank you for all your help guys.

                  • 6. Re: Resize then check collide and repeat untill max
                    CarlosCanto Adobe Community Professional & MVP

                    you were close, you just had to use the formula in # 8, the rest is the method.

                    • 7. Re: Resize then check collide and repeat untill max
                      CarlosCanto Adobe Community Professional & MVP

                      here you go, select both objects before running. Trapezoid must be Isosceles, it must have only 4 pathpoints, it must be closed. Art to fit into Trapezoid can be anything, as long is One item, and it must be on top of Trapezoid.

                       

                      fitArtInTrapezoid.PNG

                       

                      #target Illustrator
                      
                      
                      //  script.name = inscribeSelectedArtInTrapezoid.jsx
                      //  script.description = does what the title says.
                      //  script.requirement = one document with ONE piece of art (Two or more items need to be grouped) and ONE pathItem (the Trapezoid) selected.
                      //  script.parent = carlos canto // 03/24/15
                      //  script.elegant = false;
                      
                      
                      // Usage: select both, the Art and Trapezoid before running, the Art must be on top of the Trapezoid...and the Trapezoid must be ISOSCELES.
                      
                      
                      function main () {
                          var idoc = app.activeDocument;
                      
                      
                          var art = idoc.selection[0]; // anything
                          var trapezoid = idoc.selection[1]; // pathItem
                      
                      
                          var h = art.height;
                          var w = art.width;
                          var pp = trapezoid.pathPoints;
                         
                          if (trapezoid.typename == 'PathItem' && pp.length==4 && trapezoid.closed) {}
                          else {alert('Trapezoid path must be closed and have 4 pathPoints only, \rArt must be on top of Trapezoid, try again'); return}
                             
                          var t = getTrapezoidProps (pp); // t.base, t.top, t.h, t.leg
                      
                      
                          var artRatio = w/h;
                         
                          var newWidth = t.base/(2*t.ratio/artRatio+1); // this is the magic formula
                          var newHeight = newWidth/artRatio;
                      
                      
                          if (newHeight>t.h) {
                              newHeight = t.h;
                              newWidth = newHeight*artRatio;
                          }
                      
                      
                          art.width = newWidth;
                          art.height = newHeight;
                      
                      
                          art.position = [t.anchor[0]+(t.base-newWidth)/2, t.anchor[1]+newHeight];
                      
                      
                      } // end main
                      
                      
                      main ();
                      
                      
                      // return trapezoid base, top, leg, height and anchor (bottom left)
                      function getTrapezoidProps (pp) {
                          var p0 = pp[0].anchor;
                          var p1 = pp[1].anchor;
                          var p2 = pp[2].anchor;
                          var p3 = pp[3].anchor;
                         
                          var t = {}; // trapezoid object
                          var p0x = p0[0];
                          var p0y = p0[1];
                          var p2x = p2[0];
                          var p2y = p2[1];
                         
                          var deltay = p2y-p0y;
                          var deltax = p2x-p0x;
                         
                          // get the slope of the diagonal from P0 to P2
                          var m = deltay/deltax; // slope = delta y / delta x = tan teta
                          teta = Math.atan2(deltay, deltax); // slope returns a positive value for both left to right going up and right to left going down...so return teta instead
                         
                          t.h = Math.abs(deltay);
                         
                          if (m<0) { // slope less than 0, left diagonal
                              if (teta<0) { // left to right going down
                                  t.top = p1[0]-p0[0];
                                  t.leg = p2[0]-p1[0];
                                  t.base = p2[0]-p3[0];
                                  t.anchor = p3;
                              }
                              else { // right to left going up
                                  t.top = p3[0]-p2[0];
                                  t.leg = p0[0]-p3[0];
                                  t.base = p0[0]-p2[0];
                                  t.anchor = p1;
                              }
                          }
                          else { // m>0  right diagonal
                              if (teta<0) { // right to left going down
                                  t.top = p0[0]-p3[0];
                                  t.leg = p1[0]-p0[0];
                                  t.base = p1[0]-p2[0];
                                  t.anchor = p2;
                              }
                              else { // left to right going up
                                  t.top = p2[0]-p1[0];
                                  t.leg = p1[0]-p0[0];
                                  t.base = p3[0]-p0[0];
                                  t.anchor = p0;
                              }
                          }
                          t.ratio = t.leg/t.h;
                          return t;
                      } // end trapezoid props
                      
                      • 8. Re: Resize then check collide and repeat untill max
                        Zantcor Level 1

                        Wow, the things that people are capable of on here never cease to astound me.

                         

                        Thank you CarlosCanto

                        • 9. Re: Resize then check collide and repeat untill max
                          CarlosCanto Adobe Community Professional & MVP

                          Thanks, it was a good challenge, was fun

                          • 10. Re: Resize then check collide and repeat untill max
                            Qwertyfly... Level 4

                            //  script.elegant = false;


                            Great line, I think a lot of my scripts need a disclaimer like this.

                            But I don't think this script needs it.

                            Very nice.