Background mask toggle for MText

Background mask toggle for MText

h_s_walker
Mentor Mentor
1,137 Views
11 Replies
Message 1 of 12

Background mask toggle for MText

h_s_walker
Mentor
Mentor

So I asked CHATGPT for a lisp which would toggle the background mask of mtext between off and on with a border of 1.2, and I got the code below

I tried it and I got the following error

Select MText to toggle background mask: ; error: bad DXF group: (71 . 1.2)

Can anyone fix this so it will work in AutoCAD LT2025?

(defun c:ToggleMTextMask (/ ent mtext bgMask bgMargin)
  ;; Prompt user to select a single MText object
  (setq ent (car (entsel "\nSelect MText to toggle background mask: ")))
  
  (if (and ent (= "MTEXT" (cdr (assoc 0 (entget ent)))))  ; Check if the entity is MText
    (progn
      (setq mtext (entget ent))  ; Get the entity data of the MText
      
      ;; Get current background mask status (bit 70)
      (setq bgMask (cdr (assoc 70 mtext)))  
      
      ;; Toggle the background mask (0 = off, 1 = on)
      (if (= bgMask 1)
        (progn
          ;; Turn the background mask off
          (entmod (subst (cons 70 0) (assoc 70 mtext) mtext))
          (princ "\nBackground mask turned off.")
        )
        (progn
          ;; Turn the background mask on and set margin to 1.2
          (entmod (subst (cons 70 1) (assoc 70 mtext) mtext))  ; Turn mask on
          (entmod (subst (cons 71 1.2) (assoc 71 mtext) mtext))  ; Set margin to 1.2
          (princ "\nBackground mask turned on with margin of 1.2.")
        )
      )
    )
    (princ "\nSelected entity is not an MText object.")
  )
  (princ)
)

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

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

ВeekeeCZ
Consultant
Consultant

Would you post a sample dwg with 2 mtexts, with and without mask?

0 Likes
Message 3 of 12

h_s_walker
Mentor
Mentor

Drawing attached

 

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 4 of 12

Moshe-A
Mentor
Mentor
Accepted solution

@h_s_walker hi,

 

ChatGPT almost hit the target 😀

 

unfortunately classic lisp is not enough to turn on/off mtext background fill you need activex

but to set the border offset factor (dxf code 45) you still need classic autolisp.

 

check this ...

 

enjoy

Moshe

 

 

(defun c:ToggleMTextMask (/ ent mtext bgMask bgMargin AcDbMText)
  ;; Prompt user to select a single MText object
  (setq ent (car (entsel "\nSelect MText to toggle background mask: ")))
  
  (if (and ent (= "MTEXT" (cdr (assoc 0 (entget ent)))))  ; Check if the entity is MText
    (progn
      (setq AcDbMText (vlax-ename->vla-object ent))

      ;; Get current background mask status (bit 70)
      (setq bgMask (vla-get-backgroundfill AcDbMText))
      
      ;; Toggle the background mask (0 = off, 1 = on)
      (if (eq bgMask :vlax-true)
        (progn
          ;; Turn the background mask off
	  (vla-put-backgroundfill AcDbMText :vlax-false)
          (princ "\nBackground mask turned off.")
        )
        (progn
          ;; Turn the background mask on and set margin to 1.2
	  (vla-put-backgroundfill AcDbMText :vlax-true)
          (setq mtext (entget ent))  ; Get the entity data of the MText
          (entmod (subst (cons 45 1.2) (assoc 45 mtext) mtext))  ; Set margin to 1.2
          (princ "\nBackground mask turned on with margin of 1.2.")
        )
      )
      (vlax-release-object AcDbMText)
    )
    (princ "\nSelected entity is not an MText object.")
  )
  (princ)
)

 

Message 5 of 12

marko_ribar
Advisor
Advisor

Not sure if LT has (vla-xxx) functions, but perhaps it has (set/get-propertyvalue) functions - it was introuduced back with R 2012 AFAIK...

So, try this revision...

 

 

 

(defun c:ToggleMTextMask ( / ent mtext bgMask )

  ;; Prompt user to select a single MText object
  (setq ent (car (entsel "\nSelect MText to toggle background mask: ")))
  
  (if (and ent (= "MTEXT" (cdr (assoc 0 (entget ent)))))  ; Check if the entity is MText
    (progn
      (setq mtext (entget ent))  ; Get the entity data of the MText
      
      ;; Get current background mask status
      (if (= (getpropertyvalue ent "BackgroundFill") 1) ;|(assoc 45 mtext)|;
        (setq bgMask 1)
        (setq bgMask 0)
      )
      
      ;; Toggle the background mask (0 = off, 1 = on)
      (if (= bgMask 1)
        (progn
          ;; Turn the background mask off
          ;|
          (setq mtext (vl-remove (assoc 90 mtext) mtext))
          (setq mtext (vl-remove (assoc 63 mtext) mtext))
          (setq mtext (vl-remove (assoc 45 mtext) mtext))
          (setq mtext (vl-remove (assoc 441 mtext) mtext))
          (entupd (cdr (assoc -1 (entmod mtext))))
          |;
          (setpropertyvalue ent "BackgroundFill" 0)
          (princ "\nBackground mask turned off.")
        )
        (progn
          ;; Turn the background mask on and set margin to 1.2
          ;|
          (setq mtext (append mtext (list (cons 90 1))))
          (setq mtext (append mtext (list (cons 63 1))))
          (setq mtext (append mtext (list (cons 45 1.2))))  ; Set margin to 1.2
          (setq mtext (append mtext (list (cons 441 0))))
          (entupd (cdr (assoc -1 (entmod mtext))))
          |;
          (setpropertyvalue ent "BackgroundScaleFactor" 1.2)  ; Set margin to 1.2
          (setpropertyvalue ent "BackgroundFillColor" 1)  ; Set color to 1 = red
          (setpropertyvalue ent "BackgroundFill" 1)
          (princ "\nBackground mask turned on with margin of 1.2.")
        )
      )
    )
    (princ "\nSelected entity is not an MText object.")
  )
  (princ)
)

 

 

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 6 of 12

h_s_walker
Mentor
Mentor

@Moshe-A once again you've hit the mark

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 7 of 12

ВeekeeCZ
Consultant
Consultant

It's similar to @marko_ribar apart from allowing to select multiple objects at the time.

 

(defun c:ToggleMtextMask ( / s i e)

  (princ "\nSelect mtext to toogle background, ")
  (while (setq s (ssget ":S" '((0 . "MTEXT"))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (if (= 0 (getpropertyvalue e "BackgroundFill"))
	(progn
	  (setpropertyvalue e "BackgroundFill" 1)
	  (setpropertyvalue e "BackgroundScaleFactor" 1.2)
	  (setpropertyvalue e "BackgroundTransparency" 0)
	  (setpropertyvalue e "UseBackgroundColor" 1)
	  (princ "\nBackground mask turned on."))
	(progn
	  (setpropertyvalue e "BackgroundFill" 0)
	  (princ "\nBackground mask turned off.")))))
  (princ)
  )

 

0 Likes
Message 8 of 12

komondormrex
Mentor
Mentor

mod to your code. mask color, if enabled, set to the background color.

 

(defun c:ToggleMTextMask (/ ent mtext bgMask)
  ;; Prompt user to select a single MText object
  (setq ent (car (entsel "\nSelect MText to toggle background mask: ")))
  
  (if (and ent (= "MTEXT" (cdr (assoc 0 (entget ent)))))  ; Check if the entity is MText
    (progn
      (setq mtext (entget ent))  ; Get the entity data of the MText
      
      ;; Get current background mask status (group 90)
      (setq bgMask (cdr (assoc 90 mtext)))  
      
      ;; Toggle the background mask (0 = off, 1 = on)
      (if (or (null bgMask)(= 2 bgMask))
        	(progn
        	  ;; Turn the background mask on and set margin to 1.2
        	  (setq mtext (append mtext '((90 . 3))))  ; Turn mask on
        	  (entmod (append mtext '((45 . 1.2))))  ; Set margin to 1.2
        	  (princ "\nBackground mask turned on with margin of 1.2.")
        	)
        	(progn
        	  ;; Turn the background mask off
			  (vlax-put (vlax-ename->vla-object ent) 'backgroundfill 0)
        	  (princ "\nBackground mask turned off.")
        	)
      )
    )
    (princ "\nSelected entity is not an MText object.")
  )
  (princ)
)

 

 

0 Likes
Message 9 of 12

ВeekeeCZ
Consultant
Consultant

If you wanted to see how promising your attempt at using GPTChat was...

 

GPT tested for the existence of a background based on the code 70... in fact it is 90 and 70 is undefined.
It set the offset margin with the code 71... in fact it is 45. 71 defines the text alignment. 

See HELP 

 

How much time did you spend on it? No change to succeed.

 

 

(defun c:ToggleMTextMask (/ ent mtext bgMask bgMargin)
  ;; Prompt user to select a single MText object
  (setq ent (car (entsel "\nSelect MText to toggle background mask: ")))
  
  (if (and ent (= "MTEXT" (cdr (assoc 0 (entget ent)))))  ; Check if the entity is MText
    (progn
      (setq mtext (entget ent))  ; Get the entity data of the MText
      
      ;; Get current background mask status (bit 70)
      (setq bgMask (cdr (assoc 70 mtext)))  
      
      ;; Toggle the background mask (0 = off, 1 = on)
      (if (= bgMask 1)
        (progn
          ;; Turn the background mask off
          (entmod (subst (cons 70 0) (assoc 70 mtext) mtext))
          (princ "\nBackground mask turned off.")
        )
        (progn
          ;; Turn the background mask on and set margin to 1.2
          (entmod (subst (cons 70 1) (assoc 70 mtext) mtext))  ; Turn mask on
          (entmod (subst (cons 71 1.2) (assoc 71 mtext) mtext))  ; Set margin to 1.2
          (princ "\nBackground mask turned on with margin of 1.2.")
        )
      )
    )
    (princ "\nSelected entity is not an MText object.")
  )
  (princ)
)

 

0 Likes
Message 10 of 12

h_s_walker
Mentor
Mentor

@ВeekeeCZ No time. I don't know lisp, so I asked CHATGPT to create it for me

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 11 of 12

Moshe-A
Mentor
Mentor

@ВeekeeCZ ,

 

Maybe ChatGPT creators did not have the time to update their knowledge database?! 🤣

Message 12 of 12

ВeekeeCZ
Consultant
Consultant

Then there is no value in trying to fix that mess. It's just more efficient to start from scratch. That's what I thought. 

0 Likes