Insert space in text lisp problems

Insert space in text lisp problems

mruPRQUJ
Advocate Advocate
2,061 Views
12 Replies
Message 1 of 13

Insert space in text lisp problems

mruPRQUJ
Advocate
Advocate

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" "*" "")
)
0 Likes
2,062 Views
12 Replies
Replies (12)
Message 2 of 13

paullimapa
Mentor
Mentor

So what happens when you enter command ADF or SDF?  Are you not able to select any Text or MText objects?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 13

mruPRQUJ
Advocate
Advocate

I am not able to select text and mtext object, thanks.

0 Likes
Message 4 of 13

paullimapa
Mentor
Mentor

this is the restricted on text selection:

(ssget '((0 . "text,mtext") (1 . "*[ ][12345][XY]*")))

but even after changing to this so you can select text it still doesn't work:

(ssget '((0 . "text,mtext")))

so this code has some flaws


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 5 of 13

Kent1Cooper
Consultant
Consultant

@mruPRQUJ wrote:

I am not able to select text and mtext object, thanks.


Does it get to the point of asking you to, but nothing is found, or does it not even ask?

Kent Cooper, AIA
Message 6 of 13

mruPRQUJ
Advocate
Advocate

I was asked to select object, but the selected objects is 0, nothing is found, thanks 

0 Likes
Message 7 of 13

Kent1Cooper
Consultant
Consultant

This filter element for text content:

"*[ ][12345][XY]*"

means it will "see" among your selection only Text/Mtext whose content includes the specific combination of a space [which they didn't need to wrap in brackets] followed immediately by a single number between 1 and 5 followed immediately by either an X or a Y, with or without additional content before and/or after that three-character group.  Are you trying to select anything that does not contain a three-character grouping conforming to that pattern?

Kent Cooper, AIA
Message 8 of 13

mruPRQUJ
Advocate
Advocate

Yes, it works now. Could you please advise me how to change the filter? Does I need to change filter in multiple location? many thanks.

0 Likes
Message 9 of 13

Kent1Cooper
Consultant
Consultant

@mruPRQUJ wrote:

Yes, it works now. Could you please advise me how to change the filter? Does I need to change filter in multiple location? many thanks.


If you want no text-content filtering, so you can choose any Text/Mtext, just remove the text-content part -- see Message 4.  But @paullimapa tried that change and still had problems, which I wonder about if you say it's now working.  Does it work because you are now selecting only things that match the filter?

 

If you want to change [you didn't say eliminate] the filter to still restrict selection, but to some other criteria, we would need to know what those are.

 

Only the one place.  That kind of filter is part of an (ssget) function, and there's only one of those in the code you posted.

Kent Cooper, AIA
0 Likes
Message 10 of 13

mruPRQUJ
Advocate
Advocate

Sorry, I did not say it clearly. I want to keep the current filter, and add two more filters.

1. no blank space in the beginning of the text, 98 and 5W

2. there are 5 blank space in the beginning of the text 56 and Z3, thanks.

0 Likes
Message 11 of 13

mruPRQUJ
Advocate
Advocate

Hi there.

can the below items be added to the filter? 

6Z and R6

Inserted space is same with "X"

 

Please disregard my pervious reply, sorry about it, thanks. 🙂

0 Likes
Message 12 of 13

Sea-Haven
Mentor
Mentor

A good idea is to post a dwg so can test on real text.

 

Can use "*TEXT" as filter, will find Text Mtext Dtext & Rtext you may not have any of the last 2, so a simple filter. 

Message 13 of 13

mruPRQUJ
Advocate
Advocate

Hi there,

Please see attached DWG file. 3 spaces can be inserted before "1X", 10 spaces can be inserted before "2Y. Can 5 spaces be inserted before "5" and "Z"? Using "text" as a filter would be great. Many thanks.

0 Likes