AUTOLISP TO REMOVE SUFFIX VALUE

AUTOLISP TO REMOVE SUFFIX VALUE

karthikeyanskm
Contributor Contributor
578 Views
5 Replies
Message 1 of 6

AUTOLISP TO REMOVE SUFFIX VALUE

karthikeyanskm
Contributor
Contributor

THE BELOW IS NOT WORKING FOR ME.

 

(defun c:RSFX (/ suffix entities)
(setq suffix (getstring "\nEnter the suffix to remove: "))
(setq entities (ssget '((0 . "TEXT,MTEXT,ATTRIB"))))
(if entities
(progn
(foreach entity (vl-remove-if-not '(lambda (x) (member (cdr (assoc 0 (entget x))) '("TEXT" "MTEXT" "ATTRIB"))) (mapcar 'cadr (ssnamex entities)))
(cond
((= (cdr (assoc 0 (entget entity))) "TEXT")
(entmod (subst (cons 1 (strcase (substr (cdr (assoc 1 (entget entity))) 1 (- (strlen (cdr (assoc 1 (entget entity)))) (strlen suffix)))) (entget entity)) (assoc 1 (entget entity)) (entget entity)))
((= (cdr (assoc 0 (entget entity))) "MTEXT")
(entmod (subst (cons 1 (strcat (substr (cdr (assoc 1 (entget entity))) 1 (- (strlen (cdr (assoc 1 (entget entity)))) (strlen suffix))) (substr (cdr (assoc 1 (entget entity))) (- (strlen (cdr (assoc 1 (entget entity)))) (strlen suffix))))) (assoc 1 (entget entity)) (entget entity)))
((= (cdr (assoc 0 (entget entity))) "ATTRIB")
(entmod (subst (cons 1 (strcat (substr (cdr (assoc 1 (entget entity))) 1 (- (strlen (cdr (assoc 1 (entget entity)))) (strlen suffix))) (substr (cdr (assoc 1 (entget entity))) (- (strlen (cdr (assoc 1 (entget entity)))) (strlen suffix))))) (assoc 1 (entget entity)) (entget entity)))))
(princ "\nSuffix removed successfully.")
)
(princ "\nNo entities selected."))
(princ)
)

 

I want to remove the entered suffix value in a text, mtext and value of an attribute.

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

EnM4st3r
Advocate
Advocate

your function is missing closing parenthesis for your defun, if-function, progn-function

0 Likes
Message 3 of 6

EnM4st3r
Advocate
Advocate
Accepted solution

that code is a big mess altogether...
this here would remove the suffix (not for attributes)

(defun c:RSFX (/ suffix ss i obj txtstr)
  (setq suffix (getstring "\nEnter the suffix to remove: "))
  (setq ss (ssget '((0 . "TEXT,MTEXT"))))
  (setq i 0)
  (while (< i (sslength ss))
    (setq obj (vlax-ename->vla-object (ssname ss i))
          txtstr (vla-get-textstring obj)
    )
    (if (wcmatch txtstr (strcat "*" suffix))
       (vla-put-textstring obj (vl-string-subst "" suffix txtstr (- (strlen txtstr) (strlen suffix))))
    )
    (setq i (1+ i))
  )
)
0 Likes
Message 4 of 6

Kent1Cooper
Consultant
Consultant

[Is this another ChatGPT thing?]  ATTRIB is not an entity type that the (ssget) function can filter by.  It's the entity type of an Attribute in a Block insertion if you pick it by way of (nentsel) [which must be individually], but nested objects are not available to (ssget).

 

Also, that looks like it would take the number of characters in the suffix from the end of all such objects, regardless of whether they end with that suffix.  Surely that is not the intent -- there must be a check on whether the suffix is there to remove.

Kent Cooper, AIA
0 Likes
Message 5 of 6

Kent1Cooper
Consultant
Consultant

It's not necessary to accept all Text/Mtext selections, and then step through and for each one, check whether it ends with the suffix in question.  You can limit selection to those that do, and then just process everything in the selection set, without the need to check on its content:

(setq ss (ssget (list '(0 . "TEXT,MTEXT") (cons 1 (strcat "*" suffix)))))

But that would be case-sensitive, so if case differences are possible, it's not an option -- you'd still need to check each one with (strcase) applied to both the content and the suffix for comparison.

And also not for Attributes.

Kent Cooper, AIA
Message 6 of 6

EnM4st3r
Advocate
Advocate
ah, yes you're right
0 Likes