Call Lisp function within itself

Call Lisp function within itself

MarkSanchezSPEC
Advocate Advocate
3,675 Views
27 Replies
Message 1 of 28

Call Lisp function within itself

MarkSanchezSPEC
Advocate
Advocate

I have a lisp routine that is loaded at startup via an MNL file like so;

 

(defun C:MyFunc ()

)

I would like to automatically run this routine once at startup. If I put a call to "(c:MyFunc)" BEFORE the defun, I get "error: no function definition C:MyFunc". This makes sense. However, when I call it AFTER the defun, the routine runs over and over in an endless loop. Is it possible to do this?

0 Likes
Accepted solutions (1)
3,676 Views
27 Replies
Replies (27)
Message 2 of 28

cadffm
Consultant
Consultant

Is it possible? Sure, but it make no sense, or?

Is your routine running endless or do you want that the routine runs endless (what make no sense)?

 

Sorry, i can't understand what you have or what you want (and why?)

 

 

Sebastian

0 Likes
Message 3 of 28

MarkSanchezSPEC
Advocate
Advocate

I want it to be loaded and run ONE time, whenever AutoCAD is opened.

0 Likes
Message 4 of 28

SeeMSixty7
Advisor
Advisor

Run the function after the definition of your function.

(defun c:myfunc()

 (princ "do stuff")

) ;this closes your defun statement

(c:myfunc); calls it once

0 Likes
Message 5 of 28

cadffm
Consultant
Consultant

Then add the (load "d:/mypath/myfile.lsp") statement to the custom ACAD.lsp file

and set ACADLSPASDOC to the only meanful value: 0

 

or

 

(and (load "d:/mypath/myfile.lsp")(c:myfunc))

Sebastian

0 Likes
Message 6 of 28

MarkSanchezSPEC
Advocate
Advocate

I guess because I'm loading through the MNL, that is part of a loaded menu, this give me an endless loop when I open AutoCAD.

0 Likes
Message 7 of 28

cadffm
Consultant
Consultant

In mnl isn't a perfect place for this (acad.lsp would),

so you need a flag "started in this session before? yes/no".

 

A "stupid" sample, whats means SIMPLE and not bulletproof:

 

(or (= "MyFuncWasHere" (getvar "USERS5")) (progn(load "d:/mypath/myfile.lsp")(setvar "USERS5" "MyFuncWasHere")(c:myfunc))

 

or similar

Sebastian

0 Likes
Message 8 of 28

SeeMSixty7
Advisor
Advisor

It's not really a good idea to try and use the mnl file to execute code as there is no guarantee when that code will be loaded. Defining functions or commands in the mnl file is not a bad thing but trying to execute code is not the best idea. If you know the drawing is loaded and then you load the menu, then you have control over when it is executed.  

0 Likes
Message 9 of 28

Sea-Haven
Mentor
Mentor

Look into using the s::Startup section of acaddoc.lsp this runs last so should run your lisp once.

 

What happens if you use appload and add it to "Start up suite"

0 Likes
Message 10 of 28

cadffm
Consultant
Consultant

@Sea-Haven  schrieb:

Look into using the s::Startup section of acaddoc.lsp this runs last so should run your lisp once.

 

What happens if you use appload and add it to "Start up suite"


I think the current problem is it runs per file opening (or CUI/Workspace edits), SeeMSixty7s hint was general though.

(but without to know what the code is and for usual: Code what should run just once per session is not a problem/ crystal ball off)

Sebastian

0 Likes
Message 11 of 28

MarkSanchezSPEC
Advocate
Advocate

I tried this through an MNL file but get the endless loop. Should this work through an MNL file?

This is what I tried:

(defun c:MyFunc ()

)

(c:MyFunc)

 

Thanks

0 Likes
Message 12 of 28

MarkSanchezSPEC
Advocate
Advocate

We are actually defuning our function in an MNL file that is part of a menu that gets loaded when Acad loads. When done this way, I have found the behavior as I described above. Is there something different or special that needs to be applied inside the MNL file to defun it and then call it once at startup?

0 Likes
Message 13 of 28

SeeMSixty7
Advisor
Advisor

You can define functions through the MNL file, but executing them from there is not something you want to do.

If you want it run once for every dwg session, you should use the ACADDOC.LSP file.  I fit is something you want run when they open AutoCAD place it in the ACAD.LSP file. if it is something you only want run once ever. Take a different approach and have something look for an update.lsp file and then have the update.lsp file delete itself upon successful execution.

0 Likes
Message 14 of 28

ronjonp
Advisor
Advisor

@SeeMSixty7 wrote:

You can define functions through the MNL file, but executing them from there is not something you want to do.

...


Can you elaborate on this?

0 Likes
Message 15 of 28

cadffm
Consultant
Consultant

>"that gets loaded when Acad loads"

But mnl files loads everytime when the cuix loads, that is in every file and for every cuix work (command cuix)

Sebastian

0 Likes
Message 16 of 28

SeeMSixty7
Advisor
Advisor

@ronjonp The menu file's MNL file is loaded when the CUIX is loaded. AutoCAD Stated a long time ago in some developement documentation not to depend on when the mnl file would be loaded, but that if the menu is loaded the mnl file woudl be loaded by the time it was accessible to the user. This means that if your menu calls any tools when the user clicks they will execute, by design and that works. Problem is when is the mnl file loaded and you wish to execute something when the menu loads, there might not be a drawing file loaded, there might not be any access to the user interface, there might be processes running that would terminate anything you run as it continues to load the drawing. I believe Autodesk stated that so that if they change or manipulate the Menu system loading sequences your code would not depend on the menu's mnl file loading at any particular time. Where and when I read that, I can't remember, but it was most likely 20 years plus back when they added the mnl functionality.

0 Likes
Message 17 of 28

ronjonp
Advisor
Advisor

@SeeMSixty7 

Thanks for the detailed explanation! I was curious because this method has been solid for me for about 20 years *shrugs*.

0 Likes
Message 18 of 28

JamesMaeding
Advisor
Advisor

wow, this is a big mess and no one has asked for the .mnl yet? I have used the .mnl forever to define functions, run them, all kinds of stuff.

Its not a bad idea at all unless the items must be run in a particular order, and then you would put in a lisp that loads on drawing open like acaddoc.lsp.

I hate the startup suite. Makes troubleshooting other peoples machines a pain and I find people that use it generally are exactly the ones that cannot troubleshoot, so....

This endless loop thing is either a bug, or your coding is behaving and you told it to loop.

post the .mnl please so we can end the confusion.


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

0 Likes
Message 19 of 28

JamesMaeding
Advisor
Advisor

@SeeMSixty7 

Your blog almost made my head explode. Many people here would encourage you to improve it, especially if it succeeded with exploding my head.

You have like, 4 posts, and I have to scroll to see them. Just saying, that might be the least alluring blog format I have come across. I'm kind of wondering if it helps your reputation, honestly. Did you get started and give up on it?

I can't complain as i did exactly that too, but I let mine expire out of mercy.


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

0 Likes
Message 20 of 28

SeeMSixty7
Advisor
Advisor

@JamesMaeding Yeah that is pretty much exactly what happened. Got too busy. I go back to it every now and again trying to do something on it. I hate Wordpress. LOL. It does have some cool features and such, but I keep wanting to completely develop it from the ground up using some cool Javasript, Node back end, and a nice db backend, but it takes time, and as you probably experienced as well, I just can't carve out the time to do it.

 

Thanks for visiting it though and the feedback.I'll have to get off my duff and do something more with it.

0 Likes