Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lisp to swap block Names

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Anonymous
2036 Views, 7 Replies

Lisp to swap block Names

Hi, I'm looking for a lisp routine that asks me to select the 1st block I want to change and then select the 2nd block I want to switch it's name with, so that when finished the 2 block names have been switched.

Can anyone help me with this please? 

7 REPLIES 7
Message 2 of 8
greg_battin
in reply to: Anonymous

I don't know of any that can swap geometry. I do have one that will copy a block and then give you the option to give it a new name.

An explanation is seen at the link below.

http://autocadtips.wordpress.com/2011/05/03/autolisp-copy-block-with-new-name/

Message 3 of 8
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

Hi, I'm looking for a lisp routine that asks me to select the 1st block I want to change and then select the 2nd block I want to switch it's name with, so that when finished the 2 block names have been switched.

....


If I'm interpreting what you want correctly, try this:

 

(setq

  name1 (cdr (assoc 2 (entget (car (entsel "\nSelect 1st Block for name swap: ")))))

  name2 (cdr (assoc 2 (entget (car (entsel "\nSelect 2nd Block: ")))))

)

(command

  "_.rename" "_block" name1 "SwapBlockName"

  "_.rename" "_block" name2 name1

  "_.rename" "_block" "SwapBlockName" name2

)

Kent Cooper, AIA
Message 4 of 8
wkiernan
in reply to: Anonymous

Here's something that does what you want, but it requires DOSLIB for the dialog boxes with the lists of block names.  You can get DOSLIB for free here:

 

http://www.en.na.mcneel.com/doslib.htm

 

This is how SWAP_BLOCK_NAMES works.  It goes through the BLOCK table and makes a list of all the block names that don't start with "*" (those are anonymous blocks which can't be renamed.)  All the block names are raised to upper-case so they will sort right.  Next it sorts the block names in alphabetical order.  If there are fewer than two non-anonymous blocks defined in your drawing it bails out; else, using the DOS_LISTBOX function from DOSLIB, it shows you a list of these block names from which you pick one.  Now it goes through that list of block names and makes a second list, consisting of all the block names except the first one you chose.  It displays this second list of block names using DOS_LISTBOX, and you choose the second block name.  Next it makes up a temporary name, checking to make sure that there isn't a block by that temporary name currently defined in your drawing.  Finally it invokes the "rename" command three times in a row to:

 

rename the first block name as the temporary name,

rename the second block name as the first block name, and

rename the temporary name as the second block name.

Message 5 of 8
Kent1Cooper
in reply to: Kent1Cooper

I suppose it would be a good idea to protect against the possibility of selecting two insertions of the same Block:

  

(setq

  name1 (cdr (assoc 2 (entget (car (entsel "\nSelect 1st Block for name swap: ")))))

  name2 (cdr (assoc 2 (entget (car (entsel "\nSelect 2nd Block: ")))))

)

(if (/= name1 name2)

  (command; then

    "_.rename" "_block" name1 "SwapBlockName"

    "_.rename" "_block" name2 name1

    "_.rename" "_block" "SwapBlockName" name2

  )

  (alert "Blocks have the same name."); else

)

 

And you could add filtering for entity type, etc.

Kent Cooper, AIA
Message 6 of 8
Anonymous
in reply to: greg_battin

Thankks Greg, not exactly what I was looking for but I can see this being very usefull too, cheers.

Message 7 of 8
Anonymous
in reply to: Kent1Cooper

Thanks Kent, works perfectly, problem I had is that components are tagged by block name, but then tags are often switched around/redone and it's a lot easier now to swap tags with your routine instead of redoing the block or going through the renaming which can get confusing, thanks again.

Message 8 of 8
Anonymous
in reply to: wkiernan

Thanks wkiernan, I went with Kent's solution but I appreciate your help.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost