How to make a lisp run automatically

How to make a lisp run automatically

rob.aHH2T8
Enthusiast Enthusiast
5,822 Views
17 Replies
Message 1 of 18

How to make a lisp run automatically

rob.aHH2T8
Enthusiast
Enthusiast

Is there a way to bypass the command part of a lisp and let it run automatically?  I know how to reference a lisp into the ACAD lisp file.

 

Thanx!

0 Likes
Accepted solutions (2)
5,823 Views
17 Replies
Replies (17)
Message 2 of 18

john.uhden
Mentor
Mentor

First, do not mess with the ACAD.LSP file.  Edit or create your own ACADDOC.LSP file.  It will automatically load and run on each drawing open or new.

Any function you create can be called just by placing it in parentheses, even C: functions.

 

(defun c:Hello ()(princ "Hello")(princ))

 

(c:hello)

John F. Uhden

Message 3 of 18

Ranjit_Singh
Advisor
Advisor

As @john.uhden suggested use the acaddoc.lsp file to call your function. See a sample post here.

0 Likes
Message 4 of 18

cadffm
Consultant
Consultant

You can define a non-Autocad-command lispfunction which you can call with parameters.

(defun c:mycircle (/ p)

  (if (setq p (getpoint "\nPos: "))
      (mycircle p)
  )

)


(defun mycircle (p)

  (command "_.circle" p 5)

)

(defun c:mycircle_20-20-0 ()
(mycircle '(20 20 0))
)

So you can call a lispdefined Autocad command
Command: Mycircle for Userinput

Or without Userinput
call (mycircle '(20 20 0))
or as Autocadcommand: Mycircle_20-20-0

 

[EDIT: added missing bracket]

Sebastian

0 Likes
Message 5 of 18

rob.aHH2T8
Enthusiast
Enthusiast

I must be missing something...

 

This is Ranjit lisp (partial) w/ command line prompt:

 

(defun c:somefunc (/ val v)
(and (setq val "")

 

Do I simply add the 'startup' to the first part of the lisp so it reads:

(defun-q spstartup ()

(defun c:somefunc (/ val v)
(and (setq val "")

 

-OR- do I delete the comandline prompt altogether?

(defun-q spstartup ()

(and (setq val "")

 

or do I do something different?

 

Thanx!

0 Likes
Message 6 of 18

john.uhden
Mentor
Mentor
Accepted solution

I believe that in the dark ages we had to define an S::STARTUP function to automatically run its contents.  But this past year @Kent1Cooper demonstrated that S::STARTUP is really no longer required.  Any calls within your acaddoc.lsp file get run automatically.

Even, for instance:

(load "mylisp.lsp")

or

(command "_.zoom" "_E")

 

Of course you can package a whole bundle of statements into one function, and then just call that function if you want.  That may be a better idea because you can localize variables and even have a localized *error* function to clean up after any errors (oops, we're not supposed to have those, are we?)  Well I don't think so, in spite of what @ActivistMudslinger says.

John F. Uhden

Message 7 of 18

cadffm
Consultant
Consultant

@john.uhden
please post me the link to the statement, thanks


Theres no changes about the intended for acad.lsp acaddoc.lsp and s::startup in the last (18) years!?
http://help.autodesk.com/view/ACD/2018/ENU/?guid=GUID-FDB4038D-1620-4A56-8824-D37729D42520

theorie and professional
only load functions and menus with acad.lsp acaddoc.lsp
s::startup is for running a function after acad open a file (fully)

practise
acad.lsp acaddoc.lsp appload/startup and mnl files evaluate my loading and so you can run a function and edit your drawing,
but autodesk never said that this works fine. And if you read posts from the past then you know
"okay, if it is running on my system i have luck and can use it in that way"



It usually works, but there is always a timing issue and the file or needed components are not fully loaded yet, then this fails.
For one alone no problem if he knows it, but if you use this in professional programs it is negligent.
At least you should be very surprised if it works on one computer and not on the next one.

I am going to abbreviate this, but I wanted to mention it because a) nothing has changed and b) problems can arise.

And do not misunderstand, I use this constantly - for myself!
However, I also know the potential problems, causes and solutions (s :: startup).

Sebastian

Message 8 of 18

rob.aHH2T8
Enthusiast
Enthusiast

Load the command... of course!  I've done that with other pieces, but totally forgot about that part of ACAD.lsp

0 Likes
Message 9 of 18

rob.aHH2T8
Enthusiast
Enthusiast

I accepted as the solution, but can't figure out how to load the command...  Help?

 

Thanx!

0 Likes
Message 10 of 18

Kent1Cooper
Consultant
Consultant

Note that the instruction not to modify the ACAD.lsp file is misplaced.  That is  a User-optional and User-customizable file.  The ones you should not  modify are the reserved acad<release>.lsp file and the reserved acad<release>doc.lsp file.  >>Here<< is Help [for Acad2018, but the same for other releases] about it all -- see the warnings just below the green areas under The ACAD.LSP File and The ACADDOC.LSP File.

 

But note that if you use ACAD.lsp, it will load when AutoCAD is started, but not  in every drawing that you make or open, unless you set the ACADLSPASDOC system variable to have it do that.  ACADDOC.lsp loads in every drawing when you make or open it, regardless.

Kent Cooper, AIA
Message 11 of 18

Kent1Cooper
Consultant
Consultant

@rob.aHH2T8 wrote:

I accepted as the solution, but can't figure out how to load the command...


If it's a command defined in a ThisLisp.lsp file, and if that file is in a location that is in the Support File Search Path list, include this in ACADDOC.lsp to load it in every drawing:

 

(load "ThisLisp")

 

[You can include the .lsp filetype ending, if you want, but it's not necessary.]

 

Or, skip the separate file and the need to load it, and just include the command definition itself right in ACADDOC.lsp.

Kent Cooper, AIA
0 Likes
Message 12 of 18

rob.aHH2T8
Enthusiast
Enthusiast

Perhaps I don't know enough to ask the right question, but my goal is to not have to type in the command when I open the drawing.  I want it to run at startup.  I've used the (load: "sample") but it doesn't work even in the acaddoc.lsp file.

 

Each way I try it errors out as:  AutoCAD menu utilities loaded.somefunc Unknown command "SOMEFUNC".

0 Likes
Message 13 of 18

cadffm
Consultant
Consultant
Accepted solution
In 99.9% it should works (for the 0.1% read my answer to acad/acadoc.lsp vs. s::startup)

But we dont no the content of your "sample.lsp/fas/vlx" file!?

If you start a function within the file
(load: "sample") works (in 99.9%..)

If you only define a function in this file, you have to start the (loaded) function below in the acaddoc.

(load: "sample")
(somefunc)

or if you defined a autocadcommand

(load: "sample")
(c:somefunc)

Sebastian

Message 14 of 18

rob.aHH2T8
Enthusiast
Enthusiast

Ended up being

 

(load: "sample")

(somefunc)

0 Likes
Message 15 of 18

john.uhden
Mentor
Mentor

There ya go!

John F. Uhden

0 Likes
Message 16 of 18

john.uhden
Mentor
Mentor

Thank you, Kent, for that clarification.  Well done!

John F. Uhden

0 Likes
Message 17 of 18

john.uhden
Mentor
Mentor

I actually happen to agree with your premises.  I am behind learning about "newer" things like acadYYYY.lsp and such, and I still use S::STARTUP in the way it was first made available.

There was a time when Softdesk or Land Desktop defined S::STARTUP as an SUBR, so I wrote the following to handle the otherwise inability to append to it (not being a list):

 

(cond
   ((not S::STARTUP)
      (defun-q S::STARTUP ()(do_this)(do_that))
   )
   ((listp S::STARTUP)
      (setq S::STARTUP (append S::STARTUP '((do_this)(do_that))))
   )
   ((member (type S::STARTUP) '(SUBR USUBR))
      (eval
         (list 'defun-q 's::startup ()
            (list s::startup)        ;; NO QUOTE
           '(do_this)
           '(do_that)
         )
      )
   )
)

Also note that whatever is defined last in S::STARTUP may override (redefine) what was defined earlier.  That's why I appended my code.

Of course one should take that approach only if one knows what one is doing.

John F. Uhden

Message 18 of 18

cadffm
Consultant
Consultant

I have supplemented my own s::startup with this opportunity to counter possible problems in the future, thank you!
(for wrong / not defined as list, s::startup function)

 

(and tomorrow I'll think about it again why I have not used it before)

Sebastian

0 Likes