@mruPRQUJ hi,
As you request, here is my version that uses (white_spaces) function. it is mush longer then the other solution you already have but very VisualLISP style structured 😀
first lets put things accurately:
your request was to insert spaces before 1X, 2X, 3X,4X,5X and 1Y, 2Y, 3Y, 4Y 5Y - yes?
but the sample drawing you post show that one space is already in text so i think the key text to find here is:
" 1X" " 2X" " 3X" " 4X" " 5X" etc
i assume the text is coming from other lisp - am i right?!
lines 119-127, the program contains 2 commands:
1. PUSHSPC (push white spaces).
2. CONDSPC (condense white spaces) if you made a mistake or just want to revert back, this is the command.
selected texts must have at least two spaces before the key in order to be processed by this command.
line 91 declares:
(setq SPACES '(("X" . 10) ("Y" . 3)))
it defines the size of spaces for the X and Y. if you want something else, change these numbers.
enjoy
Moshe
(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 / i ch nValue)
(if (vl-string-search key value)
(progn
(setq i 0 nValue "")
(repeat (strlen value)
(setq i (1+ i) ch (substr value i 1))
(cond
((and
(eq ch " ")
(eq (substr value i 3) key)
)
(setq nValue (strcat nValue ch))
); case
((/= ch " ")
(setq nValue (strcat nValue ch))
); case
); cond
); repeat
(vla-put-textString AcDbEntity nValue)
(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" . 10) ("Y" . 3))) ; 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:pushSpc ()
(spc:main 'PUSH)
)
; condense white spaces from texts
(defun c:condSpc ()
(spc:main 'CONDENSE)
)