script end event

script end event

Moshe-A
Mentor Mentor
1,001 Views
5 Replies
Message 1 of 6

script end event

Moshe-A
Mentor
Mentor

Hi Guys,

 

have a custom application that generates a script file, it's goal is to open a list of dwg files and runs a lisp command to modify each dwg and save and close and at the end of generating the script i call it to run.

 

my problem is how do i know when the script is done? so i can alert the user about mistakes during the script run

it look like autolisp finishing running all the lisp code before it runs the script

 

i even defined a lisp reactor

 

(vlr-lisp-reactor nil '((:vlr-lispEnded . OnLispEnded)))

 

to trap the end of script base on CMDACTIVE but it also invokes before the script

 

would appriciate any help

Moshe

 

 

0 Likes
1,002 Views
5 Replies
Replies (5)
Message 2 of 6

Moshe-A
Mentor
Mentor

Guys,

 

Found a solution to this and i happy to share it with you, cause i think it is unique. i also found a special behaviuor in running script that i was unaware of and i have been googling for some hours and could not found any reference to this, so if some one has an official reference to this from Autodesk, i would like to know (please send me a link)

 

what i did is creating a text file for Mistakes which occurs during script (instead of sending a message to the screen) but the problem still was how to display this contents file? and i did add a command at the end of the script to open it in notepad but autocad did not launch it.

 

here comes the special behaviour (or weird depends on how you look on it) it turns out that if you use the OPEN command in script (then do some draw/modification work) and CLOSE the file, the next command AutoCAD is expecting is OPEN another file (or finish the script) cause no other command is allowed between the last CLOSE and OPEN. if you do? the script is terminated.

 

so to solved this i add the NEW command to script and opened an empty drawing, called a special command to open Mistakes file using (startapp)

and CLOSE it and I got Mistakes file opened in notpad right in front of user eyes Smiley Very Happy

 

 

Moshe

 

 

 

Message 3 of 6

hmsilva
Mentor
Mentor

Hello Moashe,

thank you for sharing with us your solution!

 

What has occurred to me, if the current dwg remain open after run the script file, was to use 'vla-sendcommand' to call the 'startapp' function after the "_.script" command.

In this case, the 'vla-sendcommand' function would be call when the script ends and the 'focus' would return to the 'original' dwg...

 

As a demo:

 

;; Load Supporting Functions
;; Old Version of 'BrowseForFolder' by: Tony Tanzillo 
(defun BrowseForFolder (Message / sh folder parentfolder folderobject result)
   (vl-load-com)
   (setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))
   (setq folder (vlax-invoke-method sh 'BrowseForFolder 0 Message 0))
   (vlax-release-object sh)
   (if folder
      (progn
         (setq parentfolder (vlax-get-property folder 'ParentFolder))
         (setq FolderObject (vlax-invoke-method ParentFolder 'ParseName (vlax-get-property Folder 'Title)))
         (setq result (vlax-get-property FolderObject 'Path))
         (mapcar 'vlax-release-object
                 (list folder parentfolder folderobject)
         )
         (if (/= (substr result (strlen result)) "\\")
            (setq result (strcat result "\\"))
            result
         )
      )
   )
)


(defun c:demo (/ DirPath DwgFile DwgList Ofile );Scrfile
   (if (setq DirPath (BrowseForFolder "Select directory to scan drawings."))
      (progn
         (setq Scrfile (strcat DirPath "test.scr"))
         (setq ofile (open Scrfile "w"))
         (setq DwgList (vl-directory-files DirPath "*.dwg" 1))
         (foreach Dwg DwgList
            (setq DwgFile (strcat DirPath Dwg))
            (write-line (strcat "_.open\r" (chr 34) DwgFile (chr 34) "\r") Ofile)
            (write-line "_.zoom\r_e\r" Ofile)
            (write-line (strcat "_.saveas\r\r" (chr 34) DwgFile (chr 34) "\rYes\r") ofile)
            (write-line "_.close\r" Ofile)
         )
         (close Ofile)
         (command "_.script" Scrfile)
         (vla-sendcommand (vla-get-activedocument (vlax-get-acad-object)) "(startapp \"notepad\" Scrfile)\r")
      )
   )
   (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 4 of 6

Moshe-A
Mentor
Mentor

Henrique hey,

 

yes you are right, it's working

 

have you got an explantion on how autolisp is doing this?

how can i know what i can or can not do with lisp combining script?

it took me long time to figure out the solution i have presented.

 

thanks,

Moshe

 

0 Likes
Message 5 of 6

Moshe-A
Mentor
Mentor

Henrique,

 

here is an update, it is not quite working

 

if Mistakes file  is not gererated (which is more likely the case) (vla-sendCommand ....) is invoked anyway

even if i check for the existence of the file (findfile) the result is notepad opens and complains, file is not exist?

 

Moshe

 

 

0 Likes
Message 6 of 6

hmsilva
Mentor
Mentor

Moshe,

the autolisp code will be evalueted until the end, not stoping while the script command is running, and the vla-sendcommand function, will send the text string to the current document, but only when it becomes the 'activedocument' (after the script command end) that text string will be evalueted.

 

'if Mistakes file  is not gererated (which is more likely the case) (vla-sendCommand ....) is invoked anyway'

 

Yes, the command was sent when the autolisp code was evalueted.

Using sendcommand, we can call a sub-function to check Mistakes file

i.e.

(defun TheTest nil
   (if (not (findfile MistakesFile))
      (alert "The program have ran without errors... ")
      (startapp "notepad" MistakesFile)
   )
   (princ)
)

and

 

(vla-sendcommand (vla-get-activedocument (vlax-get-acad-object)) "(TheTest)\r")

 

 

Hope this helps,
Henrique

EESignature

0 Likes