Exporting custom DWGProps to csv upon save

I'm having trouble exporting custom dwgprop data into a .csv file upon saving the CAD file. The logic I'm trying to implement is below:

  • CSV File named INTAKE in same parent folder as .dwg file (needs to be a relative path, as this is for a template that will get copied into other folder paths)
  • The CSV has 2 columns - one with the property name and one with the value
  • DWGProps have been customized to match CSV property names
  • Upon save (qsave, save, saveas) I would like all custom dwgprops to export back into the INTAKE.csv file, there will be existing data so it will need to be overwritten.

The last code version I've attempted is below, but still getting error messages without a successful export to csv. Could anyone provide any assistance or advice here? Thank you!

(defun ExportDWGPropsToCSV ( / csvFile fileHandle index name value props doc)
;; Get active document
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))

;; Get custom properties collection
(setq props (vla-get-UserDefinedProperties doc))

;; Define output file path
(setq csvFile (strcat (getvar "DWGPREFIX") "INTAKE.csv"))

;; Open file for writing
(setq fileHandle (open csvFile "w"))
(if fileHandle
(progn
;; Write header
(write-line "Field,Value" fileHandle)

  ;; Loop through custom properties
(setq index 0)
(while (< index (vla-get-count props))
(setq name (vla-get-name props index))
(setq value (vla-get-value props index))
(write-line (strcat name "," value) fileHandle)
(setq index (1+ index))
)

;; Close file
(close fileHandle)
(princ (strcat "\nCustom DWGPROPS exported to: " csvFile))
)
(princ "\nFailed to create CSV file.")

)
(princ)
)

(defun SaveReactorCallback (reactor commandList / commandName)
;; Safely extract command name
(if (and (listp commandList) (setq commandName (car commandList)))
(cond
((= commandName "QSAVE") (ExportDWGPropsToCSV))
((= commandName "SAVE") (ExportDWGPropsToCSV))
((= commandName "SAVEAS") (ExportDWGPropsToCSV))
)
)
)

;; Register the reactor
(vlr-command-reactor nil '((:vlr-commandEnded . SaveReactorCallback)))
(princ "\nDWGPROPS export on save is now active.")
(princ)

Comments

  • maybe these functions will help

    vla-NumCustomInfo, vla-GetCustomByKey, vla-GetCustomByIndex

    I have a cool solution in Python if can run this https://github.com/CEXT-Dan/PyRx

  • I used these reactor names they trap save and close.

    (cond
    ((= (vlr-current-reaction-name) ':VLR-beginSave) (Princ "\nThis function has been triggered by a Document Save event."))
    ((= (vlr-current-reaction-name) ':VLR-beginClose)(princ "\nThis function has been triggered by a Document Close event."))
    )
    (princ)
    )
    (if (not _BeginCloseReactor) (setq _BeginCloseReactor (VLR-Dwg-Reactor nil '((:VLR-beginClose . BeginCloseFunc)))))
    (if (not _BeginSaveReactor ) (setq _BeginSaveReactor (VLR-Dwg-Reactor nil '((:VLR-beginSave . BeginCloseFunc)))))

    The called function is (defun BeginCloseFunc (reactor lst / …….)