public void CreateRoutes(IFeatureClass pLineFC, string InputRouteIDFieldName, string fromMeasureField, string toMeasureField) { try { IWorkspace selectionContainer = (pLineFC as IDataset).Workspace; IFeatureClassName outputFClassName = new FeatureClassNameClass(); IDataset dataset = (IDataset)selectionContainer; IWorkspaceName fullName = (IWorkspaceName)dataset.FullName; IDatasetName name3 = (IDatasetName)outputFClassName; name3.WorkspaceName = fullName; name3.Name = "CreateRoutes"; IFields fields = pLineFC.Fields; int index = fields.FindField(pLineFC.ShapeFieldName); IClone geometryDef = (IClone)fields.get_Field(index).GeometryDef; IGeometryDef outputGeometryDef = (IGeometryDef)geometryDef.Clone(); ((ISpatialReference2)outputGeometryDef.SpatialReference).SetMFalseOriginAndUnits(-1000.0, 1000.0); IQueryFilter queryFilter = new QueryFilterClass { WhereClause = "[ROUTE1] <> 0" }; ISelectionSet2 set = (ISelectionSet2) pLineFC.Select(queryFilter, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, selectionContainer); IRouteMeasureCreator creator = new RouteMeasureCreatorClass { InputFeatureSelection = set, InputRouteIDFieldName = InputRouteIDFieldName }; IEnumBSTR mbstr = creator.CreateUsing2Fields(fromMeasureField, toMeasureField, outputFClassName, outputGeometryDef, "", null); for (string str = mbstr.Next(); str != null; str = mbstr.Next()) { } } catch (COMException exception) { MessageBox.Show(exception.Message, "COM Error: " + exception.ErrorCode.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (Exception exception2) { MessageBox.Show(exception2.Message, ".NET Error: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
public void CreateRoutesUsing2Fields(string pAccessWS, string sLineFC, string sOutRouteFC, string sWhereClause, string sRouteIDField, string sFromMeasureField, string sToMeasureField) { try { IWorkspaceFactory wsf = new AccessWorkspaceFactoryClass(); IWorkspace ws = wsf.OpenFromFile(pAccessWS, 0); IFeatureWorkspace fws = (IFeatureWorkspace)ws; IFeatureClass lineFC = fws.OpenFeatureClass(sLineFC); // Create an output feature class name object. We'll write to a stand alone feature class in the // the same workspace as the inputs IDataset ds = (IDataset)ws; IWorkspaceName outWSN = (IWorkspaceName)ds.FullName; IFeatureClassName outFCN = new FeatureClassNameClass(); IDatasetName outDSN = (IDatasetName)outFCN; outDSN.WorkspaceName = outWSN; outDSN.Name = sOutRouteFC; //This name should not already exist // Create a geometry definition for the new feature class. For the most part, we will copy the geometry // definition from the input lines. We'll explicitly set the M Domain, however. You should always set an // M Domain that is appropriate to your data. What is below is just a sample. IFields flds = lineFC.Fields; Int32 i = flds.FindField(lineFC.ShapeFieldName); IField fld = flds.get_Field(i); IClone GDefclone = (IClone)fld.GeometryDef; IGeometryDef gDef = (IGeometryDef)GDefclone.Clone(); ISpatialReference2 srRef = (ISpatialReference2)gDef.SpatialReference; srRef.SetMFalseOriginAndUnits(-1000, 10000); // Create a selection set to limit the number of lines that will be used to create routes IQueryFilter qFilt = new QueryFilterClass(); qFilt.WhereClause = sWhereClause; ISelectionSet2 selSet = (ISelectionSet2)lineFC.Select(qFilt, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, ws); // Create a new RouteMeasureCreator object. Note that below, we use the selection set and not the // InputFeatureClass property IRouteMeasureCreator routeCreator = new RouteMeasureCreatorClass(); routeCreator.InputFeatureSelection = selSet; routeCreator.InputRouteIDFieldName = sRouteIDField; IEnumBSTR errors = routeCreator.CreateUsing2Fields(sFromMeasureField, sToMeasureField, outFCN, gDef, "", null); // The results of running CreatingUsing2Fields returns IEnumBSTR, which is a container // for a list of error strings indicating why certain lines could not be used to create routes. string sError = errors.Next(); do { System.Windows.Forms.MessageBox.Show(sError); sError = errors.Next(); } while (sError.Length != 0); } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); } }