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"));
}
@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