Quantcast
Channel: Autodesk AutoCAD forums
Viewing all articles
Browse latest Browse all 14319

Extracting attributes into a text file with a script

$
0
0

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);
            }
        }
    }

 


Viewing all articles
Browse latest Browse all 14319

Trending Articles