• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help Calculating Black (raised area) vs White (recessed area)

Engaged ,
Apr 22, 2024 Apr 22, 2024

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);
TOPICS
Experiment , How-to , Scripting

Views

295

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Engaged ,
Apr 23, 2024 Apr 23, 2024

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.
Capture.GIF

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 23, 2024 Apr 23, 2024

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 `

areaIn = Math.abs(totalArea) / ppi / ppi` and it didn't matter because there were no other shapes...
 
You'll notice the alert I inserted about also includes the "polarity" value for each pathItem. When working with compound paths, typically the "knocked-out" areas have a negative polarity (and a negative area). So it would be pretty easy to calculate the raised and recessed areas if this was always the case. Unfortunately for some reason, your outermost rectangle is the one with the negative polarity and area.
 
I assume you found this post since your scripts seem similar and it has the same issue...
 
I'm sure it has something to do with how the compound path was built, but I honestly don't know why are how to account for the inconsistencies. Maybe someone else on here will.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 24, 2024 Apr 24, 2024

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:

  1. Duplicate the object
  2. Rasterize the dupe as Greyscale
  3. Image Trace the raster (will leave you with black and white shapes)

 

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 26, 2024 Apr 26, 2024

Copy link to clipboard

Copied

LATEST

@jduncan I'll give that a shot thanks!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 24, 2024 Apr 24, 2024

Copy link to clipboard

Copied

 

f13807eb59a3ac1237285668e7913591

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines