Illustrator Javascript to draw a path with more than 1,000 points

I don’t know why but it seems that Illustrator only allows Javascript’s setEntirePath() function to draw a path with up to 1,000 points. If you try to pass an array with more than 1,000 points to┬ásetEntirePath(), the program will return an “Illegal Argument” error. In some cases when 1,000 points are not enough, you might have to use pathPoint instead. With pathPoint, you are basically drawing a path point by point, rather than the entire path at once.

The following code is showing an example of how to draw a path with more than 1,000 points.

// entire_gear_profile is an array containing a whole bunch of (x, y) coordinates

if(entire_gear_profile.length > 1000)
{
    for( i = 0 ; i < entire_gear_profile.length ; i++ )
    {
        var path_point = gear_path.pathPoints.add();
        path_point.leftDirection = entire_gear_profile[i];
        path_point.anchor = entire_gear_profile[i];
        path_point.rightDirection = entire_gear_profile[i];
    }
}
else
{
    gear_path.setEntirePath( entire_gear_profile );
}

As you can see, the code checks the length of the array before getting to do the real job. If the array length is small than 1,000, the example still uses setEntirePath() because setEntirePath() function is more efficient than point-by-point process.

The following sample is a good skeleton codes with which anyone can easily start programming Illustrator with Javascript.

// Posted by zavax.wordpress.com
#target illustrator

// Constant
i2p = 72;   // inch to pt

// Preset
var doc_preset = new DocumentPreset;
doc_preset.units = RulerUnits.Inches;

// Create a new blank illustrator document and setup
var new_doc = app.documents.addDocument(DocumentColorSpace.RGB, doc_preset);
new_doc.defaultStroked = true;
new_doc.defaultStrokeWidth = 0.005;
var stroke_color = new RGBColor();
stroke_color.red = 255;
stroke_color.green = 0;
stroke_color.blue = 0;
new_doc.defaultStrokeColor = stroke_color;
new_doc.defaultFilled = false;

// Create a new layer
var new_layer = new_doc.layers.add();

// Replace the following sample odes with yours

//***************
// The sample codes is drawing a circle with 2,000 points
var length = 2000;
var vertex = new Array(length);

for(i=0;i<length;i++)
{
    var angle = 2 * Math.PI / length * i;
    vertex[i] = new Array(Math.cos(angle) * i2p, Math.sin(angle) * i2p);
}
//***************

// Finally, draw the path
var new_path = new_layer.pathItems.add();

if( vertex.length > 1000 )
{
    for(i=0;i<length;i++)
    {
        pathPoint = new_path.pathPoints.add();
        pathPoint.leftDirection = vertex[i];
        pathPoint.anchor = vertex[i];
        pathPoint.rightDirection = vertex[i];
    }
}
else
{
    new_path.setEntirePath(vertex);
}

To be honest, javascript is not very easy to debug in the Adobe Extension Toolkit. For example, if you pass an empty array to setEntirePath() function, the IDE will return a “You cannot delete all path-points of a path” error, which is really confusing……

Advertisements