.NET

Reply
New Member
zendaz
Posts: 2
Registered: ‎12-26-2013
Message 1 of 11 (647 Views)

How to programmatically extract the attributes from a CAD file?

647 Views, 10 Replies
12-26-2013 05:01 PM

Scenario: We have a bunch of dwg files. The requirement is to extract a specific list of attributes from the layers within CAD to a csv file. We are currently manually opening the dwg using AutoCAD and exporting the attributes. This has been time consuming and we need a faster process.

 

What options do I have to programmtically extract them? Thanks in advance.

 

 

Active Contributor
sdphg
Posts: 28
Registered: ‎05-21-2008
Message 2 of 11 (637 Views)

Re: How to programmatically extract the attributes from a CAD file?

12-26-2013 05:35 PM in reply to: zendaz

You can let program to open files then do the extraction. Code maybe like below:

using(Database db = new Database(false, true))
{
    db.ReadDwgFile(YOUR_FILE_NAME, FileShare.ReadWrite, false, null);
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        //your extraction code
    }
}

 you can put the code in a loop to batch your files. If you use command to start the routine, be sure to mark you command attribute with CommandFlags.Session, 

New Member
zendaz
Posts: 2
Registered: ‎12-26-2013
Message 3 of 11 (594 Views)

Re: How to programmatically extract the attributes from a CAD file?

12-27-2013 12:08 PM in reply to: sdphg

I get the following exception on the line Transaction tr = db.TransactionManager.StartTransaction();

A first chance exception of type 'System.InvalidProgramException' occurred

 

 

The following thread http://forums.autodesk.com/t5/NET/RealDWG-2013-InvalidProgramException/td-p/3472574 mentions that I need RealDWG components.

 

Questions,

  1. Do I need to buy ReadDWG even if our company has already bought AutoCAD? We have AutoCAD but no ReadDWG licences.
  2. Can someone provide more details on what licences are needed to programmatically read dwg files?
Mentor
SENL1362
Posts: 174
Registered: ‎07-20-2011
Message 4 of 11 (589 Views)

Re: How to programmatically extract the attributes from a CAD file?

12-27-2013 12:39 PM in reply to: zendaz
A valid AutoCAD license will do -- not AutoCAD LT
Valued Mentor
gasty1001
Posts: 478
Registered: ‎04-11-2010
Message 5 of 11 (585 Views)

Re: How to programmatically extract the attributes from a CAD file?

12-27-2013 12:57 PM in reply to: zendaz

Hi,

 

The task you mention looks like a good candidate for a data extraction API application, you need AutoCAD full o any vertical based on AutoCAD. No need of RealDwg. A good start point here: DX .

 

Gaston Nunez

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 6 of 11 (575 Views)

Re: How to programmatically extract the attributes from a CAD file?

12-27-2013 01:46 PM in reply to: SENL1362
you can also take a look in below link which may be useful for you:

http://through-the-interface.typepad.com/through_the_interface/2007/07/accessing-dwg-f.html
Mentor
SENL1362
Posts: 174
Registered: ‎07-20-2011
Message 7 of 11 (571 Views)

Re: How to programmatically extract the attributes from a CAD file?

12-27-2013 02:00 PM in reply to: zendaz

With one of the full AutoCAD versions this will get you started

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;


using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;


namespace AttExt
{
    public class Class1
    {
        [CommandMethod("Att2Csv")]
        public void AttributesToCsv()
        {
            Document doc=Application.DocumentManager.MdiActiveDocument;
            Editor ed=doc.Editor;


            string dwgPath = @"c:\temp\drawings";
            string csvPathname = @"c:\temp\drawings\AtEx.csv";
            if (File.Exists(csvPathname))
                File.Delete(csvPathname);

            foreach (var dwgPathname in Directory.GetFiles(dwgPath, "*.dwg", SearchOption.AllDirectories))
            {
                ed.WriteMessage("\nProcessing: {0}", dwgPathname);
                AttExt(dwgPathname,csvPathname);


            }

        }

        public void AttExt(string dwgPathname, string csvPathname)
        {
            StringBuilder attOut = new StringBuilder();
            attOut.AppendFormat("{0};", Path.GetFileNameWithoutExtension(dwgPathname));

            using (Database attDb = new Database(false, true))
            {
                attDb.ReadDwgFile(dwgPathname, FileShare.ReadWrite, false, null);

                using (Transaction tr = attDb.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)attDb.BlockTableId.GetObject(OpenMode.ForRead);
                    BlockTableRecord mBtr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                    foreach (ObjectId msId in mBtr)
                    {
                        if (msId.ObjectClass.DxfName.ToUpper() == "INSERT")
                        {
                            BlockReference blkRef = (BlockReference)tr.GetObject(msId, OpenMode.ForRead);
                            AttributeCollection atts = blkRef.AttributeCollection;
                            if (atts == null)
                                continue;

                            attOut.AppendFormat("{0}:{1};", blkRef.Name, blkRef.Position.ToString());

                            foreach (ObjectId arId in atts)
                            {
                                AttributeReference attRef = (AttributeReference)tr.GetObject(arId, OpenMode.ForRead);
                                attOut.AppendFormat("{0}:{1};", attRef.Tag, attRef.TextString);
                            }
                        }
                    }

                    tr.Commit();
                }
            }
            attOut.AppendLine();
            File.AppendAllText(csvPathname,attOut.ToString());
        }
    }
}

 

New Member
pitechops
Posts: 2
Registered: ‎01-13-2014
Message 8 of 11 (466 Views)

Re: How to programmatically extract the attributes from a CAD file?

01-13-2014 06:13 PM in reply to: SENL1362

I get the following exception "A first chance exception of type 'System.InvalidProgramException' occurred" on the code shown below.

 

Document doc=Application.DocumentManager.MdiActiveDocument;

I have attached the project zip file too. The reference file in the project were added from \\Autodesk\Autodesk_ObjectARX_2014_Win_64_and_32Bit\inc\ folder. And I am using VS Express 2012 as IDE.

 

Is there anything that I am missing here. Appreciate your help.

 

Thanks

Mentor
SENL1362
Posts: 174
Registered: ‎07-20-2011
Message 9 of 11 (456 Views)

Re: How to programmatically extract the attributes from a CAD file?

01-14-2014 12:31 AM in reply to: pitechops

Nothing wrong with the syntax, except you're project compiled into an standalone executable,

while the code is meant to be used as an (class) extension to AutoCAD.

 

Probably the easiest way for you to get this fixed is:

1. Start a new C#/CLASS LIBRARY project, see fig1;

2. Replace the contents class1.cs with the contents of cadextract.cs;

3. Add the AutoCAD references AcCoreMgd, AcDbMgd and AcMgd from you're regular AutoCAD installation folder (c:\program files...;

4. Set Copy Local of the AutoCAD references to False -- see fig2;

5. Build the project

6. Start AutoCAD and use the NETLOAD command to load the ..\(bin|release)\projectname.dll

7. Start ATT2CSV (not Main) from within AutoCAD, see fig3.

 

 

Fig1: C# Class typed project, to create an AutoCAD extension

C#Class.png

 

 

 

Fig2: AutoCAD references: CopyLocal=False

RefCopyLocal.png

 

Fig3: Run CadLoad AutoCAD extension from within AutoCAD

Command: ATT2CSV

Processing: c:\temp\tgt\CAD\1502017.dwgLoading AEC Base...
Loading AEC Base Extended...
Loading AEC Core...
Loading AEC Project Base...
Loading AEC Architectural Base...
Loading AEC Schedule...

Processing: c:\temp\tgt\CAD\HolecKlemMask_1.dwg
Processing: c:\temp\tgt\CAD\HolecStroomMask_1.dwg
Processing: c:\temp\tgt\CAD\HolecStroomMask_A3h.dwg

 

 

 

 

 

 

 

 

 

 

New Member
pitechops
Posts: 2
Registered: ‎01-13-2014
Message 10 of 11 (438 Views)

Re: How to programmatically extract the attributes from a CAD file?

01-14-2014 10:55 AM in reply to: SENL1362

Thanks for the details. 

 

Step 6 mentions that I need to start AutoCAD. How can I process and  extract attributes from dwg without having to start AutoCAD at all? What would the be the steps involved? Thanks in advance.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.