Similar to DATAEXTRACTION.

Similar to DATAEXTRACTION.

Anonymous
Not applicable
2,013 Views
6 Replies
Message 1 of 7

Similar to DATAEXTRACTION.

Anonymous
Not applicable

Hello everyone,

Can I export Txt/Csv Nu,X,Y,Z from these blocks without using the DATAEXTRACTION command?

Example:

01, X, Y, Z
02, X, Y, Z
03, X, Y, Z

[...]

 

0 Likes
Accepted solutions (2)
2,014 Views
6 Replies
Replies (6)
Message 2 of 7

roland.r71
Collaborator
Collaborator

You can export anything you want, in any text based format you desire, using lisp.

Dataextraction is just there for those who can't write lisp Smiley Tongue

Message 3 of 7

Anonymous
Not applicable

Good @roland.r71 !! lol
But as I deal with Visibility  since it is a dynamic block ?

0 Likes
Message 4 of 7

roland.r71
Collaborator
Collaborator
Accepted solution
; --- createCSV ----------------------------------------------------------------

   (defun createCSV ( csvfile dblist delimit / file_w lines text record)

      ; function to create .csv file
      ; csvfile = path+filename to use. example: c:\\temp\\mydata.csv
      ; dblist  = a 'database' list of values. (a list of lists)
      ; delimit = the delimiter to use. example: , ; \t (tab) etc.

      ; Example dblist: (("1" "x" "y")("2" "x" "y")("3" "x" "y"))

      (setq file_w (open csvfile "w"))
      (foreach record dblist
         (setq i 0 text "")
         (while (< i (length record))
            (setq text (strcat text (nth i record))
                  i (1+ i)
            )
            (if (< i (length record))
               (setq text (strcat text delimit))
            )
         )
         (write-line text file_w)
      )
      (close file_w)
   )

This function is what I use for creating .csv files.

 

Note: if you wish to export, edit data (with notepad or excel or ...) & write it back to acad entities (like block attributes) you need the entities handle for reference, just like dataextraction does.

 

For more info on that seeAbout Entity Handles and Their Uses (AutoLISP)

 

edit:

example of usage for createCSV:

(setq lst (list (list "1" "2" "3")(list "a" "b" "c")(list "A" "B" "C")))
(createCSV "c:/temp/testcsv.csv" lst "\t")

...and i bet you would like to have some lisp code to do the data extraction (?)

0 Likes
Message 5 of 7

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can try something like this:

 

(defun c:foo (/ ss file i)
  (if (setq ss (ssget "_X" '((0 . "INSERT") (66 . 1) (2 . "test,`*U*"))))
    (progn
      (setq file (open (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".csv") "w"))
      (repeat (setq i (sslength ss))
        (setq br (ssname ss (setq i (1- i))))
        (if (= (getpropertyvalue (getpropertyvalue br "BlockTableRecord") "Name") "test")
          (write-line
            (strcat
              (getpropertyvalue br "NU")
              ","
              (getpropertyvalue br "X")
              ","
              (getpropertyvalue br "Y")
            )
            file
          )
        )
      )
      (close file)
    )
  )
  (princ)
)

PS:  There's none "Z" attribute in the block.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 7

roland.r71
Collaborator
Collaborator

Nice & short. I like Smiley Wink

 

I used a bit more code, but the result is the same.

Except that i used tabs for my csv & still do check for Z, although i did notice its not there with the 'test' block.

(vl-load-com)
(defun c:DataX ( / dblist ss i ent ed entH av c abn sed tag att1 att2 att3 att4)

; --- createCSV ----------------------------------------------------------------

   (defun createCSV ( csvfile dblist delimit / file_w lines text record val)

      ; function to create .csv file
      ; csvfile = path+filename to use. example: c:\\temp\\mydata.csv
      ; dblist  = a 'database' list of values. (a list of lists)
      ; delimit = the delimiter to use. example: , ; \t (tab) etc.

      ; Example dblist: (("1" "x" "y")("2" "x" "y")("3" "x" "y"))

      (setq file_w (open csvfile "w"))
      (foreach record dblist
         (setq i 0 text "")
         (while (< i (length record))
            (setq val  (cond ((nth i record))(""))
                  text (strcat text val)
                  i    (1+ i)
            )
            (if (< i (length record))
               (setq text (strcat text delimit))
            )
         )
         (write-line text file_w)
      )
      (close file_w)
      (princ (strcat "\nCreated csv file: " csvfile))
   )

; === MAIN =====================================================================

   (if (setq ss (ssget "X" '((0 . "INSERT"))))
      (progn
         (setq i 0)
         (while (< i (sslength ss))
            (setq ent  (ssname ss i)
                  ed   (entget ent)
                  entH (cdr (assoc 5 ed))
                  av   nil
                  c    1
            )
            (if (= (vla-get-effectivename (vlax-ename->vla-object ent)) "test")
               (progn
                  (while (= c 1)
                     (setq ed (entget (entnext (cdr (assoc -1 ed))))
                           abn (cdr (assoc 0 ed))
                     )
                     (if (or (= "INSERT" abn)(= "SEQEND" abn))
                        (setq c 0)
                     )
                     (if (= "ATTRIB" abn)
                        (progn
                           (setq av (cdr (assoc 1 ed))
                                 tag (strcase (cdr (assoc 2 ed)))
                           )
                           (if (= tag "NU")(setq att1 av))
                           (if (= tag "X") (setq att2 av))
                           (if (= tag "Y") (setq att3 av))
                           (if (= tag "Z") (setq att4 av))
                        )
                     )
                     (setq blk (list entH att1 att2 att3 att4))
                  )
                  (setq dblist (append dblist (list blk))
                        i      (1+ i)
                  )
               )
            )
         )
         (createCSV (strcat (getvar "dwgprefix") (vl-filename-base (getvar "dwgname")) ".csv") dblist "\t")
      )
   )
   (princ)
)
0 Likes
Message 7 of 7

devitg
Advisor
Advisor

Some like it ??

 

Use ATTOUT 

 

 

 

0 Likes