Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to crop artwork to artboard

New Here ,
Dec 11, 2015 Dec 11, 2015

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!

TOPICS
Scripting
3.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
New Here ,
Dec 11, 2015 Dec 11, 2015

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 11, 2015 Dec 11, 2015

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;

            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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2015 Dec 11, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Dec 11, 2015 Dec 11, 2015

@ 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 14, 2015 Dec 14, 2015
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines