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

command & command-s Help questions

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
1008 Views, 5 Replies

command & command-s Help questions

In the AutoCAD 2015 - AutoLisp Help both the *push-error-using-command* and *push-error-using-stack* make the following statement in reference to the command function in an Error Handler function.

 

Note:  This funciton cannot be used when the command function is used within a custom *error* handler.

 

I assume this note was only supposed to be on the *push-error-using-stack* function help page.

 

Also, on the help page for the AutoLisp command function it makes the following statement.

Also, if you use the command function in an .lsp or .mnl file, it should be called only from within a defun statement. Use theS::STARTUP function to define commands that need to be issued immediately when you begin a drawing session.

 

What happens if you call the command function outside of a Defun statement?

 

Ever since we started writing AutoLisp code with AutoCAD version 2.52 we have written the majority of our code without using the Defun statement.  In the early days AutoLisp memory was very limited and a program could take a minute or two to start executing after calling the load function.  To save memory we create AutoLisp files that just contained a series of AutoLisp function calls.  This reduce the memory requirements and allow the program to begin executing right after it read in the first statement.

 

So in this new fiberless version of AutoCAD what would happen if I execute a command function outside of a Defun statement?

 

Anything bad???

 

5 REPLIES 5
Message 2 of 6
Gary_J_Orr
in reply to: Anonymous

A quick answer is:
In those early days you were in DOS and there was only one way to open a drawing... by starting AutoCAD then using the open command...
times have changed.
If you start launching commands before drawing initialization has finalized you will have problems.
Drawing initialization and AutoCAD Application initialization can also try to occur concurrently if you open a drawing from a link or by double clicking on a drawing in windows explorer.

For application and drawing safety you should use the S::Startup function in your acaddoc.lsp and/or mnl files to make any command calls that you want to make on load to ensure that both application and drawing initialization have both completed (AutoCAD calls the S::Startup as the final step of initialization).

To keep the startup function clean it is easier to wrap your calls within functions that you define in defun statements, then call those functions, but you can also simply place the command calls directly within the startup function if you will not need those functions to be available later in the drawing session.
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 3 of 6
Anonymous
in reply to: Gary_J_Orr

During startup we don't use any command functions unless they are in the S::Startup function.  However, once AutoCAD is fully initialized and a user selects a program from a menu, AutoLisp loads an .lsp file that in most cases does not define any functions using the Defun statement.

 

The part of the command function help that I am concerned with is:

 

Also, if you use the command function in an .lsp or .mnl file, it should be called only from within a defun statement.

 

In the past, loading an AutoLisp file that was created in this manner has not caused any problems and so far I haven't seen any problems running the code in AutoCAD 2015.  However, I don't know if it is doing something behind the scenes that may cause problems later or not.

Message 4 of 6
Anonymous
in reply to: Gary_J_Orr

Additional note:  I went back into AutoCAD 2010 & 2012 and looked at the help for the AutoLisp command function and it makes the same remarks about using the command function only in a defun statement.   Since our code has been running without any problems in the last few years I guess I won't worry about it unless someone tells me that it will cause problems in the new fiberless AutoCAD enviroment.

Message 5 of 6
Gary_J_Orr
in reply to: Anonymous

Basically you have been using lisp to script... load the lisp and it runs what is written in it.
That is not the purpose of lisp.
It is a much safer (and much smarter) method to define any command calls within a function. in fact, everything within lisp files should be defined within function statements with the exception of any variables that you want to define to stay resident for the duration of the drawing being open and/or calls such as (vl-load-com) which loads other functions that your functions may be dependant upon.
By defining functions you can recall those functions without having to reload the lisp each time and you can eliminate "stray" variables (which take up memory as well).
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 6 of 6
Anonymous
in reply to: Gary_J_Orr

True we are using lisp to script.  In addition, I didn't say that we don't define any functions using defun.  For functions used in multiple files or by DCL dialogs we definitely define functions that can be called.  In other cases we may put the common code in a separate .lsp file that can be loaded when necessary.

 

As a stated earlier most of these programs were written when we started using AutoCAD version 2.52 when memory was limited and they have not really changed since then.  I think the last major change to the AutoLisp code was to add DCL dialogs to some of the programs.  As for the AutoLisp variables we have functions we used to initialize and clean them up so that we simulate a local stack.

 

Typically, our client wants to minimize the time and expense migrating from one version of AutoCAD to another.  So if the program works in the new version without any modifications, then the program is never changed.  We have around 334 AutoLisp files which would have to be rework to define everything using defun statements.  That is something I don't think the client would pay for unless I can give them a very good reason.

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

Post to forums  

Autodesk Design & Make Report

”Boost