Inspiring
April 22, 2024
Question
Help Calculating Black (raised area) vs White (recessed area)
- April 22, 2024
- 2 replies
- 804 views
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);