Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to detect user hitting ESC key during SAVEAS command?

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
toolbox2
965 Views, 12 Replies

How to detect user hitting ESC key during SAVEAS command?

I'm putting together a simple lisp routine to help issue bound, purged drawings.

 

I have code that will do the binding & purging but need help with handling the saving.

 

This is my approach :

 

(1) ask the user to save the drawing using this line (command "_saveas" "" "~")

(2) run a subroutine that binds xrefs etc (this works ok)

(3) run the QSAVE command

 

Trouble is, if the user cancels or hits escape during the SAVEAS command the subroutine & QSAVE will still run, overwriting the current drawing. How do I handle this scenario? Or should my appoach be different?

12 REPLIES 12
Message 2 of 13
Lee_Mac
in reply to: toolbox2

Maybe compare the value of the DWGNAME System Variable before and after the call to SaveAs?

Message 3 of 13
Lee_Mac
in reply to: Lee_Mac

Quick example:

 

(defun c:test ( / cmd dwg )
    (setq cmd (getvar 'CMDECHO))
    (setvar 'CMDECHO 0)

    (setq dwg (strcase (strcat (getvar 'DWGPREFIX) (getvar 'DWGNAME))))
    (command "_.saveas" "" "~")
    (if (eq dwg (strcase (strcat (getvar 'DWGPREFIX) (getvar 'DWGNAME))))
        (princ "\nUser Cancelled Dialog.")
        (princ "\nDrawing was Saved.")
    )

    (setvar 'CMDECHO cmd)
    (princ)
)

 

Message 4 of 13
pbejse
in reply to: toolbox2

look into vlr-command-reactor

 

:vlr-commandCancelled

Message 5 of 13
Kent1Cooper
in reply to: toolbox2


@toolbox2 wrote:

....

This is my approach :

 

(1) ask the user to save the drawing using this line (command "_saveas" "" "~")

(2) run a subroutine that binds xrefs etc (this works ok)

(3) run the QSAVE command

 

Trouble is, if the user cancels or hits escape during the SAVEAS command the subroutine & QSAVE will still run, overwriting the current drawing. How do I handle this scenario? Or should my appoach be different?


Perhaps something like this:
 

(if (not (vl-cmdf "_saveas" "" "~"))

  (quit); then - simply stop overall routine before it gets to the following

); end if - no else argument - do nothing here if Saveas is completed

(.... on to the subroutine that binds xrefs, etc....)

....

 

EDIT:  Never mind -- that doesn't seem to work as I expected -- the (not (vl-cmdf.... doesn't return T if you hit Escape in the Saveas command.  But maybe the approach could be valid if formatted differently?

 

(if (vl-cmdf "_.saveas" "" "~")

  (progn

    (....the subroutine....

Kent Cooper, AIA
Message 6 of 13
Kent1Cooper
in reply to: Kent1Cooper


Kent1Cooper wrote:

 

....  But maybe the approach could be valid if formatted differently?

 

(if (vl-cmdf "_.saveas" "" "~")

  (progn

    (....the subroutine....


No, that doesn't seem to do it, either.

Kent Cooper, AIA
Message 7 of 13
scot-65
in reply to: toolbox2

Pausing in the middle of the program for user input is not a good idea.

Tisk tisk...

 

Approach it differently by using the GETFILED function, and if successful

execute the rest of the program while quietly using the command line saveas...

 

You can mimic the saveas dialog box as though one would not notice.

 

You can also apply a prefix/suffix to the original file name and use that as the "default" file name

(strcat (VL-FILENAME-BASE (getvar 'dwgname)) "-Bind01-" (itoa (fix (getvar 'cdate))) ).

Untested, but you get the idea.

 

Minimum flag to set is 1.

 

Review the AutoLISP reference for further details.

 

???


Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.


Message 8 of 13
toolbox2
in reply to: Lee_Mac

I've got this up and running but I need something more robust. It's feasible that a user would want to use the same filename - in which case the xref binding code won't run.

 

pbejse has suggested using a reactor. Not familiar with these but am trying to fathom them out.

 

By the way, good stuff on your website 🙂

Message 9 of 13
toolbox2
in reply to: scot-65

Thanks for the tip. Is it possible to get the full-on SAVEAS dialogue box with the shortcuts on the left etc

Message 10 of 13
Lee_Mac
in reply to: toolbox2


toolbox2 wrote:

I've got this up and running but I need something more robust. It's feasible that a user would want to use the same filename - in which case the xref binding code won't run.


 OK, perhaps:

 

(defun c:test ( / cmdecho tdupdate )
    (setq cmdecho (getvar 'CMDECHO))
    (setvar 'CMDECHO 0)
    
    (setq tdupdate (getvar 'TDUPDATE))
    (command "_.saveas" "" "~")
    (if (< tdupdate (getvar 'TDUPDATE))
        (princ "\nDrawing was saved.")
        (princ "\nUser pressed Cancel.")
    )

    (setvar 'CMDECHO cmdecho)
    (princ)
)

 


toolbox2 wrote:

pbejse has suggested using a reactor. Not familiar with these but am trying to fathom them out.


 

Since LISP doesn't allow multiple threads, the reactor callback function would not be evaluated whilst the program you are running has focus.



toolbox2 wrote:

By the way, good stuff on your website 🙂


 

 Cheers! Smiley Happy

Message 11 of 13
scot-65
in reply to: toolbox2


@toolbox2 wrote:

Thanks for the tip. Is it possible to get the full-on SAVEAS dialogue box with the shortcuts on the left etc


I do not think this is possible since the saveas dialog *might* be written in .NET

while getfiled *might* be written in DCL...

 

I personally would work with getfiled. It's safer than to pause for user input.

 

I think Lee_Mac is on the right track by testing if the file name is not the same before

and after the saveas. However, not testing, it would seem if one hit the cancel button

in the saveas dialog, an empty string would return - and it is different than before the

saveas... which means true (when using getfiled method)...

 

???


Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.


Message 12 of 13
toolbox2
in reply to: Lee_Mac

Decided to go with this. Thanks.

 

Incidentally, the xref binding code I'm using came from here:

http://www.cadtutor.net/forum/showthread.php?13141-Need-help-on-my-Detach-Audit-Bind-Purge-code/page...

 

where I believe you gave a helping hand.

 

 

Message 13 of 13
Lee_Mac
in reply to: toolbox2


toolbox2 wrote:

Decided to go with this. Thanks.

 

Incidentally, the xref binding code I'm using came from here:

http://www.cadtutor.net/forum/showthread.php?13141-Need-help-on-my-Detach-Audit-Bind-Purge-code/page...

 

where I believe you gave a helping hand.



Good stuff toolbox2, happy to help Smiley Happy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost