VrekMat 发表于 2022-7-5 20:05:25

重命名块问题(自动

你好
 
今天,我在这个论坛和互联网上搜索了一个解决方案,但没有成功。。
 
我借用了一个“重命名块函数”,并试图用它修改块名,但重命名步骤失败:
 
;错误:自动化错误。未找到密钥
 
可能有什么问题?请参阅下面的代码。
 
(此外,代码甚至没有“找到”其他一些块,但至少找到了“ballod1”,但无论如何都无法重命名。这可能是什么原因造成的,这是我的第二个问题。可能有些块是“错误类型的”?可以改进函数以捕捉其他类型的块吗?)
 
(defun c:wbout(/ dn pa pawbdn doc bn nn layout i)


(vl-load-com)
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))       

(setq dn (getvar "dwgname"))
(setq pa (getvar "dwgprefix"))
(setq pawbdn (strcat pa "wb\\" dn))

(setq bn "Balloon1")
(setq nn "Raaaaverad")

(vlax-for layout (vla-get-layouts doc)
                   (vlax-for i (vla-get-block layout)
                           (if (and
                                  (= (vla-get-objectname i) "AcDbBlockReference")
                                 (= (strcase (vla-get-name i)) (strcase bn))
                              )
                  
               
                   ;;;(princ "\nHELLO\n")
                          (vla-put-name i nn)
                           )
                   )
        )
(princ "\nGOODBAJ\n")
(command "SAVE" pawbdn)


)

Hippe013 发表于 2022-7-5 20:15:25

这可能有助于为您指明正确的方向。
 
(defun c:List-Blocks ()
(vl-load-com)
(setq doc (vlax-get-property (vlax-get-acad-object) 'ActiveDocument))
(setq blks (vlax-get-property doc 'Blocks))
(setq cnt 0)
(vlax-for b blks
   (princ (strcat "\nBlock Name: " (vlax-get-property b 'Name) "-Count: " (rtos cnt 2 0)))
   (setq cnt (+ 1 cnt)))
(princ)
)
 
注意到我在哪里抓取积木集合了吗?

Tharwat 发表于 2022-7-5 20:28:11

欢迎来到CADTutor。
 
1-不能从块参照重命名块定义。
2-您需要确保图形中不存在块的新名称。
3-如果目标块名称是动态块,则需要通过函数vla get effectivename获取其名称,因为DXF或vla get name无法检索真实名称。
4-无需遍历图形中的所有块,只需使用vla项实名来重命名块。
 
祝你好运

Hippe013 发表于 2022-7-5 20:37:19

重命名块最简单的方法是使用“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)
   )
   )
)

VrekMat 发表于 2022-7-5 20:45:38

嗨,谢谢大家!
 
Hippe013,你的代码非常有用和有效,我只需要为我的目的做一些修改!我用这个和我从Tharwat找到的一些其他代码来选择一个块(https://autocadtips.wordpress.com/2012/09/19/autolisp-select-block-by-name/)
 
有了所有这些,我能够在脚本和批处理文件的帮助下创建一个非常好的函数,它完全满足了我的需要!再次感谢你的帮助!

Tharwat 发表于 2022-7-5 20:56:15

祝你好运Vrekmat,我真的很惊讶地看到我的一个退出旧套路被公布在公众面前

Hippe013 发表于 2022-7-5 21:03:33

我很高兴能帮上忙!

BlackBox 发表于 2022-7-5 21:13:18

FWIW-我只需选择要重命名的块,然后右键单击:
 

 
 
 
干杯
页: [1]
查看完整版本: 重命名块问题(自动