转自原文ArcGIS Engine 编辑- ITask

下面的代码是我们定制的一个工作流-给等高线赋值

namespace EngineApplication

{

[Guid("5b0c0692-eaf7-4d64-9cee-c8c1afaf06f4")]

[ClassInterface(ClassInterfaceType.None)]

[ProgId("EditeTest.ContourTask")]

public class CalculateContour : ESRI.ArcGIS.Editor.IEditTask

{

#region

IEditor pEngineEditor;

IEditSketch pEditSketch;

IEditLayers pEditLayer;

#endregion

#region "IEditTask Implementations"

public void Activate(IEditor pEditor, ESRI.ArcGIS.Editor.IEditTask pEditTask)

{

if (pEditor == null)

return;

pEngineEditor = pEditor;

pEditSketch = pEngineEditor as IEditSketch;

pEditSketch.GeometryType = esriGeometryType.esriGeometryPolyline;

pEditLayer = pEditSketch as IEditLayers;

//Listen to engine editor events

((IEngineEditEvents_Event)pEditSketch).OnTargetLayerChanged += new IEngineEditEvents_OnTargetLayerChangedEventHandler(OnTargetLayerChanged);

((IEngineEditEvents_Event)pEditSketch).OnCurrentTaskChanged += new IEngineEditEvents_OnCurrentTaskChangedEventHandler(OnCurrentTaskChanged);

}

public void Deactivate()

{

// TODO: Add ArcGISClass1.Deactivate implementation

pEditSketch.RefreshSketch();

//Stop listening to engine editor events.

((IEngineEditEvents_Event)pEditSketch).OnTargetLayerChanged -= OnTargetLayerChanged;

((IEngineEditEvents_Event)pEditSketch).OnCurrentTaskChanged -= OnCurrentTaskChanged;

//Release object references.

pEngineEditor = null;

pEditSketch = null;

pEditLayer = null;

}

public string Name

{

get

{

// TODO: Add ArcGISClass1.Name getter implementation

return "ContourTask";

}

}

public string UniqueName

{

get

{

return "ContourTask";

}

}

public string GroupName

{

get

{

//This property allows groups to be created/used in the EngineEditTaskToolControl treeview.

//If an empty string is supplied the task will be appear in an "Other Tasks" group.

//In this example the Reshape Polyline_CSharp task will appear in the existing Modify Tasks group.

return "Other Tasks";

}

}

public void OnDeleteSketch()

{

// TODO: Add ArcGISClass1.OnDeleteSketch implementation

}

public void OnFinishSketch()

{

// TODO: Add ArcGISClass1.OnFinishSketch implementation

//get reference to featurelayer being edited

IFeatureLayer pFeatureLayer = pEditLayer.CurrentLayer as IFeatureLayer;

//get reference to the sketch geometry

IGeometry pPolyline = pEditSketch.Geometry;

if (pPolyline.IsEmpty == false)

{

ParaSetting pFormSetting = new ParaSetting(pFeatureLayer.FeatureClass);

pFormSetting.ShowDialog();

if (pFormSetting.DialogResult == DialogResult.OK)

{

pHeightName = pFormSetting.pFieldNames.Text;

pHeight = Convert.ToDouble(pFormSetting.dHeight.Text);

pInterval = Convert.ToDouble(pFormSetting.dInterval.Text);

pFormSetting.Dispose();

pFormSetting = null;

IFeatureCursor pFeatureCursor = GetFeatureCursor(pPolyline, pFeatureLayer.FeatureClass);

CalculateIntersect(pFeatureCursor, pPolyline);

MessageBox.Show("OK");

}

}

//refresh the display

IActiveView pActiveView = pEngineEditor.Map as IActiveView;

pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, (object)pFeatureLayer, pActiveView.Extent);

}

private IFeatureCursor GetFeatureCursor(IGeometry pGeometry, IFeatureClass pFeatureClass)

{

//空间过虑器的创建

ISpatialFilter pSpatialFilter = new SpatialFilter();

pSpatialFilter.Geometry = pGeometry;

//空间过虑器几何体实体

//空间过虑器参照系

//空间过虑器空间数据字段名

pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;

//空间过虑器空间关系类型

pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

//相交

IFeatureCursor pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);

return pFeatureCursor;

}

//起始等高线值

private double pHeight;

//等高线间距

private double pInterval;

//高程字段名

private string pHeightName;

private void CalculateIntersect(IFeatureCursor pFeatureCursor, IGeometry Geometry)

{

//要素游标

IMultipoint pIntersectionPoints = null;

//多点

IPointCollection pPointColl = null;

List<IFeature> pFeatureList = new List<IFeature>();

//和直线相交的要素集合,未排序

double[,] pIndex = null;

//距离和初始索引

if (pFeatureCursor == null)

{

return;

}

ITopologicalOperator pTopoOperator = Geometry as ITopologicalOperator;

IPointCollection pSketchPointColl = Geometry as IPointCollection;

//所画直线的起点

IPoint P0 = pSketchPointColl.get_Point(0);

IFeature pFeature = pFeatureCursor.NextFeature();

double HValue = 0;

int FldIndex = 0;

pFeatureList.Clear();

while ((pFeature != null))

{

//和直线相交的要素集合

pFeatureList.Add(pFeature);

//

pFeature = pFeatureCursor.NextFeature();

}

//此时pFeatureL中的等值线并不是按顺序(空间)排列,需要排序

//求出各交点到直线起点距离

int pCount = pFeatureList.Count;

pIndex = new double[2, pCount];

for (int i = 0; i <= pCount - 1; i++)

{

try

{

pFeature = pFeatureList[i];

//求交点:

pIntersectionPoints = pTopoOperator.Intersect(pFeature.Shape, esriGeometryDimension.esriGeometry0Dimension) as IMultipoint;

pPointColl = pIntersectionPoints as IPointCollection;

//QI

//原来序号

pIndex[0, i] = i;

//距离

pIndex[1, i] = GetDistace(P0, pPointColl.get_Point(0));

//下个要素

pFeature = pFeatureCursor.NextFeature();

}

catch (Exception e)

{

MessageBox.Show(e.ToString());

}

}

//排序:将和直线相交的等直线按与起点的距离排序,冒泡法

for (int i = 0; i <= pCount - 1; i++)

{

for (int j = i + 1; j <= pCount - 1; j++)

{

if (pIndex[1, j] < pIndex[1, i])

{

double pTempindex = pIndex[0, i];

pIndex[0, i] = pIndex[0, j];

pIndex[0, j] = pTempindex;

//交换索引

double pTemp = pIndex[1, i];

pIndex[1, i] = pIndex[1, j];

pIndex[1, j] = pTemp;

//交换距离

}

}

}

//开始高程赋值

HValue = pHeight;

try

{

for (int i = 0; i <= pCount - 1; i++)

{

pFeature = pFeatureList[i];

//获取高程字段的索引

FldIndex = pFeature.Fields.FindField(pHeightName);

//高程赋值

pFeature.set_Value(FldIndex, HValue as object);

//要素更新

pFeature.Store();

//Get the next feature and next H

HValue = HValue + pInterval;

}

}

catch (Exception e)

{

MessageBox.Show(e.ToString());

}

}

/// <summary>

/// 获取我们画的线和等高线之间的距离

/// </summary>

/// <param name="pPoint1"></param>

/// <param name="pPoint2"></param>

/// <returns></returns>

private double GetDistace(IPoint pPoint1, IPoint pPoint2)

{

return (pPoint1.X - pPoint2.X) * (pPoint1.X - pPoint2.X) + (pPoint1.Y - pPoint2.Y) * (pPoint1.Y - pPoint2.Y);

}

#endregion

public void OnTargetLayerChanged()

{

PerformSketchToolEnabledChecks();

}

void OnCurrentTaskChanged()

{

if (pEngineEditor.CurrentTask.Name == "CalculateContourTask")

{

PerformSketchToolEnabledChecks();

}

}

private void PerformSketchToolEnabledChecks()

{

if (pEditLayer == null)

return;

//Only enable the sketch tool if there is a polyline target layer.

if (pEditLayer.CurrentLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)

{

pEditSketch.GeometryType = esriGeometryType.esriGeometryNull;

return;

}

pEditSketch.GeometryType = esriGeometryType.esriGeometryPolyline;

}

}

}

效果如下:

task2

最新文章

  1. 弱省互测#0 t1
  2. jQuery动画特效实例教程
  3. W7无法更新
  4. How to build the theory on a specific system - may be just a brain storm[clue found]
  5. drupal记录(一)
  6. python学习之最简单的获取本机ip信息的小程序
  7. JVM的本地方法栈
  8. go 函数
  9. sphinx搜索引擎架构图
  10. 深入解析hasOwnProperty与isPrototypeOf
  11. Ubuntu zookeeper-3.5.0-alpha启动错误 zkEnv.sh: Syntax error: &quot;(&quot; unexpected (expecting &quot;fi&quot;)(转)
  12. HDU4027 Can you answer these queries? 线段树
  13. android 中使用缓存加载数据
  14. 使用mysqlbinlog工具进行基于位置或时间点的数据恢复
  15. jquery-制作选项卡
  16. 《DSP using MATLAB》示例Example6.4
  17. Haproxy的安装和配置示例
  18. ABP入门系列(20)——使用后台作业和工作者
  19. Webpack 2 视频教程 017 - Webpack 2 中分离打包项目代码与组件代码
  20. 如何在Windows系统中设置Python程序定时运行

热门文章

  1. TrueSec引导的Linux系统和安全检测工具预览
  2. golang filepath.Walk遍历指定目录下的所有文件
  3. IIS进程回收 空闲时Net线程未运行
  4. Eclipse中Git插件使用技巧:[5]还原文件
  5. Hi35xx NVR GDB调试
  6. POJ 3045 Cow Acrobats (最大化最小值)
  7. Spark MLlib LDA 源代码解析
  8. item-设置可见性
  9. php操作zip压缩文件
  10. HSV颜色模型