using System; using System.Collections.Generic; using System.Runtime.InteropServices; 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.EngineEditor; using KGIS.Framework.Maps; using KGIS.Framework.Platform; using KGIS.Framework.Utils; namespace Kingo.Plugin.EngineEditor.Commands.Tools { /// /// 变化图斑提取工具 /// public class CmdExtractFeatureTool : BaseToolCmd { public event EventHandler ExtComplete; //IFeatureLayer m_SourceJCTBLayer; IFeatureLayer m_SourceJCDLTBLayer; IFeatureLayer m_SourceJCTBLayer; IFeatureLayer m_TargetLayer; private IElement tempElement = null; List dlbmDic = null; IPoint MousePoint = null; ITextElement beforeEl = null; public string JCBH { get; set; } public override void OnCreate(object hook) { base.OnCreate(hook); //if (mapService != null) //{ //m_SourceJCTBLayer = mapService.GetFeatureLayerByName("JCTB"); //m_SourceJCDLTBLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTB"); //} } public void SetSourceLayer(IFeatureLayer pLayer) { m_SourceJCTBLayer = pLayer; } public override bool Deactivate() { if (tempElement != null) { try { m_pHookHelper.ActiveView.GraphicsContainer.DeleteElement(tempElement); m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, m_pHookHelper.ActiveView.Extent); } catch (Exception ex) { LogAPI.Debug(ex); } } tempElement = null; base.Deactivate(); this.m_deactivate = true; return this.m_deactivate; } public override void OnClick() { base.OnClick(); m_TargetLayer = (m_pEditor as EngineEditorClass).TargetLayer as IFeatureLayer; //dlbmDic = Platform.Instance.DicHelper.GetDic(DicTypeEnum.DLBM); //if (mapService == null) // mapService = MapsManager.Instance.MapService; //if (mapService != null) //{ //m_SourceJCTBLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTB"); // m_SourceJCDLTBLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("JC_DLTB"); //} } public override bool Enabled { get { if (m_pEditor == null) { return false; } if (m_pEditor.EditState != esriEngineEditState.esriEngineStateEditing) { return false; } if ((m_pEditor as EngineEditorClass).TargetLayer == null) { return false; } return true; } } public override void OnMouseDown(int Button, int Shift, int X, int Y) { } public override void OnMouseMove(int Button, int Shift, int X, int Y) { if (Button != 0) { return; } IPoint point = m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); List SourceFeatures = FeatureAPI.Identify(point, m_SourceJCTBLayer); //if (SourceFeatures.Count == 0) //{ // SourceFeatures = FeatureAPI.Identify(point, m_SourceJCDLTBLayer); //} if (SourceFeatures.Count == 0) return; DrawFeature(SourceFeatures[0]); } public override void Refresh(int hDC) { } public override void OnMouseUp(int Button, int Shift, int X, int Y) { if (Button != 1) { return; } IPoint point = m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); MousePoint = point; List SourceJCDLTBFeatures = FeatureAPI.Identify2(point, m_SourceJCTBLayer); IGeometry sourceGeometry = null; foreach (IFeature item in SourceJCDLTBFeatures) { if (sourceGeometry == null) sourceGeometry = item.ShapeCopy; else sourceGeometry = FeatureAPI.Union(sourceGeometry, item.ShapeCopy); } if (sourceGeometry == null) { sourceGeometry = point; } List TargetFeatures = FeatureAPI.Identify2(sourceGeometry, m_TargetLayer); m_pEditor.StartOperation(); if (TargetFeatures.Count != 0) { //判断图形是否存在且图形是否一致 if (TargetFeatures.Count == 1) { if (FeatureAPI.GetEqual(TargetFeatures[0].ShapeCopy, sourceGeometry)) { return; } } //要分割图斑提取 //TargetGeometry 监测图斑 IGeometry tempTargetGeo = null; foreach (IFeature item in TargetFeatures) { if (tempTargetGeo == null) tempTargetGeo = item.ShapeCopy; else tempTargetGeo = FeatureAPI.Union(tempTargetGeo, item.ShapeCopy); } //SourceGeometry 点选资源图斑 IGeometry tempSourceGeo = sourceGeometry; //相交部分 IGeometry tempInterSectGeo = FeatureAPI.InterSect(tempTargetGeo, tempSourceGeo); if (tempInterSectGeo != null && !tempInterSectGeo.IsEmpty) { //测试 绘制相交部分图形 红色 //DrawBaseGraph(tempInterSectGeo, 255, 0, 0); //原监测图斑减去相交部分 foreach (IFeature TargetFeature in TargetFeatures) { TargetFeature.Shape = FeatureAPI.Difference(TargetFeature.Shape, tempInterSectGeo); if (TargetFeature.Shape.IsEmpty) TargetFeature.Delete(); else TargetFeature.Store(); } //添加相交部分 图斑 AddIntersetFeature(tempInterSectGeo, point); } //添加差异部分 图斑 //测试 绘制差异部分图形 绿色 //DrawBaseGraph(FeatureAPI.Difference(tempSourceGeo, tempInterSectGeo), 0, 255, 0); AddIntersetFeature(FeatureAPI.Difference(tempSourceGeo, tempInterSectGeo), point); //if (MessageHelper.ShowYesNoAndTips("是否要删除已提取图斑?") != System.Windows.Forms.DialogResult.Yes) //{ // return; //} //foreach (var item in TargetFeatures) //{ // item.Delete(); //} ////ExtComplete?.Invoke(null, false); //KGIS.Framework.Platform.Platform.Instance.SendMsg(new KGIS.Framework.Utils.Interface.NotifyMsgPackage() { Content = "RefreshTBBGDetail", MsgType = "RefreshTBBGDetail" }); //return; } else //添加所有source 图斑 { //List SourceJCDLTBFeatures = FeatureAPI.Identify2(point, m_SourceJCTBLayer); if (SourceJCDLTBFeatures.Count == 0) { return; } IFeatureClass m_TargetFC = m_TargetLayer.FeatureClass; IFeatureCursor InsertCursor = m_TargetFC.Insert(true); IFeatureBuffer buffer = m_TargetFC.CreateFeatureBuffer(); //查找监测编号 IFeatureSelection featureS = m_SourceJCTBLayer as IFeatureSelection;//编辑图层 ISelectionSet pSelectionset = featureS.SelectionSet;//目标图层的被选中图斑 ICursor pCursor; pSelectionset.Search(null, true, out pCursor); IFeatureCursor featureCur = pCursor as IFeatureCursor; IFeature pFeature = featureCur.NextFeature(); if (pFeature != null) { JCBH = pFeature.Value[pFeature.Fields.FindField("JCBH")].ToString(); } if (featureCur != null) { Marshal.ReleaseComObject(featureCur); } #region //if (SourceJCDLTBFeatures.Count != 0) //{ // //图形从监测图斑获取 // buffer.Shape = SourceJCTBFeatures[0].ShapeCopy; // //属性从基础地类图斑获取 // for (int i = 0; i < m_TargetFC.Fields.FieldCount; i++) // { // IField field = m_TargetFC.Fields.Field[i]; // if (field.Name.ToUpper() == "OBJECTID" || !field.Editable || field.Name.ToUpper().Contains("SHAPE")) // { // continue; // } // int tarfieldIndex = m_TargetFC.FindField(field.Name); // int sourfieldindex = m_SourceJCDLTBLayer.FeatureClass.FindField(field.Name); // if (tarfieldIndex != -1 && sourfieldindex != -1) // { // buffer.Value[tarfieldIndex] = SourceJCDLTBFeatures[0].Value[sourfieldindex]; // } // } // //获取监测编号 // int bgjcbhIndex = m_TargetFC.FindField("JCBH");//变更监测编号 // int bgtbybhIndex = m_TargetFC.FindField("JCBH");//变更图斑与编号 // int jcbhIndex = m_SourceJCTBLayer.FeatureClass.FindField("JCBH");//监测图斑监测编号 // if (bgjcbhIndex != -1 && jcbhIndex != -1) // { // buffer.Value[bgjcbhIndex] = JCBH;// SourceJCTBFeatures[0].Value[jcbhIndex]; // } // if (bgtbybhIndex != -1 && jcbhIndex != -1) // { // string tbybh = buffer.Value[bgtbybhIndex].ToString(); // if (string.IsNullOrWhiteSpace(tbybh)) // { // buffer.Value[bgtbybhIndex] = SourceJCTBFeatures[0].Value[jcbhIndex]; // } // } // //地类编码从监测图斑的变更地类字段获取 // int dlbmIndex = m_TargetFC.FindField("DLBM"); // int dlmcIndex = m_TargetFC.FindField("DLMC"); // int bgdlIndex = m_SourceJCTBLayer.FeatureClass.FindField("BGDL"); // int jckdlbmIndex = m_SourceJCDLTBLayer.FeatureClass.FindField("DLBM"); // int jckdlmcIndex = m_SourceJCDLTBLayer.FeatureClass.FindField("DLMC"); // if (bgdlIndex != -1 && dlbmIndex != -1) // { // string bgdl = SourceJCTBFeatures[0].Value[bgdlIndex].ToString(); // //变更地类不为空,不包含逗号时,从监测图斑获取 // if (bgdl != "" && !bgdl.Trim().Contains(",") && !bgdl.Contains(",")) // { // buffer.Value[dlbmIndex] = bgdl; // if (dlbmDic != null && dlmcIndex != -1) // { // DataDicTionary dlbm = dlbmDic.FirstOrDefault(f => f.CODE == bgdl); // buffer.Value[dlmcIndex] = dlbm == null ? "" : dlbm.NAME; // } // } // else // { // if (dlbmIndex != -1 && jckdlbmIndex != -1) // { // buffer.Value[dlbmIndex] = SourceJCDLTBFeatures[0].Value[jckdlbmIndex]; // } // if (dlmcIndex != -1 && jckdlmcIndex != -1) // { // buffer.Value[dlmcIndex] = SourceJCDLTBFeatures[0].Value[jckdlmcIndex]; // } // } // } // InsertCursor.InsertFeature(buffer); // InsertCursor.Flush(); // ExtComplete?.Invoke(null, true); //} //else #endregion if (SourceJCDLTBFeatures.Count != 0) { IDataset dataset = m_TargetLayer.FeatureClass as IDataset; //string areapath = SysAppPath.GetAreaUpdateConfigPath(); string mjField = string.Empty; //if (!string.IsNullOrWhiteSpace(areapath) && System.IO.File.Exists(areapath)) // mjField = AreaConfigHelper.GetAreaPropertyByLayerName(dataset.Name); //未获取到监测图斑,图形从基础地类图斑获取 buffer.Shape = SourceJCDLTBFeatures[0].ShapeCopy; //属性从基础地类图斑获取 for (int i = 0; i < m_TargetFC.Fields.FieldCount; i++) { IField field = m_TargetFC.Fields.Field[i]; if (field.Name.ToUpper() == "OBJECTID" || !field.Editable || field.Name.ToUpper().Contains("SHAPE")) { continue; } int tarfieldIndex = m_TargetFC.FindField(field.Name); int sourfieldindex = m_SourceJCTBLayer.FeatureClass.FindField(field.Name); if (tarfieldIndex != -1 && sourfieldindex != -1) { buffer.Value[tarfieldIndex] = SourceJCDLTBFeatures[0].Value[sourfieldindex]; } } int bgjcbhIndex = m_TargetFC.FindField("JCBH"); if (bgjcbhIndex != -1) { buffer.Value[bgjcbhIndex] = JCBH;// SourceJCTBFeatures[0].Value[jcbhIndex]; } if (!string.IsNullOrWhiteSpace(mjField)) { int mjIndex = m_TargetFC.FindField(mjField); if (mjIndex > -1) { IArea area = buffer.Shape as IArea; double mj = area.Area; if (dataset.Name == "LSYD") { mj = mj * 0.0001; } mj = Math.Round(mj, 2); buffer.Value[mjIndex] = mj; } } InsertCursor.InsertFeature(buffer); InsertCursor.Flush(); m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewAll, m_TargetLayer, m_pHookHelper.ActiveView.Extent); //ExtComplete?.Invoke(null, true); KGIS.Framework.Platform.Platform.Instance.SendMsg(new KGIS.Framework.Utils.Interface.NotifyMsgPackage() { Content = "RefreshTBBGDetail", MsgType = "RefreshTBBGDetail" }); } } m_pEditor.StopOperation("提取图形"); } //测试 绘制图形 private IElement DrawBaseGraph(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(); while ((CameraPointElm = MapsManager.Instance.MapService.getAxMapControl().ActiveView.GraphicsContainer.Next()) != null) { if ((CameraPointElm as IElementProperties).Name == "BaseElement") break; } if (CameraPointElm == null) { CameraPointElm = new GroupElementClass(); (CameraPointElm as IElementProperties).Name = "BaseElement"; MapsManager.Instance.MapService.getAxMapControl().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); } MapsManager.Instance.MapService.getAxMapControl().ActiveView.Refresh(); return result; } catch (Exception ex) { LogAPI.Debug("测试绘制图形时异常:" + ex); return null; } } private void AddIntersetFeature(IGeometry shapeGeo, IPoint point) { if (shapeGeo == null || shapeGeo.IsEmpty) return; try { List SourceJCDLTBFeatures = FeatureAPI.Identify2(point, m_SourceJCTBLayer); if (SourceJCDLTBFeatures.Count == 0) { return; } IFeatureClass m_TargetFC = m_TargetLayer.FeatureClass; IFeatureCursor InsertCursor = m_TargetFC.Insert(true); IFeatureBuffer buffer = m_TargetFC.CreateFeatureBuffer(); //查找监测编号 IFeatureSelection featureS = m_SourceJCTBLayer as IFeatureSelection;//编辑图层 ISelectionSet pSelectionset = featureS.SelectionSet;//目标图层的被选中图斑 ICursor pCursor; pSelectionset.Search(null, true, out pCursor); IFeatureCursor featureCur = pCursor as IFeatureCursor; IFeature pFeature = featureCur.NextFeature(); if (pFeature != null) { JCBH = pFeature.Value[pFeature.Fields.FindField("JCBH")].ToString(); } if (featureCur != null) { Marshal.ReleaseComObject(featureCur); } if (SourceJCDLTBFeatures.Count != 0) { IDataset dataset = m_TargetLayer.FeatureClass as IDataset; //string areapath = SysAppPath.GetAreaUpdateConfigPath(); string mjField = string.Empty; //if (!string.IsNullOrWhiteSpace(areapath) && System.IO.File.Exists(areapath)) // mjField = AreaConfigHelper.GetAreaPropertyByLayerName(dataset.Name); //未获取到监测图斑,图形从基础地类图斑获取 buffer.Shape = shapeGeo; //属性从基础地类图斑获取 for (int i = 0; i < m_TargetFC.Fields.FieldCount; i++) { IField field = m_TargetFC.Fields.Field[i]; if (field.Name.ToUpper() == "OBJECTID" || !field.Editable || field.Name.ToUpper().Contains("SHAPE")) { continue; } int tarfieldIndex = m_TargetFC.FindField(field.Name); int sourfieldindex = m_SourceJCTBLayer.FeatureClass.FindField(field.Name); if (tarfieldIndex != -1 && sourfieldindex != -1) { buffer.Value[tarfieldIndex] = SourceJCDLTBFeatures[0].Value[sourfieldindex]; } } int bgjcbhIndex = m_TargetFC.FindField("JCBH"); if (bgjcbhIndex != -1) { buffer.Value[bgjcbhIndex] = JCBH;// SourceJCTBFeatures[0].Value[jcbhIndex]; } if (!string.IsNullOrWhiteSpace(mjField)) { int mjIndex = m_TargetFC.FindField(mjField); if (mjIndex > -1) { IArea area = buffer.Shape as IArea; double mj = area.Area; if (dataset.Name == "LSYD") { mj = mj * 0.0001; } mj = Math.Round(mj, 2); buffer.Value[mjIndex] = mj; } } InsertCursor.InsertFeature(buffer); InsertCursor.Flush(); m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewAll, m_TargetLayer, m_pHookHelper.ActiveView.Extent); //ExtComplete?.Invoke(null, true); KGIS.Framework.Platform.Platform.Instance.SendMsg(new KGIS.Framework.Utils.Interface.NotifyMsgPackage() { Content = "RefreshTBBGDetail", MsgType = "RefreshTBBGDetail" }); } } catch (Exception ex) { LogAPI.Debug("提取图斑时异常:" + ex); return; } } public override void OnKeyDown(int keyCode, int Shift) { } public override void OnKeyUp(int keyCode, int Shift) { } public override void OnDblClick() { } private void DrawFeature(IFeature pFeature) { try { // 获取IRGBColor接口 IRgbColor color = new RgbColor(); // 设置颜色属性 color.Red = 255; color.Green = 0; color.Blue = 0; if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint) { IMarkerElement e = new MarkerElementClass(); IMarkerSymbol symbol = new SimpleMarkerSymbolClass(); symbol.Color = color; symbol.Size = 5; e.Symbol = symbol; (e as IElement).Geometry = pFeature.Shape; m_pHookHelper.ActiveView.GraphicsContainer.AddElement(e as IElement, 0); tempElement = e as IElement; } else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolyline) { // 获取ILine符号接口 ILineSymbol outline = new SimpleLineSymbol(); // 设置线符号属 outline.Width = 2; outline.Color = color; (outline as SimpleLineSymbol).Style = esriSimpleLineStyle.esriSLSSolid; ILineElement pLineElement = new LineElementClass(); pLineElement.Symbol = outline; (pLineElement as IElement).Geometry = pFeature.Shape; m_pHookHelper.ActiveView.GraphicsContainer.AddElement(pLineElement as IElement, 0); tempElement = pLineElement as IElement; } else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon) { if (tempElement == null) { // 获取ILine符号接口 ILineSymbol outline = new SimpleLineSymbol(); // 设置线符号属 outline.Width = 2; outline.Color = color; (outline as SimpleLineSymbol).Style = esriSimpleLineStyle.esriSLSDot; //ESRI.ArcGIS.Display.ILineSymbol ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass(); // 设置填充符号属性 simpleFillSymbol.Outline = outline; simpleFillSymbol.Color = color; simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSHollow; IFillShapeElement pFillShapeElement = new PolygonElementClass(); pFillShapeElement.Symbol = simpleFillSymbol; IElement pElement = pFillShapeElement as IElement; pElement.Geometry = pFeature.Shape; m_pHookHelper.ActiveView.GraphicsContainer.AddElement(pElement, 0); tempElement = pElement; } else { tempElement.Geometry = pFeature.Shape; } } ITextSymbol pTextSymbol = new TextSymbolClass() { Color = color, //Font = pFont, Size = 12 }; //ITextElement pTextElement = new TextElementClass() //{ // Symbol = pTextSymbol, // ScaleText = true, // Text = string.Format("【{0}】 图层:OID = {1}", pFeature.Class.AliasName, pFeature.OID) //}; ////tempFeature = pFeature; //(pTextElement as IElement).Geometry = MousePoint; //m_pHookHelper.ActiveView.GraphicsContainer.AddElement(pTextElement as IElement, 0); //ITextElement pTextElement = new TextElementClass() //{ // Symbol = pTextSymbol, // ScaleText = true, // Text = string.Format("【{0}】 图层:OID = {1}", pFeature.Class.AliasName, pFeature.OID) //}; ////tempFeature = pFeature; //(pTextElement as IElement).Geometry = MousePoint; //if (beforeEl!=null) //{ // try // { // m_pHookHelper.ActiveView.GraphicsContainer.DeleteElement(beforeEl as IElement); // } // catch (Exception) // { // } //} //m_pHookHelper.ActiveView.GraphicsContainer.AddElement(pTextElement as IElement, 0); //beforeEl = pTextElement; //textElement = pTextElement as IElement; m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, m_pHookHelper.ActiveView.Extent); } catch (Exception ex) { LogAPI.Debug(ex); } } } }