Skip to content

Commit fc19bf0

Browse files
authored
Merge pull request #6 from autodotua/test_GeometryEditor
Test geometry editor
2 parents 4b3e158 + 5b0a46d commit fc19bf0

15 files changed

+373
-277
lines changed

MapBoard.UI/Mapping/EditorHelper.cs

Lines changed: 98 additions & 63 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Esri.ArcGISRuntime.Geometry;
2+
using System;
3+
4+
namespace MapBoard.Mapping
5+
{
6+
public class GeometryUpdatedEventArgs : EventArgs
7+
{
8+
public GeometryUpdatedEventArgs(Geometry geometry)
9+
{
10+
Geometry = geometry;
11+
}
12+
13+
public Geometry Geometry { get; private set; }
14+
}
15+
}

MapBoard.UI/Mapping/MainMapView.cs

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Esri.ArcGISRuntime.Mapping;
44
using Esri.ArcGISRuntime.UI;
55
using Esri.ArcGISRuntime.UI.Controls;
6+
using Esri.ArcGISRuntime.UI.Editing;
67
using FzLib;
78
using FzLib.WPF;
89
using FzLib.WPF.Dialog;
@@ -67,7 +68,6 @@ public MainMapView()
6768
Layers = new MapLayerCollection();
6869
AllowDrop = true;
6970
IsAttributionTextVisible = false;
70-
SketchEditor = new SketchEditor();
7171
this.SetHideWatermark();
7272

7373
InteractionOptions = new MapViewInteractionOptions()
@@ -144,7 +144,7 @@ public async Task LoadAsync()
144144
ZoomToLastExtent().ContinueWith(t => ViewpointChanged += ArcMapView_ViewpointChanged);
145145
Editor = new EditorHelper(this);
146146
Selection = new SelectionHelper(this);
147-
Overlay = new OverlayHelper(GraphicsOverlays, async p => await ZoomToGeometryAsync(p));
147+
Overlay = new OverlayHelper(GraphicsOverlays, async p => await this.ZoomToGeometryAsync(p));
148148
Selection.CollectionChanged += Selection_CollectionChanged;
149149
CurrentTask = BoardTask.Ready;
150150
}
@@ -158,31 +158,6 @@ public void SetLocationDisplay()
158158
LocationDisplay.IsEnabled = Config.Instance.ShowLocation;
159159
}
160160

161-
/// <summary>
162-
/// 缩放到指定位置
163-
/// </summary>
164-
/// <param name="geometry"></param>
165-
/// <param name="autoExtent"></param>
166-
/// <returns></returns>
167-
public async Task ZoomToGeometryAsync(Geometry geometry, bool autoExtent = true)
168-
{
169-
if (geometry is MapPoint || geometry is Multipoint m && m.Points.Count == 1 || geometry.Extent.Width == 0 || geometry.Extent.Height == 0)
170-
{
171-
if (geometry.SpatialReference.Wkid != SpatialReferences.WebMercator.Wkid)
172-
{
173-
geometry = GeometryEngine.Project(geometry, SpatialReferences.WebMercator);
174-
}
175-
geometry = GeometryEngine.Buffer(geometry, 500);
176-
}
177-
var extent = geometry.Extent;
178-
if (double.IsNaN(extent.Width) || double.IsNaN(extent.Height) || extent.Width == 0 || extent.Height == 0)
179-
{
180-
SnakeBar.ShowError("图形为空");
181-
return;
182-
}
183-
await SetViewpointGeometryAsync(geometry, Config.Instance.HideWatermark && autoExtent ? Config.WatermarkHeight : 0);
184-
}
185-
186161
/// <summary>
187162
/// 缩放到记忆的地图位置
188163
/// </summary>
@@ -193,7 +168,7 @@ private async Task ZoomToLastExtent()
193168
{
194169
try
195170
{
196-
await ZoomToGeometryAsync(Envelope.FromJson(Layers.MapViewExtentJson), false);
171+
await this.ZoomToGeometryAsync(Envelope.FromJson(Layers.MapViewExtentJson), false);
197172
}
198173
catch
199174
{
@@ -211,8 +186,8 @@ protected override async void OnPreviewKeyDown(KeyEventArgs e)
211186
switch (e.Key)
212187
{
213188
//Delete:移除节点、删除要素
214-
case Key.Delete when SketchEditor.SelectedVertex != null:
215-
SketchEditor.RemoveSelectedVertex();
189+
case Key.Delete when GeometryEditor.SelectedElement is GeometryEditorVertex:
190+
GeometryEditor.DeleteSelectedElement();
216191
break;
217192

218193
case Key.Delete when CurrentTask == BoardTask.Select && Layers.Selected is IEditableLayerInfo w:
@@ -235,18 +210,7 @@ protected override async void OnPreviewKeyDown(KeyEventArgs e)
235210
case BoardTask.Ready
236211
when Layers.Selected?.LayerVisible == true
237212
&& Layers.Selected?.Interaction?.CanEdit == true:
238-
SketchCreationMode? type = Layers.Selected.GeometryType switch
239-
{
240-
GeometryType.Point => SketchCreationMode.Point,
241-
GeometryType.Multipoint => SketchCreationMode.Multipoint,
242-
GeometryType.Polyline => SketchCreationMode.Polyline,
243-
GeometryType.Polygon => SketchCreationMode.Polygon,
244-
_ => null
245-
};
246-
if (type.HasValue)
247-
{
248-
await Editor.DrawAsync(type.Value);
249-
}
213+
await Editor.DrawAsync(Layers.Selected.GeometryType, null);
250214
break;
251215

252216
case BoardTask.Select
@@ -272,16 +236,16 @@ protected override async void OnPreviewKeyDown(KeyEventArgs e)
272236
break;
273237

274238
//Ctrl Z:撤销
275-
case Key.Z when Keyboard.Modifiers == ModifierKeys.Control && SketchEditor.UndoCommand.CanExecute(null):
276-
SketchEditor.UndoCommand.Execute(null);
239+
case Key.Z when Keyboard.Modifiers == ModifierKeys.Control && GeometryEditor.CanUndo:
240+
GeometryEditor.Undo();
277241
break;
278242

279243
//Ctrl SHift Z/Ctrl Y:重做
280-
case Key.Z when Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift) && SketchEditor.RedoCommand.CanExecute(null):
281-
SketchEditor.RedoCommand.Execute(null);
244+
case Key.Z when Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift) && GeometryEditor.CanRedo:
245+
GeometryEditor.Redo();
282246
break;
283-
case Key.Y when Keyboard.Modifiers == ModifierKeys.Control && SketchEditor.RedoCommand.CanExecute(null):
284-
SketchEditor.RedoCommand.Execute(null);
247+
case Key.Y when Keyboard.Modifiers == ModifierKeys.Control && GeometryEditor.CanRedo:
248+
GeometryEditor.Redo();
285249
break;
286250
}
287251
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Esri.ArcGISRuntime.Geometry;
2+
using Esri.ArcGISRuntime.UI.Controls;
3+
using FzLib.WPF.Dialog;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace MapBoard.Mapping
11+
{
12+
public static class MapViewExtension
13+
{
14+
public static async Task ZoomToGeometryAsync(this MapView mapView, Geometry geometry, bool autoExtent = true)
15+
{
16+
if (geometry is MapPoint || geometry is Multipoint m && m.Points.Count == 1 || geometry.Extent.Width == 0 || geometry.Extent.Height == 0)
17+
{
18+
if (geometry.SpatialReference.Wkid != SpatialReferences.WebMercator.Wkid)
19+
{
20+
geometry = GeometryEngine.Project(geometry, SpatialReferences.WebMercator);
21+
}
22+
geometry = GeometryEngine.Buffer(geometry, 500);
23+
}
24+
var extent = geometry.Extent;
25+
if (double.IsNaN(extent.Width) || double.IsNaN(extent.Height) || extent.Width == 0 || extent.Height == 0)
26+
{
27+
SnakeBar.ShowError("图形为空");
28+
return;
29+
}
30+
await mapView.SetViewpointGeometryAsync(geometry, Config.Instance.HideWatermark && autoExtent ? Config.WatermarkHeight : 0);
31+
}
32+
33+
}
34+
}

MapBoard.UI/Mapping/TileDownloaderMapView.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Esri.ArcGISRuntime.Symbology;
55
using Esri.ArcGISRuntime.UI;
66
using Esri.ArcGISRuntime.UI.Controls;
7+
using Esri.ArcGISRuntime.UI.Editing;
78
using FzLib.WPF.Dialog;
89
using MapBoard.Model;
910
using ModernWpf.FzExtension.CommonDialog;
@@ -16,6 +17,7 @@
1617
using System.Windows;
1718
using System.Windows.Input;
1819
using static MapBoard.Mapping.Model.TileInfoExtension;
20+
using static MapBoard.Util.GeometryUtility;
1921

2022
namespace MapBoard.Mapping
2123
{
@@ -130,7 +132,12 @@ public async Task LoadAsync()
130132
return;
131133
}
132134
}
133-
135+
private GeometryEditorTool GetGeometryEditorTool()
136+
{
137+
var tool = ShapeTool.Create(ShapeToolType.Rectangle);
138+
tool.Configuration.AllowRotatingSelectedElement = false;
139+
return tool;
140+
}
134141
/// <summary>
135142
/// 选择下载范围
136143
/// </summary>
@@ -142,11 +149,12 @@ public async Task SelectAsync()
142149
SnakeBar.Show("开始框选");
143150

144151
isSelecting = true;
145-
await SketchEditor.StartAsync(SketchCreationMode.Rectangle, GetSketchEditConfiguration());
152+
GeometryEditor.Start(GeometryType.Polygon);
153+
GeometryEditor.Tool = GetGeometryEditorTool();
146154
}
147155
else
148156
{
149-
SketchEditor.Stop();
157+
GeometryEditor.Stop();
150158
SnakeBar.Show("终止框选");
151159
isSelecting = false;
152160
}
@@ -160,10 +168,11 @@ public void SetBoundary(GeoRect<double> range)
160168
{
161169
Polygon polygon = RangeToPolygon(range);
162170
Geometry geometry = GeometryEngine.Project(polygon, SpatialReference);
163-
SketchEditor.StartAsync(geometry, SketchCreationMode.Rectangle, GetSketchEditConfiguration());
171+
GeometryEditor.Start(geometry);
172+
GeometryEditor.Tool = GetGeometryEditorTool();
164173
isSelecting = true;
174+
this.ZoomToGeometryAsync(polygon);
165175
}
166-
167176
/// <summary>
168177
/// 设置当前下载位置
169178
/// </summary>
@@ -198,10 +207,10 @@ protected override async void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
198207
{
199208
base.OnPreviewMouseLeftButtonUp(e);
200209

201-
if (SketchEditor != null && SketchEditor.Geometry != null)
210+
if (GeometryEditor.Geometry != null)
202211
{
203212
await Task.Delay(500);
204-
Boundary = GeometryEngine.Project(SketchEditor.Geometry, SpatialReferences.Wgs84).Extent;
213+
Boundary = GeometryEditor.Geometry.ToWgs84().Extent;
205214
SelectBoundaryComplete?.Invoke(this, new EventArgs());
206215
}
207216
}
@@ -218,19 +227,6 @@ private async void ArcMapViewLoaded(object sender, RoutedEventArgs e)
218227
}
219228
}
220229

221-
/// <summary>
222-
/// 获取选择框编辑器的配置
223-
/// </summary>
224-
/// <returns></returns>
225-
private SketchEditConfiguration GetSketchEditConfiguration()
226-
{
227-
return new SketchEditConfiguration()
228-
{
229-
AllowRotate = false,
230-
ResizeMode = SketchResizeMode.Stretch,
231-
AllowVertexEditing = false
232-
};
233-
}
234230

235231
/// <summary>
236232
/// 将选择范围转换为多边形

MapBoard.UI/UI/Bar/EditionBar.xaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
</Button>
7575

7676
<Button
77-
Command="{Binding MapView.SketchEditor.UndoCommand}"
77+
Click="UndoButton_Click"
78+
IsEnabled="{Binding MapView.GeometryEditor.CanUndo}"
7879
Style="{StaticResource barButtonStyle}">
7980
<StackPanel Orientation="Horizontal">
8081
<ContentControl
@@ -89,7 +90,8 @@
8990
</Button>
9091
<Button
9192
Grid.Column="2"
92-
Command="{Binding MapView.SketchEditor.RedoCommand}"
93+
Click="RedoButton_Click"
94+
IsEnabled="{Binding MapView.GeometryEditor.CanRedo}"
9395
Style="{StaticResource barButtonStyle}">
9496
<StackPanel Orientation="Horizontal">
9597
<ContentControl

0 commit comments

Comments
 (0)