男默女泪,ArcGIS AddIN 编辑逻辑赏析,走过路过,不要错过

时间:2021-12-14 18:48:27

看到了一段ESRI写的一个工具,我反编译了一下,学习工具中的几个代码片段

一.

 IEditTask task = (this._editor as IEditTaskSearch).get_TaskByUniqueName("GarciaUI_CreateNewFeatureTask");
this._editor.CurrentTask = task;

IEditTask :Provides access to a task that receives notification when the sketch is complete.

The purpose of the IEditTask interface is to take a geometry (the edit sketch) from IEditSketch::Geometry and perform some action with it. Each edit task performs a different operation with this geometry. For example, the CreateNewFeature edit task takes the edit sketch and stores it as a new feature. The Reshape Feature edit task uses the edit sketch to reshape a selected feature. The editor can only have one active edit task at a time, use IEditor::CurrentTask to set this.
All edit tasks implement the IEditTask interface. Edit tasks must be registered in the ESRI Edit Tasks component category to appear in the Edit Task dropdown menu on the Editor toolbar.
上述内容摘自开发帮助文档.
个人说明:以前写代码基本不涉及IEditTask接口,写工具的时候,经常是人为的新建Feature之类的,即使写ConstructionTool,也是直接拿IEditSketch::Geometry对象进行下一步操作了,看了上述代码之后,查询IEditTask接口,帮助文档解释如下
IEditTask接口的目的是获取到 IEditSketch::Geometry 的几何对象,以进行下一步的操作.每一个Edit Task使用该几何对象进行不同的操作.比如,CreateNewFeature edit Task使用该几何对象新建要素. ReShape Feature Edit Task 使用该几何对象进行修整要素的几何.

IEditTaskSearch:    Provides access to members that find edit tasks by name.

常见的EditTask及其名称:(From帮助文档)

Edit Task

UniqueName

Create New Feature

"GarciaUI_CreateNewFeatureTask"

Reshape Feature

"GarciaUI_ReshapeFeatureTask"

Cut Polygon Features

"GarciaUI_CutPolygonFeaturesTask"

Mirror Features

"GarciaUI_MirrorFeaturesTask"

Extend/Trim Features

"GarciaUI_ExtendTrimFeaturesTask"

Modify Feature

"GarciaUI_ModifyFeatureTask"

Calibrate Route Feature

"RouteEditorUI_CalibrateRouteFeatureTask"

Modify Portion of Line

"RouteEditorUI_ModifyPortionOfLineTask"

Modify Edge

"TitusUI_ModifyEdgeTask"

Reshape Edge

"TitusUI_ReshapeEdgeTask"

Auto Complete Polygon

"TitusUI_AutoCompletePolygonTask"

Select Features Using a Line

"GarciaUI_SelectFeaturesUsingLineTask"

Select Features Using an Area

"GarciaUI_SelectFeaturesUsingPolygonTask"

Create 2-Point Line Features

"EditTools_Create2PointLineFeaturesTask"

二.

 this._editLayers = this._editor as IEditLayers;

IEditLayers: Provides access to members that control information about layers in the edit session.

The IEditLayers interface exposes members for setting and getting the editor's current layer and current subtype. The current layer (or target layer) determines which layer will contain newly created features. For instance, if you set the target layer to 'Buildings', any new features created will be part of the Buildings layer. Edit tasks and commands that create new features use this property to determine which layer to write out the new features to. For example, the 'Create New Features' task and the 'Union' command both create new features in the target layer.

说明:IEditLayers接口用于管理编辑会话期间的图层

可以用来获取或设置编辑器的当前图层或subtype(不知为何物),当前图层决定了哪个图层用于新建要素

三.

if (this._lineFeedback == null)
{
this._lineFeedback = new NewTextBezierCurveFeedbackClass();
this._lineFeedback.Display = this._editor.Display;
this._lineFeedback.Start(this._mousePoint, this._editor.Map.ReferenceScale);
}
else
{
this._lineFeedback.AddPoint(this._mousePoint);
}

INewLineFeedback: Provides access to members that control the new line display feedback.

The NewLineFeedback coclass allows the user to form a new Polyline geometry on the display. While the feedback is being used, the line shown on the screen is a series of segments made up of straight lines between each of the points clicked by the user. If the user opts to add no intermediate vertices, that is, they simply click at the start point (Start), move the mouse (MoveTo), and double-click at the end (AddPoint and Stop), then a polyline with only one segment will be generated.

同上,还有其他很多INew***Feedback接口与对象,可以实现辅助绘制功能

以前写Tool时,经常使用Graphic对象,进行绘制和保存临时创建的对象,这么做需要做的工作很多,比如graphic对象的管理,以及Symbol符号的设置,绘制线时,代码中需要考虑以绘制的线与当前鼠标的节点所组成的图形,并进行动态更新绘制,逻辑复杂的很.

有了上述接口与对象,可以直接调用,通过AddPoint(),Stop()等方法,轻松实现对象的绘制与交互.

大体实现逻辑,在mousedown事件中,调用Start(),AddPoint(),以添加节点

在MouseMove事件中,调用MoveTo(),以更新

在DoubleClick事件中,调用Stop(),以结束绘制

四.关于捕捉逻辑

 ISnapEnvironment environment = this._editor as ISnapEnvironment;
if (this._mousePoint != null)
{
this._editor.InvertAgent(this._mousePoint, );
}
this._mousePoint = this._editor.Display.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
if (KeyboardInfo.GetKeyState(Keys.Space).IsPressed)
{
environment.SnapPoint(null);
}
else
{
environment.SnapPoint(this._mousePoint);
}
this._editor.InvertAgent(this._mousePoint, );
if (this._lineFeedback != null)
{
this._lineFeedback.MoveTo(this._mousePoint);
}

之前写过一篇关于如何调用捕捉的帖子,当时只顾为了实现捕捉,没有进一步优化

上述代码中在MouseMove事件中判断了当前是否按住了 空格键,以实现在按住空格键时临时不捕捉的效果,

与ArcMap的使用习惯一致