Skip to main content
Inspiring
Nov 3, 2020 2:19 PM UTC
Answered

Illustrator JavaScript count area with offset.

Hello,

It is possible to count area with 5 mm. offset?

Example:

  I use this script and want to add a 5 mm offset in the count:

alert (app.activeDocument.selection[0].area);

I want to count irregular square where shape like U form, + offset. Shape 10x10 cm size, with 5x5 cm cut. I want to get a shape area not 75 cm2 but 101cm2.

It is even possible or not? 

This topic is closed to new replies. Start a new post to keep the conversation going.
CarlosCanto
CarlosCantoCorrect answer
Community Expert
Nov 4, 2020 6:00 PM UTC

I see, you have to make the shape to be able to query its Area.

 

here's a script that adds the offset path, you can delete it after getting the area if you don't need it.

Select ONE shape before running

 

// apply offset path to selected pathItem
//https://community.adobe.com/t5/illustrator/illustrator-javascript-count-area-with-offset/td-p/11564739?page=1

function main () {
    var idoc = app.activeDocument;
    var sel = idoc.selection[0];

    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

    var mm = 72/25.4;
    var offset = 5*mm;
    
    // offset path Effect with Round Joins
    var xmlstringOffsetPath = '<LiveEffect name="Adobe Offset Path"><Dict data="R mlim 4 R ofst value I jntp 2 "/></LiveEffect>'; // 0 = round, 1=bevel, 2=miter
    xmlstringOffsetPath = xmlstringOffsetPath.replace("value", offset);

    idoc.selection = null;
    
    var dup = sel.duplicate(sel, ElementPlacement.PLACEAFTER);
    try {
        dup.filled = false;
    }
    catch(e) {}

    dup.selected = true; // select it before applying effects
    
    // apply offset path Effect
    dup.applyEffect(xmlstringOffsetPath);
    app.redraw();
    
    // expand appearance
    app.executeMenuCommand ('expandStyle');
    
    app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS; 
}

main();

 

 

 Carlos

8 replies

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
Nov 4, 2020 6:00 PM UTC

I see, you have to make the shape to be able to query its Area.

 

here's a script that adds the offset path, you can delete it after getting the area if you don't need it.

Select ONE shape before running

 

// apply offset path to selected pathItem
//https://community.adobe.com/t5/illustrator/illustrator-javascript-count-area-with-offset/td-p/11564739?page=1

function main () {
    var idoc = app.activeDocument;
    var sel = idoc.selection[0];

    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

    var mm = 72/25.4;
    var offset = 5*mm;
    
    // offset path Effect with Round Joins
    var xmlstringOffsetPath = '<LiveEffect name="Adobe Offset Path"><Dict data="R mlim 4 R ofst value I jntp 2 "/></LiveEffect>'; // 0 = round, 1=bevel, 2=miter
    xmlstringOffsetPath = xmlstringOffsetPath.replace("value", offset);

    idoc.selection = null;
    
    var dup = sel.duplicate(sel, ElementPlacement.PLACEAFTER);
    try {
        dup.filled = false;
    }
    catch(e) {}

    dup.selected = true; // select it before applying effects
    
    // apply offset path Effect
    dup.applyEffect(xmlstringOffsetPath);
    app.redraw();
    
    // expand appearance
    app.executeMenuCommand ('expandStyle');
    
    app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS; 
}

main();

 

 

 Carlos

Translate
GalajasAuthor
Inspiring
Nov 4, 2020 6:27 AM UTC

Everything that you all say is correct.

My issue is when, I have shape 100x100 mm with 50x50 cut, and that shape area is 7500 mm2.

Everything is ok if I count with this:

app.activeDocument.pathItems[index].area

 But I need that, area script count with like compensation. Count not original area but bigger shape like that(red dash line):

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And then I get that my area is 10100 mm2. I Dont want to add that new one shape just for the count area, it is possible to make with the script?

Translate
femkeblanco
Legend
Nov 4, 2020 9:55 AM UTC

It's possible.  But why not simply do "offset path", run your line of code and then undo.  It will take seconds.  Scripting will just emulate this.  

Translate
GalajasAuthor
Inspiring
Nov 4, 2020 11:41 AM UTC

Maybe it's possible, but I need in the same script real area and with „Offset path“, and I have countless different shapes where I mus count that area. That's why I ask it is possible to make that script, and maybe someone knows how?

Translate
pixxxelschubser
Community Expert
Community Expert
Nov 3, 2020 7:45 PM UTC

Yes. The description is still not clear for me.

 

And the result of 101cm² is even less clear to me.

Maybe a better screenshot with an offset-object in background an the black object in the foreground will give an enlightenment?

Translate
CarlosCanto
Community Expert
Community Expert
Nov 3, 2020 7:08 PM UTC

@pixxxelschubser that's what I thought at first, but I believe that by "offset" he means the 5x5 rectangle

Translate
pixxxelschubser
Community Expert
Community Expert
Nov 3, 2020 6:21 PM UTC

Only one question:

10cm ×10cm + offset 0,5cm (all sides)

=

11cm × 11cm

=

121 cm²

 

… or not? Am I wrong?

Translate
CarlosCanto
Community Expert
Community Expert
Nov 3, 2020 3:28 PM UTC

obviously you can't get 101 cm2 if you start with 10x10. So, assuming you want to find the size of a rectangle that will give you 101 after substrating a 5x5 area, try this

 

// pseudo code

x2 = 101 cm2 + 25 cm2 = 126 cm2

x = Math.sqrt(x2) = 11.22497 cm

 

Translate