using DevExpress.XtraEditors; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geodatabase; using KGIS.Framework.Utils.Helper; using KGIS.Plugin.LayerProperty.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; namespace KGIS.Plugin.LayerProperty.View { public partial class FormLabelExpression : XtraForm { private ILabelEngineLayerProperties2 m_LabelEngineLayerProperties; private IAnnotationExpressionEngine m_ExpressionEngine; private bool m_IsExpressionSimple; private string labelExpression; private bool isSampleExpression; public string LabelExpression { get { return this.labelExpression; } set { this.labelExpression = value; this.memoSQLInfo.Text = value; } } public bool IsSampleExpression { get { return this.isSampleExpression; } set { this.isSampleExpression = value; } } public FormLabelExpression() { InitializeComponent(); } private void InitForm(IFields fields) { try { this.lstField.Items.Clear(); int fieldCount = fields.FieldCount; for (int i = 0; i < fieldCount; i++) { IField field = fields.get_Field(i); if (field.Type != esriFieldType.esriFieldTypeGeometry && field.Type != esriFieldType.esriFieldTypeBlob && field.Type != esriFieldType.esriFieldTypeRaster) { ItemInfo item = new ItemInfo(field, field.Name); this.lstField.Items.Add(item); } } this.lstField.SelectedItem = this.lstField.Items[0]; } catch (Exception ex) { //RdbUtil.AddException(ex); } } internal void InitForm(ILabelEngineLayerProperties2 labelEngineLayerProperties, IFields iFields) { this.m_LabelEngineLayerProperties = labelEngineLayerProperties; this.InitForm(iFields); if (labelEngineLayerProperties != null) { this.m_IsExpressionSimple = labelEngineLayerProperties.IsExpressionSimple; this.m_ExpressionEngine = labelEngineLayerProperties.ExpressionParser; if (labelEngineLayerProperties.IsExpressionSimple) { this.memoSQLInfo.Text = labelEngineLayerProperties.Expression; return; } if (this.m_ExpressionEngine is AnnotationJScriptEngineClass) { this.cmbParser.SelectedIndex = 0; } else { this.cmbParser.SelectedIndex = 1; } //this.chkAdvanced.Checked = true; } } private void lstField_DoubleClick(object sender, System.EventArgs e) { try { this.Cursor = Cursors.AppStarting; this.InsertString("[" + this.lstField.SelectedItem.ToString().Trim() + "]"); this.memoSQLInfo.SendMouseUp(this.memoSQLInfo.Location, MouseButtons.Left); this.Cursor = Cursors.Default; } catch (Exception ex) { //RdbUtil.AddException(ex); this.Cursor = Cursors.Default; } } private void InsertString(string mStr) { int selectionStart = this.memoSQLInfo.SelectionStart; string text = this.memoSQLInfo.Text; if (selectionStart == text.Length) { this.memoSQLInfo.Text = this.memoSQLInfo.Text.Insert(selectionStart, mStr).Trim(); this.memoSQLInfo.SelectionStart = selectionStart + mStr.Length; return; } Regex regex = new Regex("\\[\\s*\\]"); MatchCollection matchCollection = regex.Matches(text); for (int i = 0; i < matchCollection.Count; i++) { Match match = matchCollection[i]; if (selectionStart > match.Index && selectionStart <= match.Index + match.Length) { this.memoSQLInfo.Text = this.memoSQLInfo.Text.Insert(match.Index + match.Length, mStr).Trim(); this.memoSQLInfo.SelectionStart = match.Index + match.Length + mStr.Length; return; } } regex = new Regex("\\[\\w*\\W*\\]"); matchCollection = regex.Matches(text); for (int j = 0; j < matchCollection.Count; j++) { Match match2 = matchCollection[j]; if (selectionStart > match2.Index && selectionStart <= match2.Index + match2.Length) { this.memoSQLInfo.Text = this.memoSQLInfo.Text.Insert(match2.Index + match2.Length, mStr).Trim(); this.memoSQLInfo.SelectionStart = match2.Index + match2.Length + mStr.Length; return; } } Match match3 = regex.Match(text, selectionStart); if (!match3.Success) { this.memoSQLInfo.Text = this.memoSQLInfo.Text.Insert(selectionStart, mStr).Trim(); this.memoSQLInfo.SelectionStart = selectionStart + mStr.Length; return; } match3 = regex.Match(text, selectionStart); int index = match3.Index; regex = new Regex("\\]"); match3 = regex.Match(text, selectionStart); if (match3.Index < index && match3.Index + 1 == index) { this.memoSQLInfo.Text = this.memoSQLInfo.Text.Insert(match3.Index + 1, mStr).Trim(); this.memoSQLInfo.SelectionStart = match3.Index + 1 + mStr.Length; return; } this.memoSQLInfo.Text = this.memoSQLInfo.Text.Insert(selectionStart, mStr).Trim(); this.memoSQLInfo.SelectionStart = selectionStart + mStr.Length; } private void AppendString(string mStr) { } private void InsertStringNospace(string mStr) { mStr = (mStr.Trim() ?? ""); int selectionStart = this.memoSQLInfo.SelectionStart; this.memoSQLInfo.Text = this.memoSQLInfo.Text.Insert(selectionStart, mStr).Trim(); if (mStr.EndsWith("() ")) { this.memoSQLInfo.SelectionStart = mStr.Length + selectionStart - 2; return; } this.memoSQLInfo.SelectionStart = mStr.Length + selectionStart; } private void btnAppend_Click(object sender, System.EventArgs e) { ItemInfo itemInfo = this.lstField.SelectedItem as ItemInfo; if (itemInfo != null) { if (this.memoSQLInfo.Text.Length > 0) { this.InsertString("& \" \" &[" + itemInfo.InnerValue.Name + "]"); return; } this.InsertString("[" + itemInfo.InnerValue.Name + "]"); } } private void btnOk_Click(object sender, System.EventArgs e) { if (this.LabelExpressionFinish()) { this.labelExpression = this.memoSQLInfo.Text; #region BUG12168 郑英杰 2018-09-06 //int num = 0; //Regex regex = new Regex("\\[\\s*\\]"); //MatchCollection matchCollection = regex.Matches(this.labelExpression); //num = matchCollection.Count + num; //regex = new Regex("\\[\\w*\\W*\\]"); //matchCollection = regex.Matches(this.labelExpression); //num = matchCollection.Count + num; //if (num > 1) //{ // this.isSampleExpression = false; //} //else //{ // this.isSampleExpression = true; //} #endregion this.m_LabelEngineLayerProperties.IsExpressionSimple = this.m_IsExpressionSimple; base.DialogResult = DialogResult.OK; base.Close(); } } private bool LabelExpressionFinish() { if (this.memoSQLInfo.Text.Length == 0) { MessageHelper.ShowTips("表达式不能为空!"); return false; } this.m_LabelEngineLayerProperties.ExpressionParser = this.m_ExpressionEngine; this.m_LabelEngineLayerProperties.Expression = this.memoSQLInfo.Text; this.m_LabelEngineLayerProperties.IsExpressionSimple = !this.chkAdvanced.Checked; return this.VerifyExpression(); } private void btnReset_Click(object sender, System.EventArgs e) { this.memoSQLInfo.Text = this.labelExpression; } private void btnVerify_Click(object sender, System.EventArgs e) { if (this.VerifyExpression()) { MessageHelper.ShowTips("表达式正确!"); } } private bool VerifyExpression() { try { IAnnotationExpressionParser annotationExpressionParser; if (this.chkAdvanced.Checked) { annotationExpressionParser = this.m_ExpressionEngine.SetCode(this.memoSQLInfo.Text, "FindLabel"); if (annotationExpressionParser.AttributeCount == 0) { MessageHelper.ShowTips("表达式错误,请检查!"); bool result = false; return result; } } else { annotationExpressionParser = this.m_ExpressionEngine.SetExpression("", this.memoSQLInfo.Text); } if (annotationExpressionParser.AttributeCount == 0) { MessageHelper.ShowTips("表达式错误,请检查!"); bool result = false; return result; } int num = 0; int num2 = 0; string text = ""; annotationExpressionParser.LastError(ref num, ref num2, ref text); if (text == null) { bool result = true; return result; } if (!(text.Contains("MSScript.ocx is either not installed or not registered") || text.Trim().Replace(" ", "").Contains("MSScript.ocx没有安装或没有注册") || text.Trim().Replace(" ", "").Contains("MSScript.ocx未安装或未注册"))) { MessageHelper.ShowTips(text); bool result = false; return result; } } catch (Exception ex) { //RdbUtil.AddException(ex); } return true; } private void memoSQLInfo_TextChanged(object sender, System.EventArgs e) { if (this.memoSQLInfo.Text.Length == 0) { this.btnVerify.Enabled = false; return; } this.btnVerify.Enabled = true; } private void chkAdvanced_CheckedChanged(object sender, System.EventArgs e) { if (this.chkAdvanced.Checked) { this.memoSQLInfo.Text = this.GetScriptSentence(this.m_ExpressionEngine, this.m_LabelEngineLayerProperties.Expression); #region BUG10428 2018-09-03 郑英杰 this.m_IsExpressionSimple = false; #endregion } else { this.memoSQLInfo.Text = this.GetExpressionByCode(this.m_ExpressionEngine, this.m_LabelEngineLayerProperties.Expression); #region BUG10428 2018-09-03 郑英杰 this.m_IsExpressionSimple = true; #endregion } #region BUG10428 2018-09-03 郑英杰 //this.m_IsExpressionSimple = !this.m_IsExpressionSimple; #endregion } private string GetScriptSentence(IAnnotationExpressionEngine pExpressionEngine, string m_LabelExpression) { if (this.m_IsExpressionSimple && this.m_LabelEngineLayerProperties.IsExpressionSimple) { IAnnotationExpressionParser annotationExpressionParser = pExpressionEngine.SetExpression("", m_LabelExpression); string text = ""; for (int i = 0; i < annotationExpressionParser.AttributeCount; i++) { if (i == 0) { text = "[" + annotationExpressionParser.get_Attribute(0) + "]"; } else { text = text + ", [" + annotationExpressionParser.get_Attribute(i) + "]"; } } return pExpressionEngine.CreateFunction("FindLabel", text, m_LabelExpression); } return this.m_LabelEngineLayerProperties.Expression; } private string GetExpressionByCode(IAnnotationExpressionEngine pExpressionEngine, string fullCode) { if (!this.m_IsExpressionSimple && !this.m_LabelEngineLayerProperties.IsExpressionSimple) { IAnnotationExpressionParser annotationExpressionParser = pExpressionEngine.SetCode(fullCode, "FindLabel"); string text = ""; for (int i = 0; i < annotationExpressionParser.AttributeCount; i++) { if (i == 0) { text = "[" + annotationExpressionParser.get_Attribute(0) + "]"; } else { text = text + "&\"\"& [" + annotationExpressionParser.get_Attribute(i) + "]"; } } return text; } return this.m_LabelEngineLayerProperties.Expression; } private void cmbParser_SelectedIndexChanged(object sender, System.EventArgs e) { if (this.cmbParser.SelectedIndex == 0) { this.m_ExpressionEngine = new AnnotationJScriptEngineClass(); } else { this.m_ExpressionEngine = new AnnotationVBScriptEngineClass(); } this.chkAdvanced.Checked = false; } } }