Skip to content

Instantly share code, notes, and snippets.

@nanoSpawn
Forked from bryanbuchanan/GetShapeArea.jsx
Last active January 10, 2022 04:51
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nanoSpawn/8e8fca48d0fb85dd961e to your computer and use it in GitHub Desktop.
Save nanoSpawn/8e8fca48d0fb85dd961e 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 */
/*
Script grabbed from
https://gist.github.com/bryanbuchanan/11387501
fixes negative areas "bug" (some areas are shown negative)
and shows result in square milimeters, given that's
the default unit used in document.
*/
if (app.documents.length > 0) {
if (app.activeDocument.selection.length < 1) {
alert('Select a path first');
} else if (app.activeDocument.selection[0].area) {
// Individual Items
var objects = app.activeDocument.selection;
} else if (app.activeDocument.selection[0].pathItems) {
// Group/Compound Shape
var objects = app.activeDocument.selection[0].pathItems;
} else {
alert('Please select a path or group.');
}
// Collect info
// TODO, if no selection returned, objects.length is undefined
// and shows an error, not important, and still works fine, though
if (objects.length > 0) {
var totalArea = 0;
for (var i = 0; i < objects.length; i++) {
if (objects[i].area) {
// If negative, make it positive
var totalArea = totalArea + Math.abs(objects[i].area);
}
}
// 12.4451 is a multiplier that converts area returned to square milimeters.
// I guess Illustrator returns inches or feet, no idea really.
// Anyway, this value has been cherry picked from my own calculations.
totalArea = 12.4451 * totalArea / 10000;
// this part helps presenting a prettier alert
var area = "area";
if (i != 1) {
area = "areas";
}
// Conversions
/*var ppi = 72;
var areaInInches = Math.round((totalArea / ppi / ppi) * 100) / 100;
if (areaInInches < 0) var areaInInches = -areaInInches;*/
// Display, I assume that 1 square milimeter = 1 square meter.
alert('Shape Area\n' + totalArea.toFixed(2) + ' square meters \n' + i + " " + area);
}
}
@nanoSpawn
Copy link
Author

Changed the way I make the area always positive, leaving it to be handled by the Math object.
Also, limiting the decimals shown in the resulting alert.

@Plomhub
Copy link

Plomhub commented Nov 28, 2017

Hi NanoSpawn. I really, really, appreciated your script. I'm going to need it and i wanted to say thank you. I also have to ask you a little help: doing some testing i noticed that if i divide an area A from an area B (with pathfinder -> divide) the script result area is A+B instead of B-A. Can you help me find a solution for that? Again, thank you very much. Have a nice day.

@Paputto0
Copy link

The script is quite helpful. Thanks! I just got one tip for you:

` // 12.4451 is a multiplier that converts area returned to square milimeters.
// I guess Illustrator returns inches or feet, no idea really.
// Anyway, this value has been cherry picked from my own calculations.

    totalArea = 12.4451 * totalArea / 10000;`

In my opinion it would be wise to use those numbers as a constant for readability :)

@nanoSpawn
Copy link
Author

@Plomhub It's unforgivable I missed your comment back in the day. Since I never returned to this repo, and somehow the mail was skipped, didn't see it. I'll try to have a look into that and fix it.

@Paputto0 You've got a fair point there. Didn't do it back in the day because the number never appears anymore, could also even simplify the operation doing a simple multiplication to 0,00124451, reducing it at a single math operation. Will attempt to find time this weekend to fix this. Didn't expect a fork I quickly made to solve a problem of mine would get reviewed.

@schroef
Copy link

schroef commented Feb 13, 2021

You have some errors in this script, i think your method of multiplying is wrong. Also at the end you state "1mm = 1meter" that kinda weird right.
On a test square of 1cm it returns 1 m square.

PS illustrator works in points, the original script showed this i believe and also has a working method for this. Im busy making a dialog for this script and im trying to solve the issue with compound paths and compoundshapes. I thought it was working, someone posted a method to do recursive checks, but that cant do CompoundPathItems properly neither.

This is what i got so far, i added some options

getShapeArea-dialog-v002
getShapeArea-dialog-v002-alert

@saphiroko
Copy link

This is exactly what I'm looking for! @schroef, I'm not getting the dialog box to choose units. I'm using Illustrator 2021. The plugin works otherwise!

@schroef
Copy link

schroef commented Apr 30, 2021

@saphiroko im using a altered version

@P-D-Mac
Copy link

P-D-Mac commented Aug 12, 2021

@schroef Did you get yours working? Would you be willing to share it?

@schroef
Copy link

schroef commented Sep 12, 2021

@P-D-Mac yes i am, i still need to drop them in a repo. Will try to do asap, ill tag you when i do. Otherwise do remind me, im currently doing a lot of PS scripting and some panel work.

@nanoSpawn
Copy link
Author

nanoSpawn commented Sep 13, 2021

I'll need to get back to this repo someday. It's an abandoned project I used to quickly fix stuff from the original code I forked. It's true that it has some mistakes, but since I no longer use Illustrator it's unlikely I'll go back to this. And also, I am not a devoted coder and I didn't study CS, I lack knowledge to generate proper code. I just suited a script to my needs while still keeping code public, because in all honestly, this does not belong to me.

Anyway, Shchroef, if you make a push I'll be glad to include any fixes from you so in the event people find this in the future they won't get broken code.

@schroef
Copy link

schroef commented Sep 17, 2021

Hi @nanoSpawn i have 2 versions i was working on. I noticed they still have issues and wonder if it can actually be solved. Checking if items are compound shapes with cutouts is very tricky!

ive havent touched these in couple months, i added both versions i was working. I remember there was something odd going on in them. There could be some alerts in there as i was doing some testing

You can find them here
https://github.com/schroef/Illustrator-Scripts

PS this version is not based on your code, but its from the other fork from Bryan Buchanan and @alijaya contributor
I added a different UI and made it save items

@nanoSpawn
Copy link
Author

If yours allows to work in units other than feet/inches and has better code, I could as well delete my gist to avoid confusion and spreading of bad code. Will try to check soon.

@schroef
Copy link

schroef commented Sep 19, 2021

I made it so you can show 2 dimension at once and adjsut the decimals and to show total or sepatate shape areas

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