Newer
Older
GHFX_REFACTOR / FrmAddWellShp.cs
using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using GeoScene.Data;
using GeoScene.Engine;
using GeoScene.Globe;

namespace Cyberpipe
{
    public partial class FrmAddWellShp : Office2007Form
    {
        GSOGlobeControl globeControl1;
        GSODataSource ds;
        private Hashtable gj_cns = new Hashtable();
        private Hashtable gj_types = new Hashtable();
        public GSOLayer rukuLayer;
        private EnumValidateType enumValidateType;
        private bool isBSQ;

        public FrmAddWellShp(GSOGlobeControl _globeControl1, GSODataSource _ds, bool _isBSQ)
        {
            globeControl1 = _globeControl1;
            ds = _ds;
            isBSQ = _isBSQ;
            InitializeComponent();
        }

        private ArrayList files =new ArrayList ();
        private ArrayList modeltypes = new ArrayList(); //井的型号
        private ArrayList deeps = new ArrayList();

        private bool invalidate()
        {
            if (comboBoxLayer.SelectedItem.ToString().Trim() == "")
            {
                MessageBox.Show("请选择一个图层文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtLayerName.Text.Trim() == "")
            {
                MessageBox.Show("图层名称为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            Regex reg = new Regex("^[0-9]");
            if (reg.Match(txtLayerName.Text.Trim()).Success)
            {
                MessageBox.Show("图层名称不能以数字开头!", "警告");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 确定按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (invalidate() == false)
                return;
            try
            {
                GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(
                    Path.GetFileNameWithoutExtension(comboBoxLayer.Text));
                
                FieldValidate fieldValidate = FieldValidateFactory.Create(ValidateConfig.LoadConfig(),
                    enumValidateType);

                if (!fieldValidate.Validate(layer))
                {
                    DateTime dt = DateTime.Now;
                    string logFilePath = Application.StartupPath + "\\log\\" + dt.Year + "\\" + dt.Year + dt.Month + "\\" + dt.Year +
                                 dt.Month + dt.Day + ".txt";
                    MessageBox.Show("选择的数据不符合数据规范,详情见日志" + logFilePath, "提示");
                    return;
                }

                ModelBuilder pointBuilder = new PointBuilder(ds);
                PointParam pointParam = new PointParam();
                pointParam.layerName = txtLayerName.Text;
                pointParam.uniqueIdFieldName = comboBoxCode.Text;
                pointParam.upGround = Convert.ToDouble(txtUpGround.Text);
                pointParam.attachNameFieldName = combModelName.Text;
                pointParam.pointEncodingFieldName = combCode.Text;
                pointParam.wellDepthFiledName = combDeep.Text;
                pointParam.altitudeFieldName = comboBoxGC.Text;
                pointParam.z = comboBoxZ.Text;

                pointBuilder.build(layer, checkBoxAdd.Checked ? EnumUpdateMode.Append : EnumUpdateMode.Update,
                    checkBoxDeep.Checked ? EnumBuildMode.Depth : EnumBuildMode.Alititude, pointParam);

                addLayerToGlobeControl(pointParam.layerName);
                DialogResult = DialogResult.OK;

                MessageBox.Show("入库完成!", "提示");
            }
            catch (Exception ex)
            {
                LogError.PublishError(ex);
//                LogHelper.WriteLog(typeof(FrmAddWellShp), ex.Message);
                LogHelper.Error(ex.Message);
            }
        }

        private void addLayerToGlobeControl(string layerName)
        {
            ds = globeControl1.Globe.DataManager.OpenOracleDataSource(Utility.DBServer.Trim() + "/" + Utility.dbdatabase.Trim(), "",
                                   "", Utility.userID, Utility.DBPassword);

            GSOLayer layer = globeControl1.Globe.Layers.Add(ds.GetDatasetByName(layerName));
            rukuLayer = layer;
        }

        /// <summary>
        /// 窗体初始化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAddWellShp_Load(object sender, EventArgs e)
        {
            buttonOpen.Visible = isBSQ;
            //填充SHP数据列表
            comboBoxLayer.Items.Clear();

            if (globeControl1 != null)
            {
                for (int i = 0; i < globeControl1.Globe.Layers.Count; i++)
                {
                    GSOLayer layer = globeControl1.Globe.Layers[i];
                    if (layer != null && layer.Name.ToLower().EndsWith(".shp"))
                    {
                        comboBoxLayer.Items.Add(layer.Name.Trim());
                    }
                }
            }
        }

        private void FrmAddWellShp_FormClosing(object sender, FormClosingEventArgs e)
        {
            globeControl1.Globe.Refresh();
        }

        private void clearComboboxParam()
        {            
            comboBoxCode.Enabled = true;
            combModelName.Enabled = true;
            combCode.Enabled = true;
            combDeep.Enabled = true;
            comboBoxGC.Enabled = true;
            comboBoxZ.Enabled = true;

            txtLayerName.Text = "";
            comboBoxCode.Items.Clear();
            combModelName.Items.Clear();
            combCode.Items.Clear();
            combDeep.Items.Clear();
            comboBoxGC.Items.Clear();
            comboBoxZ.Items.Clear();

        }

        private void addComboboxParam(string strParam)
        {
            comboBoxCode.Items.Add(strParam);
            combModelName.Items.Add(strParam);
            combCode.Items.Add(strParam);
            combDeep.Items.Add(strParam);
            comboBoxGC.Items.Add(strParam);
            comboBoxZ.Items.Add(strParam);
        }

        private void initComboboxPraram(GSOFeature feature)
        {
            if (ExitFiled(feature, "管点编码")) //特征管点
            {
                comboBoxCode.SelectedItem = "编号";
                combModelName.SelectedItem = "管点特征";
                combCode.SelectedItem = "管点编码";
                comboBoxGC.SelectedItem = "地面高程";
                comboBoxZ.SelectedItem = "Z坐标";
                combDeep.Enabled = false;
                enumValidateType = EnumValidateType.Charac;
            }
            else if (ExitFiled(feature, "附属物编码")) //附属物
            {
                comboBoxCode.SelectedItem = "编号";
                combModelName.SelectedItem = "附属物名称";
                combCode.SelectedItem = "附属物编码";
                combDeep.SelectedItem = "井深";
                comboBoxGC.SelectedItem = "地面高程";
                comboBoxZ.Enabled = false;
                enumValidateType = EnumValidateType.Attach;
            }
            else
            {
                comboBoxCode.SelectedItem = "编号";
                combModelName.Enabled = false;
                combCode.SelectedItem = "图层编号";
                combDeep.SelectedItem = "标识器埋深";
                comboBoxGC.SelectedItem = "地面高程";
                comboBoxZ.Enabled = false;
                enumValidateType = EnumValidateType.Marker;
            }
        }

        private bool ExitFiled(GSOFeature feature, string filedName)
        {
            GSOFeatureDataset featDataSet = feature.Dataset as GSOFeatureDataset;
            for (int i = 0; i < featDataSet.FieldCount; i++)
            {
                if (featDataSet.GetField(i).Name == filedName)
                    return true;
            }
            return false;
        }
        private void comboBoxLayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxLayer.SelectedIndex == -1)
                return;
            string layerName=Path.GetFileNameWithoutExtension(comboBoxLayer.Text);

            GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(layerName);

            GSOFeatures features = layer.GetAllFeatures();
            if (features.Length == 0)
                return;

            clearComboboxParam();
            txtLayerName.Text = layerName;

            for (int i = 0; i < features[0].GetFieldCount(); i++)
            {
                GSOFieldDefn fielddef = features[0].GetFieldDefn(i);
                addComboboxParam(fielddef.Name);
            }

            initComboboxPraram(features[0]);

        }

        private void checkBoxAdd_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxReset.Checked = !checkBoxAdd.Checked;
        }

        private void checkBoxReset_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxAdd.Checked = !checkBoxReset.Checked;
        }

        private void checkBoxDeep_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxH.Checked = !checkBoxDeep.Checked;
        }

        private void checkBoxH_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxDeep.Checked = !checkBoxH.Checked;
        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "*.shp|*.shp";
            dlg.Multiselect = false;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                GSOLayer layer = globeControl1.Globe.Layers.Add(dlg.FileName);

                comboBoxLayer.Items.Add(Path.GetFileName(dlg.FileName));
                comboBoxLayer.SelectedItem = Path.GetFileName(dlg.FileName);

                GSOFeatures features = layer.GetAllFeatures();
                if (features.Length == 0)
                    return;

                clearComboboxParam();
                for (int i = 0; i < features[0].GetFieldCount(); i++)
                {
                    GSOFieldDefn fielddef = features[0].GetFieldDefn(i);
                    addComboboxParam(fielddef.Name);
                }
                initComboboxPraram(features[0]);
            }

            txtLayerName.Text = Path.GetFileNameWithoutExtension(comboBoxLayer.Text);
        }

    }
}