Help with FILEOPEN Command in AutoLISP

Help with FILEOPEN Command in AutoLISP

scottividal
Contributor Contributor
1,175 Views
10 Replies
Message 1 of 11

Help with FILEOPEN Command in AutoLISP

scottividal
Contributor
Contributor

Hi everyone!

I'm working on an AutoLISP script to process multiple DWG files in AutoCAD. My goal is to open each file, perform certain operations with the SVAM-Todos command, and then return to the original file. I'm using the FILEOPEN command to activate each file, but I'm facing an issue: after executing FILEOPEN, I can't get back to the original drawing.

Here's my current code:

 

(defun c:SVAM-APP ()
(setq FILEPATH (getvar "dwgprefix"))
(setq FILES (vl-directory-files FILEPATH "*.dwg" 1))
(setq acadObject (vlax-get-acad-object))
(setq currentDoc (vla-get-ActiveDocument acadObject))

(foreach FILE FILES
(progn
(setq FILE_I_FILEPATH (strcat FILEPATH FILE))
(if (not (equal (strcase FILE) "SVAM.dwg"))
(progn
(command "FILEOPEN" FILE_I_FILEPATH)
(c:SVAM-Todos)
(vla-put-ActiveDocument acadObject currentDoc)
)
)
)
)
)

After running this code, I can't get back to the original file. Does anyone have any suggestions on how I can fix this or an alternative to the FILEOPEN command?

I appreciate any help or suggestions you can provide!

Thanks in advance.

ASV
0 Likes
1,176 Views
10 Replies
Replies (10)
Message 2 of 11

Sea-Haven
Mentor
Mentor

Dont know if this will work, but you can set the ITEM num of the dwg from within (vla-get-ActiveDocument acadObject)) you need though to be aware that the number may change as you add or close dwg's. Now where is that code. After adding a dwg you need to redo the activedocument as it now has more items.

 

I think I tried doing something similar and the rem line did not work.

 

(defun openblk (blkname / adocs)
(setq acDocs (vla-get-documents (vlax-get-acad-object)))
(vla-open acDocs blkname)
(vla-activate (vla-item acdocs 1))
(setq acDocs (vla-get-documents (vlax-get-acad-object)))
;(vla-put-activedocument (vlax-get-acad-object) (vla-item acdocs 1))
)
0 Likes
Message 3 of 11

hmsilva
Mentor
Mentor

@scottividal 

Autolisp is not really the right tool for your needs.

It is a single document application. Activating another document stops and autolsip program until the host document is activated again. 
Using a script to cycle through the documents you need to run the (c:SVAM-Todos) and in the end return to the host document, It will probably be the best solution.


This post might give you some ideas

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 4 of 11

scottividal
Contributor
Contributor

Thank you for the suggestion, I've been researching scripts (with a bit of help from ChatGPT). And I understand a bit more, but I still can't solve my problem.

I understand that I should create a script that opens all the files in the folder and runs my Lisp routine on them. However, it's not clear to me if there's a way to automate the script to know the path of the folder. Also, I don't understand if I can include something like this code in the script.

; Script

 (setq files (vl-directory-files "." "*.dwg" 1))

(foreach file files 
(if (/= file "EjecutarVigas.scr")
(progn
(command "_OPEN" (strcat (getvar "dwgprefix") file))
(command "_beams")
(command "_SAVE" "_YES")
(command "_CLOSE" "_YES") ) ) ) 

ASV
0 Likes
Message 5 of 11

Sea-Haven
Mentor
Mentor

A script would look something like this:

 

open dwg1
(load "yourlisp")
close Y
open dwg2
(load "yourlisp")
close Y
open dwg3
(load "yourlisp")
close Y
0 Likes
Message 6 of 11

hmsilva
Mentor
Mentor

@scottividal wrote:

Thank you for the suggestion, I've been researching scripts (with a bit of help from ChatGPT). And I understand a bit more, but I still can't solve my problem.

I understand that I should create a script that opens all the files in the folder and runs my Lisp routine on them. However, it's not clear to me if there's a way to automate the script to know the path of the folder. Also, I don't understand if I can include something like this code in the script.

; Script

 (setq files (vl-directory-files "." "*.dwg" 1))

(foreach file files 
(if (/= file "EjecutarVigas.scr")
(progn
(command "_OPEN" (strcat (getvar "dwgprefix") file))
(command "_beams")
(command "_SAVE" "_YES")
(command "_CLOSE" "_YES") ) ) ) 


@scottividal change "X:/Your/File/Path/FileName.lsp" to the correct file path and name an try it.

 

;; 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 process all drawings."))
	(progn
	    (setq Scrfile (vl-filename-mktemp "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 (strcat "(load " (chr 34) "X:/Your/File/Path/FileName.lsp" (chr 34) ")\r") Ofile)
		(write-line "(c:SVAM-Todos)\r" Ofile)
		(write-line "_.qsave\r" Ofile)
		(write-line "_.close\r" Ofile)
	    )
	    (close Ofile)
	    (command "_.script" Scrfile)
	)
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 7 of 11

scottividal
Contributor
Contributor

 

Hello,

I hope this message finds you well. I am currently facing an issue with an AutoLISP script designed to process multiple DWG files consecutively. The script opens each drawing file, executes the SVAM-Todos command, adds a delay, and then moves on to the next file.

Here is the original script:

 

(defun c:demo (/ DirPath DwgFile DwgList Ofile Scrfile) (if (setq DirPath (BrowseForFolder "Select directory to process all drawings.")) (progn (setq Scrfile (vl-filename-mktemp "test.scr")) (setq Ofile (open Scrfile "w")) (setq DwgList (vl-directory-files DirPath "*.dwg" 1)) ;; Iterate over each drawing file (foreach Dwg DwgList (setq DwgFile (strcat DirPath Dwg)) ;; Write AutoCAD commands to the script file (write-line (strcat "_.open\r" (chr 34) DwgFile (chr 34) "\r") Ofile) (write-line "(c:SVAM-Todos)\r" Ofile) (write-line "(vl-cmdf 500) ; Add a delay of 500 milliseconds\r" Ofile) ;; Close the script file and execute it (close Ofile) (princ (strcat "Processing: " DwgFile "\n")) ;; Execute the script (if (= 0 (command "_.script" Scrfile)) (princ "Script executed successfully.\n") (princ "Error executing the script.\n") ) ) ;; Close the script file after processing all drawings (close Ofile) ) ) (princ) )

 

Despite several attempts, the script doesn't open the second drawing after processing the first one. I have already tried a few modifications, such as ensuring a new script file is created for each drawing, but the issue persists.

Has anyone faced a similar problem or could provide insights into what might be causing this behavior? Your assistance would be greatly appreciated.

Thank you in advance!

Best regards, 

ASV
0 Likes
Message 8 of 11

paullimapa
Mentor
Mentor

share SVAM-Todos.lsp


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 9 of 11

hmsilva
Mentor
Mentor

@scottividal try this one

 

 

(defun c:demo (/ DirPath DwgFile DwgList Ofile Scrfile)
    (if	(setq DirPath (BrowseForFolder "Select directory to process all drawings."))
	(progn (setq Scrfile (vl-filename-mktemp "test.scr"))
	       (setq Ofile (open Scrfile "w"))
	       (setq DwgList (vl-directory-files DirPath "*.dwg" 1))
	       ;; Iterate over each drawing file
	       (foreach	Dwg DwgList
		   (setq DwgFile (strcat DirPath Dwg))
		   ;; Write AutoCAD commands to the script file
		   (write-line (strcat "_.open\r" (chr 34) DwgFile (chr 34) "\r") Ofile)
		   (write-line "(c:SVAM-Todos)\r" Ofile)
		   (write-line "_.deley\r500\r" Ofile) ; Add a delay of 500 milliseconds\r" 
	       )
	       ;; Close the script file and execute it
	       (close Ofile)
	       (princ (strcat "Processing: " DwgFile "\n"))
	       ;; Execute the script
	       (command "_.script" Scrfile)
	       ;|(if (= 0 (command "_.script" Scrfile)) 
		       (princ "Script executed successfully.\n")
		       (princ "Error executing the script.\n")
		   )|;
	       ;; Close the script file after processing all drawings
;;;(close Ofile)
	)
    )
    (princ)
)

 

 

(= 0 (command "_.script" Scrfile) a command call will always return nil, this test will always be false...
The script file will stop because (vl-cmdf 500) will send to the command line only 500 and the result will be *Cancel* and stop the script...
To add a deley we need to use the command deley.

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 10 of 11

scottividal
Contributor
Contributor

I suspect that the issue might be related to the fact that the SVAM-Todos command at the end performs a save-as operation, potentially placing the modified file in a subdirectory. Could this be causing difficulties when trying to open the next drawing?

ASV
0 Likes
Message 11 of 11

paullimapa
Mentor
Mentor

Temporarily remove the Saveas and just do a Qsave see what happens. But do this on test dwgs only


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes