using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using DevExpress.Xpf.Grid;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using KGIS.Framework.DBOperator;
using KGIS.Framework.Maps;
using KGIS.Framework.Platform;
using KGIS.Framework.Utils;
using KGIS.Framework.Views;
using KGIS.Framework.Utils.Helper;
using KGIS.Framework.Utils.Interface;
using KGIS.Framework.AE;
using ReactiveUI;
using System.Runtime.InteropServices;
namespace Kingo.Plugin.ResultsOfProof.ViewModel
{
public class JZTBXXBRelation
{
public int OID { get; set; }
public string BSM { get; set; }
///
/// 地类编码
///
public string DLBM { get; set; }
///
/// 地类名称
///
public string DLMC { get; set; }
///
/// 编辑状态
///
public bool EditState { get; set; }
public bool ISGXBSM { get; set; }
}
public class BGJZTBXXBRelationViewModel : ReactiveObject
{
public string JCBH { get; set; }
public string BGDL { get; set; }
private string _BtnContent = "图上选取图斑标识码";
public string btnContent { get { return _BtnContent; } set { this.RaiseAndSetIfChanged(ref _BtnContent, value); } }
public DataTable dltbData { get; set; }
///
/// 界面Grid绑定数据
///
private BindingList items = null;
public BindingList Items { get { return items; } set { this.RaiseAndSetIfChanged(ref items, value); } }
public BindingList ItemsCopyInit { get; set; }
public JZTBXXBRelation currentItem = null;
public JZTBXXBRelation CurrentItem { get { return currentItem; } set { this.RaiseAndSetIfChanged(ref currentItem, value); } }
public ReactiveCommand AddFJCommand { get; private set; }
public ReactiveCommand DeleteRowCommand { get; private set; }
public ReactiveCommand ConirmCommand { get; set; }
public ReactiveCommand CancelCommand { get; set; }
public ReactiveCommand NextDataCommand { get; private set; }
public ReactiveCommand PrevDataCommand { get; private set; }
public ReactiveCommand GC_MouseLeftButtonUpCmd { get; private set; }
private IFeatureLayer JZTB_FC = null;
private IFeatureLayer DLTB_FC = null;
private IFeatureLayer DLTBGX_FC = null;
private object Hook = null;
private IMapControl4 m_mapCtrl;
private bool IsStartAddFJ = false;
private int OID { get; set; }
public BGJZTBXXBRelationViewModel(int pOID, object pHook = null)
{
try
{
Hook = pHook;
m_mapCtrl = (IMapControl4)Hook;
CreateCommand();
if (pOID == 0) return;
OID = pOID;
JZTB_FC = MapsManager.Instance.MapService.GetFeatureLayerByName("JCTB");
DLTB_FC = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑");
DLTBGX_FC = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBGX");
}
catch (Exception ex)
{
LogAPI.Debug("人工校验失败:" + ex.Message);
MessageHelper.ShowError(ex.Message);
}
finally
{
//if (cursor != null)
//{
// Marshal.ReleaseComObject(cursor);
//}
}
}
public void SetData(int pOID)
{
ICursor cursor = null;
try
{
if (pOID == 0) return;
OID = pOID;
IFeature feature = JZTB_FC.FeatureClass.GetFeature(pOID);
//获取举证图斑预编号
//JCBH = feature.Value[feature.Fields.FindField("JCBH")].ToString();
//BGDL = feature.Value[feature.Fields.FindField("BGDL")].ToString();
JCBH = feature.Value[feature.Fields.FindField("JCBH")].ToString();
int idx = feature.Fields.FindField("BGDL");
if (idx != -1)
BGDL = feature.Value[feature.Fields.FindField("BGDL")].ToString();
//获取举证图斑关联建库标识码
string JKBSM = feature.Value[feature.Fields.FindField("DYTBBSM")].ToString();
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = string.Format("BSM in ('{0}')", JKBSM.Replace(",", "','"));
Items = new BindingList();
ItemsCopyInit = new BindingList();
int objidIndex = DLTBGX_FC.FeatureClass.Fields.FindField("OBJECTID");
int bsmIndex = DLTBGX_FC.FeatureClass.Fields.FindField("BSM");
int dltbIndex = DLTBGX_FC.FeatureClass.Fields.FindField("DLBM");
int dlmcIndex = DLTBGX_FC.FeatureClass.Fields.FindField("DLMC");
cursor = (DLTBGX_FC.FeatureClass as ITable).Search(queryFilter, true);
IRow row = null;
while ((row = cursor.NextRow()) != null)
{
JZTBXXBRelation jztbxxb = new JZTBXXBRelation()
{
OID = row.get_Value(objidIndex) == null ? 0 : (int)row.get_Value(objidIndex),
BSM = row.get_Value(bsmIndex) == null ? "" : row.get_Value(bsmIndex).ToString(),
DLBM = row.get_Value(dltbIndex) == null ? "" : row.get_Value(dltbIndex).ToString(),
DLMC = row.get_Value(dlmcIndex) == null ? "" : row.get_Value(dlmcIndex).ToString(),
ISGXBSM = true
};
Items.Add(jztbxxb);
ItemsCopyInit.Add(jztbxxb);
Marshal.ReleaseComObject(row);
}
int jcobjidIndex = DLTB_FC.FeatureClass.Fields.FindField("OBJECTID");
int jcbsmIndex = DLTB_FC.FeatureClass.Fields.FindField("BSM");
int jcdltbIndex = DLTB_FC.FeatureClass.Fields.FindField("DLBM");
int jcdlmcIndex = DLTB_FC.FeatureClass.Fields.FindField("DLMC");
cursor = (DLTB_FC.FeatureClass as ITable).Search(queryFilter, true);
IRow jcrow = null;
while ((jcrow = cursor.NextRow()) != null)
{
string bsm = jcrow.get_Value(bsmIndex) == null ? "" : jcrow.get_Value(bsmIndex).ToString();
var list = items.Where(x => x.BSM == bsm).ToList();
if (list != null && list.Count != 0)
{
continue;
}
JZTBXXBRelation jztbxxb = new JZTBXXBRelation()
{
OID = jcrow.get_Value(jcobjidIndex) == null ? 0 : (int)jcrow.get_Value(jcobjidIndex),
BSM = jcrow.get_Value(jcbsmIndex) == null ? "" : jcrow.get_Value(jcbsmIndex).ToString(),
DLBM = jcrow.get_Value(jcdltbIndex) == null ? "" : jcrow.get_Value(jcdltbIndex).ToString(),
DLMC = jcrow.get_Value(jcdlmcIndex) == null ? "" : jcrow.get_Value(jcdlmcIndex).ToString(),
ISGXBSM = false
};
Items.Add(jztbxxb);
ItemsCopyInit.Add(jztbxxb);
Marshal.ReleaseComObject(jcrow);
}
}
catch (Exception ex)
{
LogAPI.Debug("人工校验失败:" + ex.Message);
MessageHelper.ShowError(ex.Message);
}
finally
{
if (cursor != null)
{
Marshal.ReleaseComObject(cursor);
}
}
}
private void CreateCommand()
{
try
{
DrawParams param = new DrawParams();
param.enumDrawMode = EnumDrawMode.DrawRect;
CustomDrawTool customDraw = new CustomDrawTool(param);
customDraw.DoContinue += CustomDraw_DoContinue1;
customDraw.OnCreate(m_mapCtrl.Object);
NextDataCommand = ReactiveCommand.Create();
NextDataCommand.Subscribe(_ =>
{
//通知内业会审列表视图进行显示下一条数据
Platform.Instance.SendMsg(new NotifyMsgPackage() { Content = "NextData", MsgType = "JZTBXXBRelationView" });
});
PrevDataCommand = ReactiveCommand.Create();
PrevDataCommand.Subscribe(_ =>
{
//通知内业会审列表视图进行显示上一条数据
Platform.Instance.SendMsg(new NotifyMsgPackage() { Content = "PrevData", MsgType = "JZTBXXBRelationView" });
});
AddFJCommand = ReactiveCommand.Create();
AddFJCommand.Subscribe(x =>
{
if (IsStartAddFJ)
{
IsStartAddFJ = !IsStartAddFJ;
m_mapCtrl.CurrentTool = null;
btnContent = "图上选取图斑标识码";
return;
}
else
{
IsStartAddFJ = !IsStartAddFJ;
m_mapCtrl.CurrentTool = customDraw;
btnContent = "停止选取";
}
});
DeleteRowCommand = ReactiveCommand.Create();
DeleteRowCommand.Subscribe(x =>
{
if (CurrentItem != null)
{
string bsm = CurrentItem.BSM;
JZTBXXBRelation tbfj = Items.ToList().Find(p => p.BSM == bsm);
if (tbfj != null)
{
Items.Remove(tbfj);
RemoveElementByName(bsm, "TempDLTBGrapLayer");
}
}
});
ConirmCommand = ReactiveCommand.Create();
ConirmCommand.Subscribe(x =>
{
IsStartAddFJ = !IsStartAddFJ;
m_mapCtrl.CurrentTool = null;
btnContent = "图上选取图斑标识码";
string jkbsm = string.Empty;
string jkdlbm = string.Empty;
foreach (JZTBXXBRelation item in Items)
{
JZTBXXBRelation temp = ItemsCopyInit.FirstOrDefault(f => f.BSM == item.BSM);
if (temp != null)
ItemsCopyInit.Remove(temp);
jkbsm += "," + item.BSM;
jkbsm = jkbsm.Trim(',');
if (!jkdlbm.Contains(item.DLBM))
jkdlbm += "," + item.DLBM;
jkdlbm = jkdlbm.Trim(',');
}
IFeature feature = JZTB_FC.FeatureClass.GetFeature(OID);
feature.set_Value(feature.Fields.FindField("DYTBBSM"), jkbsm);
feature.set_Value(feature.Fields.FindField("JZDLBM"), jkdlbm);
feature.Store();
Platform.Instance.SendMsg(new NotifyMsgPackage() { Content = "RefreshListData", MsgType = "JZTBXXBRelationView" });
});
CancelCommand = ReactiveCommand.Create();
CancelCommand.Subscribe(x =>
{
IsStartAddFJ = !IsStartAddFJ;
m_mapCtrl.CurrentTool = null;
btnContent = "图上选取图斑标识码";
});
GC_MouseLeftButtonUpCmd = ReactiveCommand.Create();
GC_MouseLeftButtonUpCmd.Subscribe(_ =>
{
TableViewMouseLeftButtonUp(_);
});
}
catch (Exception ex)
{
LogAPI.Debug(ex);
}
}
public void TableViewMouseLeftButtonUp(MouseButtonEventArgs e)
{
try
{
IFeature feature = null;
if (CurrentItem != null)
{
System.Windows.Point p = Mouse.GetPosition(e.Source as FrameworkElement);
if (p.X > 0)
{
if (CurrentItem is JZTBXXBRelation)
{
int oid = (CurrentItem as JZTBXXBRelation).OID;
if (oid == 0) return;
bool isgxbsm = (CurrentItem as JZTBXXBRelation).ISGXBSM;
if (isgxbsm == true)
{
feature = this.DLTBGX_FC.FeatureClass.GetFeature(oid);
if (feature == null || feature.Shape.IsEmpty)
{
MessageHelper.ShowError("图形为空,定位失败!");
return;
}
}
else
{
feature = this.DLTB_FC.FeatureClass.GetFeature(oid);
if (feature == null || feature.Shape.IsEmpty)
{
MessageHelper.ShowError("图形为空,定位失败!");
return;
}
}
MapsManager.Instance.MapService.FlashShape(Hook as IMapControlDefault, feature.ShapeCopy, 2);
}
}
else
{
return;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private void CustomDraw_DoContinue1(ESRI.ArcGIS.Geometry.IGeometry pGeo)
{
try
{
if (DLTBGX_FC != null)
{
int GXBSM_index = DLTBGX_FC.FeatureClass.Fields.FindField("BSM");
int GXDLBM_index = DLTBGX_FC.FeatureClass.Fields.FindField("DLBM");
int GXDLMC_index = DLTBGX_FC.FeatureClass.Fields.FindField("DLMC");
List fjlist = Items.ToList();
List gxfeatures = FeatureAPI.Identify(pGeo, DLTBGX_FC);
if (gxfeatures != null)
{
string bsm = string.Empty;
string dlbm = string.Empty;
string dlmc = string.Empty;
foreach (IFeature item in gxfeatures)
{
int OID = item.OID;
if (GXBSM_index != -1)
{
bsm = item.Value[GXBSM_index].ToString();
}
if (GXDLBM_index != -1)
{
dlbm = item.Value[GXDLBM_index].ToString();
}
if (GXDLMC_index != -1)
{
dlmc = item.Value[GXDLMC_index].ToString();
}
JZTBXXBRelation tbfj = fjlist.Find(p => p.BSM == bsm);
if (tbfj == null)
{
Items.Add(new JZTBXXBRelation() { OID = OID, BSM = bsm, DLBM = dlbm, DLMC = dlmc, ISGXBSM = true });
DrawPolygonElement(item.ShapeCopy, bsm);
}
else
{
items.Remove(tbfj);
RemoveElementByName(bsm, "TempDLTBGrapLayer");
}
}
}
}
if (DLTB_FC != null)
{
int BSM_index = DLTB_FC.FeatureClass.Fields.FindField("BSM");
int DLBM_index = DLTB_FC.FeatureClass.Fields.FindField("DLBM");
int DLMC_index = DLTB_FC.FeatureClass.Fields.FindField("DLMC");
List fjlist = Items.ToList();
List features = FeatureAPI.Identify(pGeo, DLTB_FC);
if (features != null)
{
string bsm = string.Empty;
string dlbm = string.Empty;
string dlmc = string.Empty;
foreach (IFeature item in features)
{
int OID = item.OID;
if (BSM_index != -1)
{
bsm = item.Value[BSM_index].ToString();
}
if (DLBM_index != -1)
{
dlbm = item.Value[DLBM_index].ToString();
}
if (DLMC_index != -1)
{
dlmc = item.Value[DLMC_index].ToString();
}
JZTBXXBRelation tbfj = fjlist.Find(p => p.BSM == bsm);
if (tbfj == null)
{
var list = items.Where(x => x.BSM == bsm).ToList();
if (list != null && list.Count != 0)
{
continue;
}
Items.Add(new JZTBXXBRelation() { OID = OID, BSM = bsm, DLBM = dlbm, DLMC = dlmc, ISGXBSM = false });
DrawPolygonElement(item.ShapeCopy, bsm);
}
else
{
items.Remove(tbfj);
RemoveElementByName(bsm, "TempDLTBGrapLayer");
}
}
}
}
}
catch (Exception ex)
{
MessageHelper.ShowError("选取图斑标识码失败!");
}
}
private void DrawPolygonElement(IGeometry pGeo, string pName)
{
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
//设置线宽和线的颜色
ISimpleLineSymbol pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Color = Symbol.GetRGBColor(255, 0, 0);
pLineSymbol.Width = 1;
pSimpleFillSymbol.Outline = pLineSymbol;
PolygonElementClass element = new PolygonElementClass();
element.Name = pName;
element.Symbol = pSimpleFillSymbol;
element.Geometry = pGeo;
IGraphicsLayer grapLayer = GetSubGraphicsLayer("TempDLTBGrapLayer", true);
(grapLayer as IGraphicsContainer).AddElement(element, 0);
IMapControlDefault mapControl = Hook as IMapControlDefault;
if (mapControl != null)
{
mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, mapControl.ActiveView.Extent);
}
}
public IGraphicsLayer GetSubGraphicsLayer(string subgraphicsLayername, bool IsAdd = false)
{
ICompositeGraphicsLayer pCompositeGLayer = MapsManager.Instance.MapService.getAxMapControl().Map.BasicGraphicsLayer as ICompositeGraphicsLayer;
IGraphicsLayer pGLayer = null;
try
{
//查找是否已存在,如果不存在,跳转到catch内容.
//如果查到不到,说明集合中并没有指定名称的graphicslayer
pGLayer = pCompositeGLayer.FindLayer(subgraphicsLayername);
}
catch
{
if (IsAdd)
{
//若不存在,则添加一个指定名称的GraphicsLayer
pGLayer = pCompositeGLayer.AddLayer(subgraphicsLayername, null);
}
}
return pGLayer;
}
private void RemoveElementByName(string pName, string pGrapLayer)
{
IGraphicsLayer layer = GetSubGraphicsLayer(pGrapLayer);
if (layer == null)
return;
IGraphicsContainer pGraphic = layer as IGraphicsContainer;
if (pGraphic == null)
return;
IElement pElement = null;
pGraphic.Reset();
while ((pElement = pGraphic.Next()) != null)
{
IElementProperties pd = pElement as IElementProperties;
if (pd.Name == pName)
{
pGraphic.DeleteElement(pElement);
break;
}
}
IMapControlDefault mapControl = Hook as IMapControlDefault;
if (mapControl != null)
{
mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, mapControl.ActiveView.Extent);
}
}
}
}