{
//將距離轉換函數
public static double ConvertPixelsToMapUnits(IActiveView pActiveView, double pixelUnits)
{
IPoint p1 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperLeft;
IPoint p2 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperRight;
int x1, x2, y1, y2;
pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p1, out x1, out y1);
pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p2, out x2, out y2);
double pixelExtent = x2 - x1;
double realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
double sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
return pixelUnits * sizeOfOnePixel;
}
/// 將距離轉換函數
public static double ConvertPixelToMapUnit(IActiveView pActiveView, double pixelUnits)
{
double realWorldDisplayExtent;
int pixelExtent;
double sizeOfOnePixel;
pixelExtent = pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().right
- pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().left;
realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
return pixelUnits * sizeOfOnePixel;
}
/// <summary>
/// 獲取一個圖層中所有的地物
/// </summary>
/// <param name="featureClass"></param>
/// <returns></returns>
public static List<IFeature> GetLayerFeatures(IFeatureClass featureClass)
{
List<IFeature> result = new List<IFeature>();
if (featureClass == null)
return result;
IFeatureCursor featureCursor = featureClass.Search(null, false);
IFeature feature = featureCursor.NextFeature();
while (feature != null)
{
result.Add(feature);
feature = featureCursor.NextFeature();
}
ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(featureCursor);
return result;
}
/// <summary>
/// 根據字段名獲取地物的值
/// </summary>
/// <param name="feature"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static string GetFieldValue(IFeature feature, string fieldName)
{
//根據字段名,獲取字段的索引位置
int indexField = feature.Fields.FindField(fieldName);
if (indexField == -1)
return "";
return feature.get_Value(indexField).ToString();
}
/// <summary>
/// 獲取線性地物的長度
/// </summary>
/// <param name="feature"></param>
/// <returns></returns>
public static double GetLineFeatureLength(IFeature feature)
{
if (feature.Shape.GeometryType != esriGeometryType.esriGeometryPolyline)
return 0;
else
{
IPolyline polyline = feature.Shape as IPolyline;
return polyline.Length;
}
}
public static IRgbColor GetRGB(int red, int green, int blue)
{
IRgbColor rGBColor = new RgbColorClass();
rGBColor.Red = red;
rGBColor.Green = green;
rGBColor.Blue = blue;
return rGBColor;
}
#region 閃爍地物
/// <summary>
/// 閃爍地物
/// </summary>
/// <param name="mapControl"></param>
/// <param name="iFeature"></param>
/// <param name="iMap"></param>
public static void FlashFeature(AxMapControl mapControl, IFeature iFeature)
{
IActiveView iActiveView = mapControl.ActiveView;
if (iActiveView != null)
{
iActiveView.ScreenDisplay.StartDrawing(0, (short)esriScreenCache.esriNoScreenCache);
//根據幾何類型調用不同的過程
switch (iFeature.Shape.GeometryType)
{
case esriGeometryType.esriGeometryPolyline:
FlashLine(mapControl, iActiveView.ScreenDisplay, iFeature.Shape);
break;
case esriGeometryType.esriGeometryPolygon:
FlashPolygon(mapControl, iActiveView.ScreenDisplay, iFeature.Shape);
break;
case esriGeometryType.esriGeometryPoint:
FlashPoint(mapControl, iActiveView.ScreenDisplay, iFeature.Shape);
break;
default:
break;
}
iActiveView.ScreenDisplay.FinishDrawing();
}
}
//閃爍線
public static void FlashLine(AxMapControl mapControl, IScreenDisplay iScreenDisplay, IGeometry iGeometry)
{
ISimpleLineSymbol iLineSymbol;
ISymbol iSymbol;
IRgbColor iRgbColor;
iLineSymbol = new SimpleLineSymbol();
iLineSymbol.Width = 4;
iRgbColor = new RgbColor();
iRgbColor.Red = 255;
iLineSymbol.Color = iRgbColor;
iSymbol = (ISymbol)iLineSymbol;
iSymbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
mapControl.FlashShape(iGeometry, 3, 200, iSymbol);
}
//閃爍面
public static void FlashPolygon(AxMapControl mapControl, IScreenDisplay iScreenDisplay, IGeometry iGeometry)
{
ISimpleFillSymbol iFillSymbol;
ISymbol iSymbol;
IRgbColor iRgbColor;
iFillSymbol = new SimpleFillSymbol();
iFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
iFillSymbol.Outline.Width = 12;
iRgbColor = new RgbColor();
iRgbColor.RGB = System.Drawing.Color.FromArgb(100, 180, 180).ToArgb();
iFillSymbol.Color = iRgbColor;
iSymbol = (ISymbol)iFillSymbol;
iSymbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
iScreenDisplay.SetSymbol(iSymbol);
mapControl.FlashShape(iGeometry, 3, 200, iSymbol);
}
//閃爍點
public static void FlashPoint(AxMapControl mapControl, IScreenDisplay iScreenDisplay, IGeometry iGeometry)
{
ISimpleMarkerSymbol iMarkerSymbol;
ISymbol iSymbol;
IRgbColor iRgbColor;
iMarkerSymbol = new SimpleMarkerSymbol();
iMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
iRgbColor = new RgbColor();
iRgbColor.RGB = System.Drawing.Color.FromArgb(0, 0, 0).ToArgb();
iMarkerSymbol.Color = iRgbColor;
iSymbol = (ISymbol)iMarkerSymbol;
iSymbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
mapControl.FlashShape(iGeometry, 3, 200, iSymbol);
}
#endregion
/// <summary>
/// 閃爍地物
/// </summary>
/// <param name="mapCtrl"></param>
/// <param name="flashGeometry"></param>
public static void FlashGeometry(AxMapControl mapCtrl, IGeometry flashGeometry)
{
//通過IHookActions閃爍要素集合
HookHelperClass m_pHookHelper = new HookHelperClass();
m_pHookHelper.Hook = mapCtrl.Object;
IHookActions hookActions = (IHookActions)m_pHookHelper;
hookActions.DoAction(flashGeometry, esriHookActions.esriHookActionsFlash);
Application.DoEvents();
m_pHookHelper.ActiveView.ScreenDisplay.UpdateWindow();
}
/// <summary>
/// 定位
/// </summary>
/// <param name="geometry">要定位的地物</param>
public static void LocateFeature(IGeometry geometry,AxMapControl Axmap)
{
if (geometry == null) return;
if (geometry.GeometryType == esriGeometryType.esriGeometryPoint)
{
Axmap.CenterAt(geometry as IPoint);
}
else
{
Axmap.Extent = geometry.Envelope;
}
}
/// <summary>
/// 獲取兩個地物相交的部分
/// </summary>
/// <param name="geo1">相交的第一個地物</param>
/// <param name="geo2">相交的第二個地物</param>
/// <returns></returns>
public static IGeometry GetDifferenceGeo(IGeometry geo1, IGeometry geo2)
{
ITopologicalOperator topo = geo2 as ITopologicalOperator;
return topo.Difference(geo1);
}
public static List<IFeature> GetCurrentLyrSelectedFeatures(IMap map,esriGeometryType geoType)
{
IFeature pFeature = null;
List<IFeature> features = new List<IFeature>();
ISelection selection = map.FeatureSelection;
IEnumFeature pEnumFeature = selection as IEnumFeature;
pFeature = pEnumFeature.Next();
while (pFeature != null)
{
if (pFeature.Shape.GeometryType == geoType)
{
features.Add(pFeature);
}
pFeature= pEnumFeature.Next();
}
return features;
}
}