描述:最近在用做arcgis二次开发的一个小项目。由于还是三四年前使用过arcengine了。所有问题都是边学边做。在这儿记录下我实现的一些功能。高手请绕道
文采不行就直接进入主题吧。。。。。
实现一、创建shape面图层时指定填充为空边线为绿色。请看代码
//添加shape
IFeatureClass pFeatureClass = pWS.OpenFeatureClass(strShapeName);
m_feaCla = pFeatureClass;
IFeatureLayer pFLayer = new FeatureLayerClass(); // 4
pFLayer.FeatureClass = pFeatureClass;
pFLayer.Name = pFeatureClass.AliasName; // 5
ILayer pLayer = pFLayer as ILayer;
//以下为关键代码
//设置默认符号
ISimpleRenderer simpleRen = new SimpleRendererClass();
IRgbColor pLineColor = new RgbColor();
pLineColor.Green = 255;
ILineSymbol ilSymbl = new SimpleLineSymbolClass();
ilSymbl.Color = pLineColor;
ilSymbl.Width = 1;
ISimpleFillSymbol ipSimpleFillSymbol = new SimpleFillSymbol();
ipSimpleFillSymbol.Outline = ilSymbl;
RgbColor pFillColor = new RgbColor();
pFillColor.Green = 60;
ipSimpleFillSymbol.Color = pFillColor;
ipSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSNull;//设置成填充效果 为空,也就是不填充,有很多填充效果,这是个枚举
simpleRen.Symbol = (ISymbol)ipSimpleFillSymbol;
IGeoFeatureLayer geoFea = (IGeoFeatureLayer)pFLayer;
geoFea.Renderer = (IFeatureRenderer)simpleRen;
//以上为关键代码
axMapControl1.Map.AddLayer(pLayer);
axMapControl1.ActiveView.Refresh();
实现二、在axTOCControl点击符号打开符号选择器。请看代码
//符号设置
if (axMapControl1.LayerCount > 0)
{
esriTOCControlItem toccItem = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap iBasicMap = null;
object unk = null;
object data = null;
ILayer player = null;
axTOCControl1.HitTest(e.x, e.y, ref toccItem, ref iBasicMap, ref player, ref unk, ref data);
if (e.button == 1)
{
if (toccItem == esriTOCControlItem.esriTOCControlItemLegendClass)
{
ESRI.ArcGIS.Carto.ILegendClass pLC = new LegendClassClass();
ESRI.ArcGIS.Carto.ILegendGroup pLG = new LegendGroupClass();
if (unk is ILegendGroup)
{
pLG = (ILegendGroup)unk;
}
pLC = pLG.get_Class((int)data);
ISymbol pSym;
pSym = pLC.Symbol;
ESRI.ArcGIS.DisplayUI.ISymbolSelector pSS = new
ESRI.ArcGIS.DisplayUI.SymbolSelectorClass();
bool bOK = false;
pSS.AddSymbol(pSym);
bOK = pSS.SelectSymbol(0);
if (bOK)
{
pLC.Symbol = pSS.GetSymbolAt(0) as ISymbol;
}
this.axMapControl1.ActiveView.Refresh();
this.axTOCControl1.Refresh();
}
}
}
实现三、旋转几何,使用代码的时候注意变量的赋值就可以直接使用了。
namespace LabelForm
{
//class RotateTool
//{
//}
/// <summary>
/// 旋转元素工具
/// </summary>
public sealed class BTGraphicsRotateElement : BaseTool
{
#region 成员变量
private IHookHelper m_hookHelper = null;
private IPoint m_point;
private IElement m_element = null;
public bool m_moving;
IFeatureClass m_FeaCla = null;
private int n_FeaID = 0;
IPoint centerPoint;
/// <summary>
/// 获取或设置标注图层
/// </summary>
public IFeatureLayer AnnotationLayer { get; set; }
#endregion
#region 构造函数
public BTGraphicsRotateElement()
{
base.m_category = "";
base.m_caption = "";
base.m_message = "";
base.m_toolTip = "旋转元素";
base.m_name = "";
base.m_enabled = false;
base.m_cursor = Cursors.Default;
try
{
base.m_bitmap = Properties.Resources.spin_counterclockwise;
}
catch
{
base.m_bitmap = null;
}
}
public void SetFeacureClass(IFeatureClass fea)
{
m_FeaCla = fea;
}
public void SetEnabled(bool isEn)
{
base.m_enabled = isEn;
}
public BTGraphicsRotateElement(IFeatureLayer annotationLayer)
: this()
{
AnnotationLayer = annotationLayer;
}
#endregion
#region 方法覆写
public override void OnCreate(object hook)
{
try
{
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
if (m_hookHelper.ActiveView == null)
{
m_hookHelper = null;
}
}
catch
{
m_hookHelper = null;
}
//if (m_hookHelper == null)
// base.m_enabled = false;
//else
// base.m_enabled = true;
// TODO: Add other initialization code
}
public override void OnClick()
{
// TODO: Add StationTool.OnClick implementation
if (m_FeaCla == null)
{
return;
}
base.m_enabled = true;
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
if (base.m_enabled == false)
{
return;
}
if (Button == 1)
{
IGraphicsContainer pGraphicsContainer = m_hookHelper.ActiveView as IGraphicsContainer;
m_point = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
ISelection selection = m_hookHelper.FocusMap.FeatureSelection;
IEnumFeatureSetup iEnumFeatureSetup = (IEnumFeatureSetup)selection;
iEnumFeatureSetup.AllFields = true;
IEnumFeature enumFeature = (IEnumFeature)iEnumFeatureSetup;
enumFeature.Reset();
IFeature fre = enumFeature.Next();
n_FeaID = fre.OID;
m_element = new RectangleElement();
m_element.Geometry = fre.Shape;
IArea arr = fre.Shape as IArea;
centerPoint = arr.LabelPoint;
m_moving = true;
}
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
if (base.m_enabled == false)
{
return;
}
if (Button == 1)
{
if (!m_moving) return;
IDataset pDataset = (IDataset)m_FeaCla;
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
pWorkspaceEdit.StartEditing(true);
pWorkspaceEdit.StartEditOperation();
pWorkspaceEdit.EnableUndoRedo();
object missing = Type.Missing;
IPoint newPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
IPointCollection pPointCollection = new PolylineClass();
pPointCollection.AddPoint(centerPoint, ref missing, ref missing);
pPointCollection.AddPoint(m_point, ref missing, ref missing);
double oldAngle = GetAngle(pPointCollection as IPolyline);
IPointCollection pointCollection = new PolylineClass();
pointCollection.AddPoint(centerPoint, ref missing, ref missing);
pointCollection.AddPoint(newPoint, ref missing, ref missing);
double newAngle = GetAngle(pointCollection as IPolyline);
ITransform2D pTransform2D = m_element as ITransform2D;
pTransform2D.Rotate(centerPoint, (newAngle - oldAngle));
m_point = newPoint;
IFeature fea = m_FeaCla.GetFeature(n_FeaID);
fea.Shape = m_element.Geometry;
fea.Store();
pWorkspaceEdit.DisableUndoRedo();
pWorkspaceEdit.StopEditOperation();
pWorkspaceEdit.StopEditing(true);
m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, fea, fea.Shape as IEnvelope);
m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphicSelection, fea, fea.Shape as IEnvelope);
}
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
if (base.m_enabled == false)
{
return;
}
if (Button == 1)
{
m_hookHelper.ActiveView.Refresh();
m_moving = false;
m_element = null;
m_point = null;
}
}
#endregion
#region 方法函数
/// <summary>
/// 获取线的角度(弧度)
/// </summary>
/// <param name="pPolyline">线</param>
/// <returns></returns>
private static double GetAngle(IPolyline pPolyline)
{
ILine pTangentLine = new Line();
pPolyline.QueryTangent(esriSegmentExtension.esriNoExtension, 0.005, true, pPolyline.Length, pTangentLine);
Double radian = pTangentLine.Angle;
/*
Double angle = radian * 180 / Math.PI;
// 如果要设置正角度执行以下方法
while (angle < 0)
{
angle = angle + 360;
}
// 返回角度
return angle;
*/
// 返回弧度
return radian;
}
#endregion
}
}
//向下面这样就可以把旋转几何的方法添加到 axToolbarControl1工具里面
BTGraphicsRotateElement br = new BTGraphicsRotateElement();
ITool tool = br as ITool;
this.axToolbarControl1.AddItem(tool, -1, -1, false, 3, esriCommandStyles.esriCommandStyleIconOnly);