5 Replies Latest reply on Dec 14, 2015 1:46 AM by Jase_MK

    Script to crop artwork to artboard

    Jase_MK Level 1

      I have some SVG graphics where the features/polygons 'hang out' over the edge of my artboard by a tiny amount. Is there a way (via script) of cropping all my shapes such that nothing hangs out over the edge of the artboard?

       

      I have some script that creates a rectangle which is the exact size of my artboard. Using the menus (not script) I can then use that rectangle as a clipping mask over everything else which makes everything appear to be cropped, although what I'd actually like to do is properly, permanently modify all my polygons, rather than just having a visible clipping mask (and I need to do it all in script too). I'm then exporting the SVG files (hundreds of them) to DXF and what I don't want is coordinates of polygons that fall outside of the defined artboard boundary.

       

      These are tesselating map tiles that are eventually making their way to GIS software to form a much larger map, so I don't want these little slivers of overhanging features as they end up overlapping each other at teh tile boundaries in teh final map.

       

      Thanks!

        • 1. Re: Script to crop artwork to artboard
          Jase_MK Level 1

          It appears I can do what I want using the 'crop' option of Pathfinder, using the GUI. I guess the question is whether it's possible to use this option via my script?

          • 2. Re: Script to crop artwork to artboard
            Silly-V Adobe Community Professional

            If Crop really solves your problem, then yes it's totally possible!

            #target illustrator
            function ArtboardCrop(){
                app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
                if(app.documents.length  > 0){
                    var doc = app.activeDocument;
                    var activeArtboardIndex = doc.artboards.getActiveArtboardIndex();
                    var rc =  doc.artboards[activeArtboardIndex].artboardRect;
                    var newRect = doc.pathItems.rectangle(rc[1],rc[0],rc[2],-rc[3]);
                    newRect.filled = false;
                    newRect.stroked = false;
                    var newGroup = doc.groupItems.add();
                    var thisItem;
                    for(var i = doc.layers[0].pageItems.length - 1; i > -1;  i--){
                        thisItem = doc.layers[0].pageItems[i];
                        if(thisItem != newGroup){
                            thisItem.move(newGroup, ElementPlacement.PLACEATBEGINNING);
                        }
                    };
                    app.executeMenuCommand("selectall");
                    app.executeMenuCommand("Live Pathfinder Crop");
                    app.executeMenuCommand("expandStyle");
                }
            }
            ArtboardCrop();
            

             

            As for me, I don't know about all this, since this effect seems to destroy strokes.

            • 3. Re: Script to crop artwork to artboard
              williamadowling Level 4

              Is it possible to simply 'use artboards' when saving/exporting? this will leave the actual artwork alone, but the exported file will be cropped to the artboard.

              • 4. Re: Script to crop artwork to artboard
                W_J_T Level 4

                @ Silly-V, Nice one, albeit CS6+ , since its going to .DXF a simple basic stroke could be reapplied via script after the fact, perhaps. I would like to know further the requirements.

                 

                @ william, using '.artBoardClipping' works well for image formats (gif,jpeg,png) sadly for the various vector formats things get more tricky when cleaning/removing unneeded items.

                • 5. Re: Script to crop artwork to artboard
                  Jase_MK Level 1

                  Thanks all. In the end I recorded an action for pathfinder crop, created an appropriately sized rectangle and used script to call the action - which did exactly what I needed. Much like Silly-V's solution, although mine was a little more messy with the having to record an action. I may change it slightly to use app.executeMenuCommand("Live Pathfinder Crop"); as Silly V suggests as it's a little neater.

                   

                  William, exporting just the artboard would be great but sadly I couldn't find this as an option in the JS API docs under the various AutoCAD exporting options.

                   

                  Thanks again!