Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Mirror number and character of string

8 REPLIES 8
Reply
Message 1 of 9
cuasuavdd
927 Views, 8 Replies

Mirror number and character of string

Dear All,

 

Can you help me create a lisp as:

 

Change: 1254TFD to TFD1254 in text mtxt, value of attribute.

Just using mouse drag a region and change all in that region selected.

unlimited about number of number and characters.

8 REPLIES 8
Message 2 of 9
stevor
in reply to: cuasuavdd

Try

 (setq rnls (vl-list->string (reverse

   (vl-string->list "1254TFD" ) )))

S
Message 3 of 9
Kent1Cooper
in reply to: cuasuavdd


@cuasuavdd wrote:

.... 

Change: 1254TFD to TFD1254 in text mtxt, value of attribute.

....

unlimited about number of number and characters.


For the switching-the-ends-of-the-string part, here's one way:

 

(setq str "1254TFD"); [your example -- get from entity or User input]
(setq
  ltrs "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  nums "0123456789"
  numfirst (wcmatch (substr str 1 1) "#"); starts with numbers?
  newstr

    (strcat
      (vl-string-left-trim (if numfirst nums ltrs) str); ending portion of original
      (vl-string-right-trim (if numfirst ltrs nums) str); starting portion
    ); strcat & newstr

); setq

 

With that input string, it returns "TFD1254".  It assumes that, as in your example, there are no lower-case letters or punctuation marks, and each "half" contains only one type or the other.  Other possibilities could be accounted for.

 

Then substitute that value into the text content of whatever it is -- there are various routines on the Forums that can do that part of it.

 

EDIT:  Here's a way to do it that allows lower-case letters without needing to spell out a 52-character string of all upper- and lower-case letters to be used in (vl-string-...-trim) functions:

 

(setq
  first (if (wcmatch (substr str 1 1) "#") "#" "@"); starts with numbers or letters?
  left "" ; initially empty strings
  right ""
); setq
(while (/= str ""); not yet reduced to nothing
  (setq
    char (substr str 1 1); first [remaining] character
    end (if (wcmatch char first) (read "right") (read "left")); put into which end?
  ); setq
  (set end (strcat (eval end) char)); add to appropriate portion
  (setq str (substr str 2))
); while
(setq newstr (strcat left right)); combine portions

Kent Cooper, AIA
Message 4 of 9
Kent1Cooper
in reply to: stevor


@stevor wrote:

Try

 (setq rnls (vl-list->string (reverse

   (vl-string->list "1254TFD" ) )))


[That returns "DFT4521", reversing the entire sequence rather than swapping the positions of the number and letter groups while retaining the order within each.]

Kent Cooper, AIA
Message 5 of 9
Lee_Mac
in reply to: Kent1Cooper

Here's another method:

 

(defun switchstring ( str / fun ls1 ls2 )
    (if (wcmatch str "#*")
        (setq fun vl-member-if-not)
        (setq fun vl-member-if)
    )
    (vl-list->string
        (append
            (fun '(lambda ( x ) (setq ls2 (cons x ls2)) (< 47 x 58)) (vl-string->list str))
            (reverse (cdr ls2))
        )
    )
)

 

_$ (switchstring "1254TFD")
"TFD1254"
_$ (switchstring (switchstring "1254TFD"))
"1254TFD"

 

Message 6 of 9
cuasuavdd
in reply to: Lee_Mac

Dear All,

 

Thank you very much.

 

Can you help me like that:

 

type a command like for ex, to activate a autolisp.... (defun c: switchstring ....)

 

And it affects tomtxt, txt, value of attribute.

 

it will change all values in form: tttttnnnnn to nnnntttt, or nnnntttt to tttttnnnnn ( from DSDFSDF1256 to 1256DSDFSDF, from 4545DSDFSDF to DSDFSDF4545)

 

t: text

n: number

 

And i can drag mouse to change all text and value of attribute in a region i drag mouse.

 

Thank you very much.

Message 7 of 9
bhull1985
in reply to: cuasuavdd

tomtext?

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 8 of 9
cuasuavdd
in reply to: bhull1985

MTEXT
Message 9 of 9
Lee_Mac
in reply to: cuasuavdd

cuasuavdd wrote:

Dear All,

 

Thank you very much.

 

Can you help me like that:

 

type a command like for ex, to activate a autolisp.... (defun c: switchstring ....)

 

And it affects to mtxt, txt, value of attribute.

 

it will change all values in form: tttttnnnnn to nnnntttt, or nnnntttt to tttttnnnnn ( from DSDFSDF1256 to 1256DSDFSDF, from 4545DSDFSDF to DSDFSDF4545)

 

t: text

n: number

 

And i can drag mouse to change all text and value of attribute in a region i drag mouse.

 

 

Try the following example:

 

(defun c:switchtext ( / ent enx idx sel str )
    (if
        (setq sel
            (ssget "_:L"
               '(
                    (-4 . "<OR")
                        (-4 . "<AND")
                            (0 . "TEXT,MTEXT")
                            (1 . "*#@*,*@#*")
                        (-4 . "AND>")
                        (-4 . "<AND")
                            (0 . "INSERT")
                            (66 . 1)
                        (-4 . "AND>")
                    (-4 . "OR>")
                )
            )
        )
        (repeat (setq idx (sslength sel))
            (setq ent (ssname sel (setq idx (1- idx)))
                  enx (entget ent)
            )
            (if (wcmatch (cdr (assoc 0 enx)) "*TEXT")
                (progn
                    (setq str (assoc 1 enx))
                    (entmod (subst (cons 1 (switchstring (cdr str))) str enx))
                )
                (progn
                    (setq ent (entnext ent)
                          enx (entget  ent)
                    )
                    (while (= "ATTRIB" (cdr (assoc 0 enx)))
                        (setq str (assoc 1 enx))
                        (entmod (subst (cons 1 (switchstring (cdr str))) str enx))
                        (setq ent (entnext ent)
                              enx (entget  ent)
                        )
                    )
                )
            )
        )
    )
    (princ)
)

(defun switchstring ( str / fun ls1 ls2 )
    (if (wcmatch str "*#@*,*@#*")
        (progn
            (if (wcmatch str "#*")
                (setq fun vl-member-if-not)
                (setq fun vl-member-if)
            )
            (vl-list->string
                (append
                    (fun '(lambda ( x ) (setq ls2 (cons x ls2)) (< 47 x 58)) (vl-string->list str))
                    (reverse (cdr ls2))
                )
            )
        )
        str
    )
)

(princ)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost