Jump to content

Recommended Posts

Posted (edited)

I found a lisp to adjust mtext color to bycolor but it shows the color code after adjusting. Please help me to adjust it and only remove the code before the text! Thank you.

I want it like this

mtext : room (color cycan) to room (bylayer) 

lisp : room(color cycan) to 4:room (bylayer(

(defun c:MtextToByLayer (/ ss ent elist newtext)
  (princ "\nSelect Mtext objects to set to ByLayer.")
  (if (setq ss (ssget '((0 . "MTEXT")))) ; Select only MTEXT entities
    (progn
      (vlax-for ent (vla-get-ActiveSelectionSet (vla-get-ActiveDocument (vlax-get-acad-object)))
        (if (= (vla-get-ObjectName ent) "AcDbMText")
          (progn
            ; Set the entity color to ByLayer (color code 256)
            (vla-put-color ent 256)
            
            ; Get the Mtext content
            (setq elist (entget (vlax-vla-object->ename ent)))
            (setq newtext (cdr (assoc 1 elist)))
            
            ; Strip all internal formatting codes, including color
            (setq newtext (vl-string-subst "" "{\\C" newtext)) ; Removes color codes
            (setq newtext (vl-string-subst "" "\\c" newtext)) ; Removes inline color codes
            (while (vl-string-search "{" newtext)
              (setq newtext (vl-string-subst "" "{" newtext))
              (setq newtext (vl-string-subst "" "}" newtext))
            )
            
            ; Update the Mtext entity with the stripped text
            (vla-put-TextString ent newtext)
          )
        )
      )
      (princ (strcat "\n" (itoa (sslength ss)) " Mtext objects updated."))
    )
    (princ "\nNo Mtext objects selected.")
  )
  (princ)
)

 

Edited by SLW210
Added Code Tags!!
Posted

Please use Code Tags for your code in the future. (<> in the editor toolbar)

Posted (edited)
2 hours ago, CAD2005 said:

Please help me to adjust it and only remove the code before the text!

Try this code, calling with command: MtextToByLayerNoNum

(defun _strip-mtext-color (s / i j)
  ;; Deletes all occurrences \C<number>; and \c<number>;
  (setq i 0)
  (while (setq i (vl-string-search "\\C" s i))
    (if (setq j (vl-string-search ";" s i))
      (setq s (strcat (substr s 1 i) (substr s (+ j 2)))  i i)
      (setq s (substr s 1 i)) 
    )
  )
  (setq i 0)
  (while (setq i (vl-string-search "\\c" s i))
    (if (setq j (vl-string-search ";" s i))
      (setq s (strcat (substr s 1 i) (substr s (+ j 2)))  i i)
      (setq s (substr s 1 i))
    )
  )
  s
)

(defun c:MtextToByLayerNoNum (/ ss ent elist newtext)
  (princ "\nSelect Mtext objects to set to ByLayer.")
  (if (setq ss (ssget '((0 . "MTEXT")))) ; Select only MTEXT entities
    (progn
      (vlax-for ent (vla-get-ActiveSelectionSet (vla-get-ActiveDocument (vlax-get-acad-object)))
        (if (= (vla-get-ObjectName ent) "AcDbMText")
          (progn
            ; Set the entity color to ByLayer (color code 256)
            (vla-put-color ent 256)
            
            ; Get the Mtext content
            (setq elist (entget (vlax-vla-object->ename ent)))
            (setq newtext (cdr (assoc 1 elist)))
            
            ; Strip all internal formatting codes, including color
            (setq newtext (_strip-mtext-color newtext))    
            (while (vl-string-search "{" newtext)
              (setq newtext (vl-string-subst "" "{" newtext))
              (setq newtext (vl-string-subst "" "}" newtext))
            )
            
            ; Update the Mtext entity with the stripped text
            (vla-put-TextString ent newtext)
          )
        )
      )
      (princ (strcat "\n" (itoa (sslength ss)) " Mtext objects updated."))
    )
    (princ "\nNo Mtext objects selected.")
  )
  (princ)
)

 

Edited by Nikon

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...