/// <summary> /// The on comboBox selection change event. This creates a new table that lists the assignments for the specified category. This table is added to the map, selected in the TOC, and opened. /// </summary> /// <param name="item">The newly selected combo box item</param> protected override async void OnSelectionChange(ComboBoxItem item) { if (item == null) { return; } if (string.IsNullOrEmpty(item.Text)) { return; } if (myLayer == null) { return; } //Construct the name of our table for the category assignment report string baseCategoryReportTableName = "CategoryAssignments_" + item.Text; string categoryReportTableName = baseCategoryReportTableName.Replace(" ", "_"); bool needToCreateTable = true; bool needToAddStandaloneTable = true; // Switch to the MCT to access the geodatabase await QueuedTask.Run(() => { // Check if the table exists using (Geodatabase projectWorkspace = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(Project.Current.DefaultGeodatabasePath)))) { try { using (Table categoryReportTable = projectWorkspace.OpenDataset <Table>(categoryReportTableName)) { // Table exists, we do not need to create it... needToCreateTable = false; // .. but we should delete the existing contents categoryReportTable.DeleteRows(new QueryFilter()); // Check to see if a Standalone table exists in the map bool standaloneTableFound = false; ReadOnlyObservableCollection <StandaloneTable> initialStandaloneTables = MapView.Active.Map.StandaloneTables; foreach (StandaloneTable standaloneTable in initialStandaloneTables) { if (standaloneTable.Name == categoryReportTableName) { standaloneTableFound = true; } } // Since there is already a StandaloneTable that references our category table in the map, we don't need to add it needToAddStandaloneTable = !standaloneTableFound; } } catch { //Table doesn't exist. Not an error, but we will have to create it } } }); // Create the category report table if (needToCreateTable) { // Create table IReadOnlyList <string> createParams = Geoprocessing.MakeValueArray(new object[] { Project.Current.DefaultGeodatabasePath, categoryReportTableName, null, null }); IGPResult result = await Geoprocessing.ExecuteToolAsync("management.CreateTable", createParams); if (result.IsFailed) { MessageBox.Show("Unable to create category assignment table in project workspace", "Category Assignments"); return; } // Add field for feature class alias IReadOnlyList <string> addFieldParams = Geoprocessing.MakeValueArray(new object[] { categoryReportTableName, "FeatureClassAlias", "TEXT", null, null, 32, "Feature Class", "NULLABLE", "NON_REQUIRED", null }); result = await Geoprocessing.ExecuteToolAsync("management.AddField", addFieldParams); if (result.IsFailed) { MessageBox.Show("Unable to modify schema of category assignment table in project workspace", "Category Assignments"); return; } // Add field for Asset Group name addFieldParams = Geoprocessing.MakeValueArray(new object[] { categoryReportTableName, "AssetGroupName", "TEXT", null, null, 256, "Asset Group Name", "NULLABLE", "NON_REQUIRED", null }); result = await Geoprocessing.ExecuteToolAsync("management.AddField", addFieldParams); if (result.IsFailed) { MessageBox.Show("Unable to modify schema of category assignment table in project workspace", "Category Assignments"); return; } // Add field for Asset Type name addFieldParams = Geoprocessing.MakeValueArray(new object[] { categoryReportTableName, "AssetTypeName", "TEXT", null, null, 256, "Asset Type Name", "NULLABLE", "NON_REQUIRED", null }); result = await Geoprocessing.ExecuteToolAsync("management.AddField", addFieldParams); if (result.IsFailed) { MessageBox.Show("Unable to modify schema of category assignment table in project workspace", "Category Assignments"); return; } needToAddStandaloneTable = false; //creating a table automatically adds it to the map } // Populate table // Again, we need to switch to the MCT to execute geodatabase and utility network code await QueuedTask.Run(() => { using (Geodatabase projectWorkspace = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(Project.Current.DefaultGeodatabasePath)))) using (Table categoryReportTable = projectWorkspace.OpenDataset <Table>(categoryReportTableName)) using (UtilityNetwork utilityNetwork = UtilityNetworkSamples.UtilityNetworkUtils.GetUtilityNetworkFromLayer(myLayer)) using (UtilityNetworkDefinition utilityNetworkDefinition = utilityNetwork.GetDefinition()) { IReadOnlyList <NetworkSource> networkSources = utilityNetworkDefinition.GetNetworkSources(); // Step through each NetworkSource foreach (NetworkSource networkSource in networkSources) { IReadOnlyList <AssetGroup> assetGroups = networkSource.GetAssetGroups(); // Step through each AssetGroup foreach (AssetGroup assetGroup in assetGroups) { IReadOnlyList <AssetType> assetTypes = assetGroup.GetAssetTypes(); // Step through each AssetType foreach (AssetType assetType in assetTypes) { // Check to see if this AssetType is assigned the Category we are looking for IReadOnlyList <string> assignedCategoryList = assetType.CategoryList; foreach (string assignedCategory in assignedCategoryList) { if (assignedCategory == item.Text) { // Our Category is assigned to this AssetType. Create a row to store in the category report table using (FeatureClass networkSourceFeatureClass = utilityNetwork.GetTable(networkSource) as FeatureClass) using (FeatureClassDefinition networkSourceFeatureClassDefinition = networkSourceFeatureClass.GetDefinition()) using (RowBuffer rowBuffer = categoryReportTable.CreateRowBuffer()) { rowBuffer["FeatureClassAlias"] = networkSourceFeatureClassDefinition.GetAliasName(); rowBuffer["AssetGroupName"] = assetGroup.Name; rowBuffer["AssetTypeName"] = assetType.Name; categoryReportTable.CreateRow(rowBuffer).Dispose(); } } } } } } // If necessary, add our category report table to the map as a standalone table if (needToAddStandaloneTable) { IStandaloneTableFactory tableFactory = StandaloneTableFactory.Instance; tableFactory.CreateStandaloneTable(categoryReportTable, MapView.Active.Map); } } }); // Open category report stand alone table into a window ReadOnlyObservableCollection <StandaloneTable> standaloneTables = MapView.Active.Map.StandaloneTables; foreach (StandaloneTable standaloneTable in standaloneTables) { if (standaloneTable.Name == categoryReportTableName) { FrameworkApplication.Panes.OpenTablePane(standaloneTable, TableViewMode.eAllRecords); } } }