Jump to content

Lisp To Select Multiple Text and MText Values Entered


Recommended Posts

Posted

For example, there are texts containing area, m2, 50, door, etc. I could select them one by one thanks to QuickSelect, Text or MText, Contents and Wildcard Match, but I want to select them all at the same time like entering Area*, m2*, etc. and select all texts and mtexts containing those values like selecting them with wildcard match or a lisp to select text and mtext containing the values I want to select in the entire drawing, like if you select area:50m2, it would select area:75m2 or area 40m2 as well. Either one of these kinds of lips would help. Thanks in advance.

Posted (edited)

This will select/highlight any text that starts with "Area" or ends with "m2".

 

(defun C:seltxt (/ SS)
  (setq SS (ssget "_X" (list '(0 . "*TEXT") '(1 . "Area*,*m2") (cons 410 (getvar 'ctab)))))
  (sssetfirst nil ss)
  (princ)
)

 

Edited by mhupp
updated code.
  • Like 3
Posted

@mhupp as long as you are highlighting objects then I believe it would be much better to get the ones in current active space only than selecting all matching objects in the other invisible spaces.

  • Like 1
  • Agree 1
Posted (edited)

Thinking more about this maybe it would be  better to let the user decide what to search for. or use both top for common terms and this for not so common terms. You also have to input the * for wild cards and use , if you going to search for more then one term.  its also important to note when typing the search terms you can't use spaces.

 

(defun C:selTxt (/ SS txt)
  (setq txt (getstring "\nSearch Text for Terms: ")) ;hit enter for defult options.
  (if (eq txt "")
    (setq txt "Area*,*m2") ;set defult search options here
  )
  (if (setq SS (ssget "_X" (list '(0 . "*TEXT") (cons 1 txt) (cons 410 (getvar 'ctab)))))
    (sssetfirst nil ss)
    (prompt (strcat "\nText doesn't contain " txt))
  )
  (princ)
)

 

Example

Search Text for: area*,*m2                           This would work like above lisp.

Search Text for: *area*                                  This would select text that has the word "area" in it.

Search Text for: *door                                   This would select text that ends with "door"

Search Text for: area*,*m2,*area*,*door    This would select all 3 examples text in one go.

 

Edited by mhupp
combined the two commands into one.
  • Like 2
  • Thanks 1
Posted
On 12/6/2021 at 3:38 AM, mhupp said:

Thinking more about this maybe it would be  better to let the user decide what to search for. or use both top for common terms and this for not so common terms. You also have to input the * for wild cards and use , if you going to search for more then one term.  its also important to note when typing the search terms you can't use spaces.

 


(defun C:selTxt (/ SS txt)
  (setq txt (getstring "\nSearch Text for Terms: ")) ;hit enter for defult options.
  (if (eq txt "")
    (setq txt "Area*,*m2") ;set defult search options here
  )
  (if (setq SS (ssget "_X" (list '(0 . "*TEXT") (cons 1 txt) (cons 410 (getvar 'ctab)))))
    (sssetfirst nil ss)
    (prompt (strcat "\nText doesn't contain " txt))
  )
  (princ)
)

 

Example

Search Text for: area*,*m2                           This would work like above lisp.

Search Text for: *area*                                  This would select text that has the word "area" in it.

Search Text for: *door                                   This would select text that ends with "door"

Search Text for: area*,*m2,*area*,*door    This would select all 3 examples text in one go.

 

 

This lips is good, thanks. But, is there any way to enter multiple text, mtext values at the same time to select texts, mtexts containing either one of those values separately? 

Posted

I'm not understanding. you can search for multiple values at once just use a , in between them.

 

Do you then want to cycle through the text that match the search one at a time? to edit them or something?

Can you explain more what you want to do.

Posted

I want to select text with different content I will enter and move or delete them, as they are not useful to me.

Posted

You can use a simple (while  and just add the txt to a string.

 

(setq str "")
(while (/=  "" (setq txt (getstring "\nType text Enter to stop ")))
(setq str (strcat str "," txt))
)

 

 

  • Like 3
Posted
8 hours ago, BIGAL said:

You can use a simple (while  and just add the txt to a string.

 


(setq str "")
(while (/=  "" (setq txt (getstring "\nType text Enter to stop ")))
(setq str (strcat str "," txt))
)

 

 

I couldn't make it work, but thanks.

Posted
56 minutes ago, Elektrik said:

I couldn't make it work, but thanks.

 

This works for me, selects text with user input as BigAl suggested and MHupps first bit of code (slight modification to both, using "(getstring T....)" to allow for spaces in the text string and (cons 1 str) which mhupp uses n his second code)

 

(defun C:seltxt (/ SS str)
  (setq str "")
  (while (/=  "" (setq txt (getstring T "\nType text Enter to stop ")))
    (setq str (strcat str "," txt))
  )
  (setq SS (ssget "_X" (list '(0 . "*TEXT") (cons 1 str) (cons 410 (getvar 'ctab)))))
  (sssetfirst nil ss)
  (princ)
)

 

Noting that this is case sensitive, not sure how to do it otherwise,  and it also accepts wold cards, search term Hello won't select 'Hello World' text in the drawing. but search Hello* should pick it up (and so should *llo wo*)

 

  • Like 3
Posted
1 hour ago, Steven P said:

 

This works for me, selects text with user input as BigAl suggested and MHupps first bit of code (slight modification to both, using "(getstring T....)" to allow for spaces in the text string and (cons 1 str) which mhupp uses n his second code)

 


(defun C:seltxt (/ SS str)
  (setq str "")
  (while (/=  "" (setq txt (getstring T "\nType text Enter to stop ")))
    (setq str (strcat str "," txt))
  )
  (setq SS (ssget "_X" (list '(0 . "*TEXT") (cons 1 str) (cons 410 (getvar 'ctab)))))
  (sssetfirst nil ss)
  (princ)
)

 

Noting that this is case sensitive, not sure how to do it otherwise,  and it also accepts wold cards, search term Hello won't select 'Hello World' text in the drawing. but search Hello* should pick it up (and so should *llo wo*)

 

This works great. Thank you all.

  • Like 1
Posted
17 minutes ago, Elektrik said:

This works great. Thank you all.

 

17 minutes ago, Elektrik said:

This works great. Thank you all.

 

Not me, I just copied and pasted BigAl and Mhupp.....

  • Like 1
  • 2 weeks later...
Posted
On 12/6/2021 at 7:38 AM, mhupp said:

Thinking more about this maybe it would be  better to let the user decide what to search for. or use both top for common terms and this for not so common terms. You also have to input the * for wild cards and use , if you going to search for more then one term.  its also important to note when typing the search terms you can't use spaces.

 


(defun C:selTxt (/ SS txt)
  (setq txt (getstring "\nSearch Text for Terms: ")) ;hit enter for defult options.
  (if (eq txt "")
    (setq txt "Area*,*m2") ;set defult search options here
  )
  (if (setq SS (ssget "_X" (list '(0 . "*TEXT") (cons 1 txt) (cons 410 (getvar 'ctab)))))
    (sssetfirst nil ss)
    (prompt (strcat "\nText doesn't contain " txt))
  )
  (princ)
)

 

Example

Search Text for: area*,*m2                           This would work like above lisp.

Search Text for: *area*                                  This would select text that has the word "area" in it.

Search Text for: *door                                   This would select text that ends with "door"

Search Text for: area*,*m2,*area*,*door    This would select all 3 examples text in one go.

 

dear mate, can you add automatically delete those selected text at the end of this lisp ? if would be great 

Posted
17 minutes ago, uzumaki narudin said:

dear mate, can you add automatically delete those selected text at the end of this lisp ? if would be great 

 

output of this routine is selection.

 

so, just add 

(command "erase" )

 

before (princ), after ) in the last section

 

 

  • Agree 1
  • Thanks 1
Posted
5 minutes ago, exceed said:

 

output of this routine is selection.

 

so, just add 

(command "erase" )

 

before (princ), after ) in the last section

 

 

thanks mate.. working 

  • 3 years later...
Posted

Bonjour, auriez-vous un lisp pour sélectionner tous les textes et tous les textes multiples du fichier en même temps, au lieu de procéder à 2 sélections rapides l'une après l'autre svp?

Merci 

Juan

Posted (edited)
5 hours ago, Juan440 said:

Bonjour, auriez-vous un lisp pour sélectionner tous les textes et tous les textes multiples du fichier en même temps, au lieu de procéder à 2 sélections rapides l'une après l'autre svp?

Maybe this will do?
First, specify the sample text, then specify the search window.

;; author koMon  10.11.2024
;; Lisp select mtexts (including multi-line text) according to the specified sample text
(defun c:Sel-mtxt-filter ( )
(setq pattern (getpropertyvalue (car (entsel "\nSample MText for filtering: ")) "text")
      mtext_filtered_sset (ssadd)
)
(if (setq mtext_sset (ssget '((0 . "mtext"))))
  (sssetfirst nil
	  (foreach mtext  
		  (vl-remove-if-not '(lambda (mtext) (vl-string-search pattern (getpropertyvalue mtext "text")))
		    		     (vl-remove-if 'listp (mapcar 'cadr (ssnamex mtext_sset)))
		  )
	    	  (ssadd mtext mtext_filtered_sset)
	  )
  )
)
 (princ)
 )

 

Edited by Nikon
Posted

Select all the text and mtext at once, you can use a wildcard in 'ssget',*

 

(defun c:selecttxt ( / MySS)
  (setq MySS (ssget "_X" (list '(0 . "*TEXT"))))
)

 

  • Like 1
Posted

Nikon, I'd consider adding a check that a suitable text was selected, quick and dirty like this:

 

;;Loop till a mtext is selected.
  (while (not (equal (assoc 0 (entget (setq MyEnt (car (entsel "Select Mtext"))))) '(0 . "MTEXT") ))
    (princ "\nNo, Please ")
  )

 

Though of course if no mtext can be selected or the user changes their mind escape is the only way out so be wary of variables and error functions if needed.

 

If the text strings are always short (under 250 characters) your code can be shortened to something like this I think. The second ssget you can use a wildcard "*TEXT" to get all text types.

 

(defun c:test ( / )
;;Loop till a mtext is selected.
  (while (not (equal (assoc 0 (entget (setq MyEnt (car (entsel "Select Mtext"))))) '(0 . "MTEXT") ))
    (princ "\nNo, Please ")
  )

;;get text string. Assuming the search text is less than 250 characters else use another method
  (setq MyText (cdr (assoc 1 (entget myent))))

;;get a selection set. Again assuming text strings less then 250 characters.
  (princ "\nNow select texts to search: ")
  (setq MySS (ssget (list (cons 0 "MTEXT")(cons 1 (strcat "*" MyText "*")))))

;; do what you want here with MySS

  (princ)
)

 

 

a slight different take on yours for longer texts which should work for LT too

 

(defun c:test ( / MyText MySS FinalSS acount)
;;Loop till a mtext is selected.
  (while (not (equal (assoc 0 (entget (setq MyEnt (car (entsel "Select Mtext"))))) '(0 . "MTEXT") ))
    (princ "\nNo, Please ")
  )
  (setq MyText (cdr (assoc 1 (entget myent)))) ;; selected text string

;;Get the texts to search.
  (princ "\nNow select texts: ")
  (setq MySS (ssget (list (cons 0 "*TEXT"))))

;; Loop through texts adding to selection set where text is found
  (setq acount 0)
  (setq FinalSS (ssadd))	
  (while (< acount (sslength MySS))
    (setq MyEnt (entget (ssname MySS acount)))
    (if (or
        (wcmatch (strcase (cdr (assoc 1 MyEnt))) (strcat "*" (strcase Mytext) "*" )) ;; last 250 characters
        (if (cdr (assoc 3 MyEnt))(progn
          (wcmatch (strcase (cdr (assoc 3 MyEnt))) (strcat "*" (strcase Mytext) "*" )) ;; first 250 characers
          (wcmatch (strcase (cdr (assoc 3 MyEnt))) (strcat "*" (strcase Mytext) "*" )) ;; Middle 250 characters
        )) ; end if, end progn assoc 3
      ) ; endor
      (progn ;; found
        (setq FinalSS (ssadd (ssname MySS acount) FinalSS) )
      )
      (progn ;; not found
      )
    ) ; end if
    (setq acount (+ acount 1))
  ) ; end while

;; do what you want here with found texts
  FinalSS

)

 

 

  • Like 2
  • Thanks 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...