• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Sep 12, 2022 Sep 12, 2022

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.

Capture1.PNG

 

Kindly help me with this issue

Thank you 

TOPICS
Scripting

Views

465

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 12, 2022 Sep 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];
   
...

Votes

Translate

Translate
Adobe
LEGEND ,
Sep 12, 2022 Sep 12, 2022

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 12, 2022 Sep 12, 2022

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 12, 2022 Sep 12, 2022

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:

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 12, 2022 Sep 12, 2022

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 13, 2022 Sep 13, 2022

Copy link to clipboard

Copied

LATEST

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

AG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 12, 2022 Sep 12, 2022

Copy link to clipboard

Copied

@G  Just change this line

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

to this

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines