Hi all. I have implemented a simple interface to use the my local geometry service's cut functionality. I want to allow a user to take an existing polygon and split it into two. Seems simple given that there is an example in the Silverlight interactive SDK that does just this.However, while my code appears to suceed at this task, the returned results from the cut process are only partly correct. If I take a polygon P and draw a line to split it into two non-overlapping portions A and B together which should comprise the entire original geometry of P, I instead get two geometries back from the cut task - A and P? In other words one of the two geometries returned is as I desired one part of the original polygon P, defined by the bisecting line I drew on the map. The other geometry returned should be B, a second polygon also defined by the bisecting line - the other half of P. But instead the second geometry is the entire original polygon P.I guess I could now take my returned polygon A and use it with a reshape function to reshape the returned P-sized geometry to manually create the B polygon I was expecting, but none of the sample code I have seen suggests a need to do this.At first I thought maybe the cut task returns all three geometries - A, B, and just for good measure, P. But I looked at the returned structure during debugging and it only contains the two geometries A and P.This was very confusing at first because my code blindly adds both geometries back to my graphics layer, and the second geometry which was the size of the original P kept getting added on top of A - so I would not see A. I finally realized the issue when I started exploring the returned geometries in an ArcMap edit session...Here is some code:
private void btnStartSplit_Click(object sender, RoutedEventArgs e)
{
// Empty container that gets filled with the polygon geometry I want
// cut by a polyline geometry.
private IList<Graphic> toSplit = new List<Graphic>();
// The source of the graphics used during cutting
GraphicsLayer editsMapLayer = ((GraphicsLayer)MainPage.CPMap.Layers["GraphicsLayer_PLUSelection"]);
// Look for the polyline I drew earlier, of which there will only be one
// because without the polyline, we cannot perform the cut...
//
// Start by getting a collection of all graphics in the graphics layer
GraphicCollection tEdits = editsMapLayer.Graphics;
// Then return only the geometries (of which there will be 0 or 1) where the type is
// ESRI....Polyline
Graphic theLine = tEdits.Where(g => g.Geometry.GetType().FullName == "ESRI.ArcGIS.Client.Geometry.Polyline").FirstOrDefault();
if (theLine == null) return; // And quit here if there was no line
// Obtain the geometry of that line...
ESRI.ArcGIS.Client.Geometry.Polyline theLineGeom = (ESRI.ArcGIS.Client.Geometry.Polyline)theLine.Geometry;
// I know that there is a polygon in my graphics layer to cut, so I search for it and add
// it to my set of geometries to be cut.
toSplit.Add(tEdits.Where(g => g.Geometry.GetType().FullName == "ESRI.ArcGIS.Client.Geometry.Polygon").FirstOrDefault());
// Create a new geometry service
ESRI.ArcGIS.Client.Tasks.GeometryService geoSvc = new ESRI.ArcGIS.Client.Tasks.GeometryService(MainPage.editorMainMap.GeometryServiceUrl);
// Setup the event handler
geoSvc.CutCompleted += new EventHandler<ESRI.ArcGIS.Client.Tasks.CutEventArgs>(geoSvc_CutCompleted);
// Call the cut process asynchronously passing in the container of one polygon geometry to cut,
// and one polyline geometry with which to cut that polygon geometry.
geoSvc.CutAsync(toSplit,theLineGeom);
}
void geoSvc_CutCompleted(object sender, ESRI.ArcGIS.Client.Tasks.CutEventArgs e)
{
MessageBox.Show("Cut Event Returned At Least!");
// I will be adding the returned geometries as new graphics to the SubAreas Featurelayer of my map.
FeatureLayer subareaMapLayer = ((FeatureLayer)MainPage.CPMap.Layers["SubAreas"]);
// Featurelayer save-event-handlers
subareaMapLayer.SaveEditsFailed += new EventHandler<ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs>(subareaMapLayer_SaveEditsFailed);
subareaMapLayer.EndSaveEdits += new EventHandler<ESRI.ArcGIS.Client.Tasks.EndEditEventArgs>(subareaMapLayer_EndSaveEdits);
subareaMapLayer.BeginSaveEdits += new EventHandler<ESRI.ArcGIS.Client.Tasks.BeginEditEventArgs>(subareaMapLayer_BeginSaveEdits);
// This is where I find I will loop two times, once for each returned geometry...
// one geometry (A from my description above) is correct, and one (P) is not. There
// does not appear to be a third geometry (B)...
foreach (Graphic g in e.Results)
{
// Give the geometry/graphic a default symbol
g.Symbol = MainPage.MainGrid.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
// Set some attributes on the geometry/graphic...
g.Attributes["SubareaID"] = "{" + Guid.NewGuid().ToString() + "}";
g.Attributes["PlanningLandUnitID"] = OriginPlanningLandUnitId;
g.Attributes["LandUseID"] = Convert.ToInt32(OriginPlanningLandUnitUseId);
g.Attributes["parent_subareaid"] = SplitPractice.Description.ToString();
// Add the new graphic to the Featurelayer
subareaMapLayer.Graphics.Add(g);
// Save the edit
subareaMapLayer.SaveEdits();
} // Process next geometry/graphic in return set
subareaMapLayer.Update();
}
Aside from the interactive SDK, is there anywhere else I can look for examples on what I am supposed to do to use the cut command of my geometry service? What geometries exactly I should expect back from the cut command?Thanks for any input!Nick Floersch