コード例 #1
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (listViewEx.CheckedItems.Count == 0)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "未选择对象,无法生成历史!");
                return;
            }

            Exception err = null;

            //获取现势库连接信息

            SysCommon.Gis.SysGisDataSet sourceSysGisDataSet = new SysCommon.Gis.SysGisDataSet();
            switch (comBoxType.Text)
            {
            case "SDE":
                sourceSysGisDataSet.SetWorkspace(txtServer.Text, txtInstance.Text, txtDB.Text, txtUser.Text, txtPassword.Text, txtVersion.Text, out err);
                break;

            case "GDB":
                sourceSysGisDataSet.SetWorkspace(txtDB.Text, SysCommon.enumWSType.GDB, out err);
                break;

            case "PDB":
                sourceSysGisDataSet.SetWorkspace(txtDB.Text, SysCommon.enumWSType.PDB, out err);
                break;

            default:
                break;
            }

            if (err != null || sourceSysGisDataSet.WorkSpace == null)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "未设置用户数据库连接或连接失败,请检查!");
                return;
            }

            //获取历史库连接信息

            IPropertySet pPropSet        = new PropertySetClass();
            IWorkspace   pTagetWorkspace = null;

            try
            {
                switch (comboBoxTypeHistory.Text)
                {
                case "PDB":
                    AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                    if (!File.Exists(txtDBHistory.Text))
                    {
                        FileInfo filePDB = new FileInfo(txtDBHistory.Text);
                        pAccessFact.Create(filePDB.DirectoryName, filePDB.Name, null, 0);
                    }
                    pPropSet.SetProperty("DATABASE", txtDBHistory.Text);
                    pTagetWorkspace = pAccessFact.Open(pPropSet, 0);

                    break;

                case "GDB":
                    FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                    DirectoryInfo dirGDB = new DirectoryInfo(txtDBHistory.Text);
                    pFileGDBFact.Create(dirGDB.Parent.FullName, dirGDB.Name.Substring(0, dirGDB.Name.Length - 4), null, 0);
                    pPropSet.SetProperty("DATABASE", txtDBHistory.Text);
                    pTagetWorkspace = pFileGDBFact.Open(pPropSet, 0);

                    break;

                case "SDE":
                    IWorkspaceFactory pSdeFact = new SdeWorkspaceFactoryClass();
                    pPropSet.SetProperty("SERVER", txtServerHistory.Text);
                    pPropSet.SetProperty("INSTANCE", txtInstanceHistory.Text);
                    pPropSet.SetProperty("DATABASE", txtDBHistory.Text);
                    pPropSet.SetProperty("USER", txtUserHistory.Text);
                    pPropSet.SetProperty("PASSWORD", txtPasswordHistory.Text);
                    pPropSet.SetProperty("VERSION", txtVersionHistory.Text);
                    pTagetWorkspace = pSdeFact.Open(pPropSet, 0);

                    break;

                default:
                    break;
                }
            }
            catch (Exception er)
            {
                //*******************************************************************
                //guozheng added
                if (ModData.SysLog != null)
                {
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                else
                {
                    ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                //********************************************************************
            }

            if (pTagetWorkspace == null)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "未设置历史库连接或连接失败,请检查!");
                return;
            }

            this.Cursor = System.Windows.Forms.Cursors.AppStarting;

            List <string> lstData = new List <string>();

            try
            {
                //根据用户数据库结构创建历史库
                foreach (ListViewItem aItem in listViewEx.CheckedItems)
                {
                    if (aItem.Tag.ToString() == "FD")
                    {
                        IFeatureDataset tagetFeatureDataset = null;
                        IFeatureDataset pFeatureDataset     = sourceSysGisDataSet.GetFeatureDataset(aItem.Text, out err);
                        if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureDataset, aItem.Text + "_GOH"))
                        {
                            tagetFeatureDataset = (pTagetWorkspace as IFeatureWorkspace).CreateFeatureDataset(aItem.Text + "_GOH", (pFeatureDataset as IGeoDataset).SpatialReference);
                        }
                        else
                        {
                            tagetFeatureDataset = (pTagetWorkspace as IFeatureWorkspace).OpenFeatureDataset(aItem.Text + "_GOH");
                        }

                        IEnumDataset pEnumDs = pFeatureDataset.Subsets;
                        pEnumDs.Reset();
                        IDataset pDs = pEnumDs.Next();
                        while (pDs != null)
                        {
                            IFeatureClass pFeatureClass = pDs as IFeatureClass;
                            if (pFeatureClass != null)
                            {
                                if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureClass, pDs.Name + "_GOH"))
                                {
                                    CreateFeatCls(tagetFeatureDataset, pFeatureClass, pDs.Name + "_GOH", out err);
                                }
                                lstData.Add(pDs.Name);
                            }
                            pDs = pEnumDs.Next();
                        }
                    }
                    else if (aItem.Tag.ToString() == "FC")
                    {
                        IFeatureClass pFeatureClass = sourceSysGisDataSet.GetFeatureClass(aItem.Text, out err);
                        if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureClass, aItem.Text + "_GOH"))
                        {
                            CreateFeatCls(pTagetWorkspace as IFeatureWorkspace, pFeatureClass, aItem.Text + "_GOH", out err);
                        }
                        lstData.Add(aItem.Text);
                    }
                }
            }
            catch (Exception er)
            {
                //*******************************************************************
                //guozheng added
                if (ModData.SysLog != null)
                {
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                else
                {
                    ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                //********************************************************************
                this.Cursor = System.Windows.Forms.Cursors.Default;
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "创建历史库结构失败!");
                return;
            }

            //遍历现势库数据FC进行数据移植
            Dictionary <string, string> dicFieldsPair = new Dictionary <string, string>();

            dicFieldsPair.Add("OBJECTID", "SourceOID");
            Dictionary <string, object> dicValue = new Dictionary <string, object>();

            dicValue.Add("FromDate", DateTime.Now.ToString("u"));
            dicValue.Add("ToDate", DateTime.MaxValue.ToString("u"));//.ToString("YYYY-MM-DD HH:MI:SS"));
            dicValue.Add("State", 0);
            (pTagetWorkspace as IWorkspaceEdit).StartEditing(false);
            bool res = true;

            progressBarXLay.Maximum = lstData.Count;
            progressBarXLay.Minimum = 0;
            progressBarXLay.Value   = 0;
            foreach (string aFeatClsName in lstData)
            {
                labelXMemo.Text = "正在进行图层" + aFeatClsName + "...";
                Application.DoEvents();
                int            cnt           = 0;
                int            allcnt        = 0;
                IFeatureCursor featureCursor = null;
                IFeatureClass  tagetFeatCls  = null;
                try
                {
                    featureCursor = sourceSysGisDataSet.GetFeatureCursor(aFeatClsName, "", null, "", out err, out cnt, out allcnt);
                    tagetFeatCls  = (pTagetWorkspace as IFeatureWorkspace).OpenFeatureClass(aFeatClsName + "_GOH");
                }
                catch (Exception ex)
                {
                    //*******************************************************************
                    //guozheng added
                    if (ModData.SysLog != null)
                    {
                        ModData.SysLog.Write(ex, null, DateTime.Now);
                    }
                    else
                    {
                        ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                        ModData.SysLog.Write(ex, null, DateTime.Now);
                    }
                    //********************************************************************
                    SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", ex.Message);
                    return;
                }

                progressBarXFeat.Maximum = cnt;
                progressBarXFeat.Minimum = 0;
                progressBarXFeat.Value   = 0;
                ModDBOperator.NewFeatures(tagetFeatCls, featureCursor, dicFieldsPair, dicValue, true, false, progressBarXFeat, out err);
                Marshal.ReleaseComObject(featureCursor);
                progressBarXLay.Value++;

                labelXMemo.Text = "";
                Application.DoEvents();
                if (err != null)
                {
                    res = false;
                    break;
                }
            }
            (pTagetWorkspace as IWorkspaceEdit).StopEditing(res);

            this.Cursor = System.Windows.Forms.Cursors.Default;
            if (res)
            {
                if (m_DbProjectElement != null)
                {
                    try
                    {
                        XmlElement aElement = m_DbProjectElement.SelectSingleNode(".//内容//历史库//连接信息") as XmlElement;
                        SaveObjDB(aElement, listViewEx.Items[0].Text + "_GOH");
                    }
                    catch (Exception er)
                    {
                        //*******************************************************************
                        //guozheng added
                        if (ModData.SysLog != null)
                        {
                            ModData.SysLog.Write(er, null, DateTime.Now);
                        }
                        else
                        {
                            ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                            ModData.SysLog.Write(er, null, DateTime.Now);
                        }
                        //********************************************************************
                        return;
                    }
                }
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "生成历史库成功!");
                this.Close();
            }
            else
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "生成历史库失败!");
            }
        }
コード例 #2
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtProjectName.Text == "")
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("信息提示", "请填写输出文件名称 !");
                return;
            }

            if (!Directory.Exists(txtSavePath.Text.Trim()))
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("信息提示", "请选择保存路径 !");
                return;
            }

            if (listViewEx.CheckedItems.Count == 0)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("信息提示", "请选择数据版本信息 !");
                return;
            }

            string WorkSpacePath = txtSavePath.Text.Trim() + "\\" + txtProjectName.Text.Trim();

            IPropertySet pPropSet        = new PropertySetClass();
            IWorkspace   pTagetWorkspace = null;

            try
            {
                switch (comBoxType.Tag.ToString().Trim())
                {
                case "PDB":
                    AccessWorkspaceFactory pAccessFact = new AccessWorkspaceFactoryClass();
                    if (!File.Exists(WorkSpacePath + ".mdb"))
                    {
                        pAccessFact.Create(txtSavePath.Text, txtProjectName.Text, null, 0);
                    }
                    pPropSet.SetProperty("DATABASE", WorkSpacePath + ".mdb");
                    pTagetWorkspace = pAccessFact.Open(pPropSet, 0);
                    pAccessFact     = null;
                    pPropSet        = null;
                    break;

                case "GDB":
                    FileGDBWorkspaceFactoryClass pFileGDBFact = new FileGDBWorkspaceFactoryClass();
                    if (!Directory.Exists(WorkSpacePath + ".gdb"))
                    {
                        pFileGDBFact.Create(txtSavePath.Text, txtProjectName.Text, null, 0);
                    }
                    pPropSet.SetProperty("DATABASE", WorkSpacePath + ".gdb");
                    pTagetWorkspace = pFileGDBFact.Open(pPropSet, 0);
                    pFileGDBFact    = null;
                    pPropSet        = null;
                    break;

                default:
                    break;
                }
            }
            catch (Exception er)
            {
                //*******************************************************************
                //guozheng added
                if (ModData.SysLog != null)
                {
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                else
                {
                    ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                    ModData.SysLog.Write(er, null, DateTime.Now);
                }
                //********************************************************************
            }

            if (pTagetWorkspace == null)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "未设置输出库连接或连接失败,请检查!");
                return;
            }

            this.Cursor = System.Windows.Forms.Cursors.AppStarting;

            Exception            err        = null;
            bool                 res        = true;
            List <IFeatureLayer> lstFeatLay = new List <IFeatureLayer>();

            //根据历史图层创建输出数据图层结构
            for (int i = 0; i < Map.LayerCount; i++)
            {
                //cyf 20110706 add
                ILayer mLayer = Map.get_Layer(i);
                if (mLayer is IGroupLayer)
                {
                    ICompositeLayer pComLayer = mLayer as ICompositeLayer;
                    for (int j = 0; j < pComLayer.Count; j++)
                    {
                        IFeatureLayer featLay = pComLayer.get_Layer(j) as IFeatureLayer;
                        if (featLay == null)
                        {
                            continue;
                        }
                        if (!(featLay.FeatureClass as IDataset).Name.EndsWith("_GOH"))
                        {
                            continue;
                        }
                        lstFeatLay.Add(featLay);
                        if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureClass, featLay.Name))//(featLay.FeatureClass as IDataset).Name
                        {
                            if (!CreateFeatCls(pTagetWorkspace as IFeatureWorkspace, featLay.FeatureClass, featLay.Name, out err))
                            {
                                res       = false;
                                featLay   = null;
                                pComLayer = null;
                                mLayer    = null;
                                break;
                            }
                        }
                    }
                }//end
                else
                {
                    IFeatureLayer featLay = Map.get_Layer(i) as IFeatureLayer;
                    if (featLay == null)
                    {
                        continue;
                    }
                    if (!(featLay.FeatureClass as IDataset).Name.EndsWith("_GOH"))
                    {
                        continue;
                    }
                    lstFeatLay.Add(featLay);
                    if (!(pTagetWorkspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureClass, featLay.Name))//(featLay.FeatureClass as IDataset).Name
                    {
                        if (!CreateFeatCls(pTagetWorkspace as IFeatureWorkspace, featLay.FeatureClass, featLay.Name, out err))
                        {
                            res     = false;
                            featLay = null;
                            mLayer  = null;
                            break;
                        }
                    }
                }
            }

            if (res == false)
            {
                this.Cursor = System.Windows.Forms.Cursors.Default;
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "创建输出库结构失败!");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pTagetWorkspace);
                pTagetWorkspace = null;
                lstFeatLay.Clear();
                lstFeatLay = null;
                try
                {
                    Directory.Delete(txtSavePath.Text + "\\" + txtProjectName.Text + ".gdb", true);
                }
                catch (Exception err2)
                { }
                return;
            }

            StringBuilder sb = new StringBuilder();

            foreach (ListViewItem aItem in listViewEx.CheckedItems)
            {
                if (sb.Length != 0)
                {
                    sb.Append("or");
                }
                else
                {
                    sb.Append("(");
                }

                sb.Append("(FromDate<='" + aItem.Text + "' and ToDate>'" + aItem.Text + "')");
            }

            sb.Append(")");

            //遍历数据FC进行数据移植
            (pTagetWorkspace as IWorkspaceEdit).StartEditing(false);
            progressBarXLay.Maximum = lstFeatLay.Count;
            progressBarXLay.Minimum = 0;
            progressBarXLay.Value   = 0;
            foreach (IFeatureLayer aFeatLay in lstFeatLay)
            {
                labelXMemo.Text = aFeatLay.FeatureClass.AliasName + "...";
                Application.DoEvents();

                StringBuilder newSB = new StringBuilder();
                newSB.Append(sb.ToString());

                if (m_Sel)
                {
                    int fdIndex = aFeatLay.FeatureClass.Fields.FindField("SourceOID");
                    if (fdIndex == -1)
                    {
                        continue;
                    }
                    IFeatureLayerDefinition featLayDefTemp = aFeatLay as IFeatureLayerDefinition;
                    IEnumIDs      pEnumIDs = featLayDefTemp.DefinitionSelectionSet.IDs;
                    int           ID       = pEnumIDs.Next();
                    StringBuilder sbTemp   = new StringBuilder();
                    while (ID != -1)
                    {
                        IFeature pFeat = aFeatLay.FeatureClass.GetFeature(ID);
                        if (sbTemp.Length != 0)
                        {
                            sbTemp.Append(",");
                        }
                        sbTemp.Append(pFeat.get_Value(fdIndex).ToString());
                        ID = pEnumIDs.Next();
                    }
                    newSB.Append(" and SourceOID in (" + sbTemp.ToString() + ")");
                }

                IQueryFilter queryFilter = new QueryFilterClass();
                queryFilter.WhereClause = newSB.ToString();
                IFeatureCursor featureCursor = aFeatLay.FeatureClass.Search(queryFilter, false);
                progressBarXFeat.Maximum = aFeatLay.FeatureClass.FeatureCount(queryFilter);
                progressBarXFeat.Minimum = 0;
                progressBarXFeat.Value   = 0;
                try
                {
                    //cyf 20110706 modify:去掉用户名
                    string pFeaLayerName = aFeatLay.Name.Trim();
                    if (pFeaLayerName.Contains("."))
                    {
                        pFeaLayerName = pFeaLayerName.Substring(pFeaLayerName.IndexOf('.') + 1);
                    }
                    //end
                    IFeatureClass tagetFeatCls = (pTagetWorkspace as IFeatureWorkspace).OpenFeatureClass(pFeaLayerName);//(aFeatLay.FeatureClass as IDataset).Name
                    ModDBOperator.NewFeatures(tagetFeatCls, featureCursor, null, null, true, false, progressBarXFeat, out err);
                }
                catch (Exception ex)
                {
                    //*******************************************************************
                    //guozheng added
                    if (ModData.SysLog != null)
                    {
                        ModData.SysLog.Write(ex, null, DateTime.Now);
                    }
                    else
                    {
                        ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog();
                        ModData.SysLog.Write(ex, null, DateTime.Now);
                    }
                    //********************************************************************
                    SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "入库失败!");
                    return;
                }

                Marshal.ReleaseComObject(featureCursor);
                progressBarXLay.Value++;

                labelXMemo.Text = "";
                Application.DoEvents();
                if (err != null)
                {
                    res = false;
                    break;
                }
            }

            (pTagetWorkspace as IWorkspaceEdit).StopEditing(res);

            this.Cursor = System.Windows.Forms.Cursors.Default;
            if (res)
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "提取成功!");
                this.Close();
            }
            else
            {
                SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "提取失败!");
            }
        }