Skip to content

Instantly share code, notes, and snippets.

@alijaya
Forked from bryanbuchanan/GetShapeArea.jsx
Last active November 9, 2021 19:10
Show Gist options
  • Save alijaya/cc693da563affad034b1241f33c915cc to your computer and use it in GitHub Desktop.
Save alijaya/cc693da563affad034b1241f33c915cc to your computer and use it in GitHub Desktop.
Script to find the area of shapes in Adobe Illustrator
/* Save this file with a jsx extension and place in your
Illustrator/Presets/en_US/Scripts folder. You can then
access it from the File > Scripts menu */
var decimalPlaces = 3;
function calculateArea (obj) {
if (obj.typename == "PathItem") {
return obj.area; // could be negative
} else if (obj.typename == "CompoundPathItem") {
var totalArea = 0;
for (var i=0; i<obj.pathItems.length; i++) {
totalArea += calculateArea(obj.pathItems[i]); // could be negative
}
return Math.abs(totalArea); // make sure positive
} else if (obj.typename == "GroupItem") {
var totalArea = 0;
for (var i=0; i<obj.pathItems.length; i++) {
totalArea += Math.abs(calculateArea(obj.pathItems[i])); // make sure positive
}
for (var i=0; i<obj.compoundPathItems.length; i++) {
totalArea += calculateArea(obj.compoundPathItems[i]); // already positive
}
for (var i=0; i<obj.groupItems.length; i++) {
totalArea += calculateArea(obj.groupItems[i]); // already positive
}
return totalArea; // already positive
} else { // not path, compound path or group
return 0;
}
}
function convertArea (area) {
var scaleFactor = 1;
if (app.documents.length > 0 && app.activeDocument.scaleFactor != null) {
scaleFactor = app.activeDocument.scaleFactor;
}
var ppi = 72;
var result = {};
area *= scaleFactor * scaleFactor;
result.inch = area/ppi/ppi;
result.cm = result.inch * 2.54 * 2.54;
result.m = result.cm / 10000;
return result;
}
if (app.documents.length > 0) {
var objects = app.activeDocument.selection;
var display = ["Shape Area"];
// Collect info
var totalArea = 0;
for (var i=0; i<objects.length; i++) {
var area = Math.abs(calculateArea(objects[i])); // need absolute in case of PathItems
totalArea += area;
var conv = convertArea(area);
display.push(conv.inch.toFixed(decimalPlaces) + " in² / " + conv.cm.toFixed(decimalPlaces) + "cm² / " + conv.m.toFixed(decimalPlaces) + "m²");
}
var conv = convertArea(totalArea);
display.push("Total Area: " + conv.inch.toFixed(decimalPlaces) + " in² / " + conv.cm.toFixed(decimalPlaces) + "cm² / " + conv.m.toFixed(decimalPlaces) + "m²");
// Display
alert(display.join("\n"));
}
@alijaya
Copy link
Author

alijaya commented Nov 5, 2021 via email

@alijaya
Copy link
Author

alijaya commented Nov 5, 2021

Or if you could attach the problematic file, I could check it for you.

@rivieraneon
Copy link

Or if you could attach the problematic file, I could check it for you.

Can't attach an AI file unfortunately, and can't also put a WeTransfer link.

@rivieraneon
Copy link

do you have screenshot? and could I see the document settings, I think there's a corner case that I haven't covered with this script.

Capture d’écran 2021-11-05 à 14 55 46

Capture d’écran 2021-11-05 à 14 55 52

Capture d’écran 2021-11-05 à 14 56 06

Capture d’écran 2021-11-05 à 14 56 25

@alijaya
Copy link
Author

alijaya commented Nov 5, 2021

Weirdly I can't reproduce the bug. It would be better if I could have the file though.

@rivieraneon
Copy link

Weirdly I can't reproduce the bug. It would be better if I could have the file though.

I'll send you by email.

@alijaya
Copy link
Author

alijaya commented Nov 5, 2021 via email

@rivieraneon
Copy link

ok that would be great

Sent at 3:27PM.

@schroef
Copy link

schroef commented Nov 5, 2021

I've made an altered version of this script,allows you to pick units and also set accuracy. There are updated versions of this script actually

@schroef
Copy link

schroef commented Nov 5, 2021

In my repo i added 2 version, im sort of still working on v5. That ones has some dev comments in them. I added more options like unit comparison and some others and also a complete dialog panel. I think the base code was from that gist of Bryan Buchanary and some additions from a different user. That person added a method to work with cutout shapes. Though that does not work when there are multiple cutouts. At least not properly

I cant find that other gist at the moment, these gists are a bit weird. When you quote it doesnt add the name, so response are hard to track. I always try to manually @ them using @and then the name. Im also trying to find the gist where all that other work was done. Some others have contributed really good to that code.

https://github.com/schroef/illustrator-scripts
getShapeArea-dialog-v005-3
getShapeArea-dialog-v005-2
getShapeArea-dialog-v005-1

Edit
ive found the gist again, it was from here; https://gist.github.com/bryanbuchanan/11387501

@alijaya
Copy link
Author

alijaya commented Nov 6, 2021

ok that would be great

Sent at 3:27PM.

Hey, I've looked into the file. And yes, I see the problem. And no, I don't have any clue why that's happen XD... One thing that I notice is that all the unit is weird. For example, in that file, when you create new Artboard, and set it to A4, it shows 2100mm x 2970mm, the normal one is 210mm x 297mm. Somehow all the unit is displayed 10 times than it actually is.

I'm curious, how could you make this file?

@alijaya
Copy link
Author

alijaya commented Nov 6, 2021

In my repo i added 2 version, im sort of still working on v5. That ones has some dev comments in them. I added more options like unit comparison and some others and also a complete dialog panel. I think the base code was from that gist of Bryan Buchanary and some additions from a different user. That person added a method to work with cutout shapes. Though that does not work when there are multiple cutouts. At least not properly

I cant find that other gist at the moment, these gists are a bit weird. When you quote it doesnt add the name, so response are hard to track. I always try to manually @ them using @and then the name. Im also trying to find the gist where all that other work was done. Some others have contributed really good to that code.

https://github.com/schroef/illustrator-scripts getShapeArea-dialog-v005-3 getShapeArea-dialog-v005-2 getShapeArea-dialog-v005-1

Edit ive found the gist again, it was from here; https://gist.github.com/bryanbuchanan/11387501

Nice :D... I haven't tried if your script will work correctly with this case. @rivieraneon should try it.

@rivieraneon
Copy link

In my repo i added 2 version, im sort of still working on v5. That ones has some dev comments in them. I added more options like unit comparison and some others and also a complete dialog panel. I think the base code was from that gist of Bryan Buchanary and some additions from a different user. That person added a method to work with cutout shapes. Though that does not work when there are multiple cutouts. At least not properly

I cant find that other gist at the moment, these gists are a bit weird. When you quote it doesnt add the name, so response are hard to track. I always try to manually @ them using @and then the name. Im also trying to find the gist where all that other work was done. Some others have contributed really good to that code.

https://github.com/schroef/illustrator-scripts getShapeArea-dialog-v005-3 getShapeArea-dialog-v005-2 getShapeArea-dialog-v005-1

Edit ive found the gist again, it was from here; https://gist.github.com/bryanbuchanan/11387501

First it says "FALSE" in a first window, and then "100cm2" so same problem as it should say 10000cm2

@rivieraneon
Copy link

ok that would be great

Sent at 3:27PM.

Hey, I've looked into the file. And yes, I see the problem. And no, I don't have any clue why that's happen XD... One thing that I notice is that all the unit is weird. For example, in that file, when you create new Artboard, and set it to A4, it shows 2100mm x 2970mm, the normal one is 210mm x 297mm. Somehow all the unit is displayed 10 times than it actually is.

I'm curious, how could you make this file?

I have an official AI licence, I made this file by doing CMD+N (make a new document) and that's it.

@schroef
Copy link

schroef commented Nov 8, 2021

First it says "FALSE" in a first window, and then "100cm2" so same problem as it should say 10000cm2
As @alijaya noted, your file is not correct! I tried it myself and make a cube of 100 100cm and get returned 1000cm2

Could you share that corrupt file, idd like to have a look at it?
I what illustrator version has that file been created?

@rivieraneon
Copy link

First it says "FALSE" in a first window, and then "100cm2" so same problem as it should say 10000cm2
As @alijaya noted, your file is not correct! I tried it myself and make a cube of 100 100cm and get returned 1000cm2

Could you share that corrupt file, idd like to have a look at it? I what illustrator version has that file been created?

I'd need an email as I can't upload the file here.

Illustrator 2022 (26.0).

@schroef
Copy link

schroef commented Nov 8, 2021

romboutv @ gmail com

@schroef
Copy link

schroef commented Nov 8, 2021

Ive found the "issue", its because your document has the documents raster effects resolution set to 35ppi. This script should set that temporarily to 72 dpi since that is the measurements it uses.You can find this in Effects > Document Raster Effect Settings > PPI

I think it could be added to the script to temporarily set this to 72dpi than when done revert to old settings.
doc-raster-effect-settings

EDIT
Nevermind, just check it again and it showed 0.01 again, not sure whats wrong with this document now?!
I check the code and its hardcoded to use 72ppi

@schroef
Copy link

schroef commented Nov 8, 2021

PS i did noticed something else. I had an idea of saving your file to a new file and reopening it. When i did so, i noticed the square now suddenly was 100x100mm?!? There really is something odd with this document.

sudden-10scale-after-saving

@alijaya
Copy link
Author

alijaya commented Nov 9, 2021

Finally I found what's wrong with this document hahahaha... Basically it has something to do witht the feature of Illustrator, namely Large Scale Canvas. https://helpx.adobe.com/illustrator/using/large-sized-artwork.html

Basically if you create more than 227in (5.77m) artboard, it will use the setting of Large Scale Canvas, basically that's why it happens. So I need to find how to get this information from scripting, and doing simple if.

@alijaya
Copy link
Author

alijaya commented Nov 9, 2021

@rivieraneon I have fixed it. Could you try the new script?
I have found solution here https://community.adobe.com/t5/illustrator-discussions/get-true-artboard-size-with-quot-large-canvas-quot-document-from-extendscript/td-p/11216761

Basically we could get the scaleFactor of the document, so I use that value to correct the size. It should work now.

@rivieraneon
Copy link

Thanks now it's working even on the weird/wrong document I sent you both!
Many thanks to you 2 for your kind help and efficiency!

@alijaya
Copy link
Author

alijaya commented Nov 9, 2021

Glad it's finally working :D

@schroef
Copy link

schroef commented Nov 9, 2021

Haha that is funny, i was actually looking at that feature after reading this very long thread on adobe community. What a coincidence!!!

Never thought it would be linked to that setting!

I do wonder now,why was this document read a being large scale? Was this document first setup like that and then later scaled down?

I think this could be added in the code. Apperently what adobe does is scale it in the background. That is there trick to get these huge documents working

@schroef
Copy link

schroef commented Nov 9, 2021

You could try this code and see if semething related to large scale is in the attributes. I use these simple loops to find all items available through code.

items = []
for (attr in app.activeDocument){
    items.push(attr+", ")
}
alert(items)

Then you can alter the for loop if you a attribute you think is connected, then run it again pointing to that attribute

@schroef
Copy link

schroef commented Nov 9, 2021

After checking, my earlier script did not get very far. Ive found this great read on StackExhange, it references the scripting reference PDF and the Illustrator settings file, probably it show the correct name for the scaling to get the value for scripting.

I think im gonna check tonight at home and dig through that preferences file see if i can find anything related. Though i would guess such a setting would be saved as a setting in the document. Ill chech that as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment