+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Senior Member
    Using
    AutoCAD 2006
    Join Date
    Aug 2009
    Posts
    109

    Default Read autocad file

    Registered forum members do not see this ad.

    Hi All,

    I just want to read the list of layers of an autocad file with out open it.
    Is this possible with lisp?

    Thanks,
    Kumar.

  2. #2
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,744

    Default

    This could be done through ObjectDBX, as the following code demonstrates:

    Code:
    (defun c:test ( / doc file lst )
      (if
        (and
          (setq file (getfiled "Select Drawing" "" "dwg;dwt;dws" 16))
          (setq doc  (LM:GetDocumentObject (vlax-get-acad-object) file))
        )
        (progn
          (vlax-for l (vla-get-layers doc) (setq lst (cons (vla-get-name l) lst)))
          (vlax-release-object doc)
        )
      )
      (reverse lst)
    )        
    
    ;;-----------------=={ Get Document Object }==----------------;;
    ;;                                                            ;;
    ;;  Retrieves a the VLA Document Object for the specified     ;;
    ;;  filename. Document Object may be present in the Documents ;;
    ;;  collection, or obtained through ObjectDBX                 ;;
    ;;------------------------------------------------------------;;
    ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
    ;;------------------------------------------------------------;;
    ;;  Arguments:                                                ;;
    ;;  acapp    - AutoCAD Application Object                     ;;
    ;;  filename - filename for which to retrieve document object ;;
    ;;------------------------------------------------------------;;
    ;;  Returns:  VLA Document Object, else nil                   ;;
    ;;------------------------------------------------------------;;
    
    (defun LM:GetDocumentObject ( acapp filename / acdocs dbx )
      
      (vlax-for doc  (vla-get-Documents acapp)
        (setq acdocs (cons (cons (strcase (vla-get-fullname doc)) doc) acdocs))
      )
      (cond
        ( (not (setq filename (findfile filename)))
          nil
        )
        ( (cdr (assoc (strcase filename) acdocs))
        )
        ( (not
            (vl-catch-all-error-p
              (vl-catch-all-apply 'vla-open
                (list (setq dbx (LM:ObjectDBXDocument acapp)) filename)
              )
            )
          )
          dbx
        )
      )
    )
    
    ;;-----------------=={ ObjectDBX Document }==-----------------;;
    ;;                                                            ;;
    ;;  Retrieves a version specific ObjectDBX Document object    ;;
    ;;------------------------------------------------------------;;
    ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
    ;;------------------------------------------------------------;;
    ;;  Arguments:                                                ;;
    ;;  acapp - AutoCAD Application Object                        ;;
    ;;------------------------------------------------------------;;
    ;;  Returns:  VLA ObjectDBX Document object, else nil         ;;
    ;;------------------------------------------------------------;;
    
    (defun LM:ObjectDBXDocument ( acapp / acver )
      (vla-GetInterfaceObject acapp
        (if (< (setq acver (atoi (getvar "ACADVER"))) 16)
          "ObjectDBX.AxDbDocument" (strcat "ObjectDBX.AxDbDocument." (itoa acver))
        )
      )
    )
    
    (vl-load-com) (princ)
    An extension of this idea is used in this program.
    Last edited by Lee Mac; 24th Jun 2011 at 10:50 am.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  3. #3
    Senior Member
    Using
    AutoCAD 2006
    Join Date
    Aug 2009
    Posts
    109

    Default

    Hi Lee,

    thanks for the quick responce.

    I have changed the function as below to add a layer to file.
    Not getting error but no layer is added to file.
    Please tell me where i am doing wrong.

    Code:
     
    (defun c:test ( / doc file )
      (if
        (and
          (setq file (getfiled "Select Drawing" "" "dwg;dwt;dws" 16))
          (setq doc  (GetDocumentObject (vlax-get-acad-object) file))
        )
        (progn
          (setq LayerTable (vla-get-layers doc))
          (setq aNewLayer (vla-add LayerTable "TestLayer"))
          (vla-put-color aNewLayer 2)
          (vlax-release-object doc)
        )
      )
      (reverse lst)
    )

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,744

    Default

    Quote Originally Posted by giskumar View Post
    I have changed the function as below to add a layer to file.
    Not getting error but no layer is added to file.
    You will need to save the file after adding the layer.

    Why has this now become: ?

    Quote Originally Posted by giskumar View Post
    Code:
    ...
          (setq doc  (GetDocumentObject (vlax-get-acad-object) file))
    ...
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  5. #5
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,953

    Default

    Quote Originally Posted by Lee Mac View Post
    Why has this now become: ?
    What is "How to steal someone else's idea to take full credit for themselves?"





    OoooOoo, sooory, sooory. The correct answer is "What is a schmuck?"

    "Potential has a shelf life." - Margaret Atwood

  6. #6
    Senior Member
    Using
    AutoCAD 2006
    Join Date
    Aug 2009
    Posts
    109

    Default

    Sorry guys

    I don't want to take that credit.
    As a part of explaining it to a very much fresher i just removed it to prove that we can use any name for the functions. unfortunately i have copied that one.

    The below header is still in my original program.

    Same time thanks to Lee for the quick help.


    HTML Code:
    ;;-----------------=={ Get Document Object }==----------------;;
    ;;                                                            ;;
    ;;  Retrieves a the VLA Document Object for the specified     ;;
    ;;  filename. Document Object may be present in the Documents ;;
    ;;  collection, or obtained through ObjectDBX                 ;;
    ;;------------------------------------------------------------;;
    ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
    ;;------------------------------------------------------------;;
    ;;  Arguments:                                                ;;
    ;;  acapp    - AutoCAD Application Object                     ;;
    ;;  filename - filename for which to retrieve document object ;;
    ;;------------------------------------------------------------;;
    ;;  Returns:  VLA Document Object, else nil                   ;;
    ;;------------------------------------------------------------;;
    

  7. #7
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,953

    Default

    Perhaps my attempt at humor was a bit pointed.

    I think you understand though, that those who volunteer their work for the benefit of others (i.e., do so for free), simply want credit where credit is due. Nothing more.

    Quote Originally Posted by giskumar View Post
    I don't want to take that credit.
    As a part of explaining it to a very much fresher i just removed it to prove that we can use any name for the functions. unfortunately i have copied that one.

    The below header is still in my original program.

    Same time thanks to Lee for the quick help.
    Well, now you know... and knowing is:

    "Potential has a shelf life." - Margaret Atwood

  8. #8
    Senior Member
    Using
    AutoCAD 2006
    Join Date
    Aug 2009
    Posts
    109

    Default

    Hi All,

    After adding layer to the document, I tried to save document using

    Code:
     
    (vla-save doc)
    But it returns the following error.
    ; error: Automation Error. Description was not provided.

    I also noticed that it is working fine with currently opened active drawings.

    Code:
     
    (setq thisdrawing 
          (vla-get-activedocument 
               (vlax-get-acad-object)))
    (vla-save thisdrawing )

    Please let me know how can i save my document after adding layers to it.


    Thanks,
    Kumar.

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,744

    Default

    Change the calling function to this:

    Code:
    (defun c:test ( / doc file )
      (if
        (and
          (setq file (getfiled "Select Drawing" "" "dwg;dwt;dws" 16))
          (setq doc  (LM:GetDocumentObject (vlax-get-acad-object) file))
        )
        (progn
          (setq layer (vla-add (vla-get-layers doc) "TestLayer"))
          (vla-put-color layer 2)
          (vla-saveas doc file)
          (vlax-release-object doc)
        )
      )
      (princ)
    )
    You need to use the SaveAs method when working with ObjectDBX.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  10. #10
    Senior Member
    Using
    AutoCAD 2006
    Join Date
    Aug 2009
    Posts
    109

    Default

    Registered forum members do not see this ad.

    Thanks Lee

    It working great......

Similar Threads

  1. AutoCAD 2008 LT opens new session for every file...read further before you hate me
    By stockers in forum AutoCAD Drawing Management & Output
    Replies: 5
    Last Post: 5th Jul 2011, 12:46 am
  2. How to read a shape file
    By Herbot in forum The CUI, Hatches, Linetypes, Scripts & Macros
    Replies: 5
    Last Post: 7th May 2011, 11:31 am
  3. Read file - VBA
    By neophyte in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 30th Jul 2009, 12:30 pm
  4. VBA Read file
    By mien in forum AutoLISP, Visual LISP & DCL
    Replies: 13
    Last Post: 17th Apr 2009, 08:08 am

Bookmarks

Posting Permissions

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