FeatureClearCommand.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //======================================================================
  2. //
  3. //
  4. //
  5. // CLR 版本: 4.0.30319.1
  6. // 命名空间: Tofly.DataEditUI
  7. // 类 名: FeatureClearCommand
  8. // 创 建 人: 汤云伟
  9. // 创建时间: 2016/08/18 10:39: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.DataEdit.Util;
  38. using Tofly.GISUI.Utils;
  39. namespace Tofly.DataEditUI
  40. {
  41. /// <summary>
  42. /// 要素属性清空(不包含必要属性<录入人、录入时间等>
  43. /// </summary>
  44. [PluginComponentAttribute((long)Core.SysRegist.ModuleKey.Edit, PluginGISType.ArcGIS, GroupName = "编辑", Caption = "要素清空", Describe = "要素清空", IsCreate = true, States = Tofly.CoreUI.Control.Status.STATE_EDIT)]
  45. [Tofly.Core.Stereotype.Component(IsSingleton = "false", Name = "Tofly.DataEditUI.FeatureClearCommand")]
  46. public class FeatureClearCommand : GISPluginButtonBase
  47. {
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. private IMap _cmap;
  52. /// <summary>
  53. /// 当前选择可编辑要素字典
  54. /// </summary>
  55. private Dictionary<IFeatureLayer, List<IFeature>> _dicEditableFeatures;
  56. /// <summary>
  57. /// 管网图层集合
  58. /// </summary>
  59. private List<IFeatureLayer> _lstEditableGWLayer = new List<IFeatureLayer>();
  60. public FeatureClearCommand()
  61. {
  62. this.Enable = false;
  63. }
  64. public override void Create(IPluginContent content)
  65. {
  66. base.Create(content);
  67. this.content.ServiceLocator.ServiceValueChange += ServiceLocator_ServiceValueChange;
  68. }
  69. public override void Click()
  70. {
  71. try
  72. {
  73. if (this._cmap.SelectionCount == 0)
  74. {
  75. return;
  76. }
  77. if (this._dicEditableFeatures == null || this._dicEditableFeatures.Count == 0)
  78. {
  79. throw new System.Exception("无有效可编辑要素.");
  80. }
  81. Dictionary<IFeatureLayer, List<IFeature>> __dicSelFeaturesByLayer = new Dictionary<IFeatureLayer, List<IFeature>>();
  82. #region --------- 获取待清除要素集合 ---------
  83. if (this._dicEditableFeatures.Count > 1 || this._dicEditableFeatures.ElementAt(0).Value.Count > 1)
  84. {
  85. //实际清空要素选择窗口
  86. ISelectMultiEditFeature __frmSelMultiEditFeature = ContextRegistry.GetContext().GetObject("Tofly.DataEditUI.Win.FormSelectMultiEditFeature") as ISelectMultiEditFeature;
  87. __frmSelMultiEditFeature.CMap = this._cmap;
  88. __frmSelMultiEditFeature.LoadSelectableFeatures(this._dicEditableFeatures);
  89. if (__frmSelMultiEditFeature.ShowDialog(this.content.PluginMainForm) == DialogResult.OK)
  90. {
  91. __dicSelFeaturesByLayer = __frmSelMultiEditFeature.SelectedLayerFeatues;
  92. System.Windows.Forms.Application.DoEvents(); //确保窗口先关闭
  93. }
  94. __frmSelMultiEditFeature.Dispose();
  95. __frmSelMultiEditFeature = null;
  96. }
  97. else
  98. {
  99. __dicSelFeaturesByLayer.Add(this._dicEditableFeatures.ElementAt(0).Key, this._dicEditableFeatures.ElementAt(0).Value);
  100. }
  101. #endregion
  102. if (__dicSelFeaturesByLayer.Count > 0 && MessageManager.Show("确定清除设备要素数据?", "要素清除", MessageButton.YesNo, MessageType.Question) == MessageResult.Yes)
  103. {
  104. //进度窗口
  105. FormProgress __frmProgress = new FormProgress(tfProgressType.ProgressProcess, true)
  106. {
  107. StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen,
  108. ProgressMaxValue = __dicSelFeaturesByLayer.Sum(kvClassFeatures => kvClassFeatures.Value.Count)
  109. };
  110. __frmProgress.Show(this.content.PluginMainForm);
  111. System.Windows.Forms.Application.DoEvents();
  112. List<string> __lstExcludeFields = new List<string>(); //跳过无需清除的字段集合
  113. List<string> __lstMustClearedField = new List<string>() { "SID" }; //必须进行清空的字段集合
  114. List<Field> __lstClearedProperty = new List<Field>();
  115. int __iFeature = 0;
  116. IFeatureWorkspace __fwsEditing = __dicSelFeaturesByLayer.ElementAt(0).Key.FeatureClass.Workspace; //获取当前编辑工作空间
  117. __fwsEditing.StartEditOperation();
  118. try
  119. {
  120. #region --------- 遍历进行要素清除操作 ---------
  121. __lstExcludeFields.AddRange(RowBaseEx.ExcludeClearedFields()); //添加排除进行清理操作的字段集合
  122. __lstExcludeFields.AddRange(RowBaseEx.ReadOnlyFields()); //添加只读字段集合到不需要清除的字段
  123. __lstExcludeFields.RemoveAll(excField => __lstMustClearedField.Exists(mcField => excField.Equals(mcField, StringComparison.OrdinalIgnoreCase))); //去除必须清除的字段
  124. foreach (var __kvLayerFeatures in __dicSelFeaturesByLayer)
  125. {
  126. __lstClearedProperty.AddRange(__kvLayerFeatures.Key.FeatureClass.Fields.Where(fieldItem => fieldItem.IsGISField == false && fieldItem.IsKey == false && fieldItem.AllowNull));
  127. foreach (string __strExcField in __lstExcludeFields)
  128. {
  129. __lstClearedProperty.RemoveAll(cproperty => cproperty.FieldName.Equals(__strExcField, StringComparison.OrdinalIgnoreCase));
  130. }
  131. foreach (var __ftrCleared in __kvLayerFeatures.Value)
  132. {
  133. __iFeature += 1;
  134. __frmProgress.UpdateTitle(string.Format("正在清除第 {0} 个要素.", __iFeature));
  135. System.Windows.Forms.Application.DoEvents();
  136. foreach (var __cpField in __lstClearedProperty)
  137. {
  138. __ftrCleared.SetValue(__cpField.FieldName, DBNull.Value);
  139. }
  140. }
  141. __kvLayerFeatures.Key.FeatureClass.Save(__kvLayerFeatures.Value.Select(ftr => ftr as IRow).ToList()); //保存编辑
  142. }
  143. #endregion
  144. __fwsEditing.StopEditOperation();
  145. Dictionary<int, IFeatureWorkspace> __dicEditingWorkkspace = new Dictionary<int, IFeatureWorkspace>();
  146. __dicEditingWorkkspace.Add(1, __fwsEditing);
  147. EditorSurroundings.UpdateEditOperation(this.content, __dicEditingWorkkspace); //刷新编辑操作队列
  148. __frmProgress.Reset();
  149. MessageManager.Show(MessageType.Information, "成功完成选择要素的数据清空操作.", "要素清空");
  150. this._cmap.PartialRefresh(ViewDrawPhase.ViewGeography, null, this._cmap.Extent);
  151. }
  152. catch
  153. {
  154. __fwsEditing.AbortEditOperation();
  155. throw;
  156. }
  157. finally
  158. {
  159. //__frmProgress.Close();
  160. __frmProgress.Dispose();
  161. __frmProgress = null;
  162. __lstExcludeFields.Clear();
  163. __lstMustClearedField.Clear();
  164. __lstClearedProperty.Clear();
  165. }
  166. }
  167. }
  168. catch(System.Exception ex)
  169. {
  170. MessageManager.Show("清除失败: " + ex.Message, "要素清空");
  171. }
  172. }
  173. protected void ServiceLocator_ServiceValueChange(string key, object value)
  174. {
  175. if (key.Equals(ServiceLocatorKeys.IsMapEdit))
  176. {
  177. //this.Enable = content.ServiceLocator.GetInstance<bool>(ServiceLocatorKeys.IsMapEdit);
  178. this.ActiveViewContent_ActiveViewSelectionChanged(null);
  179. }
  180. else if (key.Equals(ServiceLocatorKeys.DocumentContent))
  181. {
  182. this._cmap = (this.content.ServiceLocator.GetInstance<IControl>(ServiceLocatorKeys.MapControl) as IMapControl).Map;
  183. var documentcontent = content.ServiceLocator.GetInstance<IDocumentContent>(ServiceLocatorKeys.DocumentContent);
  184. documentcontent.ActiveViewContent.ActiveViewSelectionChanged -= ActiveViewContent_ActiveViewSelectionChanged;
  185. documentcontent.ActiveViewContent.ActiveViewSelectionChanged += ActiveViewContent_ActiveViewSelectionChanged;
  186. }
  187. }
  188. protected void ActiveViewContent_ActiveViewSelectionChanged(ActiveViewSelectionChangedEventArgs e)
  189. {
  190. try
  191. {
  192. this.Enable = false;
  193. this._dicEditableFeatures = null;
  194. if (content.ServiceLocator.GetInstance<bool>(ServiceLocatorKeys.IsMapEdit))
  195. {
  196. if (this._cmap.SelectionCount > 0)
  197. {
  198. if (this._lstEditableGWLayer.Count == 0)
  199. {
  200. this._lstEditableGWLayer.AddRange(FeatureLayerHelper.GetFeatureLayersByDataset(this._cmap, AppConfig.GWDatasetName));
  201. this._lstEditableGWLayer.RemoveAll(flyrItem => flyrItem.FeatureClass.Workspace.IsBeingEdited() == false); //移除非编辑状态的要素图层
  202. }
  203. this._dicEditableFeatures = MapSelectionHelper.GetSelectedFeaturesByLayer(this._cmap, this._lstEditableGWLayer, true);
  204. this.Enable = (this._dicEditableFeatures.Count > 0); //分离出可编辑要素集合
  205. }
  206. }
  207. }
  208. catch
  209. {
  210. this.Enable = false;
  211. }
  212. }
  213. }
  214. }