hi People
I'm writing a script to replace text numbers with some simple path numbers (for laser engraving).
I've set up an array of points for each replacement number path and all was going great but then right at the end it all went crazy.
it's as though values for the path read from the array, which are then modified for each position on the page are somehow updating the original source value ?!?!?
I can't for the life of me tell what is going on and have been banging my head for last two days.
I'm a complete javascript numpty so i'm assuming it's some glaring basic issue i just can't see.
really hoping someone can point out my problem.
to test, open an illustrator doc, add 3 text boxes, type in "0123" in the first, "04561" in the second and "78912" in the last,select all 3 and run the script.
you'll notice that any number appearing in more than one box will start wildly offsetting, because the original points in the array have been changed ???
any suggestions very greatly appreciated
cheers../jeff
//================================
//script to replace text numbers with laser cutting friendly path numbers
//letter shapes array:
var numShapes = [];
numShapes[0] = [[5.67,0.00],[0.00,5.67],[0.00,39.69],[5.67,45.35],[17.01,45.36],[22.68,39.69],[22.68,5.6 7],[17.01,0.00],[11.34,0.00]];
numShapes[1] = [[5.67,34.02],[17.01,45.35],[17.01,0.00]];
numShapes[2] = [[0.00,39.69],[5.67,45.35],[17.01,45.35],[22.68,39.69],[22.68,39.69],[22.68,28.35],[0.00, 0.00],[22.68,0.00]];
numShapes[3] = [[0.00,45.35],[22.68,45.35],[17.01,28.35],[22.68,22.68],[22.68,5.67],[17.01,0.00],[5.67,0 .00],[0.00,5.67]];
numShapes[4] = [[22.68,17.01],[0.00,17.01],[17.01,45.35],[17.01,0.00]];
numShapes[5] = [[22.68,45.35],[0.00,45.35],[0.00,28.35],[5.67,22.68],[17.01,22.68],[22.68,17.01],[22.68, 5.67],[17.01,0.00],[5.67,0.00],[0.00,5.67]];
numShapes[6] = [[5.67,22.68],[17.01,22.68],[22.68,17.01],[22.68,5.67],[17.01,0.00],[5.67,0.00],[0.00,5.6 7],[0.00,22.68],[11.34,45.35]];
numShapes[7] = [[0.00,45.35],[22.68,45.35],[5.67,0.00]];
numShapes[8] = [[22.68,17.01],[22.68,5.67],[17.01,-0.00],[5.67,-0.00],[0.00,5.67],[0.00,17.01],[5.67,22. 68],[17.01,22.68],[22.68,28.35],[22.68,39.68],[17.01,45.35],[5.67,45.35],[0.00,39.68],[0.0 0,28.35]];
numShapes[9] = [[11.34,0.00],[22.68,22.68],[22.68,39.69],[17.01,45.35],[5.67,45.35],[0.00,39.69],[0.00,2 8.35],[5.67,22.68],[17.01,22.68]];
// the scale of each the letter, std numbers are 8x16mm
var scalar = [1,1];
var myDoc = app.activeDocument;
selectedObjects = myDoc.selection;
//check for new layer to take number shapes
if(!doesLayerExist("laserText")){
laserText = myDoc.layers.add()
laserText.name = "laserText"
} else {
laserText = myDoc.layers.getByName("laserText")
}
for (var i=0;i<selectedObjects.length;i++){
// get the numbers as an array from the text item
var curText = selectedObjects[i].contents;
var curTextArray = arrayFromText(curText);
// Get position of text
var gb = selectedObjects[i].geometricBounds;
var origin = [gb[0],gb[3]+15]; //rough adjustment for baseline
var gridUnit = [28.347*scalar[0],28.347*scalar[1]]; //convert from points to mm
//draw each number shape
for (var j=0; j <curTextArray.length; j++){
var curNum = parseInt(curTextArray[j]);
var newPathItem = laserText.pathItems.add();
newPathItem.stroked = true;
newPathItem.filled = false;
curNumPath = numShapes[curNum];
//setup position of each new number
for (var k=0; k<curNumPath.length; k++){
curNumPath[k][0] = curNumPath[k][0] * scalar[0]+ origin[0];
curNumPath[k][1] = curNumPath[k][1] * scalar[1]+ origin[1];
}
newPathItem.setEntirePath(curNumPath);
//step origin to next grid unit over for teh next number
origin = [origin[0]+gridUnit[0],origin[1]];
}
}
//---------------- utility functions
function arrayFromText(inputText){
var input = inputText
var splitText = input.split("");
return splitText
}
function doesLayerExist(name) {
for (i=0; i<app.activeDocument.layers.length; i++) {
if (app.activeDocument.layers[i].name==name) return true;
}
return false;
}