using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using KGIS.Framework.AE; using KGIS.Framework.AE.Enum; using KGIS.Framework.AE.GPHelper; using KGIS.Framework.Commands; using KGIS.Framework.DBOperator; using KGIS.Framework.Maps; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using kingo.Plugin.BGResultManager.Helper; using Kingo.PluginServiceInterface; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Runtime.InteropServices; using KUI.Windows; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.DataSourcesGDB; using KGIS.Framework.Utils.ExtensionMethod; namespace Kingo.Plugin.BGResultManager.Commands { /// /// 变更后年末库导出 /// public class BGENDNMKExportCommand : BaseMenuCommand { private IHookHelper m_hookHelper = null; public override void OnCreate(object hook) { if (hook == null) { return; } try { if (m_hookHelper == null) { m_hookHelper = new HookHelperClass(); m_hookHelper.Hook = hook; } } catch { m_hookHelper = null; return; } } public override void OnClick() { try { ProjectInfo projectInfo = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo); string Code_Name = GetXZQDic(projectInfo.CODE); #region 定义变量 IWorkspaceAPI wsAPI = null; IFeatureLayer dltbbg_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG"); IFeatureLayer dltbgx_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBGX"); IFeatureLayer JCdltb_Layer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑"); IFeatureLayer xzqgx_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("XZQGX"); IFeatureLayer xzqjxgx_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("XZQJXGX"); IFeatureLayer jcxzq_Layer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("行政区"); IFeatureLayer czcdydgx_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("CZCDYDGX"); IFeatureLayer czcdydgxzj_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("CZCDYDGXZJ"); IFeatureLayer czcdydgxgc_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("CZCDYDGXGC"); IFeatureLayer cjdcqgx_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("CJDCQGX"); IFeatureLayer cjdcqjxgx_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("CJDCQJXGX"); IFeatureLayer jccjdcq_Layer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("村级调查区"); IFeatureLayer gxzj_Layer = MapsManager.Instance.MapService.GetFeatureLayerByName("ZJGX"); IFeatureLayer jcczcdyd_Layer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("城镇村等用地"); IFeatureLayer jccjdcqjx_Layer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("村级调查区界线"); IFeatureLayer jcxzqjx_Layer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("行政区界线"); IFeatureLayer XZQNMKLayer = null; IFeatureLayer CJDCQNMKLayer = null; IFeatureLayer XZQJXNMKLayer = null; IFeatureLayer CJDCQJXNMKLayer = null; ISpatialReference pSR = null; GPParamClass gPParamClass = new GPParamClass(); IFeatureClassAPI fcAPI = null; IFeatureClass featureClass_Temps = null; IFeatureClassAPI t_FcAPIs = null; #endregion string dbPath = System.IO.Path.GetDirectoryName((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).GetProjFilePath()) + @"\BGTJ.sqlite"; pSR = (JCdltb_Layer.FeatureClass as IGeoDataset).SpatialReference; string saveFile = ""; //弹出文件选择对话框 var saveFileDialog = new Microsoft.Win32.SaveFileDialog { FileName = "年末库导出_" + projectInfo.CODE + Code_Name, Filter = "GDB文件(*.gdb)|*.gdb|MDB文件(*.mdb)|*.mdb" }; var result = saveFileDialog.ShowDialog(); if (result.Value) { string gdbFileName = saveFileDialog.SafeFileName;// projectInfo.CODE + Code_Name + ".gdb";//创建年末库储存的GDB文件 string path = string.Empty; string MDBpath = string.Empty; path = saveFileDialog.FileName; if (saveFileDialog.FileName.EndsWith(".mdb")) { saveFile = saveFileDialog.FileName.Split('.')[0].ToString(); if (Directory.Exists(saveFile)) { MessageHelper.ShowTips("该文件已存在!"); return; } gdbFileName = projectInfo.CODE + Code_Name + Guid.NewGuid().ToString() + ".gdb"; MDBpath = System.IO.Path.Combine(saveFile, projectInfo.CODE + Code_Name + ".mdb");//MDB生成路径 if (!Directory.Exists(saveFile)) Directory.CreateDirectory(saveFile); saveFile = SysAppPath.GetTempPath() + @"NMKTemp\";//临时GDB数据 if (!Directory.Exists(saveFile)) Directory.CreateDirectory(saveFile); //删除saveFile当前路径下,所有的文件和目录 PluginServiceInterface.CommonHelper.DelectDir(saveFile); path = System.IO.Path.Combine(saveFile, gdbFileName); } this.ShowLoading("进行年末库数据导出……", 0, 0); //workspaceName = pSaveFct.Create(saveFile, gdbFileName, null, 0);//创建数据库 wsAPI = GeoDBAPI.CreateLocalWorkspace(path); //wsAPI = new WorkspaceAPI(path, Framework.AE.Enum.WorkspaceTypeEnum.GDBFile, false); IFeatureWorkspace targetWorkspac = wsAPI.CurrentWorkspace as IFeatureWorkspace; IDataset newDataset = targetWorkspac.CreateFeatureDataset("TDDC", pSR); this.UpdateMsg("进行地类图斑年末库数据导出……"); IFeatureClass sd = BGNMKHelper.BgDLTBNMKData(wsAPI, newDataset); if (sd == null) { this.CloseLoading(); return; } #region 村级调查区年末库 if (cjdcqgx_Layer != null && cjdcqgx_Layer.FeatureClass.FeatureCount(null) != 0) { this.UpdateMsg("进行村级调查区年末库数据导出……"); gPParamClass = new GPParamClass(); gPParamClass.FirstFeatureLayer = jccjdcq_Layer; gPParamClass.SecondFeatureLayer = cjdcqgx_Layer; gPParamClass.OutFeatureClassPath = path + "\\" + "CJDCQs"; gPParamClass.IsGetOutPutFeature = true; GeoprocessorHelper.EraseAnalysis(gPParamClass, ref CJDCQNMKLayer); fcAPI = new FeatureClassAPI(cjdcqgx_Layer.FeatureClass); fcAPI.FcToFc(CJDCQNMKLayer.FeatureClass, null, false); featureClass_Temps = wsAPI.CreateFeatureClass("CJDCQ", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, CJDCQNMKLayer.FeatureClass.Fields).FeatureClass;//创建数据集 t_FcAPIs = new FeatureClassAPI(CJDCQNMKLayer.FeatureClass); t_FcAPIs.FcToFc(featureClass_Temps, null, false); wsAPI.DeleteFeatureClass("CJDCQs"); } else if (jccjdcq_Layer != null && jccjdcq_Layer.FeatureClass.FeatureCount(null) != 0) { featureClass_Temps = wsAPI.CreateFeatureClass("CJDCQ", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, jccjdcq_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 t_FcAPIs = new FeatureClassAPI(jccjdcq_Layer.FeatureClass); t_FcAPIs.FcToFc(featureClass_Temps, null, false); } else { if (cjdcqgx_Layer != null) featureClass_Temps = wsAPI.CreateFeatureClass("CJDCQ", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, cjdcqgx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 } #endregion #region 村级调查区界限年末库 if (cjdcqjxgx_Layer != null && cjdcqjxgx_Layer.FeatureClass.FeatureCount(null) != 0) { this.UpdateMsg("进行村级调查区界限年末库数据导出……"); IFeatureClass Temp_featureClass = wsAPI.CreateFeatureClass("KPCZCCK_Temp", pSR, cjdcqjxgx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 fcAPI = new FeatureClassAPI(cjdcqjxgx_Layer.FeatureClass); IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = " BGXW<>'3' "; fcAPI.FcToFc(Temp_featureClass, queryFilter, false); IFeatureClass Temps_featureClass = wsAPI.CreateFeatureClass("KPCZCCKs_Temp", pSR, cjdcqjxgx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 fcAPI = new FeatureClassAPI(cjdcqjxgx_Layer.FeatureClass); IQueryFilter queryFilters = new QueryFilterClass(); queryFilter.WhereClause = " BGXW<>'0' "; fcAPI.FcToFc(Temps_featureClass, queryFilter, false); gPParamClass = new GPParamClass(); gPParamClass.FirstFeatureLayer = jccjdcqjx_Layer; gPParamClass.SecondFeatureLayer = new FeatureLayerClass() { FeatureClass = Temp_featureClass, Name = Temp_featureClass.AliasName }; gPParamClass.OutFeatureClassPath = path + "\\" + "CJDCQJXs"; gPParamClass.IsGetOutPutFeature = true; GeoprocessorHelper.EraseAnalysis(gPParamClass, ref CJDCQJXNMKLayer); fcAPI = new FeatureClassAPI(Temps_featureClass); fcAPI.FcToFc(CJDCQJXNMKLayer.FeatureClass, null, false); wsAPI.DeleteFeatureClass("KPCZCCK_Temp"); wsAPI.DeleteFeatureClass("KPCZCCKs_Temp"); featureClass_Temps = wsAPI.CreateFeatureClass("CJDCQJX", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, CJDCQJXNMKLayer.FeatureClass.Fields).FeatureClass;//创建数据集 t_FcAPIs = new FeatureClassAPI(CJDCQJXNMKLayer.FeatureClass); t_FcAPIs.FcToFc(featureClass_Temps, null, false); wsAPI.DeleteFeatureClass("CJDCQJXs"); } else if (jccjdcqjx_Layer != null && jccjdcqjx_Layer.FeatureClass.FeatureCount(null) != 0) { featureClass_Temps = wsAPI.CreateFeatureClass("CJDCQJX", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, jccjdcqjx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 t_FcAPIs = new FeatureClassAPI(jccjdcqjx_Layer.FeatureClass); t_FcAPIs.FcToFc(featureClass_Temps, null, false); } else { if (cjdcqjxgx_Layer != null) featureClass_Temps = wsAPI.CreateFeatureClass("CJDCQJX", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, cjdcqjxgx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 } #endregion #region 行政区年末库 if (xzqgx_Layer != null && xzqgx_Layer.FeatureClass.FeatureCount(null) != 0) { this.UpdateMsg("进行行政区年末库数据导出……"); gPParamClass = new GPParamClass(); gPParamClass.FirstFeatureLayer = jcxzq_Layer; gPParamClass.SecondFeatureLayer = xzqgx_Layer; gPParamClass.OutFeatureClassPath = path + "\\" + "XZQs"; gPParamClass.IsGetOutPutFeature = true; GeoprocessorHelper.EraseAnalysis(gPParamClass, ref XZQNMKLayer); fcAPI = new FeatureClassAPI(xzqgx_Layer.FeatureClass); fcAPI.FcToFc(XZQNMKLayer.FeatureClass, null, false); featureClass_Temps = wsAPI.CreateFeatureClass("XZQ", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, XZQNMKLayer.FeatureClass.Fields).FeatureClass;//创建数据集 t_FcAPIs = new FeatureClassAPI(XZQNMKLayer.FeatureClass); t_FcAPIs.FcToFc(featureClass_Temps, null, false); wsAPI.DeleteFeatureClass("XZQs"); } else if (jcxzq_Layer != null && jcxzq_Layer.FeatureClass.FeatureCount(null) != 0) { featureClass_Temps = wsAPI.CreateFeatureClass("XZQ", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, jcxzq_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 t_FcAPIs = new FeatureClassAPI(jcxzq_Layer.FeatureClass); t_FcAPIs.FcToFc(featureClass_Temps, null, false); } else { if (xzqgx_Layer != null) featureClass_Temps = wsAPI.CreateFeatureClass("XZQ", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, xzqgx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 } #endregion #region 行政区界线年末库 if (xzqjxgx_Layer != null && xzqjxgx_Layer.FeatureClass.FeatureCount(null) != 0) { this.UpdateMsg("进行行政区界线年末库数据导出……"); IFeatureClass Temp_featureClassw = wsAPI.CreateFeatureClass("KPCZCCK_Temsp", pSR, xzqjxgx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 fcAPI = new FeatureClassAPI(xzqjxgx_Layer.FeatureClass); IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = " BGXW<>'3' "; fcAPI.FcToFc(Temp_featureClassw, queryFilter, false); IFeatureClass Temps_featureClassw = wsAPI.CreateFeatureClass("KPCZCCKs_Temps", pSR, xzqjxgx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 fcAPI = new FeatureClassAPI(xzqjxgx_Layer.FeatureClass); IQueryFilter queryFilters = new QueryFilterClass(); queryFilter.WhereClause = " BGXW<>'0' "; fcAPI.FcToFc(Temps_featureClassw, queryFilter, false); gPParamClass = new GPParamClass(); gPParamClass.FirstFeatureLayer = jcxzqjx_Layer; gPParamClass.SecondFeatureLayer = new FeatureLayerClass() { FeatureClass = Temp_featureClassw, Name = Temp_featureClassw.AliasName }; gPParamClass.OutFeatureClassPath = path + "\\" + "XZQJXs"; gPParamClass.IsGetOutPutFeature = true; GeoprocessorHelper.EraseAnalysis(gPParamClass, ref XZQJXNMKLayer); fcAPI = new FeatureClassAPI(Temps_featureClassw); fcAPI.FcToFc(XZQJXNMKLayer.FeatureClass, null, false); wsAPI.DeleteFeatureClass("KPCZCCK_Temsp"); wsAPI.DeleteFeatureClass("KPCZCCKs_Temps"); featureClass_Temps = wsAPI.CreateFeatureClass("XZQJX", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, XZQJXNMKLayer.FeatureClass.Fields).FeatureClass;//创建数据集 t_FcAPIs = new FeatureClassAPI(XZQJXNMKLayer.FeatureClass); t_FcAPIs.FcToFc(featureClass_Temps, null, false); wsAPI.DeleteFeatureClass("XZQJXs"); } else if (jcxzqjx_Layer != null && jcxzqjx_Layer.FeatureClass.FeatureCount(null) != 0) { featureClass_Temps = wsAPI.CreateFeatureClass("XZQJX", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, jcxzqjx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 t_FcAPIs = new FeatureClassAPI(jcxzqjx_Layer.FeatureClass); t_FcAPIs.FcToFc(featureClass_Temps, null, false); } else { if (xzqjxgx_Layer != null) featureClass_Temps = wsAPI.CreateFeatureClass("XZQJX", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, xzqjxgx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 } #endregion #region 城镇村年末库 if (czcdydgx_Layer != null && czcdydgx_Layer.FeatureClass.FeatureCount(null) != 0) { this.UpdateMsg("进行城镇村年末库数据导出……"); #region MyRegion IFeatureClass featureClass_CZCDYD = wsAPI.CreateFeatureClass("CZCDYD", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, jcczcdyd_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 fcAPI = new FeatureClassAPI(jcczcdyd_Layer.FeatureClass); fcAPI.FcToFc(featureClass_CZCDYD, null, false); IFeatureCursor _cursor = czcdydgxgc_Layer.FeatureClass.Search(new QueryFilterClass() { SubFields = "BGQBSM" }, true); Dictionary DicJcBSMs = new Dictionary(); IFeature fCZC = null; int idx = czcdydgxgc_Layer.FeatureClass.FindField("BGQBSM"); if (idx != -1) { while ((fCZC = _cursor.NextFeature()) != null) { string jcbsm = fCZC.Value[idx].ToTrim(); if (!DicJcBSMs.ContainsKey(jcbsm)) DicJcBSMs.Add(jcbsm, 0); } _cursor = featureClass_CZCDYD.Update(null, true); idx = featureClass_CZCDYD.FindField("BSM"); if (idx != -1) { while ((fCZC = _cursor.NextFeature()) != null) { string bsm = fCZC.Value[idx].ToTrim(); if (DicJcBSMs.ContainsKey(bsm)) { fCZC.Value[idx] = "-1"; _cursor.UpdateFeature(fCZC); } } _cursor.Flush(); } (featureClass_CZCDYD as ITable).DeleteSearchedRows(new QueryFilterClass() { WhereClause = "BSM = '-1'" }); } fcAPI = new FeatureClassAPI(czcdydgx_Layer.FeatureClass); fcAPI.FcToFc(featureClass_CZCDYD, null, false); IFeatureClassAPI classAPICZC = new FeatureClassAPI(featureClass_CZCDYD); if (classAPICZC.FeatureClass.FindField("XZQTZLX") != -1) classAPICZC.DeleteField("XZQTZLX"); Marshal.ReleaseComObject(featureClass_CZCDYD); #endregion } else { if (czcdydgx_Layer != null) featureClass_Temps = wsAPI.CreateFeatureClass("CZCDYD", newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, czcdydgx_Layer.FeatureClass.Fields).FeatureClass;//创建数据集 } IFeatureClassAPI classAPI = new FeatureClassAPI(featureClass_Temps); if (classAPI.FeatureClass.FindField("XZQTZLX") != -1) classAPI.DeleteField("XZQTZLX"); #endregion #region 单独图层年末库-当前已建议移除整层更新的年末图斑 //this.UpdateMsg("进行单独图层年末库数据导出……"); //XDocument xDoc = XDocument.Load(SysAppPath.BG_GetMappingConfigPath()); //foreach (XElement item in xDoc.Descendants("Table")) //{ // if (item.Attributes("TableName") == null || item.Attributes("TableName").Single().Value.ToUpper().EndsWith("GXGC") || item.Attributes("TableName").Single().Value.ToUpper().Equals("PDTGX") || item.Attributes("TableName").Single().Value.ToUpper().Equals("DLTBGX") || item.Attributes("TableName").Single().Value.ToUpper().Equals("XZQGX") || item.Attributes("TableName").Single().Value.ToUpper().Equals("CJDCQGX") || item.Attributes("TableName").Single().Value.ToUpper().Equals("CZCDYDGX") || item.Attributes("TableName").Single().Value.ToUpper().Equals("XZQJXGX") || item.Attributes("TableName").Single().Value.ToUpper().Equals("CJDCQJXGX")) // { // continue; // } // if (item.Attributes("Visiable").SingleOrDefault() == null || item.Attributes("Visiable").SingleOrDefault().Value.Equals("True")) // { // IFeatureLayer featureLayer = MapsManager.Instance.MapService.GetFeatureLayerByName(item.Attributes("TableName").Single().Value); // if (featureLayer == null) continue; // GeoDBAPI.CreateFields(featureLayer.FeatureClass.Fields, out IFields fields);//创建字段 // int v = item.Attributes("TableName").Single().Value.Count(); // string tempFLName = item.Attributes("TableName").Single().Value.Substring(0, v - 2); // IFeatureClass featureClass_Temp = wsAPI.CreateFeatureClass(tempFLName, newDataset as IFeatureDataset, pSR, esriGeometryType.esriGeometryPolygon, fields).FeatureClass;//创建数据集 // IFeatureClassAPI t_FcAPI = new FeatureClassAPI(featureLayer.FeatureClass); // t_FcAPI.FcToFc(featureClass_Temp, null, false); // t_FcAPI = new FeatureClassAPI(featureClass_Temp); // t_FcAPI.DeleteField("GXSJ"); // Marshal.ReleaseComObject(featureLayer); // Marshal.ReleaseComObject(featureClass_Temp); // tempFLName = ""; // } //} #endregion if (saveFileDialog.FileName.EndsWith(".mdb")) { this.UpdateMsg("进行年末库MDB数据导出中~"); UpDataFileAttribute(path); ExportMDB(MDBpath, path, false); } this.CloseLoading(); MessageHelper.ShowTips("年末库数据导出成功!"); } } catch (Exception ex) { this.CloseLoading(); MessageHelper.ShowError("年末库导出失败!"); LogAPI.Debug(ex.Message); LogAPI.Debug(ex); } } /// /// GDB转换为MDB /// /// MDB绝对路径 /// GDB绝对路径 /// 测试项 private void ExportMDB(string t_targetPath, string s_GDBFullPath, bool isTest = false) { WorkspaceAPI t_WsAPI = null; WorkspaceAPI s_WsAPI = null; IEnumDataset pEnumDataset = null; try { if (File.Exists(t_targetPath)) File.Delete(t_targetPath); s_WsAPI = new WorkspaceAPI(s_GDBFullPath, WorkspaceTypeEnum.GDBFile); //创建mdb CreateFileMDB(t_targetPath, System.IO.Path.GetFileName(t_targetPath)); t_WsAPI = new WorkspaceAPI(t_targetPath, WorkspaceTypeEnum.MDBFile); pEnumDataset = s_WsAPI.CurrentWorkspace.Datasets[esriDatasetType.esriDTFeatureDataset]; if (pEnumDataset != null) { pEnumDataset.Reset(); IDataset pDataset = null; //如果数据集是IFeatureDataset,则遍历它下面的子类 while ((pDataset = pEnumDataset.Next()) != null) { IDataset newDataset = null; newDataset = CreateFeatureDataset(pDataset, t_WsAPI); if (newDataset == null) { continue; } List listFeatureClass = new List(); List listFeatureClassNameList = new List(); IEnumDataset ed = pDataset.Subsets; if (ed != null) { IDataset dataset = ed.Next(); while (dataset != null) { if ((dataset as IFeatureClass) != null) { string featureClassName = dataset.BrowseName; if (!string.IsNullOrWhiteSpace(featureClassName) && !listFeatureClass.Contains(featureClassName)) { listFeatureClass.Add(System.IO.Path.Combine(pDataset.Workspace.PathName.ToString(), pDataset.BrowseName, featureClassName)); listFeatureClassNameList.Add(featureClassName); } } Marshal.FinalReleaseComObject(dataset); dataset = ed.Next(); } } if (listFeatureClass.Count > 0) { //GeoprocessorHelper iGeoprocessorHelper = new GeoprocessorHelper(); InsertMDBFeatureClassFromGDBFeatureClass(s_GDBFullPath, listFeatureClassNameList, t_targetPath, newDataset.BrowseName, null); } if (newDataset != null) Marshal.FinalReleaseComObject(newDataset); } } if (t_WsAPI.ExistFeatureClass("MJHZJCTJB_YJJBNTTB")) t_WsAPI.DeleteFeatureClass("MJHZJCTJB_YJJBNTTB");//删除多余的表结构数据 } catch (Exception ex) { LogAPI.Debug(ex.Message); } finally { if (t_WsAPI != null) { t_WsAPI.CloseWorkspace(); } if (s_WsAPI != null) { s_WsAPI.CloseWorkspace(); } if (pEnumDataset != null) { Marshal.ReleaseComObject(pEnumDataset); } } } /// /// 创建要素集 /// /// /// /// private IFeatureDataset CreateFeatureDataset(IDataset pDataset, WorkspaceAPI workspaceAPI) { try { IFeatureWorkspace targetWorkspac = workspaceAPI.CurrentWorkspace as IFeatureWorkspace; object tst = workspaceAPI.CurrentWorkspace; ISpatialReference SpatialReference = new UnknownCoordinateSystemClass(); IControlPrecision2 pCP = SpatialReference as IControlPrecision2; //要素分辨率 ISpatialReferenceResolution spatialReferenceResolution = SpatialReference as ISpatialReferenceResolution; spatialReferenceResolution.set_XYResolution(false, 0.000005); spatialReferenceResolution.set_ZResolution(false, 0.000005); //要素数据集容差 ISpatialReferenceTolerance spatialReferenceTolerance = SpatialReference as ISpatialReferenceTolerance; double tolerance = 0; if (double.TryParse(SysConfigsOprator.GetAppsetingValueByKey("Tolerance"), out tolerance)) { spatialReferenceTolerance.XYTolerance = tolerance; spatialReferenceTolerance.ZTolerance = tolerance; spatialReferenceTolerance.MTolerance = tolerance; } else { spatialReferenceTolerance.XYTolerance = 0.0001; spatialReferenceTolerance.ZTolerance = 0.0001; spatialReferenceTolerance.MTolerance = 0.0001; } SpatialReference.SetDomain(0, 45035996273.705, 0, 45035996273.705); return targetWorkspac.CreateFeatureDataset("TDDC", SpatialReference); } catch (Exception ex) { LogAPI.Debug("异常原因: " + ex + " ; "); throw ex; } } /// /// 创建FileMDB /// /// 路径名 public static void CreateFileMDB(string fullPath, string gdbName) { string temp = SysAppPath.GetTemplatePath() + "Temp.mdb"; if (File.Exists(temp)) { File.Copy(temp, fullPath); } } /// /// 从GDB中拷贝要素类插入到MDB中 2020-6-1 沈超 /// /// GDB地址 /// GDB中要拷贝的要素类名称列表 /// 要插入到的MDB的地址路径 /// 输出的数据集的名字 /// 输出坐标参考:如果不是要素集下的图层,则要赋坐标参考 public void InsertMDBFeatureClassFromGDBFeatureClass(string sFromGDBPath, List iFeatureClassNameList, string iOutPutMDBPath, string sOutPutDataSetName, string sSpatialReference = null) { IFeatureWorkspace fws_gdb = null; IWorkspace ws_gdb = null; WorkspaceAPI wsapi_gdb = null; IFeatureClassAPI fcapi_gdb = null; IFeatureClass fc_gdb = null; IFeatureClass newFeatureClass = null; IFeatureWorkspace fws_mdb = null; int iAddSuccessCount = 0; int iAddFailCount = 0; bool bFC2FCResult_Temp = false; int iFCToFCType = 0;//fc2fc类型(0不涉及到数据集的方式 1涉及到数据集的方式) try { iFCToFCType = string.IsNullOrWhiteSpace(sOutPutDataSetName) == true ? 0 : 1; if (string.IsNullOrWhiteSpace(sFromGDBPath) == true) { LogAPI.Debug("从GDB中拷贝要素类插入到MDB中 时失败,原因: GDB地址 为空 ; "); throw new Exception("GDB地址 不能为空"); } if (iFeatureClassNameList == null || iFeatureClassNameList.Count <= 0) { LogAPI.Debug("从GDB中拷贝要素类插入到MDB中 时失败,原因: GDB中要拷贝的要素类名称列表 为空 ; "); throw new Exception("要转换的要素类 不能为空"); } IWorkspaceFactory factoryAce = new AccessWorkspaceFactoryClass(); IPropertySet pset = new PropertySetClass(); pset.SetProperty("DATABASE", iOutPutMDBPath); fws_mdb = factoryAce.Open(pset, 0) as IFeatureWorkspace; if (fws_mdb == null) { throw new Exception("MDB地址 不能为空"); } wsapi_gdb = new WorkspaceAPI(sFromGDBPath, WorkspaceTypeEnum.GDBFile); fws_gdb = wsapi_gdb.CurrentWorkspace as IFeatureWorkspace; ws_gdb = fws_gdb as IWorkspace; for (int i = 0; i < iFeatureClassNameList.Count; i++) { fcapi_gdb = wsapi_gdb.OpenFeatureClass(iFeatureClassNameList[i]); if (fcapi_gdb != null) { fc_gdb = fcapi_gdb.FeatureClass; IGeoDataset geoDataset = fc_gdb as IGeoDataset; if (iFCToFCType == 1)//fc2fc类型:1涉及到数据集的方式; { bFC2FCResult_Temp = Fc2Fc(fc_gdb, fws_mdb as IWorkspace, iFeatureClassNameList[i], sOutPutDataSetName, null); #region 处理坐标参考系 IFeatureDataset dataSet = fws_mdb.OpenFeatureDataset(sOutPutDataSetName); ISpatialReference sp = geoDataset.SpatialReference; IGeoDataset pGeoDataset = dataSet as IGeoDataset; IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = pGeoDataset as IGeoDatasetSchemaEdit; if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true) { pGeoDatasetSchemaEdit.AlterSpatialReference(sp); } Marshal.ReleaseComObject(dataSet); Marshal.ReleaseComObject(pGeoDataset); #endregion } else//fc2fc类型:0不涉及到数据集的方式; { bFC2FCResult_Temp = Fc2Fc(fc_gdb, fws_mdb as IWorkspace, iFeatureClassNameList[i], null); #region 处理坐标参考系 newFeatureClass = fws_mdb.OpenFeatureClass(iFeatureClassNameList[i]); IGeoDataset gdataset = newFeatureClass as IGeoDataset; if (gdataset != null) { IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = gdataset as IGeoDatasetSchemaEdit; if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true) { pGeoDatasetSchemaEdit.AlterSpatialReference(geoDataset.SpatialReference); } } #endregion Marshal.ReleaseComObject(gdataset); } Marshal.ReleaseComObject(geoDataset); if (bFC2FCResult_Temp == true) { iAddSuccessCount += 1; } else { iAddFailCount += 1; } } } return; } catch (Exception ex) { LogAPI.Debug("将要素类拷贝至MDB中失败,异常原因: " + ex + " ; "); throw new Exception("GDB转化成MDB地址失败"); } finally { if (fws_gdb != null) { Marshal.ReleaseComObject(fws_gdb); fws_gdb = null; } if (ws_gdb != null) { ws_gdb = null; } if (wsapi_gdb != null) { wsapi_gdb.CloseWorkspace(); } if (fcapi_gdb != null) { fcapi_gdb.CloseFeatureClass(); } if (fc_gdb != null) { Marshal.ReleaseComObject(fc_gdb); fc_gdb = null; } if (fws_mdb != null) { Marshal.ReleaseComObject(fws_mdb); fws_mdb = null; } if (newFeatureClass != null) { Marshal.ReleaseComObject(newFeatureClass); newFeatureClass = null; } } } public bool Fc2Fc(IFeatureClass pInFeatureClass, IWorkspace pOutWorkspace, string poutFatureName, string pOutDataSetName = null, IQueryFilter queryFilter = null) { try { if (pInFeatureClass == null || pOutWorkspace == null || string.IsNullOrEmpty(poutFatureName)) return false; IFeatureClassName pInFeatureclassName; IDataset pIndataset; pIndataset = (IDataset)pInFeatureClass; pInFeatureclassName = (IFeatureClassName)pIndataset.FullName; IFeatureClassName pOutFeatureClassName; IDatasetName pOutDatasetName; IDataset pOutDataset; pOutDataset = (IDataset)pOutWorkspace; IEnumDatasetName dataset = pOutWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset); while ((pOutDatasetName = dataset.Next()) != null) { if (pOutDatasetName.Name == pOutDataSetName) { break; } } pOutFeatureClassName = new FeatureClassNameClass() { Name = poutFatureName }; if (pOutDatasetName == null) { pOutDatasetName = (IDatasetName)pOutFeatureClassName; pOutDatasetName.WorkspaceName = (IWorkspaceName)pOutDataset.FullName; pOutDatasetName.Name = poutFatureName; } pOutFeatureClassName.FeatureDatasetName = pOutDatasetName; IFields pInFields, pOutFields; IFieldChecker pFieldChecker = new FieldCheckerClass(); IEnumFieldError pError; pInFields = pInFeatureClass.Fields; //pOutFields pFieldChecker.Validate(pInFields, out pError, out pOutFields); Console.WriteLine(pError); IFeatureDataConverter one2another = new FeatureDataConverterClass(); try { if (pOutWorkspace.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace) { one2another.ConvertFeatureClass(pInFeatureclassName, null, null, pOutFeatureClassName, null, pOutFields, "", 1000, 0); } else { one2another.ConvertFeatureClass(pInFeatureclassName, queryFilter, pOutDatasetName as IFeatureDatasetName, pOutFeatureClassName, null, pOutFields, "", 1000, 0); } } finally { Marshal.ReleaseComObject(one2another); } return true; } catch (Exception ex) { throw ex; } } /// /// 修改文件的可读写 /// /// private void UpDataFileAttribute(string T_Path) { try { DirectoryInfo dirInfo = new DirectoryInfo(T_Path); dirInfo.Attributes = FileAttributes.Normal | FileAttributes.Directory; FileInfo fileInfo = new FileInfo(T_Path); if (fileInfo.IsReadOnly) { File.SetAttributes(T_Path, FileAttributes.Normal); } } catch (Exception ex) { LogAPI.Debug(ex.Message); } } /// /// 根据区划代码获取县级名称 /// /// /// public string GetXZQDic(string Codes) { IRDBHelper rdbHelper = null; string CodeName = string.Empty; try { string systemPath = SysAppPath.GetDataBasePath() + "System.mdb"; if (File.Exists(systemPath)) { string connStr = SysConfigsOprator.GetDBConnectionByName("MDBOledbConnection"); connStr = string.Format(connStr, systemPath); rdbHelper = RDBFactory.CreateDbHelper(connStr, DatabaseType.MSAccess); string strSQL = "select OBJECTID AS ID, XZQ AS CODE,XZQMC AS NAME from XZQ Where XZQ LIKE '" + Codes + "%'"; DataTable dt = rdbHelper.ExecuteDatatable("Dic", strSQL, true); if (dt != null) { CodeName = dt.Rows[0]["NAME"].ToString(); } } } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (rdbHelper != null) { rdbHelper.DisConnect(); } } return CodeName; } /// /// 是否功能展示 /// public override bool Enabled { get { ProjectInfo pTemp = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo); if (pTemp == null) { return false; } else { if (string.IsNullOrWhiteSpace(pTemp.GetProjFilePath())) { return false; } else { return true; } } } } } }