- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
i have over 50 000 dwg's that will be migrated to Autodesk Vault. In order to be able to extract attributes from these dwg's and into Vault's UPD's i need to map properties.
Before i can do it i need to list up all title blocks that have been used with these 50 000 drawings (there have been used approx 60 different templates with own title block names).
What i try to do now is to create AutoLisp that will open every single drawing, read all blocks, save results into a TXT file, close current file and open the next one.
I have managed to get a code doing all of this except closing file and opening a new one. Autocad crashes after collected data is saved into a TXT file.
I would appreciate help i finding error that causes Autocad crash.
I use Autocad 2023.
Thanks!
Here the code:
(defun c:ExportBlockNamesToTextFile ()
(vl-load-com)
;; Prompt for folder path
(princ "\nEnter the folder path containing DWG files (e.g., C:\\MyDWGs): ")
(setq folder (getstring))
;; Check if folder exists
(if (and folder (vl-file-directory-p folder))
(progn
;; Get DWG files in the folder
(setq dwg-files (vl-directory-files folder "*.dwg" 1))
(if dwg-files
(progn
;; Create output and error log files
(setq output-file (strcat folder "\\BlockNamesReport.txt"))
(setq error-log (strcat folder "\\ErrorLog.txt"))
(setq file-handle (open output-file "w"))
(setq log-handle (open error-log "w"))
;; Process each DWG file
(foreach dwg dwg-files
(setq full-path (strcat folder "\\" dwg))
(princ (strcat "\nProcessing: " full-path))
(setq doc nil) ;; Initialize document variable
;; Use error-handling wrapper
(setq result
(vl-catch-all-apply
(function
(lambda ()
;; Open and activate the document
(setq doc (vla-open (vla-get-documents (vlax-get-acad-object)) full-path))
(vla-activate doc)
;; Get Model Space and block references
(setq ms (vla-get-modelspace doc))
(setq blk-names "")
(vlax-for obj ms
(if (= (vla-get-objectname obj) "AcDbBlockReference")
(setq blk-names (strcat blk-names (vla-get-effectivename obj) "\n"))
)
)
;; Write results to the output file
(if (> (strlen blk-names) 0)
(progn
(write-line (strcat "DWG File: " dwg) file-handle)
(write-line "Block Names:" file-handle)
(write-line blk-names file-handle)
(write-line "----------------------------" file-handle)
)
(progn
(write-line (strcat "DWG File: " dwg) file-handle)
(write-line "No blocks found." file-handle)
(write-line "----------------------------" file-handle)
)
)
;; Close and release resources
(if doc
(progn
(vla-save doc)
(vla-close doc)
(vlax-release-object doc)
(setq doc nil)
)
)
)
)
)
)
;; Handle errors
(if (vl-catch-all-error-p result)
(progn
(princ (strcat "\nError processing: " full-path " - " (vl-catch-all-error-message result)))
(write-line (strcat "Error processing: " full-path " - " (vl-catch-all-error-message result)) log-handle)
;; Attempt to close the document if open
(if (and doc (not (vlax-erased-p doc)))
(progn
(vla-close doc)
(vlax-release-object doc)
)
)
)
)
;; Force garbage collection
(gc)
)
;; Close the output and error log files
(close file-handle)
(close log-handle)
(princ (strcat "\nBlock names have been exported to: " output-file))
(princ (strcat "\nErrors logged to: " error-log))
)
(princ "\nNo DWG files found in the specified folder.")
)
)
(princ "\nInvalid folder path.")
)
(princ)
)
Solved! Go to Solution.