This sample demonstrates using an ESRI.ArcGIS.Client.Tasks GeometryService that takes an input polyline and a set of other polyline graphics and returns either a trimmed or extended set of polyline graphics. If the input polyline crosses another polyline graphic a trim will occur where the two cross. If the input polyline does not cross the other polyline but crosses an imaginary ray that extends out from the other polyline that and extend will occur.
To use this sample, digitize a line that crosses over or near the end of one or more blue lines. Trimmed or extended lines are returned in red.
<UserControlx:Class="ArcGISWPFSDK.TrimExtend"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:esri="http://schemas.esri.com/arcgis/client/2009"><Gridx:Name="LayoutRoot"Background="White"><Grid.Resources><esri:SimpleLineSymbolx:Key="DefaultLineSymbol"Color="Blue"Width="8"/><esri:SimpleLineSymbolx:Key="DrawLineSymbol"Color="Yellow"Width="3"/><esri:SimpleLineSymbolx:Key="ResultsLineSymbol"Color="Red"Width="4"/><LinearGradientBrushx:Key="PanelGradient"EndPoint="0.5,1"StartPoint="0.5,0"><LinearGradientBrush.RelativeTransform><TransformGroup><ScaleTransformCenterY="0.5"CenterX="0.5"/><SkewTransformCenterY="0.5"CenterX="0.5"/><RotateTransformAngle="176"CenterY="0.5"CenterX="0.5"/><TranslateTransform/></TransformGroup></LinearGradientBrush.RelativeTransform><GradientStopColor="#FF145787"Offset="0.16"/><GradientStopColor="#FF3D7FAC"Offset="0.502"/><GradientStopColor="#FF88C5EF"Offset="0.984"/></LinearGradientBrush></Grid.Resources><esri:Mapx:Name="MyMap"Extent="-119,30,-114,36"><esri:Map.Layers><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/><esri:GraphicsLayerID="MyPolylineGraphicsLayer"><esri:GraphicsLayer.Graphics><esri:GraphicSymbol="{StaticResource DefaultLineSymbol}"><esri:Polylinex:Name="Polyline_0"><esri:Polyline.Paths><esri:PointCollection>-117,34 -117,32</esri:PointCollection></esri:Polyline.Paths></esri:Polyline></esri:Graphic><esri:GraphicSymbol="{StaticResource DefaultLineSymbol}"><esri:Polylinex:Name="Polyline_1"><esri:Polyline.Paths><esri:PointCollection>-118,35 -118,31</esri:PointCollection></esri:Polyline.Paths></esri:Polyline></esri:Graphic><esri:GraphicSymbol="{StaticResource DefaultLineSymbol}"><esri:Polylinex:Name="Polyline_2"><esri:Polyline.Paths><esri:PointCollection>-116,33.8 -116,31.4</esri:PointCollection></esri:Polyline.Paths></esri:Polyline></esri:Graphic><esri:GraphicSymbol="{StaticResource DefaultLineSymbol}"><esri:Polylinex:Name="Polygline_3"><esri:Polyline.Paths><esri:PointCollection>-115,34.5 -115,32.3</esri:PointCollection></esri:Polyline.Paths></esri:Polyline></esri:Graphic></esri:GraphicsLayer.Graphics></esri:GraphicsLayer><esri:GraphicsLayerID="MyResultsGraphicsLayer"/></esri:Map.Layers></esri:Map><GridHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,15,15,0"><RectangleFill="{StaticResource PanelGradient}"Stroke="Gray"RadiusX="10"RadiusY="10"Margin="0,0,0,5"><Rectangle.Effect><DropShadowEffect/></Rectangle.Effect></Rectangle><RectangleFill="#FFFFFFFF"Stroke="DarkGray"RadiusX="5"RadiusY="5"Margin="10,10,10,15"/><TextBlockx:Name="InfoTextBlock"Text="Digitize a line that crosses over or near the end of one or more blue lines. Trimmed or extended lines are returned in red."Width="200"TextAlignment="Left"Margin="30,20,20,30"TextWrapping="Wrap"Foreground="Black"/></Grid></Grid></UserControl>
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Geometry;
using System.Collections.Generic;
namespace ArcGISWPFSDK
{
publicpartialclass TrimExtend : UserControl
{
private Draw MyDrawObject;
private GraphicsLayer polylineLayer;
private GraphicsLayer resultsLayer;
private GeometryService geometryService;
public TrimExtend()
{
InitializeComponent();
polylineLayer = MyMap.Layers["MyPolylineGraphicsLayer"] as GraphicsLayer;
resultsLayer = MyMap.Layers["MyResultsGraphicsLayer"] as GraphicsLayer;
MyMap.Layers.LayersInitialized += Layers_LayersInitialized;
MyDrawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polyline,
IsEnabled = true,
LineSymbol = LayoutRoot.Resources["DrawLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol
};
MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
}
void Layers_LayersInitialized(object sender, EventArgs args)
{
foreach (Graphic g in polylineLayer.Graphics)
g.Geometry.SpatialReference = MyMap.SpatialReference;
}
privatevoid MyDrawObject_DrawComplete(object sender, DrawEventArgs args)
{
MyDrawObject.IsEnabled = false;
resultsLayer.ClearGraphics();
Polyline polyline = args.Geometry as Polyline;
polyline.SpatialReference = MyMap.SpatialReference;
geometryService =
new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
geometryService.TrimExtendCompleted += GeometryService_TrimExtendCompleted;
geometryService.Failed += GeometryService_Failed;
List<Polyline> polylineList = new List<Polyline>();
foreach (Graphic g in polylineLayer.Graphics)
polylineList.Add(g.Geometry as Polyline);
geometryService.TrimExtendAsync(polylineList, polyline, CurveExtension.DefaultCurveExtension);
}
void GeometryService_TrimExtendCompleted(object sender, GraphicsEventArgs e)
{
foreach (Graphic g in e.Results)
{
g.Symbol = LayoutRoot.Resources["ResultsLineSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
resultsLayer.Graphics.Add(g);
}
MyDrawObject.IsEnabled = true;
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Geometry Service error: " + args.Error);
}
}
}
Imports System.Linq
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Geometry
Imports System.Collections.Generic
Namespace ArcGISWPFSDK
PartialPublicClass TrimExtend
Inherits UserControl
Private MyDrawObject As Draw
Private polylineLayer As GraphicsLayer
Private resultsLayer As GraphicsLayer
Private geometryService As GeometryService
PublicSubNew()
InitializeComponent()
polylineLayer = TryCast(MyMap.Layers("MyPolylineGraphicsLayer"), GraphicsLayer)
resultsLayer = TryCast(MyMap.Layers("MyResultsGraphicsLayer"), GraphicsLayer)
AddHandler MyMap.Layers.LayersInitialized, AddressOf Layers_LayersInitialized
MyDrawObject = New Draw(MyMap) With { _
.DrawMode = DrawMode.Polyline, _
.IsEnabled = True, _
.LineSymbol = TryCast(LayoutRoot.Resources("DrawLineSymbol"), ESRI.ArcGIS.Client.Symbols.LineSymbol) _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
EndSubPrivateSub Layers_LayersInitialized(sender AsObject, args As EventArgs)
ForEach g As Graphic In polylineLayer.Graphics
g.Geometry.SpatialReference = MyMap.SpatialReference
NextEndSubPrivateSub MyDrawObject_DrawComplete(sender AsObject, args As DrawEventArgs)
MyDrawObject.IsEnabled = False
resultsLayer.ClearGraphics()
Dim polyline As Polyline = TryCast(args.Geometry, Polyline)
polyline.SpatialReference = MyMap.SpatialReference
geometryService = New GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
AddHandler geometryService.TrimExtendCompleted, AddressOf GeometryService_TrimExtendCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
Dim polylineList AsNew List(Of Polyline)()
ForEach g As Graphic In polylineLayer.Graphics
polylineList.Add(TryCast(g.Geometry, Polyline))
Next
geometryService.TrimExtendAsync(polylineList, polyline, CurveExtension.DefaultCurveExtension)
EndSubPrivateSub GeometryService_TrimExtendCompleted(sender AsObject, e As GraphicsEventArgs)
ForEach g As Graphic In e.Results
g.Symbol = TryCast(LayoutRoot.Resources("ResultsLineSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
resultsLayer.Graphics.Add(g)
Next
MyDrawObject.IsEnabled = TrueEndSubPrivateSub GeometryService_Failed(sender AsObject, args As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(args.[Error]))
EndSubEndClassEndNamespace