Jump to content

How to check if file is loaded or not, and so on...


Recommended Posts

Posted

Hey guys,

 

I am stuck with this, allthough it is not that complicated I believe... I want to make a macro that does following:

 

1. check if a lisp file is loaded (so that a custom created function is available)

 

2. if it is not loaded, then check if the file itself is present (the lisp file in a specific map or in one of the support file paths)

 

3. if it is there then load the thing

 

4. if it is not there, alert for a problem, whatever, and stop the command.

 

5. if lisp file is actually there and loaded, then execute the command in the lisp.

 

This would be test.lsp (just an example):

 

[color=black](defun c:ttt()[/color]
[color=black] (setq pt1 (getpoint "Point 1:")[/color]
[color=black]pt2 (getpoint "Point 2:")[/color]
[color=black] )[/color]
[color=black] (command "line" pt1 pt2 "")[/color]
[color=black] (princ)[/color]
[color=black])[/color]
[color=black](princ)[/color]

 

This is my macro so far:

[color=black]^C^C^P(if (and (findfile (strcat (getenv "programfiles") "[/color][url="file://\\MyMap\\Program\\test.lsp"))(not(c:ttt)))(load"][color=black]\\MyMap\\Program\\test.lsp"))(not(c:ttt)))(load[/color][/url][color=black] (strcat (getenv[u] "programfiles") "[/u][/color][url="file://\\MyMap\\program\\test.lsp"))(alert"][color=black]\\MyMap\\program\\test.lsp"))(alert[/color][/url][color=black] "Not found"))ttt;[/color]

 

I know it is not all corresponding (the lisp and macro) but I don't come up with better... espescially the part "check if it is allready loaded"... I am totally unsure about (not(c:ttt)).

 

Any help is appreciated, as allways.

Posted

You would save yourself a lot of headache if you added your Lisp folder path to the Support Paths (in options). It's also the best policy.

 

What you have is about the best you can do. Generally, if I having something like that, I'll call it with:

 

(or c:TTT (load "test.lsp" nil))

 

 

Also, to remove any chance of error, you should always fill the second variable requirement for the load subroutine. Either it be nil or a "String" message to display if the file cannot be found. If nothing is there, it will error if it can't be found.

 

Example:

 

Command: (load "blah.lsp")

Error: LOAD failed: "blah.lsp"
Command: (load "blah.lsp" nil)
nil

Command: (load "blah.lsp" "Cannot find blah.lsp")
"Cannot find blah.lsp"

Posted

Oh yeah, I forgot to mention, to just check, and is what you use.

 

(and c:ttt)

Posted

Alanjt,

 

If I'd leave the "getenv" part and add the lisp-folder to the support file path, that would make that part a bit easier, that is true.

 

Let's say I have added the folder, but I do not want to place the lisp in the startupsuite, and I don't want to add something in acaddoc.lsp.

 

Then I still need a macro that does what I need: look if the function is loaded, if so then execute the command. If it is not loaded, then load it. If the file does not exist, then alert for an error.

 

In this way AutoCad does not have to load all lisp files when starting up a drawing. I think that is good to decrease time needed for opening.

 

Now this macro will work but if the file is not loaded and it cannot be found it still tries to execute the (alert "command"). That should not happen.

 

(Without the ^C^C^P and ttt; that has to come in the macro)

 
(if
   (not c:ttt)
   (if
     (findfile "test.lsp")
     (load "test.lsp")
     (alert "Error: file not found")
     )
   )
 (alert"command")

 

:x it bothers me that I can't find my solution. Any of you guys?

Posted

Just out of curiosity, have you tried autoload?

 

Example:

(autoload "test.lsp" '("tt" "test"))

 

This will perform an on demand load of the routine when either tt or test is typed.

 

If this will not suite, then we'll continue with what you are wanting. :)

Posted

Also, to remove any chance of error, you should always fill the second variable requirement for the load subroutine. Either it be nil or a "String" message to display if the file cannot be found. If nothing is there, it will error if it can't be found.

 

Example:

 

Command: (load "blah.lsp")

Error: LOAD failed: "blah.lsp"
Command: (load "blah.lsp" nil)
nil

Command: (load "blah.lsp" "Cannot find blah.lsp")
"Cannot find blah.lsp"

 

 

Thanks for posting this, I have been modifying an acaddoc.lsp file I copied from here and was not sure why it was written the way it is. But I'm still confused here.

 

The file I have uses :

(if (Load "mcopy" "mcopy.lsp failed to load\n")
(princ "\nMcopy lisp is loaded, MCOPY to invoke.\n")
)

But if I understand that correctly, the if will never return NIL since the load has a string to return if it fails to load. What am I doing (or thinking) wrong?

 

Glen

Posted
Thanks for posting this, I have been modifying an acaddoc.lsp file I copied from here and was not sure why it was written the way it is. But I'm still confused here.

 

The file I have uses :

(if (Load "mcopy" "mcopy.lsp failed to load\n")
(princ "\nMcopy lisp is loaded, MCOPY to invoke.\n")
)

But if I understand that correctly, the if will never return NIL since the load has a string to return if it fails to load. What am I doing (or thinking) wrong?

 

Glen

 

 

You'll get a non-nil return, but I'm not sure why you would want what you are doing. Are you going to do this for several files on startup?

 

Command: (load "mergetext.lsp")
C:MERGETEXT

Command: (and (load "mergetext.lsp"))
T

Command: (and (load "mergetext.lsp" nil))
T

Command: (and (load "mergetext.lsp" ""))
T

Posted
Just out of curiosity, have you tried autoload?

 

Example:

(autoload "test.lsp" '("tt" "test"))

 

This will perform an on demand load of the routine when either tt or test is typed.

 

If this will not suite, then we'll continue with what you are wanting. :)

 

Tnx for your reply but I am home now... need to try tomorrow. I believe there are allways many roads that lead to the destination. And as for autoload: no I did not try that. Am I a dummy? :oops:

Posted
Tnx for your reply but I am home now... need to try tomorrow. I believe there are allways many roads that lead to the destination. And as for autoload: no I did not try that. Am I a dummy? :oops:

Not at all. :)

Posted
You'll get a non-nil return, but I'm not sure why you would want what you are doing. Are you going to do this for several files on startup?

 

Right, I load several different LISP routines by loading them individually in the ACADDOC.lsp. The problem I had was that I fat fingered one of the filenames. So it was looking for MCPOY.lsp, which did not exist. The load failed, (and probably showed the "failed to load") but in the command window, I also got the "MCOPY is loaded, MCOPY to invoke." prompt since the IF still returned non NIL.

 

Glen

Posted

Glen, I would not use an IF statement, the load function compensates for the case in which the function does not load with it's extra argument.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...