年度变更建库软件5.0版本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

451 lines
23 KiB

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using KGIS.Framework.AE;
using KGIS.Framework.AE.ExtensionMethod;
using KGIS.Framework.Maps;
using KGIS.Framework.OpenData.Control;
using KGIS.Framework.OpenData.Filter;
using KGIS.Framework.OpenData.InterFace;
using KGIS.Framework.Platform;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Xml.Linq;
using System.Xml;
using Kingo.PluginServiceInterface;
using UIShell.OSGi;
using System.Linq;
namespace Kingo.Plugin.MapView.Commands
{
//[PathAttribute("Kingo.Plugin.MapView.Commands.CmdAddLayer")]
public class CmdAddLayer : BaseMapMenuCommand
{
private ILayerFactoryHelper m_LayerFactoryHelper;
private List<ILayer> m_Layers;
public override void OnClick()
{
if (m_hookHelper == null)
return;
IDataCatalogService _DataCatalog = BundleRuntime.Instance.GetFirstOrDefaultService<IDataCatalogService>();
if (_DataCatalog == null) return;
try
{
object layer = m_hookHelper.GetCustomProperty();
if ((!(layer is IGroupLayer) && !(layer is IMap)) || layer == null)
{
layer = m_hookHelper.FocusMap;
}
OpenDataDialog pDialog = new OpenDataDialog();
ISpatialDataObjectFilter pOFilter;
pOFilter = new FilterDatasetsAndLayers();
pDialog.AddFilter(pOFilter, true);
pDialog.Title = "选择添加的数据";
pDialog.AllowMultiSelect = true;
pDialog.RestoreLocation = true;
pDialog.StartLocation = pDialog.FinalLocation;
DialogResult dialogResult = pDialog.ShowDialog();
if (dialogResult == DialogResult.OK && pDialog.Selection.Count != 0)
{
if (m_Layers == null)
{
m_Layers = new List<ILayer>();
}
else
{
m_Layers.Clear();
}
ILayer layerQS = MapsManager.Instance.MapService.GetGroupLayer((layer as ILayerGeneralProperties).LayerDescription);//获取参考数据图层组
LayerCfg GroupLayerInfo = null;
if (_DataCatalog != null)
{
_DataCatalog.GetNodeByLayer(layerQS);
LayerCfg rootLayer = _DataCatalog.CurrentLayers as LayerCfg;
List<LayerCfg> AllLayers = rootLayer.GetAllItem();
if (layerQS != null)
GroupLayerInfo = AllLayers.FirstOrDefault(f => f.LayerName == layerQS.Name);
}
List<ILayer> ListLayers = new List<ILayer>();
foreach (ISpatialDataObject distObj in pDialog.Selection)
{
if (!string.IsNullOrWhiteSpace(distObj.Name) && distObj.Name.ToLower().EndsWith(".kotiles"))
{
KOTilesLayer ko = KOTilesLayer.CreateInstance(distObj.FullName);
ko.Name = distObj.Name.ToLower().Replace(".kotiles", "");
if (layer is IGroupLayer)
{
//(layer as IGroupLayer).Add(ko);
_DataCatalog.AddLayer(ko, GroupLayerInfo);
}
else if (layer is IMap)
{
_DataCatalog.AddLayer(ko, null);
//(layer as IMap).AddLayer(ko);
}
ListLayers.Add(ko);
}
else if (distObj.DatasetType == esriDatasetType.esriDTTable)
{
}
else if (distObj.DatasetName == null && distObj.FullName.ToLower().EndsWith(".lyr"))
{
IMapControl2 myAddLayerMap = new MapControlClass();
myAddLayerMap.AddLayerFromFile(distObj.FullName, 0);
ILayer pLayer = myAddLayerMap.get_Layer(0);
if (layer is IGroupLayer)
{
(layer as IGroupLayer).Add(pLayer);
}
else if (layer is IMap)
{
(layer as IMap).AddLayer(pLayer);
}
SaveLayerStyleToConfig(pLayer);
ListLayers.Add(pLayer);
}
else
{
if (m_LayerFactoryHelper == null)
m_LayerFactoryHelper = new LayerFactoryHelperClass();
//层厂帮手创建从名字对象层的正确类型返回枚举的层
IEnumLayer pEnumLayer = m_LayerFactoryHelper.CreateLayersFromName((distObj.DatasetName as IName));
ILayer pLayer;
pEnumLayer.Reset();
//通过循环的枚举和添加的每个层
while ((pLayer = pEnumLayer.Next()) != null)
{
//判断坐标参考是否与当前工程坐标参考一致
try
{
IGeoDataset geoDataset = null;
if ((pLayer as IFeatureLayer) != null && (pLayer as IFeatureLayer).FeatureClass != null)
{
geoDataset = (pLayer as IFeatureLayer).FeatureClass as IGeoDataset;
}
else if ((pLayer as IRasterLayer) != null && (pLayer as IRasterLayer).Raster != null)
{
geoDataset = (pLayer as IRasterLayer).Raster as IGeoDataset;
}
if (geoDataset != null && geoDataset.SpatialReference != null && m_hookHelper.FocusMap.SpatialReference != null)
{
if (string.IsNullOrEmpty(m_hookHelper.FocusMap.SpatialReference.Name) || m_hookHelper.FocusMap.SpatialReference.Name.ToUpper().Equals("UNKNOWN") || m_hookHelper.FocusMap.SpatialReference.FactoryCode == 0)
{
m_hookHelper.FocusMap.SpatialReference = geoDataset.SpatialReference;
}
if (geoDataset != null && !GeoDBAPI.SpatialReferenceCompare(geoDataset.SpatialReference, m_hookHelper.FocusMap.SpatialReference))
{
m_Layers.Add(pLayer);
ListLayers.Add(pLayer);
continue;
}
}
}
catch (Exception ex1)
{
LogAPI.Debug("在添加图层期间 通过循环的枚举和添加的每个层 时发生异常,异常信息如下:");
LogAPI.Debug(ex1);
LogAPI.Debug("异常信息结束。");
}
if (layer is IGroupLayer)
{
//IGroupLayer groupLayer = (layer as IGroupLayer);
LayerCfg layerInfo3 = new LayerCfg();
if (GroupLayerInfo != null)
{
if (distObj.FullName.ToLower().EndsWith(".img") && pLayer is IRasterLayer)
{
IRasterLayer rasterlayer = pLayer as IRasterLayer;
IRasterStretch rst = rasterlayer.Renderer as IRasterStretch;
if (rst != null)
{
RgbColorClass rgb = new RgbColorClass();
rgb.NullColor = true;
rst.Background = true;
rst.BackgroundColor = rgb;
rst.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_NONE;
}
//(layer as IGroupLayer).Add(rasterlayer);
_DataCatalog.AddLayer(rasterlayer, GroupLayerInfo);
layerInfo3 = GroupLayerInfo.Layers.FirstOrDefault(x => x.FcName == rasterlayer.Name);
ListLayers.Add(rasterlayer);
}
else
{
_DataCatalog.AddLayer(pLayer, GroupLayerInfo);
layerInfo3 = GroupLayerInfo.Layers.FirstOrDefault(x => x.FcName == pLayer.Name);
ListLayers.Add(pLayer);
}
}
if (layerInfo3 != null)
{
layerInfo3.AbsolutePathType = "AddLayerWay";//标记为外部路径数据
layerInfo3.AbsolutePath = layerInfo3.FcPath;
}
}
else if (layer is IMap)
{
//(layer as IMap).AddLayer(pLayer);
_DataCatalog.AddLayer(pLayer, null);
ListLayers.Add(pLayer);
}
}
if (m_Layers != null && m_Layers.Count > 0)
{
DialogResult dialogResult2 = MessageHelper.ShowYesNoAndTips("图层坐标系与工程坐标系不一致,是否继续添加?");
if (dialogResult2 == DialogResult.Yes)
{
foreach (ILayer item in m_Layers)
{
if (layer is IGroupLayer)
{
if (distObj.FullName.ToLower().EndsWith(".img") && pLayer is IRasterLayer)
{
IRasterLayer rasterlayer = pLayer as IRasterLayer;
IRasterStretch rst = rasterlayer.Renderer as IRasterStretch;
if (rst != null)
{
RgbColorClass rgb = new RgbColorClass();
rgb.NullColor = true;
rst.Background = true;
rst.BackgroundColor = rgb;
rst.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_NONE;
}
(layer as IGroupLayer).Add(rasterlayer);
ListLayers.Add(rasterlayer);
}
else
{
//(layer as IGroupLayer).Add(item);
_DataCatalog.AddLayer(item, GroupLayerInfo);
ListLayers.Add(item);
}
}
else if (layer is IMap)
{
//(layer as IMap).AddLayer(item);
_DataCatalog.AddLayer(item, null);
ListLayers.Add(item);
}
}
}
}
}
}
if (_DataCatalog != null)
{
if (GroupLayerInfo != null && GroupLayerInfo.Layers.Count > 0)
GroupLayerInfo.Expanded = true;
_DataCatalog.UpdateTree();
}
//Platform.Instance.SendMsg(new KGIS.Framework.Utils.Interface.NotifyMsgPackage() { MsgType = "RefreshLayer" });
Platform.Instance.SendMsg(new KGIS.Framework.Utils.Interface.NotifyMsgPackage() { MsgType = "SaveProjectForNoEditor" });
}
}
catch (Exception ex)
{
MessageHelper.ShowError(string.Format("添加图层失败!{0}", ex.Message));
LogAPI.Debug("添加图层时发生异常,异常信息如下:");
LogAPI.Debug(ex);
LogAPI.Debug("异常信息结束。");
}
}
/// <summary>
/// 保存图层样式至配置文件
/// </summary>
/// <param name="pLayer"></param>
private void SaveLayerStyleToConfig(ILayer pLayer)
{
try
{
IStyleGalleryClass symbologyStyleClass = null;
ISymbol symbol = null;
IFillSymbol fillSymbol = null;
IFeatureLayer2 featureLayer = pLayer as IFeatureLayer2;
if (featureLayer != null)
{
esriGeometryType geometryType = featureLayer.ShapeType;
switch (geometryType)
{
case esriGeometryType.esriGeometryPoint:
symbologyStyleClass = new MarkerSymbolStyleGalleryClassClass();
break;
case esriGeometryType.esriGeometryPolyline:
symbologyStyleClass = new LineSymbolStyleGalleryClassClass();
break;
case esriGeometryType.esriGeometryPolygon:
symbologyStyleClass = new FillSymbolStyleGalleryClassClass();
break;
}
ISimpleRenderer simpleRenderer = (featureLayer as IGeoFeatureLayer).Renderer as ISimpleRenderer;
if (simpleRenderer != null)
{
symbol = simpleRenderer.Symbol;
}
else
{
symbol = GetDefaultSymbol(geometryType);
}
fillSymbol = (symbol as IFillSymbol);
}
string appLayerConfigspath = System.IO.Path.Combine((MapsManager.Instance.CurrProjectInfo as ProjectInfo).ProjDir, "AppLayerConfigs.xml");
if (!System.IO.File.Exists(appLayerConfigspath))
{
XmlDataDocument xml = new XmlDataDocument();
XmlElement layersElement = xml.CreateElement("Layers");
xml.AppendChild(layersElement);
xml.Save(appLayerConfigspath);
}
XDocument xDoc = XDocument.Load(appLayerConfigspath);
XElement element = xDoc.Element("Layers");
bool isHave = false;
IFeatureLayer feature = pLayer as IFeatureLayer;
string layerNameStr = (feature as IDataset).BrowseName;
string layerCaptionStr = feature.Name;
System.Drawing.Color fillColor = Converter.FromRGBColor(fillSymbol.Color as IRgbColor);
System.Drawing.Color borderColor = Converter.FromRGBColor(fillSymbol.Outline.Color as IRgbColor);
System.Drawing.Color lableColor = ColorTranslator.FromHtml("#FF000000");
if (element != null)
{
foreach (var item in element.Elements("Layer"))
{
if (item.Attribute("LayerName").Value == layerNameStr)
{
isHave = true;
item.SetAttributeValue("LayerName", layerNameStr);
item.SetAttributeValue("LayerCaption", layerCaptionStr);
item.SetAttributeValue("LayerBorderStyle", "0");
item.SetAttributeValue("LayerBorderWidth", "1");
item.SetAttributeValue("LayerFillColor", System.Windows.Media.Color.FromArgb(fillColor.A, fillColor.R, fillColor.G, fillColor.B));
item.SetAttributeValue("LayerBorderColor", System.Windows.Media.Color.FromArgb(borderColor.A, borderColor.R, borderColor.G, borderColor.B));
item.SetAttributeValue("LayerLableColor", System.Windows.Media.Color.FromArgb(lableColor.A, lableColor.R, lableColor.G, lableColor.B));
item.SetAttributeValue("LayerLableName", "");
item.SetAttributeValue("LayyerGroup", "");
break;
}
}
}
if (isHave == false)
{
XElement xLayer = new XElement("Layer");
//图层名
XAttribute layerName = new XAttribute("LayerName", layerNameStr);
//图层别名
XAttribute layerCaption = new XAttribute("LayerCaption", layerCaptionStr);
//边框样式
XAttribute layerBorderStyle = new XAttribute("LayerBorderStyle", "0");
//边框宽度
XAttribute layerBorderWidth = new XAttribute("LayerBorderWidth", "1");
//填充颜色
XAttribute layerFillColor = new XAttribute("LayerFillColor", System.Windows.Media.Color.FromArgb(fillColor.A, fillColor.R, fillColor.G, fillColor.B));
//边框颜色
XAttribute layerBorderColor = new XAttribute("LayerBorderColor", System.Windows.Media.Color.FromArgb(borderColor.A, borderColor.R, borderColor.G, borderColor.B));
//标签颜色
XAttribute layerLableColor = new XAttribute("LayerLableColor", System.Windows.Media.Color.FromArgb(lableColor.A, lableColor.R, lableColor.G, lableColor.B));
XAttribute layerLableName = new XAttribute("LayerLableName", "");
xLayer.Add(layerName, layerCaption, layerBorderStyle, layerBorderWidth, layerFillColor, layerBorderColor, layerLableColor, layerLableName);
element.Add(xLayer);
}
xDoc.Save(appLayerConfigspath);
}
catch (Exception ex)
{
LogAPI.Debug("保存图层样式配置失败:" + ex);
}
}
private ISymbol GetDefaultSymbol(esriGeometryType geometryType)
{
try
{
ISymbol result = null;
IRgbColor rgbColor = new RgbColorClass();
rgbColor.Red = 184;
rgbColor.Green = 242;
rgbColor.Blue = 200;
switch (geometryType)
{
case esriGeometryType.esriGeometryPoint:
result = (new SimpleMarkerSymbolClass
{
Color = rgbColor,
Style = esriSimpleMarkerStyle.esriSMSCircle
} as ISymbol);
goto IL_B3;
case esriGeometryType.esriGeometryMultipoint:
goto IL_B3;
case esriGeometryType.esriGeometryPolyline:
break;
case esriGeometryType.esriGeometryPolygon:
result = (new SimpleFillSymbolClass
{
Color = rgbColor,
Style = esriSimpleFillStyle.esriSFSSolid
} as ISymbol);
goto IL_B3;
default:
if (geometryType != esriGeometryType.esriGeometryLine)
{
goto IL_B3;
}
break;
}
result = (new SimpleLineSymbolClass
{
Color = rgbColor,
Width = 1.0,
Style = esriSimpleLineStyle.esriSLSSolid
} as ISymbol);
IL_B3:
return result;
}
catch (Exception ex)
{
LogAPI.Debug(ex.Message);
}
return null;
}
public override void OnCreate(object Hook)
{
try
{
if (m_hookHelper == null)
{
m_hookHelper = new HookHelper();
m_hookHelper.Hook = Hook;
}
}
catch (Exception ex)
{
LogAPI.Debug("加载 添加图层 命令时发生异常,异常信息如下:");
LogAPI.Debug(ex);
LogAPI.Debug("异常信息结束");
}
}
}
}