13 Replies Latest reply on Nov 9, 2015 5:58 AM by Egor Chistyakov

    Snap every point to grid

    Egor Chistyakov Level 1

      Sometimes I need every point of my working path to be snapped to document grid.

      Right now I have do it manually — cycle every point and type new rounded coordinates or turn on snapping to grid and drag every point with mouse.

      Is there a way to automate it with JS?

        • 1. Re: Snap every point to grid
          Qwertyfly... Level 4

          should not be hard, may run a little slow.

          what size grid do you want to snap to?

          • 2. Re: Snap every point to grid
            Egor Chistyakov Level 1

            Hi! Ideally — the grid defined by document, in Guides&Grid section of Preferences; 'Gridline every' divided by 'Subdivisions', so all points literally would be positioned on actual grid.

            • 3. Re: Snap every point to grid
              Qwertyfly... Level 4

              what about the handles do they need to be on grid points? left as is? or moved by same amount as their points?

              • 4. Re: Snap every point to grid
                Egor Chistyakov Level 1

                The handles should be moved as well with their points (the third variant).

                • 5. Re: Snap every point to grid
                  Qwertyfly... Level 4

                  I have to leave thew PC in a few min.

                  am happy to give you a hand getting it correct.

                  but here is something to get you started.

                   

                  this will round to 1 px.

                  and does the same for the handles.

                   

                  we can play with fixing it later. I'll be away from my PC tomorrow so can help again it a few days.

                   

                  var doc = app.activeDocument;
                  var sel = doc.selection;
                  for(var i=0; i<sel.length; i++){
                      for(var j=0; j<sel[i].pathItems.length; j++){
                          var item = sel[i].pathItems[j];
                          for(var k=0; k<item.pathPoints.length; k++){
                              item.pathPoints[k].anchor = [Math.round(item.pathPoints[k].anchor[0]),Math.round(item.pathPoints[k].anchor[1])];
                              item.pathPoints[k].leftDirection = [Math.round(item.pathPoints[k].leftDirection[0]),Math.round(item.pathPoints[k].leftDirection[1])];
                              item.pathPoints[k].rightDirection = [Math.round(item.pathPoints[k].rightDirection[0]),Math.round(item.pathPoints[k].rightDirection[1])];
                          }
                      }
                  }
                  
                  • 6. Re: Snap every point to grid
                    Egor Chistyakov Level 1

                    Looks promising! I think I'll better need it for millimetres rather then point or inches.

                    I also think that there could be a flag for handles to be also snapped to grid. I think of both ways are useful.

                    Right now it throws an 'undefined is not an object' error for line 4.

                    • 7. Re: Snap every point to grid
                      Qwertyfly... Level 4

                      error was due to me. I was not thinking clearly and had to do with the example art I was playing with being grouped so it worked.

                      that's fixed in the code below, it now just acts on all pathitems in the doc.

                      snaps all anchors to whole mm, moves handles same distance as it moved the anchor.

                       

                      please note that this is likely to change art as it is moving points in the same shape by different amounts and in different directions.

                      obvious but worth putting in writing.

                       

                      var doc = app.activeDocument;
                      var tomm = 2.83466796875;
                      for(var i=0; i<doc.pathItems.length; i++){
                          var item = doc.pathItems[i];
                          for(var k=0; k<item.pathPoints.length; k++){
                              var A = item.pathPoints[k].anchor;
                              item.pathPoints[k].anchor = [Math.round(A[0]/tomm)*tomm,Math.round(A[1]/tomm)*tomm];
                              var x = A[0]-(Math.round(A[0]/tomm)*tomm);
                              var y = A[1]-(Math.round(A[1]/tomm)*tomm);
                              item.pathPoints[k].leftDirection = [item.pathPoints[k].leftDirection[0]-x, item.pathPoints[k].leftDirection[1]-y]
                              item.pathPoints[k].rightDirection = [item.pathPoints[k].rightDirection[0]-x,item.pathPoints[k].rightDirection[1]-y];
                          }
                      }
                      
                      • 8. Re: Snap every point to grid
                        Qwertyfly... Level 4

                        PS.

                        I had a look but I can't see a way to get the grid details from the document via Javascript.

                        someone please chime in if i am wrong about this.

                        • 9. Re: Snap every point to grid
                          Egor Chistyakov Level 1

                          It works! There are some insignificant fractions in coordinates due recalculating.

                          It really affects everything. I'll try to make it work with selected art only at least, or selected points next.

                          Thanks! Yes, the path does change, but it is intended.

                          • 10. Re: Snap every point to grid
                            Qwertyfly... Level 4

                            to make it work with selection you want:(these 3 lines to replace line no. 3)

                            var sel = doc selection;

                            for(var i=0; i<sel.length; i++){

                            if(sel[i].typename == "PathItem"){


                            and don't forget a closing brace around line 12 or 13

                             

                            untested, but should work... (what's the emoji for fingers crossed...)

                            • 11. Re: Snap every point to grid
                              CarlosCanto Adobe Community Professional & MVP

                              Qwertyfly... wrote:

                               

                              PS.

                              I had a look but I can't see a way to get the grid details from the document via Javascript.

                              someone please chime in if i am wrong about this.

                               

                              open the ai file in text mode, seek this line

                               

                              %AI7_GridSettings: 72 8 72 8 1 0 0.800000011920929 0.800000011920929.....

                              • 12. Re: Snap every point to grid
                                Qwertyfly... Level 4

                                nice. Very hack. I like it.

                                • 13. Re: Snap every point to grid
                                  Egor Chistyakov Level 1

                                  The hack is nice, even without me knowing exact syntax of it, hehe. What is this fractioned value is, I guess? 72-8 is quite understandable.

                                  Can JS open current document in text mode and excavate this line to take needed?

                                  I will test the change for selection as soon as I could and report here.