Skip to main content
Inspiring
September 12, 2022
Answered

How to place symbols exactly over an item using that item position

  • September 12, 2022
  • 3 replies
  • 1250 views

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 topic has been closed for replies.
Correct answer rcraighead

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
       }
    }
 }

3 replies

femkeblanco
Legend
September 12, 2022

@@G31_2  Just change this line

var bounds = currLayer.pageItems[a].position;

to this

var bounds = currLayer.pageItems[a].visibleBounds;

 

rcraighead
rcraigheadCorrect answer
Legend
September 12, 2022

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
       }
    }
 }
@G31_2Author
Inspiring
September 13, 2022

@rcraighead, This worked perfectly, thank you for taking the time to help me

AG

rcraighead
Legend
September 12, 2022

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
       }
    }
 }

 

@G31_2Author
Inspiring
September 12, 2022

@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"?

rcraighead
Legend
September 12, 2022

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:

aDoc.symbolItems.add(tmark).position = [Number(bounds[0]),Number(bounds[1])];
can be simplified to:
aDoc.symbolItems.add(tmark).position = bounds;