Jump to content

Mtext rotated to "viewtwist" lisp troubleshoot


RyanGC

Recommended Posts

Hi all,

 

First post, but straight to it!

 

When using the following code, the mtext editor is suppressed, and instead the text entry is moved to the command line (see screenshot). I've noticed this before in another lisp routine (not written by myself, nor is it available to me review). Can my routine be modified to prevent the mtext editor from being suppressed?

 

(defun c:MTR (/ mtang1 mtang2)
(setq oldcmd (getvar "cmdecho"))
(setvar "cmdecho" 0)
(setq mtang1 (getvar "viewtwist"))
(setq mtang2 (/ (* mtang1 180) pi))
(command "mtext" pause "r" mtang2 pause)
(setvar "cmdecho" oldcmd)
(princ)
)

mtexteditorsuppressed.jpg

 

Please forgive me if my coding disagrees with your own sensibilities, but this only my second day of writing AutoLISP! I'm a tidy person, and I'm sure that in time my nesting, and parenthesis placement, etc, will look pleasing to all you neat programmers.

Link to comment
Share on other sites

Hi Ryan,

 

Welcome to CADTutor :)

 

Give this a try:

 

(defun c:MTR ( / *error* oldcmd mtang )

 (defun *error* ( msg )
   (and oldcmd (setvar "CMDECHO" oldcmd))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )
   
 (setq oldcmd (getvar "cmdecho"))
 (setvar "cmdecho" 0)
 
 (setq mtang (/ (* (getvar "viewtwist") 180.) pi))
 (initdia)
 (command "mtext" pause "r" mtang pause)
 
 (setvar "cmdecho" oldcmd)
 (princ)
)

 

I have added an Error handler so that the CMDECHO is restored should the user hit Esc.

Link to comment
Share on other sites

(defun c:MTR (/ mtang1 mtang2)

(setq oldcmd (getvar "cmdecho"))

(setvar "cmdecho" 0)

(setq mtang1 (getvar "viewtwist"))

(setq mtang2 (/ (* mtang1 180) pi))

(setq mtang3 (rtos mtang2 2 2))

(command "mtext" pause pause mtang3 "" "")

(setvar "cmdecho" oldcmd)

(princ "Enjoy the codes RyanGC")

(princ)

)

 

Hi , I would be happy if this reached your needs. if not, I could remodify it for you.

 

Tharwat

Professional Autocad Draftsman

Link to comment
Share on other sites

(initdia) usage:

initdia

(initdia [dialogflag])

Currently, the following commands make use of the initdia function:

ATTDEF, ATTEXT, BHATCH, BLOCK, COLOR, IMAGE, IMAGEADJUST, INSERT, LAYER,

LINETYPE, MTEXT, PLOT, RENAME, STYLE, TOOLBAR, and VIEW.

Arguments

dialogflag An integer. If this argument is not present or is present

and nonzero, the next use (and next use only) of a

command will display that command's dialog box rather

than its command line prompts.

If dialogflag is zero, any previous call to this function is

cleared, restoring the default behavior of presenting the

command line interface.

Link to comment
Share on other sites

Hi Ryan,

 

Welcome to CADTutor :)

 

Give this a try:

 

(defun c:MTR ( / *error* oldcmd mtang )

 (defun *error* ( msg )
   (and oldcmd (setvar "CMDECHO" oldcmd))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )
   
 (setq oldcmd (getvar "cmdecho"))
 (setvar "cmdecho" 0)
 
 (setq mtang (/ (* (getvar "viewtwist") 180.) pi))
 (initdia)
 (command "mtext" pause "r" mtang pause)
 
 (setvar "cmdecho" oldcmd)
 (princ)
)

I have added an Error handler so that the CMDECHO is restored should the user hit Esc.

 

Hi Lee,

 

This worked perfectly. Thank you. My original attempt was quite a bit off the mark! Steep learning curve ahead for me, huh...

Link to comment
Share on other sites

Hi Lee,

 

This worked perfectly. Thank you. My original attempt was quite a bit off the mark! Steep learning curve ahead for me, huh...

You were closer than you think. Minus the error handler, the only thing you needed was (initdia).

Link to comment
Share on other sites

(defun c:MTR (/ mtang1 mtang2)

(setq oldcmd (getvar "cmdecho"))

(setvar "cmdecho" 0)

(setq mtang1 (getvar "viewtwist"))

(setq mtang2 (/ (* mtang1 180) pi))

(setq mtang3 (rtos mtang2 2 2))

(command "mtext" pause pause mtang3 "" "")

(setvar "cmdecho" oldcmd)

(princ "Enjoy the codes RyanGC")

(princ)

)

 

Hi , I would be happy if this reached your needs. if not, I could remodify it for you.

 

Tharwat

Professional Autocad Draftsman

 

Hi there Tharwat,

 

This didn't work, for reasons I haven't established just yet. As I want to leanr about what's going under the "lisp" hood, I'm going to have a close look at this and perhaps I can attempt to explain why this did not achieve the results I am looking for. In all cases Lee Mac's edit to my original worked perfectly.

 

Thanks, all the same.

R

Link to comment
Share on other sites

You were closer than you think. Minus the error handler, the only thing you needed was (initdia).

 

And you have kindly posted some details on (initdia) for me, thanks.

Link to comment
Share on other sites

You could use getpoint for the first pick, but sadly, getcorner won't match the current UCS, so the process can't be completely seamless.

 

(defun c:MTR (/ *error* oldcmd pt)

 (defun *error* (msg)
   (and oldcmd (setvar "CMDECHO" oldcmd))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **"))
   )
   (princ)
 )

 (setq oldcmd (getvar "cmdecho"))
 (setvar "cmdecho" 0)

 (initdia)
 (if (setq pt (getpoint "\nSpecify first corner: "))
   (command "_.mtext" "_non" pt "_r" (/ (* (getvar "viewtwist") 180.) pi) pause)
 )

 (setvar "cmdecho" oldcmd)
 (princ)
)

 

 

I also put the viewtwist conversion within the command expression (just to show it could be done).

Link to comment
Share on other sites

There are a couple of quirks I wanted to ask about...

 

(defun c:MTR ( / *error* oldcmd mtang )

 (defun *error* ( msg )
   (and oldcmd (setvar "CMDECHO" oldcmd))
   (or (wcmatch (strcase msg) "[color=Red]*BREAK[/color],*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )
   
 (setq oldcmd (getvar "cmdecho"))
 (setvar "cmdecho" 0)
 
 (setq mtang (/ (* (getvar "viewtwist") [color=Blue]180.[/color]) pi))
 (initdia)
 (command "mtext" pause "r" mtang pause)
 
 (setvar "cmdecho" oldcmd)
 (princ)
)

Why does BREAK have only one * surrounding it, as opposed to the two * surrounding CANCEL and EXIT?

 

Is there a reason for placing the decimal point after 180? Why not simply write it as 180, and only add a decimal if it be necessary?

 

Thanks again...

Link to comment
Share on other sites

With regards to the BREAK*, I'm using a Wildcard filter to catch 'Console Break',

 

Whereas, the others catch 'Function Cancelled' and 'Quit / Exit Abort'...

 

As for the decimal point, try these at the command line:

 

(/ 1  2 )
(/ 1. 2 )
(/ 1  2.)
(/ 1. 2.)

 

:wink:

Link to comment
Share on other sites

With regards to the BREAK*, I'm using a Wildcard filter to catch 'Console Break',

 

Whereas, the others catch 'Function Cancelled' and 'Quit / Exit Abort'...

 

Okay. I'll need to absorb this for awhile.

 

As for the decimal point, try these at the command line:

 

(/ 1  2 )
(/ 1. 2 )
(/ 1  2.)
(/ 1. 2.)

:wink:

 

Okay. All, apart from the first line (which resulted in 0), resulted in 0.5. Unfortunately, I'm not quite clever enough to see the significance of this right now...

 

I tested the mtext lisp from this thread with, and without, the decimal, and the results "appeared" to be identical. So is it simply a "good" programming habit to include the decimal, just in case one has a scenario similar to (/ 1 2 )?

Link to comment
Share on other sites

Exactly, its a really difficult bug to catch if you miss it..

 

When I want a real answer, I almost always deal in reals - else, if all arguments are integer, the answer may not be what is expected.

Link to comment
Share on other sites

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