Jump to content

Recommended Posts

Posted

Best way is to convert those texts, numbers, etc to attribute then you can use data extraction to export the data to excel.

Posted (edited)

I asked this question a few days ago. I was creating a custom csv (comma-separated values) to use on Excel. 

Most of the time you can just use ATTOUT in the command line and select your blocks - which only works for blocks with attributes. 

What I had solved was to also include dynamic properties of custom blocks or anonymous blocks. I didn't include the handle and the anonymous block name but with a few tweaking it could act exactly like ATTOUT but extended to dynamic properties and any other entity properties you need from (entget). 

 

What I would do in your case is to create a function that create a selection boundary within each shape and then add each text to a list and then do a lot of lisp manipulation. What I expect is to compare (cdr (assoc 10 ent)) for each entity seen and then group close blocks together but this doesn't tell you what data they are to include in your table. This would be easier if each of those plots were blocks with attributes like nod684 mentioned which would make ATTOUT the tool of choice.

Edited by j2lstaples
Posted (edited)

The way to go is use search for plines and get all text within, sort on Y value of text, then export to csv or excel direct.

 

This question has been asked for many times before. Do a google "get text inside pline Autocad lisp"

 

Hint (setq ss (ssget "WP" lstpts ))

Hint2 is can get centroid of a pline.

Edited by BIGAL
  • 3 years later...
Posted (edited)
;;-------------------------------------------------------------------------
;; TBL2XL.lsp | Version 1.0 
;; Date: 2026-07-20
;; NEW TOOL - Pick one AutoCAD TABLE. Copies its full grid (all rows and
;; columns, same layout) straight into Excel - uses your open Excel if
;; there is one, otherwise opens a fresh one, starting from wherever your
;; cursor is in the sheet.
;; Changelog:
;;   v1.0 - initial release
;;-------------------------------------------------------------------------

(vl-load-com)

;; Returns the VLA Active Document Object
(defun t2x_acdoc nil
    (eval (list 'defun 't2x_acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
    (t2x_acdoc)
)

;; Opens an Undo Group.
(defun t2x_startundo ( doc )
    (t2x_endundo doc)
    (vla-startundomark doc)
)

;; Closes an Undo Group.
(defun t2x_endundo ( doc / tries )
    (setq tries 0)
    (while (and (= 8 (logand 8 (getvar 'undoctl))) (< tries 5))
        (vl-catch-all-apply 'vla-endundomark (list doc))
        (setq tries (1+ tries))
    )
)

;; Write a value into a cell (row, col) via the Cells collection's Item property
(defun t2x_write_cell ( xlCells row col val )
    (vlax-put-property xlCells "Item" row col val)
)

;; Main command
(defun c:TBL2XL ( / *error* xlApp xlRun xlBooks xlBook xlSheets xlSheet xlCells xlCell
                     es ent tbl rows cols r c cellval baseRow baseCol wr skipped )

    (defun *error* ( msg )
        (if xlBooks (vl-catch-all-apply 'vlax-release-object (list xlBooks)))
        (if xlApp
            (vl-catch-all-apply 'vla-put-visible (list xlApp :vlax-true)) ;; leave visible so Ajmal keeps the data
        )
        (if xlApp (vl-catch-all-apply 'vlax-release-object (list xlApp)))
        (t2x_endundo (t2x_acdoc))
        (if (and msg (not (wcmatch (strcase msg t) "*break*,*cancel*,*exit*")))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )

    (t2x_startundo (t2x_acdoc))

    (setq es (entsel "\nSelect the table: "))

    (if (not es)
        (princ "\nNothing selected.")
        (progn
            (setq ent (car es))
            (if (/= "ACAD_TABLE" (cdr (assoc 0 (entget ent))))
                (princ "\nThat's not a table.")
                (progn
                    (setq tbl (vlax-ename->vla-object ent))
                    (setq rows (vla-get-Rows tbl)
                          cols (vla-get-Columns tbl)
                    )

                    ;; use  Excel if one is already open, otherwise open a fresh one
                    (setq xlApp (vlax-get-or-create-object "Excel.Application")
                          xlRun (vlax-get-property xlApp 'Visible)
                    )
                    (if (= xlRun :vlax-false)
                        (progn
                            (setq xlBooks  (vlax-get-property xlApp "Workbooks")
                                  xlBook   (vlax-invoke-method xlBooks "Add")
                                  xlSheets (vlax-get-property xlBook "Sheets")
                                  xlSheet  (vlax-get-property xlSheets "Item" 1)
                            )
                            (vla-put-visible xlApp :vlax-true)
                        )
                        (setq xlSheet (vlax-get-property xlApp 'ActiveSheet))
                    )
                    (setq xlCells (vlax-get-property xlSheet "Cells"))

                    ;; start writing from wherever the cursor is in Excel right now
                    (setq xlCell (vlax-get-property xlApp 'ActiveCell))
                    (setq baseRow (vlax-get-property xlCell 'Row)
                          baseCol (vlax-get-property xlCell 'Column)
                    )

                    (setq skipped 0)
                    (princ (strcat "\nTable is " (itoa rows) " rows x " (itoa cols) " columns. Copying..."))

                    (setq r 0)
                    (repeat rows
                        (setq c 0)
                        (repeat cols
                            (setq cellval (vl-catch-all-apply 'vla-GetText (list tbl r c)))
                            (if (vl-catch-all-error-p cellval)
                                (setq skipped (1+ skipped)) ;; likely a merged cell - leave blank
                                (progn
                                    (setq wr (vl-catch-all-apply 't2x_write_cell
                                        (list xlCells (+ baseRow r) (+ baseCol c) cellval)))
                                    (if (vl-catch-all-error-p wr)
                                        (princ (strcat "\nExcel write error at row " (itoa r) " col " (itoa c)
                                            ": " (vl-catch-all-error-message wr)))
                                    )
                                )
                            )
                            (setq c (1+ c))
                        )
                        (setq r (1+ r))
                    )

                    (princ (strcat "\nDone. " (itoa rows) "x" (itoa cols) " table copied to Excel."
                        (if (> skipped 0) (strcat " (" (itoa skipped) " merged cell(s) left blank)") "")))
                )
            )
        )
    )

    (vl-catch-all-apply '*error* (list nil))
    (princ)
)

(princ "\n:: TBL2XL.lsp | Version 1.0 :: Type TBL2XL to run.")
(princ)

 

Edited by Ajmal
small change
Posted

Not saying what you have done is not worthwhile but a search here would possibly have found this "Table to excel.lsp". I also know there are a few others out there.

 

; simple table to excel
; expects Title header and data
; BY Alanh Jan 2022
; do not have excel open

 

You may be interested in this it contains multiple Excel defuns to carry out task to do with Excel. A big help was FIXO who did some great Excel stuff but is no longer of this world. It is a work in progress as more functions are added. Just copy and paste the defun needed. Alan Excel library.lsp

 

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...