Copy link to clipboard
Copied
Hi All,
I'm working on a CAD document and symbol library. I need to place the symbols over the page items, so I used the page item name and symbol name to match once both the names matched then the position of the page item is used for adding the symbol to that position.
var pathItemLayer = currLayer.pageItems[a].name;
if(app.activeDocument.symbols.length > 0){
for(var s=0; s<app.activeDocument.symbols.length; s++){
symbolName = app.activeDocument.symbols[s].name;
if(String(pathItemLayer) == String(symbolName)){
var bounds = currLayer.pageItems[a].position;
var tmark = app.activeDocument.symbols.getByName(symbolName);
app.activeDocument.symbolItems.add(tmark).position = [Number(bounds[0]),Number(bounds[1])];
continue
}
}
}
I can able to place the symbol in that position, but I'm finding issues with that (Please find the screenshot). Both Pageitem and Symbol have the same width and Height. I tried to place the symbol manual over the page item fits exactly without getting the below error.
Kindly help me with this issue
Thank you 
This is a workaround because I can't figure out how to evaluate the "visible bounds" correctly:
This script "Translates" the placed Symbol to center on the original artwork:
var aDoc = app.activeDocument;
var pathItemLayer = aDoc.pageItems[0].name;
if(aDoc.symbols.length > 0){
for(var s=0; s<aDoc.symbols.length; s++){
symbolName = aDoc.symbols[s].name;
if(String(pathItemLayer) == String(symbolName)){
var myPageItem = aDoc.pageItems[0];
...
Copy link to clipboard
Copied
It appears your script is only a snippet of the complete script.
After altering it to work I was able to match a"Symbol" to an existing page item. Is it possible the Symbol does not share the same "Position" coordinates as the original page item? Have you tried expanding a Symbol then trying to match it with the Symbol using your script?
var aDoc = app.activeDocument;
var pathItemLayer = aDoc.pageItems[0].name;
if(aDoc.symbols.length > 0){
for(var s=0; s<aDoc.symbols.length; s++){
symbolName = aDoc.symbols[s].name;
if(String(pathItemLayer) == String(symbolName)){
var bounds = aDoc.pageItems[0].position;
var tmark = aDoc.symbols.getByName(symbolName);
aDoc.symbolItems.add(tmark).position = [Number(bounds[0]),Number(bounds[1])];
continue
}
}
}
Copy link to clipboard
Copied
@rcraighead, what I did for expanding the symbol is by placing the symbol in that position and then deleting it from the symbol library.
Sorry, I'm not getting this "Is it possible the Symbol does not share the same "Position" coordinates as the original page item"?
Copy link to clipboard
Copied
You can also expand a symbol instance by choosing "Break Link" from Control Bar or "Expand" from Object Menu.
Since my test correctly aligned the symbol to the expanded artwork I wonder if there is something different about your artwork.
By "Position" I am refering to the top left X, Y coordinate for each element (Symbol and expanded artwork).
Edit:
Have you verified there are no stray points in your artwork (group or Symbol)? Something must be different in the artwork.
This line:
Copy link to clipboard
Copied
This is a workaround because I can't figure out how to evaluate the "visible bounds" correctly:
This script "Translates" the placed Symbol to center on the original artwork:
var aDoc = app.activeDocument;
var pathItemLayer = aDoc.pageItems[0].name;
if(aDoc.symbols.length > 0){
for(var s=0; s<aDoc.symbols.length; s++){
symbolName = aDoc.symbols[s].name;
if(String(pathItemLayer) == String(symbolName)){
var myPageItem = aDoc.pageItems[0];
var tmark = aDoc.symbols.getByName(symbolName);
aDoc.symbolItems.add(tmark).position = myPageItem.position;
//Adjust Symbol position based on difference in width and height
aDoc.symbolItems[0].translate(((myPageItem.width - aDoc.symbolItems[0].width)/2), ((myPageItem.height - aDoc.symbolItems[0].height)*-1)/2);
continue
}
}
}
Copy link to clipboard
Copied
@rcraighead, This worked perfectly, thank you for taking the time to help me
AG
Copy link to clipboard
Copied
@G Just change this line
var bounds = currLayer.pageItems[a].position;
to this
var bounds = currLayer.pageItems[a].visibleBounds;