Text Swap using a slash (/)

Text Swap using a slash (/)

Anonymous
Not applicable
1,428 Views
10 Replies
Message 1 of 11

Text Swap using a slash (/)

Anonymous
Not applicable

I'm looking for a text swap lsp or command that uses a slash (/) to separate the sides. For example:

I have this wire marker:

TB-PCS-3-1/SCSSV-HPA-JOA-C1

and I want to copy it with the text "flip flopped" to look like this:

SCSSV-HPA-JOA-C1/TB-PCS-3-1

We have a wiremarker program that does this within the program but I can't find anything for Autocad. I have several columns of this and I have to change the numbering scheme. Rather than do it multiple times, I'd like to do it once, copy the whole column, then "flip flop" the numbers.

Any ideas?

Thank you in advance!
Veronica

0 Likes
1,429 Views
10 Replies
Replies (10)
Message 2 of 11

roland.r71
Collaborator
Collaborator

@Anonymous wrote:

I'm looking for a text swap lsp or command that uses a slash (/) to separate the sides. For example:

I have this wire marker:

TB-PCS-3-1/SCSSV-HPA-JOA-C1

and I want to copy it with the text "flip flopped" to look like this:

SCSSV-HPA-JOA-C1/TB-PCS-3-1

We have a wiremarker program that does this within the program but I can't find anything for Autocad. I have several columns of this and I have to change the numbering scheme. Rather than do it multiple times, I'd like to do it once, copy the whole column, then "flip flop" the numbers.

Any ideas?

Thank you in advance!
Veronica


The "flip flopping" is easy. Locate the / within the string, substring until that position = first part. Substring from / is last part. strcat them together in reverse order.

 

But what kind of entity are we speaking about? Text, Mtext, Attribute???

& what's with the "i want to copy it ..." do you mean the original should stay? (and a copy is reversed?)

 

edit:

This will do the swaping:

Command: (defun c:flipflop ( fliptxt / pos flip flop)
(_>    (setq pos (vl-string-position (ascii "/") fliptxt)
((_>          flip (substr fliptxt 1 pos)
((_>          flop (substr fliptxt (+ 2 pos))
((_>          fliptxt (strcat flop "/" flip)
((_>    )
(_>    fliptxt
(_> )
C:FLIPFLOP

Command: (setq fliptxt (c:flipflop "TB-PCS-3-1/SCSSV-HPA-JOA-C1"))
"SCSSV-HPA-JOA-C1/TB-PCS-3-1"
0 Likes
Message 3 of 11

cadffm
Consultant
Consultant

@Anonymous

 

Without issue/error handling and not nice, but my interpretation of your goal.

 

(defun c:CopySwapCol (/ ss1 ss2 bp string)
    (and
      (setq ss1 (ssget '((1 . "*/*")(8 . "*")))) ; <- Use [F1] for "WCMATCH" about 1.Textvalue or 8.Layername pattern creation.
      (setq ss2 (ssadd))
      (setq bp (getpoint "\nCopy basepoint: "))
      (foreach obj (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss1)))
      (setq string (cdr(assoc 1 (entget obj))))
      (ssadd (entmakex (subst
                          (cons 1 (strcat (substr string (+ 2 (vl-string-search "/" string)))
                                              "/"
                                              (substr string 1 (vl-string-search "/" string))
                              )       )
                      (cons 1 string)
                          (entget obj)
                           )
                 )
            ss2
      )
      )
      (command "_.MOVE" ss2 "" "_non" bp)
    )
 (princ)
)

 

@roland.r71

defun c: in its most beautiful form with an argument, today creative or is there more behind it?

Sebastian

0 Likes
Message 4 of 11

roland.r71
Collaborator
Collaborator

Since it is unclear where the text is coming from, i simply limited my code to do the text swap. Regardless of it's origin.

 

This can easily be expanded by code that gets it from a selected (m)text, or block attribute.

 

edit:

...So in the end it would become a local function, without the c:

0 Likes
Message 5 of 11

DannyNL
Advisor
Advisor

And this is my solution.

 

A different approach by utilizing regular expressions to find the / and group the text before and after the found /.

Uses my library function RegEx which is more divers and extended than what's only necessarily for this text swap.

 

(defun c:Test (/ T_Selection T_Text T_TextSwap)
   (if
      (setq T_Selection (ssget '((0 . "TEXT"))))
      (progn         
         (foreach T_Text (vl-remove-if '(lambda (T_Item) (listp (cadr T_item))) (ssnamex T_Selection))
            (if
               (setq T_TextSwap (RegEx "\(.*\)/(.*\)" (vla-get-TextString (setq T_Text (vlax-ename->vla-object (cadr T_Text)))) :vlax-false :vlax-false :vlax-false :vlax-true "$2/$1"))
               (vla-put-TextString T_Text T_TextSwap)
            )
         )
      )
   )
   (princ)
)

(defun RegEx (RE_Pattern RE_SearchString RE_GlobalMode RE_IgnoreCaseMode RE_SubMatchMode RE_SearchReplaceMode RE_ReplaceString / RE_RegExObject RE_Count RE_Result RE_Return)
   (if
      (and
         (= (type RE_SearchString)      'STR)
         (= (type RE_Pattern)           'STR)
         (/= RE_Pattern                   "")
         (/= RE_SearchString              "")
         (= (type RE_GlobalMode)        'SYM)
         (= (type RE_IgnoreCaseMode)    'SYM)
         (= (type RE_SubMatchMode)      'SYM)
         (= (type RE_SearchReplaceMode) 'SYM)
         (or
            (= RE_SearchReplaceMode :vlax-false)
            (and
               (= RE_SearchReplaceMode :vlax-true)
               (= (type RE_ReplaceString) 'STR)
            )
         )
      )
      (progn
         (setq RE_RegExObject (vlax-get-or-create-object "VBScript.RegExp"))
         (vlax-put-property RE_RegExObject 'Pattern     RE_Pattern)
         (vlax-put-property RE_RegExObject 'Global      RE_GlobalMode)
         (vlax-put-property RE_RegExObject 'IgnoreCase  RE_IgnoreCaseMode)
         (cond
            (
               (= RE_SearchReplaceMode :vlax-false)
               (setq RE_Result (vl-catch-all-apply 'vlax-invoke-method (list RE_RegExObject 'Execute RE_SearchString)))
               (if
                  (and
                     (not (vl-catch-all-error-p RE_Result))
                     (> (vla-get-Count RE_Result) 0)
                     (setq RE_Count -1)
                  )
                  (if
                     (and
                        (= RE_SubMatchMode :vlax-true)
                        (> (vla-get-Count (setq RE_SubMatches (vlax-get-property (vlax-get-property RE_Result 'Item 0) 'SubMatches))) 0)
                     )
                     (progn
                        (repeat (vla-get-Count RE_SubMatches)
                           (setq RE_Return (append RE_Return (list (vlax-variant-value (vlax-get-property RE_SubMatches 'Item (setq RE_Count (1+ RE_Count)))))))
                        )
                     )
                     (progn
                        (repeat (vla-get-Count RE_Result)
                           (setq RE_Return (append RE_Return (list (vla-get-Value (vlax-get-property RE_Result 'Item (setq RE_Count (1+ RE_Count)))))))
                        )
                     )
                  )
               )
            )
            (
               RE_SearchReplaceMode
               (setq RE_Result (vl-catch-all-apply 'vlax-invoke-method (list RE_RegExObject 'Replace RE_SearchString RE_ReplaceString)))
               (if
                  (/= RE_Result RE_SearchString)
                  (setq RE_Return RE_Result)
               )
            )
            (
               T
               nil
            )
         )         
         (vlax-release-object RE_RegExObject)
      )
   )
   RE_Return
)
Message 6 of 11

roland.r71
Collaborator
Collaborator

As i just gave you one, i do like it, but ...

This code gives new meaning to the word: Overkill Smiley Tongue

0 Likes
Message 7 of 11

DannyNL
Advisor
Advisor

Depends on how you look at it I guess.

 

The RegEx code is something which I wrote a long time ago and is part of my personal and companies library with (sub)functions. So I didn't write it for this purpose and I'm just re-using some of my old but already existing code.

And this RegEx function is way more powerful than then writing specific LISP code to just do one task only and can be used for possible future text handling as well.

0 Likes
Message 8 of 11

roland.r71
Collaborator
Collaborator

@DannyNL wrote:

Depends on how you look at it I guess.

 

The RegEx code is something which I wrote a long time ago and is part of my personal and companies library with (sub)functions. So I didn't write it for this purpose and I'm just re-using some of my old but already existing code.

And this RegEx function is way more powerful than then writing specific LISP code to just do one task only and can be used for possible future text handling as well.



Smiley Happy No need to explain. It was clear from the get go Smiley Wink

0 Likes
Message 9 of 11

Anonymous
Not applicable

It's just regular Text, no MText or Attribute

0 Likes
Message 10 of 11

roland.r71
Collaborator
Collaborator

@Anonymous wrote:

It's just regular Text, no MText or Attribute


In that case, this will do the trick: (note: it doesn't create a copy, but i could add that too)

(defun c:fliptext ( / ss txtent)

   (defun flipflop ( fliptxt / pos flip flop)
      (setq pos (vl-string-position (ascii "/") fliptxt)
            flip (substr fliptxt 1 pos)
            flop (substr fliptxt (+ 2 pos))
            fliptxt (strcat flop "/" flip)
      )
      fliptxt
   )
   
   (while (= ss nil)
      (setq ss (ssget ":S" '((0 . "TEXT"))))
   )
   (setq txtent (entget (ssname ss 0)))
   (setq txtent (subst (cons 1 (flipflop (cdr (assoc 1 txtent))))(assoc 1 txtent) txtent))
   (entmod txtent)
)
0 Likes
Message 11 of 11

Anonymous
Not applicable

I'm not worried about creating a copy. I'll have to copy the column of wiremarkers to the other end of the wire so that's easy. 

Thanks! I'll try it.

0 Likes