重命名块最简单的方法是使用“rename”命令,但这里有一个快速的例子,展示了如何通过选择块来完成。
我对每个行项目进行注释以向您展示过程,并指出Tharwat对effectivename属性的看法。
- (defun c:rename-block ()
- ;Get the Block Definition Collection from the Active Document - Get the Active Document from the Acad Object
- (setq blks (vlax-get-property (vlax-get-property (vlax-get-acad-object) 'ActiveDocument) 'Blocks))
- ;Prompt the User to Select the Block to Rename
- (princ "\Select Block to Rename: ")
- (setq ss (ssget ":s" '(( 0 . "INSERT"))))
- ;If a Block was selected then proceed
- (if ss
- (progn
- ;Get the Block Reference form the Selection Set
- (setq blk-ref (vlax-ename->vla-object (ssname ss 0)))
- ;Get the Name of the Block - Must use EffectiveName is case it's a Dynamic Block
- (setq eff-name (vlax-get-property blk-ref 'EffectiveName))
- ;Prompt User for New Name and check if the Name Exists - See get-new-name Below
- (setq new-name (get-new-name eff-name blks))
- ;Get the Block Definition from the Block Collection
- (setq blk-def (vla-item blks eff-name))
- ;Catch any unhandled exceptions
- (setq msg (vl-catch-all-apply 'vlax-put-property (list blk-def 'Name new-name)))
- ;If msg is an error Display the error
- (if (vl-catch-all-error-p msg)
- (alert (vl-catch-all-error-message msg))
- (princ (strcat "\nBlock " eff-name " has been renamed to " new-name "."))
- )
- )
- )
- (princ)
- )
- (defun get-new-name (eff-name blks / new-name msg)
- ;Display the Current Name of the Block
- (princ (strcat "\nCurrent Name: " eff-name))
- ;Prompt for a New Name
- (setq new-name (getstring T "\nEnter New Name for Block: "))
- ;Catch any unhandled exceptions
- (setq msg (vl-catch-all-apply 'vla-item (list blks new-name)))
- ;If an error is not produced then the block name already exists
- (if (vl-catch-all-error-p msg)
- new-name
- (progn
- ;Let user know that the new name already exists as a block name
- (princ (strcat "\n" new-name " already exists."))
- ;Pompt the user to select a new name
- (get-new-name eff-name blks)
- )
- )
- )
|