Jump to content

How to export information block attributes to TXT or CSV?


tuantrinhdp

Recommended Posts

Hello everyone. I need to export information block attributes include: coord X,Y, Tag, Value to TXT or CSV or XLS? 
I know DATAEXTRACTION, but i want to faster when use lisp. 
 1TOFWb6.png

Link to comment
Share on other sites

Here is my set of routines, I may have missed including some helper routines. I use it with a reactor to automatically export my symbol and ight fixture blocks and then I have an excel spreadsheet that reads the data files to create schedules. An example export is attached.

 

(defun c:SAA_ExportKeynoteValues ()
  (SAA_ExportKeynoteValues
    (list
      "#" "DOOR" "DTL" "E" "FLR1" "FUNCTION" "GLAZING" "KEYNOTE" "LOCATION" "MATERIAL"
      "MAT1" "MAT2" "MOD" "MOD1" "MOD2" "MODIFIER" "NOTES" "NUMBER" "SHT" "TYPE"
      "WIN" "WT" "WIDTH" "x" "HEIGHT"
    ) ;_list
  ) ;_SAA
  (princ)
)

(defun SAA_ExportKeynoteValues (tagList / tagList ss i aList csvfilename)
  (if (setq ss (ssget "X" '((0 . "INSERT")
                             (-4 . "<OR")
                                (-4 . "<AND")(2 . "sym*")    (8 . "A-Anno*")    (-4 . "AND>")
                                (-4 . "<AND")(2 . "p-light*")(8 . "A-Clng-Lfix")(-4 . "AND>")
                             (-4 . "OR>")
                             (66 . 1)						 ;66 filters for blocks with attributes (assumed meaning?)
                            ))
                ) ;_ssget
    (progn
      ;; create header (in reverse order of txt export file)
      (setq aList (list "EXPORTDATE" "BLOCKNAME" "HANDLE" ))
      (foreach tag tagList (setq aList (cons tag aList)))
      (setq aList (list (SAA_List2String (reverse aList) "\t")))
      ;; cycle through blocks and create list of attribute values
      (repeat (setq i (sslength ss))
        (setq aList (cons
                      (SAA_List2String 
                        (SAA_getKeynoteAttributeValues
                         (vlax-ename->vla-object (ssname ss (setq i (1- i))))
                         tagList
                        ) ;_SAA
                        "\t"
                      ) ;_SAA_List2String
                      aList
                    ) ;_list
        ) ;_setq
      ) ;_repeat
      (setq aList (reverse aList))
      ;; write list to file
      (SAA_WriteList2File alist (setq csvfilename (strcat (getvar "dwgprefix")(vl-filename-base (getvar "dwgname")) "_queryExport.txt")))
      (princ (strcat "\n" (vl-filename-base (getvar "dwgname")) "_queryExport.txt - values exported"))
      (saa_debugPrinc (strcat " to: " (strcat csvfilename)))
    ) ;_progn
  ) ;_if
  (princ)
)

;;;==============================================================================
;;; List to String  -  by Lee Mac (renamed)
;;; Concatenates each string in a supplied list, separated by a given delimiter
;;; lst - [lst] List of strings to concatenate
;;; del - [str] Delimiter string to separate each item
;;;==============================================================================
(defun SAA_List2String ( lst del )
    (if (cdr lst)
        (strcat (car lst) del (SAA_List2String (cdr lst) del))
        (car lst)
    )
)


;;;------------------------------------------------------------;;
;;; get block attributes based on order of attribute list provided 
;;; blk - [vla] VLA Block Reference Object
;;; attList - list of string names for atributes
;;; return example: ("'10CE39" "sym-keynote02" "11.4" "Qty (2)" "NIL")
;;; Inspired by Lee-Mac routines
;;;------------------------------------------------------------;;
(defun SAA_getKeynoteAttributeValues ( blk attList / varPrefix aList attValue)
  (setq varPrefix "SAA_getKeynoteAttributeValues_")
  ;; cycle through block attributes to save list of values
  (mapcar
    '(lambda ( att )
      (if (member (vla-get-tagstring att) attList)
        ;; save attribute value to temporary variable name
        ;; will be part of a list in order of passed in attribute list
        (set (read (strcat varPrefix (vla-get-tagstring att))) (vla-get-textstring att))
      ) ;_if
    ) ;_lambda
    (vlax-invoke blk 'getattributes) 
  ) ;_mapcar
  ;; create list initial data - uses standard header format of "HANDLE" "BLOCKNAME" "EXPORTDATE" 
  ;; defined in SAA_ExportKeynoteValues lisp
  (setq alist
    (list 
      (strcat "'" (vla-get-Handle blk))				;block handle
      (vla-get-effectivename blk)				;block name
      (menucmd "M=$(edtime, $(getvar, date),MO.dd.YY HH:MM)")	;date & time of export
    ) ;_list
  )
  ;; cycle through passed in list of attribute values
  ;; create a list of block data & attribute values
  ;; matching format of ATTEXT row data
  (foreach att attList
    (setq attValue (read (strcat varPrefix att)))
    (if (eval attValue)
      (setq aList    (append aList (list (eval attValue)))
            attValue nil	;clear variable
      ) ;_setq
      (setq aList (append aList (list "nil"))
      ) ;_setq
    )
  ) ;_foreach
  ;; cycle through and clear passed in list of attribute values
  ;; as the temp variables are saved as global
  (foreach att attList
    (set (read (strcat varPrefix att)) nil)
  ) ;_foreach
  aList

;;;==========================================================
;;; Write List to File
;;; write each list item to separate line
;;;==========================================================
(defun SAA_WriteList2File ( lst fname / out_file )
    (if (setq out_file (open fname "w"))
        (progn
            (foreach row lst (princ (strcat row "\n") out_file))
            (close out_file)
            T
        )
    )
)

 

borica_rcp_queryExport.txt

  • Like 1
Link to comment
Share on other sites

A quick and dirty, if happy post in your other forums where you have asked.

 

(defun c:attex ( / blkname ss blk xy att str fo)
(setq fo (open (setq fname "D:\\acadtemp\\attexport.csv") "w"))
(setq blkname (cdr (assoc 2 (entget (car (entsel "\nPick block for name "))))))
(prompt "\nSelect blocks ")
(setq ss (ssget (list (cons 0 "INSERT")(cons 2 blkname))))
(repeat (setq x (sslength ss))
(setq str "")
(setq blk (ssname ss (setq  x (1- x))))
(setq xy (cdr (assoc 10 (entget blk))))
(setq str (strcat blkname "," (rtos (car xy) 2 3)  "," (rtos (cadr xy) 2 3)))
(foreach att (vlax-invoke (vlax-ename->vla-object blk) 'getattributes)
(setq str (strcat str "," (vla-get-textstring att)))
)
(princ str)
(write-line str fo)
)
(close fo)
(princ)
)
(c:attex)

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...