Tuesday, August 24, 2010

Importing Data from CAD (.dwg) File into your Geodatabase

One of the most helpful and wanted functionalities for clients

is integration through multiple platforms.For GIS clients it’s very important to import from CAD file (.dwg). and here is a web service code solution.

 

[WebMethod]

    public void loadCadFile()

    {

ESRI.ArcGIS.esriSystem.IAoInitialize ao = new ESRI.ArcGIS.esriSystem.AoInitialize();

ao.Initialize(ESRI.ArcGIS.esriSystem.esriLicenseProductCode.esriLicenseProductCodeArcInfo);

IWorkspaceFactory2 workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();

IWorkspace workspace = workspaceFactory.OpenFromFile(“[.mdf file]”, 0);

IWorkspaceFactory pWorkspaceFact = new CadWorkspaceFactory();

IWorkspace pWorkSpace = pWorkspaceFact.OpenFromFile(System.IO.Path.GetDirectoryName("[File Path]"), 0);

IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;

IFeatureClass FCBuildings = featureWorkspace.OpenFeatureClass("[Layer Name in .mdb file]"); // Layer Name

IFeature feature;

object ob1 = Type.Missing, ob2 = Type.Missing;

IWorkspaceEdit workSpaceEditor = (IWorkspaceEdit)featureWorkspace;

workSpaceEditor.StartEditing(false);

workSpaceEditor.StartEditOperation();

IFeatureWorkspace FWS;

FWS = pWorkSpace as IFeatureWorkspace;

IFeatureClass FCIs = FWS.OpenFeatureClass("[filename:LayerName]");// Example “SjCad.dwg:Polygons”  || “SjCad.dwg:Points”

IFeatureCursor FCur = FCIs.Search(null, false);

while (1 == 1)

{

        IFeature Feat = FCur.NextFeature();

        if (Feat != null)

        {

                Polygon Poly = new Polygon();

               feature = FCBuildings.CreateFeature();

                feature.Shape = Feat.Shape;

                feature.Store();

          }

         else break;

}

workSpaceEditor.StopEditOperation();

workSpaceEditor.StopEditing(true);

}

 

Thank you,

      Sj