trim text and add bradkets

trim text and add bradkets

Anonymous
Not applicable
1,426 Views
11 Replies
Message 1 of 12

trim text and add bradkets

Anonymous
Not applicable

Hello all.  below is a small lisp routine where i can pick text and it will add brackets and an X to the front and a bracket to the end.  I would like to modify this so it will remove the last character of the text i pick before if adds the brackets and the X.  Can anyone help me modify this?

 

(defun c:P4( / i s x )
(if (setq s (ssget "_:L" '((0 . "TEXT") (1 . "~(*)"))))
(repeat (setq i (sslength s))
(setq e (entget (ssname s (setq i (1- i))))
x (assoc 1 e)
)
(entmod (subst (cons 1 (strcat "(X" (cdr x) ")")) x e))
)
)
(princ)
)

0 Likes
Accepted solutions (1)
1,427 Views
11 Replies
Replies (11)
Message 2 of 12

dlanorh
Advisor
Advisor
Please provide an example of the before and after text, as removing the last character could result in "Code" becoming "(X Cod)"; or is this a strip space or punctuation?

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

0 Likes
Message 3 of 12

Anonymous
Not applicable

before = 00300+

after = (X00300)

 

thanx

0 Likes
Message 4 of 12

dlanorh
Advisor
Advisor
Is it always a "+" at the end?

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

0 Likes
Message 5 of 12

Moshe-A
Mentor
Mentor

@Anonymous,

 

check this

 

(defun c:P4 (/ i s x l)
 (if (setq s (ssget "_:L" '((0 . "TEXT") (1 . "~(*)"))))
  (repeat (setq i (sslength s))
   (setq e (entget (ssname s (setq i (1- i))))
         x (assoc 1 e)
	 l (strlen (cdr x))
   )
   (entmod (subst (cons 1 (strcat "(X" (substr (cdr x) 1 (1- l)) ")")) x e))
  )
 )
 (princ)
)
0 Likes
Message 6 of 12

Anonymous
Not applicable

yes, it is always a "+"

 

thanx

0 Likes
Message 7 of 12

dlanorh
Advisor
Advisor
Accepted solution

OK

 

Try this

(defun c:P4( / i s e x txt)
  (if (setq s (ssget "_:L" '((0 . "TEXT") (1 . "~(*)"))))
    (repeat (setq i (sslength s))
      (setq e (entget (ssname s (setq i (1- i))))
            x (assoc 1 e)
            txt (substr (cdr x) 1 (1- (strlen (cdr x))))
      )
      (entmod (subst (cons 1 (strcat "(X" txt ")")) x e))
    )
  )
  (princ)
)

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

0 Likes
Message 8 of 12

Anonymous
Not applicable

Hi Moshe-A.  I see your solution was just to remove the last character.  it works for me.

 

thanks for you help

0 Likes
Message 9 of 12

Anonymous
Not applicable

Hi dlanorh,

 

this one worked too.  that everyone for the help

0 Likes
Message 10 of 12

dlanorh
Advisor
Advisor

Or using visual lisp

 

(defun c:P4( / c_doc ss)
  (vl-load-com)
  (setq c_doc (vla-get-activedocument (vlax-get-acad-object))
        ss (ssget "_:L" '((0 . "TEXT") (1 . "~(*)")))
  )      
  (if ss
    (vlax-for t_obj (vla-get-activeselectionset c_doc)
      (vlax-put-property t_obj 'textstring (strcat "(X" (vl-string-right-trim "+" (vlax-get-property t_obj 'textstring)) ")"))
    );end_for
  );end_if
  (princ)
);end_defun

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

Message 11 of 12

dlanorh
Advisor
Advisor

@Moshe-A's solution works too. I think you need to credit him once and not me twice. Robot wink

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

0 Likes
Message 12 of 12

Anonymous
Not applicable

Hello all,

 

I appologize for not giving credit proporly to the people that help.  There was not intent just my enexperince with this form.

 

so thanks again to all for the quick response and good solution

0 Likes