Results 1 to 3 of 3

Thread: Extract Attributes from multiple DWG files

  1. #1
    Member
    Join Date
    2005-01
    Posts
    18
    Login to Give a bone
    0

    Default Extract Attributes from multiple DWG files

    Hi,

    I was wondering if there was a way of extracting block attributes from several DWG files into one txt file. For example, where a full suite of project drawings (multiple DWG files) are located in a folder, could the information from the drawing title blocks be extracted into one list.

    I challenged ChatGPT to have a go and it came up with this:

    Code:
    (defun c:ExportAttributes (/ folder output-file)
      (setq folder (getfolder "\nSelect the folder containing AutoCAD drawing files: "))
      (setq output-file (getfiled "Save As" "" "Text Files (*.txt)|*.txt" 1))
      
      (if (and folder output-file)
        (progn
          (setq output-stream (open output-file "w"))
          (setq dwg-files (vl-directory-files folder "*.dwg" 1))
          
          (foreach dwg dwg-files
            (if (vl-file-directory-p dwg)
              (setq full-path (strcat folder "\" dwg))
              (setq full-path dwg)
            )
            
            (if (findfile full-path)
              (progn
                (setq doc (vla-get-activedocument (vlax-get-acad-object)))
                (vla-open (vla-get-documents doc) full-path)
                (setq blk-ref (vla-get-blockreferences doc))
                
                (foreach blk blk-ref
                  (setq atts (vla-get-attributes blk))
                  (foreach att atts
                    (setq att-value (vla-get-textstring att))
                    (write-line (strcat (vla-get-name att) "," att-value) output-stream)
                  )
                )
                
                (vla-close doc)
              )
            )
          )
          
          (close output-stream)
          (princ "\nAttributes exported successfully!")
        )
        (princ "\nError: Please select a valid folder and output file.")
      )
      (princ)
    )


    but it doesn't seem to work (I'm no Lisp expert so I've no idea if the code above is close to working or way off the mark!).

    Does anyone have any code that would work or is anyone able to tweak the code above?

    Thanks,

    Rich.
    Last edited by Ed Jobe; 2024-02-07 at 03:35 PM. Reason: Added code tags [CODE] [/CODE]

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,380
    Login to Give a bone
    0

    Default Re: Extract Attributes from multiple DWG files

    I didn't test it but it looks good. I would add one line though.

    Code:
    (vl-load-com) \\\loads the vl function definitions into memory.
    (defun c:ExportAttributes (/ folder output-file)
      (setq folder (getfolder "\nSelect the folder containing AutoCAD drawing files: "))
      (setq output-file (getfiled "Save As" "" "Text Files (*.txt)|*.txt" 1))
    C:> ED WORKING....

  3. #3
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    553
    Login to Give a bone
    0

    Default Re: Extract Attributes from multiple DWG files

    If you want 1 file its easy, (setq output-stream (open output-file "w")) use "A" for append to file also hard code the output file as you want to run code multiple times.

    I answered this exact question on another forum this is a start.

    Code:
    (defun c:DCAJA3 ( / fo lays lay obj atts att x str)
    (setq fo (Open "d:\\acadtemp\\D-CAJ-A3.csv" "A"))
    (setq lays (layoutlist))
    (foreach lay lays
      (setvar 'ctab lay)
      (command "pspace")
      (alert "wow")
      (setq str (strcat (getvar 'dwgprefix)(getvar 'dwgname)"," lay))
      (setq ss (ssget "X" '((0 . "INSERT")(cons 2  "D-CAJ-A3")(cons 410 lay))))
      (if (= ss nil)
        (princ "no title blocks")
        (progn
        (setq obj (vlax-ename->vla-object (ssname ss 0)))
        (setq atts (vlax-invoke obj 'Getattributes))
        (setq att (nth 0 atts))
        (setq str (strcat str "," (vlax-get att 'textstring) ))
        (setq x 1)
        (repeat (- (length atts) 1)
          (setq att (nth x atts))
          (setq str (strcat str "," (vlax-get att 'textstring)))
          (setq x (1+ x))
        )
        (write-line str fo)
        )
      )
    )
    (close fo)
    (princ)
    )
    (c:DCAJA3)
    script

    Code:
    open "d:\\acadtemp\\Test (2).dwg"
    (load "d:\\acadtemp\\Test.lsp")
    close n
    open "d:\\acadtemp\\Test (3).dwg"
    (load "d:\\acadtemp\\Test.lsp")
    close n
    open "d:\\acadtemp\\Test (4).dwg"
    (load "d:\\acadtemp\\Test.lsp")
    close n

Similar Threads

  1. Script to extract cannoscalevalue from .dwg files, append filename and saveas
    By wadehammond in forum AutoCAD Civil 3D - General
    Replies: 0
    Last Post: 2011-03-17, 04:38 PM
  2. Extract attributes
    By lfish in forum AutoCAD General
    Replies: 2
    Last Post: 2007-05-04, 09:52 AM
  3. Extract nested attributes by selected blocks
    By charlie.bauer341340 in forum AutoLISP
    Replies: 4
    Last Post: 2006-12-19, 04:31 PM
  4. Extract Dynamic Block Attributes, values change as Block changes
    By dave.buckberry in forum Dynamic Blocks - Technical
    Replies: 11
    Last Post: 2006-09-05, 04:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •