FeatureDeleteCommand.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //======================================================================
  2. //
  3. //
  4. //
  5. // CLR 版本: 4.0.30319.1
  6. // 命名空间: Tofly.DataEditUI
  7. // 类 名: FeatureDeleteCommand
  8. // 创 建 人: 汤云伟
  9. // 创建时间: 2016/09/28 14:10:00
  10. // 修 改 人:
  11. // 修改时间:
  12. //
  13. //======================================================================
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Text;
  17. using System.Linq;
  18. using Tofly.Core.Context.Support;
  19. using Tofly.Core.Context;
  20. using Tofly.Core.ServiceLocator;
  21. using Tofly.CoreUI.Plot;
  22. using Tofly.CoreUI.Control;
  23. using Tofly.CoreUI.Message;
  24. using Tofly.CoreUI.Win.Form.Forms;
  25. using Tofly.CoreUI.Utils;
  26. using Tofly.Data.Metadata;
  27. using Tofly.GIS.Display;
  28. using Tofly.GISUI.Content;
  29. using Tofly.GISUI.Controls;
  30. using Tofly.GIS.Geometry;
  31. using Tofly.GIS.Carto;
  32. using Tofly.GIS.SpatialDatabase;
  33. using Tofly.GIS.Util;
  34. using Tofly.DataEditUI.Properties;
  35. using Tofly.GISUI.Plugin;
  36. using Tofly.Data.General;
  37. using Tofly.GISUI.Utils;
  38. namespace Tofly.DataEditUI
  39. {
  40. /// <summary>
  41. /// 删除命令(要素、元素等)
  42. /// </summary>
  43. [PluginComponentAttribute((long)Core.SysRegist.ModuleKey.Edit, PluginGISType.ArcGIS, GroupName = "编辑", Caption = "删除", Describe = "删除所选元素", IsCreate = true, States = Tofly.CoreUI.Control.Status.STATE_EDIT)]
  44. [Tofly.Core.Stereotype.Component(IsSingleton = "false", Name = "Tofly.DataEditUI.FeatureDeleteCommand")]
  45. public class FeatureDeleteCommand : EditCommand
  46. {
  47. public FeatureDeleteCommand()
  48. : base()
  49. {
  50. EditType = EditTaskType.Delete;
  51. base.editName = "GeoEditDelete";
  52. editTableTypes = new List<TableType>();
  53. editTableTypes.Add(TableType.PolylineFeatureClass);
  54. editTableTypes.Add(TableType.PolygoneFeatureClass);
  55. editTableTypes.Add(TableType.PointFeatureClass);
  56. selType = selectType.HaveTo;
  57. pViewDrawPhase = ViewDrawPhase.ViewGeography | ViewDrawPhase.ViewGeoSelection;
  58. }
  59. public override void DoTask()
  60. {
  61. try
  62. {
  63. if (this.pMap.SelectionCount == 0)
  64. {
  65. return;
  66. }
  67. List<IFeatureLayer> __lstMapLayers = new List<IFeatureLayer>(this.pMap.GetFeatureLayersByMap());
  68. DataEdit.GeoEdit.FeatureLayerSel[] __arrEditableLayers = ServiceLocatorFactory.ServiceLocator.GetInstance<DataEdit.GeoEdit.FeatureLayerSel[]>(ServiceLocatorKeys.EditFeatureLayers);
  69. List<IFeatureLayer> __lstEditableFeatureLayer = new List<IFeatureLayer>(__arrEditableLayers.Select(featLayerSel => featLayerSel.FeatureLayer));
  70. List<IFeatureLayer> __lstUneditableLayer = (from flayer in __lstMapLayers
  71. where __lstEditableFeatureLayer.Where(elayer => elayer.FeatureClass.ObjectClassID == flayer.FeatureClass.ObjectClassID).Count() == 0
  72. select flayer).ToList();
  73. Dictionary <IFeatureLayer, List<IFeature>> __dicEditableFeaturesByLayer = MapSelectionHelper.GetSelectedFeaturesByLayer(this.pMap, __lstEditableFeatureLayer);
  74. Dictionary<IFeatureLayer, List<IFeature>> __dicUneidtableSelFeatures = MapSelectionHelper.GetSelectedFeaturesByLayer(this.pMap, __lstUneditableLayer);
  75. if (__dicEditableFeaturesByLayer.Count > 0)
  76. {
  77. #region 不可编辑图层要素的处理
  78. if (__dicUneidtableSelFeatures.Count > 0)
  79. {
  80. foreach (var __kvUneditableLayer in __dicUneidtableSelFeatures)
  81. {
  82. __kvUneditableLayer.Key.ClearSelection(); //清除选择集,避免错误删除操作
  83. }
  84. this.pMap.ActiveView.PartialRefresh(ViewDrawPhase.ViewGeoSelection, null, this.pMap.ActiveView.Extent); //刷新地图显示
  85. }
  86. #endregion
  87. IEnvelope __gEnvRefreshed = MapSelectionHelper.GetMapSelectionEnvelope(this.pMap); //获取选择集范围
  88. if (__dicEditableFeaturesByLayer.Count > 1 || __dicEditableFeaturesByLayer.ElementAt(0).Value.Count > 1)
  89. {
  90. //Dictionary<IFeatureLayer, List<IFeature>> __dicSelFeaturesByLayer = new Dictionary<IFeatureLayer, List<IFeature>>();
  91. //IFeatureLayer __flayerCurrent;
  92. //foreach (var __kvSelFeaturesByClass in __dicEditableFeaturesByLayer)
  93. //{
  94. // __flayerCurrent = __lstEditableFeatureLayer.Find(flayer => flayer.FeatureClass.ObjectClassID == __kvSelFeaturesByClass.Key.ObjectClassID && flayer.FeatureClass.Workspace.WorkspaceType == WorkspaceType.Oracle);
  95. // __dicSelFeaturesByLayer.Add(__flayerCurrent, __kvSelFeaturesByClass.Value);
  96. //}
  97. //实际删除要素选择窗口
  98. ISelectMultiEditFeature __frmSelMultiEditFeature = ContextRegistry.GetContext().GetObject("Tofly.DataEditUI.Win.FormSelectMultiEditFeature") as ISelectMultiEditFeature;
  99. __frmSelMultiEditFeature.CMap = this.pMap;
  100. __frmSelMultiEditFeature.LoadSelectableFeatures(__dicEditableFeaturesByLayer);
  101. if (__frmSelMultiEditFeature.ShowDialog(this.content.PluginMainForm) == DialogResult.OK)
  102. {
  103. var __dicSelFeaturesByLayer = __frmSelMultiEditFeature.SelectedLayerFeatues;
  104. System.Windows.Forms.Application.DoEvents(); //确保窗口先关闭
  105. if (__dicSelFeaturesByLayer.Count > 0 && MessageManager.Show("是否确定删除?", "要素删除", MessageButton.YesNo, MessageType.Question) == MessageResult.Yes)
  106. {
  107. WaitManager.Show("正在删除要素……");
  108. this.DoDeleteAction(__dicSelFeaturesByLayer, this.pMap, __gEnvRefreshed); //执行删除操作
  109. }
  110. }
  111. __frmSelMultiEditFeature.Dispose();
  112. __frmSelMultiEditFeature = null;
  113. __dicUneidtableSelFeatures.Clear(); //= new Dictionary<IFeatureClass, List<IFeature>>(); //重置不可编辑要素集合
  114. }
  115. else
  116. {
  117. if (MessageManager.Show("是否确定删除?", "要素删除", MessageButton.YesNo, MessageType.Question) == MessageResult.Yes)
  118. {
  119. WaitManager.Show("正在删除要素……");
  120. base.DoTask(); //执行删除编辑任务
  121. this.DeleteSelectedElements(this.pMap.ActiveView); //清除选择的图形
  122. }
  123. else
  124. {
  125. __dicUneidtableSelFeatures.Clear(); //= new Dictionary<IFeatureClass, List<IFeature>>(); //重置不可编辑要素集合
  126. }
  127. }
  128. this.pMap.ActiveView.PartialRefresh(ViewDrawPhase.ViewGeoSelection | ViewDrawPhase.ViewGraphics, null, __gEnvRefreshed); //刷新地图显示
  129. var __docContent = content.ServiceLocator.GetInstance<IDocumentContent>(ServiceLocatorKeys.DocumentContent);
  130. IActiveView __viewSelction = __docContent.ActiveViewContent.ActiveView; //广播地图选择集变化的视图对象
  131. __viewSelction.PartialRefresh(ViewDrawPhase.ViewGeoSelection, null, __gEnvRefreshed); //__viewSelction.Extent
  132. WaitManager.Close(); //提前关闭提示
  133. }
  134. bool __bExistUnEditableGW = (__dicUneidtableSelFeatures.Keys.Count(flayer => flayer.FeatureClass.FeatureDataset != null && flayer.FeatureClass.FeatureDataset.InnerObject != null && flayer.FeatureClass.FeatureDataset.ShortName.Equals(AppConfig.GWDatasetName, StringComparison.OrdinalIgnoreCase)) > 0);
  135. if (__dicUneidtableSelFeatures.Count > 0 && __bExistUnEditableGW)
  136. {
  137. MessageManager.Show("删除错误: 选择集中包含不在管辖片区内管网数据,这部分数据无权删除!", "删除");
  138. }
  139. }
  140. catch(System.Exception ex)
  141. {
  142. WaitManager.Close(); //提前关闭提示
  143. if (ex is System.Runtime.InteropServices.COMException && (ex as System.Runtime.InteropServices.COMException).ErrorCode == -2147205088)
  144. {
  145. MessageManager.Show("删除失败: 选择要素中存在错误连接关系的数据,请先纠正处理后,再进行删除.", "删除要素");
  146. }
  147. else
  148. {
  149. MessageManager.Show("删除失败:" + ex.Message, "删除要素");
  150. }
  151. var __docContent = content.ServiceLocator.GetInstance<IDocumentContent>(ServiceLocatorKeys.DocumentContent);
  152. IActiveView __viewSelction = __docContent.ActiveViewContent.ActiveView; //广播地图选择集变化的视图对象
  153. __viewSelction.PartialRefresh(ViewDrawPhase.ViewGeoSelection | ViewDrawPhase.ViewGeography, null, __viewSelction.Extent);
  154. }
  155. }
  156. /// <summary>
  157. /// 执行删除操作(地图选择集的更新处理=>基类的删除操作)
  158. /// </summary>
  159. /// <param name="layerFeatures">选择的要素字典</param>
  160. /// <param name="cmap"></param>
  161. /// <param name="envelope">操作范围</param>
  162. private void DoDeleteAction(Dictionary<IFeatureLayer, List<IFeature>> layerFeatures, IMap cmap, IEnvelope envelope)
  163. {
  164. try
  165. {
  166. if (layerFeatures.Count > 0)
  167. {
  168. cmap.ClearSelection();
  169. cmap.ActiveView.PartialRefresh(ViewDrawPhase.ViewGeoSelection, null, envelope);
  170. List<int> __lstFeatureOIDs;
  171. foreach (var __kvLayerFeature in layerFeatures)
  172. {
  173. __lstFeatureOIDs = __kvLayerFeature.Value.Select(ftrItem => ftrItem.OID).ToList();
  174. __kvLayerFeature.Key.AddSelectFeatureList(__lstFeatureOIDs);
  175. __lstFeatureOIDs.Clear();
  176. }
  177. cmap.ActiveView.PartialRefresh(ViewDrawPhase.ViewGeoSelection, null, envelope);
  178. System.Windows.Forms.Application.DoEvents(); //确保用户选择的实际删除要素选择状态
  179. base.DoTask(); //执行删除编辑任务
  180. this.DeleteSelectedElements(this.pMap.ActiveView); //清除选择的图形
  181. }
  182. }
  183. catch (System.Exception ex)
  184. {
  185. if (ex is System.Runtime.InteropServices.COMException && (ex as System.Runtime.InteropServices.COMException).ErrorCode == -2147205088)
  186. {
  187. MessageManager.Show("删除失败: 选择要素中存在错误连接关系的数据,请先纠正处理后,再进行删除.", "删除要素");
  188. }
  189. else
  190. {
  191. MessageManager.Show("删除失败:" + ex.Message, "删除要素");
  192. }
  193. var __docContent = content.ServiceLocator.GetInstance<IDocumentContent>(ServiceLocatorKeys.DocumentContent);
  194. IActiveView __viewSelction = __docContent.ActiveViewContent.ActiveView; //广播地图选择集变化的视图对象
  195. __viewSelction.PartialRefresh(ViewDrawPhase.ViewGeoSelection | ViewDrawPhase.ViewGeography, null, __viewSelction.Extent);
  196. }
  197. }
  198. /// <summary>
  199. /// 元素图形删除
  200. /// </summary>
  201. /// <param name="acView"></param>
  202. private void DeleteSelectedElements(IActiveView acView)
  203. {
  204. IElement[] __arrElement = (acView as IGraphicsContainerSelect).SelectedElements;
  205. IGraphicsContainer __graContainer = acView.GraphicsContainer;
  206. foreach(var __element in __arrElement)
  207. {
  208. __graContainer.DeleteElement(__element);
  209. }
  210. }
  211. }
  212. }