Jump to content

Exit the command with the right mouse button in one click


Recommended Posts

Posted

Good day everyone!

After editing the text, its color changes, but to exit the command, 
you need to press RMB or enter or Esc twice.
Is it possible to complete the execution of the command with the right mouse button in one click?
 

;; If the current color is not equal to 136, then change it to 136.
;; If the current color is 136, then change it to 230.

(defun c:TEcolor136_230 (/ error oldcmdecho sel e type obj res currentColor)
  (vl-load-com)

  (defun error (msg)
    (if oldcmdecho (setvar "CMDECHO" oldcmdecho))
    (if (and msg (not (wcmatch (strcase msg) "BREAK,CANCEL,EXIT")))
      (princ (strcat "
Error: " msg))
    )
    (princ)
  )

  (setq oldcmdecho (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)

  (while (setq sel (entsel "
Select TEXT/MTEXT to edit (Enter — exit): "))
    (setq e (car sel)
          type (strcase (cdr (assoc 0 (entget e))))
    )
    (if (wcmatch type "TEXT,MTEXT")
      (progn
        (setq obj (vlax-ename->vla-object e))
        ;; get the current color
        (setq currentColor
          (vl-catch-all-apply 'vlax-get-property (list obj 'Color))
        )
        ;; Error handling on receipt
        (if (vl-catch-all-error-p currentColor)
          (setq currentColor 0)
        )
        ;; Opening the text editor
        (setq res
          (vl-catch-all-apply 'vl-cmdf (list "_.textedit" e))
        )
        (if (vl-catch-all-error-p res)
          (princ "
Editing canceled.")
          ;; After editing, we change the color
          (progn
            (if (= currentColor 136)
              ;; If it's 136, change it to 230.
              (if (vl-catch-all-error-p 
                    (vl-catch-all-apply 'vlax-put-property (list obj 'Color 230))
                    )
                (princ "
Couldn't change color to 230.")
                (princ "
The color has been changed to 230.")              )
            )
            (if (/= currentColor 136)
              ;; If it's not 136, change it to 136.
              (if (vl-catch-all-error-p 
                    (vl-catch-all-apply 'vlax-put-property (list obj 'Color 136))
                    )
                (princ "
Couldn't change the color to 136.")
                (princ "
The color has been changed to 136.")
              )
            )
            (princ "
Editing completed.")
          )
        )
      )
      (princ "
It's not an object TEXT/MTEXT.")
    )
  )
  (setvar "CMDECHO" oldcmdecho)
  (princ)
)

 

Posted (edited)

Do you only need to change the color or also edit the text?

Edited by GLAVCVS
Posted
2 hours ago, GLAVCVS said:

Do you only need to change the color or also edit the text?

I need to edit the text and change the color.

Posted (edited)

Why not check the color 1st change the color then edit it ?

 

Also you can shorten the get put property. Must be a vl object. 

 

(setq colorobj (vlax-get obj 'color))

(vlax-put obj 'color 230)

 Maybe this also

 

;; If it's 136, change it to 230.
(if (= currentColor 136)
  (vlax-put obj 'color 230)
  (vlax-put obj 'color 130)
)

 To select only *text you can use a ssget 

 

(while (setq ss (ssget '(( 0 . "*TEXT"))))
(setq e (ssname ss 0))
(setq obj (vlax-ename->vla-object e))

 Have a look at Lee-mac SSGET functions look at the E: option.

Edited by BIGAL
  • Thanks 1
Posted (edited)
23 hours ago, Nikon said:

Good day everyone!

After editing the text, its color changes, but to exit the command, 
you need to press RMB or enter or Esc twice.
Is it possible to complete the execution of the command with the right mouse button in one click?
 

;; If the current color is not equal to 136, then change it to 136.
;; If the current color is 136, then change it to 230.

(defun c:TEcolor136_230 (/ error oldcmdecho sel e type obj res currentColor)
  (vl-load-com)

  (defun error (msg)
    (if oldcmdecho (setvar "CMDECHO" oldcmdecho))
    (if (and msg (not (wcmatch (strcase msg) "BREAK,CANCEL,EXIT")))
      (princ (strcat "
Error: " msg))
    )
    (princ)
  )

  (setq oldcmdecho (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)

  (while (setq sel (entsel "
Select TEXT/MTEXT to edit (Enter — exit): "))
    (setq e (car sel)
          type (strcase (cdr (assoc 0 (entget e))))
    )
    (if (wcmatch type "TEXT,MTEXT")
      (progn
        (setq obj (vlax-ename->vla-object e))
        ;; get the current color
        (setq currentColor
          (vl-catch-all-apply 'vlax-get-property (list obj 'Color))
        )
        ;; Error handling on receipt
        (if (vl-catch-all-error-p currentColor)
          (setq currentColor 0)
        )
        ;; Opening the text editor
        (setq res
          (vl-catch-all-apply 'vl-cmdf (list "_.textedit" e))
        )
        (if (vl-catch-all-error-p res)
          (princ "
Editing canceled.")
          ;; After editing, we change the color
          (progn
            (if (= currentColor 136)
              ;; If it's 136, change it to 230.
              (if (vl-catch-all-error-p 
                    (vl-catch-all-apply 'vlax-put-property (list obj 'Color 230))
                    )
                (princ "
Couldn't change color to 230.")
                (princ "
The color has been changed to 230.")              )
            )
            (if (/= currentColor 136)
              ;; If it's not 136, change it to 136.
              (if (vl-catch-all-error-p 
                    (vl-catch-all-apply 'vlax-put-property (list obj 'Color 136))
                    )
                (princ "
Couldn't change the color to 136.")
                (princ "
The color has been changed to 136.")
              )
            )
            (princ "
Editing completed.")
          )
        )
      )
      (princ "
It's not an object TEXT/MTEXT.")
    )
  )
  (setvar "CMDECHO" oldcmdecho)
  (princ)
)

 

 

I  think you need one ENTER to exit from 'textedit' and another one to exit from the while loop.

I think It’s not possible to solve everything with a right click.

The only option that comes close to what you’re asking for is to remove the while loop, so that the command ends after editing each text.

But this will force you to press right click to repeat the command.

 

The only option that would allow what you’re describing is to edit the text content with getstring instead of textedit.

 

But I don’t know if you’re willing to do that.

Edited by GLAVCVS
  • Thanks 1
Posted
20 minutes ago, GLAVCVS said:

I  think you need one ENTER to exit from 'textedit' and another one to exit from the while loop.

I think It’s not possible to solve everything with a right click.

The only option that comes close to what you’re asking for is to remove the while loop, so that the command ends after editing each text.

But this will force you to press right click to repeat the command.

 

The only option that would allow what you’re describing is to edit the text content with getstring instead of textedit.

That's fine with me...

Posted

There's another option: to respect the call to 'textedit' and exit it by clicking the mouse over the empty background.

This is the option applied in the attached code.

 

(defun c:TEcolor136_230
       (/ error oldcmdecho sel e typo obj res currentColor para)
  (vl-load-com)

  (defun error (msg)
    (if	oldcmdecho
      (setvar "CMDECHO" oldcmdecho)
    )
    (if	(and msg (not (wcmatch (strcase msg) "BREAK,CANCEL,EXIT")))
      (princ (strcat "Error: " msg))
    )
    (princ)
  )

  (setq oldcmdecho (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)

;;;  (while (setq sel (entsel "\nSelect TEXT/MTEXT to edit (Enter — exit): "))
    (setq sel (entsel "\nSelect TEXT/MTEXT to edit (Enter — exit): ")
	  e    (car sel)
	  typo (strcase (cdr (assoc 0 (entget e))))
    )
    (if	(wcmatch typo "TEXT,MTEXT")
      (progn
	(setq obj (vlax-ename->vla-object e))
	;; get the current color
	(setq currentColor
	       (vl-catch-all-apply 'vlax-get-property (list obj 'Color))
	)
	;; Error handling on receipt
	(if (vl-catch-all-error-p currentColor)
	  (setq currentColor 0)
	)
	;; Opening the text editor (getstring)
	(setq res
	       (vl-catch-all-apply 'vl-cmdf (list "_.textedit" e ""))
	)
;;;	(if (/= (setq res (getstring (strcat "\nEdit text <" (cdr (assoc 1 (entget e))) ">: "))) "")
;;;	  (entmod (subst (cons 1 res) (assoc 1 (entget e)) (entget e)))
;;;	)
	(if (vl-catch-all-error-p res)
	  (princ "Editing canceled.")
	  ;; After editing, we change the color
	  (progn
	    (if	(= currentColor 136)
	      ;; If it's 136, change it to 230.
	      (if (vl-catch-all-error-p
		    (vl-catch-all-apply
		      'vlax-put-property
		      (list obj 'Color 230)
		    )
		  )
		(princ "Couldn't change color to 230.")
		(princ "The color has been changed to 230.")
	      )
	    )
	    (if	(/= currentColor 136)
	      ;; If it's not 136, change it to 136.
	      (if (vl-catch-all-error-p
		    (vl-catch-all-apply
		      'vlax-put-property
		      (list obj 'Color 136)
		    )
		  )
		(princ "Couldn't change the color to 136.")
		(princ "The color has been changed to 136.")
	      )
	    )
	    (princ "Editing completed.")
	  )
	)
      )
      (princ "It's not an object TEXT/MTEXT.")
    )
;;;  )
  (setvar "CMDECHO" oldcmdecho)
  (princ)
)

 

  • Like 1
Posted

I hope the 'textedit' options in your version of AutoCAD aren't a problem.

  • Agree 1
Posted

If you don't want the command to end after editing the text, you can keep the "while" loop, as shown in this other code. The operation will be the same except that to exit the command, you'll need an additional "right click."

(defun c:TEcolor136_230
       (/ error oldcmdecho sel e typo obj res currentColor para)
  (vl-load-com)

  (defun error (msg)
    (if	oldcmdecho
      (setvar "CMDECHO" oldcmdecho)
    )
    (if	(and msg (not (wcmatch (strcase msg) "BREAK,CANCEL,EXIT")))
      (princ (strcat "Error: " msg))
    )
    (princ)
  )

  (setq oldcmdecho (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)

  (while (setq sel (entsel "\nSelect TEXT/MTEXT to edit (Enter — exit): "))
    (setq ;sel (entsel "\nSelect TEXT/MTEXT to edit (Enter — exit): ")
	  e    (car sel)
	  typo (strcase (cdr (assoc 0 (entget e))))
    )
    (if	(wcmatch typo "TEXT,MTEXT")
      (progn
	(setq obj (vlax-ename->vla-object e))
	;; get the current color
	(setq currentColor
	       (vl-catch-all-apply 'vlax-get-property (list obj 'Color))
	)
	;; Error handling on receipt
	(if (vl-catch-all-error-p currentColor)
	  (setq currentColor 0)
	)
	;; Opening the text editor (getstring)
	(setq res
	       (vl-catch-all-apply 'vl-cmdf (list "_.textedit" e ""))
	)
;;;	(if (/= (setq res (getstring (strcat "\nEdit text <" (cdr (assoc 1 (entget e))) ">: "))) "")
;;;	  (entmod (subst (cons 1 res) (assoc 1 (entget e)) (entget e)))
;;;	)
	(if (vl-catch-all-error-p res)
	  (princ "Editing canceled.")
	  ;; After editing, we change the color
	  (progn
	    (if	(= currentColor 136)
	      ;; If it's 136, change it to 230.
	      (if (vl-catch-all-error-p
		    (vl-catch-all-apply
		      'vlax-put-property
		      (list obj 'Color 230)
		    )
		  )
		(princ "Couldn't change color to 230.")
		(princ "The color has been changed to 230.")
	      )
	    )
	    (if	(/= currentColor 136)
	      ;; If it's not 136, change it to 136.
	      (if (vl-catch-all-error-p
		    (vl-catch-all-apply
		      'vlax-put-property
		      (list obj 'Color 136)
		    )
		  )
		(princ "Couldn't change the color to 136.")
		(princ "The color has been changed to 136.")
	      )
	    )
	    (princ "Editing completed.")
	  )
	)
      )
      (princ "It's not an object TEXT/MTEXT.")
    )
  )
  (setvar "CMDECHO" oldcmdecho)
  (princ)
)

 

  • Like 1
Posted
27 minutes ago, GLAVCVS said:

here's another option: to respect the call to 'textedit' and exit it by clicking the mouse over the empty background.

This is the option applied in the attached code.

 

@GLAVCVS thank you very much for the 2 options, I like both options, but the first option suits me better.

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...