sercanmetu

shapefile

Discussion created by sercanmetu on Nov 24, 2011
Hello everyone

I want to write a shapefile that consists of more than one multipatch features in c#.

How can i do this job?

Can anyone help me ? A code snippet will be very nice.

Thanks in advance.

Here is my code that handles just one multipatch feature.




  string folder = "D:\\";
           string nameOfShapeFile = "multipatch.shp";
           string shapeFieldName = "Shape";
           try
           {
               IWorkspaceFactory workspaceFactory = null;
               workspaceFactory = new ShapefileWorkspaceFactory();
               IWorkspace workspace = workspaceFactory.OpenFromFile(folder, 0);
               IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;

               IFields fields = null;
               IFieldsEdit fieldsEdit = null;
               fields = new Fields();
               fieldsEdit = (IFieldsEdit)fields;

               IField field = null;
               IFieldEdit fieldEdit = null;
               field = new Field();///###########
               fieldEdit = (IFieldEdit)field;
               fieldEdit.Name_2 = "Shape";
               fieldEdit.Type_2 = (esriFieldType.esriFieldTypeGeometry);
               IGeometryDef geomDef = null;
               IGeometryDefEdit geomDefEdit = null;
               geomDef = new GeometryDef();///#########
               geomDefEdit = (IGeometryDefEdit)geomDef;

               ISpatialReferenceFactory3 spatialReferenceFactory = (ISpatialReferenceFactory3)new SpatialReferenceEnvironment();

               geomDefEdit.HasZ_2 = true;
               geomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryMultiPatch;
               geomDefEdit.SpatialReference_2 = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)ESRI.ArcGIS.Geometry.esriSRGeoCSType.esriSRGeoCS_WGS1984);

               fieldEdit.GeometryDef_2 = geomDef;
               fieldsEdit.AddField(field);

               //Add another miscellaneous text field
               field = new Field();
               fieldEdit = (IFieldEdit)field;
               fieldEdit.Length_2 = 30;
               fieldEdit.Name_2 = "Id";
               fieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
               fieldsEdit.AddField(field);



               IFeatureClass featClass = null;
               featClass = featureWorkspace.CreateFeatureClass(nameOfShapeFile, fields, null, null, esriFeatureType.esriFTSimple, shapeFieldName, "");
               IFeature feature = featClass.CreateFeature();

            


               IGeometryCollection coll = (IGeometryCollection)new MultiPatch();
               ((IZAware)coll).ZAware = true;

               IPoint uppercenter = new ESRI.ArcGIS.Geometry.Point();
               ((IZAware)uppercenter).ZAware = true;

               uppercenter.PutCoords(center.X, center.Y);
               uppercenter.Z = center.Z + height;

               IPointCollection pcoll = new Polygon();
               ((IZAware)pcoll).ZAware = true;

               IPoint[] points = new IPoint[36];
               IPoint[] points2 = new IPoint[36];
               for (int i = 0; i < 36; i++)
               {
                   points[i] = new ESRI.ArcGIS.Geometry.Point();
                   ((IZAware)points[i]).ZAware = true;

                   points2[i] = new ESRI.ArcGIS.Geometry.Point();
                   ((IZAware)points2[i]).ZAware = true;
               }

               double newlon, newlat;

                for (int i = 0; i < 360; i += 10)
                {
                    shiftCoordinate(center.X, center.Y, i, width, out newlon, out newlat);
                    points[i / 10].PutCoords(newlon, newlat);
                    points[i / 10].Z = center.Z;
                }

               for (int i = 0; i <= 35; i++)
               {
                   pcoll.AddPoint(points[i], ref _missing, ref _missing);
               }


               IExtrude2 ext = (IExtrude2)new GeometryEnvironment();
               IGeometry geom = ext.Extrude(1000000, pcoll as IGeometry);

               IMultiPatch patch = geom as IMultiPatch;


               feature.Shape = (IGeometry)patch;

               feature.Store();


           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

Outcomes