using System; using System.Collections.Generic; using System.Text; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Carto; using System.Runtime.InteropServices; using ESRI.ArcGIS.Geometry; using System.IO; using KGIS.Framework.Utils; namespace Kingo.Plugin.MakeTaskPackage.Entity { public class ProcessMXDClass { public ProcessMXDClass() { } ///// ///// 替换mxd数据源 ///// ///// ///// ///// //public bool RestFeatureLayerDataSource(ICustomPath cusPath, List layerNameList) //{ // IFeatureWorkspace toWorkSpace = null; // IMapDocument pMapDocument = null; // try // { // if (cusPath == null || layerNameList == null || layerNameList.Count == 0) return false; // FeatureProcess featureP = new FeatureProcess(); // toWorkSpace = featureP.GetAcessWorkspace(cusPath.mdbPath); // if (toWorkSpace == null) // { // throw new Exception("打开数据空间失败:" + cusPath.mdbPath); // } // pMapDocument = new MapDocumentClass(); // pMapDocument.Open(cusPath.mxdPath); // IMap map = pMapDocument.get_Map(0); // if (map != null) // { // foreach (var layerName in layerNameList) // { // IFeatureLayer fLayer = FindFeatureLayer(map, layerName); // if (fLayer == null) // { // LogAPI.Debug("未找到层:" + layerName); // continue; // } // fLayer.FeatureClass = toWorkSpace.OpenFeatureClass(layerName); // } // pMapDocument.Save(pMapDocument.UsesRelativePaths); // } // return true; // } // catch (Exception ex) // { // throw ex; // } // finally // { // if (toWorkSpace != null) // Marshal.ReleaseComObject(toWorkSpace); // if (pMapDocument != null) // pMapDocument.Close(); // } //} public IFeatureLayer FindFeatureLayer(IMap map, string LayerName) { try { if (map == null || string.IsNullOrEmpty(LayerName)) return null; ILayer layer = null; for (int i = 0; i < map.LayerCount; i++) { layer = GetLayer(map.get_Layer(i), LayerName); if (layer != null) break; } return layer as IFeatureLayer; } catch (Exception ex) { LogAPI.Debug(ex); return null; } } public ILayer FindLayer(IMap map, string LayerName) { try { if (map == null || string.IsNullOrEmpty(LayerName)) return null; ILayer layer = null; for (int i = 0; i < map.LayerCount; i++) { layer = GetLayer(map.get_Layer(i), LayerName); if (layer != null) break; } return layer; } catch (Exception ex) { LogAPI.Debug(ex); return null; } } public int FindLayerIndex(IMap map, string LayerName) { try { if (map == null || string.IsNullOrEmpty(LayerName)) return -1; int layer = -1; for (int i = 0; i < map.LayerCount; i++) { layer = GetLayerIndex(map.get_Layer(i), LayerName, ref i); if (layer != null) break; } return layer; } catch (Exception ex) { LogAPI.Debug(ex); return -1; } } private int GetLayerIndex(ILayer layer, string layername, ref int layerIndex) { try { if (layer is IGroupLayer) { ICompositeLayer pComLayer = layer as ICompositeLayer; for (int j = 0; j < pComLayer.Count; j++) { layerIndex++; int lay = GetLayerIndex(pComLayer.get_Layer(j), layername, ref layerIndex); if (lay > 0) return layerIndex; } } ILayerGeneralProperties generalPro = layer as ILayerGeneralProperties; if (generalPro != null && generalPro.LayerDescription.ToUpper().Equals(layername.ToUpper())) return layerIndex; return -1; } catch (Exception ex) { throw ex; } } public IFeatureLayer FindFeatureLayer1(IMap map, string LayerName) { try { if (map == null || string.IsNullOrEmpty(LayerName)) return null; ILayer layer = null; for (int i = 0; i < map.LayerCount; i++) { layer = GetLayer1(map.get_Layer(i), LayerName); if (layer != null) break; } return layer as IFeatureLayer; } catch (Exception ex) { LogAPI.Debug(ex); return null; } } private ILayer GetLayer1(ILayer layer, string layername) { try { if (layer is IGroupLayer) { ICompositeLayer pComLayer = layer as ICompositeLayer; for (int j = 0; j < pComLayer.Count; j++) { ILayer lay = GetLayer1(pComLayer.get_Layer(j), layername); if (lay != null) return lay; } } if (layer.Name.ToUpper() == layername.ToUpper()) return layer; //ILayerGeneralProperties generalPro = layer as ILayerGeneralProperties; //if (generalPro != null && generalPro.ToUpper().Equals(layername.ToUpper())) // return layer; return null; } catch (Exception ex) { throw ex; } } private ILayer GetLayer(ILayer layer, string layername) { try { if (layer is IGroupLayer) { ICompositeLayer pComLayer = layer as ICompositeLayer; for (int j = 0; j < pComLayer.Count; j++) { ILayer lay = GetLayer(pComLayer.get_Layer(j), layername); if (lay != null) return lay; } } ILayerGeneralProperties generalPro = layer as ILayerGeneralProperties; if (generalPro != null && generalPro.LayerDescription.ToUpper().Equals(layername.ToUpper())) return layer; return null; } catch (Exception ex) { throw ex; } } public static void SetMxdSpatialReference(string mxdPath, ESRI.ArcGIS.Geometry.ISpatialReference spatialReference) { IMapDocument pMapDocument = null; try { pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map != null) { map.SpatialReference = spatialReference; pMapDocument.Save(pMapDocument.UsesRelativePaths); } } catch (Exception ex) { throw ex; } finally { if (pMapDocument != null) pMapDocument.Close(); } } public void SetMXDLayerVisble(string mxdPath, List openList, List closeList) { IMapDocument pMapDocument = null; try { if (openList == null && closeList == null) return; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map != null) { for (int i = 0; i < map.LayerCount; i++) { SetLayerVisble(map.get_Layer(i), openList, closeList); } pMapDocument.Save(pMapDocument.UsesRelativePaths); } } catch (Exception ex) { throw ex; } finally { if (pMapDocument != null) pMapDocument.Close(); } } public void SetMapLayerVisble(IMap map, string layerName, bool isVisible = true) { try { for (int i = 0; i < map.LayerCount; i++) { SetLayerVisble(map.get_Layer(i), layerName, isVisible); } } catch (Exception ex) { throw ex; } } private void SetLayerVisble(ILayer layer, string layerName, bool isVisible = true) { try { if (layer == null) return; if (string.IsNullOrEmpty(layerName)) return; ILayerGeneralProperties generalPro = layer as ILayerGeneralProperties; if (generalPro != null) { string str = generalPro.LayerDescription.Trim(); if (str == layerName) { layer.Visible = isVisible; } else { if (layer is IGroupLayer) { ICompositeLayer pComLayer = layer as ICompositeLayer; if (pComLayer != null && pComLayer.Count > 0) { for (int j = 0; j < pComLayer.Count; j++) { SetLayerVisble(pComLayer.get_Layer(j), layerName, isVisible); } } layer.Visible = isVisible; } else { layer.Visible = false; } } } } catch (Exception ex) { throw ex; } } public void SetMapLayerVisbleNew(IMap map, string layerName, bool isVisible = true) { try { for (int i = 0; i < map.LayerCount; i++) { SetLayerVisbleNew(map.get_Layer(i), layerName, isVisible); } } catch (Exception ex) { throw ex; } } private void SetLayerVisbleNew(ILayer layer, string layerName, bool isVisible = true) { try { if (layer == null) return; if (string.IsNullOrEmpty(layerName)) return; ILayerGeneralProperties generalPro = layer as ILayerGeneralProperties; if (generalPro != null) { string str = generalPro.LayerDescription.Trim(); if (str == layerName) { layer.Visible = isVisible; } else { if (layer is IGroupLayer) { ICompositeLayer pComLayer = layer as ICompositeLayer; if (pComLayer != null && pComLayer.Count > 0) { for (int j = 0; j < pComLayer.Count; j++) { SetLayerVisbleNew(pComLayer.get_Layer(j), layerName, isVisible); } } } } } } catch (Exception ex) { throw ex; } } private void SetLayerVisble(ILayer layer, List open, List close) { try { if (layer == null) return; if (open == null && close == null) return; ILayerGeneralProperties generalPro = layer as ILayerGeneralProperties; if (generalPro != null) { string str = generalPro.LayerDescription.Trim(); if (open != null && open.Contains(str)) { layer.Visible = true; } else if (close != null && close.Contains(str)) { layer.Visible = false; } else { if (layer is IGroupLayer) { ICompositeLayer pComLayer = layer as ICompositeLayer; if (pComLayer != null && pComLayer.Count > 0) { for (int j = 0; j < pComLayer.Count; j++) { SetLayerVisble(pComLayer.get_Layer(j), open, close); } } } } } } catch (Exception ex) { throw ex; } } private ILayer GetLayerGroup(ILayer layer, string layername) { try { if (layer is IGroupLayer) { ICompositeLayer pComLayer = layer as ICompositeLayer; for (int j = 0; j < pComLayer.Count; j++) { ILayer lay = GetLayer(pComLayer.get_Layer(j), layername); if (lay != null) return lay; } ILayerGeneralProperties generalPro = layer as ILayerGeneralProperties; if (generalPro != null && generalPro.LayerDescription.ToUpper().Equals(layername.ToUpper())) return layer; } return null; } catch (Exception ex) { throw ex; } } public IGroupLayer FindLayerGroup(IMap map, string layerName) { try { if (map == null || string.IsNullOrEmpty(layerName)) return null; ILayer layer = null; for (int i = 0; i < map.LayerCount; i++) { layer = GetLayerGroup(map.get_Layer(i), layerName); if (layer != null) break; } return layer == null ? null : layer as IGroupLayer; } catch (Exception ex) { LogAPI.Debug(ex); return null; } } public void UpdateMxdLayer(string mxdPath, string layerName, ILayer layerNew) { IMapDocument pMapDocument = null; try { if (!File.Exists(mxdPath)) return; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map == null || map.LayerCount == 0) return; ILayer layer = null; for (int i = 0; i < map.LayerCount; i++) { layer = GetLayer(map.get_Layer(i), layerName); if (layer != null) { layer = layerNew; break; } } pMapDocument.Save(); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (pMapDocument != null) pMapDocument.Close(); } } public ILayer FindLayer(string mxdPath, string layerName) { IMapDocument pMapDocument = null; try { if (!File.Exists(mxdPath)) return null; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map == null || map.LayerCount == 0) return null; ILayer layer = null; for (int i = 0; i < map.LayerCount; i++) { layer = GetLayer(map.get_Layer(i), layerName); if (layer != null) break; } return layer; } catch (Exception ex) { LogAPI.Debug(ex); return null; } finally { if (pMapDocument != null) pMapDocument.Close(); } } public void UpdateLayerName(string mxdPath, string layerName, string layerNewName) { IMapDocument pMapDocument = null; try { if (!File.Exists(mxdPath)) return; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map == null || map.LayerCount == 0) return; ILayer layer = null; for (int i = 0; i < map.LayerCount; i++) { layer = GetLayer(map.get_Layer(i), layerName); if (layer != null) { (layer as IFeatureLayer).Name = layerNewName; break; } } pMapDocument.Save(); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (pMapDocument != null) pMapDocument.Close(); } } public void UpdateLayerVisble(string mxdPath, string layerName, bool isVisble) { IMapDocument pMapDocument = null; try { if (!File.Exists(mxdPath)) return; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map == null || map.LayerCount == 0) return; ILayer layer = null; for (int i = 0; i < map.LayerCount; i++) { layer = GetLayer(map.get_Layer(i), layerName); if (layer != null) { (layer as IFeatureLayer).Visible = isVisble; break; } } pMapDocument.Save(); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (pMapDocument != null) pMapDocument.Close(); } } public void UpdateGroupLayerVisble(string mxdPath, string layerName, bool isVisble) { IMapDocument pMapDocument = null; try { if (!File.Exists(mxdPath)) return; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map == null || map.LayerCount == 0) return; ILayer layer = FindLayerGroup(map, layerName); (layer as IGroupLayer).Visible = isVisble; pMapDocument.Save(); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (pMapDocument != null) pMapDocument.Close(); } } public void UpdateGroupLayerName(string mxdPath, string layerName, string layerNewName) { IMapDocument pMapDocument = null; try { if (!File.Exists(mxdPath)) return; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map == null || map.LayerCount == 0) return; ILayer layer = FindLayerGroup(map, layerName); (layer as IGroupLayer).Name = layerNewName; pMapDocument.Save(); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (pMapDocument != null) pMapDocument.Close(); } } public void DelGroupLayer(IMap map, string layerName) { IGroupLayer groupLayer = FindLayerGroup(map, layerName); if (groupLayer == null) return; map.DeleteLayer(groupLayer); } public void DelLayer(IMap map, string layerName) { ILayer layer = FindLayer(map, layerName); if (layer == null) return; map.DeleteLayer(layer); } public void DelGroupLayer(string mxdPath, string layerName) { IMapDocument pMapDocument = null; try { if (!File.Exists(mxdPath)) return; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map == null || map.LayerCount == 0) return; ILayer layer = FindLayerGroup(map, layerName); if (layer != null) map.DeleteLayer(layer); pMapDocument.Save(); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (pMapDocument != null) pMapDocument.Close(); } } public void DelLayer(string mxdPath, string layerName) { IMapDocument pMapDocument = null; try { if (!File.Exists(mxdPath)) return; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); IMap map = pMapDocument.get_Map(0); if (map == null || map.LayerCount == 0) return; ILayer layer = FindLayerGroup(map, layerName); if (layer != null) map.DeleteLayer(layer); pMapDocument.Save(); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (pMapDocument != null) pMapDocument.Close(); } } /// /// 获取map的外界范围 /// /// /// public IEnvelope GetMapEnvelope(string mxdPath) { IMapDocument pMapDocument = null; try { if (!File.Exists(mxdPath)) return null; pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath); return pMapDocument.ActiveView.Extent; //IMap map = pMapDocument.get_Map(0); //if (map == null || map.LayerCount == 0) return null; //IEnvelope enve = null; //for (int i = 0; i < map.LayerCount; i++) //{ // GetIEnvelope(map.get_Layer(i), ref enve); //} //return enve; } catch (Exception ex) { LogAPI.Debug(ex); return null; } finally { if (pMapDocument != null) pMapDocument.Close(); } } private void GetIEnvelope(ILayer layer, ref IEnvelope enve) { try { if (layer == null) return; if (layer is IGroupLayer) { ICompositeLayer pComLayer = layer as ICompositeLayer; if (pComLayer != null && pComLayer.Count > 0) { for (int j = 0; j < pComLayer.Count; j++) { GetIEnvelope(pComLayer.get_Layer(j), ref enve); } } } else { if (layer != null && layer.Valid) { IEnvelope env = layer.AreaOfInterest; if (enve == null) { enve = env; } else { if (env != null) { //if (env.XMin < 0 || env.YMin < 0) return; //if (env.XMax > enve.XMax) //{ // enve.XMax = env.XMax; //} //if (env.YMax > enve.YMax) //{ // enve.YMax = env.YMax; //} //if (env.XMin < enve.XMin) //{ // enve.XMin = enve.XMin; //} //if (env.YMin < enve.YMin) //{ // enve.YMin = env.YMin; //} enve.Union(env); } } } } } catch (Exception ex) { throw ex; } } public void SetMapLayerVisble(IMap map, List openList, List closeList) { try { if (map == null) return; if (openList == null && closeList == null) return; for (int i = 0; i < map.LayerCount; i++) { SetLayerVisble(map.get_Layer(i), openList, closeList); } } catch (Exception ex) { throw ex; } finally { } } public static bool RoomToMap(IMap pMap) { try { if (pMap == null) { return false; } IActiveView pActiveView = pMap as IActiveView; double minValue = double.MinValue; double maxValue = double.MaxValue; double dlYMax = double.MinValue; double dlYMin = double.MaxValue; ILayer pLayer = null; //IFeatureLayer layer2 = null; for (int i = 0; i < pMap.LayerCount; i++) { pLayer = pMap.get_Layer(i); //layer2 = pLayer as IFeatureLayer; //if ((layer2 != null) && (layer2.FeatureClass != null)) //{ // string str = TrimUserName((layer2.FeatureClass as IDataset).BrowseName); // if (((str == DBConst.TB_LSD) || (str == DBConst.TB_LSX)) || (str == DBConst.TB_LSM)) // { // continue; // } //} if (pLayer is ICompositeLayer) { GetCompositeLayerSideCoordinate(pLayer as ICompositeLayer, ref minValue, ref maxValue, ref dlYMax, ref dlYMin); } else { GetLayerSideCoordinate(pLayer, ref minValue, ref maxValue, ref dlYMax, ref dlYMin); } } if ((((minValue == double.MinValue) && (maxValue == double.MaxValue)) && (dlYMax == double.MinValue)) && (dlYMin == double.MaxValue)) { pActiveView.Extent = pActiveView.FullExtent; PartialRefresh(pActiveView); } else { IEnvelope pGeometry = new EnvelopeClass();//GetEnvByCoor(minValue, maxValue, dlYMax, dlYMin); pGeometry.XMax = minValue; pGeometry.XMin = maxValue; pGeometry.YMax = dlYMax; pGeometry.YMin = dlYMin; LocateGeometry(pActiveView, pGeometry, 0.2); } return true; } catch (Exception ex) { LogAPI.Debug(ex); return false; } finally { } } public static bool LocateGeometry(IActiveView pActiveView, IGeometry pGeometry, double dblRatio) { try { if ((pGeometry == null) || (pActiveView == null)) { return false; } if (pGeometry.IsEmpty) { return false; } if ((pGeometry is IPoint) || (pGeometry is IMultipoint)) { ITopologicalOperator @operator = pGeometry as ITopologicalOperator; @operator.Simplify(); pGeometry = @operator.Buffer(100.0); } IEnvelope envelope = pGeometry.Envelope; IPoint p = new PointClass(); if (envelope != null) { p.X = (envelope.XMin + envelope.XMax) / 2.0; p.Y = (envelope.YMin + envelope.YMax) / 2.0; } double num = pActiveView.Extent.Width / pActiveView.Extent.Height; double num2 = envelope.Width / envelope.Height; if (num > num2) { envelope.Width = envelope.Height * num; } else { envelope.Height = envelope.Width / num; } envelope.Expand(1.0 + dblRatio, 1.0 + dblRatio, true); envelope.CenterAt(p); if (envelope.SpatialReference != pActiveView.FocusMap.SpatialReference) { envelope.Project(pActiveView.FocusMap.SpatialReference); } pActiveView.Extent = envelope; PartialRefresh(pActiveView); pActiveView.ScreenDisplay.UpdateWindow(); return true; } catch (Exception) { return false; } } public static bool PartialRefresh(IActiveView pActiveView) { if (pActiveView == null) { return false; } PartialRefresh(pActiveView, esriViewDrawPhase.esriViewForeground | esriViewDrawPhase.esriViewGraphicSelection | esriViewDrawPhase.esriViewGraphics | esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGeography | esriViewDrawPhase.esriViewBackground); return true; } public static bool PartialRefresh(IActiveView pActiveView, esriViewDrawPhase enumViewDrawPhase) { if (pActiveView == null) { return false; } pActiveView.PartialRefresh(enumViewDrawPhase, null, pActiveView.Extent); return true; } private static bool GetCompositeLayerSideCoordinate(ICompositeLayer pCompositeLayer, ref double dlXMax, ref double dlXMin, ref double dlYMax, ref double dlYMin) { if (pCompositeLayer == null) { return false; } for (int i = 0; i < pCompositeLayer.Count; i++) { ILayer pLayer = pCompositeLayer.get_Layer(i); if (pLayer is ICompositeLayer) { if (!GetCompositeLayerSideCoordinate(pLayer as ICompositeLayer, ref dlXMax, ref dlXMin, ref dlYMax, ref dlYMin)) { return false; } } else if (!GetLayerSideCoordinate(pLayer, ref dlXMax, ref dlXMin, ref dlYMax, ref dlYMin)) { return false; } } return true; } private static bool GetLayerSideCoordinate(ILayer pLayer, ref double dlXMax, ref double dlXMin, ref double dlYMax, ref double dlYMin) { if (pLayer == null || !pLayer.Valid) { return false; } if (pLayer is ITable) { try { if ((pLayer as ITable).RowCount(null) == 0) { return true; } } catch (Exception) { } } IEnvelope areaOfInterest = pLayer.AreaOfInterest; if (areaOfInterest != null) { dlXMax = Math.Max(dlXMax, areaOfInterest.XMax); dlXMin = Math.Min(dlXMin, areaOfInterest.XMin); dlYMax = Math.Max(dlYMax, areaOfInterest.YMax); dlYMin = Math.Min(dlYMin, areaOfInterest.YMin); } return true; } } }