This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation 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.LocalTrimExtend"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="-15000000,2000000,-7000000,8000000"Background="#FFE3E3E3"><esri:Map.Layers><esri:ArcGISLocalTiledLayerID="BaseMap"Path="..\\Data\\TPKs\\Topographic.tpk"/><esri:GraphicsLayerID="PolylineGraphicsLayer"x:Name="_polylineGraphicsLayer"><esri:GraphicsLayer.Graphics><esri:GraphicSymbol="{StaticResource DefaultLineSymbol}"><esri:Polylinex:Name="Polyline_0"><esri:Polyline.Paths><esri:PointCollection>-13222659,6689903 -13222659,2649896</esri:PointCollection></esri:Polyline.Paths></esri:Polyline></esri:Graphic><esri:GraphicSymbol="{StaticResource DefaultLineSymbol}"><esri:Polylinex:Name="Polyline_1"><esri:Polyline.Paths><esri:PointCollection>-12330709,6270162 -12330709,3741232</esri:PointCollection></esri:Polyline.Paths></esri:Polyline></esri:Graphic><esri:GraphicSymbol="{StaticResource DefaultLineSymbol}"><esri:Polylinex:Name="Polyline_2"><esri:Polyline.Paths><esri:PointCollection>-11144941,6417071 -11144941,2807299</esri:PointCollection></esri:Polyline.Paths></esri:Polyline></esri:Graphic><esri:GraphicSymbol="{StaticResource DefaultLineSymbol}"><esri:Polylinex:Name="Polygline_3"><esri:Polyline.Paths><esri:PointCollection>-9277094,5098527 -9277094,4024548</esri:PointCollection></esri:Polyline.Paths></esri:Polyline></esri:Graphic></esri:GraphicsLayer.Graphics></esri:GraphicsLayer><esri:GraphicsLayerID="ResultsGraphicsLayer"x:Name="_resultsGraphicsLayer"/></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;
using ESRI.ArcGIS.Client.Local;
namespace ArcGISWPFSDK
{
publicpartialclass LocalTrimExtend : UserControl
{
private Draw _drawObject;
GeometryService _geometryTask;
public LocalTrimExtend()
{
InitializeComponent();
LocalGeometryService.GetServiceAsync(lgs =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = lgs.UrlGeometryService;
_geometryTask.TrimExtendCompleted += GeometryService_TrimExtendCompleted;
_geometryTask.Failed += GeometryService_Failed;
});
MyMap.Layers.LayersInitialized += (s, e) =>
{
_drawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polyline,
IsEnabled = true,
LineSymbol = LayoutRoot.Resources["DrawLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol
};
_drawObject.DrawComplete += DrawObject_DrawComplete;
foreach (Graphic g in _polylineGraphicsLayer.Graphics)
g.Geometry.SpatialReference = MyMap.SpatialReference;
};
}
privatevoid DrawObject_DrawComplete(object sender, DrawEventArgs args)
{
_drawObject.IsEnabled = false;
_resultsGraphicsLayer.ClearGraphics();
Polyline polyline = args.Geometry as Polyline;
polyline.SpatialReference = MyMap.SpatialReference;
List<Polyline> polylineList = new List<Polyline>();
foreach (Graphic g in _polylineGraphicsLayer.Graphics)
polylineList.Add(g.Geometry as Polyline);
_geometryTask.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;
_resultsGraphicsLayer.Graphics.Add(g);
}
_drawObject.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
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalTrimExtend
Inherits UserControl
Private MyDrawObject As Draw
Private polylineLayer As GraphicsLayer
Private resultsLayer As GraphicsLayer
Private _geometryService As GeometryService
PublicSubNew()
InitializeComponent()
Dim localGeometryService AsNew LocalGeometryService()
AddHandler localGeometryService.StartCompleted, Function(sender, eventargs)
Dim lgs As LocalGeometryService = TryCast(sender, LocalGeometryService)
_geometryService = New GeometryService()
_geometryService.Url = lgs.UrlGeometryService
AddHandler _geometryService.TrimExtendCompleted, AddressOf GeometryService_TrimExtendCompleted
AddHandler _geometryService.Failed, AddressOf GeometryService_Failed
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
EndFunction
localGeometryService.StartAsync()
polylineLayer = TryCast(MyMap.Layers("MyPolylineGraphicsLayer"), GraphicsLayer)
resultsLayer = TryCast(MyMap.Layers("MyResultsGraphicsLayer"), GraphicsLayer)
AddHandler MyMap.Layers.LayersInitialized, AddressOf Layers_LayersInitialized
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
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