我想以编程的方式使用VBA为AutoCAD生成代码。代码将在AutoCAD中找到并替换文本。我发现AutoLISP允许使用脚本在AutoCAD中调用命令。但是,由于"Find“命令将导致弹出窗口,因此它无法通过脚本完成该过程(即(命令"Find”文本“替换文本”)。
有没有使用AutoLISP脚本查找和替换文本的方法?
发布于 2022-07-05 19:30:33
您要查找的AutoLsip中的特定命令是vl-string-subst
。但是,在找到字符串中的第一个匹配项后,此操作将停止。如果你想通过整个字符串替换每一场比赛,那么Lee有一个简单的解决方案来解决这个问题。您可能需要在脚本中的某个地方添加vl-load-com
才能使用vl-string-subst
。
;;--------------------=={ String Subst }==--------------------;;
;; ;;
;; Substitutes a string for all occurrences of another ;;
;; string within a string. ;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
;;------------------------------------------------------------;;
;; Arguments: ;;
;; new - string to be substituted for 'old' ;;
;; old - string to be replaced ;;
;; str - the string to be searched ;;
;;------------------------------------------------------------;;
;; Returns: String with 'old' replaced with 'new' ;;
;;------------------------------------------------------------;;
(defun LM:StringSubst ( new old str / inc len )
(setq len (strlen new)
inc 0
)
(while (setq inc (vl-string-search old str inc))
(setq str (vl-string-subst new old str inc)
inc (+ inc len)
)
)
str
)
https://stackoverflow.com/questions/72696324
复制相似问题