VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Extracting attributes into a text file with a script

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
schwabg
3146 Views, 7 Replies

Extracting attributes into a text file with a script

hello,

 

I have a requirement to extract metadata from AutoCAD files with a script. This metadata is the attributes that appear in the files Title Block in the corner. I have come up with some code that I believe can do this, but I cannot run it, since I've been developing in Visual Studio using .NET, and it appears that that .NET API can only be used to build plugins for AutoCAD.

 

Do I have to use VBA to access the COM? How do I even get started with VBA/COM API? It took me forever to actually find a listing for the .NET Object Model - I don't want to search for that long again to find the COM API, especially if I'm going about this in the wrong way again.

 

Thanks,

-George

 

Here is the code I've written to extract the Attributes, but it's probably very wrong as well, since I have not had the opportunity to test it:

 

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (Database db = new Database())
                {
                    db.ReadDwgFile(@"C:\Users\schwabg\Desktop\TSD-LY-306-9001-003.dwg.DWG", FileOpenMode.OpenForReadAndReadShare, false, "");

                    using (Transaction transaction = db.TransactionManager.StartTransaction())
                    {
                        foreach (DBObject obj in transaction.GetAllObjects())
                        {
                            ObjectId id = obj.Id;
                            BlockReference blockreference = (BlockReference)transaction.GetObject(id, OpenMode.ForRead);
                            //BlockTableRecord btr = (BlockTableRecord)transaction.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);
                            foreach (ObjectId attributeid in blockreference.AttributeCollection)
                            {
                                AttributeReference attributereference = (AttributeReference)transaction.GetObject(attributeid, OpenMode.ForRead);
                                Console.WriteLine("key: " + attributereference.Tag + "\n\t\t\tvalue: " + attributereference.TextString);
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
    }

 

7 REPLIES 7
Message 2 of 8
norman.yuan
in reply to: schwabg

There are 2 things you need to understand well before rushing out some codoe to run.

 

Firstly, you need to konw what kind of APIs are available for your code to work with AutoCAD. It looks like at least you are aware .NET API and COM API (which does not necessarily mean VBA code, but VBA code uses COM API).

 

Secondly, based on your understaning of the API to use, you want to think hard where your code runs, inside AutoCAD or outside AutoCAD. We probably can exclude the case of no AutoCAD installation is required here.

 

With .NET API, the code that references ,NET API can only run inside AutoCAD, while with COM API, you can write VBA code that runs inside AutoCAD, you can write external app to automate AutoCAD. In both the cases, AutoCAD must be installed and runs (to load the .NET/VBA code to run, or to be launched by your app via COM API).

 

With COM API, you can either wirte VBA code easily and quickly (well, due to 64-bit issue, things in VBA now is a bit complicated than before), or you can do an external app. With exteranl app (you use VB.NET or C#, of couse), you simply set reference to AutoCAD COM type library, then off you go,

 

You want to seriously think how your code would work, now that AutoCAD must be running, regardless your code uses .NET API or COM API: do you want to run a separate app besides AutoCAD to do the work, or you simply run the code in an already started AutoCAD? In most cases, running inside AutoCAD would be better choice, IMO.

 

In short, if you have to write an external app, you must use COM API. If you use .NET API as your code shown, it must be running inside AutoCAD, and IMO, it MIGHT be the better way to go (without knowning the whole picture of your needs).

Message 3 of 8
dgorsman
in reply to: norman.yuan

Short version: you need AutoCAD to read a DWG file.  You can't get around that with "VBA".

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 4 of 8
schwabg
in reply to: dgorsman

So are you saying, even with VBA/ActiveX, any code to read DWG files will actually start up AutoCAD and then talk to it, rather than actually reading the DWG file itself? If that is the case, than my requirements cannot be met without looking for 3rd party solutions. I need to read the Title Block attributes from files en masse, and starting up AutoCAD for each file (or loading each file into AutoCAD, however it works) would take too long and put too much strain on the servers (I'm mixing two different requirements here).

I am very surprised there is no easy way to get metadata out of these files. What about converting them to DXF files and then reading them? Can that be done en masse?
Message 5 of 8
dgorsman
in reply to: schwabg

There is a means, which was alluded to above.  You can purchase the RealDWG toolkit from AutoDesk which will allow non-AutoCAD based DWG access.  It is quite advanced however, and not VBA/COM based.

 

The process I normally see for this is to export the metadata to a neutral format (text file, database, etc.) while the user works in the drawing, initiated by a save or drawing close operation.  From there other applications can access the information as needed.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 6 of 8
norman.yuan
in reply to: schwabg

Well, you need to know more about AutoCAD and the Dwg file. DWG file format is not an open file format, it is proprietory commmercial property of Autodesk. Theroretically, DWG can only be handled correctly with software produced by Autodesk.

 

If you do not wan to run AutoCAD to read DWG file, you can license RealDwg from Autodesk with a prohibitive price, aimed to commercial software development. With RealDwg, you can deal Dwg file without AutoCAD.

 

As alternative, you may look into non-Autodesk software support for dealing with DWG file.

 

But with AutoCAD, you can open drawing as side DB and works quite fast.

 

Message 7 of 8
schwabg
in reply to: norman.yuan

I see. Are there any restrictions in the AutoCAD license prohibiting the use of third party software to interact with DWG files?

Message 8 of 8
norman.yuan
in reply to: schwabg

I am not aware of any, but it may depend on what the third party software is/does.

 

Basically, you need to deside whether you want AutoCAD being used as your solution. If not, then you'd either license RealDwg from Autodesk or find a right third party app, which is very likely based on RealDwg. For the former, the price for the license is very expensive that it only makes sense if you develop commercial software or your app have large enough user base. For the latter, it is hard to find existing product that fits your exact needs.

 

So, in many places where DWG file is used, running AutoCAD to do batch processing for something, such as you needed, is very common tasks. Most AutoCAD supporting programmers have done similar code with AutoLISP/VBA/.NET API or an stand-alone app once or many times. You just need to carefully analyse your need and choose the API that meets your situation the best.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost