Jump to content

Read autocad file


giskumar

Recommended Posts

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.

Link to comment
Share on other sites

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

 

(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.

Edited by Lee Mac
Link to comment
Share on other sites

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.

 

 
(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)
) 

Link to comment
Share on other sites

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: ? :glare:

 

...
     (setq doc  ([color=red][b]GetDocumentObject[/b][/color] (vlax-get-acad-object) file))
...

Link to comment
Share on other sites

Why has this now become: ? :glare:

 

What is "How to steal someone else's idea to take full credit for themselves?"

 

ken.jpg

 

 

 

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

 

alex-trebek-picture.jpg

Link to comment
Share on other sites

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.

 

 

;;-----------------=={ 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                   ;;
;;------------------------------------------------------------;;


Link to comment
Share on other sites

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.

 

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:

 

knowing_2.png

Link to comment
Share on other sites

Hi All,

 

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

 

 
(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.

 

 
(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.

Link to comment
Share on other sites

Change the calling function to this:

 

(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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...