Jump to content

LISP to Rename BLock to Match its Layer Name


nod684

Recommended Posts

can anyone please help me formulate a lisp that will rename the block name to match its layer name?

what i need is to select multiple blocks on multiple layers and each block name should adopt its layer name. I have more than a thousand block here for 15 floor binded drawing

 

Example :

LayerName : E-Ligh-BR

BlockName : X-?????-?????$0$-E-Ligh-BR

Link to comment
Share on other sites

I don't think that can actually work. Each block name needs to be unique. If you rename one of them you rename all the references of it as well. What happens if you've got 2 different block types on the same layer, or does this never happen - i.e. you've got 1000 layers as well?

 

You mention binded. Those blocks came from the xref originally, there should have been a layer also coming from the xrefs named as follows: XrefName$LayerName. If you don't want this then you should rather just re-bind the DWG and use the Insert option instead of the Bind option.

 

BTW, that ...$0$... portion in the name actually tells me you had nested xrefs with duplicate blocks inside. Or at some point someone used RefEdit and it didn't close properly (which is quite usual for RefEdit :stop:). If this is the case, then what you're after is something which merges the "duplicate" block definitions and changes all references to the merged version then purges the old duplicates out. That might be a bit complex to write - depending on how "accurate" you want the test regarding equality of block definitions. If the test is simply to check if the 2 blocks have the same end-portion in their names (i.e. after the last $), then it might be much less complex.

 

If the later: Rather than write it for you, I'll give you some pointers:

 

  • Use tblnext to step through each block definition in the drawing.
  • Get the name of the current block using assoc with the relevant DXF code for a block definition's name. In this case 2.
  • For each name check if there's a $ in the name. Use wcmatch for this.
  • If such name, strip the preceding portion off it. Various ways of going about it. But here's a very simple example:

;; Say the name is stored in a variable name
(while (wcmatch name "*$*") (setq name (substr name 2)))

  • Now store that name together with the original name in a list for later use, use cons. But you should check if this name is already in that list using assoc. E.g.:

(if (setq found (assoc name SavedList))
 (setq SavedList (subst (cons name (cons OriginalName (cdr found))) found SavedList))
 (setq SavedList (cons (list name OriginalName) SavedList)))

  • Once you've run through all the blocks you've got a list containing sublists of duplicate blocks named differently.
  • From this you should be able to rename the 1st in each sublist to the "correct" name. You might have to check if such name already is defined - use tblsearch.
  • For the rest of the original names, you need to search for their references (use ssget if there's no nested blocks), else step through block definitions again and inside their definitions using entnext. Obtain the dxf data list using entget.
    • For each Insert object check its name against one of those originals.
    • If found use entmod to change its name to the new cleaned name.

     

    [*]Finally do a purge of the drawing.

It might be possible to combine all this into one loop and/or make it more efficient. But the principle is much the same whatever you do.

Link to comment
Share on other sites

  • 2 weeks later...
I don't think that can actually work. Each block name needs to be unique. If you rename one of them you rename all the references of it as well. What happens if you've got 2 different block types on the same layer, or does this never happen - i.e. you've got 1000 layers as well?

 

You mention binded. Those blocks came from the xref originally, there should have been a layer also coming from the xrefs named as follows: XrefName$LayerName. If you don't want this then you should rather just re-bind the DWG and use the Insert option instead of the Bind option.

 

BTW, that ...$0$... portion in the name actually tells me you had nested xrefs with duplicate blocks inside. Or at some point someone used RefEdit and it didn't close properly (which is quite usual for RefEdit :stop:). If this is the case, then what you're after is something which merges the "duplicate" block definitions and changes all references to the merged version then purges the old duplicates out. That might be a bit complex to write - depending on how "accurate" you want the test regarding equality of block definitions. If the test is simply to check if the 2 blocks have the same end-portion in their names (i.e. after the last $), then it might be much less complex.

 

If the later: Rather than write it for you, I'll give you some pointers:

 

  • Use tblnext to step through each block definition in the drawing.
  • Get the name of the current block using assoc with the relevant DXF code for a block definition's name. In this case 2.
  • For each name check if there's a $ in the name. Use wcmatch for this.
  • If such name, strip the preceding portion off it. Various ways of going about it. But here's a very simple example:

;; Say the name is stored in a variable name

(while (wcmatch name "*$*") (setq name (substr name 2)))

  • Now store that name together with the original name in a list for later use, use cons. But you should check if this name is already in that list using assoc. E.g.:

(if (setq found (assoc name SavedList))
 (setq SavedList (subst (cons name (cons OriginalName (cdr found))) found SavedList))

 (setq SavedList (cons (list name OriginalName) SavedList)))

  • Once you've run through all the blocks you've got a list containing sublists of duplicate blocks named differently.
  • From this you should be able to rename the 1st in each sublist to the "correct" name. You might have to check if such name already is defined - use tblsearch.
  • For the rest of the original names, you need to search for their references (use ssget if there's no nested blocks), else step through block definitions again and inside their definitions using entnext. Obtain the dxf data list using entget.
    • For each Insert object check its name against one of those originals.
    • If found use entmod to change its name to the new cleaned name.

    [*]Finally do a purge of the drawing.

It might be possible to combine all this into one loop and/or make it more efficient. But the principle is much the same whatever you do.

 

 

guess you are right. i would end up doing one block and deleting all the others

and re-inserting this block to their respective locations...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...