您的位置:首页 > 其它

AE二次开发,右键图层,设置图层要素标注

2013-10-15 13:52 295 查看
#region 成员变量
private string m_zttName = string.Empty;
private string m_sqlCondition = "";
private ITool m_tool;

private ITOCControl2 tocControl;
private IMapControl3 mapControl;
private IToolbarMenu toolMenuMap;
private IToolbarMenu toolMenuLayer;
private MakeFeature _makeFeature = new MakeFeature();
#endregion

//右键菜单Load方法中写
tocControl = (ITOCControl2)toc.Object;
mapControl = (IMapControl3)mcView.Object;
//创建菜单
toolMenuMap = new ToolbarMenuClass();
toolMenuLayer = new ToolbarMenuClass();
toolMenuLayer.AddItem(_makeFeature, -1, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
toolMenuLayer.SetHook(mapControl);
private void toc_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
try
{
#region toolbarmenu
esriTOCControlItem tocItem = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap basicMap = null; ;
ILayer layer = null;
object legendGroup = null;
object index = null;
//获取鼠标点击信息
toc.HitTest(e.x, e.y, ref tocItem, ref basicMap, ref layer, ref legendGroup, ref index);
if (e.button == 2)
{
//设置选中图层标注状态
IGeoFeatureLayer curLayer = layer as IGeoFeatureLayer;
_makeFeature.SetChecked = curLayer.DisplayAnnotation;

if (tocItem == esriTOCControlItem.esriTOCControlItemMap)
{
toc.SelectItem(basicMap, null);
}
else
{
toc.SelectItem(layer, null);
}

//设置CustomProperty为layer (用于自定义的Layer命令)
mcView.CustomProperty = layer;
//弹出右键菜单
if (tocItem == esriTOCControlItem.esriTOCControlItemMap)
{
toolMenuMap.PopupMenu(e.x, e.y, toc.hWnd);
}
else
{
toolMenuLayer.PopupMenu(e.x, e.y, toc.hWnd);
}
}
#endregion
}
catch (Exception ex)
{
FrmErrHandle.ErrorLogShow(ex);
}
}

#region 图层要素标注
/// <summary>
/// 图层要素标注
/// </summary>
public class MakeFeature : BaseCommand
{
private IMapControl3 mapControl;
private ITextSymbol textSymbolTC = null;//图层标注

/// <summary>
/// 设置图层选择状态
/// </summary>
public bool SetChecked
{
get
{
return base.m_checked;
}
set
{
base.m_checked = value;
}
}

//构造函数
public MakeFeature()
{
base.m_caption = "标注要素";
base.m_checked = false;
//标注字体初始化
this.textSymbolTC = new TextSymbolClass();
IRgbColor color = new RgbColorClass();
color.Red = 0;
color.Green = 0;
color.Blue = 0;
color.Transparency = 255;
this.textSymbolTC.Color = color;
this.textSymbolTC.Size = 9;
this.textSymbolTC.Font.Bold = false;
this.textSymbolTC.Font.Name = "宋体";

}
//重写BaseCommand基类的虚拟方法OnClick()
public override void OnClick()
{
ILayer layer = (ILayer)mapControl.CustomProperty;
IFeatureLayer featureLayer = layer as IFeatureLayer;
DataRow[] drs = PublicConstant.TC_DRS;
foreach (DataRow dr in drs)
{
string labelField = dr["labelField"] == DBNull.Value ? "" : dr["labelField"].ToString();
string tcTitle = dr["title"] == DBNull.Value ? "" : dr["title"].ToString();

if (tcTitle == featureLayer.Name)
{
string[] fieldShow = { labelField };
IGeoFeatureLayer layerShow = featureLayer as IGeoFeatureLayer;

if (base.m_checked == false && layerShow.DisplayAnnotation == false)
{
base.m_checked = true;
this.DrawLabel(fieldShow, layerShow, textSymbolTC, mapControl);
}
else if (base.m_checked == true && layerShow.DisplayAnnotation == true)
{
base.m_checked = false;
layerShow.DisplayAnnotation = false;
}
this.mapControl.Refresh();
}
}
}

//重写BaseCommand基类的抽象方法OnCreate(object hook)
public override void OnCreate(object hook)
{
mapControl = (IMapControl3)hook;
}

/// <summary>
/// 设置图层标签
/// </summary>
/// <param name="FieldShow">标注字段</param>
/// <param name="layerShow">标注图层</param>
/// <param name="textSymbol">标注字体</param>
/// <param name="MapShow">AxMapControl</param>
/// <returns></returns>
private void DrawLabel(string[] FieldShow, IGeoFeatureLayer layerShow, ITextSymbol textSymbol, ESRI.ArcGIS.Controls.IMapControl3 MapShow)
{
if (FieldShow.Length == 0) return;
layerShow.DisplayAnnotation = true;

string strFieldString = "[" + FieldShow[0] + "]";
if (FieldShow[0] == "TBBH")
{
strFieldString = "CInt(" + strFieldString + ")";
}
for (int i = 1; i < FieldShow.Length; i++)
{
strFieldString = strFieldString + "+[" + FieldShow[i] + "]";
}

IAnnotateLayerPropertiesCollection pannprocol = layerShow.AnnotationProperties;
ILabelEngineLayerProperties plabelengin;
IAnnotateLayerProperties pannPro;

IBasicOverposterLayerProperties pbasicoverposterlayerprops = new BasicOverposterLayerPropertiesClass();
pbasicoverposterlayerprops.NumLabelsOption = esriBasicNumLabelsOption.esriOneLabelPerShape;
IElementCollection eleTmp = new ElementCollectionClass();

for (int j = 0; j < pannprocol.Count; j++)
{
pannprocol.QueryItem(j, out pannPro, out eleTmp, out eleTmp);
plabelengin = (ILabelEngineLayerProperties)pannPro;
plabelengin.Expression = strFieldString;

plabelengin.Symbol = textSymbol;
plabelengin.BasicOverposterLayerProperties = pbasicoverposterlayerprops;
}

ITrackCancel pcon = new CancelTrackerClass();
pcon.Continue();
layerShow.Draw(esriDrawPhase.esriDPAnnotation, MapShow.ActiveView.ScreenDisplay, pcon);
}
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐