1

I am trying to write a function that takes a PathPoint and returns its index relative to it's parent PathItem. Something like this:

function getPathPointIndex(pathPoint) {  
    var pathPoints = pathPoint.parent.pathPoints,  
        i,  
        length = pathPoints.length;

    for (i = 0; i < length; i++) {
        if (pathPoint === pathPoints[i]) {  
            return i;  
        }  
    }

    return false;
}

However I have found when working with PathPoint objects even if they represent the same point they rarely identify as the same object. This can be demonstrated with the following example. Create a new Illustrator document and draw a path with at least two points then run the following:

var pointA = app.activeDocument.pathItems[0].pathPoints[1],  
    pointB,  
    i,  
    length = pointA.parent.pathPoints.length;  

for (i = 0; i < length; i++) {  
    var pointB = pointA.parent.pathPoints[i];  

    if (pointA === pointB) {  
        $.writeln('index ' + i + ': Same');  
    } else {  
        $.writeln('index ' + i + ': Different');  
    }  
}  

On my system the pointA and pointB are never the same. It doesn't make much that a point doesn't exist in it's parent element.

You will notice that I used the second PathPoint (index 1) to demonstrate this issue. This is because the script works as expected if the first PathPoint (index 0) is used.

It would be great if somebody could please:

  1. Explain this phenomenon
  2. Provide a solution to testing if two PathPoints are the same
  3. Get the index of a PathPoint relative to its parent PathItem (this should be easy if point to can be achieved)

Thanks

2 Answers 2

1

This just doesn't work because in JavaScript you can't compare an object to an object, they will be different as far as JavaScript is concerned even if they have all the same properties. So you can only compare with primitive values: grab the coordinates of a point's leftControl, rightControl and anchor and then you can compare these 6 values against some other point's 6 values. This will work until there is a situation where 2 or more points are directly on top of each other.

-1

Yes it works quite as expected. See you do not own illustrator, nor does illustrator own your process. So to separate the two you get a wrapper object.

That wrapper serves a important role: Illustrator can not allow you to directly access the memory, because illustrator may have done memory restructuring, between the time you accessed the object and used it. This is not C and its not your system. Instead you get a object that can be checked to exist, this way you do not need to concern about underlying implementation. But this works the other way too, illustrator can not know about the details of javascript, because there may be any number of scripts and interfaces that access your system. The wrapper just gets done anew each time to avoid information passing from one script horror one system (imagine running javascript and python) to another.

Now the system could make a cache to see if you asked for this particular object earlier but that would make the objects unnatural for javascript. But you can do this caching yourself if you wish. so whever you fish out a point write the index down in the object. Or better yet work on your own index array.

Anyway being the same index is not the same as being same object. Anything can have changed since you last read the data. And i doubt even illustrator internally knowing points from each other, just their indexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.