using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using KGIS.Framework.AE; using KGIS.Framework.Maps; using KGIS.Framework.Utils; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using UIShell.OSGi; namespace Kingo.PluginServiceInterface { public class ShowMaskHelper { public AxMapControl axMapControl; private OutLine outLine; public static IGeometry outLineGeomery; public void ShowAllMask(AxMapControl mulitAxMapControl, IGeometry currentGeo) { ShowMask(currentGeo); axMapControl = mulitAxMapControl; //IUcMulitMapControlHelper ucMulitMapControlHelper = BundleRuntime.Instance.GetFirstOrDefaultService(); //axMapControl = ucMulitMapControlHelper.GetUcMapControl(); ShowMask(currentGeo); } public void ShowBorderMask() { IFeatureCursor pFeatureCursor = null; IFeature f = null; IEnvelope currentFeatureEnvelope = null; IWorkspaceAPI s_WsAPI = null; IFeatureClassAPI featureClassAPI = null; try { axMapControl.ActiveView.GraphicsContainer.DeleteAllElements(); outLine = new OutLine(); InitOutLine(); outLineGeomery = GetGeometry(outLine); DrawBaseGraph(true, outLineGeomery, 150, 150, 150); axMapControl.ActiveView.Extent = outLineGeomery.Envelope; axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, outLineGeomery.Envelope); } catch (Exception ex) { LogAPI.Debug("添加遮罩失败!" + ex); return; } finally { if (f != null) Marshal.ReleaseComObject(f); if (pFeatureCursor != null) Marshal.ReleaseComObject(pFeatureCursor); if (currentFeatureEnvelope != null) Marshal.ReleaseComObject(currentFeatureEnvelope); if (s_WsAPI != null) s_WsAPI.CloseWorkspace(); if (featureClassAPI != null) { featureClassAPI.CloseFeatureClass(); featureClassAPI = null; } GC.Collect(); } } public void ShowMask(IGeometry currentGeo, bool isUpdateView = true) { if (axMapControl == null) return; IFeatureCursor pFeatureCursor = null; IFeature f = null; IEnvelope currentFeatureEnvelope = null; IWorkspaceAPI s_WsAPI = null; IFeatureClassAPI featureClassAPI = null; try { outLine = new OutLine(); InitOutLine(); outLineGeomery = GetGeometry(outLine); if (outLineGeomery == null) return; axMapControl.ActiveView.GraphicsContainer.DeleteAllElements(); currentFeatureEnvelope = currentGeo.Envelope; double xmin = currentFeatureEnvelope.XMin; double xmax = currentFeatureEnvelope.XMax; double ymin = currentFeatureEnvelope.YMin; double ymax = currentFeatureEnvelope.YMax; currentFeatureEnvelope.XMin = xmin - (xmax - xmin) / 2.0; currentFeatureEnvelope.XMax = xmax + (xmax - xmin) / 2.0; currentFeatureEnvelope.YMin = ymin - (ymax - ymin) / 2.0; currentFeatureEnvelope.YMax = ymax + (ymax - ymin) / 2.0; IPointCollection poly = new PolygonClass(); poly.AddPoint(new PointClass() { X = currentFeatureEnvelope.XMin, Y = currentFeatureEnvelope.YMax }); poly.AddPoint(new PointClass() { X = currentFeatureEnvelope.XMax, Y = currentFeatureEnvelope.YMax }); poly.AddPoint(new PointClass() { X = currentFeatureEnvelope.XMax, Y = currentFeatureEnvelope.YMin }); poly.AddPoint(new PointClass() { X = currentFeatureEnvelope.XMin, Y = currentFeatureEnvelope.YMin }); poly.AddPoint(new PointClass() { X = currentFeatureEnvelope.XMin, Y = currentFeatureEnvelope.YMax }); IPolygon pPolygon = (IPolygon)poly; IGeometry resultGeometry = FeatureAPI.Difference(outLineGeomery, pPolygon); DrawBaseGraph(isUpdateView, resultGeometry, 150, 150, 150); if(isUpdateView) { axMapControl.ActiveView.Extent = currentFeatureEnvelope; axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, currentFeatureEnvelope); } } catch (Exception ex) { LogAPI.Debug("添加遮罩失败!" + ex); return; } finally { if (f != null) Marshal.ReleaseComObject(f); if (pFeatureCursor != null) Marshal.ReleaseComObject(pFeatureCursor); if (currentFeatureEnvelope != null) Marshal.ReleaseComObject(currentFeatureEnvelope); if (s_WsAPI != null) s_WsAPI.CloseWorkspace(); if (featureClassAPI != null) { featureClassAPI.CloseFeatureClass(); featureClassAPI = null; } GC.Collect(); } } private IElement DrawBaseGraph(bool isUpdateView, IGeometry geoDraw, int R = 255, int G = 0, int B = 0) { if (geoDraw == null) return null; try { IElement result = null; IGraphicsContainer iGraphicsContainer = null; IElement CameraPointElm = null; // axMapControl.ActiveView.GraphicsContainer.Next(); if (isUpdateView) { while ((CameraPointElm = axMapControl.ActiveView.GraphicsContainer.Next()) != null) { if ((CameraPointElm as IElementProperties).Name == "BaseElement") break; } } if (CameraPointElm == null) { CameraPointElm = new GroupElementClass(); (CameraPointElm as IElementProperties).Name = "BaseElement"; axMapControl.ActiveView.GraphicsContainer.AddElement(CameraPointElm, 2); } (CameraPointElm as IGroupElement).ClearElements(); //替换Env改动---无用 //IMapControl2 axMapDraw = Env.Instance.KMap.HookHelper.Hook as IMapControl2; // 获取IRGBColor接口 IRgbColor color = new RgbColor(); // 设置颜色属性 color.Red = R; color.Green = G; color.Blue = B; switch (geoDraw.GeometryType) { case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint: result = new MarkerElementClass(); IMarkerSymbol symbol = new SimpleMarkerSymbolClass(); symbol.Color = color; symbol.Size = 10; (result as IMarkerElement).Symbol = symbol; result.Geometry = geoDraw; break; case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon: result = new KGIS.Framework.Maps.PolygonElement() { Opacity = 100 }; // 获取ILine符号接口 ILineSymbol lineSymbol = new SimpleLineSymbol(); // 设置线符号属 lineSymbol.Width = 2; ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass(); simpleFillSymbol.Color = color; IRgbColor colorLine = new RgbColor(); colorLine.Red = 0; colorLine.Green = 0; colorLine.Blue = 0; lineSymbol.Color = colorLine; // 设置填充符号属性 simpleFillSymbol.Outline = lineSymbol; simpleFillSymbol.Style = ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSSolid;//esriSimpleFillStyle.esriSFSSolid; (result as IFillShapeElement).Symbol = simpleFillSymbol; result.Geometry = geoDraw; break; case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline: result = new LineElementClass(); // 获取ILine符号接口 ILineSymbol outline = new SimpleLineSymbol(); // 设置线符号属 outline.Width = 3; outline.Color = color; (outline as SimpleLineSymbol).Style = ESRI.ArcGIS.Display.esriSimpleLineStyle.esriSLSSolid; (result as ILineElement).Symbol = outline; result.Geometry = geoDraw; break; default: break; } (CameraPointElm as IGroupElement).AddElement(result); if (iGraphicsContainer != null) { iGraphicsContainer.AddElement(result, 0); } axMapControl.ActiveView.Refresh(); return result; } catch (Exception ex) { LogAPI.Debug("执行绘制遮罩方法时异常:" + ex); return null; } } private IGeometry GetGeometry(OutLine reg) { if (reg == null) return null; ArrayList result = new ArrayList(); try { result.Add((byte)1);//顺序 result.AddRange(BitConverter.GetBytes(3));//类型 面 result.AddRange(BitConverter.GetBytes(1));//环数 ArrayList jmdArray = new ArrayList(); jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinX)); jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinY)); //上边 jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinX)); jmdArray.AddRange(BitConverter.GetBytes((double)reg.MaxY)); //右边 jmdArray.AddRange(BitConverter.GetBytes((double)reg.MaxX)); jmdArray.AddRange(BitConverter.GetBytes((double)reg.MaxY)); //下边 jmdArray.AddRange(BitConverter.GetBytes((double)reg.MaxX)); jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinY)); //首尾相连 jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinX)); jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinY)); result.AddRange(BitConverter.GetBytes(jmdArray.Count / 16)); result.AddRange(jmdArray); byte[] wkb = (byte[])result.ToArray(typeof(byte)); IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3; IGeometry geom; int countin = wkb.GetLength(0); factory.CreateGeometryFromWkbVariant(wkb, out geom, out countin); if ((geom as IArea).Area < 0) { IPolygon pPolygon = geom as IPolygon; pPolygon.ReverseOrientation(); } if (geom == null) return null; return geom; } catch (Exception ex) { LogAPI.Debug("获取分幅后的图斑形状异常:" + ex); return null; } } private void InitOutLine() { IEnvelope CurrentEnvelope = null; List MapLayerList = MapsManager.Instance.MapService.GetAllLayerInMap(); List SelectLayerList = new List(); List geometries = new List(); try { foreach (ILayer item in MapLayerList) { if ((item as RasterLayer) != null) { SelectLayerList.Add(item); //确保最大范围 if (CurrentEnvelope == null) CurrentEnvelope = item.AreaOfInterest; else CurrentEnvelope.Union(item.AreaOfInterest); } else if ((item as KOTilesLayer) != null) { SelectLayerList.Add(item); //确保最大范围 if (CurrentEnvelope == null) CurrentEnvelope = item.AreaOfInterest; else CurrentEnvelope.Union(item.AreaOfInterest); } } if (SelectLayerList.Count == 0) { foreach (ILayer item in MapLayerList) { if ((item as IFeatureLayer) != null) { SelectLayerList.Add(item); //确保最大范围 if (CurrentEnvelope == null) CurrentEnvelope = item.AreaOfInterest; else { IEnvelope env = item.AreaOfInterest; //if (env != null) // CurrentEnvelope.Union(env); if (env.XMin < 0 || env.YMin < 0) continue; if (env.XMax > CurrentEnvelope.XMax) { CurrentEnvelope.XMax = env.XMax; } if (env.YMax > CurrentEnvelope.YMax) { CurrentEnvelope.YMax = env.YMax; } if (env.XMin < CurrentEnvelope.XMin) { CurrentEnvelope.XMin = env.XMin; } if (env.YMin < CurrentEnvelope.YMin) { CurrentEnvelope.YMin = env.YMin; } } } } } IEnvelope envelope = CurrentEnvelope; if (envelope == null) return; outLine.MinX = envelope.XMin < 0 ? 0 : envelope.XMin; outLine.MaxX = envelope.XMax; outLine.MinY = envelope.YMin < 0 ? 0 : envelope.YMin; outLine.MaxY = envelope.YMax; } catch (Exception ex) { LogAPI.Debug("初始化作业区生成页面值失败:" + ex); return; } } } public class OutLine { public double MaxX { get; set; } public double MaxY { get; set; } public double MinX { get; set; } public double MinY { get; set; } } }