Skip to main content
Inspiring
February 17, 2023
Question

FInd the position is closest the item's path to place the snippet item

  • February 17, 2023
  • 1 reply
  • 490 views

Dear experts,

I need to place a polygon item from a snippet file (.idms) with a dimension of around 13.4mm. The problem is detecting the closest point to place the polygon around the selected item's path - I call the target item. I clipped the target and collected all the points of its path. The struggle I need to solve is:
1. Find the largest between the target's bound and its path to place a polygon.
2. After detecting this area, how to detect the closest point to place a polygon without intersecting the target's path?
I have attached pictures to demo some points where I want to place the polygon and my code.

app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;
app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
app.activeDocument.layoutWindows[0].transformReferencePoint = AnchorPoint.TOP_LEFT_ANCHOR;
app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
var doc = app.activeDocument;
var pages = doc.pages;
var objStylesGroups = doc.objectStyleGroups;
var userSelect = app.selection[0], currentLayer, currentPage, currentSpread;
var celebration = File("path of snippet/snippet.idms");
doc.zeroPoint = [0,0];
main();
function main() {
    if (checkUserSelection() == 1) {
        currentPage = userSelect.properties.parentPage;
        /* dimention of star with outer glow*/
        var dimensionStar = [13.432 + 3, 12.809 + 3];
        var stepImageBound = getDimension(userSelect, true);
        var stepImageWidth = getWidth(userSelect, false);
        var stepImageHeight = getHeight(userSelect, false);   
        userSelect.select();
        // CREATE ALPHA CHANEL PATH FOR SELECT ITEM
        var stepImgPoints = clippingPathObj(userSelect);

        /* ------- */
        if (stepImgPoints) {
            // find the position to place the polygon
        } else {
            alert("There is no clipping path applied!");
        }
    } else {
        alert("Please choose the item want to add polygon!");

    }
}

function distance(point1, point2) {
        const xDist = point2[0] - point1[0];
        const yDist = point2[1] - point2[1];
        return Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2));
}

function renderPolygon(position) {
    var star = currentPage.place(celebration, position)[0];
    star.clearTransformations();
    star.move(position, undefined);    
    return star;
}

function clippingPathObj(obj) {
    try {
        var objClipped = obj.graphics[0].getElements()[0].clippingPath;
        var propertiesToApply =
            {
                clippingType: ClippingPathType.ALPHA_CHANNEL,
                name: "alpha path",
                tolerance: 0,
                threshold: 255,
            }
        objClipped.properties = propertiesToApply;
        var pointList = objClipped.paths[0].pathPoints;
        var pathPoints = [];
        for (var i = 0; i < pointList.length; i++) {
            pathPoints.push(pointList[i]);
        }
        return pathPoints;
    } catch (e) {
        return false;
    }
}

function checkItem(arr, obj) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] === obj) {
            return true;
        }
    }
    return false;
}

function checkUserSelection() {
    if (app.selection.length == 0) {
        return 0;
    } else {
        return 1;
    }
}


I get stuck for over three days to find the solution but have no hope :(((
I appreciate any help. Thanks for your attention.

This topic has been closed for replies.

1 reply

m1b
Community Expert
Community Expert
February 17, 2023

Hi @ShaneLe147, You're actually asking a pretty hard question that I think would require a lot of work. You might have trouble getting free answers to it. - Mark

Inspiring
February 18, 2023

Hi @m1b ,

I know it's not easy question, so I only hope someone show me the solution and I will do a script by myself.

My solution was creating the rectangles to cover the item's bound and push the position [x1,y1,x2,y2] of rectangle that is not intersect with the item's path into the array.

Thanks for your attention and advise