ArcGIS Engine 编辑- ITask
轉自原文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
?
轉載于:https://www.cnblogs.com/arxive/p/6262948.html
總結
以上是生活随笔為你收集整理的ArcGIS Engine 编辑- ITask的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python SQLAlchemy --
- 下一篇: 基于ELK的简单数据分析