insert space to multiple text and mtext

insert space to multiple text and mtext

mruPRQUJ
Advocate Advocate
2,797 Views
27 Replies
Message 1 of 28

insert space to multiple text and mtext

mruPRQUJ
Advocate
Advocate

Hello,

Could you please write a lisp to insert space to multiple text and mtext? The space could be 3 time or 15 times letter width? Please see the image below, insert 10 times width of letter space before 1X, 2X, 3X,4X,5X and 1Y, 2Y, 3Y, 4Y 5Y, Thank you very much in advance. 🙂

 

mruPRQUJ_0-1686353858787.png

 

0 Likes
Accepted solutions (2)
2,798 Views
27 Replies
Replies (27)
Message 2 of 28

Moshe-A
Mentor
Mentor

@mruPRQUJ  hi,

 

Here is a nice function that will make your life better 😀

 

usage:

(white_spaces 10 "Mi" "Michael Ru") return => "          Michael Ru"

 

or

 

(white_spaces 3 "ha" "Michael Ru") return => "Mic   hael Ru"

 

works?

 

Moshe

 

 

;; 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 ch)
 (if (setq p (vl-string-search pattern string))
  (strcat (substr string 1 p) (progn (setq ch "") (repeat size (setq ch (strcat ch " ")))) (substr string (1+ p)))
 ); if
); white_spaces

 

Message 3 of 28

mruPRQUJ
Advocate
Advocate

Hi, the function (usage) are great! Could you please advise me how to add the usage (function) to the lisp? I can't thank you enough 🙂

 

(white_spaces 10 "Mi" "Michael Ru") return => "          Michael Ru"

or

(white_spaces 3 "ha" "Michael Ru") return => "Mic   hael Ru"

 

0 Likes
Message 4 of 28

Moshe-A
Mentor
Mentor

@mruPRQUJ 

 

Good Morning,

 

Have this story:

You are on business trip from work out of town and a rookie co-worker email you that he need your help on a design you are working (Huston we have a problem 😀) you are the only one that can help him. you have your laptop with AutoCAD installed. 

 

How do you thing you can help him, what are the options?

 

still thinking how is this related to this thread, think again.

 

Message 5 of 28

mruPRQUJ
Advocate
Advocate

Hi, please see attached CAD file, thanks a million. 🙂

0 Likes
Message 6 of 28

Moshe-A
Mentor
Mentor

@mruPRQUJ 

 

Nice to see that you quickly got the message hope next time you will be posting it as you open the thread (as i can recall you're been here for some time, maybe for some months and you already knows how this all works 😀)

 

so i saw the dwg and i do not understand where the spaces is to be insert i thought insert space in a middle of a text but from the dwg it's all sum with moving texts?

 

maybe a better sample of the state before and after will clear that?!

 

Moshe

 

Message 7 of 28

mruPRQUJ
Advocate
Advocate

Sorry, I did not say it clearly. You are right, this time is to sum moving text before 1X, 2X, 3X,4X,5X and 1Y, 2Y, 3Y, 4Y 5Y. Maybe I need to create another post  for inserting space before 1X, 2X, 3X,4X,5X and 1Y, 2Y, 3Y, 4Y 5Y.  I need this one as well for different project,  thank you very much! 🙂

0 Likes
Message 8 of 28

Moshe-A
Mentor
Mentor

no don't create another thread, just post the sample dwg with the state before and after

 

Message 9 of 28

komondormrex
Mentor
Mentor

hey,

check the following.

 

 

(defun c:insert_nth_space (/ 20_space_string mtext_ename_list mtext_value change_pos pattern_found)
	(setq 20_space_string (vl-list->string '(32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32))
	      mtext_ename_list (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "*text") (1 . "*[12345][XY]*"))))))
	)
  	(foreach mtext mtext_ename_list
		(setq mtext_value (cdr (assoc 1 (entget mtext))))
  		(vl-some '(lambda (pattern) (setq change_pos (vl-string-search pattern mtext_value)))
			  '("1X" "2X" "3X" "4X" "5X" "1Y" "2Y" "3Y" "4Y" "5Y")
	 	)
	  	(setq pattern_found (substr mtext_value change_pos 2) 
	  	      mtext_value (vl-string-subst (strcat (substr 20_space_string 1 10) pattern_found) pattern_found mtext_value)
	  	)
	  	(entmod (subst (cons 1 mtext_value) (assoc 1 (entget mtext)) (entget mtext)))   
	)
  	(princ)
)

 

Message 10 of 28

mruPRQUJ
Advocate
Advocate

Could you please advise me if it works on both text and mtext? how to adjust space width? thanks.

0 Likes
Message 11 of 28

komondormrex
Mentor
Mentor

check update on ssget filter above.

Message 12 of 28

mruPRQUJ
Advocate
Advocate

Hi, the lisp is great! I tried it, but I did not know how to change width space. Could you please provide some advice to me? Thanks a lot! 🙂

0 Likes
Message 13 of 28

komondormrex
Mentor
Mentor
Accepted solution

hi, there it is.

(substr 20_space_string 1 10)

10 is the number of spaces.

Message 14 of 28

mruPRQUJ
Advocate
Advocate

Thanks a million!

0 Likes
Message 15 of 28

mruPRQUJ
Advocate
Advocate

Hi, white space is also useful for different project. Is it possible to use the same  DWG and insert 10 space before 1X, 2X, 3X, 4X, 5X, 3 space before 1Y, 2Y, 3Y,4Y, 5Y. I can’t thank you enough.

0 Likes
Message 16 of 28

komondormrex
Mentor
Mentor

hi,

sure.

check the code following.

(defun c:insert_nth_space (/ 20_space_string mtext_ename_list mtext_value change_pos pattern_found)
	(setq 20_space_string (vl-list->string '(32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32))
	      mtext_ename_list (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "*text") (1 . "*[12345][XY]*"))))))
	)
  	(foreach mtext mtext_ename_list
		(setq mtext_value (cdr (assoc 1 (entget mtext))))
  		(vl-some '(lambda (pattern) (setq change_pos (vl-string-search pattern mtext_value)))
			  '("1X" "2X" "3X" "4X" "5X" "1Y" "2Y" "3Y" "4Y" "5Y")
	 	)
	  	(setq pattern_found (substr mtext_value (1+ change_pos) 2) 
	  	      mtext_value (vl-string-subst (strcat (substr 20_space_string 1 (if (wcmatch pattern_found "*X") 10 3)) pattern_found) pattern_found mtext_value)
	  	)
	  	(entmod (subst (cons 1 mtext_value) (assoc 1 (entget mtext)) (entget mtext)))   
	)
  	(princ)
)

 

0 Likes
Message 17 of 28

Moshe-A
Mentor
Mentor

@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)
)

 

 

 

 

 

 

0 Likes
Message 18 of 28

Moshe-A
Mentor
Mentor
Accepted solution

@mruPRQUJ ,

 

Good Morning, fixed a bug 🤣

 

Moshe

Message 19 of 28

mruPRQUJ
Advocate
Advocate

Hi, I am really sorry to reply to you so late, as I am tied up with my work. I will test it later, thank you very much! 🙂

0 Likes
Message 20 of 28

Moshe-A
Mentor
Mentor

@mruPRQUJ  hi,

 

What's the test result?

 

0 Likes