Plannedscape Postings

  • Blog Home
  • /
  • Reading Autocad DWGs When Closed

Reading Autocad DWGs When Closed
The .ReadDWGFile Method

Posted by Charlie Recksieck on 2020-03-05
This article is just for Autocad people. About 30% of our business involves Autocad projects so from time to time we love sharing some specific things from that world. If this doesn't include you, then you have the week off from the blog this week & we'll see you next week with something more generic.

* * *

It’s normally safe to say that items in Autocad drawings are best viewed inside Autocad. Beyond that, there are interfaces to import things into Autocad and to export them out in an Autocad session.

There is a LOT of data in Autocad drawings. Of course, there is a lot of geospatial data, but so much more. Would it interest you to be able to read (or write) something from an ACAD .DWG without having to open Autocad?

Looking Up What's In A Closed Autocad .DWG

Everything in an Autocad drawing is in that DWGs database. Everything. All objects. All views. All Drawing Properties. Let's say you'd like to read things from multiple drawings. If you did it manually by opening Autocad then browsing to it, you can certainly find it. But it takes some time.

Yet if you wanted to look up something repetitively in lots of files, writing code/projects that look at Autocad drawings "behind the scenes" is much more efficient.

You can do it. If you have a project (be it an executable or all functions that you call from another project), you'll bring in Autocad code references (particularly the Autodesk.AutoCAD.DatabaseServices namespace).

.ReadDWGFile Method

How do you do it? The magic bullet is the .ReadDWGFile command.

Here's the official Autocad documentation on the command.

Some Sample Code

Below you'll find a function which retrieves the value of a certain attribute from a closed DWG with a supplied location. A parent function could loop through a bunch of files or folders in a loop and call this function for repetitive results. (This is included as a JPG; if you want the actual source code, feel free to email us - or email us to have us walk you through the function a bit.

Kean Walmsley's Take

If you'd like an alternate explanation of how to do this, I also should direct you to the great Kean Walmsley and his blog. He's been a longtime Autodesk employee. I've never worked with him directly but been in a couple sessions with him at Autodesk University. He's incredibly sharp and a great guy. If you're serious about Autocad and education, get to know him.

Here's Kean’s similar use of the .ReadDWGFile method.

Writing Values To Closed Files

Not only can we read from closed file, but we can also rewrite/edit values in closed DWGs.

Basically, we save a DatabaseSummaryInfo object from the original, and a "clone" version. You can iterate through or jump down some levels to actually edit some values in your clone version - then replace the DWG database's SummaryInfo object with your modified one.

There are some glitchy things about this; but if you get into it, feel free to email us to see some sample code with our workarounds on this that will help.

Save vs. SaveAs Tip When Altering DWGs

Another glitch to be aware of when editing closed DWGs: The "Save" method does not work, so use "SaveAs" like this:

dbThisDWG.SummaryInfo = infoSummary;
dbThisDWG.RetainOriginalThumbnailBitmap = true;
// CHARLIE: another known bug, Save doesn’t work, must do SaveAs
dbThisDWG.SaveAs(strFullPath, DwgVersion.Current);

A Note About Speed

Starting in 2013 versions of Autocad and beyond, it sure seems like finishing transactions with .ReadDWGFile slowed down. Still in fractions of seconds, but if code is searching through 100 files in a folder and looking in each for some values, then those fractions add up to something like 8 seconds, which can seem like an eternity these days.

There are some best practices you can do to optimize the use of ReadDWGFile. If you're doing read only versions, then the StartOpenCloseTransaction method is preferable to StartTransaction.

You need to be smart about document locks, database locks, using the "using" command. Make sure to also use "using" on your database object so that disposes efficiently, too.

Long story short, good optimization can really make a difference here.

Some Practical Uses

* Count the instances of references to a block of a large number of DWG files on your server.

* Swap out one block for another in a group of drawings.

* Select a group of files that have a certain value in one of your drawing properties.

These are just a few ideas. Basically anything that can be done in an Autocad session can also be executed "in code" by modifying internal database values. Knowing where these values are, that can be the tricky part.


This code isn't for the faint of heart, or newbies to working with Autocad directly in code. But once you get the hang of it, it's a powerful tool.

Start thinking about what you could do with this.

And if you have some questions on this or want our two cents for free, go ahead and contact us and we'll get you started. Happy coding, everybody!