Search code examples
autocaddata-extractionautolisp

Export Selected Text From AutoCad


I am wondering if any of you fine people can point me in the right direction. I came up with an ssget function that selects the text I'm looking for but I'm not quite sure where to go from there to extract the text to either a txt or csv file.

Here is the ssget function that is working for me

(ssget "_X" '((0 . "TEXT,MTEXT")(1 . "ETCH*,MARK*,STAMP*")))

I need to grab this text from a folder full of drawings and export it to preferably a csv file where I can easily read.

Thanks in advance!

Allan


Solution

  • As per your question, you are extracting text from all drawings of the respective folder you can use ObjectODBX method here you can run this code directly and it extract text from drawing and create csv file with drawing name in the same folder try it you can add filter condition if you required.

        (Defun C:ExtractFolderToCSV( /  dwgfile filelist textstring f doc LM:GetDocumentObject FolderBox folderpath)
    
    ;; Get Document Object  -  Lee Mac
    ;; Retrieves the VLA Document Object for the supplied filename.
    ;; The Document Object may be present in the Documents collection, or obtained through ObjectDBX.
    ;; It is the callers responsibility to release such object.
    ;;This function I collect from Lee-Mac Thanks lee
    (defun LM:GetDocumentObject (dwg / app dbx dwl err vrs)
      (cond
        ((not (setq dwg (findfile dwg))) nil)
        ((cdr
           (assoc
         (strcase dwg)
         (vlax-for doc
               (vla-get-documents (setq app (vlax-get-acad-object)))
           (setq dwl
              (cons (cons (strcase (vla-get-fullname doc)) doc) dwl)
           )
         )
           )
         )
        )
        ((progn
           (setq dbx
              (vl-catch-all-apply
            'vla-getinterfaceobject
            (list app
                  (if (< (setq vrs (atoi (getvar 'acadver))) 16)
                "objectdbx.axdbdocument"
                (strcat "objectdbx.axdbdocument." (itoa vrs))
                  )
            )
              )
           )
           (or (null dbx) (vl-catch-all-error-p dbx))
         )
         (prompt "\nUnable to interface with ObjectDBX.")
        )
        ((vl-catch-all-error-p
           (setq err (vl-catch-all-apply 'vla-open (list dbx dwg)))
         )
         (prompt (strcat "\n" (vl-catch-all-error-message err)))
        )
        (dbx)
      )
    )
    
    
    ;This function for select folder
    (defun FolderBox (message directory flag / folder sh)
    ;;I found thiscode on web I am not remember website. sorry for that 
    ;; 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
    )
    
    
    (setq folderpath (FolderBox "Select Folder" "D:/" 0))
    (if (setq filelist (vl-directory-files (strcat folderpath "/") "*.dwg" 1))
      (foreach dwgfile filelist
    
        (setq
          f (open
          (strcat folderpath
              "/"
              (vl-string-subst ".CSV" ".DWG" (strcase dwgfile))
    
          )
          "w"
        )
        )                   ;create csv file in same folder with replaceing .dwg to .csv
    
        (if (setq doc (LM:GetDocumentObject (strcat folderpath "/" dwgfile)))
          (progn
        (vlax-for lyt (vla-get-layouts doc)
          (vlax-for obj (vla-get-block lyt)
            (if
              (or
            (= "AcDbMText" (vla-get-objectname obj));select onlly m_text and text
            (= "AcDbText" (vla-get-objectname obj))
    
              )
               (progn
             (setq textstring
                (vla-get-TextString obj)
             )
             (if
               (or (= (vl-string-search "ETCH" textstring) 0) ;your test condition
                 (= (vl-string-search "MARK" textstring) 0)
                 (= (vl-string-search "STAMP" textstring) 0)
                 )
    
               (write-line textstring f)
             )
               )
            )
          )
        )
        (vlax-release-object doc)
          )
        )
        (close f)
    
      )
    )
    
    
    
    );close defun
    

    Hope this helps