Monday, January 3, 2011

Working with CAD files in ArcObjects

 

Cad files are files used by architects to draw buildings sketches. The files are with the extension of dwg.

As always with ESRI I started by copying their examples and trying them for myself. Taken from:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/How_to_return_CAD_drawing_layer_properties/0001000003mp000000/

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/How_to_access_members_that_control_the_CAD_drawing_dataset/000100000m01000000/

I extracted the code to create IWorkspace to my WorkspaceProvider:

        public CadWorkspaceUtils GetCadWorkspaceUtils(string path)

        {

            var workspaceFactory = new CadWorkspaceFactory();

            var workspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(path, 0);

            return new CadWorkspaceUtils(workspace);

        }

 

I have created a new class – CadWorkspaceUtils that inherits from WorkspaceUtils

 with a function to get back the geometries in the file:

        public List<IGeometry> GetGeometries(string fileName, GeometryType type)

        {

            var featureClassName = GetFeatureClassName(fileName, type);

 

            var result = new List<IGeometry>();

 

            DoActionOnSelectFeatures(featureClassName, "",

                feature =>

                {

                    var shape = feature.Shape.Copy();

                    shape.SpatialReference = GeometryUtils.CreateWgsSpatialReference();

                    result.Add(shape);

                });

 

            return result;

        }

Helper method:

        private string GetFeatureClassName(string fileName, GeometryType type)

        {

 

            if (type != GeometryType.Polyline)

                throw new NotImplementedException("The method is only implemented for polyline geometries");

 

            return String.Concat(fileName, ":Polyline");

        }

 

Thank god I only need polyline type because I haven't seen an example with anything different.

 

 

// TODO: Make links user friendly

//TODO: Post about WorkspaceUtils first 

Resources:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/CAD_data/000100000mt1000000/

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//001900000064000000

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Members/0047000033sn000000/

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00120000001z000000.htm