Beispiel #1
0
 private void btnImport_Click(object sender, EventArgs e)
 {
     SdeConnection conn = new SdeConnection();
     ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
     TranferDatabase.ImportFromXml(sdeConn.Workspace, txtFileName.Text, false);
     this.Close();
 }
Beispiel #2
0
        private void showMap()
        {
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            string thuaLayer = string.Format("{0}_{1}", DataNameTemplate.Thua, _curConfig.NamApDung);
            string duongLayer = string.Format("{0}", DataNameTemplate.Duong);
            IFeatureClass fcThua=null;
            try
            {
                fcThua = fw.OpenFeatureClass(thuaLayer);
            }
            catch (Exception ex) { }
            IFeatureLayer flThua = new FeatureLayerClass();
            flThua.FeatureClass = fcThua;
            ILayer layerThua = (ILayer)flThua;
            IFeatureClass fcDuong=null;
            try
            {
                fcDuong = fw.OpenFeatureClass(duongLayer);
            }
            catch (Exception ex) { }
            IFeatureLayer flDuong = new FeatureLayerClass();
            flDuong.FeatureClass = fcDuong;
            ILayer layerDuong = (ILayer)flDuong;
            layerDuong.Name = fcDuong.AliasName;
            layerThua.Name = fcThua.AliasName;

            _mapController.AddLayer(flDuong);
        }
Beispiel #3
0
 public Evaluation(string expr)
 {
     this._expr = expr;
     _conn = new SdeConnection();
     _sdeConn = (ISdeConnectionInfo)_conn;
     _conf = CurrentConfig.CallMe();
     _fcName = new TnFeatureClassName(_sdeConn.Workspace);
     _tblName = new TnTableName(_sdeConn.Workspace);
     _dicParams = new Dictionary<string, object>();
 }
Beispiel #4
0
        void Frm_Load(object sender, EventArgs e)
        {
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            this._tblName = new TnTableName(sdeConn.Workspace);
            string giadatNnName = string.Format("{0}_{1}", DataNameTemplate.Gia_Dat_Pnn_Nongthon, _curConfig.NamApDung);
            this._tblName.GIA_DAT_O_NONGTHON.NAME = giadatNnName;
            this._tblName.GIA_DAT_O_NONGTHON.InitIndex();
            _edit.ExpectedTableName = giadatNnName;

            _edit.DbConnectOccur();
            _edit.AliasFieldName = _tblName.GIA_DAT_O_NONGTHON.ALIAS_FIELD_LIST;
            //setAliasFieldName();
        }
Beispiel #5
0
        void Frm_Load(object sender, EventArgs e)
        {
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            this._tblName = new TnTableName(sdeConn.Workspace);

            string giadatNnName = string.Format("{0}_{1}", DataNameTemplate.Gia_Dat_Nongnghiep, _curConfig.NamApDung);
            _tblName.GIA_DAT_NONGNGHIEP.NAME = giadatNnName;
            _tblName.GIA_DAT_NONGNGHIEP.InitIndex();
            _edit.ExpectedTableName = giadatNnName;

            _edit.DbConnectOccur();
            //MessageBox.Show(_tblName.GIA_DAT_NONGNGHIEP.ALIAS_FIELD_LIST[0][0,0]);
            _edit.AliasFieldName = _tblName.GIA_DAT_NONGNGHIEP.ALIAS_FIELD_LIST;
            //setAliasFieldName();
        }
Beispiel #6
0
        void IGMap.AddLayer(params string[] layer)
        {
            IFeatureClass fc = null;

            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            for (int i = 0; i < layer.Length; i++)
            {
                fc = fw.OpenFeatureClass(layer[i]);
                IFeatureLayer fl=new FeatureLayerClass();
                fl.FeatureClass = fc;
                fl.Name = fc.AliasName;
                ILayer l = (ILayer)fl;
                _mapHook.AddLayer(l, 0);
            }
        }
Beispiel #7
0
        private void showMap()
        {
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            IFeatureClass fcHem = fw.OpenFeatureClass("sde.thixa_hem");
            IFeatureLayer flHem = new FeatureLayerClass();
            flHem.FeatureClass = fcHem;
            ILayer layerThua = (ILayer)flHem;
            IFeatureClass fcDuong = fw.OpenFeatureClass("sde.thixa_duong");
            IFeatureLayer flDuong = new FeatureLayerClass();
            flDuong.FeatureClass = fcDuong;
            ILayer layerDuong = (ILayer)flDuong;
            layerDuong.Name = fcDuong.AliasName;
            layerThua.Name = fcHem.AliasName;

            _mapController.AddLayer(flHem);
            _mapController.AddLayer(flDuong);
        }
Beispiel #8
0
        void ThematicView2_Load(object sender, EventArgs e)
        {
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            IFeatureClass fcThua = fw.OpenFeatureClass("sde.thixa_thua");
            IFeatureLayer flThua = new FeatureLayerClass();
            flThua.FeatureClass = fcThua;
            ILayer layerThua = (ILayer)flThua;
            IFeatureClass fcDuong = fw.OpenFeatureClass("sde.thixa_duong");
            IFeatureLayer flDuong = new FeatureLayerClass();
            flDuong.FeatureClass = fcDuong;
            ILayer layerDuong = (ILayer)flDuong;
            layerDuong.Name = fcDuong.AliasName;
            layerThua.Name = fcThua.AliasName;

            _mapController.AddLayer(layerDuong);
            _mapController.AddLayer(layerThua);
        }
Beispiel #9
0
        public TnThuaInfo(string nam, string chusohuu, string diachi, string loaidat, string giadat,
            string soto, string sothua, string somattien,string somathem, string socachtinh)
        {
            //*************
            //Lay connection info hien tai
            //SdeUserInfo va SqlUserInfo la static
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            _tableName = new TnTableName(sdeConn.Workspace);
            _fcName = new TnFeatureClassName(sdeConn.Workspace);

            _nam = nam;
            _chuSoHuu = chusohuu;
            _diaChi = diachi;
            _loaiDat = loaidat;
            _giaDat = giadat;
            _soTo = soto;
            _soThua = sothua;
            _soMatTien = somattien;
            _soMatHem = somathem;
            _soCachTinh = socachtinh;
        }
Beispiel #10
0
        private void btnThem_Click(object sender, EventArgs e)
        {
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;

            int len=chkbLayers.CheckedItems.Count;
            //string[] layers =new string[len];
            string layer = "";
            for(int i=0;i<len;i++)
            {
                layer = chkbLayers.CheckedItems[i].ToString();
                try
                {
                    ITable table = ((IFeatureWorkspace)sdeConn.Workspace).OpenTable(layer);
                    FeatureClassManagement.DeleteFeatureClass((IDataset)table);
                    //chkbLayers.Items.Remove(chkbLayers.CheckedItems[i]);
                    //chkbLayers.Items.RemoveAt(i);
                }
                catch (Exception ex) {MessageBox.Show(String.Format("line 60 FrmDeleteLayers, \nexp={0}",ex)); continue; }
            }
            loadData();
            //_mapView.AddLayer(layers);
            //this.Close();
        }
Beispiel #11
0
        void _bwk_DoWork(object sender, DoWorkEventArgs e)
        {
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            //ISqlConnectionInfo sqlConn = (ISqlConnectionInfo)sdeConn;
            ISdeUserInfo sdeUser = sdeConn.GetSdeUserInfo();
            _fcName = new TnFeatureClassName(sdeConn.Workspace);
            _tblName = new TnTableName(sdeConn.Workspace);
            _sqlUser = sdeUser.GetSqlUserInfo();

            SqlDataReader reader = null;
            SqlDataAdapter adapter = null;
            DataSet dataSet = new DataSet();
            QueryEventArg qrEvt = new QueryEventArg();

            _nameOfThua = String.Format("{0}_{1}", DataNameTemplate.Thua, _config.NamApDung);
            if (_sqlTable.IsClosed())
            {
                _sqlTable.SetUserInfo(_sqlUser.GetConnectionStringAsArray());
                _sqlTable.OpenConnection();
            }
            int t = 0;
            bool rs = int.TryParse(_type.ToString(), out t);
            if (!rs)
            {
                t = 0;
            }
            IInfoForQuerySql infoSql = (IInfoForQuerySql)_info;
            string selectSql = string.Format("select * from {0} where ",_nameOfThua);
            string whereClause = "";
            List<string> lstSql = new List<string>();
            string maxa = "0";
            int c = 0;
            switch (t)
            {
                //truy van uniq, so to,so thua,dia chi,ten chu
                case 0:
                    #region Tim uniq
                    //kiem tra 1 trong cac gia tri sau
                    //xa,chusohuu,dia chi,soto,sothua
                    //neu ="" thi bo qua, neu ko thi them vao sql and
                    qrEvt.Data = "Đang tìm ...";
                    onQuerying(qrEvt);
                    if (_info.Xa != "")
                    {
                        //tim trong bang xa, lay ma xa theo ten xa

                        #region tim ma xa
                        string sqlxa = "";
                        sqlxa = string.Format("select maxa from {0} where {1}",DataNameTemplate.Ranh_Xa_Poly, infoSql.XaSql);

                        //adapter = new SqlDataAdapter(_sqlTable.GetSqlCommand(sqlxa));
                        //adapter.Fill(dataSet);
                        reader = _sqlTable.GetSqlCommand(sqlxa).ExecuteReader();
                        if (reader != null)
                        {
                            if (reader.HasRows)
                            {
                                //lay 1 ma xa
                                if (reader.Read())
                                {

                                    maxa = reader.GetValue(0).ToString();
                                    //MessageBox.Show(String.Format("line 119 QueryThua {0}", maxa));
                                }
                            }
                        }
                        else
                        {
                            maxa = "0";
                        }
                        reader.Close();
                        #endregion

                        //tim cac thua

                        lstSql.Add(string.Format("{0}='{1}'", _fcName.FC_THUA.MA_XA, maxa));
                    }
                    if (_info.ChuSoHuu != "")
                    {
                        lstSql.Add(string.Format(infoSql.ChuSoHuuSql));
                    }
                    if (_info.DiaChi != "")
                    {
                        lstSql.Add(string.Format(infoSql.DiaChiSql));
                    }
                    if (_info.SoTo != "")
                    {
                        lstSql.Add(string.Format(infoSql.SoToSql));
                    }
                    if (_info.SoThua != "")
                    {
                        lstSql.Add(string.Format(infoSql.SoThuaSql));
                    }
                    if (_info.MaThua != "")
                    {
                        lstSql.Add(string.Format(infoSql.MaThuaSql));
                    }
                    //ghep cac sql
                    //c = lstSql.Count;
                    //if (c > 0)
                    //{
                    //    whereClause += string.Format("{0}", lstSql[0]);
                    //    for (int i = 1; i < c; i++)
                    //    {
                    //        whereClause += string.Format(" and {0}", lstSql[i]);
                    //    }
                    //    selectSql = selectSql + whereClause;
                    //    adapter = new SqlDataAdapter(_sqlTable.GetSqlCommand(selectSql));
                    //    adapter.Fill(dataSet);
                    //    //MessageBox.Show(selectSql);
                    //    //reader = _sqlTable.GetSqlCommand(whereClause).ExecuteReader();
                    //    //if (reader.HasRows)
                    //    //{
                    //    foreach (IQueryThuaView v in _views)
                    //    {
                    //        v.GetResult(dataSet);
                    //    }
                    //    //}
                    //    //reader.Close();
                    //}
                    //qrEvt.Data = "Đã tìm xong";
                    //onFinished(qrEvt);
                    break;
                    #endregion

                //tim theo loai dat,so mat tien,..
                case 1:
                    #region tim theo loai dat,...
                    //xet loai dat de biet xet trong bang gia dat nao
                    if (_info.LoaiDat != "")
                    {
                        lstSql.Add(string.Format(infoSql.LoaiDatSql));
                    }
                    if (_info.SoMatTien != "")
                    {
                        lstSql.Add(string.Format(infoSql.SoMatTienSql));
                    }
                    if (_info.SoTo != "")
                    {
                        lstSql.Add(string.Format(infoSql.SoToSql));
                    }
                    if (_info.SoThua != "")
                    {
                        lstSql.Add(string.Format(infoSql.SoThuaSql));
                    }
                    #endregion
                    break;
                case 2:
                    //tim theo vi tri
                    #region tim theo vi tri

                    #endregion
                    break;
                case 3:
                    //tim theo dien tich
                    #region tim theo dien tich
                    if (_info.DienTich != "")
                    {
                        lstSql.Add(string.Format(infoSql.DienTichSql));
                    }
                    #endregion
                    break;
                default:
                    break;
            }
            c = lstSql.Count;
            if (c > 0)
            {
                whereClause += string.Format("{0}", lstSql[0]);
                for (int i = 1; i < c; i++)
                {
                    whereClause += string.Format(" and {0}", lstSql[i]);
                }
                selectSql = selectSql + whereClause;
                adapter = new SqlDataAdapter(_sqlTable.GetSqlCommand(selectSql));
                adapter.Fill(dataSet);
                //MessageBox.Show(selectSql);
                //reader = _sqlTable.GetSqlCommand(whereClause).ExecuteReader();
                //if (reader.HasRows)
                //{
                foreach (IQueryThuaView v in _views)
                {
                    v.GetResult(dataSet);
                }
                //}
                //reader.Close();
            }
            qrEvt.Data = "Đã tìm xong";
            onFinished(qrEvt);

            _sqlTable.CloseConnection();
        }
Beispiel #12
0
        void ILoader.Load(EnumG1ArcGisTnRecType type)
        {
            //*************
            //Lay connection info hien tai
            //SdeUserInfo va SqlUserInfo la static
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            ISdeUserInfo sdeUser = sdeConn.GetSdeUserInfo();
            //ISqlConnectionInfo sqlConn = (ISqlConnectionInfo)sdeConn;

            //MessageBox.Show(sqlConn.GetSqlUserInfo().UserName);
            _sqlUser = sdeUser.GetSqlUserInfo();

            SqlDataReader reader = null;
            //_pair = new XPair();
            LoaderEventArg e=new LoaderEventArg(type);
            //MessageBox.Show(string.Format("line 39 Loader:--{0}, so luong view:{1}", _sql, _lstView.Count));
            try
            {
                if (_sqlTable.IsClosed())
                {
                    _sqlTable.SetUserInfo(_sqlUser.GetConnectionStringAsArray());
                    //MessageBox.Show(String.Format("line 59 Loader, ConnectionString={0}", _sqlUser.GetConnectionString()));

                    _sqlTable.OpenConnection();
                }

                reader = _sqlTable.GetSqlCommand(_sql).ExecuteReader();
                //MessageBox.Show("line 49 Loader: --" + reader.HasRows.ToString() + "--" + _sql);
                //_pair.ClearPair();
                foreach (IReceiverView r in _lstView)
                {
                    r.ReceiveData(reader,type);
                }
                reader.Close();
                _sqlTable.CloseConnection();
                onFinished(e);

            }
            catch (Exception e1)
            {
                if (reader != null)
                {
                    reader.Close();
                }
                _sqlTable.CloseConnection();
                //MessageBox.Show("line 67 - Loader \n" + e1.ToString());
            }
        }
Beispiel #13
0
 void IReceiverView.Load(EnumG1ArcGisTnRecType type)
 {
     SdeConnection conn = new SdeConnection();
     ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
     _fcName = new TnFeatureClassName(sdeConn.Workspace);
     _tblName = new TnTableName(sdeConn.Workspace);
     //if (type == EnumG1ArcGisTnRecType.Duong)
     //{
     //    this._dicSql[type]=string.Format("select distinct {0} from {1}", this._dicColsName[type], this._dicTableName[type]);
     //}
     //else if (type == EnumG1ArcGisTnRecType.Doanduong)
     //{
     //    string.Format("select {0},{1},{2} from {3} where {4}=N'{5}'", _fcName.FC_DUONG.MA_DUONG, _fcName.FC_DUONG.BAT_DAU, _fcName.FC_DUONG.KET_THUC, "sde.thixa_duong", _dicColsNameInWhereClause[type], cbxDuong.Text);
     //}
     _dicSql[EnumG1ArcGisTnRecType.Doanduong] = string.Format("select {0},{1},{2} from {3} where {4}=N'{5}'", _tblName.TEN_DUONG.MA_DUONG, _tblName.TEN_DUONG.BAT_DAU, _tblName.TEN_DUONG.KET_THUC, DataNameTemplate.Ten_Duong, _tblName.TEN_DUONG.TEN_DUONG, cbxDuong.Text);
     //_dicSql.Add(EnumG1ArcGisTnRecType.Doanduong, );
     //_sql = string.Format("select {0},{1},{2} from {3} where {4}='a'", _fcName.FC_DUONG.MA_DUONG, _fcName.FC_DUONG.BAT_DAU, _fcName.FC_DUONG.KET_THUC, "do.thixa_duong", _fcName.FC_DUONG.TEN_DUONG);
     //_dicSql.Add(EnumG1ArcGisTnRecType.Doanduong, "");
     _dicSql[EnumG1ArcGisTnRecType.Duong] = string.Format("select distinct {0} from {1}", _tblName.TEN_DUONG.TEN_DUONG, DataNameTemplate.Ten_Duong);
     //_dicSql.Add(EnumG1ArcGisTnRecType.Huyen, "");
     _dicSql[EnumG1ArcGisTnRecType.Xa] = string.Format("select {0},{1} from {2}", _fcName.FC_RANH_XA_POLY.MA_XA, _fcName.FC_RANH_XA_POLY.TEN_XA, DataNameTemplate.Ranh_Xa_Poly);
     //MessageBox.Show("line 126 CalculationView --" + type.ToString() + "\n " + _dicSql[EnumG1ArcGisTnRecType.Xa]);
     _loaderController.ReqLoad(type);
 }
Beispiel #14
0
        ISelectionSet ISpatialAlgrorithm.Execute()
        {
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ITnFeatureClassName fcName = new TnFeatureClassName(sdeConn.Workspace);
            ITnTableName tblName = new TnTableName(sdeConn.Workspace);
            IFeatureClass xaFeatureClass = fw.OpenFeatureClass(fcName.FC_RANH_XA_POLY.NAME);
            IFeatureLayer xaFeatureLayer = new FeatureLayerClass();
            xaFeatureLayer.FeatureClass = xaFeatureClass;
            IFeatureClass duongFeatureClass = fw.OpenFeatureClass(fcName.FC_DUONG.NAME);
            IFeatureLayer duongFeatureLayer = new FeatureLayerClass();
            duongFeatureLayer.FeatureClass = duongFeatureClass;
            //IQueryFilter qrf = new QueryFilterClass();
            //qrf.WhereClause = string.Format("{0}='{1}'", fcName.FC_RANH_XA_POLY.MA_XA, 5);
            //ISelectionSet xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
            //IFeatureSelection xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
            //xaFeatureSelection.SelectionSet = xaSelectionSet;
            //if (this._fromLayer == null || this._byLayer == null)
            //{
            //    return null;
            //}
            //this._resultSelectionSet=this._queryByLayer.Select();
            //IFeatureLayer f=new FeatureLayerClass();
            //f.FeatureClass=this._frmFc;
            //IFeatureLayer b=new FeatureLayerClass();
            //b.FeatureClass = this._byFc;
            //IFeatureSelection fs = (IFeatureSelection)this._byLayer;
            //MessageBox.Show(fs.SelectionSet.Count.ToString());
            IQueryByLayer qrbl = new QueryByLayerClass();
            qrbl.FromLayer = duongFeatureLayer;
            qrbl.ByLayer = xaFeatureLayer;
            qrbl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
            qrbl.ResultType = esriSelectionResultEnum.esriSelectionResultNew;
            qrbl.UseSelectedFeatures = true;

            ISelectionSet sl = qrbl.Select();
            MessageBox.Show("Line 125 AlgTimDuong:"+sl.Count.ToString());
            //MessageBox.Show(xaFeatureLayer.FeatureClass.AliasName);
            try
            {
                //this._resultSelectionSet = this._queryByLayer.Select();
                //this._resultSelectionSet =qrbl.Select();
                //MessageBox.Show(string.Format("line 120 AlgrithmTimDuong, so luong duong:{0}", this._resultSelectionSet.Count));
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }

            //this._resultSelectionSet = sls;
            //Dang bi loi cho nay
            this._resultFeatureSelection = (IFeatureSelection)this._resultLayer;
            this._resultFeatureSelection.SelectionSet = this._resultSelectionSet;
            return this._resultSelectionSet;
        }
Beispiel #15
0
        void IGMap.ZoomToSelectMa(string layerName, string fieldName, params object[] key)
        {
            bool hasLayer = false;
            int c = this._mapHook.LayerCount;
            if (c == 0)
            {
                return;
            }
            IFeatureClass fc = null;
            IFeatureLayer fl = null;
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            string name = "";
            string queryString = QueryStringBuilder.CreateOrQueryString(fieldName, key);
            for (int i = 0; i < c; i++)
            {
                fl = (IFeatureLayer)this._mapHook.get_Layer(i);

                name = fl.FeatureClass.AliasName;
                //MessageBox.Show(string.Format("line 260 GMap name={0} - layername={1}", name,layerName));
                if (Compare.Compare2SdeLayerName(name, layerName) == 0)
                {
                    hasLayer = true;
                    //MessageBox.Show(string.Format("line 261 GMap layername={0}", name));
                    fc = fl.FeatureClass;
                    break;
                }
            }
            if (!hasLayer)
            {
                try
                {
                    fc = fw.OpenFeatureClass(layerName);
                }
                catch (Exception)
                {
                    return;
                }
                fl = new FeatureLayerClass();
                fl.FeatureClass = fc;
                fl.Name = fc.AliasName;
                ILayer layer = (ILayer)fl;
                this._mapHook.AddLayer(layer, 0);
            }
            if (fl == null)
            {
                //MessageBox.Show(string.Format("line 285 GMap"));
                return;
            }
            IQueryFilter qrf = new QueryFilterClass();
            qrf.WhereClause = queryString;
            //MessageBox.Show(string.Format("line 290 GMap, qrf={0}",queryString));
            ISelectionSet sls = fc.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
            IFeatureSelection fts = (IFeatureSelection)fl;
            fts.SelectionSet = sls;
            //MessageBox.Show(string.Format("selection count={0}", sls.Count));
            _mapHook.ActiveView.Refresh();
            ((IGMap)this).ZoomToSelected();
        }
        protected override void calculate()
        {
            #region khoi dau
            base.calculate();
            CalculationEventArg evt = new CalculationEventArg();
            //evt.Type = EnumTypeOfLoopCalculation.InListCalculators;
            evt.CurrentIndexCalculator = this._index;
            evt.Log = string.Format("**********  Bắt đầu tính cho thửa nông nghiệp ở vị trí {0} ********", vitri);
            onCalculating(evt);
            #endregion

            //[thaydoi] - them cac khai bao can thiet
            //************************************
            #region khai bao cac bien
            //Lay connection info hien tai
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
            this._fcName = new TnFeatureClassName(sdeConn.Workspace);
            this._tblName = new TnTableName(sdeConn.Workspace);
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ICopyFeatures copyTool = new DataManager(sdeConn.Workspace,sdeConn.Environment);

            #region thua
            string thuaName = string.Format("{0}_{1}", DataNameTemplate.Thua, this._currentConfig.NamApDung);
            _fcName.FC_THUA.NAME = thuaName;
            _fcName.FC_THUA.InitIndex();
            IFeatureClass thuaFeatureClass = fw.OpenFeatureClass(thuaName);
            IFeatureLayer thuaFeatureLayer = new FeatureLayerClass();
            thuaFeatureLayer.FeatureClass = thuaFeatureClass;
            IFeatureSelection thuaFeatureSelection;
            #endregion
            #region xa
            IFeatureClass xaFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Ranh_Xa_Poly);
            IFeatureLayer xaFeatureLayer = new FeatureLayerClass();
            xaFeatureLayer.FeatureClass = xaFeatureClass;
            IFeatureSelection xaFeatureSelection;
            #endregion
            #region duong
            IFeatureClass duongFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Duong);
            IFeatureLayer duongFeatureLayer = new FeatureLayerClass();
            duongFeatureLayer.FeatureClass = duongFeatureClass;
            IFeatureSelection duongFeatureSelection;
            //_fcName.FC_DUONG.InitIndex();
            #endregion

            #region thua gia dat
            string tgd = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat_Draft, this._currentConfig.NamApDung);
            _fcName.FC_THUA_GIADAT_DRAFT.NAME = tgd;
            _fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            //_fcName.FC_THUA_GIADAT_DRAFT.NAME = tgd;
            //_fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            IFeatureClass tgdFeatureClass=null;
            try
            {
                tgdFeatureClass = fw.OpenFeatureClass(tgd);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", tgd);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            ITable tblThuaGiaDat = (ITable)tgdFeatureClass;
            IFeatureLayer tgdFeatureLayer = new FeatureLayerClass();
            ISDETableEditor sdeTblTgdEditor = new SDETable(tblThuaGiaDat, sdeConn.Workspace);
            #endregion

            #region gia dat duong
            //string gdd = string.Format("{0}_{1}", DataNameTemplate.Gia_Dat_Duong, this._currentConfig.NamApDung);
            //_tblName.GIA_DAT_DUONG.NAME = gdd;
            //_tblName.GIA_DAT_DUONG.InitIndex();
            #endregion

            #region ten duong
            ITable tblTenDuong;
            try
            {
                tblTenDuong = fw.OpenTable(DataNameTemplate.Ten_Duong);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Ten_Duong);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region loai dat
            ITable tblLoaiDat;
            try
            {
                tblLoaiDat = fw.OpenTable(DataNameTemplate.Loai_Dat);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Loai_Dat);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region he so vi tri
            ITable tblHesoVitri;
            try
            {
                tblHesoVitri = fw.OpenTable(DataNameTemplate.He_So_K);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.He_So_K);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region khac
            IQueryFilter qrf = new QueryFilterClass();
            bool result = false;
            int sothuatimthay = 0;
            int sothuatinhduoc = 0;
            int sothuaKhongTinhDuoc = 0;
            #endregion

            #endregion
            //************************************

            //*******************************************
            //===========================================
            //===========================================

            #region bat dau tinh

            //[thaydoi] - cac may tinh khac chi can thay dieu kien truy van he so vi tri
            //******************************************************************
            #region lay cac quy tac tim vi tri

            #region log---
            evt.Log = string.Format("\n----Lấy các quy tắc tìm vị trí thửa từ bảng {0}, ứng với hệ số {1} ...", DataNameTemplate.He_So_K, TnHeSoK.DatNongNghiepVt1);
            onCalculating(evt);
            #endregion

            qrf.WhereClause = string.Format("{0}='{1}'", "hesovitri", TnHeSoK.DatNongNghiepVt1);
            ICursor cur = tblHesoVitri.Search(qrf, false);
            string quytac = "";
            string cachtinh = "";
            string cachtinhdongia = "";
            try
            {
                IRow row = cur.NextRow();
                if (row != null)
                {
                    quytac = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.QUY_TAC)).ToString();
                    cachtinh = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH)).ToString();
                    cachtinhdongia = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH_DON_GIA)).ToString();
                }
            }
            catch { }
            finally
            {
                Marshal.ReleaseComObject(cur);
            }
            #endregion

            #region tinh theo xa
            if (this._inputParams.MA_XA != "-1")
            {
                //[kodoi]
                //=======================
                #region chon xa co ma dang xet
                //neu dang tinh cho 1 xa
                if (_inputParams.MA_XA != "*")
                {
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.MA_XA, _inputParams.MA_XA);
                }
                else//neu dang tinh cho toan huyen
                {
                    qrf.WhereClause = "";
                }
                ISelectionSet xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                xaFeatureSelection.SelectionSet = xaSelectionSet;
                #endregion
                //=======================

                #region vong lap tung xa

                //[thaydoi] - co the them bien chay
                //*************************
                #region khoi dau
                IEnumIDs xaIds = xaSelectionSet.IDs;
                int xaId = 0;
                IFeature xaFt = null;
                List<object> lstMaDuong = new List<object>();
                #endregion
                //*************************

                while ((xaId = xaIds.Next()) != -1)
                {
                    //[kodoi]
                    //=======================
                    #region lay thong tin xa
                    xaFt = xaFeatureClass.GetFeature(xaId);
                    string maxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_XA)).ToString();
                    string tenxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.TEN_XA)).ToString();
                    string loaixa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_LOAI_XA)).ToString();
                    #endregion

                    #region -----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho xã/phường: {0}  ******", tenxa);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion

                    #region tim duong trong khu vuc do thi bang qua xa
                    //chon xa dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.OID, xaId);
                    xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                    xaFeatureSelection.SelectionSet = xaSelectionSet;
                    //Chon cac duong co do rong theo quy dinh
                    qrf.WhereClause = "";//string.Format("{0}='{1}'", _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";
                    ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                    duongFeatureSelection.SelectionSet = duongSelectionSet;

                    //bat dau truy van khong gian
                    _qrBl.FromLayer = duongFeatureLayer;
                    _qrBl.ByLayer = xaFeatureLayer;
                    _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                    _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultAnd;
                    _qrBl.UseSelectedFeatures = true;

                    duongSelectionSet = _qrBl.Select();

                    #endregion
                    //=======================

                    #region vong lap xet tung duong

                    //[thaydoi] - co the them bien chay
                    //===============================
                    #region khoi dau
                    IEnumIDs eIds = duongSelectionSet.IDs;
                    int duongId;
                    IFeature ftDuong;
                    int iDuong = 0;
                    int progressingTotalCount = 1;
                    evt.Reset();
                    evt.ProgressingTotal = duongSelectionSet.Count;
                    onCalculating(evt);
                    #endregion
                    //================================

                    while ((duongId = eIds.Next()) != -1)
                    {
                        //[kodoi]
                        //======================
                        #region log-----
                        evt.Reset();
                        evt.ProgressingTotalCount = progressingTotalCount;
                        onCalculating(evt);
                        progressingTotalCount++;
                        #endregion
                        //======================

                        //[capnhat] - lay ten duong tu bang ten duong
                        //++++++++++++++++++++++++
                        #region lay thong tin cua duong dang xet

                        ftDuong = duongFeatureClass.GetFeature(duongId);
                        double dorongDuong;
                        result = double.TryParse(ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.DO_RONG)).ToString(), out dorongDuong);
                        if (!result)
                        {
                            dorongDuong = 0;
                        }
                        if (dorongDuong < _currentConfig.DRongDuongVitri1Nn)
                        {
                            continue;
                        }
                        string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                        object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                        lstMaDuong.Add(maduong);
                        string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                        string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                        #endregion
                        //++++++++++++++++++++++++

                        //[kodoi]
                        //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                        //====================
                        #region chon duong dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                        duongFeatureSelection.SelectionSet = duongSelectionSet;
                        #endregion
                        //====================

                        //tim cac thua theo dieu kien mat tien
                        #region tinh vi tri cho cac thua
                        //doc cach tim thua trong bang he so vi tri
                        //lay cach tim cho dat o
                        //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                        //MessageBox.Show(func);
                        //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                        //[kodoi]
                        //============================
                        #region tim cac thua theo vi tri quy dinh
                        thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                        thuaFeatureSelection.Clear();
                        Evaluation eval = new Evaluation(quytac);
                        eval.ThuaLayer = thuaFeatureLayer;
                        eval.DuongLayer = duongFeatureLayer;
                        eval.EvaluateQuery();
                        //thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                        ISelectionSet thuaSelectionSet = thuaFeatureSelection.SelectionSet;
                        #endregion
                        //============================

                        //MessageBox.Show(thuaSelectionSet.Count.ToString());
                        #endregion

                        //[kodoi]
                        //============================
                        #region ----log
                        if (thuaSelectionSet.Count == 0)
                        {
                            evt.Log = string.Format("\n !!! Không tìm thấy thửa nào tiếp giáp với đường {0} đoạn từ {1} đến {2} và nằm trong vùng buffer 50m", tenduong, batdau, ketthuc);
                            onCalculating(evt);

                            #region report progressing duong
                            if (iDuong < duongSelectionSet.Count)
                            {
                                decimal i = (decimal)iDuong % (decimal)duongSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iDuong++;
                            #endregion

                            continue;
                        }
                        #endregion
                        sothuatimthay += thuaSelectionSet.Count;
                        //============================

                        #region xet tung thua

                        //[thaydoi] - co the them bien chay
                        //********************
                        #region khoi dau
                        IEnumIDs thuaIds = thuaSelectionSet.IDs;
                        int thuaId;
                        IFeature thuaFt;
                        int iThua = 0;
                        List<object[,]> pairColValTgd = new List<object[,]>();
                        int rowTgdNnHandleUpdate = 0;
                        List<object> newId = new List<object>();
                        #endregion
                        //********************

                        while ((thuaId = thuaIds.Next()) != -1)
                        {
                            int hesoVitri = TnHeSoK.DatNongNghiepVt1;

                            //[thaydoi] - co them them dieu kien de xac dinh vi tri cua thua
                            //***********************************
                            #region lay thong tin thua dang xet

                            //[kodoi]
                            //============================
                            #region lay thong tin co ban
                            thuaFt = thuaFeatureClass.GetFeature(thuaId);
                            //neu thua bi khoa tim vi tri,bo qua,xet thua ke
                            string lockTimVitri = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOCKED)).ToString();
                            if (lockTimVitri == "1")
                            {
                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;

                            }
                            //neu thua ko thuoc xa dang xet thi ko tinh tiep
                            object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                            object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                            object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));
                            #endregion
                            //============================

                            //[kodoi]
                            //============================
                            #region kiem tra maxa
                            if (maxaThua.ToString() == "" || maxaThua == null)
                            {
                                maxaThua = "0";

                                #region ----log
                                evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                                #endregion

                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }

                            #region danh rieng khi tinh theo xa
                            else if (maxaThua.ToString() != maxa)
                            {
                                #region ----log
                                evt.Reset();
                                evt.Log = string.Format("thửa {0} không thuộc xã {1}", mathua, tenxa);
                                onCalculating(evt);
                                #endregion
                                sothuaKhongTinhDuoc++;
                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #endregion

                            #endregion
                            //============================

                            #region lay loai dat

                            //neu ko co dat phi nong nghiep thi bo qua,xet thua ke
                            //neu co dat nong nghiep thi datnn=true
                            //neu la dat hon hop, neu co dat sxkd thi datsxkd=true,
                            //neu co dat o thi codato=true
                            //cac truong hop he so vi tri:
                            //o:3010, sxkd:4010, o+nn:5010, o+sxkd:6010
                            string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                            //evt.Log = string.Format("\n loai dat cua thua {0} la {1}", thuaId, loaidat);
                            //onCalculating(evt);

                            //[thaydoi] - may tinh dat nong nghiep co the khac
                            //***********************
                            #region kiem tra dat nong nghiep
                            bool codatnn = false;
                            object datnn = "";
                            foreach (string s in TnLoaiDats.NONG_NGHIEP)
                            {
                                if (loaidat == s)
                                {
                                    codatnn = true;
                                    datnn = s;
                                    break;
                                }
                                else
                                {
                                    codatnn = false;
                                }
                            }
                            if (!codatnn)
                            {
                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #endregion
                            //***********************

                            #endregion

                            #endregion
                            //***********************************

                            //[thaydoi] - thay doi he so vi tri phu hop
                            //***********************************
                            #region quyet dinh he so vi tri cho thua
                            if (codatnn)
                            {
                                hesoVitri = TnHeSoK.DatNongNghiepVt1;
                            }
                            else
                            {
                                hesoVitri = TnHeSoK.KhongXacDinh;
                            }
                            #endregion
                            //***********************************

                            //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                            //***********************************
                            #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                            evt.Reset();
                            evt.Log = string.Format("\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới cho thửa {0} ...", mathua);
                            onCalculating(evt);
                            qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                                _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                            ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);
                            IRow tgdRow = null;
                            //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                            try
                            {
                                tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                                if (tgdRow != null)
                                {
                                    //MessageBox.Show("co");
                                    //kiem tra co cho phep tinh lai vi tri
                                    //neu co:xoa feater cu,them feature moi
                                    #region xet thua da co vi tri

                                    bool isOverWritePos = true;

                                    if (!isOverWritePos)
                                    {
                                        newId.Add(tgdRow.OID);
                                        //continue;
                                    }
                                    else
                                    {
                                        //[kodoi]
                                        //===================
                                        #region xoa feature cu
                                        wspEdit.StartEditing(true);
                                        wspEdit.StartEditOperation();
                                        //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                        //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                        tgdRow.Delete();
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                        #endregion
                                        //===================

                                        //[thaydoi] - them gia tri
                                        //**********************
                                        #region them feature moi
                                        wspEdit.StartEditing(true);
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                        sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        newId.Add(copiedId);
                                        #endregion
                                        //**********************
                                    }

                                    #endregion

                                }
                                else
                                {
                                    //MessageBox.Show("ko co");
                                    //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                    //***********************************
                                    #region them feature moi
                                    wspEdit.StartEditing(true);
                                    wspEdit.StartEditOperation();
                                    object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);

                                    //them gia tri mathua,maduong,hesovitri
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                    sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    newId.Add(copiedId);
                                    #endregion
                                    //***********************************
                                }
                                //MessageBox.Show(newId.Count.ToString());
                            }
                            catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                            finally { Marshal.ReleaseComObject(tgdFcs); }
                            #endregion
                            //***********************************

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion
                        }
                        #endregion

                        //[kodoi]
                        //============================
                        #region luu thong tin vao bang gia dat
                        if (!sdeTblTgdEditor.IsEditing())
                        {
                            sdeTblTgdEditor.StartEditing(true);
                            sdeTblTgdEditor.StartEditOperation();
                        }

                        #region ----log
                        evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                        onCalculating(evt);
                        #endregion

                        sdeTblTgdEditor.SaveEdit();
                        sdeTblTgdEditor.StopEditOperation();
                        sdeTblTgdEditor.StopEditing(true);

                        #region ----log
                        evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                        onCalculating(evt);
                        #endregion

                        #endregion

                        #region tinh gia dat cho cac thua vua them vi tri
                        CalcLandprice calc = new CalcLandprice(this);
                        //MessageBox.Show(string.Format("final:{0}", newId.Count));
                        calc.Maduong = maduong;
                        calc.Calculate(newId);
                        newId.Clear();
                        #endregion
                        //============================

                    }
                    #endregion

                }
                #endregion
            }
            #endregion
            //=====================================================================
            //=====================================================================
            #region tinh theo duong,doan duong
            else
            {
                #region timduong co ten dang xet
                //tinh theo doan duong
                #region tinh theo doan duong
                if (_inputParams.MA_DUONG != "-1")
                {
                    qrf.WhereClause = string.Format("{0}={1}", _fcName.FC_DUONG.MA_DUONG, _inputParams.MA_DUONG);// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho đoạn đường có mã: {0}  ******", _inputParams.MA_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                }
                #endregion
                //tinh cho 1 duong
                #region tinh cho 1 duong
                else if (_inputParams.TEN_DUONG != "" && _inputParams.TEN_DUONG != "*")
                {
                    //phai sua lai thiet ke
                    //dung relationshipclass many to many gia duong va ten duong
                    #region tim trong bang ten duong
                    qrf.WhereClause = string.Format("{0}=N'{1}'", _tblName.TEN_DUONG.TEN_DUONG, _inputParams.TEN_DUONG);
                    //MessageBox.Show(string.Format("line 908 CalPosThuaNnVt1, tenduong={0}", _inputParams.TEN_DUONG));
                    ICursor tenduongCur = tblTenDuong.Search(qrf, false);
                    object idDuong;
                    List<object> lstIdDuong=new List<object>();
                    try
                    {
                        IRow tenduongRow = null;
                        while ((tenduongRow = tenduongCur.NextRow()) != null)
                        {
                            idDuong = tenduongRow.get_Value(tenduongRow.Fields.FindField(_tblName.TEN_DUONG.MA_DUONG));
                            lstIdDuong.Add(idDuong);
                        }
                    }
                    catch
                    { }
                    finally{Marshal.ReleaseComObject(tenduongCur);}
                    #endregion

                    #region tim trong bang duong
                    string q = "";
                    for (int i = 0; i < lstIdDuong.Count; i++)
                    {
                        if (i == lstIdDuong.Count - 1)
                        {
                            q +=string.Format("{0}='{1}'",_fcName.FC_DUONG.MA_DUONG,lstIdDuong[i]);
                        }
                        else
                        {
                            q += string.Format("{0}='{1}' or ", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                    }
                        qrf.WhereClause = q;// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho cả đường: {0}  ******", _inputParams.TEN_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                    #endregion
                }
                #endregion
                //tinh cho ca duong
                else
                {
                    qrf.WhereClause = "";
                }
                //MessageBox.Show(string.Format("whereclause:{0}",duong.QueryFilter.WhereClause));
                ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);

                if (duongSelectionSet.Count == 0)
                {
                    return;
                }
                duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                duongFeatureSelection.SelectionSet = duongSelectionSet;

                #endregion

                #region vong lap xet tung duong

                //[thaydoi] - co the them bien chay
                //===============================
                #region khoi dau
                IEnumIDs eIds = duongSelectionSet.IDs;
                int duongId;
                IFeature ftDuong;
                int iDuong = 0;
                int progressingTotalCount = 1;
                evt.Reset();
                evt.ProgressingTotal = duongSelectionSet.Count;
                onCalculating(evt);
                List<object> lstMaDuong = new List<object>();
                #endregion
                //================================

                while ((duongId = eIds.Next()) != -1)
                {
                    //[kodoi]
                    //======================
                    #region log-----
                    evt.Reset();
                    evt.ProgressingTotalCount = progressingTotalCount;
                    onCalculating(evt);
                    progressingTotalCount++;
                    #endregion
                    //======================

                    //[capnhat] - lay ten duong tu bang ten duong
                    //++++++++++++++++++++++++
                    #region lay thong tin cua duong dang xet

                    ftDuong = duongFeatureClass.GetFeature(duongId);
                    double dorongDuong;
                    result = double.TryParse(ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.DO_RONG)).ToString(), out dorongDuong);
                    if (!result)
                    {
                        dorongDuong = 0;
                    }
                    if (dorongDuong < _currentConfig.DRongDuongVitri1Nn)
                    {
                        continue;
                    }
                    string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                    object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                    lstMaDuong.Add(maduong);
                    string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                    string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                    #endregion
                    //++++++++++++++++++++++++

                    //[kodoi]
                    //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                    //====================
                    #region chon duong dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                    duongFeatureSelection.SelectionSet = duongSelectionSet;
                    #endregion
                    //====================

                    //tim cac thua theo dieu kien mat tien
                    #region tinh vi tri cho cac thua
                    //doc cach tim thua trong bang he so vi tri
                    //lay cach tim cho dat o
                    //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                    //MessageBox.Show(func);
                    //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                    //[kodoi]
                    //============================
                    #region tim cac thua theo vi tri quy dinh
                    thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                    thuaFeatureSelection.Clear();
                    Evaluation eval = new Evaluation(quytac);
                    eval.ThuaLayer = thuaFeatureLayer;
                    eval.DuongLayer = duongFeatureLayer;
                    eval.EvaluateQuery();
                    //thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                    ISelectionSet thuaSelectionSet = thuaFeatureSelection.SelectionSet;
                    #endregion
                    //============================

                    //MessageBox.Show(thuaSelectionSet.Count.ToString());
                    #endregion

                    //[kodoi]
                    //============================
                    #region ----log
                    if (thuaSelectionSet.Count == 0)
                    {
                        evt.Log = string.Format("\n !!! Không tìm thấy thửa nào tiếp giáp với đường {0} đoạn từ {1} đến {2} và nằm trong vùng buffer 50m", tenduong, batdau, ketthuc);
                        onCalculating(evt);

                        #region report progressing duong
                        if (iDuong < duongSelectionSet.Count)
                        {
                            decimal i = (decimal)iDuong % (decimal)duongSelectionSet.Count * 100;
                            int i1 = Convert.ToInt32(i);
                            this._bgwCalculating.ReportProgress(i1);
                            //MessageBox.Show("log 009");
                        }
                        else
                        {
                            this._bgwCalculating.ReportProgress(99);
                        }
                        iDuong++;
                        #endregion

                        continue;
                    }
                    #endregion
                    sothuatimthay += thuaSelectionSet.Count;
                    //============================

                    #region xet tung thua

                    //[thaydoi] - co the them bien chay
                    //********************
                    #region khoi dau
                    IEnumIDs thuaIds = thuaSelectionSet.IDs;
                    int thuaId;
                    IFeature thuaFt;
                    int iThua = 0;
                    List<object[,]> pairColValTgd = new List<object[,]>();
                    int rowTgdNnHandleUpdate = 0;
                    List<object> newId = new List<object>();
                    #endregion
                    //********************

                    while ((thuaId = thuaIds.Next()) != -1)
                    {
                        int hesoVitri = TnHeSoK.DatNongNghiepVt1;

                        //[thaydoi] - co them them dieu kien de xac dinh vi tri cua thua
                        //***********************************
                        #region lay thong tin thua dang xet

                        //[kodoi]
                        //============================
                        #region lay thong tin co ban
                        thuaFt = thuaFeatureClass.GetFeature(thuaId);
                        //neu thua bi khoa tim vi tri,bo qua,xet thua ke
                        string lockTimVitri = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOCKED)).ToString();
                        if (lockTimVitri == "1")
                        {
                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;

                        }
                        //neu thua ko thuoc xa dang xet thi ko tinh tiep
                        object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                        object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                        object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));
                        #endregion
                        //============================

                        //[kodoi]
                        //============================
                        #region kiem tra maxa
                        if (maxaThua.ToString() == "" || maxaThua==null)
                        {
                            maxaThua = "0";

                            #region ----log
                            evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                            #endregion

                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;
                        }
                        #endregion
                        //============================

                        #region lay loai dat

                        //neu ko co dat phi nong nghiep thi bo qua,xet thua ke
                        //neu co dat nong nghiep thi datnn=true
                        //neu la dat hon hop, neu co dat sxkd thi datsxkd=true,
                        //neu co dat o thi codato=true
                        //cac truong hop he so vi tri:
                        //o:3010, sxkd:4010, o+nn:5010, o+sxkd:6010
                        string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                        //evt.Log = string.Format("\n loai dat cua thua {0} la {1}", thuaId, loaidat);
                        //onCalculating(evt);

                        //[thaydoi] - may tinh dat nong nghiep co the khac
                        //***********************
                        #region kiem tra dat nong nghiep
                        bool codatnn = false;
                        object datnn="";
                        foreach (string s in TnLoaiDats.NONG_NGHIEP)
                        {
                            if (loaidat==s)
                            {
                                codatnn = true;
                                datnn = s;
                                break;
                            }
                            else
                            {
                                codatnn = false;
                            }
                        }
                        if (!codatnn)
                        {
                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;
                        }
                        #endregion
                        //***********************

                        #endregion

                        #endregion
                        //***********************************

                        //[thaydoi] - thay doi he so vi tri phu hop
                        //***********************************
                        #region quyet dinh he so vi tri cho thua
                        if (codatnn)
                        {
                            hesoVitri = TnHeSoK.DatNongNghiepVt1;
                        }
                        else
                        {
                            hesoVitri = TnHeSoK.KhongXacDinh;
                        }
                        #endregion
                        //***********************************

                        //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                        //***********************************
                        #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                        evt.Reset();
                        evt.Log = string.Format("\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới cho thửa {0} ...", mathua);
                        onCalculating(evt);
                        qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                            _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                            _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                            _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                            _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                        ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);
                        IRow tgdRow = null;
                        //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                        try
                        {
                            tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                            if (tgdRow != null)
                            {
                                //MessageBox.Show("co");
                                //kiem tra co cho phep tinh lai vi tri
                                //neu co:xoa feater cu,them feature moi
                                #region xet thua da co vi tri

                                bool isOverWritePos = true;

                                if (!isOverWritePos)
                                {
                                    newId.Add(tgdRow.OID);
                                    //continue;
                                }
                                else
                                {
                                    //[kodoi]
                                    //===================
                                    #region xoa feature cu
                                    wspEdit.StartEditing(true);
                                    wspEdit.StartEditOperation();
                                    //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                    //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                    tgdRow.Delete();
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);
                                    #endregion
                                    //===================

                                    //[thaydoi] - them gia tri
                                    //**********************
                                    #region them feature moi
                                    wspEdit.StartEditing(true);
                                    wspEdit.StartEditOperation();
                                    object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);

                                    //them gia tri mathua,maduong,hesovitri
                                    pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                    pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                    sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    newId.Add(copiedId);
                                    #endregion
                                    //**********************
                                }

                                #endregion

                            }
                            else
                            {
                                //MessageBox.Show("ko co");
                                //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                //***********************************
                                #region them feature moi
                                wspEdit.StartEditing(true);
                                wspEdit.StartEditOperation();
                                object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                wspEdit.StopEditOperation();
                                wspEdit.StopEditing(true);

                                //them gia tri mathua,maduong,hesovitri
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                rowTgdNnHandleUpdate++;
                                pairColValTgd.Clear();
                                newId.Add(copiedId);
                                #endregion
                                //***********************************
                            }
                            //MessageBox.Show(newId.Count.ToString());
                        }
                        catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                        finally { Marshal.ReleaseComObject(tgdFcs); }
                        #endregion
                        //***********************************

                        #region report progressing thua
                        if (iThua < thuaSelectionSet.Count)
                        {
                            decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                            int i1 = Convert.ToInt32(i);
                            this._bgwCalculating.ReportProgress(i1);
                            //MessageBox.Show("log 009");
                        }
                        else
                        {
                            this._bgwCalculating.ReportProgress(99);
                        }
                        iThua++;
                        #endregion
                    }
                    #endregion

                    //[kodoi]
                    //============================
                    #region luu thong tin vao bang gia dat
                    if (!sdeTblTgdEditor.IsEditing())
                    {
                        sdeTblTgdEditor.StartEditing(true);
                        sdeTblTgdEditor.StartEditOperation();
                    }

                    #region ----log
                    evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                    onCalculating(evt);
                    #endregion

                    sdeTblTgdEditor.SaveEdit();
                    sdeTblTgdEditor.StopEditOperation();
                    sdeTblTgdEditor.StopEditing(true);

                    #region ----log
                    evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                    onCalculating(evt);
                    #endregion

                    #endregion

                    #region tinh gia dat cho cac thua vua them vi tri
                    CalcLandprice calc = new CalcLandprice(this);
                    //MessageBox.Show(string.Format("final:{0}", newId.Count));
                    calc.Maduong = maduong;
                    calc.Calculate(newId);
                    newId.Clear();
                    #endregion
                    //============================

                }
                #endregion
            }
            #endregion

            #endregion

            //===========================================
            //===========================================
            //*******************************************

            //[kodoi]
            //==============
            #region doan ket
            sothuatinhduoc = sothuatimthay - sothuaKhongTinhDuoc;

            #region ----log
            evt.Log = string.Format("\n\n**************************************\n******* Đã tính xong các thửa phi nông nghiệp tại đô thị vị trí mặt tiền*******\n**********************************");
            //evt.Log += string.Format("\n Số thửa tìm thấy: {0}", sothuatimthay);
            //evt.Log += string.Format("\n Số thửa được tính: {0}", sothuatinhduoc);
            onCalculating(evt);
            #endregion
            evt.Reset();
            evt.ProgressingTotalCount = ".";
            evt.ProgressingTotal = ".";
            onCalculating(evt);

            evt.CurrentIndexCalculator = this._index;
            onFinished(evt);
            #endregion
            //==============
        }
Beispiel #17
0
 void IQueryThuaView.Query()
 {
     //this.Cursor = Cursors.AppStarting;
     if (_conn == null)
     {
         _conn = new SdeConnection();
         _sdeConn = (ISdeConnectionInfo)_conn;
         _fcName = new TnFeatureClassName(_sdeConn.Workspace);
     }
     _info.SetWorkspace(_sdeConn.Workspace);
     _controller.Query() ;
 }
 private void turnOnData()
 {
     SdeConnection conn = new SdeConnection();
     ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
     _fcName = new TnFeatureClassName(sdeConn.Workspace);
     _tblName = new TnTableName(sdeConn.Workspace);
 }
        private void registerVersionedForAllLayer()
        {
            if (!Settings.Default.ShowRegisterVersioned)
            {
                return;
            }
            FrmVerifyRegisterVersioned frm = new FrmVerifyRegisterVersioned();
            frm.ShowDialog();
            if (frm.ShowAgain)
            {
                Settings.Default.ShowRegisterVersioned = true;
            }
            else
            {
                Settings.Default.ShowRegisterVersioned = false;
            }
            Settings.Default.Save();
            if (!frm.OK)
            {
                MessageBox.Show("Bạn chưa đăng ký versioned cho dữ liệu");
                return;
            }
            ISdeVersionInfo _version;
            _version = SdeVersionsTool.CallMe();
            SdeConnection conn=new SdeConnection();
            ISdeConnectionInfo sdeConn=(ISdeConnectionInfo)conn;

            ISDETableQuery qrf = new SDETable();
            List<string> layers = qrf.GetLayersAndTables();
            foreach (string s in layers)
            {
                try
                {
                    ITable table = ((IFeatureWorkspace)sdeConn.Workspace).OpenTable(s);
                    _version.RegisterDataset((IDataset)table, true, true);
                }
                catch (Exception ex) { continue; }
            }
        }
Beispiel #20
0
        private void loadData(string table)
        {
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
            ISqlUserInfo _user = sdeConn.GetSqlUserInfo();
            try
            {
                _oAdapter = new OleDbDataAdapter("select * from " + table, sdeConn.GetOleDbConnection());
                _dataset.Reset();
                _oAdapter.Fill(_dataset);
                this.grcAttributeTable.DataSource = _dataset.Tables[0];
            }
            catch { }

            hide2Cols();
            //try
            //{
            //    if (sqlTable.IsClosed())
            //    {
            //        sqlTable.SetUserInfo(_user.GetConnectionStringAsArray());
            //        sqlTable.OpenConnection();
            //    }
            //    string sql = "select * from " + table;

            //    using (SqlDataAdapter adap = new SqlDataAdapter(sql, sqlTable.GetConnection))
            //    {
            //        DataTable t = new DataTable();
            //        adap.Fill(t);
            //        this.grcAttributeTable.DataSource = t;
            //        //grvLoaiXa.Columns.ColumnByFieldName(_tableName.LOAI_XA.OID).Visible = false;
            //        //gridView1.Columns.ColumnByName("duocchon").ColumnEdit = this.repositoryItemCheckEdit1;
            //    }
            //}
            //catch { }
        }
Beispiel #21
0
 void IEditTableView.DbConnectOccur()
 {
     _isConnected = true;
     if (this._tableName == "")
     {
         return;
     }
     SdeConnection conn = new SdeConnection();
     ISdeConnectionInfo sdeConn = (ISdeConnectionInfo)conn;
     IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
     ITable table = null;
     try
     {
         table = fw.OpenTable(this._tableName);
         loadData(this._tableName);
     }
     catch (Exception e)
     {
         MessageBox.Show(string.Format("Không tìm thấy bảng {0}", this._tableName));
         return;
     }
     this._sdeEditor = new SDETable(table, sdeConn.Workspace);
 }
Beispiel #22
0
        void IAttributeQuery.Excute()
        {
            //*************
            //Lay connection info hien tai
            //SdeUserInfo va SqlUserInfo la static
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            ISqlConnectionInfo sqlConn = (ISqlConnectionInfo)sdeConn;
            _sqlUser = sqlConn.GetSqlUserInfo();

            SqlDataReader reader = null;
            QueryEventArg e = new QueryEventArg();

            try
            {
                if (_sqlTable.IsClosed())
                {
                    _sqlTable.SetUserInfo(_sqlUser.GetConnectionStringAsArray());
                    _sqlTable.OpenConnection();
                }

                reader = _sqlTable.GetSqlCommand(_sql).ExecuteReader();
                //MessageBox.Show("line 49 Loader: --" + reader.HasRows.ToString() + "--" + _sql);
                //_pair.ClearPair();
                foreach (IAttributeQueryView r in _views)
                {
                    r.ReceiveResult(reader,EnumTypeOfResult.Thua);
                }
                reader.Close();
                _sqlTable.CloseConnection();
                onFinished(e);

            }
            catch (Exception e1)
            {
                if (reader != null)
                {
                    reader.Close();
                }
                _sqlTable.CloseConnection();
                //MessageBox.Show("line 67 - Loader \n" + e1.ToString());
            }
        }
Beispiel #23
0
 void IAttributeQuery.ExcuteForDataSet()
 {
     try
     {
         SdeConnection conn = new SdeConnection();
         ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
         OleDbDataAdapter oAdapter = new OleDbDataAdapter(this._selectSql, sdeConn.GetOleDbConnection());
         DataSet dataset = new DataSet();
         oAdapter.Fill(dataset);
         foreach (IAttributeQueryView r in _views)
         {
             r.ReceiveResult(dataset);
         }
     }
     catch
     { }
 }
Beispiel #24
0
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            bool removeOldPos = true;
            if (chkRemoveOldPos.CheckState == CheckState.Unchecked)
            {
                removeOldPos = false;
            }
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
            IMultiuserWorkspaceEdit mwspEdit = (IMultiuserWorkspaceEdit)sdeConn.Workspace;
            ITnFeatureClassName _fcName = new TnFeatureClassName(sdeConn.Workspace);
            ITnTableName _tblName = new TnTableName(sdeConn.Workspace);
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ICopyFeatures copyTool = new DataManager(sdeConn.Workspace, sdeConn.Environment);
            IQueryFilter qrf = new QueryFilterClass();
            IFeatureClass tgdFeatureClass = null;
            ICurrentConfig config = CurrentConfig.CallMe();
            List<object[,]> pairColValTgd = new List<object[,]>();
            int rowTgdNnHandleUpdate = 0;
            List<object> newId = new List<object>();
            string tgdDraft = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat_Draft, config.NamApDung);
            _fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            _fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            try
            {
                tgdFeatureClass = fw.OpenFeatureClass(tgdDraft);
            }
            catch (Exception exc)
            {
                //evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", tgdDraft);
                //onCalculating(evt);
                //onFinished(evt);
                return;
            }
            ITable tblThuaGiaDat = (ITable)tgdFeatureClass;
            ISDETableEditor sdeTblTgdEditor = new SDETable(tblThuaGiaDat, sdeConn.Workspace);

            //lay thong tin thua
            for (int i=0;i< _lstThuaFt.Count;i++)
            {
                IFeature thuaFt = _lstThuaFt[i];

                //string lockTimVitri = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOCKED)).ToString();
                object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));

                object mathua = _lstMaThua[i];//txtMathua.Text;
                string maduong = txtMaDuong.Text;
                string mahem = txtMaHem.Text;

                if (maduong == "" || maduong ==null)
                {
                    maduong = "0";
                }
                if (mahem == "" || mahem == null)
                {
                    mahem = "0";
                }
                string hesoVitri = txtHsk.Text;
                ICursor tgdFcs1;
                IRow tgdRow = null;
                if (removeOldPos)
                {
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua);
                    tgdFcs1=tblThuaGiaDat.Search(qrf, false);
                    try
                    {
                        while ((tgdRow = tgdFcs1.NextRow()) != null)
                        {
                            #region xoa feature cu
                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            wspEdit.StartEditOperation();
                            //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                            //tblThuaGiaDat.DeleteSearchedRows(qrf);
                            tgdRow.Delete();
                            wspEdit.StopEditOperation();
                            wspEdit.StopEditing(true);
                            #endregion
                        }
                    }
                    catch { }
                    finally { Marshal.ReleaseComObject(tgdFcs1); }
                }

                if (mahem == "0")
                {
                    qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                    _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                                    _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                }
                else if (mahem != "0")
                {
                    qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                    _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_HEM, mahem,
                                    _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                }
                //MessageBox.Show("line 157 FrmSet.., ",+ qrf.WhereClause);
                ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);

                try
                {
                    tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                    if (tgdRow != null)
                    {
                        //MessageBox.Show("co");
                        //kiem tra co cho phep tinh lai vi tri
                        //neu co:xoa feater cu,them feature moi
                        #region xet thua da co vi tri

                        bool isOverWritePos = true;

                        if (!isOverWritePos)
                        {
                            newId.Add(tgdRow.OID);
                            //continue;
                        }
                        else
                        {
                            //[kodoi]
                            //===================
                            #region xoa feature cu
                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            wspEdit.StartEditOperation();
                            //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                            //tblThuaGiaDat.DeleteSearchedRows(qrf);
                            tgdRow.Delete();
                            wspEdit.StopEditOperation();
                            wspEdit.StopEditing(true);
                            #endregion
                            //===================

                            //[thaydoi] - them gia tri
                            //**********************
                            #region them feature moi
                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            wspEdit.StartEditOperation();
                            object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                            wspEdit.StopEditOperation();
                            wspEdit.StopEditing(true);

                            //them gia tri mathua,maduong,hesovitri
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                            sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                            rowTgdNnHandleUpdate++;
                            pairColValTgd.Clear();
                            newId.Add(copiedId);
                            #endregion
                            //**********************
                        }

                        #endregion

                    }
                    else
                    {
                        //MessageBox.Show("ko co");
                        //MessageBox.Show("line 220 FrmSet.., " + thuaFt.ToString());
                        //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                        //***********************************
                        #region them feature moi
                        mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                        wspEdit.StartEditOperation();
                        object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                        //MessageBox.Show("line 220 FrmSet.., copiedid" + copiedId.ToString());
                        wspEdit.StopEditOperation();
                        wspEdit.StopEditing(true);

                        //them gia tri mathua,maduong,hesovitri
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                        //MessageBox.Show(string.Format("line 248 FrmSet.., mathua={0},maduong={1},hsk={2}, dientich={3}" , mathua,maduong,hesoVitri,dientichpl));
                        //MessageBox.Show(string.Format("line 249 FrmSet..,index mathua={0},maduong={1},hsk={2}, dientich={3}" , _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY)));
                        sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                        rowTgdNnHandleUpdate++;
                        pairColValTgd.Clear();
                        newId.Add(copiedId);
                        #endregion
                        //***********************************
                    }
                    //MessageBox.Show(newId.Count.ToString());
                }
                catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                finally { Marshal.ReleaseComObject(tgdFcs); }
            }
            #region luu thong tin vao bang gia dat
            //MessageBox.Show("line 250 FrmSet..., "+wspEdit.IsBeingEdited().ToString());
            if (!sdeTblTgdEditor.IsEditing())
            {
                sdeTblTgdEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                //MessageBox.Show("line 250 FrmSet..., " + wspEdit.IsBeingEdited().ToString());
                sdeTblTgdEditor.StartEditOperation();
            }
            try
            {
                sdeTblTgdEditor.SaveEdit();
                sdeTblTgdEditor.StopEditOperation();
                sdeTblTgdEditor.StopEditing(true);
            }
            catch
            {
                sdeTblTgdEditor.AbortEditOperation();
                sdeTblTgdEditor.StopEditing(false);
            }

            #endregion

            #region tinh gia dat cho cac thua vua them vi tri
            CalcLandprice calc = new CalcLandprice();
            calc.Calculate(newId);
            newId.Clear();
            #endregion
            this.Close();
        }
Beispiel #25
0
        void IGMap.CreateSelectionSet()
        {
            SdeConnection conn=new SdeConnection();
            ISdeConnectionInfo sdeConn=(ISdeConnectionInfo)conn;

            ILayer l=this._mapHook.get_Layer(0);
            IFeatureLayer fl = (IFeatureLayer)l;
            IFeatureClass fc = fl.FeatureClass;
            IQueryFilter qrf = new QueryFilterClass();
            //MessageBox.Show(_ma);
            qrf.WhereClause = string.Format("{0}='{1}'", "mathua", _ma);
            ISelectionSet sls= fc.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
            IFeatureSelection fts = (IFeatureSelection)fl;
            fts.SelectionSet = sls;
        }
Beispiel #26
0
 void ILandprice.Remove(object oid)
 {
     IQueryFilter qrf = new QueryFilterClass();
     qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_THUA_GIADAT.OID, oid);
     MessageBox.Show(string.Format("line 519 {0}", qrf.WhereClause));
     IFeatureCursor fcur = _thuaGiaDatFc.Search(qrf, false);
     IFeature ft = null;
     if((ft = fcur.NextFeature())== null)
     {
         return;
     }
     SdeConnection conn = new SdeConnection();
     ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
     IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
     IMultiuserWorkspaceEdit mwspEdit = (IMultiuserWorkspaceEdit)sdeConn.Workspace;
     wspEdit.StartEditing(true);
     wspEdit.StartEditOperation();
     try
     {
         ft.Delete();
         wspEdit.StopEditOperation();
         wspEdit.StopEditing(true);
     }
     catch (Exception ex) { MessageBox.Show("line 538 GLandprice, ex=" + ex.ToString()); }
     finally
     {
         wspEdit.StopEditOperation();
         wspEdit.StopEditing(false);
     }
 }
Beispiel #27
0
        void ILandpriceView.LoadPrice()
        {
            if (_conn == null)
            {
                _conn = new SdeConnection();
                _sdeConn = (ISdeConnectionInfo)_conn;
                _fw = (IFeatureWorkspace)_sdeConn.Workspace;
                _fcName = new TnFeatureClassName(_sdeConn.Workspace);
                _tblName = new TnTableName(_sdeConn.Workspace);

            }
            //MessageBox.Show(string.Format("line 122 GLandPriceView layerCount={0}", _mapView.GetMapControl().LayerCount));
            //if (_mapView.GetMapControl().LayerCount == 0)
            //{
            //    MessageBox.Show(string.Format("line 125 GLandPriceView layerCount={0}", _mapView.GetMapControl().LayerCount));
            //    _tgdName = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat, _conf.NamApDung);
            //    IFeatureClass fc = _fw.OpenFeatureClass(_tgdName);
            //    IFeatureLayer fl = new FeatureLayerClass();
            //    fl.FeatureClass = fc;
            //    fl.Name = fc.AliasName;
            //    ILayer layer = (ILayer)fl;
            //    _mapView.AddLayer(layer);
            //}

            _controller.LoadPrice();
        }
Beispiel #28
0
 void ISQLTable.SetUserInfo(string[,] userInfo)
 {
     //if (userInfo != null)
     //{
     //    _arrUserInfo = userInfo;
     //    int l = _arrUserInfo.Length / 2;
     //    for (int j = 0; j < l; j++)
     //    {
     //        if (j == 4)
     //            break;
     //        connetionString += _arrUserInfo[j, 0] + "=" + _arrUserInfo[j, 1];
     //        if (j < l - 1)
     //        {
     //            connetionString += ";";
     //        }
     //    }
     //}
     //else
     //{
     //    connetionString = GConnectionString.TRUSTED_CONNECT_NON_EXPRESS;
     //}
     //connetionString = "Server=ht-laptop;Database=sde,Uid=sde,Pwd=sde";//"Data Source=ht-laptop;Initial Catalog=sde;User ID=sde;Password=sde";
     //sql = "select * from tn_thua";
     //MessageBox.Show(connetionString);
     SdeConnection conn = new SdeConnection();
     ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
     ISdeUserInfo sdeUser = sdeConn.GetSdeUserInfo();
     ISqlUserInfo sqlUser = sdeUser.GetSqlUserInfo();
     //IUserInfo userInfo = (IUserInfo)sqlUser;
     connectionString = sqlUser.GetConnectionString();
     try
     {
         connection = new SqlConnection(connectionString);
     }
     catch (Exception e)
     { MessageBox.Show("line 131 - TnSQLTable"+e.InnerException.ToString()); }
 }
        protected override void calculate()
        {
            #region khoi dau
            base.calculate();
            CalculationEventArg evt = new CalculationEventArg();
            //evt.Type = EnumTypeOfLoopCalculation.InListCalculators;
            evt.CurrentIndexCalculator = this._index;
            evt.Log = string.Format("**********  Bắt đầu tính cho thửa phi nông nghiệp ở vị trí {0} tại đô thị ********", vitri);
            onCalculating(evt);
            #endregion

            //[thaydoi] - them cac khai bao can thiet
            //************************************
            #region khai bao cac bien
            //Lay connection info hien tai
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
            IMultiuserWorkspaceEdit mwspEdit = (IMultiuserWorkspaceEdit)sdeConn.Workspace;
            this._fcName = new TnFeatureClassName(sdeConn.Workspace);
            this._tblName = new TnTableName(sdeConn.Workspace);
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ICopyFeatures copyTool = new DataManager(sdeConn.Workspace, sdeConn.Environment);

            #region thua

            string thuaName = string.Format("{0}_{1}", DataNameTemplate.Thua, this._currentConfig.NamApDung);
            _fcName.FC_THUA.NAME = thuaName;
            //MessageBox.Show(string.Format("line 55 CalcMattien {0}", thuaName));
            _fcName.FC_THUA.InitIndex();
            IFeatureClass thuaFeatureClass = fw.OpenFeatureClass(thuaName);
            IFeatureLayer thuaFeatureLayer = new FeatureLayerClass();
            thuaFeatureLayer.FeatureClass = thuaFeatureClass;
            IFeatureSelection thuaFeatureSelection;
            #endregion
            #region xa
            IFeatureClass xaFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Ranh_Xa_Poly);
            IFeatureLayer xaFeatureLayer = new FeatureLayerClass();
            xaFeatureLayer.FeatureClass = xaFeatureClass;
            IFeatureSelection xaFeatureSelection;
            #endregion
            #region duong
            IFeatureClass duongFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Duong);
            IFeatureLayer duongFeatureLayer = new FeatureLayerClass();
            duongFeatureLayer.FeatureClass = duongFeatureClass;
            IFeatureSelection duongFeatureSelection;
            //_fcName.FC_DUONG.InitIndex();
            #endregion

            #region hem
            IFeatureClass hemFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Hem);
            IFeatureLayer hemChinhFeatureLayer = new FeatureLayerClass();
            hemChinhFeatureLayer.FeatureClass = hemFeatureClass;
            IFeatureSelection hemChinhFeatureSelection;
            #endregion

            #region thua gia dat
            string tgdDraft = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat_Draft, this._currentConfig.NamApDung);
            _fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            _fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            //_fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            //_fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            IFeatureClass tgdFeatureClass = null;
            try
            {
                tgdFeatureClass = fw.OpenFeatureClass(tgdDraft);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", tgdDraft);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            ITable tblThuaGiaDat = (ITable)tgdFeatureClass;
            IFeatureLayer tgdFeatureLayer = new FeatureLayerClass();
            ISDETableEditor sdeTblTgdEditor = new SDETable(tblThuaGiaDat, sdeConn.Workspace);
            #endregion

            #region gia dat hem
            string gdh = DataNameTemplate.Gia_Hem_Phu + "_" + _currentConfig.NamApDung.ToString();
            _fcName.FC_GIA_DAT_HEM_PHU.NAME = gdh;
            _fcName.FC_GIA_DAT_HEM_PHU.InitIndex();
            IFeatureClass gdhChinhFeatureClass = fw.OpenFeatureClass(gdh);
            IFeatureLayer gdhChinhFeatureLayer = new FeatureLayerClass();
            gdhChinhFeatureLayer.FeatureClass = gdhChinhFeatureClass;
            IFeatureSelection gdhChinhFeatureSelection;

            #endregion

            #region gia dat duong
            string gdd = string.Format("{0}_{1}", DataNameTemplate.Gia_Dat_Duong, this._currentConfig.NamApDung);
            _tblName.GIA_DAT_DUONG.NAME = gdd;
            _tblName.GIA_DAT_DUONG.InitIndex();
            #endregion

            #region ten duong
            ITable tblTenDuong;
            try
            {
                tblTenDuong = fw.OpenTable(DataNameTemplate.Ten_Duong);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Ten_Duong);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region loai dat
            ITable tblLoaiDat;
            try
            {
                tblLoaiDat = fw.OpenTable(DataNameTemplate.Loai_Dat);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Loai_Dat);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region he so vi tri
            ITable tblHesoVitri;
            try
            {
                tblHesoVitri = fw.OpenTable(DataNameTemplate.He_So_K);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.He_So_K);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region khac
            IQueryFilter qrf = new QueryFilterClass();
            bool result = false;
            int sothuatimthay = 0;
            int sothuatinhduoc = 0;
            int sothuaKhongTinhDuoc = 0;
            #endregion

            #endregion
            //************************************

            //*******************************************
            //===========================================
            //===========================================

            #region bat dau tinh

            #region test
            //string ex = string.Format("VongLap(ChonDuong('dorong=10'),VongLap(ChonThua('dientich<1.5'),test([doituonglap]),[doituonglap]))");
            ////ex=string.Format("VongLap(DuongDangChon(),VongLap()
            //Evaluation evalu = new Evaluation(ex);
            //evalu.DuongLayer = duongFeatureLayer;
            //evalu.ThuaLayer = thuaFeatureLayer;
            //evalu.EvaluateCalculating();
            //return;

            #endregion

            //[thaydoi] - cac may tinh khac chi can thay dieu kien truy van he so vi tri
            //******************************************************************
            #region lay cac quy tac tim vi tri

            #region log---
            evt.Log = string.Format("\n----Lấy các quy tắc tìm vị trí thửa từ bảng {0}, ứng với hệ số {1} ...", DataNameTemplate.He_So_K, TnHeSoK.DatOMtTtDt);
            onCalculating(evt);
            #endregion
            //MessageBox.Show(String.Format("line 189 CalcThuaMattien, hsk={0}",TnHeSoK.DatOMtTtDt));
            qrf.WhereClause = string.Format("{0}='{1}'", "hesovitri", TnHeSoK.DatOMtTtDt);
            ICursor cur = tblHesoVitri.Search(qrf, false);
            string quytac = "";
            string cachtinh = "";
            string cachtinhdongia = "";
            try
            {
                IRow row = cur.NextRow();
                if (row != null)
                {

                    quytac = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.QUY_TAC)).ToString();
                    cachtinh = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH)).ToString();
                    cachtinhdongia = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH_DON_GIA)).ToString();
                    //MessageBox.Show(string.Format("line 198 CalcMatTien, quytac={0}", quytac));
                }
            }
            catch { }
            finally
            {
                Marshal.ReleaseComObject(cur);
            }
            #endregion

            #region tinh theo xa
            if (this._inputParams.MA_XA != "-1")
            {
                //[kodoi]
                //=======================
                #region chon xa co ma dang xet
                //neu dang tinh cho 1 xa
                if (_inputParams.MA_XA != "*")
                {
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.MA_XA, _inputParams.MA_XA);
                }
                else//neu dang tinh cho toan huyen
                {
                    qrf.WhereClause = "";
                }
                ISelectionSet xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                xaFeatureSelection.SelectionSet = xaSelectionSet;
                #endregion
                //=======================

                #region vong lap tung xa

                //[thaydoi] - co the them bien chay
                //*************************
                #region khoi dau
                IEnumIDs xaIds = xaSelectionSet.IDs;
                int xaId = 0;
                IFeature xaFt = null;
                List<object> lstMaDuong = new List<object>();
                #endregion
                //*************************

                while ((xaId = xaIds.Next()) != -1)
                {
                    //[kodoi]
                    //=======================
                    #region lay thong tin xa
                    xaFt = xaFeatureClass.GetFeature(xaId);
                    string maxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_XA)).ToString();
                    string tenxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.TEN_XA)).ToString();
                    string loaixa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_LOAI_XA)).ToString();
                    int loaidothi = 0;
                    result = int.TryParse(xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.LOAI_DO_THI)).ToString(), out loaidothi);
                    #endregion

                    if (loaidothi == 0)
                    {
                        continue;
                    }

                    #region -----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho xã/phường: {0}  ******", tenxa);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion

                    #region tim duong trong khu vuc do thi bang qua xa
                    //chon xa dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.OID, xaId);
                    xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                    xaFeatureSelection.SelectionSet = xaSelectionSet;
                    //Chon cac duong co do rong theo quy dinh
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";
                    ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                    duongFeatureSelection.SelectionSet = duongSelectionSet;

                    //bat dau truy van khong gian
                    _qrBl.FromLayer = duongFeatureLayer;
                    _qrBl.ByLayer = xaFeatureLayer;
                    _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                    _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultAnd;
                    _qrBl.UseSelectedFeatures = true;

                    duongSelectionSet = _qrBl.Select();

                    #endregion
                    //=======================

                    #region vong lap xet tung duong

                    //[thaydoi] - co the them bien chay
                    //===============================
                    #region khoi dau
                    IEnumIDs eIds = duongSelectionSet.IDs;
                    int duongId;
                    IFeature ftDuong;
                    int iDuong = 0;
                    int progressingTotalCount = 1;
                    evt.Reset();
                    evt.ProgressingTotal = duongSelectionSet.Count;
                    onCalculating(evt);
                    //List<object> lstMaDuong = new List<object>();
                    #endregion
                    //================================

                    while ((duongId = eIds.Next()) != -1)
                    {
                        //[kodoi]
                        //======================
                        #region log-----
                        evt.Reset();
                        evt.ProgressingTotalCount = progressingTotalCount;
                        onCalculating(evt);
                        progressingTotalCount++;
                        #endregion
                        //======================

                        //[capnhat] - lay ten duong tu bang ten duong
                        //++++++++++++++++++++++++
                        #region lay thong tin cua duong dang xet

                        ftDuong = duongFeatureClass.GetFeature(duongId);
                        string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                        object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                        lstMaDuong.Add(maduong);
                        string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                        string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                        #endregion
                        //++++++++++++++++++++++++

                        //[kodoi]
                        //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                        //====================
                        #region chon duong dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                        duongFeatureSelection.SelectionSet = duongSelectionSet;
                        #endregion
                        //====================

                        #region chon hem gia dat cua duong
                        qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}'",
                                        _fcName.FC_GIA_DAT_HEM_PHU.LOCKED, 0, _fcName.FC_GIA_DAT_HEM_PHU.LOCKED,
                                        _fcName.FC_GIA_DAT_HEM_PHU.MA_DUONG, maduong);
                        ISelectionSet gdhChinhSelectionSet = gdhChinhFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                        gdhChinhFeatureSelection = (IFeatureSelection)gdhChinhFeatureLayer;
                        gdhChinhFeatureSelection.SelectionSet = gdhChinhSelectionSet;

                        #endregion

                        #region xet tung hem co gia
                        IEnumIDs gdhChinhIds = gdhChinhSelectionSet.IDs;
                        //IFeatureCursor hemClipCur = hemClipFc.Search(null, false);
                        int gdhChinhId;
                        IFeature gdhChinhFt;
                        int iHem = 0;
                        //List<object[,]> pairColValTgd = new List<object[,]>();
                        //int rowTgdNnHandleUpdate = 0;
                        //List<object> newId = new List<object>();
                        #endregion
                        while ((gdhChinhId = gdhChinhIds.Next()) != -1)
                        {
                            gdhChinhFt = gdhChinhFeatureClass.GetFeature(gdhChinhId);
                            int hesoHem = 3;
                            result = int.TryParse(gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.HE_SO)).ToString(), out hesoHem);
                            if (!result)
                            {
                                hesoHem = 3;
                            }
                            object giahem = gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.GIA_DAT));
                            #region lay thong tin hem
                            object mahem = gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.MA_HEM));
                            qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, mahem);
                            IFeatureCursor hemCur = hemFeatureClass.Search(qrf, false);
                            IFeature hemFt = null;
                            double dorong = 0;
                            try
                            {
                                if ((hemFt = hemCur.NextFeature()) != null)
                                {
                                    result = double.TryParse(hemFt.get_Value(hemFt.Fields.FindField(_fcName.FC_HEM.DO_RONG)).ToString(), out dorong);
                                    if (!result)
                                    {
                                        dorong = 0;
                                    }
                                }
                            }
                            catch { }
                            finally { Marshal.ReleaseComObject(hemCur); }
                            #endregion

                            #region chon hem dang xet
                            qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_GIA_DAT_HEM_PHU.OID, gdhChinhId);
                            gdhChinhSelectionSet = gdhChinhFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                            gdhChinhFeatureSelection = (IFeatureSelection)gdhChinhFeatureLayer;
                            gdhChinhFeatureSelection.SelectionSet = gdhChinhSelectionSet;
                            #endregion
                            #region chon cac thua tiep giap voi hem dang xet
                            //thuaFeatureSelection.Clear();
                            //IQueryByLayer _qrBl = new QueryByLayerClass();
                            _qrBl.FromLayer = thuaFeatureLayer;
                            _qrBl.ByLayer = gdhChinhFeatureLayer;
                            _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                            _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultNew;
                            _qrBl.UseSelectedFeatures = true;
                            _qrBl.BufferDistance = 1;
                            ISelectionSet thuaSelectionSet;
                            try
                            {
                                thuaSelectionSet = _qrBl.Select();
                                thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                                thuaFeatureSelection.SelectionSet = thuaSelectionSet;
                            }
                            catch
                            { continue; }
                            #endregion
                            #region xet tung thua
                            if (thuaSelectionSet.Count == 0 || thuaSelectionSet == null)
                            {
                                continue;
                            }
                            #region khoi dau
                            IEnumIDs thuaIds = thuaSelectionSet.IDs;
                            int thuaId;
                            IFeature thuaFt;
                            int iThua = 0;
                            List<object[,]> pairColValTgd = new List<object[,]>();
                            int rowTgdNnHandleUpdate = 0;
                            List<object> newId = new List<object>();
                            #endregion
                            while ((thuaId = thuaIds.Next()) != -1)
                            {
                                thuaFt = thuaFeatureClass.GetFeature(thuaId);
                                object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                                object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                                //[kodoi]
                                //============================
                                #region kiem tra maxa
                                if (maxaThua.ToString() == "" || maxaThua == null)
                                {
                                    maxaThua = "0";

                                    #region ----log
                                    evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                                    #endregion

                                    sothuaKhongTinhDuoc++;

                                    #region report progressing thua
                                    if (iThua < thuaSelectionSet.Count)
                                    {
                                        decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                        int i1 = Convert.ToInt32(i);
                                        this._bgwCalculating.ReportProgress(i1);
                                        //MessageBox.Show("log 009");
                                    }
                                    else
                                    {
                                        this._bgwCalculating.ReportProgress(99);
                                    }
                                    iThua++;
                                    #endregion

                                    continue;
                                }
                                #region danh rieng khi tinh theo xa
                                else if (maxaThua.ToString() != maxa)
                                {
                                    #region ----log
                                    evt.Reset();
                                    evt.Log = string.Format("thửa {0} không thuộc xã {1} (maxaThua={2},maxa={3})", mathua, tenxa, maxaThua, maxa);
                                    onCalculating(evt);
                                    #endregion
                                    sothuaKhongTinhDuoc++;
                                    #region report progressing thua
                                    if (iThua < thuaSelectionSet.Count)
                                    {
                                        decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                        int i1 = Convert.ToInt32(i);
                                        this._bgwCalculating.ReportProgress(i1);
                                        //MessageBox.Show("log 009");
                                    }
                                    else
                                    {
                                        this._bgwCalculating.ReportProgress(99);
                                    }
                                    iThua++;
                                    #endregion

                                    continue;
                                }
                                #endregion
                                #endregion
                                object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));

                                string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                                int hesoVitri = CalcTool.GetHeSoViTriThuaHem(dorong, hesoHem, loaidat, true);
                                if (hesoVitri == -1)
                                {
                                    continue;
                                }
                                //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                                //***********************************
                                #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                                evt.Reset();
                                evt.Log = "\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới ...";
                                onCalculating(evt);
                                qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                    _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                                    _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                                ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);
                                IRow tgdRow = null;
                                //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                                try
                                {
                                    tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                                    if (tgdRow != null)
                                    {
                                        //MessageBox.Show("co");
                                        //kiem tra co cho phep tinh lai vi tri
                                        //neu co:xoa feater cu,them feature moi
                                        #region xet thua da co vi tri

                                        bool isOverWritePos = true;

                                        if (!isOverWritePos)
                                        {
                                            newId.Add(tgdRow.OID);
                                            //continue;
                                        }
                                        else
                                        {
                                            //[kodoi]
                                            //===================
                                            #region xoa feature cu
                                            if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                            {
                                                mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                            }
                                            else
                                            {
                                                wspEdit.StartEditing(true);
                                            }
                                            wspEdit.StartEditOperation();
                                            //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                            //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                            tgdRow.Delete();
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);
                                            #endregion
                                            //===================

                                            //[thaydoi] - them gia tri
                                            //**********************
                                            #region them feature moi
                                            if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                            {
                                                mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                            }
                                            else
                                            {
                                                wspEdit.StartEditing(true);
                                            }
                                            wspEdit.StartEditOperation();
                                            object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);

                                            //them gia tri mathua,maduong,hesovitri
                                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                            //pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.DON_GIA), giahem } });
                                            pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                            sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                            rowTgdNnHandleUpdate++;
                                            pairColValTgd.Clear();
                                            newId.Add(copiedId);
                                            #endregion
                                            //**********************
                                        }

                                        #endregion

                                    }
                                    else
                                    {
                                        //MessageBox.Show("ko co");
                                        //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                        //***********************************
                                        #region them feature moi
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                        //pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DON_GIA), giahem } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                        sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        newId.Add(copiedId);
                                        #endregion
                                        //***********************************
                                    }
                                    //MessageBox.Show(newId.Count.ToString());
                                }
                                catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                                finally { Marshal.ReleaseComObject(tgdFcs); }
                                #endregion
                                //***********************************

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion
                            }
                            #endregion

                            //[kodoi]
                            //============================
                            #region luu thong tin vao bang gia dat
                            if (!sdeTblTgdEditor.IsEditing())
                            {
                                sdeTblTgdEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                sdeTblTgdEditor.StartEditOperation();
                            }

                            #region ----log
                            evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgdDraft);
                            onCalculating(evt);
                            #endregion

                            sdeTblTgdEditor.SaveEdit();
                            sdeTblTgdEditor.StopEditOperation();
                            sdeTblTgdEditor.StopEditing(true);

                            #region ----log
                            evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgdDraft);
                            onCalculating(evt);
                            #endregion

                            #endregion

                            #region tinh gia dat cho cac thua vua them vi tri
                            CalcLandprice calc = new CalcLandprice(this);
                            //MessageBox.Show(string.Format("final:{0}", newId.Count));
                            calc.Calculate(newId);
                            newId.Clear();
                            #endregion
                            //============================

                        }
                    }
                    #endregion
                }
                #endregion
            }
            #endregion
            //=====================================================================
            //=====================================================================
            #region tinh theo duong,doan duong
            else
            {
                #region timduong co ten dang xet
                //tinh theo doan duong
                #region tinh theo doan duong
                if (_inputParams.MA_DUONG != "-1")
                {
                    qrf.WhereClause = string.Format("{0}={1} and {2}={3}", _fcName.FC_DUONG.MA_DUONG, _inputParams.MA_DUONG, _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho đoạn đường có mã: {0}  ******", _inputParams.MA_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                }
                #endregion
                //tinh cho 1 duong
                #region tinh cho 1 duong
                else if (_inputParams.TEN_DUONG != "" && _inputParams.TEN_DUONG != "*")
                {
                    //phai sua lai thiet ke
                    //dung relationshipclass many to many gia duong va ten duong
                    #region tim trong bang ten duong
                    qrf.WhereClause = string.Format("{0}=N'{1}'", _tblName.TEN_DUONG.TEN_DUONG, _inputParams.TEN_DUONG);
                    ICursor tenduongCur = tblTenDuong.Search(qrf, false);
                    object idDuong;
                    List<object> lstIdDuong = new List<object>();
                    try
                    {
                        IRow tenduongRow = null;
                        while ((tenduongRow = tenduongCur.NextRow()) != null)
                        {
                            idDuong = tenduongRow.get_Value(tenduongRow.Fields.FindField(_tblName.TEN_DUONG.MA_DUONG));
                            lstIdDuong.Add(idDuong);
                        }
                    }
                    catch
                    { }
                    finally { Marshal.ReleaseComObject(tenduongCur); }
                    #endregion

                    #region tim trong bang duong
                    string q = "";
                    for (int i = 0; i < lstIdDuong.Count; i++)
                    {
                        if (i == lstIdDuong.Count - 1)
                        {
                            q += string.Format("{0}='{1}'", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                        else
                        {
                            q += string.Format("{0}='{1}' or ", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                    }
                    qrf.WhereClause = q;// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho cả đường: {0}  ******", _inputParams.TEN_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                    #endregion
                }
                #endregion
                //tinh cho ca duong
                else
                {
                    qrf.WhereClause = "";
                }
                //MessageBox.Show(string.Format("whereclause:{0}",duong.QueryFilter.WhereClause));
                ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);

                if (duongSelectionSet.Count == 0)
                {
                    return;
                }
                duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                duongFeatureSelection.SelectionSet = duongSelectionSet;

                #endregion

                #region vong lap xet tung duong

                //[thaydoi] - co the them bien chay
                //===============================
                #region khoi dau
                IEnumIDs eIds = duongSelectionSet.IDs;
                int duongId;
                IFeature ftDuong;
                int iDuong = 0;
                int progressingTotalCount = 1;
                evt.Reset();
                evt.ProgressingTotal = duongSelectionSet.Count;
                onCalculating(evt);
                List<object> lstMaDuong = new List<object>();
                #endregion
                //================================

                while ((duongId = eIds.Next()) != -1)
                {
                    //[kodoi]
                    //======================
                    #region log-----
                    evt.Reset();
                    evt.ProgressingTotalCount = progressingTotalCount;
                    onCalculating(evt);
                    progressingTotalCount++;
                    #endregion
                    //======================

                    //[capnhat] - lay ten duong tu bang ten duong
                    //++++++++++++++++++++++++
                    #region lay thong tin cua duong dang xet

                    ftDuong = duongFeatureClass.GetFeature(duongId);
                    string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                    object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                    lstMaDuong.Add(maduong);
                    string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                    string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                    #endregion
                    //++++++++++++++++++++++++

                    //[kodoi]
                    //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                    //====================
                    #region chon duong dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                    duongFeatureSelection.SelectionSet = duongSelectionSet;
                    #endregion
                    //====================

                    #region chon hem gia dat cua duong
                    qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}'",
                                    _fcName.FC_GIA_DAT_HEM_PHU.LOCKED, 0, _fcName.FC_GIA_DAT_HEM_PHU.LOCKED,
                                    _fcName.FC_GIA_DAT_HEM_PHU.MA_DUONG, maduong);
                    ISelectionSet gdhChinhSelectionSet = gdhChinhFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    gdhChinhFeatureSelection = (IFeatureSelection)gdhChinhFeatureLayer;
                    gdhChinhFeatureSelection.SelectionSet = gdhChinhSelectionSet;

                    #endregion

                    #region xet tung hem co gia
                    IEnumIDs gdhChinhIds = gdhChinhSelectionSet.IDs;
                    //IFeatureCursor hemClipCur = hemClipFc.Search(null, false);
                    int gdhChinhId;
                    IFeature gdhChinhFt;
                    int iHem = 0;
                    //List<object[,]> pairColValTgd = new List<object[,]>();
                    //int rowTgdNnHandleUpdate = 0;
                    //List<object> newId = new List<object>();
                    #endregion
                    while ((gdhChinhId = gdhChinhIds.Next()) != -1)
                    {
                        gdhChinhFt = gdhChinhFeatureClass.GetFeature(gdhChinhId);
                        int hesoHem = 3;
                        result = int.TryParse(gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.HE_SO)).ToString(), out hesoHem);
                        if (!result)
                        {
                            hesoHem = 3;
                        }
                        object giahem = gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.GIA_DAT));
                        #region lay thong tin hem
                        object mahem = gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.MA_HEM));
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, mahem);
                        IFeatureCursor hemCur = hemFeatureClass.Search(qrf, false);
                        IFeature hemFt = null;
                        double dorong = 0;
                        try
                        {
                            if ((hemFt = hemCur.NextFeature()) != null)
                            {
                                result = double.TryParse(hemFt.get_Value(hemFt.Fields.FindField(_fcName.FC_HEM.DO_RONG)).ToString(), out dorong);
                                if (!result)
                                {
                                    dorong = 0;
                                }
                            }
                        }
                        catch { }
                        finally { Marshal.ReleaseComObject(hemCur); }
                        #endregion

                        #region chon hem dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_GIA_DAT_HEM_PHU.OID, gdhChinhId);
                        gdhChinhSelectionSet = gdhChinhFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                        gdhChinhFeatureSelection = (IFeatureSelection)gdhChinhFeatureLayer;
                        gdhChinhFeatureSelection.SelectionSet = gdhChinhSelectionSet;
                        #endregion
                        #region chon cac thua tiep giap voi hem dang xet
                        //thuaFeatureSelection.Clear();
                        //IQueryByLayer _qrBl = new QueryByLayerClass();
                        _qrBl.FromLayer = thuaFeatureLayer;
                        _qrBl.ByLayer = gdhChinhFeatureLayer;
                        _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                        _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultNew;
                        _qrBl.UseSelectedFeatures = true;
                        _qrBl.BufferDistance = 1;
                        ISelectionSet thuaSelectionSet;
                        try
                        {
                            thuaSelectionSet = _qrBl.Select();
                            thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                            thuaFeatureSelection.SelectionSet = thuaSelectionSet;
                        }
                        catch
                        { continue; }
                        #endregion
                        #region xet tung thua
                        if (thuaSelectionSet.Count == 0 || thuaSelectionSet == null)
                        {
                            continue;
                        }
                        #region khoi dau
                        IEnumIDs thuaIds = thuaSelectionSet.IDs;
                        int thuaId;
                        IFeature thuaFt;
                        int iThua = 0;
                        List<object[,]> pairColValTgd = new List<object[,]>();
                        int rowTgdNnHandleUpdate = 0;
                        List<object> newId = new List<object>();
                        #endregion
                        while ((thuaId = thuaIds.Next()) != -1)
                        {
                            thuaFt = thuaFeatureClass.GetFeature(thuaId);
                            object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                            object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                            //[kodoi]
                            //============================
                            #region kiem tra maxa
                            if (maxaThua.ToString() == "" || maxaThua == null)
                            {
                                maxaThua = "0";

                                #region ----log
                                evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                                #endregion

                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #endregion
                            object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));

                            string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                            int hesoVitri = CalcTool.GetHeSoViTriThuaHem(dorong, hesoHem, loaidat,true);
                            if (hesoVitri == -1)
                            {
                                continue;
                            }
                            //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                            //***********************************
                            #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                            evt.Reset();
                            evt.Log = "\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới ...";
                            onCalculating(evt);
                            qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                                _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                            ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);
                            IRow tgdRow = null;
                            //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                            try
                            {
                                tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                                if (tgdRow != null)
                                {
                                    //MessageBox.Show("co");
                                    //kiem tra co cho phep tinh lai vi tri
                                    //neu co:xoa feater cu,them feature moi
                                    #region xet thua da co vi tri

                                    bool isOverWritePos = true;

                                    if (!isOverWritePos)
                                    {
                                        newId.Add(tgdRow.OID);
                                        //continue;
                                    }
                                    else
                                    {
                                        //[kodoi]
                                        //===================
                                        #region xoa feature cu
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                        //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                        tgdRow.Delete();
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                        #endregion
                                        //===================

                                        //[thaydoi] - them gia tri
                                        //**********************
                                        #region them feature moi
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                        //pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.DON_GIA), giahem } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                        sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        newId.Add(copiedId);
                                        #endregion
                                        //**********************
                                    }

                                    #endregion

                                }
                                else
                                {
                                    //MessageBox.Show("ko co");
                                    //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                    //***********************************
                                    #region them feature moi
                                    if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                    {
                                        mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                    }
                                    else
                                    {
                                        wspEdit.StartEditing(true);
                                    }
                                    wspEdit.StartEditOperation();
                                    object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);

                                    //them gia tri mathua,maduong,hesovitri
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                    //pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DON_GIA), giahem } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                    sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    newId.Add(copiedId);
                                    #endregion
                                    //***********************************
                                }
                                //MessageBox.Show(newId.Count.ToString());
                            }
                            catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                            finally { Marshal.ReleaseComObject(tgdFcs); }
                            #endregion
                            //***********************************

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion
                        }
                        #endregion

                        //[kodoi]
                        //============================
                        #region luu thong tin vao bang gia dat
                        if (!sdeTblTgdEditor.IsEditing())
                        {
                            sdeTblTgdEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            sdeTblTgdEditor.StartEditOperation();
                        }

                        #region ----log
                        evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgdDraft);
                        onCalculating(evt);
                        #endregion

                        sdeTblTgdEditor.SaveEdit();
                        sdeTblTgdEditor.StopEditOperation();
                        sdeTblTgdEditor.StopEditing(true);

                        #region ----log
                        evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgdDraft);
                        onCalculating(evt);
                        #endregion

                        #endregion

                        #region tinh gia dat cho cac thua vua them vi tri
                        CalcLandprice calc = new CalcLandprice(this);
                        //MessageBox.Show(string.Format("final:{0}", newId.Count));
                        calc.Calculate(newId);
                        newId.Clear();
                        #endregion
                        //============================

                    }
                }
                #endregion
            }
            #endregion

            #endregion

            //===========================================
            //===========================================
            //*******************************************

            //[kodoi]
            //==============
            #region doan ket
            sothuatinhduoc = sothuatimthay - sothuaKhongTinhDuoc;

            #region ----log
            evt.Log = string.Format("\n\n**************************************\n******* Đã tính xong các thửa phi nông nghiệp tại đô thị vị trí mặt tiền*******\n**********************************");
            //evt.Log += string.Format("\n Số thửa tìm thấy: {0}", sothuatimthay);
            //evt.Log += string.Format("\n Số thửa được tính: {0}", sothuatinhduoc);
            onCalculating(evt);
            #endregion
            evt.Reset();
            evt.ProgressingTotalCount = ".";
            evt.ProgressingTotal = ".";
            onCalculating(evt);
            evt.CurrentIndexCalculator = this._index;
            onFinished(evt);
            #endregion
            //==============
        }
        protected override void calculate()
        {
            #region khoi dau
            base.calculate();
            CalculationEventArg evt = new CalculationEventArg();
            //evt.Type = EnumTypeOfLoopCalculation.InListCalculators;
            evt.CurrentIndexCalculator = this._index;
            evt.Log = string.Format("**********  Bắt đầu tính giá cho hẻm {0} ********", vitri);
            onCalculating(evt);
            #endregion

            //[thaydoi] - them cac khai bao can thiet
            //************************************
            #region khai bao cac bien
            //Lay connection info hien tai
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
            IMultiuserWorkspaceEdit mwspEdit = (IMultiuserWorkspaceEdit)sdeConn.Workspace;
            this._fcName = new TnFeatureClassName(sdeConn.Workspace);
            this._tblName = new TnTableName(sdeConn.Workspace);
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ICopyFeatures copyTool = new DataManager(sdeConn.Workspace, sdeConn.Environment);

            #region thua

            //string thuaName = string.Format("{0}_{1}", DataNameTemplate.Thua, this._currentConfig.NamApDung);
            //_fcName.FC_THUA.NAME = thuaName;
            ////MessageBox.Show(string.Format("line 55 CalcMattien {0}", thuaName));
            //_fcName.FC_THUA.InitIndex();
            //IFeatureClass thuaFeatureClass = fw.OpenFeatureClass(thuaName);
            //IFeatureLayer thuaFeatureLayer = new FeatureLayerClass();
            //thuaFeatureLayer.FeatureClass = thuaFeatureClass;
            //IFeatureSelection thuaFeatureSelection;
            #endregion
            #region xa
            IFeatureClass xaFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Ranh_Xa_Poly);
            IFeatureLayer xaFeatureLayer = new FeatureLayerClass();
            xaFeatureLayer.FeatureClass = xaFeatureClass;
            IFeatureSelection xaFeatureSelection;
            #endregion
            #region duong
            IFeatureClass duongFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Duong);
            IFeatureLayer duongFeatureLayer = new FeatureLayerClass();
            duongFeatureLayer.FeatureClass = duongFeatureClass;
            IFeatureSelection duongFeatureSelection;
            //_fcName.FC_DUONG.InitIndex();
            #endregion

            #region hem
            IFeatureClass hemFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Hem);
            IFeatureLayer hemChinhFeatureLayer = new FeatureLayerClass();
            hemChinhFeatureLayer.FeatureClass = hemFeatureClass;
            IFeatureSelection hemChinhFeatureSelection;

            //IFeatureClass hemPhuFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Hem);
            IFeatureLayer hemPhuFeatureLayer = new FeatureLayerClass();
            hemPhuFeatureLayer.FeatureClass = hemFeatureClass;
            IFeatureSelection hemPhuFeatureSelection;
            //_fcName.FC_DUONG.InitIndex();
            #endregion
            #region gia dat hem
            string gdh = DataNameTemplate.Gia_Hem + "_" + _currentConfig.NamApDung.ToString();
            _fcName.FC_GIA_DAT_HEM.NAME = gdh;
            _fcName.FC_GIA_DAT_HEM.InitIndex();
            IFeatureClass gdhChinhFeatureClass = fw.OpenFeatureClass(gdh);
            IFeatureLayer gdhChinhFeatureLayer = new FeatureLayerClass();
            gdhChinhFeatureLayer.FeatureClass = gdhChinhFeatureClass;
            IFeatureSelection gdhChinhFeatureSelection;
            ITable tblGdhChinh = (ITable)gdhChinhFeatureClass;
            ISDETableEditor sdeTblGdhEditor = new SDETable(tblGdhChinh, sdeConn.Workspace);
            #endregion

            #region thua gia dat
            //string tgdDraft = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat_Draft, this._currentConfig.NamApDung);
            //_fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            //_fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            ////_fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            ////_fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            //IFeatureClass tgdFeatureClass=null;
            //try
            //{
            //    tgdFeatureClass = fw.OpenFeatureClass(tgdDraft);
            //}
            //catch (Exception exc)
            //{
            //    evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", tgdDraft);
            //    onCalculating(evt);
            //    onFinished(evt);
            //    return;
            //}
            //ITable tblThuaGiaDat = (ITable)tgdFeatureClass;
            //IFeatureLayer tgdFeatureLayer = new FeatureLayerClass();
            //ISDETableEditor sdeTblTgdEditor = new SDETable(tblThuaGiaDat, sdeConn.Workspace);
            #endregion

            #region gia dat duong
            //string gdd = string.Format("{0}_{1}", DataNameTemplate.Gia_Dat_Duong, this._currentConfig.NamApDung);
            //_tblName.GIA_DAT_DUONG.NAME = gdd;
            //_tblName.GIA_DAT_DUONG.InitIndex();
            #endregion

            #region ten duong
            ITable tblTenDuong;
            try
            {
                tblTenDuong = fw.OpenTable(DataNameTemplate.Ten_Duong);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Ten_Duong);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region loai dat
            //ITable tblLoaiDat;
            //try
            //{
            //    tblLoaiDat = fw.OpenTable(DataNameTemplate.Loai_Dat);
            //}
            //catch (Exception exc)
            //{
            //    evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Loai_Dat);
            //    onCalculating(evt);
            //    onFinished(evt);
            //    return;
            //}
            #endregion

            #region he so vi tri
            //ITable tblHesoVitri;
            //try
            //{
            //    tblHesoVitri = fw.OpenTable(DataNameTemplate.He_So_K);
            //}
            //catch (Exception exc)
            //{
            //    evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.He_So_K);
            //    onCalculating(evt);
            //    onFinished(evt);
            //    return;
            //}
            #endregion

            #region khac
            IQueryFilter qrf = new QueryFilterClass();
            IDataManager _dataManager;
            ISdeVersionInfo _version;
            IBuffer _bufferTool;
            //IClip _clipTool;
            IErase _eraseTool;
            ITnSystemTempPath _sysTempPath;
            _sysTempPath = new TnSystemTempPath();
            bool result = false;
            int sothuatimthay = 0;
            int sothuatinhduoc = 0;
            int sothuaKhongTinhDuoc = 0;
            _dataManager = new DataManager(sdeConn.Workspace, sdeConn.Environment);
            _version = SdeVersionsTool.CallMe();
            //_clipTool = new GExtractTool(sdeConn.Environment);
            _eraseTool = new GExtractTool(sdeConn.Environment);
            string duongBuffer200mNoSde = FilterSdeLayerName.GetActualName(DataNameTemplate.Duong_Buffer_);
            duongBuffer200mNoSde += "200";
            string hemErase200mNoSde = FilterSdeLayerName.GetActualName(DataNameTemplate.Hem_Erase_);
            hemErase200mNoSde += "200";
            //string duongBuffer50mCoSde = string.Format("{0}{1}", DataNameTemplate.Duong_Buffer_, 50);
            string hemErase200mCoSde = string.Format("{0}{1}", DataNameTemplate.Hem_Erase_, 200);
            #endregion

            #endregion
            //************************************

            //*******************************************
            //===========================================
            //===========================================

            #region bat dau tinh
            _bufferTool = new GProximityTool(sdeConn.Environment);

            if (!_dataManager.LayerExist(duongBuffer200mNoSde))
            {

                _bufferTool.BufferInsideSde(duongFeatureClass.AliasName, duongBuffer200mNoSde, 200);
            }
            IFeatureClass duongBuff200FeatureClass = fw.OpenFeatureClass(duongBuffer200mNoSde);
            IFeatureLayer duongBuff200FeatureLayer = new FeatureLayerClass();
            ISelectionSet duongBuff200Sls;
            IFeatureSelection duongBuff200Fsls;
            duongBuff200FeatureLayer.FeatureClass = duongBuff200FeatureClass;
            #region test
            //string ex = string.Format("VongLap(ChonDuong('dorong=10'),VongLap(ChonThua('dientich<1.5'),test([doituonglap]),[doituonglap]))");
            ////ex=string.Format("VongLap(DuongDangChon(),VongLap()
            //Evaluation evalu = new Evaluation(ex);
            //evalu.DuongLayer = duongFeatureLayer;
            //evalu.ThuaLayer = thuaFeatureLayer;
            //evalu.EvaluateCalculating();
            //return;

            #endregion

            //[thaydoi] - cac may tinh khac chi can thay dieu kien truy van he so vi tri
            //******************************************************************
            #region lay cac quy tac tim vi tri

            //#region log---
            //evt.Log = string.Format("\n----Lấy các quy tắc tìm vị trí thửa từ bảng {0}, ứng với hệ số {1} ...", DataNameTemplate.He_So_K, TnHeSoK.DatOMtTtDt);
            //onCalculating(evt);
            //#endregion
            ////MessageBox.Show(String.Format("line 189 CalcThuaMattien, hsk={0}",TnHeSoK.DatOMtTtDt));
            //qrf.WhereClause = string.Format("{0}='{1}'", "hesovitri", TnHeSoK.DatOMtTtDt);
            //ICursor cur = tblHesoVitri.Search(qrf, false);
            //string quytac = "";
            //string cachtinh = "";
            //string cachtinhdongia = "";
            //try
            //{
            //    IRow row = cur.NextRow();
            //    if (row != null)
            //    {

            //        quytac = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.QUY_TAC)).ToString();
            //        cachtinh = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH)).ToString();
            //        cachtinhdongia = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH_DON_GIA)).ToString();
            //        //MessageBox.Show(string.Format("line 198 CalcMatTien, quytac={0}", quytac));
            //    }
            //}
            //catch { }
            //finally
            //{
            //    Marshal.ReleaseComObject(cur);
            //}
            #endregion

            #region tinh theo xa
            if (this._inputParams.MA_XA != "-1")
            {
                //[kodoi]
                //=======================
                #region chon xa co ma dang xet
                //neu dang tinh cho 1 xa
                if (_inputParams.MA_XA != "*")
                {
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.MA_XA, _inputParams.MA_XA);
                }
                else//neu dang tinh cho toan huyen
                {
                    qrf.WhereClause = "";
                }
                ISelectionSet xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                xaFeatureSelection.SelectionSet = xaSelectionSet;
                #endregion
                //=======================

                #region vong lap tung xa

                //[thaydoi] - co the them bien chay
                //*************************
                #region khoi dau
                IEnumIDs xaIds = xaSelectionSet.IDs;
                int xaId = 0;
                IFeature xaFt = null;
                List<object> lstMaDuong = new List<object>();
                #endregion
                //*************************

                while ((xaId = xaIds.Next()) != -1)
                {
                    //[kodoi]
                    //=======================
                    #region lay thong tin xa
                    xaFt = xaFeatureClass.GetFeature(xaId);
                    string maxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_XA)).ToString();
                    string tenxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.TEN_XA)).ToString();
                    string loaixa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_LOAI_XA)).ToString();
                    #endregion

                    #region -----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho xã/phường: {0}  ******", tenxa);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion

                    #region tim duong trong khu vuc do thi bang qua xa

                    //chon xa dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.OID, xaId);
                    xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                    xaFeatureSelection.SelectionSet = xaSelectionSet;
                    //Chon cac duong co do rong theo quy dinh
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";
                    ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                    duongFeatureSelection.SelectionSet = duongSelectionSet;

                    //bat dau truy van khong gian
                    _qrBl.FromLayer = duongFeatureLayer;
                    _qrBl.ByLayer = xaFeatureLayer;
                    _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                    _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultAnd;
                    _qrBl.UseSelectedFeatures = true;

                    duongSelectionSet = _qrBl.Select();

                    #endregion
                    //=======================

                    #region vong lap xet tung duong

                    //[thaydoi] - co the them bien chay
                    //===============================
                    #region khoi dau
                    IEnumIDs eIds = duongSelectionSet.IDs;
                    int duongId;
                    IFeature ftDuong;
                    int iDuong = 0;
                    int progressingTotalCount = 1;
                    evt.Reset();
                    evt.ProgressingTotal = duongSelectionSet.Count;
                    onCalculating(evt);
                    //List<object> lstMaDuong = new List<object>();
                    #endregion
                    //================================

                    while ((duongId = eIds.Next()) != -1)
                    {
                        //[kodoi]
                        //======================
                        #region log-----
                        evt.Reset();
                        evt.ProgressingTotalCount = progressingTotalCount;
                        onCalculating(evt);
                        progressingTotalCount++;
                        #endregion
                        //======================

                        //[capnhat] - lay ten duong tu bang ten duong
                        //++++++++++++++++++++++++
                        #region lay thong tin cua duong dang xet

                        ftDuong = duongFeatureClass.GetFeature(duongId);
                        string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                        object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                        lstMaDuong.Add(maduong);
                        string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                        string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                        #endregion
                        //++++++++++++++++++++++++

                        //[kodoi]
                        //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                        //====================
                        #region chon duong dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                        duongFeatureSelection.SelectionSet = duongSelectionSet;
                        #endregion
                        //====================

                        //tim cac thua theo dieu kien mat tien

                        //doc cach tim thua trong bang he so vi tri
                        //lay cach tim cho dat o
                        //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                        //MessageBox.Show(func);
                        //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                        //[kodoi]
                        //============================

                        #region chon hem chinh cua duong
                        qrf.WhereClause = string.Format("{0}='{1}' and ({2}='{3}' or {4} is null)", _fcName.FC_HEM.MA_DUONG, maduong, _fcName.FC_HEM.HEM_CHINH, 0, _fcName.FC_HEM.HEM_CHINH);
                        ISelectionSet hemChinhSelectionSet = hemFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                        hemChinhFeatureSelection = (IFeatureSelection)hemChinhFeatureLayer;
                        hemChinhFeatureSelection.SelectionSet = hemChinhSelectionSet;

                        #endregion

                        #region clip cac hem vua tim duoc

                        #region delete bang hem_sau100m_clip
                        IFeatureClass hemClipFc = null;
                        #region log---
                        evt.Log = string.Format("\n----Kiểm tra, xóa bảng {0} ...", hemErase200mCoSde);
                        onCalculating(evt);
                        #endregion

                        //if (_dataManager.LayerExist(hemErase200mNoSde))
                        //{
                        //thuaClipFc = fw.OpenFeatureClass("sde.thua_sau50m_clip");
                        //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                        //wspEdit.StartEditing(false);
                        //wspEdit.StartEditOperation();
                        try
                        {
                            ((IFeatureClassManager)_dataManager).DeleteFcInSde(hemErase200mCoSde);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("line 924 CalGiaHemTren200,ex=" + ex.ToString());
                        }
                        //((IDataset)thuaClipFc).Delete();
                        //wspEdit.StopEditOperation();
                        //wspEdit.StopEditing(true);
                        //}
                        #endregion

                        #region chon duong buffer 100 dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongBuff200Sls = duongBuff200FeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongBuff200Fsls = (IFeatureSelection)duongBuff200FeatureLayer;
                        duongBuff200Fsls.SelectionSet = duongBuff200Sls;
                        #endregion

                        #region chon hem nam trong vung buffer 200m nhưng khong nam trong vùng buffer 100m
                        _qrBl.FromLayer = hemChinhFeatureLayer;
                        _qrBl.ByLayer = duongFeatureLayer;
                        _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectContainedBy;
                        _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultSubtract;
                        _qrBl.UseSelectedFeatures = true;
                        _qrBl.BufferDistance = 200;
                        try
                        {
                            hemChinhSelectionSet = _qrBl.Select();
                            hemChinhFeatureSelection = (IFeatureSelection)hemChinhFeatureLayer;
                            hemChinhFeatureSelection.SelectionSet = hemChinhSelectionSet;
                        }
                        catch
                        { continue; }

                        if (!(hemChinhSelectionSet.Count > 0))
                        {
                            continue;
                        }
                        #endregion

                        #region log---
                        evt.Log = string.Format("\n----Kiểm tra, clip hẻm theo đường buffer {0}m, lưu vào bảng {1} ...", _currentConfig.DKhoangCach50mMatTien, hemErase200mCoSde);
                        onCalculating(evt);
                        #endregion

                        string hemlyr = string.Format("{0}/{1}", _sysTempPath.TempPath, hemErase200mNoSde);
                        string duonglyr = string.Format("{0}/{1}", _sysTempPath.TempPath, duongBuffer200mNoSde);
                        _eraseTool.EraseByLayerFileInsideSde(hemChinhFeatureLayer, hemlyr, duongBuff200FeatureLayer, duonglyr, hemErase200mNoSde);
                        try
                        {
                            hemClipFc = fw.OpenFeatureClass(hemErase200mCoSde);
                        }
                        catch (Exception ex) { continue; }
                        _version.RegisterDataset((IDataset)hemClipFc, true, true);
                        #endregion

                        #region xet tung hem da clip

                        //[thaydoi] - co the them bien chay
                        //********************
                        #region khoi dau
                        //IFeatureCursor hemErCur = hemClipFc.Search(null, false);

                        IEnumIDs hemChinhIds = hemChinhSelectionSet.IDs;
                        int hemChinhId;
                        IFeature hemChinhFt;
                        int iHem = 0;
                        List<object[,]> pairColValTgd = new List<object[,]>();
                        int rowTgdNnHandleUpdate = 0;
                        List<object> newId = new List<object>();
                        #endregion
                        try
                        {
                            while ((hemChinhId = hemChinhIds.Next()) != -1)
                            {
                                //he so vi tri hem ung voi doan sau <100m
                                int hesoVitri = 3;
                                #region lay thong tin hem dang xet

                                //[kodoi]
                                //============================
                                #region lay thong tin co ban
                                hemChinhFt = hemFeatureClass.GetFeature(hemChinhId);
                                //hemErId = hemErFt.OID;
                                object maHem = hemChinhFt.get_Value(hemChinhFt.Fields.FindField(_fcName.FC_HEM.MA_HEM));

                                #endregion

                                #endregion

                                #region kiem tra trong bang giadat_hem, voi dieu kien:mahem,hesovitri,khoagia=0
                                evt.Reset();
                                evt.Log = "\n[!]--- Kiểm tra các hẻm đã có giá, cập nhật giá mới ...";
                                onCalculating(evt);
                                qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}'",
                                    _fcName.FC_GIA_DAT_HEM.LOCKED, 0, _fcName.FC_GIA_DAT_HEM.LOCKED,
                                    _fcName.FC_GIA_DAT_HEM.MA_HEM, maHem,
                                    _fcName.FC_GIA_DAT_HEM.HE_SO, hesoVitri);
                                IFeatureCursor gdhChinhFcs = gdhChinhFeatureClass.Search(qrf, false);
                                IFeature gdhChinhFt = null;

                                try
                                {
                                    gdhChinhFt = gdhChinhFcs.NextFeature();//dam bao la chi co 1 hang ket qua
                                    if (gdhChinhFt != null)
                                    {
                                        //MessageBox.Show("co");
                                        //kiem tra co cho phep tinh lai vi tri
                                        //neu co:xoa feater cu,them feature moi
                                        #region xet thua da co vi tri

                                        bool isOverWritePos = true;

                                        if (!isOverWritePos)
                                        {
                                            newId.Add(gdhChinhFt.OID);
                                            //continue;
                                        }
                                        else
                                        {
                                            //[kodoi]
                                            //===================
                                            #region xoa feature cu
                                            if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                            {
                                                mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                            }
                                            else
                                            {
                                                wspEdit.StartEditing(true);
                                            }
                                            wspEdit.StartEditOperation();
                                            //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                            //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                            gdhChinhFt.Delete();
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);
                                            #endregion
                                            //===================
                                            #region chon trong bang thua clip
                                            qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, maHem);
                                            IFeatureCursor hemClipFcur = hemClipFc.Search(qrf, false);
                                            IFeature hemErFt = null;
                                            try
                                            {
                                                hemErFt = hemClipFcur.NextFeature();
                                                if (hemErFt == null)
                                                {
                                                    continue;
                                                }
                                            }
                                            catch (Exception ex) { return; }
                                            finally { Marshal.ReleaseComObject(hemClipFcur); }

                                            #region lay thong tin thua clip
                                            //IArea area = (IArea)hemClipFt.Shape;
                                            #endregion

                                            #endregion
                                            //[thaydoi] - them gia tri
                                            //**********************
                                            #region them feature moi
                                            if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                            {
                                                mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                            }
                                            else
                                            {
                                                wspEdit.StartEditing(true);
                                            }
                                            wspEdit.StartEditOperation();
                                            object copiedId = copyTool.Copy(hemErFt, gdhChinhFeatureClass);
                                            try
                                            {
                                                wspEdit.StopEditOperation();
                                                wspEdit.StopEditing(true);
                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show("line 1292 CalcGiaHemChinh ex=" + ex.ToString());
                                                wspEdit.StopEditOperation();
                                                wspEdit.StopEditing(false);
                                            }

                                            //them gia tri mathua,maduong,hesovitri
                                            pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.MA_HEM), maHem } });
                                            pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.MA_DUONG), maduong } });
                                            pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.LOCKED), 0 } });
                                            pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.HE_SO), hesoVitri } });
                                            //MessageBox.Show(string.Format("line 1062 CalcGiaHemChinh mahem={0},maduong={1}", maHem, maduong));
                                            sdeTblGdhEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                            rowTgdNnHandleUpdate++;
                                            pairColValTgd.Clear();
                                            //MessageBox.Show("line 1066 CalcGiaHemChinh copyid=" + copiedId);
                                            newId.Add(copiedId);
                                            #endregion
                                            //**********************
                                        }

                                        #endregion

                                    }
                                    else
                                    {
                                        //MessageBox.Show("ko co");
                                        //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                        //***********************************
                                        #region chon trong bang thua clip
                                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, maHem);
                                        //MessageBox.Show("line 1322 CalcGiaHemChinh, mahem=" + maHem);
                                        IFeatureCursor hemClipFcur = hemClipFc.Search(qrf, false);
                                        IFeature hemErFt = null;
                                        try
                                        {
                                            hemErFt = hemClipFcur.NextFeature();
                                            if (hemErFt == null)
                                            {
                                                continue;
                                            }
                                        }
                                        catch (Exception ex) { return; }
                                        finally { Marshal.ReleaseComObject(hemClipFcur); }

                                        #region lay thong tin thua clip
                                        //IArea area = (IArea)hemClipFt.Shape;
                                        #endregion

                                        #endregion
                                        #region them feature moi
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(hemErFt, gdhChinhFeatureClass);
                                        try
                                        {
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show("line 1355 CalcGiaHemChinh ex=" + ex.ToString());
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(false);
                                        }

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.MA_HEM), maHem } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.HE_SO), hesoVitri } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.LOCKED), 0 } });
                                        //MessageBox.Show(string.Format("line 1125 CalcGiaHemChinh mahem={0},maduong={1}",maHem,maduong));
                                        sdeTblGdhEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        //MessageBox.Show("line 1129 CalcGiaHemChinh copyid=" + copiedId);
                                        newId.Add(copiedId);

                                        #endregion
                                        //***********************************
                                    }
                                    //MessageBox.Show(newId.Count.ToString());
                                }
                                catch (Exception e1)
                                {
                                    MessageBox.Show(string.Format("CalcGiaHemChinh, line 1448-\n{0}", e1));
                                }
                                finally { Marshal.ReleaseComObject(gdhChinhFcs); }

                                #endregion
                            }
                        }
                        catch (Exception ex) { }
                        //finally { Marshal.ReleaseComObject(hemErCur); }

                        #endregion

                        //[kodoi]
                        //============================
                        #region luu thong tin vao bang gia dat
                        if (!sdeTblGdhEditor.IsEditing())
                        {
                            sdeTblGdhEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            sdeTblGdhEditor.StartEditOperation();
                        }

                        #region ----log
                        evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", gdhChinhFeatureClass.AliasName);
                        onCalculating(evt);
                        #endregion
                        try
                        {
                            sdeTblGdhEditor.SaveEdit();
                            sdeTblGdhEditor.StopEditOperation();
                            sdeTblGdhEditor.StopEditing(true);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("line 1405 CalcGiaHemChinh ex=" + ex.ToString());
                            sdeTblGdhEditor.StopEditOperation();
                            sdeTblGdhEditor.StopEditing(false);
                        }

                        #region ----log
                        evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", gdhChinhFeatureClass.AliasName);
                        onCalculating(evt);
                        #endregion

                        #endregion

                        #region tinh gia dat cho cac hem vua them vi tri
                        CalcLandprice calc = new CalcLandprice(this);
                        //MessageBox.Show(string.Format("final:{0}", newId.Count));

                        calc.CalcGiaHem(newId);
                        newId.Clear();
                        #endregion
                        //============================

                    }
                    #endregion

                }
                #endregion
            }
            #endregion
            //=====================================================================
            //=====================================================================
            #region tinh theo duong,doan duong

            else
            {
                #region timduong co ten dang xet
                //tinh theo doan duong
                #region tinh theo doan duong
                if (_inputParams.MA_DUONG != "-1")
                {
                    qrf.WhereClause = string.Format("{0}={1} and {2}={3}", _fcName.FC_DUONG.MA_DUONG, _inputParams.MA_DUONG, _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho đoạn đường có mã: {0}  ******", _inputParams.MA_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                }
                #endregion
                //tinh cho 1 duong
                #region tinh cho 1 duong
                else if (_inputParams.TEN_DUONG != "" && _inputParams.TEN_DUONG != "*")
                {
                    //phai sua lai thiet ke
                    //dung relationshipclass many to many gia duong va ten duong
                    #region tim trong bang ten duong
                    qrf.WhereClause = string.Format("{0}=N'{1}'", _tblName.TEN_DUONG.TEN_DUONG, _inputParams.TEN_DUONG);
                    ICursor tenduongCur = tblTenDuong.Search(qrf, false);
                    object idDuong;
                    List<object> lstIdDuong = new List<object>();
                    try
                    {
                        IRow tenduongRow = null;
                        while ((tenduongRow = tenduongCur.NextRow()) != null)
                        {
                            idDuong = tenduongRow.get_Value(tenduongRow.Fields.FindField(_tblName.TEN_DUONG.MA_DUONG));
                            lstIdDuong.Add(idDuong);
                        }
                    }
                    catch
                    { }
                    finally { Marshal.ReleaseComObject(tenduongCur); }
                    #endregion

                    #region tim trong bang duong
                    string q = "";
                    for (int i = 0; i < lstIdDuong.Count; i++)
                    {
                        if (i == lstIdDuong.Count - 1)
                        {
                            q += string.Format("{0}='{1}'", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                        else
                        {
                            q += string.Format("{0}='{1}' or ", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                    }
                    qrf.WhereClause = q;// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho cả đường: {0}  ******", _inputParams.TEN_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                    #endregion
                }
                #endregion
                //tinh cho ca duong
                else
                {
                    qrf.WhereClause = "";
                }
                //MessageBox.Show(string.Format("whereclause:{0}",duong.QueryFilter.WhereClause));
                ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);

                if (duongSelectionSet.Count == 0)
                {
                    return;
                }
                duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                duongFeatureSelection.SelectionSet = duongSelectionSet;

                #endregion

                #region vong lap xet tung duong

                //[thaydoi] - co the them bien chay
                //===============================
                #region khoi dau
                IEnumIDs eIds = duongSelectionSet.IDs;
                int duongId;
                IFeature ftDuong;
                int iDuong = 0;
                int progressingTotalCount = 1;
                evt.Reset();
                evt.ProgressingTotal = duongSelectionSet.Count;
                onCalculating(evt);
                List<object> lstMaDuong = new List<object>();
                #endregion
                //================================

                while ((duongId = eIds.Next()) != -1)
                {
                    //[kodoi]
                    //======================
                    #region log-----
                    evt.Reset();
                    evt.ProgressingTotalCount = progressingTotalCount;
                    onCalculating(evt);
                    progressingTotalCount++;
                    #endregion
                    //======================

                    //[capnhat] - lay ten duong tu bang ten duong
                    //++++++++++++++++++++++++
                    #region lay thong tin cua duong dang xet

                    ftDuong = duongFeatureClass.GetFeature(duongId);
                    string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                    object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                    lstMaDuong.Add(maduong);
                    string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                    string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                    #endregion
                    //++++++++++++++++++++++++

                    //[kodoi]
                    //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                    //====================
                    #region chon duong dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                    duongFeatureSelection.SelectionSet = duongSelectionSet;
                    #endregion
                    //====================

                    //tim cac thua theo dieu kien mat tien

                    //doc cach tim thua trong bang he so vi tri
                    //lay cach tim cho dat o
                    //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                    //MessageBox.Show(func);
                    //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                    //[kodoi]
                    //============================

                    #region chon hem chinh cua duong
                    qrf.WhereClause = string.Format("{0}='{1}' and ({2}='{3}' or {4} is null)", _fcName.FC_HEM.MA_DUONG, maduong, _fcName.FC_HEM.HEM_CHINH, 0, _fcName.FC_HEM.HEM_CHINH);
                    ISelectionSet hemChinhSelectionSet = hemFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    hemChinhFeatureSelection = (IFeatureSelection)hemChinhFeatureLayer;
                    hemChinhFeatureSelection.SelectionSet = hemChinhSelectionSet;

                    #endregion

                    #region clip cac hem vua tim duoc

                    #region delete bang hem_sau100m_clip
                    IFeatureClass hemClipFc = null;
                    #region log---
                    evt.Log = string.Format("\n----Kiểm tra, xóa bảng {0} ...", hemErase200mCoSde);
                    onCalculating(evt);
                    #endregion

                    //if (_dataManager.LayerExist(hemErase200mNoSde))
                    //{
                        //thuaClipFc = fw.OpenFeatureClass("sde.thua_sau50m_clip");
                        //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                        //wspEdit.StartEditing(false);
                        //wspEdit.StartEditOperation();
                        try
                        {
                            ((IFeatureClassManager)_dataManager).DeleteFcInSde(hemErase200mCoSde);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("line 924 CalGiaHemTren200,ex=" + ex.ToString());
                        }
                        //((IDataset)thuaClipFc).Delete();
                        //wspEdit.StopEditOperation();
                        //wspEdit.StopEditing(true);
                    //}
                    #endregion

                    #region chon duong buffer 100 dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongBuff200Sls = duongBuff200FeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongBuff200Fsls = (IFeatureSelection)duongBuff200FeatureLayer;
                    duongBuff200Fsls.SelectionSet = duongBuff200Sls;
                    #endregion

                    #region chon hem nam trong vung buffer 200m nhưng khong nam trong vùng buffer 100m
                    _qrBl.FromLayer = hemChinhFeatureLayer;
                    _qrBl.ByLayer = duongFeatureLayer;
                    _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectContainedBy;
                    _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultSubtract;
                    _qrBl.UseSelectedFeatures = true;
                    _qrBl.BufferDistance = 200;
                    try
                    {
                        hemChinhSelectionSet = _qrBl.Select();
                        hemChinhFeatureSelection = (IFeatureSelection)hemChinhFeatureLayer;
                        hemChinhFeatureSelection.SelectionSet = hemChinhSelectionSet;
                    }
                    catch
                    { continue; }

                    if (!(hemChinhSelectionSet.Count > 0))
                    {
                        continue;
                    }
                    #endregion

                    #region log---
                    evt.Log = string.Format("\n----Kiểm tra, clip hẻm theo đường buffer {0}m, lưu vào bảng {1} ...", _currentConfig.DKhoangCach50mMatTien, hemErase200mCoSde);
                    onCalculating(evt);
                    #endregion

                    string hemlyr = string.Format("{0}/{1}", _sysTempPath.TempPath, hemErase200mNoSde);
                    string duonglyr = string.Format("{0}/{1}", _sysTempPath.TempPath, duongBuffer200mNoSde);
                    _eraseTool.EraseByLayerFileInsideSde(hemChinhFeatureLayer, hemlyr, duongBuff200FeatureLayer, duonglyr, hemErase200mNoSde);
                    try
                    {
                        hemClipFc = fw.OpenFeatureClass(hemErase200mCoSde);
                    }
                    catch (Exception ex) { continue; }
                    _version.RegisterDataset((IDataset)hemClipFc, true, true);
                    #endregion

                    #region xet tung hem da clip

                    //[thaydoi] - co the them bien chay
                    //********************
                    #region khoi dau
                    //IFeatureCursor hemErCur = hemClipFc.Search(null, false);

                    IEnumIDs hemChinhIds = hemChinhSelectionSet.IDs;
                    int hemChinhId;
                    IFeature hemChinhFt;
                    int iHem = 0;
                    List<object[,]> pairColValTgd = new List<object[,]>();
                    int rowTgdNnHandleUpdate = 0;
                    List<object> newId = new List<object>();
                    #endregion
                    try
                    {
                        while ((hemChinhId = hemChinhIds.Next()) != -1)
                        {
                            //he so vi tri hem ung voi doan sau <100m
                            int hesoVitri = 3;
                            #region lay thong tin hem dang xet

                            //[kodoi]
                            //============================
                            #region lay thong tin co ban
                            hemChinhFt = hemFeatureClass.GetFeature(hemChinhId);
                            //hemErId = hemErFt.OID;
                            object maHem = hemChinhFt.get_Value(hemChinhFt.Fields.FindField(_fcName.FC_HEM.MA_HEM));

                            #endregion

                            #endregion

                            #region kiem tra trong bang giadat_hem, voi dieu kien:mahem,hesovitri,khoagia=0
                            evt.Reset();
                            evt.Log = "\n[!]--- Kiểm tra các hẻm đã có giá, cập nhật giá mới ...";
                            onCalculating(evt);
                            qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}'",
                                _fcName.FC_GIA_DAT_HEM.LOCKED, 0, _fcName.FC_GIA_DAT_HEM.LOCKED,
                                _fcName.FC_GIA_DAT_HEM.MA_HEM, maHem,
                                _fcName.FC_GIA_DAT_HEM.HE_SO, hesoVitri);
                            IFeatureCursor gdhChinhFcs = gdhChinhFeatureClass.Search(qrf, false);
                            IFeature gdhChinhFt = null;

                            try
                            {
                                gdhChinhFt = gdhChinhFcs.NextFeature();//dam bao la chi co 1 hang ket qua
                                if (gdhChinhFt != null)
                                {
                                    //MessageBox.Show("co");
                                    //kiem tra co cho phep tinh lai vi tri
                                    //neu co:xoa feater cu,them feature moi
                                    #region xet thua da co vi tri

                                    bool isOverWritePos = true;

                                    if (!isOverWritePos)
                                    {
                                        newId.Add(gdhChinhFt.OID);
                                        //continue;
                                    }
                                    else
                                    {
                                        //[kodoi]
                                        //===================
                                        #region xoa feature cu
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                        //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                        gdhChinhFt.Delete();
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                        #endregion
                                        //===================
                                        #region chon trong bang thua clip
                                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, maHem);
                                        IFeatureCursor hemClipFcur = hemClipFc.Search(qrf, false);
                                        IFeature hemErFt = null;
                                        try
                                        {
                                            hemErFt = hemClipFcur.NextFeature();
                                            //MessageBox.Show("line 1058 CalcGiaHemTren200, id="+hemErFt.get_Value(0));
                                            if (hemErFt == null)
                                            {
                                                continue;
                                            }
                                        }
                                        catch (Exception ex) { continue; }
                                        finally { Marshal.ReleaseComObject(hemClipFcur); }

                                        #region lay thong tin thua clip
                                        //IArea area = (IArea)hemClipFt.Shape;
                                        #endregion

                                        #endregion
                                        //[thaydoi] - them gia tri
                                        //**********************
                                        #region them feature moi
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(hemErFt, gdhChinhFeatureClass);
                                        try
                                        {
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show("line 1292 CalcGiaHemChinh ex=" + ex.ToString());
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(false);
                                        }

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.MA_HEM), maHem } });
                                        pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.LOCKED), 0 } });
                                        pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.HE_SO), hesoVitri } });
                                        //MessageBox.Show(string.Format("line 1062 CalcGiaHemChinh mahem={0},maduong={1}", maHem, maduong));
                                        sdeTblGdhEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        //MessageBox.Show("line 1066 CalcGiaHemChinh copyid=" + copiedId);
                                        newId.Add(copiedId);
                                        #endregion
                                        //**********************
                                    }

                                    #endregion

                                }
                                else
                                {
                                    //MessageBox.Show("ko co");
                                    //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                    //***********************************
                                    #region chon trong bang thua clip
                                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, maHem);
                                    //MessageBox.Show("line 1322 CalcGiaHemChinh, mahem=" + maHem);
                                    IFeatureCursor hemClipFcur = hemClipFc.Search(qrf, false);
                                    IFeature hemErFt = null;
                                    try
                                    {
                                        hemErFt = hemClipFcur.NextFeature();
                                        if (hemErFt == null)
                                        {
                                            continue;
                                        }
                                    }
                                    catch (Exception ex) { return; }
                                    finally { Marshal.ReleaseComObject(hemClipFcur); }

                                    #region lay thong tin thua clip
                                    //IArea area = (IArea)hemClipFt.Shape;
                                    #endregion

                                    #endregion
                                    #region them feature moi
                                    if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                    {
                                        mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                    }
                                    else
                                    {
                                        wspEdit.StartEditing(true);
                                    }
                                    wspEdit.StartEditOperation();
                                    object copiedId = copyTool.Copy(hemErFt, gdhChinhFeatureClass);
                                    try
                                    {
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show("line 1355 CalcGiaHemChinh ex=" + ex.ToString());
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(false);
                                    }

                                    //them gia tri mathua,maduong,hesovitri
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.MA_HEM), maHem } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.HE_SO), hesoVitri } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.LOCKED), 0 } });
                                    //MessageBox.Show(string.Format("line 1125 CalcGiaHemChinh mahem={0},maduong={1}",maHem,maduong));
                                    sdeTblGdhEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    //MessageBox.Show("line 1129 CalcGiaHemChinh copyid=" + copiedId);
                                    newId.Add(copiedId);

                                    #endregion
                                    //***********************************
                                }
                                //MessageBox.Show(newId.Count.ToString());
                            }
                            catch (Exception e1)
                            {
                                MessageBox.Show(string.Format("CalcGiaHemChinh, line 1448-\n{0}", e1));
                            }
                            finally { Marshal.ReleaseComObject(gdhChinhFcs); }

                            #endregion
                        }
                    }
                    catch (Exception ex) { }
                    //finally { Marshal.ReleaseComObject(hemErCur); }

                    #endregion

                    //[kodoi]
                    //============================
                    #region luu thong tin vao bang gia dat
                    if (!sdeTblGdhEditor.IsEditing())
                    {
                        sdeTblGdhEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                        sdeTblGdhEditor.StartEditOperation();
                    }

                    #region ----log
                    evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", gdhChinhFeatureClass.AliasName);
                    onCalculating(evt);
                    #endregion
                    try
                    {
                        sdeTblGdhEditor.SaveEdit();
                        sdeTblGdhEditor.StopEditOperation();
                        sdeTblGdhEditor.StopEditing(true);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("line 1405 CalcGiaHemChinh ex=" + ex.ToString());
                        sdeTblGdhEditor.StopEditOperation();
                        sdeTblGdhEditor.StopEditing(false);
                    }

                    #region ----log
                    evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", gdhChinhFeatureClass.AliasName);
                    onCalculating(evt);
                    #endregion

                    #endregion

                    #region tinh gia dat cho cac hem vua them vi tri
                    CalcLandprice calc = new CalcLandprice(this);
                    //MessageBox.Show(string.Format("final:{0}", newId.Count));

                    calc.CalcGiaHem(newId);
                    newId.Clear();
                    #endregion
                    //============================

                }
                #endregion
            }
            #endregion

            #endregion

            //===========================================
            //===========================================
            //*******************************************

            //[kodoi]
            //==============
            #region doan ket
            sothuatinhduoc = sothuatimthay - sothuaKhongTinhDuoc;

            #region ----log
            evt.Log = string.Format("\n\n**************************************\n******* Đã tính xong các thửa phi nông nghiệp tại đô thị vị trí mặt tiền*******\n**********************************");
            //evt.Log += string.Format("\n Số thửa tìm thấy: {0}", sothuatimthay);
            //evt.Log += string.Format("\n Số thửa được tính: {0}", sothuatinhduoc);
            onCalculating(evt);
            #endregion
            evt.Reset();
            evt.ProgressingTotalCount = ".";
            evt.ProgressingTotal = ".";
            onCalculating(evt);
            evt.CurrentIndexCalculator = this._index;
            onFinished(evt);
            #endregion
            //==============
        }