Automatically Running a Lisp when Opening Drawings

Automatically Running a Lisp when Opening Drawings

schmidtjBEEVY
Advocate Advocate
2,148 Views
10 Replies
Message 1 of 11

Automatically Running a Lisp when Opening Drawings

schmidtjBEEVY
Advocate
Advocate

Hello everyone,

 

I am trying to get a lisp to run a series of commands when I open a drawing. I found this post from 2017 that had some code on how to do it. I have modified it, and placed it in my acaddoc.lsp file, but it doesn't seem to be working. Can someone point out what I'm doing wrong?

 

Thanks

-JD

(defun-q spstartup () ;Run the following commands on start up of each drawing
  
    (command "snap" "on")
    (command "snap" ".0625")
    (command "ucsicon" "off")
    (command "grid" "off")
    (command "units" "2" "3" "1" "0" "0" "n")
    (command "style" "romans" "romans" "0" "1" "0" "n" "n" "n")
    (command "ZOOM" "E" "")
    (command "DISPLAYVIEWCUBEIN2D" "off")
    (command "_navvcube" "off")
     
)
(setq s::startup (append s::startup spstartup))
0 Likes
Accepted solutions (1)
2,149 Views
10 Replies
Replies (10)
Message 2 of 11

Kent1Cooper
Consultant
Consultant

@schmidtjBEEVY wrote:

.... it doesn't seem to be working. ....


Not enough information.  Does any of it happen?  How much?  Etc.

 

BUT it doesn't seem necessary to go through the (defun-q...) approach and the s::startup business.  I expect you could just include lines 3 through 11 in acaddoc.lsp, just as they are, without "wrapping" them inside anything else.  I would also be inclined to use (setvar) for much of it, rather than (command) functions, and for those you choose to do with (command) functions, combine them into one, but that's not essential.

Kent Cooper, AIA
Message 3 of 11

schmidtjBEEVY
Advocate
Advocate

Hi Kent, thanks for your reply! 

 


@Kent1Cooper wrote:

@schmidtjBEEVY wrote:

.... it doesn't seem to be working. ....


Not enough information.  Does any of it happen?  How much?  Etc.


Ope, yeah that would have been good information up front; none of the commands were making changes.

 

As for the rest of your message, I did take them out of the (defun-q) and put them with the rest of the (SETVAR)s in the file. I also changed them from (command)s to (SETVAR)s. It appears that the changes did not occur. A few of them, such as turning the grid off, were already off on the drawing I opened to test the changes.

 

I will mess with it some more tomorrow to see if I can get it working, thanks for the suggestion, Kent!

0 Likes
Message 4 of 11

Kent1Cooper
Consultant
Consultant

@schmidtjBEEVY wrote:

.... I also changed them from (command)s to (SETVAR)s. It appears that the changes did not occur. ....


Just guessing here....  Did you perhaps make that change by just changing the function name?  This, for example:

 

(setvar "snap" ".0625")

 

would not work.  The System Variable names are not necessarily the same as the command names, and numerical values would not be text strings.  That one would need to be done this way:

 

(setvar 'snapunit '(0.0625 0.0625))

 

[Note that there are two numbers in a list -- Snap can have different increments in the X & Y directions.]  Find the system variables in Help and use (getvar) to see in what format they store their information.  For example, some that you set using "On" or "Off" at the command line actually store their values as 1 or 0, and those would be the actual System Variable values you would need to use.

 

And of course, not all of those settings can be done with (setvar) at all.

Kent Cooper, AIA
Message 5 of 11

Sea-Haven
Mentor
Mentor

Rather than using acaddoc.lsp save as a custom lisp, then just use Appload and add to Start up Suite, much easier as you can have code saved in say your directory of lisp programs. My Startup has like 5 lisps loaded on opening CAD. 

0 Likes
Message 6 of 11

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:

Rather than using acaddoc.lsp ... just use Appload and add to Start up Suite, much easier .... 


[In my opinion, acaddoc.lsp is the easier way, especially when it comes to upgrading to a new version.  You don't need to go into APPLOAD and populate the Startup Suite with multiple things, but merely to put your acaddoc.lsp file into some place where AutoCAD knows to look, which you could presumably do even before you first open any drawing in the new version.  And you can put direct operations in it as in this topic, without their needing to be defined into .lsp files as is needed to put them in the Startup Suite.  Also, the Startup Suite is described as being run on startup of the program -- hence the name -- but not on opening or creating every drawing as acaddoc.lsp does.  It seems in trials that at least some things you can put in there are available in every drawing anyway, but I am reluctant to trust that everything would be.]

Kent Cooper, AIA
Message 7 of 11

schmidtjBEEVY
Advocate
Advocate

How would I write the (defun) instruction such that I don't need to type in the command? I already have a few lisps in my startup suite, but they all require being called from the command line. They don't just run when I open a drawing.

0 Likes
Message 8 of 11

TomBeauford
Advisor
Advisor

As code in your startup suite, acad.lsp or acaddoc.lsp is run automatically you could either add (C:function) for code that would normally be run when "function" was entered at the command line.

As defun is for defining functions to be called later you shouldn't need to defun code that's being automatically run anyway.

64bit AutoCAD Map & Civil 3D 2023
Architecture Engineering & Construction Collection
2023
Windows 10 Dell i7-12850HX 2.1 Ghz 12GB NVIDIA RTX A3000 12GB Graphics Adapter
Message 9 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

@schmidtjBEEVY wrote:

How would I write the (defun) instruction such that I don't need to type in the command? I already have a few lisps in my startup suite, but they all require being called from the command line. They don't just run when I open a drawing.


See Message 2.  It's an advantage of acaddoc.lsp that if you put those lines [or the (setvar) equivalents of some of them] into it "raw," they will just run upon opening or creating any drawing, with no (defun)ition of a command or function name needed.

 

EDIT:  Or, I find that you can do the same in a file that can be in the Startup Suite, if you prefer that middle-man approach.  Just put those operations "raw" into that file, with no "wrapping" (defun) function, and the same will happen.

Kent Cooper, AIA
Message 10 of 11

schmidtjBEEVY
Advocate
Advocate
Ohhhhh, ok, I didn't fully understand message 2 until now. I thought you meant to say I cannot put raw (command)s in the acaddoc and that they had to be in the form of a (setvar). I was able to get it working with using (command)s.
Thank you for your help, everyone! (Especially you, Kent)
0 Likes
Message 11 of 11

Sea-Haven
Mentor
Mentor

Just a comment been using "start up suite" for as long as I can remember, I use a setup lsp when doing new installs, which sets paths, loads menu's etc, yes need to add to the startup suite as part of setup.  For Bricscad its in a directory C:\USER\xxx\appdata\Roaming\Bricysys..... \appload.dfs and is a dfs text file. Expect same for Acad. Using custom mnu you can take advantage of MNL files that say autoload matching lisp programs. NOTE I believe Acad LT 2024 does not support MNL.

 

0 Likes