AutoCAD Lisp to remove specific characters from Block Name

AutoCAD Lisp to remove specific characters from Block Name

kieran.leadbetter
Advocate Advocate
1,731 Views
7 Replies
Message 1 of 8

AutoCAD Lisp to remove specific characters from Block Name

kieran.leadbetter
Advocate
Advocate

Hello, would anyone have a lisp which would remove the characters "_1" from the end of any block names.

I know I can use the command Rename, then type *_1, and rename it to *, but I am unable to input this into a lisp for some reason as autocad doesn't allow -Rename to find blocks with a wild card

Kind Regards,

0 Likes
Accepted solutions (2)
1,732 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant

That will probably require stepping through the Block table and checking the ending of each name.  But not just that -- if there's a "GEORGE" Block and a "GEORGE_1" Block, you won't be able to rename the latter by taking the "_1" off the end.  Is there risk of that kind of conflict?  And if it finds such a conflict, what should it do with the "GEORGE_1" Block name?

Kent Cooper, AIA
Message 3 of 8

paullimapa
Mentor
Mentor
Accepted solution

You can try my BlockApps

https://apps.autodesk.com/ACD/en/Detail/Index?id=3309945226076337959&appLang=en&os=Win64

There’s a DDBlkF&R command which allows you to select blocks to rename and then you can Find “_1” and replace with nothing

But like @Kent1Cooper  replied the command would not rename the block if there’s an existing block with a matching name. 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 4 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

@Kent1Cooper wrote:

That will probably require stepping through the Block table and checking the ending of each name.  ...


Something like this [minimally tested]:

(defun C:BNS_1 ; = Block Name(s): Strip "_1" ending
  (/ blk oldname newname)
  (while (setq blk (tblnext "block" (not blk)))
    (if
      (and
        (wcmatch (setq oldname (cdr (assoc 2 blk))) "*_1")
        (not (assoc 1 blk)); not an Xref [no file path]
        (not (tblsearch "block" (setq newname (substr oldname 1 (- (strlen oldname) 2)))))
      ); and
      (command "_.rename" "block" oldname newname)
    ); if
  ); while
  (prin1)
)

It checks for the "wrong" ending you want removed, and for whether a Block name is an Xref, and for whether the truncated name is already used.  If the new name is already used, suggest something for it to do -- nothing [as the above], or notify the User, or supply a different suffix, or...?

Kent Cooper, AIA
Message 5 of 8

pendean
Community Legend
Community Legend
@kieran.leadbetter You most likely have BLOCKNAME_1 because BLOCKNAME already exists or existed when someone in charge of your block library did a copy-in-place to create this variation. Yes?

Which means every time to add that block back again, _1 is going to almost always reappear. Yes?
0 Likes
Message 6 of 8

kieran.leadbetter
Advocate
Advocate
This works great, I used this within my master file a while ago and it helped tidy up so many block name mistakes.

Thank you
0 Likes
Message 7 of 8

kieran.leadbetter
Advocate
Advocate
This is exactly what I need, and it works perfect too, impressive work as always.

Thank you again
0 Likes
Message 8 of 8

paullimapa
Mentor
Mentor

Glad to have helped…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes