5 Replies Latest reply on Jan 8, 2017 8:28 AM by karth80472045

    Recheck pathItems.length property in for loop

    karth80472045

      hello everyone,

       

      i need your help , i am grouping pathitems and text frames in a layer , while looping through pathitems and text frames using for loop  , if required pathitem and text frames is found it will group move the group to beginning , but here is the problem so pathitem position is changed so i could miss many pathitems so how to resolve this problem.

       

      for eg

       

      here is my code

       

      function read_create()

      {

        

        for(layer_iteration=0;layer_iteration<layer_file.length;layer_iteration++)

          {

       

       

      if (layer_file[layer_iteration].locked){

       

       

             layer_file[layer_iteration].locked=false;

             }

       

       

          if(layer_file[layer_iteration].name=="NUMBER")

          {

              var theGrp =layer_file[layer_iteration].pathItems;

            

       

       

                for(var i=0,len1=theGrp.length; i<len1; i++) {

                   

            

       

       

      if(theGrp[i].pathPoints.length == 8)

                    if(theGrp[i].stroked == true && theGrp[i].filled == true) {

                 var strkColor = new CMYKColor();

       

       

              strkColor.cyan = Math.round(theGrp[i].strokeColor.cyan);

              strkColor.magenta = Math.round(theGrp[i].strokeColor.magenta);

              strkColor.yellow = Math.round(theGrp[i].strokeColor.yellow);

              strkColor.black = Math.round(theGrp[i].strokeColor.black);

           

              if(Math.round(theGrp[i].strokeWidth*25.4/72*10)/10 == 0.3 && strkColor.cyan == magentaColor.cyan && strkColor.magenta == magentaColor.magenta && strkColor.yellow == magentaColor.yellow && strkColor.black == magentaColor.black) { 

                  

                  

       

       

       

       

                       x=Math.round(theGrp[i].pathPoints[1].anchor[0]);

                       y=Math.round(theGrp[i].pathPoints[3].anchor[1]);

                       xx=Math.round(theGrp[i].pathPoints[5].anchor[0]);

                       xy=Math.round(theGrp[i].pathPoints[7].anchor[1]);

                       count=0;

                        for(text_iteration=0,len=layer_file[layer_iteration].textFrames.length;text_iteration<len ;text_iteration++)

                       {

                           if(text_frame[text_iteration].contents.match(/[A-Z]{1}[0-9]{1}$/) != null||text_frame[text_iteration].contents.match(/[A-Z]{1}[0-9]{2}$/) != null||text_frame[text_iteration].contents.match(/[A-Z]{1}[0-9]{3}$/) != null){

                              

                         t=Math.round(text_frame[text_iteration].anchor[1]);

                      

                         t1=(Math.round(text_frame[text_iteration].anchor[0]))

                         alert(text_frame[text_iteration].contents);

                       

                       if(t1>xx&&t1<x){

                           if(t>y&&t<xy) {

                             

                          

                          

                        {

                          

                            var newGroup = layer_file[layer_iteration].groupItems.add();

                            newGroup.name = text_frame[text_iteration].contents+":";

                            theGrp[i].moveToBeginning(newGroup);

                            text_frame[text_iteration].moveToBeginning(newGroup);

                            len=layer_file[layer_iteration].textFrames.length;

                       

                            

                            }

                       

                     }}}

       

       

         }}}}}}}}}

        • 1. Re: Recheck pathItems.length property in for loop
          o-marat Level 3

          Try to use reverse loop sorting of items, like this:

          for (var i = collection.length - 1; i >= 0; i--) {
            var obj = collection[i];
            // some actions
          }
          
          • 2. Re: Recheck pathItems.length property in for loop
            o-marat Level 3

            I'm few simplified the code:

            • without changing the logic I removed unnecessary levels of nesting
            • use the reverse iterators (for objects that moved)
            • simplify the regexp
            • declared variables and simplify some variable names

            Now it is better to read. Should work. Run it in ESTK and catch the errors:

             

            //@target illustrator
            
            read_create(activeDocument.layers);
            
            function read_create(lays) {
              var lay_i, txt_i, x, y, xx, xy, count, txtFramesLen, t, t1;
            
              for (lay_i = 0; lay_i < lays.length; lay_i++) {
            
                if (lays[lay_i].locked) lays[lay_i].locked = false;
                if (lays[lay_i].name != "NUMBER") continue;
            
                var theGrp = lays[lay_i].pathItems;
            
                for (var i = theGrp.length - 1; i >= 0 ; i--) {
            
                  if (theGrp[i].pathPoints.length != 8)  continue;
                  if (!theGrp[i].stroked || !theGrp[i].filled)  continue;
            
                  var strkColor     = new CMYKColor();
                  strkColor.cyan    = Math.round(theGrp[i].strokeColor.cyan);
                  strkColor.magenta = Math.round(theGrp[i].strokeColor.magenta);
                  strkColor.yellow  = Math.round(theGrp[i].strokeColor.yellow);
                  strkColor.black   = Math.round(theGrp[i].strokeColor.black);      
            
                  if (!(Math.round(theGrp[i].strokeWidth * 25.4 / 72 * 10) / 10 == 0.3 &&
                    strkColor.cyan == magentaColor.cyan &&
                    strkColor.magenta == magentaColor.magenta &&
                    strkColor.yellow == magentaColor.yellow &&
                    strkColor.black == magentaColor.black)) {
                    continue;
                  }
                  x            = Math.round(theGrp[i].pathPoints[1].anchor[0]);
                  y            = Math.round(theGrp[i].pathPoints[3].anchor[1]);
                  xx           = Math.round(theGrp[i].pathPoints[5].anchor[0]);
                  xy           = Math.round(theGrp[i].pathPoints[7].anchor[1]);
                  count        = 0;
                  txtFramesLen = lays[lay_i].textFrames.length;
            
                  for (txt_i = txtFramesLen - 1; txt_i >= 0; txt_i--) {
                    var text_frame = lays[lay_i].textFrames;
            
                    if (!text_frame[txt_i].contents.match(/[A-Z][0-9]{1,3}$/)) continue;
            
                    t  = Math.round(text_frame[txt_i].anchor[1]);
                    t1 = (Math.round(text_frame[txt_i].anchor[0]));
                    alert(text_frame[txt_i].contents);
            
                    if (!(t1 > xx && t1 < x)) continue;
                    if (!(t > y && t < xy)) continue;
            
                    var newGroup  = lays[lay_i].groupItems.add();
                    newGroup.name = text_frame[txt_i].contents + ":";
                    text_frame[txt_i].moveToBeginning(newGroup);
                  }      
            
                  theGrp[i].moveToBeginning(newGroup);
                }
              }
            }
            
            • 3. Re: Recheck pathItems.length property in for loop
              karth80472045 Level 1

              Thank you sir,  but pathitems is at beginning , i want text frame should be placed before the pathItem inside the group so textframe will be visible.

              • 4. Re: Recheck pathItems.length property in for loop
                o-marat Level 3

                maybe replace this line:

                theGrp[i].moveToBeginning(newGroup);

                to that:

                theGrp[i].move(newGroup, ElementPlacement.PLACEATEND);