从dwg文件中按图层名称提取指定图层到对应arcgis的矢量图层

思路:获取dwg文件,由于dwg文件没有要素类的概念,用arcengine读取的时候,读取的图层分为四类:点线面和注记;具体代码如下:

       private void GetLayer()   
        {
        string sFolderPath = textBox1.Text.Substring(0, textBox1.Text.LastIndexOf("\\"));
        string sFeaClsName = textBox1.Text.Substring(textBox1.Text.LastIndexOf("\\") + 1);
        IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactoryClass();
        IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(sFolderPath, 0);


        IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(sFeaClsName);
        IFeatureClassContainer pFeatureClassContainer = (IFeatureClassContainer)pFeatureDataset;

        nameName1 = new string[allname.Count];
        for (int i = 0; i < allname.Count; i++)
        {
            nameName1[i] = allname[i];
        }

        QueryFilter pfile = new QueryFilterClass();
        pfile.WhereClause = "Layer = "  + "'1-地形'";

        for (int i = 0; i < pFeatureClassContainer.ClassCount; i++)
        {
            IFeatureClass featureClass = pFeatureClassContainer.get_Class(i);
            if (featureClass.FeatureType != esriFeatureType.esriFTAnnotation )
            {
                int c = featureClass.Fields.FieldCount;

                if (featureClass.ShapeType == esriGeometryType.esriGeometryPoint)
                {
                    IFeatureCursor pcur = featureClass.Search(pfile, false);
                    if (pcur.NextFeature() != null)
                    {
                        SaveFeatureclass(featureClass, pcur, "1-地形");
                    }
                }
            }
        }
    }

    private void SaveFeatureclass(IFeatureClass featureClass, IFeatureCursor pfeatureCur, string featureclassName)
    {
        try
        {
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pFWS = (IFeatureWorkspace)pWSF.OpenFromFile(textBox2.Text, 0);

            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit;
            pFieldsEdit = (IFieldsEdit)pFields;

            for (int i = 0; i < featureClass.Fields.FieldCount; i++)
            {
                pFieldsEdit.AddField(featureClass.Fields.Field[i]);
            }
            IFeatureClass pclass = pFWS.CreateFeatureClass(featureclassName, pFields, featureClass.CLSID, featureClass.EXTCLSID, featureClass.FeatureType, "Shape", "");
            InsertFeaturesUsingCursor(pfeatureCur, pclass);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message,"提示");
        }
       
    }

    public void InsertFeaturesUsingCursor(IFeatureCursor cursor, IFeatureClass targetFeatureClass)
    {
        using (ComReleaser comReleaser = new ComReleaser())
        {
            IFeatureBuffer featureBuffer = targetFeatureClass.CreateFeatureBuffer();
            comReleaser.ManageLifetime(featureBuffer);

            IFeatureCursor insertCursor = targetFeatureClass.Insert(true);
            comReleaser.ManageLifetime(insertCursor);

            IFeature sourceFeature = cursor.NextFeature();

            while (sourceFeature != null)
            {
                //如果是线或面要素类需要执行下Simplify,这里用的点要素类,不做验证了
                featureBuffer.Shape = sourceFeature.ShapeCopy;

                for (int i = 0; i < sourceFeature.Fields.FieldCount; i++)
                {
                    IField field = sourceFeature.Fields.get_Field(i);
                    if (field.Type != esriFieldType.esriFieldTypeOID && field.Type != esriFieldType.esriFieldTypeGeometry && field.Type != esriFieldType.esriFieldTypeGlobalID && field.Type != esriFieldType.esriFieldTypeGUID)
                    {
                        string fieldName = field.Name;
                        int index = featureBuffer.Fields.FindField(fieldName);
                        if (index > -1)
                            featureBuffer.set_Value(index, sourceFeature.get_Value(i));
                    }

                }
                insertCursor.InsertFeature(featureBuffer);
                sourceFeature = cursor.NextFeature();
            }

            // Flush the buffer to the geodatabase.
            insertCursor.Flush();
            ComReleaser.ReleaseCOMObject(cursor);

        }
        IFeatureClassManage targetFeatureClassManage = targetFeatureClass as IFeatureClassManage;
        targetFeatureClassManage.UpdateExtent();
    }

你可能感兴趣的:(ArcGIS开发)