2

I want to extract all the coordinates and the text itself of a specific layer into a CSV file. I am using the Mac version of AutoCAD 2018. DATAEXTRACT seems to be not available on Mac and -ATTEXT always writes: “0 records in extract file.” With the following template file:

NAME C008000
X N007001
Y N007001
Z N007001

I’ve tried it with lisp as well, but I only got until here:

(defun c:asdf () 
    (setq coordinates (assoc 10 (entget (entlast)))))
    (alert coordinates)
)

Any ideas? Thanks in advance!

richardXYZ
  • 33
  • 5

1 Answers1

1

Sorry for late reply I wrote some function and modified as per your need. load This lisp and type command TEXT2CSV.
then programme asks you for Layer name, Output folder name, and CSV file name. if the input is correct then CSV file create and save on the target location

;
(Defun C:text2CSV (/         extractText   FolderBox
           makecsvstring lyr           layerlist
           layerstring
          )


                    ;Function-----extractText ---------------------------------------------------------------------
                    ;[filterLIst]--->send selection filter criteria to Function by default nil then select all text
  (defun extractText
     (filterLIst / outputfile f en i ss name x y z path)

                    ;filterLIst check if filterLIst is nil then select all text from all layer
    (if (= nil filterLIst)
      (setq filterLIst
         '((-4 . "<or") (0 . "TEXT") (0 . "MTEXT") (-4 . "or>"))
      )
    )


                    ;get file path and file name
                    ;(setq path (FolderBox "Select folder" "D:/" 0))
    (setq path (FolderBox "Select Output folder:" "D:/" 0))
    (initget 1)
    (setq filename (getstring "\nType output CSV filename:"))

    (setq outputfile
       (strcat path
           "/"
           filename
           ".CSV"
       )
    )



                    ;select entity from dwg and set initial parameter
    (setq i  -1
      ss (ssget "X" filterLIst)
    )
                    ;create csv file
    (if ss
      (progn
    (setq f (open outputfile "w"))
    (if f
      (progn
        (write-line (strcat "Name," "X," "Y," "Z,") f)
        (while (setq en (ssname ss (setq i (+ 1 i))))
          (setq name (makecsvstring (cdr (assoc 1 (entget en)))))
          (setq x (rtos (nth 1 (assoc 10 (entget en)))))
          (setq y (rtos (nth 2 (assoc 10 (entget en)))))
          (setq z (rtos (nth 3 (assoc 10 (entget en)))))
                    ;write to csv
          (write-line (strcat name "," X "," Y "," Z) f)

        )
        (close f)
                    ;done
        (Alert "Done!")
      )
                    ;if error
      (print "\nWrong input unable to create csv file:")
    )
      )

      (print "\nNo text entity found:")
    )
    (princ)
  )
  ;;---------------------------------------------------------------EnD extractText --------------------------------






                    ;This function to select output folder
  (defun FolderBox (message directory flag / folder sh)
    ;; Arguments:
    ;; message: the message displayed in th dialog box
    ;; directory: the directory to browse
    ;; flag values:
    ;; 0 = Default
    ;; 1 = Only file system folders can be selected. If this bit is set, the OK button is disabled if the user selects a folder that doesn't belong to the file system (such as the Control Panel folder).
    ;; 2 = The user is prohibited from browsing below the domain within a network (during a computer search).
    ;; 4 = Room for status text is provided under the text box.
    ;; 8 = Returns file system ancestors only.
    ;; 16 = Shows an edit box in the dialog box for the user to type the name of an item.
    ;; 32 = Validate the name typed in the edit box.
    ;; 512 = None "New folder" button
    ;; 4096 = Enables the user to browse the network branch of the shell's namespace for computer names.
    ;; 8192 = Enables the user to browse the network branch of the shell's namespace for printer names.
    ;; 16384 = Allows browsing for everything.
    (vl-load-com)
    (setq shell (vlax-create-object "Shell.Application"))
    (if (setq
      folder (vlax-invoke
           shell 'browseforfolder 0 message flag directory)
    )
      (setq folder
         (vlax-get-property (vlax-get-property folder 'self) 'path)
      )
      (setq folder nil)
    )
    (vlax-release-object shell)
    folder
  )


                    ;this function add " to start and end of supply string 
  (defun makecsvstring (instring)
    (if (= nil instring)
      (setq instring "")
    )
    (strcat "\"" instring "\"")
  )


                    ;if can send direct layer name
  (setq layer (getstring "\nEnter Layer Name:" T))

  (if (and (/= layer "") (/= layer nil))
    (setq filterLIst

       (append
         (list (cons 8 layer))
         '((-4 . "<or")
           (0 . "TEXT")
           (0 . "MTEXT")
           (-4 . "or>")
          )
       )
    )

    (setq filterLIst

       '((-4 . "<or")
         (0 . "TEXT")
         (0 . "MTEXT")
         (-4 . "or>")
        )
    )

                    ;you can Edit for more filter criteria
  )
  (extractText filterLIst)


)

You can modified code as per your need further Hope this help

Dinesh Vilas Pawar
  • 493
  • 2
  • 5
  • 18
  • This could be much more helpful if it was formatted in any consistent way. – Svante Jun 22 '18 at 23:25
  • 1
    @Svante it is consistent, just A. follows its own style, and B. uses tabs=4 spaces. – Will Ness Jun 23 '18 at 09:29
  • I get this: ; "error: vl-load-com not supported on "Mac OS X Version 10.13 (x86_64)" – richardXYZ Jun 26 '18 at 07:16
  • @richardXYZ I have no experience with MAC here is [link](https://forums.autodesk.com/t5/autocad-for-mac-forum/vl-load-com-supported-on-quot-mac-os-x-version-10-6-quot/td-p/2993932) may be useful for you. – Dinesh Vilas Pawar Jun 26 '18 at 08:21
  • @richardXYZ I think you got this error while the selection of folder Just try to comment `(setq path (FolderBox "Select Output folder:" "D:/" 0))` this line and `(setq path "D:/")` below the commented line, this path is output folder of CSV file you can add your own path. – Dinesh Vilas Pawar Jun 26 '18 at 09:30