Export Table

Export Table

Anonymous
Not applicable
6,619 Views
23 Replies
Message 1 of 24

Export Table

Anonymous
Not applicable

My original goal was to export a table with the push of a button. I've looked at vba and lisp methods. However, I'm struggling with identifying the table with any method. This is as close as I've made it. I want to name the exported file ( .csv ) as the current layout tab name. I don't want to see the dialog box. When using the lisp below, I have to select the table first which I can live with.

 

The table I export is a material list and I sometimes have as many as five tabs in one file I have to export. I would be fine with switching to each tab in order to run this procedure.  

 

I have limited experience with lisp and no experience with AutoCAD vba but, have used it with excel. Would someone help point me in a better direction. 

 

(defun c:tabx()
	(command "tableexport" "" "test.csv")
	(princ "\nTable Export Complete:")
	(princ)
) ;_end of defun

 

Steve.

Accepted solutions (2)
6,620 Views
23 Replies
Replies (23)
Message 2 of 24

hmsilva
Mentor
Mentor

Hi Steve,

as far as I know, AutoCAD don't have a command line version from the 'tableexport' command, so we can´t bypass the dialog box calling the command from a .lsp file...

 

But if the tables to export, are not 'very complicated' with cells merged... probably we can write some Auto/Visual Lisp code to read each layout table and export to a .cvs file with the layout name.

 

If possible to you, post a sample .dwg (AC2010) with some sample tables.

 

Henrique

EESignature

0 Likes
Message 3 of 24

Anonymous
Not applicable

Here would be an example of the table (material list). I've only placed info in (1) table but it's common for us to use multiple sheets.

 

Steve

Message 4 of 24

hmsilva
Mentor
Mentor

Steve,

the layout have two tables, we have to find a way to choose the highest/larger one, and the table have also some string formatting, we have to deal with that too...

 

Today I have a deadline to meet, so tomorrow, I'll see what I can do.

 

Henrique

EESignature

Message 5 of 24

chriswade
Collaborator
Collaborator

I would suggest looking at this thread on theswamp.org:

http://www.theswamp.org/index.php?topic=44382.msg496519#msg496519

 

Message 6 of 24

hmsilva
Mentor
Mentor

Hi Steve,
attached is a 'quick and dirty' code, to export the larger (with more columns) table in a tab 'tbl-export', and to export the larger (with more columns) table in all layout tabs 'tbls-export'.


The code uses as sub-function, Lee Mac's UnFormat String to remove the MText formatting codes from the text strings.

 

The code was very little tested, so try it, and if you find something that does not work as expected, say something...

 

Hope this helps,
Henrique

EESignature

Message 7 of 24

Anonymous
Not applicable

That's so close! I've attached a screen shot showing the result I get when I export manually which is my goal except with the luxury of automation. The righthand side of the image is the result of the current lisp.

 

Based on your code and the the above links I may need to do some homework.

 

Thank you

Steve

0 Likes
Message 8 of 24

chriswade
Collaborator
Collaborator
Accepted solution

The problem is the seperators are set as ; in the code when they they should be ,

 

Try the attached version instead, it worked well in my test.

Message 9 of 24

hmsilva
Mentor
Mentor

@Anonymous wrote:

That's so close! I've attached a screen shot showing the result I get when I export manually which is my goal except with the luxury of automation. The righthand side of the image is the result of the current lisp.

 

Based on your code and the the above links I may need to do some homework.

 

Thank you

Steve


Steve and @chriswade

I did tested the code, in 'Excel 2007', and the seperators as semicolons ';' were recognized without issues...

Table2Excel.PNG

 

Henrique

EESignature

0 Likes
Message 10 of 24

chriswade
Collaborator
Collaborator

In 2010, the ; isn't recognized as a separator for some reason, only the , is.

0 Likes
Message 11 of 24

Anonymous
Not applicable

If I start saving in newer formats will I have to use a different separator?

 

Steve

0 Likes
Message 12 of 24

hmsilva
Mentor
Mentor

@chriswade wrote:

In 2010, the ; isn't recognized as a separator for some reason, only the , is.


Chris,

 

I don't have 'Excel 2010' in this old laptop, but 'Excel' allows us to choose the separator type, when we import 'text' data into a spreadsheet...

Open Excel

Data tab

From text

select the file

delimited, the fields are delimited by commas or tabs,

next

This separator allows us to choose the separator type, or tabs, semicolon, colon, space or other...

 

Henrique

EESignature

0 Likes
Message 13 of 24

hmsilva
Mentor
Mentor
Accepted solution

@Anonymous wrote:

If I start saving in newer formats will I have to use a different separator?

 

Steve


Steve and @chriswade

 

the quickly written code I've posted, was tested in a laptop with the language set to Português, and the separator is defined at the the Registry / International, so the following revised code, should work as expected, in any language...

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 14 of 24

pradeepgds
Observer
Observer

Hi,

 

this lisp works great. thank to you.

a small query,

do it export the multiple table in same layout ? could it possible any way to implement in lisp?

but i can manage with lisp for merging the tables.

0 Likes
Message 15 of 24

pradeepgds
Observer
Observer

Hi,

i need a small help. i need to export the layout name to excel sheet.

i got the lisp, but its gives the result that, opening the excel instead of that the excel need to be saved on the same directory of cad file like tbls-export.lsp

below the lisp program, can u modify and send if possible plz.

 

(defun bbox:exportlayout (dwgs / *error* bbox:getdate bbox:gettime bbox:writedata
acapp odocuments oshell path filepath dbxdoc file
opendoc olayouts layoutname)
(vl-load-com)

 


(defun *error* (msg)
(if file (close file))
(if oshell (vlax-release-object oshell))
(if dbxdoc (vlax-release-object dbxdoc))
(cond ((not msg))
((member msg '("Function cancelled" "quit / exit abort")))
((princ (strcat "\n** Error: " msg " ** ")))
)
(princ)
)

(defun bbox:getdate (date)
(setq date (mapcar '(lambda (x) (itoa x)) date))
(strcat (nth 0 date) "-" (nth 1 date) "-" (nth 3 date))
)

(defun bbox:gettime (date / hr mn)
(setq hr (nth 4 date))
(setq mn (itoa (nth 5 date)))
(if (= 1 (strlen mn))
(setq mn (strcat "0" mn))
)
(cond ((> 12 hr) (strcat (itoa hr) ":" mn " AM"))
((strcat (itoa (- hr 12)) ":" mn " PM"))
)
)

(defun bbox:writedata (dwg layoutname file / filepath date)
(write-line
(vl-string-right-trim
","
(apply
'strcat
(mapcar
'(lambda (x) (strcat x ","))
(list (setq filepath (strcat dwg))
(if (setq date (vl-file-systime filepath))
(bbox:getdate date)
""
)
(if date
(bbox:gettime date)
""
)
layoutname
)
)
)
)
file
)
)

(if (and (setq acapp (vlax-get-acad-object))
(setq odocuments (vla-get-documents acapp))
(setq oshell (vla-getinterfaceobject acapp "Shell.Application"))
(setq path (car dwgs))
(setq filepath (strcat (vl-filename-directory (vl-filename-mktemp))
"\\Export Layout Report_"
(getvar 'DWGNAME)"_"(menucmd "M=$(edtime,$(getvar,date),DD-MO-YYYY)")
".csv"
)
)
(princ "\nWorking, please wait...")
(princ)
(setq dbxdoc (vla-getinterfaceobject acapp (strcat "ObjectDBX.AxDbDocument." (substr (getvar 'acadver) 1 2))))
)
(progn
(setq file (open filepath "w"))
(write-line "Directory Searched:" file)
(write-line path file)
(write-line "" file)
(write-line "Drawing Name: , ******** , ******** ,Layout Name:" file)
(foreach dwg dwgs ; edited here (cdr dwgs)
(if (not (vl-catch-all-error-p
(setq opendoc (vl-catch-all-apply
'vla-item
(list odocuments dwg)
)
)
)
)
(setq olayouts (vla-get-layouts opendoc))
(progn
(vl-catch-all-apply
'vla-open
(list dbxdoc dwg) ;edited here (strcat path dwg)
)
(setq olayouts (vla-get-layouts dbxdoc))
)
)
(vlax-for olayout olayouts
(if (/= "Model" (setq layoutname (vla-get-name olayout)))
(bbox:writedata (getvar 'DWGNAME) layoutname file) ; Edited (bbox:writedata path dwg layoutname file
)
)
)
(princ "Done.")
(princ)
;(setq file (close file)) ;Abra-CAD-Abra
(vlax-invoke oshell 'open filepath)
(*error* nil)
)
(cond
(filepath (*error* "Unable to create \"ObjectDBX.AxDbDocument\" Object"))
(acapp (*error* "Unable to create \"Shell.Application\" Object"))
)
)
)

(defun c:el (/ opt dwgs)

(defun str2lst (str sep / len lst)
(setq len (strlen sep))
(while (setq pos (vl-string-search sep str))
(setq lst (cons (substr str 1 pos) lst)
str (substr str (+ len pos 1))
)
)
(reverse (cons (substr str 1 pos) lst))
)

(initget "Enter")
(if (not (setq opt (getkword "\nPress Enter if you would like to process Active Drawing: <Enter> ")))
(setq opt "Enter")
)
(cond
((= "Enter" opt) (if (= 1 (getvar 'dwgtitled))
(bbox:exportlayout (list (getvar 'dwgprefix) (getvar 'dwgname)))
(prompt "\n** Drawing not saved ** ")
))


)

(princ)
)
(c:el)

0 Likes
Message 16 of 24

aridzv
Enthusiast
Enthusiast

Hi.

I used tbl-export.lsp and it is working grate.

I need to set the second column width to 20 and all the rows hight to 10.

I tried to set it in the "hms:export_table_" function using 

(vla-setcolumnwidth tabl 2 20)
(vla-SetRowHeight tabl row 10)

 but it didn't work.

how can I set the column and rows size?

 

thanks,

aridzv.

0 Likes
Message 17 of 24

Moshe-A
Mentor
Mentor

@aridzv hi,

 

i think you are missing some here, tbls-export auto select all tables and export them to csv file.

i recommend open a new thread and and post a sample dwg. 

 

Moshe

 

Message 18 of 24

Sea-Haven
Mentor
Mentor

This is export to excel a table maybe start again. You would have to add a loop for multiple tables, if located in layouts or Model. Load multi toggles 1st.

 

0 Likes
Message 19 of 24

aridzv
Enthusiast
Enthusiast

@Sea-Haven 

Hi.

following this topic I managet to get "table to excel.lsp" to work:

 

;;;;;;;;;;;;;;;;;;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-to-export-table-from-autocad-to-excel/td-p/10930210;;;;;;;;;;;;;;;
; simple table to excel
; expects Title header and data
; BY Alanh Jan 2022
; do not have excel open

(defun c:tab2excel_v1 ( /  x y z AH:putcell Ah:opennew number2alpha obj cols row)

(defun AH:putcell (cellname val1 / )
(setq myRange (vlax-get-property  (vlax-get-property myxl "ActiveSheet") "Range" cellname))
(vlax-put-property myRange 'Value2 val1)
)

(defun AH:opennew ( /  )
(if (= (setq myxl (vlax-get-object "Excel.Application") ) nil)
(setq myxl (vlax-get-or-create-object "excel.Application"))
)
(vla-put-visible myXL :vlax-true)
(vlax-put-property myxl 'ScreenUpdating :vlax-true)
(vlax-put-property myXL 'DisplayAlerts :vlax-true)
)

; Number2Alpha - Converts Number into Alpha string
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Num# = Number to convert
; Syntax example: (Number2Alpha 731) = "ABC"
;-------------------------------------------------------------------------------
(defun Number2Alpha (Num# / Val#)
  (if (< Num# 27)
    (chr (+ 64 Num#))
    (if (= 0 (setq Val# (rem Num# 26)))
      (strcat (Number2Alpha (1- (/ Num# 26))) "Z")
      (strcat (Number2Alpha (/ Num# 26)) (chr (+ 64 Val#)))
    )
  )
);defun Number2Alpha

(AH:opennew)
(vlax-invoke-method (vlax-get-property myXL 'WorkBooks) 'Add)

(setq obj (vlax-ename->vla-object (car  (entsel "Pick table "))))
(setq cols (vla-get-columns obj))
(setq rows (vla-get-rows obj))
(alert (strcat (rtos rows 2 0) " rows " (rtos cols 2 0) " columns \n will now send to excel "))

(AH:putcell "A1" (vla-getText Obj 0 0 ))

(setq x 1 y 1 z 1)
;(repeat cols
;(AH:putcell (strcat (Number2Alpha x) (rtos z 2 0)) (vla-getText Obj y (- x 1)  ))
;(setq x (1+ x))
;)

(alert (itoa rows))
(setq y 1)
(repeat rows ;(- rows 2)
  (setq x 1 )
  (repeat cols
    (AH:putcell (strcat (Number2Alpha x) (rtos y 2 0)) (vla-getText Obj (- y 1) (- x 1)  ))
    ;(alert (vla-getText Obj y (- x 1)  ))
    (setq x (1+ x))
    (princ (strcat "\nRow " (rtos y 2 0)))
  )
  (setq y (1+ y))
)

(vlax-release-object myXL)
(alert "Please save the excel and close if you want to import another table ")

(princ)
)

 

now,

my table contains "image in cell" (see attached dwg).

with the current code the images are not exported,only text.

is there a way to export the images as well?

 

thanks,

aridzv.

0 Likes
Message 20 of 24

Sea-Haven
Mentor
Mentor

You have a very extensive answer here "export table with blocks to excel with Python - .NET, ObjectARX & VBA - AutoCAD Forums" why post again ?

0 Likes