0

Edit:

Oops, it's working!
It seems that the problem has been solved by loading the updated script via File / Script / Other File... option. Before the stroke width change wasn't affecting the result. I guess it was some caching. I'll post the answer anyway, maybe it will be useful for someone.

Original question:

The purpose of the following Illustrator JavaScript is to create a rotated rectangle that spans the selected line and place these object in the middle of it:

  1. a small circle
  2. a vertical line rotated the same way as the rectangle
  3. a vertical line rotated the same way as the rectangle

I'm doing it by rotating the selected line with a reversed angle, creating objects and rotate everything back.

The problem is that the objects are not centered, as you can see below. It seems that the degree of misalignmet depends on a with of the selected line, but even with a zero width there is a problem. I tried to apply a zero width stroke to each object but it doesn't seem to work. They have some predefined width that I don't know how to change. Please help.
enter image description here
enter image description here

#target Illustrator

// Define the new color 1
var newRGBColor = new RGBColor();
newRGBColor.red = 255;
newRGBColor.green = 0;
newRGBColor.blue = 0;
app.activeDocument.defaultFillColor = newRGBColor;

// Define the new color 2
var newRGBColor_Green = new RGBColor();
newRGBColor_Green.red = 0;
newRGBColor_Green.green = 255;
newRGBColor_Green.blue = 0;

// Define the new color 3
var newRGBColor_Black = new RGBColor();
newRGBColor_Black.red = 0;
newRGBColor_Black.green = 0;
newRGBColor_Black.blue = 0;

var idoc = app.activeDocument;
// This doesn't seem to work
idoc.defaultStrokeWidth = 55;

// My desired angle
var ang = 11.947;


// Stroke withs and element sizes.
// 1 point = 1p = 1/2660 m = 0.37594 mm
var ellRadius = 0.566/2.0/0.37594;
var ellDiameter = ellRadius*2.0;
var ellStrokeWidth = ellRadius/10.0;
var zeroStrokeWidth = 0.0001;
var defaultStrokeWidth = ellRadius;


var mySelection = new Object; 

// Get active selection
selections = idoc.selection;

for(i=0;i<selections.length;i++)  
{  
    mySelection = idoc.selection[i];

    mySelection.rotate(-ang);

    var newRectWidth = mySelection.width; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.  
    var newRectHeight = mySelection.height;  
    var newRectTop = mySelection.top; // Top is actually Visible top  
    var newRectLeft = mySelection.left; // dito for Left

    mySelection.rotate(ang);
    //mySelection.remove();container.pathItems. (top, left, width, height, undefined, true); 

    // Create a rectangle
    var rect1Full = idoc.pathItems.rectangle(newRectTop, newRectLeft, newRectWidth, newRectHeight);
    rect1Full.strokeWidth = zeroStrokeWidth;
    rect1Full.strokeColor = newRGBColor; // defined above
    rect1Full.fillColor = new NoColor();
    rect1Full.rotate(ang);

    // Create a vertical line (by reducing rectangle's width)
    var rect2Vert = idoc.pathItems.rectangle(newRectTop, newRectLeft, newRectWidth, newRectHeight); 
    rect2Vert.strokeWidth = zeroStrokeWidth;
    rect2Vert.width=0.0;
    rect2Vert.left=rect1Full.left+rect1Full.width/2.0;
    rect2Vert.strokeColor = newRGBColor; // defined above
    rect2Vert.fillColor = new NoColor();        
    rect2Vert.rotate(ang);

    // Create a horizontal line (by reducing rectangle's height)
    var rect3Horiz = idoc.pathItems.rectangle(newRectTop, newRectLeft, newRectWidth, newRectHeight);
    rect3Horiz.strokeWidth = zeroStrokeWidth;
    rect3Horiz.height=0.0;
    rect3Horiz.top=rect1Full.top-rect1Full.height/2.0;
    rect3Horiz.strokeColor = newRGBColor; // defined above
    rect3Horiz.fillColor = new NoColor();       
    rect3Horiz.rotate(ang);

    // Create an ellipse
    var ellip1 = idoc.pathItems.ellipse(rect1Full.top-rect1Full.height/2 +ellRadius, rect1Full.left+rect1Full.width/2.0 - ellRadius, ellDiameter, ellDiameter, undefined, true);
    ellip1.strokeWidth = zeroStrokeWidth;
    ellip1.strokeColor = newRGBColor_Black; // defined above
    ellip1.fillColor = newRGBColor_Green;
    ellip1.strokeWidth = ellStrokeWidth;

}
alert(selections.length+" objects replaced");
  • @CAI Oops, I'm sorry, I've just done it. – ellockie Apr 5 '16 at 11:55
0

I'm sorry for the confusion, but the script is working after a small correction:

// This doesn't seem to work
idoc.defaultStrokeWidth = 55;

// This does
idoc.defaultStrokeWidth = 0;

Before I put this huge number to check if it makes any difference, but there wasn't any. Later, after loading the script via "File / Script / Other File..." option the defaultStrokeWidth suddenly the new stroke started to be visible. And that seemed to be the root of the original problem. Like the visual boundaries are taken into consideration by Illustrator with the operations used, not path points positions.

So, also it seems that Illustrator was caching my script this time, although usually it doesn't. Strange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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