public bool OnContextMenu(int x, int y) { System.Drawing.Point pt = this.PointToClient(System.Windows.Forms.Cursor.Position); //Get the active category IEngineNAWindowCategory2 activeCategory = m_naWindow.ActiveCategory as IEngineNAWindowCategory2; if (activeCategory == null) { return(false); } MenuItem separator = new MenuItem("-"); miLoadLocations.Enabled = false; miClearLocations.Enabled = false; // in order for the AddItem choice to appear in the context menu, the class // should be an input class, and it should not be editable INAClassDefinition pNAClassDefinition = activeCategory.NAClass.ClassDefinition; if (pNAClassDefinition.IsInput) { miLoadLocations.Enabled = true; miClearLocations.Enabled = true; // canEditShape should be false for AddItem to Apply (default is false) // if it's a StandaloneTable canEditShape is implicitly false (there's no shape to edit) bool canEditShape = false; IFields pFields = pNAClassDefinition.Fields; int nField = -1; nField = pFields.FindField("Shape"); if (nField >= 0) { int naFieldType = 0; naFieldType = pNAClassDefinition.get_FieldType("Shape"); // determining whether or not the shape field can be edited consists of running a bitwise comparison // on the FieldType of the shape field. See the online help for a list of the possible field types. // For our case, we want to verify that the shape field is an input field. If it is an input field, // then we do NOT want to display the Add Item menu option. canEditShape = ((naFieldType & (int)esriNAFieldType.esriNAFieldTypeInput) == (int)esriNAFieldType.esriNAFieldTypeInput) ? true : false; } if (!canEditShape) { contextMenu1.MenuItems.Add(separator); contextMenu1.MenuItems.Add(miAddItem); } } contextMenu1.Show(this, pt); // even if the miAddItem menu item has not been added, Remove() won't crash. contextMenu1.MenuItems.Remove(separator); contextMenu1.MenuItems.Remove(miAddItem); return(true); }
public bool ShowModal(IMapControl3 mapControl, IEngineNetworkAnalystEnvironment naEnv) { // Initialize variables m_okClicked = false; m_listDisplayTable = new System.Collections.ArrayList(); // Get the NALayer and NAContext INALayer naLayer = naEnv.NAWindow.ActiveAnalysis; INAContext naContext = naLayer.Context; //Get the active category IEngineNAWindowCategory2 activeCategory = naEnv.NAWindow.ActiveCategory as IEngineNAWindowCategory2; if (activeCategory == null) { return(false); } INAClass naClass = activeCategory.NAClass; IDataset naDataset = naClass as IDataset; IDataLayer pDataLayer = activeCategory.DataLayer; ILayer pLayer = pDataLayer as ILayer; IFeatureLayer pFeatureLayer = pDataLayer as IFeatureLayer; IStandaloneTable pStandaloneTable = pDataLayer as IStandaloneTable; esriGeometryType targetGeoType = esriGeometryType.esriGeometryNull; String dataLayerName = ""; if (pFeatureLayer != null) { if (pLayer.Valid) { IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; if (pFeatureClass != null) { targetGeoType = pFeatureClass.ShapeType; dataLayerName = pLayer.Name; } } } else if (pStandaloneTable != null) { dataLayerName = pStandaloneTable.Name; } if (dataLayerName.Length == 0) { return(false); } this.Text = "Load Items into " + dataLayerName; // Loop through all the sourcedisplayTables having targetGeoType to the combo box and the // list of displayTables IEnumLayer sourceLayers = null; ILayer sourceLayer = null; IDisplayTable sourceDisplayTable = null; UID searchInterfaceUID = new UID(); if (targetGeoType != esriGeometryType.esriGeometryNull) { searchInterfaceUID.Value = typeof(IFeatureLayer).GUID.ToString("B"); sourceLayers = mapControl.Map.get_Layers(searchInterfaceUID, true); sourceLayer = sourceLayers.Next(); while (sourceLayer != null) { IFeatureLayer sourceFeatureLayer = sourceLayer as IFeatureLayer; sourceDisplayTable = sourceLayer as IDisplayTable; if ((sourceFeatureLayer != null) && (sourceDisplayTable != null)) { IFeatureClass sourceFeatureClass = sourceFeatureLayer.FeatureClass; esriGeometryType sourceGeoType = sourceFeatureClass.ShapeType; if ((sourceGeoType == targetGeoType) || (targetGeoType == esriGeometryType.esriGeometryPoint && sourceGeoType == esriGeometryType.esriGeometryMultipoint)) { // Add the layer name to the combobox and the layer to the list cboInputData.Items.Add(sourceLayer.Name); m_listDisplayTable.Add(sourceDisplayTable); } } sourceLayer = sourceLayers.Next(); } } else //if (targetGeoType == esriGeometryType.esriGeometryNull) { IStandaloneTableCollection sourceStandaloneTables = mapControl.Map as IStandaloneTableCollection; IStandaloneTable sourceStandaloneTable = null; sourceDisplayTable = null; int count = 0; if (sourceStandaloneTables != null) { count = sourceStandaloneTables.StandaloneTableCount; } for (int i = 0; i < count; ++i) { sourceStandaloneTable = sourceStandaloneTables.get_StandaloneTable(i); sourceDisplayTable = sourceStandaloneTable as IDisplayTable; if ((sourceStandaloneTable != null) && (sourceDisplayTable != null)) { // Add the table name to the combobox and the layer to the list cboInputData.Items.Add(sourceStandaloneTable.Name); m_listDisplayTable.Add(sourceDisplayTable); } } searchInterfaceUID.Value = typeof(INALayer).GUID.ToString("B"); sourceLayers = mapControl.Map.get_Layers(searchInterfaceUID, true); sourceLayer = sourceLayers.Next(); while (sourceLayer != null) { INALayer sourceNALayer = sourceLayer as INALayer; if (sourceNALayer != null) { sourceStandaloneTables = sourceNALayer as IStandaloneTableCollection; sourceStandaloneTable = null; sourceDisplayTable = null; count = 0; if (sourceStandaloneTables != null) { count = sourceStandaloneTables.StandaloneTableCount; } for (int i = 0; i < count; ++i) { sourceStandaloneTable = sourceStandaloneTables.get_StandaloneTable(i); sourceDisplayTable = sourceStandaloneTable as IDisplayTable; if ((sourceStandaloneTable != null) && (sourceDisplayTable != null)) { // Add the table name to the combobox and the layer to the list cboInputData.Items.Add(sourceStandaloneTable.Name); m_listDisplayTable.Add(sourceDisplayTable); } } } sourceLayer = sourceLayers.Next(); } } //Select the first display table from the list if (cboInputData.Items.Count > 0) { cboInputData.SelectedIndex = 0; } // Show the window this.ShowDialog(); // If we selected a layer and clicked OK, load the locations if (m_okClicked && (cboInputData.SelectedIndex >= 0)) { // Get a cursor on the source display table (either though the selection set or table) // Use IDisplayTable because it accounts for joins, querydefs, etc. // IDisplayTable is implemented by FeatureLayers and StandaloneTables. // IDisplayTable displayTable = m_listDisplayTable[cboInputData.SelectedIndex] as IDisplayTable; ICursor cursor; if (chkUseSelection.Checked) { ISelectionSet selSet; selSet = displayTable.DisplaySelectionSet; selSet.Search(null, false, out cursor); } else { cursor = displayTable.SearchDisplayTable(null, false); } // Setup NAClassLoader and Load Locations INAClassLoader2 naClassLoader = new NAClassLoader() as INAClassLoader2; naClassLoader.Initialize(naContext, naDataset.Name, cursor); // Avoid loading network locations onto non-traversable portions of elements INALocator3 locator = naContext.Locator as INALocator3; locator.ExcludeRestrictedElements = true; locator.CacheRestrictedElements(naContext); int rowsIn = 0; int rowsLocated = 0; naClassLoader.Load(cursor, null, ref rowsIn, ref rowsLocated); return(true); } return(false); }
private void miAddItem_Click(object sender, System.EventArgs e) { //可以双击该项来编辑属性 //只为新项填充initDefaultValues方法中的默认值和自动生成的名称值。 IMapControl3 mapControl = (IMapControl3)axMapControl1.Object; IEngineNAWindowCategory2 activeCategory = m_naWindow.ActiveCategory as IEngineNAWindowCategory2; IDataLayer pDataLayer = activeCategory.DataLayer; // 在类中创建一个新行并填充并初始默认值 ITable table = pDataLayer as ITable; IRow row = table.CreateRow(); IRowSubtypes rowSubtypes = row as IRowSubtypes; rowSubtypes.InitDefaultValues(); // 自动生成显示名称 IFeatureLayer ipFeatureLayer = activeCategory.Layer as IFeatureLayer; IStandaloneTable ipStandaloneTable = pDataLayer as IStandaloneTable; string name = ""; if (ipFeatureLayer != null) { name = ipFeatureLayer.DisplayField; } else if (ipStandaloneTable != null) { name = ipStandaloneTable.DisplayField; } //如果显示字段为空字符串或不代表NaClass上的实际字段,则跳过自动生成 string currentName = ""; int fieldIndex = row.Fields.FindField(name); if (fieldIndex >= 0) { currentName = row.get_Value(fieldIndex) as string; if (currentName.Length <= 0) { row.set_Value(fieldIndex, "Item" + ++autogenInt); } } INAClassDefinition naClassDef = activeCategory.NAClass.ClassDefinition; if (naClassDef.Name == "OrderPairs") { fieldIndex = row.Fields.FindField("SecondOrderName"); if (fieldIndex >= 0) { string secondName = row.get_Value(fieldIndex) as string; if (secondName.Length <= 0) { row.set_Value(fieldIndex, "Item" + ++autogenInt); } } } row.Store(); // 向naclass添加项 INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit; contextEdit.ContextChanged(); // 刷新 NAWindow 和屏幕界面 INALayer naLayer = m_naWindow.ActiveAnalysis; mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, mapControl.Extent); m_naWindow.UpdateContent(m_naWindow.ActiveCategory); }
private void addItemToolStripMenuItem_Click(object sender, EventArgs e) { IMapControl3 mapControl = (IMapControl3)axMapControl1.Object; IEngineNAWindowCategory2 activeCategory = m_naWindow.ActiveCategory as IEngineNAWindowCategory2; IDataLayer pDataLayer = activeCategory.DataLayer; // In order to add an item, we need to create a new row in the class and populate it // with the initial default values for that class. ITable table = pDataLayer as ITable; IRow row = table.CreateRow(); IRowSubtypes rowSubtypes = row as IRowSubtypes; rowSubtypes.InitDefaultValues(); IFeatureLayer ipFeatureLayer = activeCategory.Layer as IFeatureLayer; IStandaloneTable ipStandaloneTable = pDataLayer as IStandaloneTable; string name = ""; if (ipFeatureLayer != null) { name = ipFeatureLayer.DisplayField; } else if (ipStandaloneTable != null) { name = ipStandaloneTable.DisplayField; } string currentName = ""; int fieldIndex = row.Fields.FindField(name); if (fieldIndex >= 0) { currentName = row.get_Value(fieldIndex) as string; if (currentName.Length <= 0) { row.set_Value(fieldIndex, "Item" + ++autogenInt); } } INAClassDefinition naClassDef = activeCategory.NAClass.ClassDefinition; if (naClassDef.Name == "OrderPairs") { fieldIndex = row.Fields.FindField("SecondOrderName"); if (fieldIndex >= 0) { string secondName = row.get_Value(fieldIndex) as string; if (secondName.Length <= 0) { row.set_Value(fieldIndex, "Item" + ++autogenInt); } } } row.Store(); // notify that the context has changed because we have added an item to a NAClass within it INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit; contextEdit.ContextChanged(); // refresh the NAWindow and the Screen INALayer naLayer = m_naWindow.ActiveAnalysis; mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, mapControl.Extent); m_naWindow.UpdateContent(m_naWindow.ActiveCategory); }