error trapping for scripts

error trapping for scripts

roland.r71
Collaborator Collaborator
2,476 Views
15 Replies
Message 1 of 16

error trapping for scripts

roland.r71
Collaborator
Collaborator

I'm looking into a way to trap certain errors, if possible, during a script (*.scr) execution.

 

For example:

Lets say you try opening a file, but it's open by another user and acad starts asking if you wish to open it read-only. However you did not account for the question with your script, so it goes south with "yes or no please" and/or an "unknown command".

 

Leaving my progressbar on screen, sdi on & filedia off. Smiley Frustrated

 

I'm using a function to open the files (which actually checks for the example situation & more, but that aside) which has it's local *error* trap, but that's not invoked.

Nor does a redefine of the regular global *error* trap at the beginning of my script seem to get called. The function is there to prevent this, but during my tests while rewriting my script generator it sometimes fails & i'm stuck with a never completing progressbar (which comes with DOSlib).

 

Is there any (other?) way to catch those errors, so i can restore my settings when the script fails?

0 Likes
2,477 Views
15 Replies
Replies (15)
Message 2 of 16

cadffm
Consultant
Consultant

It is not clear to me for what (why) the mentioned * .scr file is there.

 

Solve everything in Lisp and you have the best way to manage problems.

Your request reads as if you open/save the various files in a batchjob via the * .scr file,
if that's the only reason, why do you use *.scr? Do that in Lisp and check first if you

have write permission to the file.

If you need write permission for the job skip the file and log it,
if no write access is required, open a copy of the file.

 

Now I'm pretty sure I did not understand the request and I'm looking forward to the explanation, thanks in advance.

Sebastian

0 Likes
Message 3 of 16

Moshe-A
Mentor
Mentor

Roland,

 

have you thought about writing these values to registry?

 

these values are stored in:

HKCU\Software\Autodesk\AutoCAD\Rxx.x\ACAD-xxxx:xxx\Profile\<<your current profile>>\SysvarMonitor

 

you will need a small exe program to save them at startup and another to restore them at exist

invoke AutoCAD from batch

 

Moshe

 

 

 

 

 

0 Likes
Message 4 of 16

roland.r71
Collaborator
Collaborator

Yes, the script is used for opening and/or saving drawings and to load and execute lisp files on them, or a series of commands.

 

My generator actually accepts a list of commands to create the script, but by default i'm just adding 1 command to load a lisp file (theoretically that's all you need to do whatever you want)

 

My own 'fileopen' function actually checks if files exist, are read-only or already open (local or on a network). It checks the current file too, in case it changes (but was opened read-only).

 

It has options to warn, skip or open read-only (even if writeable). Users can retry indefinately to open such files (closing it, making it writeable, or ask others to close it, etc.), or automatically skip them if unatended execution is preferred. It can log any skipped file + reason.

 

In the end, pretty much any situation is handled and the script is not likely to fail, so the problem should not occur, ones i finnish rewriting it all. But for the moment i frequently have to restore my settings and turn of the progressbar by hand.

 

The choice for a *.scr file is actually an old one, as my first version is over 10 years old.

Back then i knew of no other way to process multiple drawings in sequence without using a batch (& SDI). I chose to keep it that way. (although i might be able to do it with just lisp)

0 Likes
Message 5 of 16

roland.r71
Collaborator
Collaborator

The values are not the problem.

At the moment the script is generated it checks for the filedia & sdi values and changes them (only) if needed. Their starting values are stored in a .ini file, in case of an error.

 

The error trap within my own open function will retrieve these values and restore them, besides turning off the progressbar.

 

However, there doesn't seem to be any error trap invoked when the (original) fileopen command fails, or one of my preset answers is evaluated as a command.

 

edit:

To give you guys an idea, this is what the generated script looks like (which is actually more lisp then batch):

(setvar "SDI" 1)
(setvar "FILEDIA" 0)
(DOS_GETPROGRESS "scrGen" "Please wait..." 10)
(load "C:/Lisp/scrGen/MODULES/scrGenOpen.lsp")
(c:scrGenOpen "C:/acad_test/dwg/ztm2/TT043-0ANE01-SET.dwg")
(if (= *skipFile* nil)(progn
      (load "C:/Lisp/test/zea.lsp")
))
(DOS_GETPROGRESS -1)
(load "C:/Lisp/scrGen/MODULES/scrGenOpen.lsp")
(c:scrGenOpen "C:/acad_test/dwg/ztm2/TT043-1ARK01-SET.dwg")
(if (= *skipFile* nil)(progn
      (load "C:/Lisp/test/zea.lsp")
))
(DOS_GETPROGRESS -1)
(load "C:/Lisp/scrGen/MODULES/scrGenOpen.lsp")
(c:scrGenOpen "C:/acad_test/dwg/ztm2/TT043-1ARK03-SET.dwg")
(if (= *skipFile* nil)(progn
      (load "C:/Lisp/test/zea.lsp")
))
;
; etc, etc, for as many files as selected
;
(DOS_GETPROGRESS T)
(setq msg "scrGen : Process Complete")
(setq cntr (DOS_GETINI "config" "numSkip" "c:/Lisp/scrGen/scrGen.ini"))
(if (/= cntr "0")
   (progn
      (setq msg (strcat msg "\n" cntr " file(s) skipped"))
      (if (= (DOS_GETINI "config" "logSkip" "c:/Lisp/scrGen/scrGen.ini") "1")
         (progn
            (setq confdia (DOS_MSGBOX (strcat msg "\nWould you like to view the logfile ?") "Done" 2 1))
            (if (/= confdia 1)
               (DOS_SHELLEXE "notepad.exe" (DOS_GETINI "config" "logfile" "c:/Lisp/scrGen/scrGen.ini"))
            )
         )
         (progn
            (DOS_MSGBOX msg "Done" 1 1)
         )
      )
   )
   (progn
      (DOS_MSGBOX msg "Done" 1 1)
   )
)
(setvar "SDI" 0)
(setvar "FILEDIA" 1)
0 Likes
Message 6 of 16

cadffm
Consultant
Consultant

So if the scripts are only needed for immediate implementation and file switching at the moment: Do not invest any more time in the matter.
At the same time you have a simple implementation in Lisp which does nothing else but you have more control.

 

 

I know you can interpret it, so it's extremely "short / not working"

 

  

 

LISPINIT=0
and so on

(if (SetSDI)
    (foreach FILE <filelist>
      (if (and SAVE? (or WR-Lock? DWLock?))
          (ERRLIST FILE)
          (progn
      (if (or WR-Lock? DWLock?)
    (if (zerop (getvar "DBMOD"))(command "_.OPEN" FILE "_Y")(command "_.OPEN" "_Y" FILE "_Y"))
    (if (zerop (getvar "DBMOD"))(command "_.OPEN" FILE)     (command "_.OPEN" "_Y" FILE))
       )
       (if (= 'LIST (type FUNC)) (eval FUNC) (eval(list FUNC)))
       (if SAVE? (command "_.QSAVE"))
    )
      );_if
    );_foreach
    (alert "SDI-Hint")
 )

 

 

 

 

Sebastian

0 Likes
Message 7 of 16

Moshe-A
Mentor
Mentor

this is your scr file - right? (it looks like a lsp)

 

anyway i do not know\see what's in (c:scrGenOpen) but have you

tried to check for the dwg to be opened before (c:scrGenOpen)?

 

 

 

0 Likes
Message 8 of 16

roland.r71
Collaborator
Collaborator

Yes, it is lisp in a *.scr file.

 

The checking is actually done inside the scrGenOpen function.

It checks if the file to open exists, isn't already open, open by other user or read-only.

It also checks if the current file has changed and needs saving or not. (user has choice to not save anything, in case you only need to collect info from the drawings without saving.)

 

In the end it determines what the fileopen command should look like, with a possible _Y or _N at the beginning and/or end of the command.

 

edit:

If for some reason the file needs to be skipped it will add an entry to the log file & set the global *skipFile* to be True, so the following command(s) do not get processed.

0 Likes
Message 9 of 16

roland.r71
Collaborator
Collaborator

I'm giving it some thought.

Although that means i'm gonna have to start over rewriting it all & dumping my generator, as i won't be needing that if i'm going to do it all from a single lisp routine.

 

I was kinda proud of it, as it allows to create virtually any script (even in multiple sessions, by adding a start, mid & end section separately.) with all sorts of nice to have features. Took a while to write too Smiley Sad

 

Still, by the way it works & looks right now, I'm not sure it will prevent errors with the fileopen command from messing it up.

 

edit:

Within the c:scrGenOpen function, at the verry end, it calls the command like this:

(note: this example is 1 of 6 variants)

(command "_fileopen" cmd_0 get_file cmd_1)

Where cmd_0 & cmd_1 can be "_Y" or "_N".

 

The last one (cmd_1) is to answer "open file readonly?"

Lets say it's set to _Y, but the file isn't read-only and acad (fileopen) will not ask.

It then responds with:

Unknown command _Y

Leaving my SDI on 1 & FILEDIA on 0 & my progressbar remains active.

 

Eventhough the c:scrGenOpen function has a local errortrap resetting these things.

Adding a errortrap to the script (storing the original *error* and restoring it at the end) doesn't make a difference either.

0 Likes
Message 10 of 16

roland.r71
Collaborator
Collaborator

I did a little test with a scriptless approach.

But it doesn't change ACAD's behaviour with the fileopen function.

Except that the "_Y Unknown command "Y"" message doesn't break the routine.

 

(defun C:MF (/ *error* filelist file)
   (defun *error* (/)
      (princ "\nInternal error")
   )
   (setq filelist 
      (list 
         "C:/Lisp/TestDWG/TT049-1ARK03-SET.dwg" 
         "C:/Lisp/TestDWG/TT049-1ARK04-SET.dwg" 
         "C:/Lisp/TestDWG/TT049-1ARK05-SET.dwgs" 
         "C:/Lisp/TestDWG/TT049-1ARK06-SET.dwg"
      )
   )
   (setvar "SDI" 1)
   (setvar "LISPINIT" 0)
   (foreach file filelist
      (command "_.fileopen" file "_Y")
      (command "Zoom" "e")
      (command "Qsave")
   )
)

The function above works like a charm, untill i added 2 errors.

 

1: For the 3rd file in the list i added a "s" to the extension, so it's now an invalid filename.

2: I added a "_Y" which will be evaluated as a command.

 

The first will mess up the routine untill the next correct drawingname passes, the second will report an "unknown command" but it continues.

 

I never get to see my "Internal error" message.

 

As it is indeed way easier and offers more control, i'm going to skip the script and do it all from a single lisp routine, which does mean i'll have to start over.

 

Although i can't catch these errors, in the end they should not occur (due to various checks) so i'm not spending anymore time to try and solve it, i'll just have to prevent it from happening.

0 Likes
Message 11 of 16

doglips
Advocate
Advocate

Hey guys, you could try using the resume command in the spot where it goes south.

When a script file loads and runs a lisp file the rules still hold for the script. No interuptions but if you use (command "resume") at the point where it breaks the app should continue.

Hope this helps

0 Likes
Message 12 of 16

Anonymous
Not applicable
@cadffm wrote:

It is not clear to me for what (why) the mentioned * .scr file is there.

 

Solve everything in Lisp and you have the best way to manage problems.

Your request reads as if you open/save the various files in a batchjob via the * .scr file,
if that's the only reason, why do you use *.scr? Do that in Lisp and check first if you

have write permission to the file.

If you need write permission for the job skip the file and log it,
if no write access is required, open a copy of the file.

 

Now I'm pretty sure I did not understand the request and I'm looking forward to the explanation, thanks in advance.



@cadffm wrote:

It is not clear to me for what (why) the mentioned * .scr file is there.

 

Solve everything in Lisp and you have the best way to manage problems.

Your request reads as if you open/save the various files in a batchjob via the * .scr file,
if that's the only reason, why do you use *.scr? Do that in Lisp and check first if you

have write permission to the file.

If you need write permission for the job skip the file and log it,
if no write access is required, open a copy of the file.

 

Now I'm pretty sure I did not understand the request and I'm looking forward to the explanation, thanks in advance.


Lisp's aren't the end all and be all. For a start Lisps and VBA do not run in any version of AutoCAD Light so if you are in a company which uses both then you need scripts to run some of the functionality that companies like to have. It's the one thing that annoys me on these forums when I'm searching for a solution that works in a script, there's always someone whose solution is "Use a Lisp". As a script-monkey Design Drafter I've learnt it's necessary to have answers to the solutions in both ways.

0 Likes
Message 13 of 16

cadffm
Consultant
Consultant
Yes Mark, but thus is not your Thread and the op have fullversions and he is using lisp inside the script.

My question about "why script" was not a general questions, i ask the op espacially for this case.

I don't know about your knowledge in autocad(lt), macro, script, actionrecorder and controling by third part programm or own programs.
But if you have a question, feel free to open a new thread in LT Forum and ask about, post a sample and all important informations.
Your welcome.

Sebastian

0 Likes
Message 14 of 16

Anonymous
Not applicable
Actually attitudes like that are exactly the reason why I don't bother asking questions on the AutoCAD Forums. No doubt you don't think you have an attitude but I think you need to consider how other people will react to what you write.
Message 15 of 16

cadffm
Consultant
Consultant
yes, of course, but i only go to technical questions with my answer and here asked Roland, no one else.

He already uses Lisp (in the script) and the question was justified.
Nothing to do with attitudes.

I have nothing against scripts, but in this case you can not really do much better except do it complete in Lisp.

But you are welcome to participate constructively in the thread!

We all like to learn(and help), that why we are here.

Sebastian

0 Likes
Message 16 of 16

roland.r71
Collaborator
Collaborator

You're aware you're responding to a thread (MY thread) in: "Visual LISP, AutoLISP and General Customization" ???

& you're just spewing rants with an attitude, of which you accuse one of the more helpfull people around here?

 

If you don't like LISP, because you got LT, that's NOT our problem & most definately not mine. (or CADffm's)

 

So please take your ranting elsewhere.

If you realy feal an urge to express your frustration: Blame your boss for being a cheapscate for not buying the real deal, but "LT".

 

Note: In case you continue ranting, this is the only reply you will get from me on this "subject". We are here to discuss solutions to problems, not to create them or focus our frustrations from using limited versions of ACAD on fellow solution providers.