Message 1 of 8
Help fixing my LISP - Inserting .dwg from directory as a block
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I am trying to fix this LISP routine which pulls a list of .dwg files from a directory and displays them in a dropdown menu organized by subfolder. Currently, using the insert detail button returns a stringp nil error.
(defun c:DetailsBrowser (/ dcl_id dialog_return folderPath folderList fileDict doc selectedFolder selectedFile fullFilePath)
;; Initialize variables
(setq folderPath "I:\\CADLIB\\Standard Details")
(setq fileDict '())
;; Function to populate the folder list and file dictionary
(defun populate-folder-and-file-list ()
(setq folderList (vl-directory-files folderPath nil -1))
(foreach folder folderList
(if (and (not (wcmatch folder "."))
(not (wcmatch folder ".."))
(eq (vl-file-directory-p (strcat folderPath "\\" folder)) T))
(progn
(setq fileList (vl-sort (vl-directory-files (strcat folderPath "\\" folder) "*.dwg" 1) '<))
(setq fileDict (cons (cons folder fileList) fileDict))
)
)
)
)
;; Function to populate the folder dropdown list
(defun populate-folder-dropdown ()
(start_list "folder_dropdown")
(foreach entry fileDict
(add_list (car entry))
)
(end_list)
)
;; Function to populate the file list for a folder
(defun populate-file-list (folder)
(setq fileList (cdr (assoc folder fileDict)))
(start_list "file_list")
(foreach file fileList
(add_list file)
)
(end_list)
)
;; Load the DCL file
(setq dcl_file "details_browser.dcl")
(setq dcl_path (findfile dcl_file))
(if (not dcl_path)
(progn
(princ (strcat "\nDCL file not found: " dcl_file))
(exit)
)
)
(setq dcl_id (load_dialog dcl_path))
(if (not (new_dialog "details_browser" dcl_id))
(progn
(princ "\nFailed to load dialog.")
(exit)
)
)
;; Get the document object
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
;; Populate the initial folder and file lists
(populate-folder-and-file-list)
(populate-folder-dropdown)
;; Set up the action for the folder dropdown selection
(action_tile "folder_dropdown"
"(progn
(setq selectedFolder (nth (atoi (get_tile \"folder_dropdown\")) (mapcar 'car fileDict)))
(populate-file-list selectedFolder)
(princ (strcat \"\\nSelected folder: \" selectedFolder))
)")
;; Set up the action for the "Insert Detail" button
(action_tile "insert_detail" "(done_dialog 1)")
;; Set up the action for the "Cancel" button
(action_tile "cancel" "(done_dialog 0)")
;; Display the dialog and get the user's input
(setq dialog_return (start_dialog))
;; Handle the user's input
(cond
((= dialog_return 1)
;; Insert the selected detail
(setq selectedFile (nth (atoi (get_tile "file_list")) fileList))
(princ (strcat "\nSelected file: " (get_tile "file_list") "\n"))
(princ (strcat "\nSelected file (variable): " selectedFile "\n"))
(if selectedFile
(progn
(setq fullFilePath (strcat folderPath "\\" selectedFolder "\\" selectedFile))
(princ (strcat "\nFull file path: " fullFilePath "\n"))
(if (findfile fullFilePath)
(insert-block fullFilePath)
(princ (strcat "\nFile not found: " fullFilePath "\n")))
(princ (strcat "\nInserted detail: " fullFilePath "\n"))
)
)
)
(t (princ "\nDialog canceled."))
)
;; Cleanup
(unload_dialog dcl_id)
(princ)
)
(defun insert-block (fullFilePath)
(vl-load-com)
(setq acadObj (vlax-get-acad-object))
(setq doc (vla-get-ActiveDocument acadObj))
(setq db (vla-get-Database doc))
(setq blockName (vl-filename-base fullFilePath))
;; Create a temporary database
(setq tempDb (vlax-create-object "AutoCAD.AcadDatabase"))
(vla-ReadDwgFile tempDb fullFilePath)
;; Insert the block into the current database
(vla-Insert db blockName tempDb :vlax-false)
;; Clean up
(vlax-release-object tempDb)
(princ "\nBlock inserted successfully.\n")
)
(princ "\nDetailsBrowser loaded. Type DetailsBrowser to start.")
(princ)
Let me know if you have any suggestions!