using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using KGIS.Framework.Utils.Helper; using KGIS.Plugin.LayerProperty.Interface; using KGIS.Plugin.LayerProperty.Utils; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace KGIS.Plugin.LayerProperty.View.UC_Controls { public partial class UCUniqueValueManyFieldsRenderer : XtraUserControl, IUCPropertyPageEx, IPropertyPage { private const string CONST_SYMBOL = "F_Symbol"; private const string CONST_VALUE = "F_Value"; private const string CONST_LABEL = "F_Label"; private const string CONST_BITMAP = "F_BITMAP"; private DataTable m_SymbolTable; private IUniqueValueRenderer m_LyrRender; private ISymbol m_SelectedSymbol; private DataRow m_SelectedRow; private IStyleGalleryClass m_SymbologyStyleClass; private ISymbol m_DefaultSymbol; private IRotationRenderer mRotationrenderer; private IColorRamp m_ColorRamp; private IFeatureRenderer m_CurFeatureRenderer; private esriGeometryType GeometryType; private IFeatureLayer2 m_MatchLayer; private FormPointRatation frmPointRatation; private string m_ValueField = ""; private FormAddValue frmAddRendValue; private bool isDirty; public event System.EventHandler EditorChanged; public bool IsPageDirty { get { return this.isDirty; } set { this.isDirty = value; } } public int Priority { get { throw new Exception("The method or operation is not implemented."); } set { throw new Exception("The method or operation is not implemented."); } } public string Title { get { throw new Exception("The method or operation is not implemented."); } set { throw new Exception("The method or operation is not implemented."); } } public UCUniqueValueManyFieldsRenderer() { InitializeComponent(); } public void InitUC(object layerProperty) { try { this.m_MatchLayer = (layerProperty as IFeatureLayer2); if (this.m_MatchLayer != null) { this.btnRotateSet.Enabled = false; this.GeometryType = this.m_MatchLayer.ShapeType; switch (this.GeometryType) { case esriGeometryType.esriGeometryPoint: this.m_SymbologyStyleClass = new MarkerSymbolStyleGalleryClassClass(); this.btnRotateSet.Enabled = true; break; case esriGeometryType.esriGeometryPolyline: this.m_SymbologyStyleClass = new LineSymbolStyleGalleryClassClass(); break; case esriGeometryType.esriGeometryPolygon: this.m_SymbologyStyleClass = new FillSymbolStyleGalleryClassClass(); break; } this.InitSymbolTable(); this.BindGrid(); this.m_CurFeatureRenderer = (layerProperty as IGeoFeatureLayer).Renderer; this.m_LyrRender = (this.m_CurFeatureRenderer as IUniqueValueRenderer); if (this.m_LyrRender == null) { this.m_LyrRender = new UniqueValueRendererClass(); this.InitDefaultRender(); this.ucCRColors.InitColorRamp(this.m_LyrRender.ColorScheme); try { this.m_ValueField = this.m_MatchLayer.FeatureClass.Fields.get_Field(0).Name; Utils.Util.InitFieldList(ref this.cmbValueField1, this.m_MatchLayer.FeatureClass.Fields, this.m_ValueField, false, false, true, false); Utils.Util.InitFieldList(ref this.cmbValueField2, this.m_MatchLayer.FeatureClass.Fields, "", true, false, true, false); Utils.Util.InitFieldList(ref this.cmbValueField3, this.m_MatchLayer.FeatureClass.Fields, "", true, false, true, false); goto IL_1BE; } catch { goto IL_1BE; } } this.ucCRColors.InitColorRamp(this.m_LyrRender.ColorScheme); this.m_DefaultSymbol = this.m_LyrRender.DefaultSymbol; this.chkUseDefaultSymbol.Checked = this.m_LyrRender.UseDefaultSymbol; this.LoadDefaultRender(); IL_1BE: this.RefreshDefaultSymbol(); } } catch (Exception ex) { //RdbUtil.AddException(ex); } } public void Write2Prop() { try { (this.m_MatchLayer as IGeoFeatureLayer).Renderer = this.m_CurFeatureRenderer; this.isDirty = false; } catch (Exception ex) { //RdbUtil.AddException(ex); } } public bool CanWrite2Prop() { this.SetUniqeRender(); return this.m_CurFeatureRenderer != null && this.isDirty; } public void SetDefaultValue(object value) { this.m_LyrRender = (value as IUniqueValueRenderer); this.InitSymbolTable(); this.BindGrid(); this.LoadDefaultRender(); this.InvokeEditValueChanged(this, null); } private void InvokeEditValueChanged(object sender, System.EventArgs e) { if (this.EditorChanged != null) { this.EditorChanged(sender, e); } this.isDirty = true; } private void InitSymbolTable() { this.m_SymbolTable = new DataTable(); DataColumn dataColumn = new DataColumn(); dataColumn.DataType = Type.GetType("System.Object"); dataColumn.ColumnName = "F_BITMAP"; dataColumn.Caption = "F_BITMAP"; this.m_SymbolTable.Columns.Add(dataColumn); dataColumn = new DataColumn(); dataColumn.DataType = Type.GetType("System.String"); dataColumn.ColumnName = "F_Value"; dataColumn.Caption = "F_Value"; this.m_SymbolTable.Columns.Add(dataColumn); dataColumn = new DataColumn(); dataColumn.DataType = Type.GetType("System.String"); dataColumn.ColumnName = "F_Label"; dataColumn.Caption = "F_Label"; this.m_SymbolTable.Columns.Add(dataColumn); dataColumn = new DataColumn(); dataColumn.DataType = Type.GetType("System.Object"); dataColumn.ColumnName = "F_Symbol"; dataColumn.Caption = "F_Symbol"; this.m_SymbolTable.Columns.Add(dataColumn); } private void BindGrid() { this.gridSymCtrl.BeginInit(); this.gridSymCtrl.DataSource = this.m_SymbolTable; this.grdSymbol.FieldName = "F_BITMAP"; this.grdSymbolVal.FieldName = "F_Value"; this.grdLabel.FieldName = "F_Label"; this.gridSymCtrl.EndInit(); } private void InitDefaultRender() { this.m_DefaultSymbol = RenderUtil.GetDefaultSymbol(this.m_MatchLayer.FeatureClass.ShapeType); } private void LoadDefaultRender() { try { try { this.m_ValueField = this.m_LyrRender.get_Field(0); Utils.Util.InitFieldList(ref this.cmbValueField1, this.m_MatchLayer.FeatureClass.Fields, this.m_ValueField, false, false, true, false); Utils.Util.InitFieldList(ref this.cmbValueField2, this.m_MatchLayer.FeatureClass.Fields, "", true, false, true, false); Utils.Util.InitFieldList(ref this.cmbValueField3, this.m_MatchLayer.FeatureClass.Fields, "", true, false, true, false); if (this.m_LyrRender.FieldCount > 1) { Utils.Util.InitFieldList(ref this.cmbValueField2, this.m_MatchLayer.FeatureClass.Fields, this.m_LyrRender.get_Field(1), true, false, true, false); } if (this.m_LyrRender.FieldCount == 3) { Utils.Util.InitFieldList(ref this.cmbValueField3, this.m_MatchLayer.FeatureClass.Fields, this.m_LyrRender.get_Field(2), true, false, true, false); } } catch { } this.m_DefaultSymbol = this.m_LyrRender.DefaultSymbol; this.RefreshDefaultSymbol(); RenderUtil.SetUniqueValuesToTable(this.m_LyrRender, this.m_SymbologyStyleClass, this.m_SymbolTable); } catch (Exception ex) { //RdbUtil.AddException(ex); } } private void InitRowData(Bitmap bp, string uValue, string uLabel, ISymbol symbol, ref DataRow pRow) { pRow["F_BITMAP"] = bp; pRow["F_Value"] = uValue; pRow["F_Label"] = uLabel; pRow["F_Symbol"] = symbol; } private ISymbol GetSymbol(IColor pColor) { ISymbol symbol = null; if (this.m_DefaultSymbol != null) { IClone clone = this.m_DefaultSymbol as IClone; symbol = (clone.Clone() as ISymbol); } try { esriGeometryType geometryType = this.GeometryType; switch (geometryType) { case esriGeometryType.esriGeometryPoint: { if (symbol == null) { symbol = (new SimpleMarkerSymbolClass { Color = pColor } as ISymbol); goto IL_F1; } IMarkerSymbol markerSymbol = symbol as IMarkerSymbol; if (markerSymbol == null) { ISymbol result = symbol; return result; } markerSymbol.Color = pColor; goto IL_F1; } case esriGeometryType.esriGeometryMultipoint: goto IL_F1; case esriGeometryType.esriGeometryPolyline: break; case esriGeometryType.esriGeometryPolygon: { if (symbol == null) { symbol = (new SimpleFillSymbolClass { Color = pColor } as ISymbol); goto IL_F1; } IFillSymbol fillSymbol = symbol as IFillSymbol; if (fillSymbol == null) { ISymbol result = symbol; return result; } fillSymbol.Color = pColor; goto IL_F1; } default: if (geometryType != esriGeometryType.esriGeometryLine) { goto IL_F1; } break; } if (symbol == null) { symbol = (new SimpleLineSymbolClass { Color = pColor } as ISymbol); } else { ILineSymbol lineSymbol = symbol as ILineSymbol; if (lineSymbol == null) { ISymbol result = symbol; return result; } lineSymbol.Color = pColor; } IL_F1:; } catch (Exception ex) { //RdbUtil.AddException(ex); } return symbol; } private void RefreshDefaultSymbol() { this.picDefaultSymbol.Image = RenderUtil.GetSymbolBitMap(this.picDefaultSymbol.Width - 1, this.picDefaultSymbol.Height - 1, this.m_SymbologyStyleClass, this.m_DefaultSymbol); } private void SetUniqeRender() { this.m_LyrRender.RemoveAllValues(); if (this.m_MatchLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint && this.mRotationrenderer != null) { this.m_LyrRender = (IUniqueValueRenderer)this.mRotationrenderer; } List selectedFields = this.GetSelectedFields(); this.m_LyrRender.FieldCount = selectedFields.Count; string text = ""; for (int i = 0; i < selectedFields.Count; i++) { if (text == "") { text = selectedFields[i].Name; } else { text = text + "," + selectedFields[i].Name; } this.m_LyrRender.set_Field(i, selectedFields[i].Name); this.m_LyrRender.set_FieldType(i, selectedFields[i].Type == esriFieldType.esriFieldTypeString); } if (this.chkUseDefaultSymbol.Checked) { this.m_LyrRender.DefaultSymbol = this.m_DefaultSymbol; this.m_LyrRender.UseDefaultSymbol = this.chkUseDefaultSymbol.Checked; } else { this.m_LyrRender.DefaultSymbol = null; this.m_LyrRender.UseDefaultSymbol = false; } RenderUtil.SetUniqueValuesFromTable(this.m_LyrRender, text, this.m_SymbolTable); if (this.m_ColorRamp == null) { this.m_ColorRamp = this.ucCRColors.GetSelectColorRamp(); } if (this.m_ColorRamp != null) { this.m_LyrRender.ColorScheme = this.m_ColorRamp.Name; } this.m_CurFeatureRenderer = (this.m_LyrRender as IFeatureRenderer); } private List GetSelectedFields() { List list = new List(); ItemInfo itemInfo = this.cmbValueField1.SelectedItem as ItemInfo; ItemInfo itemInfo2 = this.cmbValueField2.SelectedItem as ItemInfo; ItemInfo itemInfo3 = this.cmbValueField3.SelectedItem as ItemInfo; if (itemInfo == null) { return list; } list.Add(itemInfo.InnerValue); if (itemInfo2.InnerValue != null) { list.Add(itemInfo2.InnerValue); } if (itemInfo3.InnerValue != null) { list.Add(itemInfo3.InnerValue); } return list; } private void ControlsEnable(bool enable) { base.Enabled = enable; this.btnAddVal.Enabled = enable; this.btnDel.Enabled = enable; this.btnDelAll.Enabled = enable; } private void btnAddAll_Click(object sender, System.EventArgs e) { try { this.ControlsEnable(false); if (this.m_MatchLayer == null) { return; } List selectedFields = this.GetSelectedFields(); this.m_SymbolTable.Rows.Clear(); string text = null; IDisplayTable displayTable = (IDisplayTable)this.m_MatchLayer; ICursor cursor = displayTable.DisplayTable.Search(null, false); IEnumerator enumerator = null; int num = 0; if (selectedFields.Count > 1) { IList uniqueValuesManyFields = Utils.Util.GetUniqueValuesManyFields(selectedFields, cursor); enumerator = uniqueValuesManyFields.GetEnumerator(); num = uniqueValuesManyFields.Count; } else { IDataStatistics dataStatistics = new DataStatisticsClass(); dataStatistics.Cursor = cursor; dataStatistics.Field = selectedFields[0].Name; dataStatistics.SampleRate = 2000; enumerator = dataStatistics.UniqueValues; num = dataStatistics.UniqueValueCount; } Application.DoEvents(); bool flag = false; this.m_ColorRamp = this.ucCRColors.GetSelectColorRamp(); bool flag2 = false; bool flag3 = true; int num2 = 0; try { this.m_ColorRamp.Size = num; Application.DoEvents(); if (num >= 500 && !flag) { if (MessageHelper.ShowYesNoAndTips("找到的唯一值个数大于500个,是否继续生成唯一值的完整列表?这会消耗较长时间。") == DialogResult.No) { flag3 = false; this.m_ColorRamp.Size = 500; } flag = true; } this.m_ColorRamp.CreateRamp(out flag2); } catch (Exception) { this.ControlsEnable(true); return; } IColor color = null; ISymbol symbol = null; DataRow row = null; enumerator.Reset(); while (enumerator.MoveNext()) { row = this.m_SymbolTable.NewRow(); try { text = Convert.ToString(enumerator.Current); } catch (Exception) { } try { color = this.m_ColorRamp.get_Color(num2); } catch (Exception) { } if (color != null) { symbol = this.GetSymbol(color); } if (symbol == null) { symbol = this.m_DefaultSymbol; } Bitmap symbolBitMap = RenderUtil.GetSymbolBitMap(40, 20, this.m_SymbologyStyleClass, symbol); this.InitRowData(symbolBitMap, text, text, symbol, ref row); this.m_SymbolTable.Rows.Add(row); if (num >= 500 && !flag) { if (MessageHelper.ShowYesNoAndTips("找到的唯一值个数大于500个,是否继续生成唯一值的完整列表?这会消耗较长时间。") == DialogResult.No) { flag3 = false; } flag = true; } if (!flag3 && num2 >= 500) { break; } num2++; } this.InvokeEditValueChanged(sender, e); Marshal.ReleaseComObject(cursor); } catch (Exception ex) { //RdbUtil.AddException(ex); MessageHelper.ShowError(ex.Message); } this.ControlsEnable(true); } private void btnDelAll_Click(object sender, System.EventArgs e) { this.m_SymbolTable.Rows.Clear(); this.m_SymbolTable.AcceptChanges(); this.InvokeEditValueChanged(sender, e); } private void btnDel_Click(object sender, System.EventArgs e) { if (this.m_SelectedRow == null) { return; } try { this.m_SymbolTable.Rows.Remove(this.m_SelectedRow); } catch (Exception ex) { //RdbUtil.AddException(ex); } this.InvokeEditValueChanged(sender, e); } private void gridSymView_MouseDown(object sender, MouseEventArgs e) { try { GridHitInfo gridHitInfo = this.gridSymView.CalcHitInfo(new System.Drawing.Point(e.X, e.Y)); if (gridHitInfo.HitTest == GridHitTest.RowCell) { this.m_SelectedRow = this.gridSymView.GetDataRow(gridHitInfo.RowHandle); this.m_SelectedSymbol = (this.m_SelectedRow["F_Symbol"] as ISymbol); } } catch (Exception ex) { //RdbUtil.AddException(ex); } } private void btnDefaultSymbol_Click(object sender, System.EventArgs e) { try { IDoStyleGalleryItem doStyleGalleryItem = RenderUtil.OpenStyleSelector(this.GeometryType); if (doStyleGalleryItem != null) { this.m_DefaultSymbol = (doStyleGalleryItem.Item.Item as ISymbol); this.RefreshDefaultSymbol(); } this.InvokeEditValueChanged(sender, e); } catch (Exception ex) { //RdbUtil.AddException(ex); } } private void btnRotateSet_Click(object sender, System.EventArgs e) { try { IGeoFeatureLayer geoFeatureLayer = this.m_MatchLayer as IGeoFeatureLayer; IFeatureRenderer renderer = geoFeatureLayer.Renderer; if (this.frmPointRatation == null) { this.frmPointRatation = new FormPointRatation(); } if (this.frmPointRatation.Init(this.m_MatchLayer, renderer)) { if (this.frmPointRatation.ShowDialog() == DialogResult.OK) { this.mRotationrenderer = this.frmPointRatation.RotationRenderer; } this.InvokeEditValueChanged(sender, e); this.btnRotateSet.Focus(); } } catch (Exception ex) { //RdbUtil.AddException(ex); } } private void gridSymView_DoubleClick(object sender, System.EventArgs e) { } private void gridSymCtrl_MouseDoubleClick(object sender, MouseEventArgs e) { try { if (this.m_SelectedSymbol != null) { if (this.m_MatchLayer != null) { this.gridSymView.CalcHitInfo(System.Windows.Forms.Control.MousePosition); ISymbol symbol = this.m_SelectedSymbol; symbol = (RenderUtil.OpenStyleSelector(this.GeometryType, symbol) as ISymbol); if (symbol != null) { this.m_SelectedRow["F_BITMAP"] = RenderUtil.GetSymbolBitMap(40, 20, this.m_SymbologyStyleClass, symbol); this.m_SelectedRow["F_Symbol"] = symbol; this.m_SymbolTable.AcceptChanges(); this.InvokeEditValueChanged(sender, e); } } } } catch (Exception ex) { //RdbUtil.AddException(ex); } } private void cmbFields_EditValueChanged(object sender, System.EventArgs e) { try { this.m_SymbolTable.Rows.Clear(); this.m_SymbolTable.AcceptChanges(); this.InvokeEditValueChanged(sender, e); } catch (Exception ex) { //RdbUtil.AddException(ex); } } private void ucCRColors_SelectIndexChanged(IColorRamp pColorRamp, string name) { try { this.m_ColorRamp = pColorRamp; if (this.m_ColorRamp != null) { bool flag = false; this.m_ColorRamp.Size = this.m_SymbolTable.Rows.Count; Application.DoEvents(); this.m_ColorRamp.CreateRamp(out flag); if (flag) { IColor color = null; ISymbol symbol = null; for (int i = 0; i < this.m_SymbolTable.Rows.Count; i++) { DataRow dataRow = this.m_SymbolTable.Rows[i]; try { color = this.m_ColorRamp.get_Color(i); } catch (Exception) { } if (color != null) { symbol = this.GetSymbol(color); } if (symbol == null) { symbol = this.m_DefaultSymbol; } Bitmap symbolBitMap = RenderUtil.GetSymbolBitMap(40, 20, this.m_SymbologyStyleClass, symbol); this.InitRowData(symbolBitMap, dataRow["F_Value"].ToString(), dataRow["F_Label"].ToString(), symbol, ref dataRow); } this.m_SymbolTable.AcceptChanges(); this.InvokeEditValueChanged(null, null); } } } catch (Exception ex) { //RdbUtil.AddException(ex); } } private void gridSymCtrl_MouseDown(object sender, MouseEventArgs e) { } private void gridSymView_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e) { try { this.m_SelectedRow = this.gridSymView.GetDataRow(e.FocusedRowHandle); if (this.m_SelectedRow != null) { this.btnDel.Enabled = true; } } catch (Exception ex) { //RdbUtil.AddException(ex); } } private void picDefaultSymbol_Click(object sender, System.EventArgs e) { try { this.Cursor = Cursors.WaitCursor; this.m_DefaultSymbol = (RenderUtil.OpenStyleSelector(this.GeometryType, this.m_DefaultSymbol) as ISymbol); if (this.m_DefaultSymbol == null) { this.Cursor = Cursors.Default; return; } this.RefreshDefaultSymbol(); this.InvokeEditValueChanged(sender, e); } catch (Exception ex) { //RdbUtil.AddException(ex); } this.Cursor = Cursors.Default; } private void cmbFields_SelectedIndexChanged(object sender, System.EventArgs e) { ItemInfo itemInfo = this.cmbValueField1.SelectedItem as ItemInfo; if (itemInfo != null) { this.m_ValueField = itemInfo.InnerValue.Name; } } public int Activate() { throw new Exception("The method or operation is not implemented."); } public void Deactivate() { throw new Exception("The method or operation is not implemented."); } public void Cancel() { throw new Exception("The method or operation is not implemented."); } private void btnAddVal_Click(object sender, System.EventArgs e) { if (this.cmbValueField1.Text.Length == 0) { MessageHelper.ShowTips("未设置字段!"); return; } if (this.m_DefaultSymbol == null) { MessageHelper.ShowTips("未设置缺省符号!"); return; } string text = this.cmbValueField1.Text.ToString().Trim(); IDisplayTable displayTable = (IDisplayTable)this.m_MatchLayer; IFeatureClass featureClass = (IFeatureClass)displayTable.DisplayTable; //BUG12551 郑英杰 2018-09-26 int i = featureClass.Fields.FindFieldByAliasName(text); IField pField = featureClass.Fields.get_Field(i); ITable mTable = (ITable)featureClass; if (this.frmAddRendValue == null) { this.frmAddRendValue = new FormAddValue(); } this.frmAddRendValue.listValue.Items.Clear(); this.frmAddRendValue.mFieldName = text; this.frmAddRendValue.mTable = mTable; this.frmAddRendValue.mFeatrueLayer = this.m_MatchLayer; ICursor cursor = this.m_MatchLayer.Search(null, false) as ICursor; IDataStatistics dataStatistics = new DataStatisticsClass(); dataStatistics.Cursor = cursor; dataStatistics.Field = pField.Name; dataStatistics.SampleRate = -1; IEnumerator uniqueValues = dataStatistics.UniqueValues; uniqueValues.Reset(); while (uniqueValues.MoveNext()) { this.m_SymbolTable.NewRow(); try { string item = Convert.ToString(uniqueValues.Current); this.frmAddRendValue.listValue.Items.Add(item); } catch (Exception) { } } this.frmAddRendValue.SetValues(dataStatistics.UniqueValueCount); if (this.frmAddRendValue.ShowDialog() == DialogResult.OK) { string mFieldValue = this.frmAddRendValue.mFieldValue; DataRow[] array = this.m_SymbolTable.Select("F_Value = '" + mFieldValue + "'"); if (array.Length != 0) { MessageHelper.ShowTips("添加的属性值 " + mFieldValue + "已经存在"); return; } DataRow row = this.m_SymbolTable.NewRow(); ISymbol symbol = null; if (this.m_DefaultSymbol != null) { IClone clone = this.m_DefaultSymbol as IClone; symbol = (clone.Clone() as ISymbol); } Bitmap symbolBitMap = RenderUtil.GetSymbolBitMap(40, 20, this.m_SymbologyStyleClass, symbol); this.InitRowData(symbolBitMap, mFieldValue, mFieldValue, symbol, ref row); this.m_SymbolTable.Rows.Add(row); this.InvokeEditValueChanged(sender, e); } } } }