December 2014

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      










« Automatic closing of AutoCAD objects with ObjectARX SmartPointers | Main | The New RibbonBar API in AutoCAD 2009 »

April 07, 2008

Extracting XML data from drawings using a new .NET API in AutoCAD 2009

This post is the latest in the series of closer looks at the new APIs in AutoCAD 2009. It covers the Data Extraction API, a new .NET API allowing you to drive the Data Extraction feature inside AutoCAD.

There is a very thorough C# sample included on the ObjectARX SDK (under samples/dotNet/DataExtraction), so I thought I'd focus on a much simpler example where we extract data from a single drawing.

Here's the C# code:

using System.Collections.Generic;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.DataExtraction;


namespace DataExtraction

{

  public class Commands

  {

    const string path =

      @"c:\Program Files\Autodesk\AutoCAD 2009\Sample\";

    const string fileName =

      "Visualization - Aerial.dwg";

    const string outputXmlFile =

      @"c:\temp\data-extract.xml";


    [CommandMethod("extd")]

    public void extractData()

    {

      if (!System.IO.File.Exists(path + fileName))

      {

        Document doc =

          Application.DocumentManager.MdiActiveDocument;

        Editor ed =

          doc.Editor;

        ed.WriteMessage("\nFile does not exist.");


        return;

      }


      // Create some settings for the extraction


      IDxExtractionSettings es =

        new DxExtractionSettings();


      IDxDrawingDataExtractor de =

        es.DrawingDataExtractor;


      de.Settings.ExtractFlags =

        ExtractFlags.ModelSpaceOnly |

        ExtractFlags.XrefDependent |

        ExtractFlags.Nested;


      // Add a single file to the settings


      IDxFileReference fr =

        new DxFileReference(path, path + fileName);


      de.Settings.DrawingList.AddFile(fr);


      // Scan the drawing for object types & their properties


      de.DiscoverTypesAndProperties(path);


      List<IDxTypeDescriptor> types =

        de.DiscoveredTypesAndProperties;


      // Select all the types and properties for extraction

      // by adding them one-by-one to these two lists


      List<string> selTypes = new List<string>();

      List<string> selProps = new List<string>();


      foreach (IDxTypeDescriptor type in types)

      {

        selTypes.Add(type.GlobalName);

        foreach (

          IDxPropertyDescriptor pr in type.Properties

        )

        {

          if (!selProps.Contains(pr.GlobalName))

            selProps.Add(pr.GlobalName);

        }

      }


      // Pass this information to the extractor


      de.Settings.SetSelectedTypesAndProperties(

        types,

        selTypes,

        selProps

      );


      // Now perform the extraction itself


      de.ExtractData(path);


      // Get the results of the extraction


      System.Data.DataTable dataTable =

        de.ExtractedData;


      // Output the extracted data to an XML file


      if (dataTable.Rows.Count > 0)

      {

        dataTable.TableName = "My_Data_Extract";

        dataTable.WriteXml(outputXmlFile);

      }

    }

  }

}

Here are the first few objects from the output file found in c:\temp\data-extract.xml after running the EXTD command:

<?xml version="1.0" standalone="yes"?>

<DocumentElement>

  <My_Data_Extract>

    <AcDxHandleData>183</AcDxHandleData>

    <Layer>0</Layer>

    <LinetypeScale>1</LinetypeScale>

    <PlotStyleName>ByLayer</PlotStyleName>

    <LineWeight>ByLayer</LineWeight>

    <Material>Material 3</Material>

    <Linetype>ByLayer</Linetype>

    <Color>ByLayer</Color>

    <AcDxObjectTypeName>3D Solid</AcDxObjectTypeName>

    <AcDxObjectTypeGlobalName>

      Autodesk.AutoCAD.DatabaseServices.Solid3d

    </AcDxObjectTypeGlobalName>

    <AcDxDwgSummaryDwgName>

      Visualization - Aerial.dwg

    </AcDxDwgSummaryDwgName>

    <AcDxDwgSummaryDwgLocation>

      c:\Program Files\Autodesk\AutoCAD 2009\Sample

    </AcDxDwgSummaryDwgLocation>

    <AcDxDwgSummaryDwgSize>472480</AcDxDwgSummaryDwgSize>

    <AcDxDwgSummaryDwgCreated>

      2007-01-03T00:44:20+01:00

    </AcDxDwgSummaryDwgCreated>

    <AcDxDwgSummaryDwgModified>

      2007-01-03T00:44:20+01:00

    </AcDxDwgSummaryDwgModified>

    <AcDxDwgSummaryDwgAccessed>

      2008-03-03T11:40:53.796875+01:00

    </AcDxDwgSummaryDwgAccessed>

    <AcDxDwgSummaryDwgTitle />

    <AcDxDwgSummaryDwgSubject />

    <AcDxDwgSummaryDwgAuthor />

    <AcDxDwgSummaryDwgKeywords />

    <AcDxDwgSummaryDwgComments />

    <AcDxDwgSummaryDwgHyperLinkBase />

    <AcDxDwgSummaryDwgLastSavedBy>

      thompsl

    </AcDxDwgSummaryDwgLastSavedBy>

    <AcDxDwgSummaryDwgRevisionNumber />

    <AcDxDwgSummaryDwgTotalEditingTime>

      1179

    </AcDxDwgSummaryDwgTotalEditingTime>

    <AcDxEntityHyperlink />

  </My_Data_Extract>

  <My_Data_Extract>

    <AcDxHandleData>191</AcDxHandleData>

    <Layer>0</Layer>

    <LinetypeScale>1</LinetypeScale>

    <PlotStyleName>ByLayer</PlotStyleName>

    <LineWeight>ByLayer</LineWeight>

    <Material>Material 3</Material>

    <Linetype>ByLayer</Linetype>

    <Color>ByLayer</Color>

    <AcDxObjectTypeName>3D Solid</AcDxObjectTypeName>

    <AcDxObjectTypeGlobalName>

      Autodesk.AutoCAD.DatabaseServices.Solid3d

    </AcDxObjectTypeGlobalName>

    <AcDxDwgSummaryDwgName>

      Visualization - Aerial.dwg

    </AcDxDwgSummaryDwgName>

    <AcDxDwgSummaryDwgLocation>

      c:\Program Files\Autodesk\AutoCAD 2009\Sample

    </AcDxDwgSummaryDwgLocation>

    <AcDxDwgSummaryDwgSize>472480</AcDxDwgSummaryDwgSize>

    <AcDxDwgSummaryDwgCreated>

      2007-01-03T00:44:20+01:00

    </AcDxDwgSummaryDwgCreated>

    <AcDxDwgSummaryDwgModified>

      2007-01-03T00:44:20+01:00

    </AcDxDwgSummaryDwgModified>

    <AcDxDwgSummaryDwgAccessed>

      2008-03-03T11:40:53.796875+01:00

    </AcDxDwgSummaryDwgAccessed>

    <AcDxDwgSummaryDwgTitle />

    <AcDxDwgSummaryDwgSubject />

    <AcDxDwgSummaryDwgAuthor />

    <AcDxDwgSummaryDwgKeywords />

    <AcDxDwgSummaryDwgComments />

    <AcDxDwgSummaryDwgHyperLinkBase />

    <AcDxDwgSummaryDwgLastSavedBy>

      thompsl

    </AcDxDwgSummaryDwgLastSavedBy>

    <AcDxDwgSummaryDwgRevisionNumber />

    <AcDxDwgSummaryDwgTotalEditingTime>

      1179

    </AcDxDwgSummaryDwgTotalEditingTime>

    <AcDxEntityHyperlink />

  </My_Data_Extract>

  <!-- Stuff deleted from the XML output -->

  <!-- ... -->

</DocumentElement>

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200e551b064388833

Listed below are links to weblogs that reference Extracting XML data from drawings using a new .NET API in AutoCAD 2009:

blog comments powered by Disqus

Feed/Share

10 Random Posts