我建议在这项任务中使用正则表达式——以下应说明包含彩色和非彩色数字内容的多行文字,以及负数和小数:
- ;; Colour Numerical MText - Lee Mac
- (defun c:numcol ( / *error* enx idx rgx sel str )
- (defun *error* ( msg )
- (if (and (= 'vla-object (type rgx)) (not (vlax-object-released-p rgx)))
- (vlax-release-object rgx)
- )
- (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
- (princ (strcat "\nError: " msg))
- )
- (princ)
- )
-
- (if (setq sel (ssget "_:L" '((0 . "MTEXT") (1 . "*#*"))))
- (if (setq rgx (vlax-get-or-create-object "vbscript.regexp"))
- (progn
- (vlax-put-property rgx 'global actrue)
- (vlax-put-property rgx 'ignorecase acfalse)
- (vlax-put-property rgx 'multiline actrue)
- (vlax-put-property rgx 'pattern "(-?\\d+(?:\\.\\d)?\\d*)(?![0-9;}\\|])")
- (repeat (setq idx (sslength sel))
- (setq enx (entget (ssname sel (setq idx (1- idx))))
- str (assoc 1 enx)
- )
- (entmod (subst (cons 1 (vlax-invoke rgx 'replace (cdr str) "{\\C1;$1}")) str enx))
- )
- )
- (princ "\nUnable to interface with RegExp object.")
- )
- )
- (*error* nil)
- (princ)
- )
- (vl-load-com) (princ)
|