;; CADLisp Tools - Export Block Attributes to CSV
(vl-load-com)

(defun ct-ac:replace-all (text find replacement / position start)
  (setq start 0)
  (while (setq position (vl-string-search find text start))
    (setq text (strcat (substr text 1 position) replacement
                       (substr text (+ position (strlen find) 1)))
          start (+ position (strlen replacement))))
  text)

(defun ct-ac:csv (value)
  (if (null value) (setq value ""))
  (setq value (ct-ac:replace-all value "\r" " ")
        value (ct-ac:replace-all value "\n" " ")
        value (ct-ac:replace-all value "\"" "\"\""))
  (strcat "\"" value "\""))

(defun ct-ac:invoke-list (obj method / result)
  (setq result (vl-catch-all-apply 'vlax-invoke (list obj method)))
  (if (vl-catch-all-error-p result) nil result))

(defun ct-ac:attributes (obj)
  (append (ct-ac:invoke-list obj 'GetAttributes)
          (ct-ac:invoke-list obj 'GetConstantAttributes)))

(defun ct-ac:property (object getter / result)
  (setq result (vl-catch-all-apply getter (list object)))
  (if (vl-catch-all-error-p result) nil result))

(defun ct-ac:block-name (obj / result)
  (if (vlax-property-available-p obj 'EffectiveName)
    (setq result (ct-ac:property obj 'vla-get-EffectiveName)))
  (if (null result) (setq result (ct-ac:property obj 'vla-get-Name)))
  result)

(defun c:ATTCSV (/ *error* answer att attrs ent file handle i item name obj path
                   rows skipped ss summary tag value)
  (defun *error* (message)
    (if file (progn (close file) (setq file nil)))
    (if (and message (not (wcmatch (strcase message) "*CANCEL*,*QUIT*,*EXIT*")))
      (prompt (strcat "\nATTCSV error; a partial file may remain: " message)))
    (princ))
  (prompt "\nSelect attributed blocks: ")
  (if (setq ss (ssget '((0 . "INSERT"))))
    (if (setq path (getfiled "Save attribute CSV" "block-attributes.csv" "csv" 1))
      (progn
        (if (findfile path)
          (progn
            (initget "Yes No")
            (setq answer (getkword "\nFile exists. Overwrite it? [Yes/No] <No>: "))))
        (if (or (null (findfile path)) (= answer "Yes"))
          (progn
            (setq file (vl-catch-all-apply 'open (list path "w")))
            (if (vl-catch-all-error-p file) (setq file nil))
            (if file
              (progn
                (write-line "Handle,Block,Attributes" file)
                (setq i 0 rows 0 skipped 0)
                (repeat (sslength ss)
                  (setq ent (ssname ss i)
                        obj (and ent (vl-catch-all-apply 'vlax-ename->vla-object (list ent)))
                        summary "")
                  (if (and obj (not (vl-catch-all-error-p obj)))
                    (progn
                      (setq attrs (ct-ac:attributes obj))
                      (foreach item attrs
                        (setq tag (ct-ac:property item 'vla-get-TagString)
                              value (ct-ac:property item 'vla-get-TextString))
                        (if tag
                          (setq summary (strcat summary (if (= summary "") "" "; ")
                                                tag "=" (if value value "")))))
                      (setq handle (ct-ac:property obj 'vla-get-Handle)
                            name (ct-ac:block-name obj))
                      (if (and handle name)
                        (progn
                          (write-line (strcat (ct-ac:csv handle) "," (ct-ac:csv name) ","
                                              (ct-ac:csv summary)) file)
                          (setq rows (1+ rows)))
                        (setq skipped (1+ skipped))))
                    (setq skipped (1+ skipped)))
                  (setq i (1+ i)))
                (close file)
                (setq file nil)
                (prompt (strcat "\nCSV saved: " path " (" (itoa rows)
                                " row(s), " (itoa skipped) " skipped).")))
              (prompt "\nThe file could not be opened for writing.")))
          (prompt "\nExport cancelled; the existing file was not changed.")))
      (prompt "\nExport cancelled."))
    (prompt "\nNo attributed blocks selected."))
  (princ))
