internal void OnDeleteRouteButtonClick() { QueuedTask.Run(() => { using (var cursor = RoutesStandaloneTable.GetSelection().Search(null)) { var operation = new EditOperation(); cursor.MoveNext(); var routeRow = cursor.Current; string routeID = (string)routeRow[RouteID]; string routeName = (string)routeRow[RouteName]; operation.Name = $"Delete route: {routeName}"; operation.Delete(RoutesStandaloneTable, routeRow.GetObjectID()); var query = new QueryFilter() { WhereClause = $"{RouteID} = '{routeID}'" }; using (var segsCursor = RouteToTrailSegmentsTable.Search(query)) using (var headsCursor = RouteToTrailheadsTable.Search(query)) { while (segsCursor.MoveNext()) { operation.Delete(RouteToTrailSegmentsTable, segsCursor.Current.GetObjectID()); } while (headsCursor.MoveNext()) { operation.Delete(RouteToTrailheadsTable, headsCursor.Current.GetObjectID()); } } operation.Execute(); if (operation.IsSucceeded) { Notification notification = new Notification(); notification.Title = FrameworkApplication.Title; notification.Message = $"Route: \"{routeName}\" deleted successfully!"; FrameworkApplication.AddNotification(notification); RoutesStandaloneTable.ClearSelection(); } else { MessageBox.Show(operation.ErrorMessage); } } }); }
public async void AddNewRoute(string routeName) { var map = MapView.Active.Map; if (!BuildOnSelect && SegmentsLayer.SelectionCount == 0) { MessageBox.Show("At least one segment must be selected!"); return; } await QueuedTask.Run(async() => { using (var routesTable = RoutesStandaloneTable.GetTable()) using (var routeToHeadsTable = RouteToTrailheadsTable.GetTable()) using (var routeToSegmentsTable = RouteToTrailSegmentsTable.GetTable()) using (var routeBuf = routesTable.CreateRowBuffer()) using (var tempSegsFeatureClass = TempSegmentsLayer.GetFeatureClass()) { var namesFilter = new QueryFilter() { WhereClause = $"Upper({RouteName}) = '{routeName.ToUpper().Replace("'", "''")}'" }; using (var namesCursor = RoutesStandaloneTable.Search(namesFilter)) { if (namesCursor.MoveNext()) { MessageBox.Show($"There is already a route named: {routeName}!"); return; } } var operation = new EditOperation(); operation.Name = "Create new trails route: " + routeName; await EnsureIDsForSelectedAsync(operation); await operation.ExecuteAsync(); if (!operation.IsSucceeded) { MessageBox.Show(operation.ErrorMessage); return; } var operation2 = operation.CreateChainedOperation(); operation2.Callback(context => { // create route row routeBuf[RouteName] = routeName; routeBuf[RouteID] = $"{{{Guid.NewGuid()}}}"; using (var routeRow = routesTable.CreateRow(routeBuf)) using (var headsCursor = HeadsLayer.GetSelection().Search(null, false)) using (var segmentCursor = SegmentsLayer.GetSelection().Search((QueryFilter)null, false)) { var segments = new List <string>(); var parts = new Dictionary <int, List <Polyline> >(); if (BuildOnSelect) { // get segments from TempSegments layer bool atLeastOne = false; using (var tempSegsCursor = tempSegsFeatureClass.Search(null, false)) { while (tempSegsCursor.MoveNext()) { atLeastOne = true; var row = tempSegsCursor.Current; var partNum = int.Parse(row[RoutePart].ToString()); var segID = (string)row[USNG_SEG]; CreateRoutePart(segID, (string)routeRow[RouteID], partNum, context, routeToSegmentsTable); segments.Add(segID); var geometry = (Polyline)row["SHAPE"]; if (parts.ContainsKey(partNum)) { parts[partNum].Add(geometry); } else { parts[partNum] = new List <Polyline>() { geometry }; } } Reset(); tempSegsFeatureClass.DeleteRows(new QueryFilter()); context.Invalidate(tempSegsFeatureClass); } if (!atLeastOne) { context.Abort("There must be at least one feature in TempSegments!"); } } else { //get segments from selected features while (segmentCursor.MoveNext()) { var segRow = segmentCursor.Current; var segID = (string)segRow[USNG_SEG]; const int partNum = 1; CreateRoutePart(segID, routeRow[RouteID], partNum, context, routeToSegmentsTable); segments.Add(segID); var geometry = (Polyline)segRow["SHAPE"]; if (parts.ContainsKey(partNum)) { parts[partNum].Add(geometry); } else { parts[partNum] = new List <Polyline>() { geometry }; } } } if (segments.Count > 1 && !ValidateConnectivity(parts)) { context.Abort("Not all segments are connected!"); return; } // trailhead if (HeadsLayer.SelectionCount > 0) { while (headsCursor.MoveNext()) { using (var headBuffer = routeToHeadsTable.CreateRowBuffer()) { headBuffer[RouteID] = (string)routeRow[RouteID]; headBuffer[USNG_TH] = headsCursor.Current[USNG_TH]; using (var headRow = routeToHeadsTable.CreateRow(headBuffer)) { context.Invalidate(headRow); } } } } context.Invalidate(routeRow); } }, routesTable, routeToSegmentsTable, routeToHeadsTable, tempSegsFeatureClass); await operation2.ExecuteAsync(); if (operation2.IsSucceeded) { FrameworkApplication.AddNotification(new Notification { Title = FrameworkApplication.Title, Message = $"Route: \"{routeName}\" added successfully!", ImageUrl = "pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericCheckMark32.png" }); SegmentsLayer.ClearSelection(); HeadsLayer.ClearSelection(); } else { MessageBox.Show(operation2.ErrorMessage); } } }); }