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?
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
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/
@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
)
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.
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.
Thankks Greg, not exactly what I was looking for but I can see this being very usefull too, cheers.
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.