TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Andrea on October 06, 2006, 12:31:26 PM

Title: unable the escape key..
Post by: Andrea on October 06, 2006, 12:31:26 PM
hi all....

I work to make a little function detection...

Code: [Select]
(defun INGcommand( / g)
(while (setq g (strcase (getstring "INGcommand: ")))
  (progn
(if (vl-string-search "ING" g)(alert "You have entered a ING command"))
(if (eq g "COMMAND")(exit))
(if (and
      (not (vl-string-search "ING" g))
      (not (eq g "COMMAND"))
    )
      (alert "Invalid ING command.\n Type \"Command\" to return to AutoCAd command.")
)
)
  (INGcommand)
  )
)
(INGcommand)

It work great....since press the ESCAPE key.

any suggestion ?
Title: Re: unable the escape key..
Post by: kpblc on October 09, 2006, 02:30:20 AM
You can try to use (vl-catch-all-apply) function or check the errno system variable value.
Title: Re: unable the escape key..
Post by: Kerry on October 09, 2006, 05:48:38 AM
Andrea,
you could modify or use something like this  ...

Code: [Select]
;;;--------------------------------------------------------------------------
;;;--------------------------------------------------------------------------
;| #lib.
kb:getString (<Promptmsg><Default><InitBit><AllowSpaces>)

Revised Library : kwb 20051031
20051101 kwb : ESC test added.
Build 2.0 :

(SETQ tmpVal (kb:getString "Lot Description" nil (+ 1 ) T))
(SETQ tmpVal (kb:getString "Name" "Me" (+ 1 ) Nil))
(SETQ tmpVal (kb:getString nil nil nil Nil))
|;

(DEFUN kb:getString (Promptmsg                    ; The prompt string.
                     Default                      ; Value to return if response is <enter>
                     InitBit                      ; Initget bit
                     AllowSpaces                  ; spaces Flag < T or nil )
                                                  ;
                     / returnvalue)
   ;;------------------------------
   (OR InitBit (SETQ InitBit 0))
   ;;------------------------------
   (SETQ Promptmsg (STRCAT "\n"
                           (COND (Promptmsg)
                                 ("Specify String Value")
                           )
                   )
   )
   ;;------------------------------
   (IF (AND Default (= (TYPE Default) 'str) (/= Default ""))
      (PROGN
         (SETQ Promptmsg (STRCAT "\n" Promptmsg " << " Default " >>: "))
         (IF (VL-CATCH-ALL-ERROR-P
                (SETQ returnvalue (VL-CATCH-ALL-APPLY
                                     'GETSTRING
                                     (LIST AllowSpaces Promptmsg)
                                  )
                )
             )
            ;; ESC was pressed.
            (SETQ ReturnValue nil
                  Default nil
            )
         )
         (SETQ returnvalue (IF (= returnvalue "")
                              Default
                              returnvalue
                           )
         )
      )
      ;; Else no default, so don't accept ENTER or SPACEBAR
      ;;
      (PROGN (SETQ Promptmsg (STRCAT "\n" Promptmsg ": "))
             (IF (= InitBit 1)
                (WHILE (= ""
                          (SETQ returnvalue (VL-CATCH-ALL-APPLY
                                               'GETSTRING
                                               (LIST AllowSpaces Promptmsg)
                                            )
                          )
                       )
                )
                ;;
                (SETQ returnvalue
                        (VL-CATCH-ALL-APPLY 'GETSTRING
                                            (LIST AllowSpaces Promptmsg)
                        )
                )
             )
             (IF (VL-CATCH-ALL-ERROR-P returnvalue)
                ;; ESC was pressed.
                (SETQ ReturnValue nil)
             )
      )
   )
   ;;------------------------------
   returnvalue
)

;;;--------------------------------------------------------------------------
;;;--------------------------------------------------------------------------
Title: Re: unable the escape key..
Post by: MP on October 09, 2006, 10:26:13 AM
<warning, I just woke up>

Abused from this (http://www.theswamp.org/index.php?topic=7259.msg89772#msg89772) post --

Code: [Select]
(defun Evaluate ( expression / result )
    (vl-catch-all-apply
       '(lambda ( )
            (setq result
                (eval expression)
            )   
        )
    )
    result
)

Possible useage --

Code: [Select]
(defun INGcommand ( / g done )

    (while
   
        (and
            (not done)
            (setq g (Evaluate '(strcase (getstring "INGCommand: "))))
        )   
   
        (cond
            (   (eq g "COMMAND")
                (setq done t)
            )
            (   (vl-string-search "ING" g)
                (alert "You have entered a ING command")
            )
            (   t
                (alert
                    (strcat
                        "Invalid ING command.\n"
                        "Type \"Command\" to return"
                        "to AutoCAD command."
                    )   
                )
            )
        )   
           
    )
   
    (princ)
   
)
Title: Re: unable the escape key..
Post by: Patrick_35 on October 09, 2006, 10:58:04 AM
Hi

Code: [Select]
(defun INGcommand(/ g)
  (while (setq g (vl-catch-all-apply 'getstring (list "\nINGcommand: ")))
    (if (not (vl-catch-all-error-p g))
      (progn
        (if (vl-string-search "ING" g)(alert "You have entered a ING command"))
        (if (eq g "COMMAND")(exit))
        (if (and
          (not (vl-string-search "ING" g))
          (not (eq g "COMMAND")))
          (alert "Invalid ING command.\n Type \"Command\" to return to AutoCAd command.")
        )
      )
      (if (member (vl-catch-all-error-message a) (list "Fonction annulée" "Function cancelled"))
        (setq g T)
      )
    )
  )
)

(INGcommand)

@+
Title: Re: unable the escape key..
Post by: Kerry on October 09, 2006, 04:56:34 PM
<warning, I just woke up>

Abused from this (http://www.theswamp.org/index.php?topic=7259.msg89772#msg89772) post --


DejaVu
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 09:06:04 AM
Thanks guys...

but...

MP...your code have same result....when user press escape it return to the AutoCAd command line.

Patrick35....see the picture below when pressing escape..
Title: Re: unable the escape key..
Post by: MP on October 10, 2006, 09:27:54 AM
...when user press escape it return to the AutoCAd command line.

Without specification what are we to imagine you want aside from standard behavior?
Title: Re: unable the escape key..
Post by: Patrick_35 on October 10, 2006, 10:20:04 AM
oh !
I'm stupid  :-o
Replace
Code: [Select]
(if (member (vl-catch-all-error-message a) (list "Fonction annulée" "Function cancelled"))by
Code: [Select]
(if (member (vl-catch-all-error-message g) (list "Fonction annulée" "Function cancelled"))
@+
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 10:28:36 AM
Sorry MP....if wasn't clear.. :|
all i need to do its to prevent user while using the escape key.

eg:

INGcommand: (user press escape here)
return to
INGcommand:

but don't know how...
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 10:32:32 AM
Bravo Patrick !..

it work great !.

a simple question..
why using a list here ..

Code: [Select]
...(list "\nINGcommand: ")
Title: Re: unable the escape key..
Post by: MP on October 10, 2006, 10:34:30 AM
I have to ask why? Elegant or not standard behavior is that hitting the <Esc> terminates a function/command.

Anyway, if you're bent on this approach (which I don't recommend) all you have to do is have the Evaluate function return an empty string instead of nil.
Title: Re: unable the escape key..
Post by: Patrick_35 on October 10, 2006, 10:51:23 AM
In the help of Autolisp
(vl-catch-all-apply 'function list)

Arguments

'function

A function. The function argument can be either a symbol identifying a defun, or a lambda expression.

list

A list containing arguments to be passed to the function.

@+
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 11:22:43 AM
Merci..

there is another problem....

Code: [Select]
(defun INGcommand(/ g)
  (while (setq g (strcase (vl-catch-all-apply 'getstring (list "\nINGcommand: "))))
    (if (not (vl-catch-all-error-p g))
      (progn
        (if (vl-string-search "ING" g)
  (progn
    (setq g1 g)
    (ing_go)
  ))
        (if (eq g "COMMAND")(exit))
        (if (and
          (not (vl-string-search "ING" g))
          (not (eq g "COMMAND")))
          (alert "Invalid ING command.\n Type \"Command\" to return to AutoCAd command.")
        )
      )
      (if (member (vl-catch-all-error-message g) (list "Fonction annulée" "Function cancelled"))
        (setq g T)
      )
    )
  )
)

(INGcommand)


(defun ing_go ()
(setq g T)
  (if (eq g1 "ING_SAVE")
    (progn
    (initdia 1)
    (command "_saveas"))
  ))
 
Title: Re: unable the escape key..
Post by: CAB on October 10, 2006, 11:27:34 AM
Try this:

Code: [Select]
(defun INGcommand (/ g)
  (while
    (progn
      (setq g (vl-catch-all-apply 'getstring (list "INGcommand: ")))
      (cond
        ((vl-catch-all-error-p g) ; yes, error
         (alert "Escape not allowed.")
         t ; stay in loop
        )
        ((vl-string-search "ING" (setq g (strcase g)))
         (alert "You have entered a ING command")
         nil     ; exit loop & return the INGcommand entered.
        )
        ((eq g "COMMAND")
         (exit) ; bye bye
        )
        (t
         (alert
           "Invalid ING command.\n Type \"Command\" to return to AutoCAd command."
         )
         t
        )
      )
    )
  )
  g
)


Code: [Select]
(setq cmd (INGcommand))
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 12:00:59 PM
hhhmm.....seem having same problem..


Code: [Select]
(defun goto_INGcommand ()
  (if (eq g "ING_SAVE")
    (progn
  (initdia 1)
  (command "_saveas")
))
)


(defun INGcommand ()
  (setq g nil)
  (while
    (progn
      (setq g (vl-catch-all-apply 'getstring (list "INGcommand: ")))
      (cond
        ((vl-catch-all-error-p g) ; yes, error
         (alert "Escape not allowed.")
         t ; stay in loop
        )
        ((vl-string-search "ING" (setq g (strcase g)))
         (goto_INGcommand)
         nil     ; exit loop & return the INGcommand entered.
        )
        ((eq g "COMMAND")
         (exit) ; bye bye
        )
        (t
         (alert
           "Invalid ING command.\n Type \"Command\" to return to AutoCAd command."
         )
         t
        )
      )
    )
  )
  g
)



(setq cmd (INGcommand))

maybe need to think for another solution....
using reactor..or something else....

Thanks. CAB.
Title: Re: unable the escape key..
Post by: Patrick_35 on October 10, 2006, 12:06:51 PM
Salut

Code: [Select]
(defun INGcommand(/ g)
  (while (setq g (vl-catch-all-apply 'getstring (list "\nINGcommand: ")))
    (if (not (vl-catch-all-error-p g))
      (progn
        (setq g (strcase g))
        (if (vl-string-search "ING" g)
          (ing_go g)
)
        (if (eq g "COMMAND")(exit))
        (if (and
          (not (vl-string-search "ING" g))
          (not (eq g "COMMAND")))
          (alert "Invalid ING command.\n Type \"Command\" to return to AutoCAd command.")
        )
      )
      (if (member (vl-catch-all-error-message g) (list "Fonction annulée" "Function cancelled"))
        (setq g T)
        (setq g nil)
      )
    )
  )
)

(defun ing_go (g1)
  (if (eq g1 "ING_SAVE")
    (progn
    (initdia 1)
    (command "_saveas"))
  ))

(INGcommand)
Title: Re: unable the escape key..
Post by: CAB on October 10, 2006, 12:09:32 PM
Works for me.   :-o
Code: [Select]
(defun INGcommand (/ g)
  (while
    (progn
      (setq g (vl-catch-all-apply 'getstring (list "INGcommand: ")))
      (cond
        ((vl-catch-all-error-p g) ; yes, error
         (alert "Escape not allowed.")
         t     ; stay in loop
        )
        ((vl-string-search "ING" (setq g (strcase g)))
         (alert "You have entered a ING command")
         nil   ; exit loop & return the INGcommand entered.
        )
        ((eq g "COMMAND")
         (exit) ; bye bye
        )
        (t
         (alert
           "Invalid ING command.\n Type \"Command\" to return to AutoCAd command."
         )
         t
        )
      )
    )
  )
  g
)

(defun c:test (/ cmd)
  (setq cmd (INGcommand))
  (cond
    ((eq cmd "ING_SAVE")
     (initdia 1)
     (command "_saveas")
    )
    (t
     (alert "Invalid ING Command")
     )
  )
)
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 02:02:27 PM
Works for me. 

hug ?..   :?
hmm....bizzarre..!..

Thanks CAB nice try !..   :wink:

Patrick,..your code work perfectly. thank you.  :roll:
Title: Re: unable the escape key..
Post by: CAB on October 10, 2006, 02:16:18 PM
Works for me. 

hug ?..   :?
hmm....bizzarre..!..

Thanks CAB nice try !..   :wink:
Andrea did you copy the second test routine without any changed & test it?
Did you get an error? If it works in the test the problem is in your implementation.  8-)

Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 02:33:34 PM
Andrea did you copy the second test routine without any changed & test it?

Yes, CAB...
copy and paste in a new lisp file...

but the result is that return to AutoCAd command line..
Title: Re: unable the escape key..
Post by: GDF on October 10, 2006, 02:49:59 PM
Works for me.   :-o
Code: [Select]
(defun INGcommand (/ g)
  (while
    (progn
      (setq g (vl-catch-all-apply 'getstring (list "INGcommand: ")))
      (cond
        ((vl-catch-all-error-p g) ; yes, error
         (alert "Escape not allowed.")
         t     ; stay in loop
        )
        ((vl-string-search "ING" (setq g (strcase g)))
         (alert "You have entered a ING command")
         nil   ; exit loop & return the INGcommand entered.
        )
        ((eq g "COMMAND")
         (exit) ; bye bye
        )
        (t
         (alert
           "Invalid ING command.\n Type \"Command\" to return to AutoCAd command."
         )
         t
        )
      )
    )
  )
  g
)

(defun c:test (/ cmd)
  (setq cmd (INGcommand))
  (cond
    ((eq cmd "ING_SAVE")
     (initdia 1)
     (command "_saveas")
    )
    (t
     (alert "Invalid ING Command")
     )
  )
)

Alan

Work fine for me.

Gary
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 05:01:16 PM
ok....let see ...
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 05:05:53 PM
ok guys...

I think i have wrong start...
The program that i'm trying to do...
is to permit only some AutoCad function to the user...
like:

_SAVEAS
_OPEN
_PLOT
_PREVIEW
_LINE
_PLINE
...and some few other...

before continuing this project..
is this program a good way to do it ?

need your experience.   :kewl:
Title: Re: unable the escape key..
Post by: Kerry on October 10, 2006, 05:10:35 PM
What a waste of energy ... why didn't you say that at the start. ?
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 05:16:41 PM
sorry Kerry..
your right....

but i have lurn something again....
maybe is wasting time for you..
but when i lurn.....is not wasting time for me.

I'm here to understand and lurn
how of all of guru like you, make a program better.

 :oops:
Title: Re: unable the escape key..
Post by: Kerry on October 10, 2006, 05:20:59 PM
Andrea, dont play the old "I want to learn" card.

We have been having this same discussion for years.

You continually think that people can read your mind .. and expect it.
Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 06:00:47 PM
this is my another way...
what is your comment about this one..

Code: [Select]
(defun loadFreactor ()

(vl-load-com)
(vlr-command-reactor nil '((:vlr-commandWillStart . startFCommand)))
)


(defun startFCommand (getcommand / CMD)
  (setq CMD (nth 0 getcommand))
  (cond
   
    ((and (/= CMD "TEXT")
  (/= CMD "MTEXT")
  (/= CMD "DTEXT")
  )
     (progn
     (alert "Command disable.")
     (exit)
     )
     
)))

(loadFreactor)

the problem is..that the (exit) do not exit the command.

Title: Re: unable the escape key..
Post by: Andrea on October 10, 2006, 06:01:45 PM
Kerry..please...don't start.. :x
Title: Re: unable the escape key..
Post by: sinc on October 10, 2006, 08:32:31 PM
ok guys...

I think i have wrong start...
The program that i'm trying to do...
is to permit only some AutoCad function to the user...
like:

_SAVEAS
_OPEN
_PLOT
_PREVIEW
_LINE
_PLINE
...and some few other...

before continuing this project..
is this program a good way to do it ?

need your experience.   :kewl:

You could always just UNDEFINE certain commands.  For example, you could put lines like the following in your ACAD.LSP file:
Code: [Select]
(command "undefine" "circle")
It is a simple matter for a moderately-savvy user to override this by using REDEFINE or by preceding the command name with a period ("."), but it might work for low-level users.  Anyway, a moderately-savvy user could probably get around anything you try to do...
Title: Re: unable the escape key..
Post by: Andrea on October 11, 2006, 09:24:28 AM
thanks sinc....

but i can't use undefine command..
because as you said...users can put the " . " symbol.
or use "redefine".

 :|
Title: Re: unable the escape key..
Post by: sinc on October 11, 2006, 12:32:44 PM
thanks sinc....

but i can't use undefine command..
because as you said...users can put the " . " symbol.
or use "redefine".

 :|

If that's important, then the solution you are trying to create probably won't work either.  Lisp is not very robust, and is not designed to be used as a "user shell" inside of Autocad.  It would be very easy for a command to error in such a way that your Lisp routine gets interrupted, and the user is right back at a normal Autocad prompt.  Your user would have to manually restart your little ING thing, and it sounds like you can't trust your users to do this.

I don't think Autocad has any built-in support for what you are trying to do, and I don't think it's possible to create a robust solution in Lisp.  There might be a way in some other language, but I doubt that, too.  Autocad is designed so that the built-in commands can always be accessed, even if they are redefined or undefined, and I can't think of any way of changing that.  I suspect it would be a challenge even if you write your own complete vertical app and run "Autocad inside".

There may be some sort of incredibly clever "trick" you can do and get the results you're after, but I can't think of what it would be...  The usual solution is to just train your users...   :-D
Title: Re: unable the escape key..
Post by: Andrea on October 11, 2006, 01:20:29 PM
Ok...

thanks Sinc..
I will try another way....

599 users to train !!....ouf !.   :lol: