Need help change Lisp Routine to Selecting multiple Items

Need help change Lisp Routine to Selecting multiple Items

Anonymous
Not applicable
1,109 Views
5 Replies
Message 1 of 6

Need help change Lisp Routine to Selecting multiple Items

Anonymous
Not applicable

This is a lisp routine for adding prefixes and suffix to text but it will only let me change one at a time. How do I let it give me the option to window select or select multiple items at once?

 

(defun c:PST (/ PreSuf Str ent Cstr)
 (vl-load-com)
 (initget "P S")
 (setq PreSuf (getkword "\nChoose [Prefix/Suffix]  <Suffix>: "))
 (if (not PreSuf)
   (setq PreSuf "S")
 )
 (while (not str)
   (setq str (getstring T "\nEnter String: "))
   (cond ((and (eq str "")
 (princ "Null Input Try again")
 (setq str nil)
   )
  )
   )
 )
 (while (and (setq ent (car (nentsel "\nSelect Text/Attribute: ")))
      (member (cdr (assoc 0 (entget ent)))
       '("TEXT" "MTEXT" "ATTRIB")
      )
 )
   (setq ent  (vlax-ename->vla-object ent)
  Cstr (vla-get-textstring ent)
   )
   (vla-put-textstring
     ent
     (if (eq PreSuf "S")
(strcat Cstr "" str)
(strcat str "" Cstr)
     )
   )
 )(princ)
)

 

0 Likes
Accepted solutions (1)
1,110 Views
5 Replies
Replies (5)
Message 2 of 6

Kent1Cooper
Consultant
Consultant

I can imagine how to do that if you can live without the Attribute capability.  Selection with multiple-object possibility is going to "see" only the Block  that an Attribute is part of, not the Attribute itself -- that depends on the (nentsel) [= nested-entity selection] part of the code.

Kent Cooper, AIA
0 Likes
Message 3 of 6

Anonymous
Not applicable

Is there a way for it to work without the necessity of editing the attribute? Worse case scenario is I would just do an attribute explode and edit Text or MText from there. 

 

How would I go about rewriting the code without Attribute capability?

0 Likes
Message 4 of 6

dlanorh
Advisor
Advisor
Accepted solution

Try this re-written code.

 

The getkword options work on the initial letter, although the whole word is in the initget, as the initial letters are unique.

 

Select Prefix or Suffix - Default (return pressed = Suffix)

Enter Prefix or Suffix string

Select Option Text Attribute Exit - Default (return pressed = Text)

 

Text Option Selected - Item selection for text is by ssget, only Text and MText entities can be selected. Once right mouse is clicked or enter pressed the selected items are processed

 

Attribute Option Selected Item selection is by nentsel. Select Attribute and item is processed. It will then ask you to select another Attribute. This loop will continue until a null selection is made (select on blank area of drawing)

 

Exit Option Selected Quits Lisp

 

(defun c:PST (/ c_doc PreSuf str t_type ss ent obj)
  (vl-load-com)
  
  (setq c_doc (vla-get-activedocument (vlax-get-acad-object)))
  
  (initget "Prefix Suffix")
  (setq PreSuf "Suffix" 
        PreSuf (getkword (strcat "\nChoose [Prefix/Suffix]  < " PreSuf " > : "))
  );end_setq
  
  (if (not PreSuf) (setq PreSuf "Suffix"))
  
  (while (not str)
    (setq str (getstring T (strcat "\nEnter " PreSuf " String : ")))
    (cond ( (< (strlen str) 1)
            (princ "Null Input Try again")
            (setq str nil)
          )
    );end_cond
  );end_while
  
  (while (/= t_type "Exit")
    (initget "Text Attribute Exit")
    (setq t_type "Text" 
          t_type (getkword (strcat "\nSelect Option [Text/Attribute/Exit]  < " t_type " > : "))
    );end_setq
    (if (not t_type) (setq t_type "Text"))
    
    (cond ( (= t_type "Text")
            (prompt "\nSelect Text/MText Items : ")
            (setq ss (ssget '((0 . "TEXT,MTEXT"))))
            (vlax-for obj (vla-get-activeselectionset c_doc)
              (cond ( (= PreSuf "Prefix")
                      (vlax-put-property obj 'textstring (strcat str (vlax-get-property obj 'textstring)))
                    )
                    ( (= PreSuf "Suffix")
                      (vlax-put-property obj 'textstring (strcat (vlax-get-property obj 'textstring) str))
                    )
              );end_cond
            );end_for
            (setq ss nil)
          )
          ( (= t_type "Attribute")
            (while (setq ent (nentsel "\Select Attribute to convert to Text : "))
              (cond ( (= (cdr (assoc 0 (entget (car ent)))) "ATTRIB") 
                      (setq obj (vlax-ename->vla-object (car ent)))
                      (cond ( (= PreSuf "Prefix")
                              (vlax-put-property obj 'textstring (strcat str (vlax-get-property obj 'textstring)))
                            )
                            ( (= PreSuf "Suffix")
                              (vlax-put-property obj 'textstring (strcat (vlax-get-property obj 'textstring) str))
                            )
                      );end_cond
                    )
                    (
                      (alert "Not an Attribute \n\nYou MUST Select an Attribute")
                    )
              );end_cond
            );end_while  
          )
    );end_cond
  );end_while  
(princ)
);end_defun
(princ)

I am not one of the robots you're looking for

Message 5 of 6

Anonymous
Not applicable

Exactly what I needed. Perfect! Thanks for the help!

0 Likes
Message 6 of 6

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

How would I go about rewriting the code without Attribute capability?


 

Here's my take on it, for only Text/Mtext.  This one remembers both whether you want to add Prefix(es) or Suffix(es), and what you typed in for the content, and it prevents you from typing in just Enter if there's no prior content [Enter is OK to accept the prior string as default, when there is one] or only spaces.

Kent Cooper, AIA