Message 1 of 13
Insert space in text lisp problems
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi there,
The below lisp is used to insert space before letter X or Y in the text. But there are some problems with it. After load App and run it, selected texts are 0. In another word, it can't select text. Could you please look at it in your convenience? The lisp was attached as well, thank you very much in advance.
(vl-load-com) ; load activex support
(defun spc:main (cmd / _dataKeys white_spaces push_white_spaces condense_white_spaces ; local functions
SPACES adoc ctr AcDbEntity value)
; Anonymous function
(setq _dataKeys (lambda () (mapcar (function (lambda (ch) (mapcar (function (lambda (n) (strcat " " (itoa n) ch))) '(1 2 3 4 5)))) '("X" "Y"))))
;; insert white spaces inside string
;; size - [int] the requied length of white space
;; pattern - [string] the pattern to serach in string, white spaces is insert before
;; string - [string] the string to be modified
;; return the combined string or nil if pattern not found
(defun white_spaces (size pattern string / p s)
(if (setq p (vl-string-search (strcase pattern) (strcase string)))
(strcat (substr string 1 p) (progn (setq s "") (repeat size (setq s (strcat s " ")))) (substr string (1+ p)))
); if
); white_spaces
; Anonymous function
; Return T if text has the "key" + at lease extra one space
(setq _hasWhiteSpace (lambda (s) (vl-some (function (lambda (item) (vl-some (function (lambda (key) (vl-string-search (strcat " " key) s))) item))) (_dataKeys))))
(defun push_white_spaces ()
(vl-some
(function
(lambda (item)
(vl-some
(function
(lambda (key / dp nValue)
(if (setq dp (assoc (substr key 3 1) SPACES))
(if (setq nValue (white_spaces (cdr dp) key value))
(progn
(vla-put-textString AcDbEntity nValue)
(setq ctr (1+ ctr))
); progn
); if
); if
); lambda
); function
item
); vl-some
); lambda
); function
(_dataKeys)
); vl-some
); push_white_spaces
; condense texts, remove white spaces from text
(defun condense_white_spaces ()
(if (_hasWhiteSpace value)
(vl-some
(function
(lambda (item)
(vl-some
(function
(lambda (key / n0 n1 ch)
(if (setq n1 (vl-string-search key value))
(progn
(setq n1 (1+ n1) n0 n1 ch (substr value n0 1))
(while (eq ch " ")
(setq n0 (1- n0) ch (substr value n0 1))
); while
(vla-put-textString AcDbEntity (strcat (substr value 1 n0) (substr value n1)))
(setq ctr (1+ ctr))
); progn
); if
); lambda
); function
item
); vl-some
); lambda
); function
(_dataKeys)
); vl-some
); if
); condense_white_spaces
; here start spc:main
(setq SPACES '(("X" . 3) ("Y" . 10))) ; dotted pair
(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
(vla-startundomark adoc)
(setq ctr 0)
(if (ssget '((0 . "text,mtext") (1 . "*[ ][12345][XY]*")))
(progn
(vlax-for AcDbEntity (vla-get-activeselectionset adoc)
(setq value (vla-get-textString AcDbEntity))
(cond
((eq cmd 'PUSH)
(push_white_spaces)
(princ (strcat "\n" (itoa ctr) " text(s) pushed."))
); case
((eq cmd 'CONDENSE)
(condense_white_spaces)
(princ (strcat "\n" (itoa ctr) " text(s) condensed."))
); case
); cond
(vlax-release-object AcDbEntity)
); vlax-for
); progn
); if
(vla-endundomark adoc)
(vlax-release-object adoc)
(princ)
); spc:main
; push white spaces in texts
(defun c:sdf ()
(spc:main 'PUSH)
)
; condense white spaces from texts
(defun c:adf ()
(spc:main 'CONDENSE)
)
(defun c:unlall ()
(command "._layer" "_unlock" "*" "")
)
(defun c:thwall ()
(command "._layer" "_thaw" "*" "")
)