Run a lisp (not load) when a drawing opens

Run a lisp (not load) when a drawing opens

yan_ken
Advocate Advocate
6,023 Views
22 Replies
Message 1 of 23

Run a lisp (not load) when a drawing opens

yan_ken
Advocate
Advocate

HI Folks,

 

I want to run a run or execute a lisp routine when a drawing is opened. I don't mean loading lisp file, which is already in the APPLOAD startup suite.

 

How can I run a lisp on every drawing I open?

 

Thank you,

Ken

0 Likes
6,024 Views
22 Replies
Replies (22)
Message 2 of 23

Satoews
Advocate
Advocate

INSTEAD OF THE USUAL DEFUN USE THIS ONE.

 

(defun-q S::STARTUP () 
Shawn T
0 Likes
Message 3 of 23

Kent1Cooper
Consultant
Consultant

@yan_ken wrote:

HI Folks,

 

I want to run a run or execute a lisp routine when a drawing is opened. I don't mean loading lisp file, which is already in the APPLOAD startup suite.

 

How can I run a lisp on every drawing I open?

 

Thank you,

Ken


You can either:

 

A)  Include the operational code of the routine directly in acaddoc.lsp, not defined into a routine with a name;

 

B)  Include the calling of the routine itself within the file that defines it, at the end, so that when it is loaded it runs, whether as a function:

 

(defun dothis (something / whatever)

  (...the code to do this..)

); defun

(dothis)

 

or as a command:

 

(defun C:dothis (/ whatever)

  (...the code to do this..)

); defun

(C:dothis)

 

C)  Include the calling of the routine in acaddoc.lsp after the loading of it:

 

(load "dothis")

(dothis)

Kent Cooper, AIA
0 Likes
Message 4 of 23

john.uhden
Mentor
Mentor

Kent knows this.  He just didn't mention it, I think because he likes people to learn sometimes the hard way.  It's definitely a way to put things in the front of your memory.

 

You can't call a function within acaddoc.lsp that uses the (command) function.

So they gave us a specially named function we can define called S::STARTUP.

The help instructions tell us to create it using defun-q so that it is a list, not an SUBR.

It also advises to not overwrite the function if it has already been defined.  But I ran into a situation years ago where Land Desktopment had defined it as an SUBR, so you couldn't append to it.  Here's the workaround (it's been posted before)...

 

(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)
         )
      )
   )
)

The idea is to include the code in your acaddoc.lsp file along with the function(s) it calls.

John F. Uhden

Message 5 of 23

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

Kent knows this.  ....  You can't call a function within acaddoc.lsp that uses the (command) function.

 

....

I had forgotten that, but it eliminates only my A) option, and even that only if what the OP wants to run involves any (command) functions, which it may not.

 

Help says:

  If you use the command function in an acad.lsp, acaddoc.lsp, or MNL file, it should be called only from within a defun statement.

 

which would mean you can't have a (command) function directly in there, but not that you can't call a function that uses one.

Kent Cooper, AIA
0 Likes
Message 6 of 23

john.uhden
Mentor
Mentor

I think I will disagree with you.  I think that any function called within acad*.lsp can not call the command function.  They can be defined, but not called.  That's what S::STARTUP is for.

John F. Uhden

0 Likes
Message 7 of 23

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

I think I will disagree with you.  I think that any function called within acad*.lsp can not call the command function.  They can be defined, but not called.  That's what S::STARTUP is for.


So I broke down and actually tried it, and in fact a function including a (command) function can be called from within acaddoc.lsp.  I made a little file called TEST.lsp, containing this command definition:

 

(defun C:TEST ()
  (command "_.circle" (getvar 'viewctr) 12)
)
(C:TEST)

 

in which the last line calls its own command when it's loaded [and I tested that, just to be sure, with APPLOAD in an already-open drawing -- merely load it, and the Circle is drawn].  That's the B) option in Post 3.

 

I then included this line [temporarily] in my acaddoc.lsp file:

 

(load "TEST")

 

and when I opened some other drawings [both existing and new], in each one the Circle was drawn.

 

Then I tried the C) option in Post 3 -- omitting the (C:TEST) self-calling line from TEST.lsp, and putting it into acaddoc.lsp after the (load "TEST") line.  Same result.

 

AND THEN, just for a lark [not expecting it to work, because of the advice in Help], I tried just putting the (command) function itself:

 

(command "_.circle" (getvar 'viewctr) 12)

 

directly in acaddoc.lsp [the A) option in Post 3], and that also worked.

Kent Cooper, AIA
Message 8 of 23

john.uhden
Mentor
Mentor

Thank you for your experimentation.  I guess the rules change, and unless we test them we might never know.

But that's why we are here... to learn from others.

John F. Uhden

0 Likes
Message 9 of 23

Anonymous
Not applicable

@Kent1Cooper wrote:

@yan_ken wrote:

HI Folks,

 

I want to run a run or execute a lisp routine when a drawing is opened. I don't mean loading lisp file, which is already in the APPLOAD startup suite.

 

How can I run a lisp on every drawing I open?

 

Thank you,

Ken


You can either:

 

A)  Include the operational code of the routine directly in acaddoc.lsp, not defined into a routine with a name;

 

B)  Include the calling of the routine itself within the file that defines it, at the end, so that when it is loaded it runs, whether as a function:

 

(defun dothis (something / whatever)

  (...the code to do this..)

); defun

(dothis)

 

or as a command:

 

(defun C:dothis (/ whatever)

  (...the code to do this..)

); defun

(C:dothis)

 

C)  Include the calling of the routine in acaddoc.lsp after the loading of it:

 

(load "dothis")

(dothis)


hi kent 

 

i am facing same problem as YANKEN told. i would like to need to load all lisp one time . no need to one by one.i did everything as you mentioned but i could not get till now. what should i do? kindly see attached the image. are this correct or not ? thanks advance

 

best regards 

hussain

0 Likes
Message 10 of 23

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... kindly see attached the image. are this correct or not ? ....

Try removing the double-quote character in the middle of the first line.

 

If it still doesn't work, are there any error messages?

Kent Cooper, AIA
0 Likes
Message 11 of 23

Anonymous
Not applicable
hi kent

thank so much your reply. that is folder name. are you talking about this
TH-LSP?

BEST REGARDS

hussain
0 Likes
Message 12 of 23

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:
.... that is folder name. are you talking about this
TH-LSP?

...

I'm talking about this line in the image:

 

(load "D:/TH-LSP/"mepel")

 

which I suspect should be just:

 

(load "D:/TH-LSP/mepel")

Kent Cooper, AIA
0 Likes
Message 13 of 23

JamesMaeding
Advisor
Advisor

I wish more people know how to do this, as its critical to getting the value out of acad you paid for.

 

I might add to this, you should make sure acad always finds your acad.lsp and acadoc.lsp, use:

(findfile "acad.lsp") and (findfile "acaddoc.lsp")

 

What I tell people as a recipe for startup control is:

1) do not use startup suite, blank it out

2) make a folder called "my tools" on your c drive and add it as top support path in acad.

3) make a blank acad.lsp in that folder to be sure some other is not used.

4) make an acaddoc.lsp in that folder, with code like:

(DEFUN S::STARTUP ()
  (vl-load-com)
  (IF (NOT STARTUP-HAS-RUN)
    (PROGN
      (princ "\nRunning Initial Startup...")
      ;---------------- start stuff to run once per session --------------------
      (COMMAND "-GRAPHICSCONFIG" "L" "S" "X") ;HARDWARE ACCELL OFF - an example
      ;add more here
      
      ;---------------- end stuff to run once per session --------------------
    )
  )
  ;set ran flag
  (SETQ STARTUP-HAS-RUN 1)
  (vl-propagate 'STARTUP-HAS-RUN) ;so future dwg's get this variable
  ;---------------- start stuff to run on every drawing --------------------
  (if (findfile "somelisp.lsp")(progn (load "somelisp.lsp")(thefunctiontorun)))
  (setvar "blipmode" 0) ;....whatever you like to set
  ;add more here
  
  ;---------------- end stuff to run on every drawing --------------------
  (princ "\nDone with acad startup.\n")
  (princ)
)

 

Once you do that, you have control of startup and can add anything you like.

The defun-q Jon mentioned may or may not be important to you. I would likely do it his way so ask if you need that shown explicitly.


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

0 Likes
Message 14 of 23

Anonymous
Not applicable
HI KENT

Thank you so much your reply.i followed your instruction after there is
showing no successfully loaded.could you help me.
i have attached image here. kindly find below.

best regards

hussain
0 Likes
Message 15 of 23

Anonymous
Not applicable

@Anonymous wrote:
HI KENT

Thank you so much your reply.i followed your instruction after there is
showing no successfully loaded.could you help me.
i have attached image here. kindly find below.

best regards

hussain

 

0 Likes
Message 16 of 23

Kent1Cooper
Consultant
Consultant

Are you sure the file is in that location, and that that name is correct?

 

(findfile "D:/TH-LSP/mepel.lsp")

 

[assuming it's a .lsp file type -- use the correct filetype ending].  What is returned by that?

 

This may or may not help, but I don't typically put file paths into (load) or (autoload) functions in acaddoc.lsp.  I put the files in a folder that is in the Support File Search Path list [in OPTIONS, Files tab], where I know AutoCAD will be able to find them without my specifying the path, and I (load) or (autoload) them with just their file names only.

Kent Cooper, AIA
0 Likes
Message 17 of 23

Anonymous
Not applicable
hi kent

i am glad your reply.yes i had done as you mentioned but i could not get
till now. i don't know if i were doing right or wrong. any way thanks kent.
your instruction is really helping me. i have attached image . kindly find
below.

best regards

hussain
0 Likes
Message 18 of 23

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:
... i have attached image . ....

[It didn't attach.]

Kent Cooper, AIA
0 Likes
Message 19 of 23

Anonymous
Not applicable

hi kent 

 

i am so sorry.i reply via google.some time does not happen in post. ok any i will post here

 

best regards 

 

hussain

0 Likes
Message 20 of 23

Kent1Cooper
Consultant
Consultant

That list should not contain .lsp files themselves, only folder locations.  Put the file in a folder that is in that list, and AutoCAD should be able to find it.

Kent Cooper, AIA
0 Likes