Copy link to clipboard
Copied
I have a script that calculates the area of production art Positive Space vs Negative Space. In my TestFile, the layout on the left works but the layout on the right does not. Can anyone assist me in adjusting the script so it works every time?
function calculateRaisedArea() {
var decimalPlaces = 3;
var areaIn = 0;
var totalShapes = 0;
if (app.documents.length > 0) {
if (app.activeDocument.selection.length < 1) {
// alert('Select a path');
} 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
var totalArea = 0;
for (var i = 0; i < objects.length; i++) {
if (objects[i].area) {
totalArea += objects[i].area;
totalShapes++;
// Apply the graphic style to the current shape
var kerningOutline = app.activeDocument.graphicStyles.getByName("Kerning Outline");
kerningOutline.applyTo(objects[i]);
}
}
// Conversions
var ppi = 72;
areaIn = Math.abs(totalArea) / ppi / ppi;
}
return { area: areaIn, shapes: totalShapes };
}
function calculateTotalArea() {
var decimalPlaces = 3;
var largestArea = 0;
if (app.documents.length > 0) {
if (app.activeDocument.selection.length < 1) {
// alert('Select a path');
} 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.');
}
// Array to store paths and their areas
var pathsAndAreas = [];
// Calculate area for each path and store in array
for (var i = 0; i < objects.length; i++) {
if (objects[i].area) {
var ppi = 72;
var area = Math.abs(objects[i].area) / ppi / ppi;
pathsAndAreas.push(area);
}
}
// Find the largest area
for (var j = 0; j < pathsAndAreas.length; j++) {
if (pathsAndAreas[j] > largestArea) {
largestArea = pathsAndAreas[j];
}
}
}
return largestArea;
}
// Call the functions to execute the script
var raisedAreaObj = calculateRaisedArea();
var raisedArea = raisedAreaObj.area;
var totalShapes = raisedAreaObj.shapes;
var totalArea = calculateTotalArea();
var recessedArea = (totalArea - raisedArea).toFixed(3);
alert('Total Area: ' + totalArea.toFixed(3) + ' in\n' +
'Raised Area: ' + raisedArea.toFixed(3) + ' in\n' +
'Recessed Area: ' + recessedArea + ' in\n' +
'Number of Shapes: ' + totalShapes);
Copy link to clipboard
Copied
The 2nd one on the right side should have more Raised square inches and for some reason does not, and I cannot figure out why.
Copy link to clipboard
Copied
@BryanPagenkopf, there are some issues with your script. First off, if you notice in the last screenshot you sent, the raised area of the right shape 15.078 is exactly .922 less than the raised area of the left shape, and that is the exact amount the recessed area increased on the right shape. If you run your script on just the letters, you'll notice their raised area is, you guessed it, .922.
If you were to add the alert included below at the exact area shown inside of your calculateRaisedArea function, you would see what is happening.
First, the script starts with the letters and adds up their areas correctly, 528.791, 653.591, 528.791, 653.624, 714.146, 528.813, 528.813, 642.343 for a total of 4778.912... But then the script adds the area of the knocked-out rectangle 46656 (wrong since it is a recessed area) and then adds the area of the large outer rectangle -129600. Since the area of the outer rectangle is a negative number, that amount actually gets subtracted (wrong because it is a raised area.
function calculateRaisedArea() {
....
if (objects[i].area) {
totalArea += objects[i].area;
alert(
i + "\n" + objects[i].area + "\n" + totalArea + "\n" + objects[i].polarity
);
totalShapes++;
...
}
You got around this with the first shape because you used `Math.abs()` in your calculation `
Copy link to clipboard
Copied
So I thought of a quick hack that may get you close to what you need. Use your current script and if you detect a compoundPath then...
Steps:
Then continue with your script to sum the areas of the black shapes and white shapes separately which should give you an approximation of the info you want.
Copy link to clipboard
Copied
@jduncan I'll give that a shot thanks!
Copy link to clipboard
Copied