距离标注主要代码:
带附加箭头属性的线标注。
private void AddDisLineElement(IActiveView activeView, IPolyline disLine)
{
if (activeView != null)
{
try
{
IGraphicsContainer graphicsContainer = activeView as IGraphicsContainer;
if (disLine != null)
{
IPolyline geometryLine = new PolylineClass();
geometryLine.FromPoint = disLine.FromPoint.X > disLine.ToPoint.X ? disLine.ToPoint : disLine.FromPoint;
geometryLine.ToPoint = disLine.FromPoint.X > disLine.ToPoint.X ? disLine.FromPoint : disLine.ToPoint;
//构建带箭头的线元素
IElement lineWithArrow = GetLineElementWithArrow(geometryLine);
//构建文本元素
ITextElement areaText = new TextElementClass();
double distance = geometryLine.Length;
distance = distance > 0 ? distance : (-distance);
string unitString = GetUnitsString(m_HookHelper.FocusMap);
areaText.Text =distance.ToString("0.0") + unitString;
ITextSymbol textSymbol = areaText.Symbol as ITextSymbol;
ILine line = new LineClass();
line.FromPoint = geometryLine.FromPoint;
line.ToPoint = geometryLine.ToPoint;
textSymbol.Angle = line.Angle;
textSymbol.Size = 10;
areaText.Symbol = textSymbol;
IElement textElement = areaText as IElement;
IObjectCopy objectCopy = new ObjectCopyClass();
IPolyline textGeometry = objectCopy.Copy(geometryLine) as IPolyline;
(textGeometry as ITransform2D).Move(0, 10);
textElement.Geometry = textGeometry;
IElementProperties3 textElementPro = textElement as IElementProperties3;
textElementPro.Name = "DistanceLabel";
textElementPro.CustomProperty = lineWithArrow;
// 为了使距离标注中的文字可单独移动,文字标注与箭头元素分开而不组合成一个元素
(m_HookHelper.FocusMap as IGraphicsContainer).AddElement(lineWithArrow, 0);
(m_HookHelper.FocusMap as IGraphicsContainer).AddElement(textElement, 0);
}
}
finally
{
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, activeView.Extent);
}
}
}
/// <summary>
/// 构建带箭头的线元素
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private IElement GetLineElementWithArrow(IPolyline line)
{
ICartographicLineSymbol pCartoLineSymbol = new CartographicLineSymbolClass();
pCartoLineSymbol.Cap = esriLineCapStyle.esriLCSRound;
ILineProperties pLineProp = pCartoLineSymbol as ILineProperties;
pLineProp.DecorationOnTop = true;
ILineDecoration pLineDecoration = new LineDecorationClass();
ISimpleLineDecorationElement pSimpleLineDecoElem = new SimpleLineDecorationElementClass();
pSimpleLineDecoElem.AddPosition(0);
pSimpleLineDecoElem.AddPosition(1);
IArrowMarkerSymbol pArrowMarkerSym = new ArrowMarkerSymbolClass();
pArrowMarkerSym.Size = 8;
pSimpleLineDecoElem.MarkerSymbol = pArrowMarkerSym as IMarkerSymbol;
pLineDecoration.AddElement(pSimpleLineDecoElem as ILineDecorationElement);
pLineProp.LineDecoration = pLineDecoration;
ILineSymbol pLineSymbol = pCartoLineSymbol as ILineSymbol;
pLineSymbol.Width = 1;
ILineElement pLineElem = new LineElementClass();
pLineElem.Symbol = pLineSymbol;
IElement elementWithArrow = pLineElem as IElement;
elementWithArrow.Geometry = line;
return elementWithArrow;
}
private string GetUnitsString(IMap map)
{
string unitStrig = map.MapUnits.ToString();
switch (map.MapUnits)
{
case esriUnits.esriCentimeters:
unitStrig = "厘米"; break;
case esriUnits.esriDecimeters:
unitStrig = "分米"; break;
case esriUnits.esriMeters:
unitStrig = "米"; break;
case esriUnits.esriKilometers:
unitStrig = "千米"; break;
case esriUnits.esriUnknownUnits:
unitStrig = "未知单位"; break;
default:
unitStrig = unitStrig.Substring(4); break;
}
return unitStrig;
}private void AddDisLineElement(IActiveView activeView, IPolyline disLine)
{
if (activeView != null)
{
try
{
IGraphicsContainer graphicsContainer = activeView as IGraphicsContainer;
if (disLine != null)
{
IPolyline geometryLine = new PolylineClass();
geometryLine.FromPoint = disLine.FromPoint.X > disLine.ToPoint.X ? disLine.ToPoint : disLine.FromPoint;
geometryLine.ToPoint = disLine.FromPoint.X > disLine.ToPoint.X ? disLine.FromPoint : disLine.ToPoint;
//构建带箭头的线元素
IElement lineWithArrow = GetLineElementWithArrow(geometryLine);
//构建文本元素
ITextElement areaText = new TextElementClass();
double distance = geometryLine.Length;
distance = distance > 0 ? distance : (-distance);
string unitString = GetUnitsString(m_HookHelper.FocusMap);
areaText.Text =distance.ToString("0.0") + unitString;
ITextSymbol textSymbol = areaText.Symbol as ITextSymbol;
ILine line = new LineClass();
line.FromPoint = geometryLine.FromPoint;
line.ToPoint = geometryLine.ToPoint;
textSymbol.Angle = line.Angle;
textSymbol.Size = 10;
areaText.Symbol = textSymbol;
IElement textElement = areaText as IElement;
IObjectCopy objectCopy = new ObjectCopyClass();
IPolyline textGeometry = objectCopy.Copy(geometryLine) as IPolyline;
(textGeometry as ITransform2D).Move(0, 10);
textElement.Geometry = textGeometry;
IElementProperties3 textElementPro = textElement as IElementProperties3;
textElementPro.Name = "DistanceLabel";
textElementPro.CustomProperty = lineWithArrow;
// 为了使距离标注中的文字可单独移动,文字标注与箭头元素分开而不组合成一个元素
(m_HookHelper.FocusMap as IGraphicsContainer).AddElement(lineWithArrow, 0);
(m_HookHelper.FocusMap as IGraphicsContainer).AddElement(textElement, 0);
}
}
finally
{
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, activeView.Extent);
}
}
}
/// <summary>
/// 构建带箭头的线元素
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private IElement GetLineElementWithArrow(IPolyline line)
{
ICartographicLineSymbol pCartoLineSymbol = new CartographicLineSymbolClass();
pCartoLineSymbol.Cap = esriLineCapStyle.esriLCSRound;
ILineProperties pLineProp = pCartoLineSymbol as ILineProperties;
pLineProp.DecorationOnTop = true;
ILineDecoration pLineDecoration = new LineDecorationClass();
ISimpleLineDecorationElement pSimpleLineDecoElem = new SimpleLineDecorationElementClass();
pSimpleLineDecoElem.AddPosition(0);
pSimpleLineDecoElem.AddPosition(1);
IArrowMarkerSymbol pArrowMarkerSym = new ArrowMarkerSymbolClass();
pArrowMarkerSym.Size = 8;
pSimpleLineDecoElem.MarkerSymbol = pArrowMarkerSym as IMarkerSymbol;
pLineDecoration.AddElement(pSimpleLineDecoElem as ILineDecorationElement);
pLineProp.LineDecoration = pLineDecoration;
ILineSymbol pLineSymbol = pCartoLineSymbol as ILineSymbol;
pLineSymbol.Width = 1;
ILineElement pLineElem = new LineElementClass();
pLineElem.Symbol = pLineSymbol;
IElement elementWithArrow = pLineElem as IElement;
elementWithArrow.Geometry = line;
return elementWithArrow;
}
private string GetUnitsString(IMap map)
{
string unitStrig = map.MapUnits.ToString();
switch (map.MapUnits)
{
case esriUnits.esriCentimeters:
unitStrig = "厘米"; break;
case esriUnits.esriDecimeters:
unitStrig = "分米"; break;
case esriUnits.esriMeters:
unitStrig = "米"; break;
case esriUnits.esriKilometers:
unitStrig = "千米"; break;
case esriUnits.esriUnknownUnits:
unitStrig = "未知单位"; break;
default:
unitStrig = unitStrig.Substring(4); break;
}
return unitStrig;
}
面积标注主要代码:
标注文字在面中间。
IPolygon polygonRegion = _polygonFeedback.Stop();
if (polygonRegion == null || polygonRegion.IsEmpty)
return;
polygonRegion.SpatialReference = m_HookHelper.FocusMap.SpatialReference;
//构建面元素
IElement polygonElement = new PolygonElementClass();
ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
fillSymbol.Style = esriSimpleFillStyle.esriSFSNull;
polygonElement.Geometry = polygonRegion;
(polygonElement as IFillShapeElement).Symbol = fillSymbol;
//构建文本元素
ITextElement areaText = new TextElementClass();
IPoint labelPoint = (polygonRegion as IArea).LabelPoint;
double area = (polygonRegion as IArea).Area;
area = area > 0 ? area : (-area);
string unitString = GetUnitsString(m_HookHelper.FocusMap);
areaText.Text = area.ToString("0.00") + unitString;
ITextSymbol textSymbol = areaText.Symbol as ITextSymbol;
if (LabelOptionGloble.DrawFont != null)
{
System.Drawing.Font font = LabelOptionGloble.DrawFont;
IFontDisp fontDisp = new StdFontClass() as IFontDisp;
fontDisp.Name = font.Name;
fontDisp.Bold = font.Bold;
fontDisp.Italic = font.Italic;
fontDisp.Underline = font.Underline;
fontDisp.Strikethrough = font.Strikeout;
fontDisp.Size = (decimal)font.Size;
textSymbol = new TextSymbolClass();
textSymbol.Font = fontDisp;
textSymbol.Color = RgbColorWrapper.GetRGBColor(LabelOptionGloble.TextColor);
}
else
{
textSymbol.Size = 10;
}
areaText.Symbol = textSymbol;
IElement textElement = areaText as IElement;
textElement.Geometry = labelPoint;
//组成组合元素
IGroupElement areaLabelElement = new GroupElementClass();
areaLabelElement.AddElement(polygonElement);
areaLabelElement.AddElement(textElement);
IElement addElement = areaLabelElement as IElement;
IElementProperties3 addElementPro = addElement as IElementProperties3;
addElementPro.Name = "AreaLabel";
(m_HookHelper.FocusMap as IGraphicsContainer).AddElement(addElement, 0);
m_HookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, m_HookHelper.ActiveView.Extent);