Data Extract lisp to XL file

Data Extract lisp to XL file

3arizona
Advocate Advocate
4,238 Views
10 Replies
Message 1 of 11

Data Extract lisp to XL file

3arizona
Advocate
Advocate

can someone help with this lisp. i found this on one of the forums. i have modified for my use. I have 2 problems that i can't resolve. i have tried searching for the original lisp programmers without luck.

 

Here are the problems:

  1. My block is a dynamic block and lisp wont read it. if i remove the dynamic properties from the attribute, it works fine but this is not an option. can this be made to pick up both dynamic and regular attribute blocks? 
  2. There is a column "A" that it's generated when information is transferred to XL (see picture) can this be remove?

LISP.PNG

(defun c:DATAX (/ a1 a2 a3 atts att_lst file i n n1 obj sel_lst ss tmp_lst x)

  ;; http://www.theswamp.org/index.php?action=post;quote=435739;topic=38450.15;last_msg=466989
  ;; by Gilles Chanteau
  ;;-------------------------------------------------------------------------------
  ;; gc:WriteExcel
  ;; Ecrit dans un fichier Excel
  ;;
  ;; Arguments : 4
  ;;   filename   : chemin complet du fichier
  ;;   sheet      : nom de la feuille (ou nil pour la feuille courante)
  ;;   startRange : nom de la cellule de départ (ou nil pour "A1")
  ;;   dataList   : liste de sous-listes contenant les données (une sous liste par rangée)
  ;;-------------------------------------------------------------------------------
  (defun gc:WriteExcel (filename sheet    startRange        dataList /        *error*  xlApp
                        wBook    save     sheets   active   start    row      col      rng
                        n        cell
                       )
    (vl-load-com)

    (defun *error* (msg)
      (and msg
           (/= msg "Fonction annulée")
           (princ (strcat "\nErreur: " msg))
      )
      (and wBook (vlax-invoke-method wBook 'Close :vlax-False))
      (and xlApp (vlax-invoke-method xlApp 'Quit))
      (and reg (vlax-release-object reg))
      (mapcar (function (lambda (obj) (and obj (vlax-release-object obj))))
              (list cell rng wBook xlApp)
      )
      (gc)
    )

    (setq xlapp (vlax-get-or-create-object "Excel.Application"))

    (if (findfile filename)
      (setq wBook (vlax-invoke-method (vlax-get-property xlapp 'WorkBooks) 'Open filename)
            save  T
      )
      (setq wBook (vlax-invoke-method (vlax-get-property xlapp 'WorkBooks) 'Add))
    )

    (if sheet
      (progn
        (setq sheets (vlax-get-property xlApp 'Sheets))
        (vlax-for s sheets
          (if (= (strcase (vlax-get-property s 'Name)) (strcase sheet))
            (progn
              (vlax-invoke-method s 'Activate)
              (setq active T)
            )
          )
        )
        (or active
            (vlax-put-property (vlax-invoke-method sheets 'Add) 'Name sheet)
        )
      )
    )

    (if startRange
      (setq start (gc:ColumnRow startRange)
            col   (car start)
            row   (cadr start)
      )
      (setq col 1
            row 1
      )
    )

    (setq rng (vlax-get-property xlApp 'Cells))
    (vlax-invoke-method rng 'Clear)
    (foreach sub dataList
      (setq n col)
      (foreach data sub
        (setq cell (vlax-variant-value (vlax-get-property rng 'Item row n)))
        (if (= (type data) 'STR)
          (vlax-put-property cell 'NumberFormat "@")
        )
        (vlax-put-property cell 'Value2 data)
        (setq n (1+ n))
      )
      (setq row (1+ row))
    )

    (vlax-invoke-method
      (vlax-get-property
        (vlax-get-property xlApp 'ActiveSheet)
        'Columns
      )
      'AutoFit
    )

    (if save
      (vlax-invoke-method wBook 'Save)
      (if (and
            (< "11.0" (vlax-get-property xlapp "Version"))
            (= (strcase (vl-filename-extension filename) T) ".xlsx")
          )
        (vlax-invoke-method
          wBook 'SaveAs filename 51 "" "" :vlax-false :vlax-false 1 1)
        (vlax-invoke-method
          wBook 'SaveAs filename -4143 "" "" :vlax-false :vlax-false 1 1)
      )
    )

    (*error* nil)
  )
  ;; gc:WriteExcel


  (if (and (setq ss (ssget
                      (list '(0 . "INSERT")
                            '(2 . "TITLE INFO1")
                            '(66 . 1)
                            (cons 410 (getvar 'CTAB))
                      )
                    )
           )
           (setq file (getfiled "Output File" "" "xls;xlsx" 1))
      )
    (progn
      (repeat (setq i (sslength ss))
        (setq obj  (vlax-ename->vla-object (ssname ss (setq i (1- i))))
              atts (vlax-invoke obj 'getattributes)
        )
        (foreach att atts
          (cond ((wcmatch (vla-get-tagstring att) "PIECE_MATERIAL")
                 (if (and (setq n1 (vla-get-textstring att))
                          (>= (strlen n1) 3)
                     )
                   (setq n (substr n1 (- (strlen n1) 2)))
                 )
                )
                ((wcmatch (vla-get-tagstring att) "PIECE_FINISH")
                 (setq a1 (vla-get-textstring att))
                )
                ((wcmatch (vla-get-tagstring att) "PIECE_MARC")
                 (setq a2 (vla-get-textstring att))
                )
                ((wcmatch (vla-get-tagstring att) "PIECE_QTY")
                 (setq a3 (vla-get-textstring att))
                )
                ((wcmatch (vla-get-tagstring att) "PIECE_LENGTH")
                 (setq a4 (vla-get-textstring att))
                )
                ((wcmatch (vla-get-tagstring att) "STRETCH_OUT_TO_1/16")
                 (setq a5 (vla-get-textstring att))
                )
          )
        )
        (if (and n n1 a1 a2 a3 a4 a5)
          (progn
            (setq att_lst (cons (list n n1 a1 a2 a3 a4 a5) att_lst))
            (mapcar '(lambda (x) (set x nil)) '(n n1 a1 a2 a3 a4 a5))
          )
        )
      )
      (setq att_lst (vl-sort att_lst '(lambda (a b) (< (car a) (car b)))))
      (setq i 0)
      (foreach x att_lst
        (setq tmp_lst (cons (subst (itoa (setq i (1+ i))) (car x) x) tmp_lst))
      )
      (setq tmp_lst (reverse tmp_lst))
      (setq sel_lst (cons '("NO." "PIECE MATERIAL" "PIECE FINISH" "PIECE MARK" "PIECE QUANTITY" "PIECE LENGTH" "STRETCH OUT to 1/16") sel_lst))
      (setq sel_lst (append sel_lst tmp_lst))
      (gc:WriteExcel file nil nil sel_lst)
    )
  )
  (princ)
)

 

0 Likes
Accepted solutions (1)
4,239 Views
10 Replies
Replies (10)
Message 2 of 11

devitg
Advisor
Advisor

Please upload both the sample dwg, and xls, where you apply such lisp

 

0 Likes
Message 3 of 11

_gile
Consultant
Consultant
Accepted solution

Hi,

 

I just remove all data related to the first column (n variable and "NO." constant)

I can't try because don't have the "TITLE INFO1" block.

 

(defun c:DATAX (/ a1 a2 a3 atts att_lst file i n n1 obj sel_lst ss tmp_lst x)

  ;; http://www.theswamp.org/index.php?action=post;quote=435739;topic=38450.15;last_msg=466989
  ;; by Gilles Chanteau
  ;;-------------------------------------------------------------------------------
  ;; gc:WriteExcel
  ;; Ecrit dans un fichier Excel
  ;;
  ;; Arguments : 4
  ;;   filename   : chemin complet du fichier
  ;;   sheet      : nom de la feuille (ou nil pour la feuille courante)
  ;;   startRange : nom de la cellule de départ (ou nil pour "A1")
  ;;   dataList   : liste de sous-listes contenant les données (une sous liste par rangée)
  ;;-------------------------------------------------------------------------------
  (defun gc:WriteExcel (filename sheet    startRange        dataList /        *error*  xlApp
                        wBook    save     sheets   active   start    row      col      rng
                        n1 a1 a2 a3 a4        cell
                       )
    (vl-load-com)

    (defun *error* (msg)
      (and msg
           (/= msg "Fonction annulée")
           (princ (strcat "\nErreur: " msg))
      )
      (and wBook (vlax-invoke-method wBook 'Close :vlax-False))
      (and xlApp (vlax-invoke-method xlApp 'Quit))
      (and reg (vlax-release-object reg))
      (mapcar (function (lambda (obj) (and obj (vlax-release-object obj))))
              (list cell rng wBook xlApp)
      )
      (gc)
    )

    (setq xlapp (vlax-get-or-create-object "Excel.Application"))

    (if (findfile filename)
      (setq wBook (vlax-invoke-method (vlax-get-property xlapp 'WorkBooks) 'Open filename)
            save  T
      )
      (setq wBook (vlax-invoke-method (vlax-get-property xlapp 'WorkBooks) 'Add))
    )

    (if sheet
      (progn
        (setq sheets (vlax-get-property xlApp 'Sheets))
        (vlax-for s sheets
          (if (= (strcase (vlax-get-property s 'Name)) (strcase sheet))
            (progn
              (vlax-invoke-method s 'Activate)
              (setq active T)
            )
          )
        )
        (or active
            (vlax-put-property (vlax-invoke-method sheets 'Add) 'Name sheet)
        )
      )
    )

    (if startRange
      (setq start (gc:ColumnRow startRange)
            col   (car start)
            row   (cadr start)
      )
      (setq col 1
            row 1
      )
    )

    (setq rng (vlax-get-property xlApp 'Cells))
    (vlax-invoke-method rng 'Clear)
    (foreach sub dataList
      (setq n col)
      (foreach data sub
        (setq cell (vlax-variant-value (vlax-get-property rng 'Item row n)))
        (if (= (type data) 'STR)
          (vlax-put-property cell 'NumberFormat "@")
        )
        (vlax-put-property cell 'Value2 data)
        (setq n (1+ n))
      )
      (setq row (1+ row))
    )

    (vlax-invoke-method
      (vlax-get-property
        (vlax-get-property xlApp 'ActiveSheet)
        'Columns
      )
      'AutoFit
    )

    (if save
      (vlax-invoke-method wBook 'Save)
      (if (and
            (< "11.0" (vlax-get-property xlapp "Version"))
            (= (strcase (vl-filename-extension filename) T) ".xlsx")
          )
        (vlax-invoke-method
          wBook 'SaveAs filename 51 "" "" :vlax-false :vlax-false 1 1)
        (vlax-invoke-method
          wBook 'SaveAs filename -4143 "" "" :vlax-false :vlax-false 1 1)
      )
    )

    (*error* nil)
  )
  ;; gc:WriteExcel


  (if (and (setq ss (ssget
                      (list '(0 . "INSERT")
                            '(2 . "TITLE INFO1")
                            '(66 . 1)
                            (cons 410 (getvar 'CTAB))
                      )
                    )
           )
           (setq file (getfiled "Output File" "" "xls;xlsx" 1))
      )
    (progn
      (repeat (setq i (sslength ss))
        (setq obj  (vlax-ename->vla-object (ssname ss (setq i (1- i))))
              atts (vlax-invoke obj 'getattributes)
        )
        (foreach att atts
          (cond ((wcmatch (vla-get-tagstring att) "PIECE_MATERIAL")
                 (setq n1 (vla-get-textstring att))
                )
                ((wcmatch (vla-get-tagstring att) "PIECE_FINISH")
                 (setq a1 (vla-get-textstring att))
                )
                ((wcmatch (vla-get-tagstring att) "PIECE_MARC")
                 (setq a2 (vla-get-textstring att))
                )
                ((wcmatch (vla-get-tagstring att) "PIECE_QTY")
                 (setq a3 (vla-get-textstring att))
                )
                ((wcmatch (vla-get-tagstring att) "PIECE_LENGTH")
                 (setq a4 (vla-get-textstring att))
                )
                ((wcmatch (vla-get-tagstring att) "STRETCH_OUT_TO_1/16")
                 (setq a5 (vla-get-textstring att))
                )
          )
        )
        (if (and n1 a1 a2 a3 a4 a5)
          (progn
            (setq att_lst (cons (list n1 a1 a2 a3 a4 a5) att_lst))
            (mapcar '(lambda (x) (set x nil)) '(n1 a1 a2 a3 a4 a5))
          )
        )
      )
      (setq att_lst (vl-sort att_lst '(lambda (a b) (< (car a) (car b)))))
      (setq i 0)
      (foreach x att_lst
        (setq tmp_lst (cons (subst (itoa (setq i (1+ i))) (car x) x) tmp_lst))
      )
      (setq tmp_lst (reverse tmp_lst))
      (setq sel_lst (cons '("PIECE MATERIAL" "PIECE FINISH" "PIECE MARK" "PIECE QUANTITY" "PIECE LENGTH" "STRETCH OUT to 1/16") sel_lst))
      (setq sel_lst (append sel_lst tmp_lst))
      (gc:WriteExcel file nil nil sel_lst)
    )
  )
  (princ)
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 11

3arizona
Advocate
Advocate

Sorry for reopening this thread. But can someone help me change above lisp from exporting to EXCEL to an AutoCAD table?

 

Thanks

0 Likes
Message 5 of 11

devitg
Advisor
Advisor
Please upload both the sample dwg, and xls, where you apply such lisp, and the lsp too
0 Likes
Message 6 of 11

3arizona
Advocate
Advocate

Here you go hope someone can help me. I need the lisp to go from exporting to Excel sheet to paste to ACAD table.   

thanks

 

 

(defun c:TESTD (/ a1 a2 a3 a4 a5 atts att_lst file i n n1 obj sel_lst ss tmp_lst x)

 (defun gc:WriteExcel (filename sheet startRange dataList / *error* xlApp wBook save sheets active start row col rng n cell)
        (vl-load-com)

        (defun *error* (msg)
            (and msg
                 (/= msg "Fonction annulée")
                 (princ (strcat "\nErreur: " msg))
            )
            (and wBook (vlax-invoke-method wBook 'Close :vlax-False))
            (and xlApp (vlax-invoke-method xlApp 'Quit))
            (and reg (vlax-release-object reg))
            (mapcar (function (lambda (obj) (and obj (vlax-release-object obj))))
                    (list cell rng wBook xlApp)
            )
            (gc)
        )

        (setq xlapp (vlax-get-or-create-object "Excel.Application"))

        (if (findfile filename)
            (setq wBook (vlax-invoke-method (vlax-get-property xlapp 'WorkBooks) 'Open filename)
                  save  T
            )
            (setq wBook (vlax-invoke-method (vlax-get-property xlapp 'WorkBooks) 'Add))
        )

        (if sheet
            (progn
                (setq sheets (vlax-get-property xlApp 'Sheets))
                (vlax-for s sheets
                    (if (= (strcase (vlax-get-property s 'Name)) (strcase sheet))
                        (progn
                            (vlax-invoke-method s 'Activate)
                            (setq active T)
                        )
                    )
                )
                (or active
                    (vlax-put-property (vlax-invoke-method sheets 'Add) 'Name sheet)
                )
            )
        )

        (if startRange
            (setq start (gc:ColumnRow startRange)
                  col   (car start)
                  row   (cadr start)
            )
            (setq col 1
                  row 1
            )
        )

        (setq rng (vlax-get-property xlApp 'Cells))
        (vlax-invoke-method rng 'Clear)
        (foreach sub dataList
            (setq n col)
            (foreach data sub
                (setq cell (vlax-variant-value (vlax-get-property rng 'Item row n)))
                (if (= (type data) 'STR)
                    (vlax-put-property cell 'NumberFormat "@")
                )
                (vlax-put-property cell 'Value2 data)
                (setq n (1+ n))
            )
            (setq row (1+ row))
        )

        (vlax-invoke-method
            (vlax-get-property
                (vlax-get-property xlApp 'ActiveSheet)
                'Columns
            )
            'AutoFit
        )

        (if save
            (vlax-invoke-method wBook 'Save)
            (if (and
                    (< "11.0" (vlax-get-property xlapp "Version"))
                    (= (strcase (vl-filename-extension filename) T) ".xlsx")
                )
                (vlax-invoke-method
                    wBook 'SaveAs filename 51 "" "" :vlax-false :vlax-false 1 1)
                (vlax-invoke-method
                    wBook 'SaveAs filename -4143 "" "" :vlax-false :vlax-false 1 1)
            )
        )

        (*error* nil)
    )
    ;; gc:WriteExcel


    (if (and (setq ss (ssget
                          (list '(0 . "INSERT")
                                '(2 . "Glass Cut List,`*U*")
                                '(66 . 1)
                                (cons 410 (getvar 'CTAB))
                          )
                      )
             )
             (setq file (getfiled "Output File" "" "xls;xlsx" 1))
        )
        (progn
            (repeat (setq i (sslength ss))
                (setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
                (cond ((= (vla-get-effectivename obj) "Glass Cut List")
                       (setq atts (vlax-invoke obj 'getattributes))
                       (foreach att atts
                           (cond ((wcmatch (vla-get-tagstring att) "QTY_PER_ELEV2")
                                  (if (and (setq n1 (vla-get-textstring att))
                                           (>= (strlen n1) 3)
                                      )
                                      (setq n (substr n1 (- (strlen n1) 2)))
                                  )
                                 )
                                 ((wcmatch (vla-get-tagstring att) "QTY_ELEV2")
                                  (setq a1 (atof (vla-get-textstring att)))
                                 )
                                 ((wcmatch (vla-get-tagstring att) "LENGTH2")
                                  (setq a2 (atof (vla-get-textstring att)))
                                 )
                                 ((wcmatch (vla-get-tagstring att) "DIE_EXTRUSION2")
                                  (setq a3 (vla-get-textstring att))
                                 )
                                 ((wcmatch (vla-get-tagstring att) "PART_NAME2")
                                  (setq a4 (vla-get-textstring att))
                                 )
                                 ((wcmatch (vla-get-tagstring att) "FINISH_COLOR2")
                                  (setq a5 (vla-get-textstring att))
                                 )
                           )
                       )
                      )
                )
                (if (and n1 a1 a2 a3 a4 a5)
                    (progn
                        (setq att_lst (cons (list n1 a1 a2 a3 a4 a5) att_lst))
                        (mapcar '(lambda (x) (set x nil)) '(n1 a1 a2 a3 a4 a5))
                    )
                )
            )
            (setq att_lst (vl-sort att_lst '(lambda (a b) (< (car a) (car b)))))
            ;|(setq i 0)
            (foreach x att_lst
                (setq tmp_lst (cons (subst (itoa (setq i (1+ i))) (car x) x) tmp_lst))
            ) |;
           (setq tmp_lst (reverse tmp_lst))
            (setq sel_lst (cons '( "QTY per Elev." "Total QTY" "Length" "Die Extrusion" "Part Name" "Color")
                                sel_lst
                          )
            )
            (setq sel_lst (append sel_lst att_lst))
            (gc:WriteExcel file nil nil sel_lst)
        )
    )
    (princ)
)
0 Likes
Message 7 of 11

Sea-Haven
Mentor
Mentor

Here is an example of how to make a table. In other code it sets up row column the same for a table.

 

; example of creating a table using passed variables
; By Alan H July 2017


(defun AH:table_make (numcolumns  / numrows curspc colwidth numcolumns numrows objtable rowheight sp doc)
(vl-load-com)
(setq sp (vlax-3d-point (getpoint "Pick lower left"))); or use getpoint
(setq doc  (vla-get-activedocument (vlax-get-acad-object) ))

(if (= (vla-get-activespace doc) 0)
(setq  curspc (vla-get-paperspace doc))
(setq curspc (vla-get-modelspace doc))
)

(setq numrows 2)
(setq rowheight 2.)
(setq colwidth 15.)
(setq objtable (vla-addtable curspc sp numrows numcolumns rowheight colwidth))
(vla-settext objtable 0 0 "Block Details")
(vla-settext objtable 1 0 "Block namei") 
(vla-settext objtable 1 1 "Detail 1")
(vla-settext objtable 1 2 "Detail 2")
(vla-settext objtable 1 3 "Total")

(vla-setcolumnwidth objtable 0 15) ; 0 is first column
(vla-setcolumnwidth objtable 1 15)
(vla-setcolumnwidth objtable 2 15)
(command "_zoom" "e")
(princ)
)
(AH:table_make)
0 Likes
Message 8 of 11

3arizona
Advocate
Advocate

OK, but how do i use this in the existing lisp?  i wouldn't know how much to remove or add.  Existing lisp has a Excel portion in the begging and at the end.  

 

thanks

0 Likes
Message 9 of 11

devitg
Advisor
Advisor
You can use DATEXTRACTION command. Find attached dwg, and the dataextraction file , use it when the dataextaction ask for a file. Follow the instructions , an the select write to table .
0 Likes
Message 10 of 11

Sea-Haven
Mentor
Mentor

3arizona the code is basically all there but needs a big cut to remove all the excel stuff. Start again is easiest, for me pick blocks and create table lines.

 

I will have a look at your drawing something I have been working on is to do a table of multiple blocks doing counts, the difference is compared to block count, it also breaks down a level further taking into account the attribute values, so rather than door it will make a table with say color then handle type. The table is sized based on the maximum number of attributes.

 

Door Black Gold 23

Door Black Silver 25

Door Gold Gold 10

Door Gold Siver 12l

0 Likes
Message 11 of 11

3arizona
Advocate
Advocate

devitg, Thanks for clarifying your lisp but this still take me through the data extraction steps. I'm sure i can use this for something else but for now i want to see if someone can modify existing lisp. 

 

 

Thanks, again for your time

0 Likes