private void BtnUpdate_Click(object sender, EventArgs e)
        {
            frmProgressBar2 pb2 = null;

            try
            {
                this.BtnUpdate.Enabled = false;
                this.Cursor            = Cursors.WaitCursor;
                //
                string txtPath = this.textBox1.Text.Trim();
                if (txtPath == "" || System.IO.File.Exists(txtPath) == false)
                {
                    MessageBox.Show("请先选择YSDM对照txt文件", "提示");
                    this.BtnUpdate.Enabled = true;
                    this.Cursor            = Cursors.Default;
                    return;
                }
                //
                if (MessageBox.Show("请确认是否要对选中项进行批量更新YSDM字段的值操作?", "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
                {
                    this.BtnUpdate.Enabled = true;
                    this.Cursor            = Cursors.Default;
                    return;
                }
                //--------------------------------------
                List <ItemClass> ItemList = new List <ItemClass>();
                StreamReader     sr       = new StreamReader(txtPath, Encoding.Default);
                while (sr.EndOfStream == false)
                {
                    string   line      = sr.ReadLine();
                    string[] lineArray = line.Split(new char[] { ',' });
                    if (lineArray != null && lineArray.Length >= 3)
                    {
                        ItemClass item = new ItemClass();
                        item.Value   = lineArray[0].Trim();   //YSDM
                        item.Caption = lineArray[1].Trim();   //YSDMName
                        item.Tag     = lineArray[2].Trim();   //LayerName
                        //
                        ItemList.Add(item);
                    }
                }
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                    sr = null;
                }
                //--------------------------------------
                //获取选中的要素类
                List <IFeatureClass> fcList = new List <IFeatureClass>();
                for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
                {
                    if (this.checkedListBox1.GetItemChecked(i) == true)
                    {   //选中要素类
                        CommonComboBoxItem cbitem = this.checkedListBox1.Items[i] as CommonComboBoxItem;
                        fcList.Add(cbitem.Tag as IFeatureClass);
                    }
                }
                if (fcList != null && fcList.Count > 0)
                {
                    pb2      = new frmProgressBar2();
                    pb2.Text = "批量更新YSDM字段进度...";
                    pb2.progressBar1.Maximum = fcList.Count + 1;
                    pb2.progressBar1.Value   = 0;
                    pb2.Caption1.Text        = "正在更新...";
                    pb2.Show(this);
                    Application.DoEvents();
                    //
                    ZHFeaturePoint zhfeat    = null;
                    int            featIndex = 0;
                    int            featCount = 0;
                    //
                    string YSDM = "";
                    for (int i = 0; i < fcList.Count; i++)
                    {
                        featIndex = 0;
                        IFeatureClass fc = fcList[i];
                        pb2.Caption1.Text      = "正在更新图层:" + fc.AliasName + "...";
                        pb2.progressBar1.Value = i;
                        Application.DoEvents();
                        //
                        featCount = fc.FeatureCount(null);
                        pb2.progressBar2.Value   = 0;
                        pb2.progressBar2.Maximum = featCount + 1;
                        pb2.Caption2.Text        = "正在更新要素...";
                        Application.DoEvents();
                        //获取图层对应的要素代码值
                        string fcName = fc.AliasName;
                        if (fc is IDataset)
                        {
                            fcName = (fc as IDataset).Name;
                        }
                        foreach (ItemClass item in ItemList)
                        {
                            string txtLayerName = item.Tag.ToString();
                            //-------------------------------
                            if (cb_IsLayerLastPreStr.Checked == true)
                            {   //是后缀字符串
                                txtLayerName = txtLayerName + this.txt_LayerPreStr.Text.Trim();
                            }
                            else
                            {   //是前缀字符串
                                txtLayerName = this.txt_LayerPreStr.Text.Trim() + txtLayerName;
                            }
                            //-------------------------------
                            if (fcName.ToUpper() == txtLayerName.ToUpper())
                            {
                                YSDM = item.Value.ToString();
                                break;
                            }
                        }
                        //
                        IFeatureCursor fcur = fc.Update(null, false);
                        IFeature       feat = fcur.NextFeature();
                        while (feat != null)
                        {
                            featIndex += 1;
                            if (featIndex % 200 == 0)
                            {
                                pb2.Caption2.Text      = "正在更新图层:" + fc.AliasName + "(当前:" + featIndex.ToString() + "/" + featCount.ToString() + ")";
                                pb2.progressBar2.Value = featIndex;
                                Application.DoEvents();
                                fcur.Flush();
                            }
                            zhfeat = new ZHFeaturePoint(feat);
                            zhfeat.setFieldValue("YSDM", YSDM);
                            fcur.UpdateFeature(feat);
                            //
                            feat = fcur.NextFeature();
                        }
                        fcur.Flush();
                        if (fcur != null)
                        {
                            TokayWorkspace.ComRelease(fcur);
                            fcur = null;
                        }
                    }
                    if (pb2 != null)
                    {
                        pb2.Close();
                        pb2.Dispose();
                        pb2 = null;
                    }
                    MessageBox.Show("更新YSDM完毕!", "提示");
                }
            }
            catch (Exception ee)
            {
                Log.WriteLine(ee);
                MessageBox.Show(ee.Message, "提示");
            }
            finally
            {
                this.BtnUpdate.Enabled = true;
                this.Cursor            = Cursors.Default;
                if (pb2 != null)
                {
                    pb2.Close();
                    pb2.Dispose();
                    pb2 = null;
                }
            }
        }
Esempio n. 2
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ProgressDialog pd = null;

            try
            {
                ItemClass item = this.comboBox1.SelectedItem as ItemClass;
                if (item == null)
                {
                    MessageBox.Show("请先选择要更新的图层", "提示");
                    return;
                }
                ItemClass fd_item = this.comboBox2.SelectedItem as ItemClass;
                if (fd_item == null)
                {
                    MessageBox.Show("请先选择要更新的字段", "提示");
                    return;
                }

                ItemClass fd_from = this.comboBox3.SelectedItem as ItemClass;
                if (fd_from == null)
                {
                    MessageBox.Show("请先选择值来源的字段", "提示");
                    return;
                }
                int index_from = CommonClass.TInt(this.fd_indexFrom.Value);
                int index_to   = CommonClass.TInt(this.fd_indexTo.Value);
                if (index_from < 0)
                {
                    MessageBox.Show("取子字符串只能从大于等于0开始!", "提示");
                    return;
                }
                //
                ILayer        layer     = item.Value as ILayer;
                IFeatureLayer featLayer = layer as IFeatureLayer;
                IFeatureClass fc        = featLayer.FeatureClass;
                //
                string fdName     = fd_item.Caption;
                string fdfromName = fd_from.Caption;
                //
                int fCount = fc.FeatureCount(null);
                if (fCount > 0)
                {
                    pd         = new ProgressDialog();
                    pd.Text    = "进度";
                    pd.Message = "计算扣除地类系数中......";
                    pd.Minimum = 0;
                    pd.Maximum = fCount;
                    pd.Show(this);
                    //
                    Application.DoEvents();
                    //
                    IFeatureCursor pcursor     = fc.Update(null, false);
                    IFeature       pf          = pcursor.NextFeature();
                    int            index       = fc.FindField(fdfromName);
                    int            indexUpdate = fc.FindField(fdName);
                    //
                    int n = 0;
                    while (pf != null)
                    {
                        n = n + 1;
                        if (n % 200 == 0)
                        {
                            pd.Value   = n;
                            pd.Message = "取子字符串中......" + pd.Value.ToString() + "/" + pd.Maximum.ToString();
                            Application.DoEvents();
                            pcursor.Flush();
                        }
                        object v = pf.get_Value(index);
                        if (v != null)
                        {
                            string value_from = v.ToString();
                            string result_v   = value_from;
                            if (index_to < value_from.Length)
                            {
                                result_v = value_from.Substring(index_from, index_to);
                            }
                            else
                            {
                                result_v = value_from.Substring(index_from);
                            }
                            //
                            pf.set_Value(indexUpdate, result_v);
                            pcursor.UpdateFeature(pf);
                        }
                        pf = pcursor.NextFeature();
                    }
                    pcursor.Flush();
                    if (pcursor != null)
                    {
                        TokayWorkspace.ComRelease(pcursor);
                        pcursor = null;
                    }
                    if (pd != null)
                    {
                        pd.Dispose();
                        pd = null;
                    }
                    MessageBox.Show("更新完毕!", "提示");
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.StackTrace, "提示");
            }
            finally
            {
                if (pd != null)
                {
                    pd.Dispose();
                    pd = null;
                }
            }
        }