private IFeatureClass CreateGridFeatureClass(Grid grid, IFeatureWorkspace workspace, IFeatureDataset dataset, string featureClassName) { try { IFields fields; if (workspace is IGxFolder) { MessageBox.Show(@"Creating Shapefile " + featureClassName + @" in " + ((IWorkspace)workspace).PathName); if (!ValidateShapefileName()) { MessageBox.Show(@"Shapefile may exist, name may be too long," + @"folder may not exist, folder may be readonly,", @"Error"); return null; } fields = CreateShapefileFields(); } else { string msg = dataset == null ? string.Format("Creating {0} in {1}", featureClassName, ((IWorkspace)workspace).PathName) : string.Format("Creating {0} in {1}\\{2}", featureClassName, ((IWorkspace)workspace).PathName, dataset.Name); MessageBox.Show(msg); if (!ValidateGdbfileName()) return null; fields = CreateGdbFields(); } IFeatureClass generatedFeatureClass = CreateFeatureClass((IWorkspace2)workspace, dataset, featureClassName, fields, null, null, ""); if (generatedFeatureClass == null) return null; PutGridInFeatureClass(generatedFeatureClass, grid); return generatedFeatureClass; } catch (Exception) { //Debug.Print("Exception Creating Feature Class: {0}",ex); return null; } }
private static void PutGridInFeatureClass(IFeatureClass featureClass, Grid grid) { using (var comReleaser = new ComReleaser()) { // Create a feature buffer. IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(featureBuffer); // Create an insert cursor. IFeatureCursor insertCursor = featureClass.Insert(true); comReleaser.ManageLifetime(insertCursor); // All of the features to be created are classified as Primary Highways. int colFieldIndex = featureClass.FindField("Col"); int rowFieldIndex = featureClass.FindField("Row"); int colLabelFieldIndex = featureClass.FindField("Col_Label"); int rowLabelFieldIndex = featureClass.FindField("Row_Label"); int cellLabelFieldIndex = featureClass.FindField("Cell_Label"); int pageFieldIndex = featureClass.FindField("Page"); foreach (Cell cell in grid.Cells) { featureBuffer.Shape = cell.Shape; featureBuffer.Value[colFieldIndex] = cell.Column; featureBuffer.Value[rowFieldIndex] = cell.Row; featureBuffer.Value[colLabelFieldIndex] = cell.ColumnLabel; featureBuffer.Value[rowLabelFieldIndex] = cell.RowLabel; featureBuffer.Value[cellLabelFieldIndex] = cell.Label; featureBuffer.Value[pageFieldIndex] = cell.Page; insertCursor.InsertFeature(featureBuffer); } // Flush the buffer to the geodatabase. insertCursor.Flush(); } }