AutoLISP: Copy Block with New Name

If you have ever needed to make an instance of a block unique so that it may have different geometry or whatever…. One way to do so is to open the the block in the Block Editor and then enter the command BSAVEAS. then close the Block Editor and then insert the newly named block.

But here’s a quick way to do it with LISP.

  • COPYBLOCK3 <enter> to star the LISP routine.
  • Select the block that is to be copied and given a new name.
  • Enter the new name for the copied block.
  • Place the newly named block copy.


~enjoy

;VVA

;make a copy of a block with a new name

;select block to copy

;enter new name unless anonymous block, then new name = ols name less *

;pick insertion point

(defun C:CopyBlock3 (/ *error* OldBlockName NewBlockName

rewind BlockName Info BlockInfo ent_name ent_info)

(defun *error* (Msg)

(cond

((or (not Msg)

(member Msg '("console break"

"Function cancelled"

"quit / exit abort"))))

((princ (strcat "\nError: " Msg)))

) ;cond

(princ)

) ;end error

(sssetfirst)

(setq OldBlockName (entsel "\nSelect Block to copy: "))

(while

(or

(null OldBlockName)

(/= "INSERT" (cdr (assoc 0 (entget (car OldBlockName)))))

)

(princ "\nSelection was not a block - try again...")

(setq OldBlockName (entsel "\nSelect Block to copy: "))

)

;block name

(setq OldBlockName (strcase (cdr (assoc 2 (entget (car OldBlockName))))))

(princ (strcat "\nSelected block name: " OldBlockName))

(if (= "*" (substr OldBlockName 1 1))

(setq NewBlockName (substr OldBlockName 2))

(setq NewBlockName (getstring T "\nEnter new block name: "))

)

(setq rewind T)

(while (setq Info (tblnext "BLOCK" rewind))

(setq BlockName (strcase (cdr (assoc 2 Info))))

(if (= OldBlockName BlockName)

(setq BlockInfo Info)

)

(setq rewind nil)

)

(if BlockInfo

(progn

(setq ent_name (cdr (assoc -2 BlockInfo)))

;header definition:

(entmake (list '(0 . "BLOCK")

(cons 2 NewBlockName)

'(70 . 2)

(cons 10 '(0 0 0))

)

)

;body definition:

(entmake (cdr (entget ent_name)))

(while (setq ent_name (entnext ent_name))

(setq ent_info (cdr (entget ent_name)))

(entmake ent_info)

)

;footer definition:

(entmake '((0 . "ENDBLK")))

(command "-INSERT" NewBlockName pause "1" "1" "0")

)

)

(*Error* nil)

(princ)

) ;end


Advertisements

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, Blocks, Modifying, TIPS. Bookmark the permalink.

8 Responses to AutoLISP: Copy Block with New Name

  1. hi sir,
    block rename is possible?

  2. hussain says:

    WHY I CANNOT COPY TO OTHER DRAWING. IS ANY PROBLEM IN COPY BLOCK? COULD U HELP ME

    • AutoCAD Tips says:

      Do you Get any error saying that the copy to clipboard failed?
      There probably is a “proxy” object in the selection set that you are trying to copy/paste.
      A proxy object can be created by Civil 3D or Autocad Plant… These are not simple AutoCAD objects that can be copied and pasted…

      You will have to explode the object, then copy & paste its geometry

  3. mok says:

    hi,
    idea with lisp is great but have some problems.
    when I put by accident wrong name, i.e incorrect sign in name (syntax error) then block is not done of course but all entities of block appear on drawing in about 0,0 point as exploded. Can this be changed? Also if name already exist then all other blocks are renamed/changed without notification (dangerous :)). Best if there would be text saying ‘wrong name’, or ‘name already exist’ – enter ne name.

  4. Rob says:

    Hi, its clean and (relatively) simple and I like it. Lee’s program is also brilliant but much more complex. Hard to tell wich is better , but I’m a big fan of kiss (keep it simple stupid :-) …)

    thanx

  5. TranTin says:

    Thank you for helping. But when I use your LISP, I can’t use command EDIT BLOCK IN PLACE of AUTOCAD. I only use command EDIT BLOCK. It makes me much time when I edit many blocks. Please repair your LISP to perfect. Thank you very much.
    (I’m Vietnamese)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s