ArcObjects SDK 014 MapSurround和普通Element

1、如何获取MapSurround

和获取MapFrame类似,如果你已经获取指北针、比例尺等对象,可以通过IGraphicsContainer的FindFrame函数获取。如果没有,则通过IGraphicsContainer循环所有Element去判断即可。

2、添加MapSurround

指北针、比例尺、图例等都是MapSurround,我们以最简单的指北针为例,把MapSurround添加到PageLayout上。

var myGraphicsContainer = pLayoutApplication.PageLayout as IGraphicsContainer;
var myActiveView = pLayoutApplication.PageLayout as IActiveView;
var myMap = myActiveView.FocusMap;
var myMapFrame = myGraphicsContainer.FindFrame(myMap) as IMapFrame;
if (this._MapSurroundFrame == null)
{
    this._MapSurroundFrame = new MapSurroundFrameClass();
    var myMarkerNorthArrow = new MarkerNorthArrowClass();
    var myMarkerSymbol = myMarkerNorthArrow.MarkerSymbol;
    var myCharacterMarkerSymbol = myMarkerSymbol as ICharacterMarkerSymbol;
    myCharacterMarkerSymbol.CharacterIndex = 177;
    myMarkerNorthArrow.MarkerSymbol = myCharacterMarkerSymbol;
    myMarkerNorthArrow.Size = 30;
    this._MapSurroundFrame.MapSurround = myMarkerNorthArrow;
    this._MapSurroundFrame.MapFrame = myMapFrame;
    var myQuerySize = this._MapSurroundFrame.MapSurround as IQuerySize;
    double myWidth = 0;
    double myHeight = 0;
    myQuerySize.QuerySize(ref myWidth, ref myHeight);
    var myUnitConverter = new UnitConverterClass();
    this.Width = Math.Round(myUnitConverter.ConvertUnits(myWidth, esriUnits.esriPoints, esriUnits.esriMillimeters), 1);
    this.Height = Math.Round(myUnitConverter.ConvertUnits(myHeight, esriUnits.esriPoints, esriUnits.esriMillimeters), 1);
}
else
{
    this._MapSurroundFrame.MapFrame = myMapFrame;
}
var myNewEnvelope = new EnvelopeClass
{
    XMin = this.X,
    YMin = this.Y,
    Width = this.Width,
    Height = this.Height
};
this.GetElment().Geometry = myNewEnvelope;
if (GraphicsContainerHelper.Contain(myGraphicsContainer, this.GetElment()) == false)
{
    myGraphicsContainer.AddElement(this.GetElment(), 0);
}

添加MapSurround流程都是一样的,不同的地方主要是在实例化具体MapSurround的代码。例如指北针、比例尺、图例等。实例化比例尺的代码如下。

IGraphicsContainer myGraphicsContainer = pLayoutApplication.PageLayout as IGraphicsContainer;
if (this._MapSurroundFrame != null)
{
    myGraphicsContainer.DeleteElement(this._MapSurroundFrame as IElement);
    this._MapSurroundFrame = null;
}

IActiveView myActiveView = pLayoutApplication.PageLayout as IActiveView;
IMap myMap = myActiveView.FocusMap;
IMapFrame myMapFrame = myGraphicsContainer.FindFrame(myMap) as IMapFrame;

this._MapSurroundFrame = new MapSurroundFrameClass();
this._MapSurroundFrame.MapFrame = myMapFrame;

//创建比例尺
this._ScaleBar = this.CreateScaleBar();
this._ScaleBar.Map = myMapFrame.Map;
this._ScaleBar.ResizeHint = esriScaleBarResizeHint.esriScaleBarFixed;
this._ScaleBar.LabelFrequency = esriScaleBarFrequency.esriScaleBarDivisions;
this._MapSurroundFrame.MapSurround = this._ScaleBar;

this._ScaleBar.BarHeight = this.BarHeight;
this._ScaleBar.Division = this.Division;
this._ScaleBar.Divisions = this.Divisions;
this._ScaleBar.Subdivisions = this.Subdivisions;
this._ScaleBar.DivisionsBeforeZero = this.DivisionsBeforeZero;

this._ScaleBar.LabelPosition = this.LabelPosition;
this._ScaleBar.LabelGap = this.LabelGap;
ITextSymbol myLabelSymbol = new TextSymbolClass();
IFontDisp myFontDisp = myLabelSymbol.Font;
myFontDisp.Name = "Times New Roman";
myLabelSymbol.Font = myFontDisp;
myLabelSymbol.Size = this.LabelSize;
this._ScaleBar.LabelSymbol = myLabelSymbol;

//pScaleBar的UnitLabel属性设置需要放在pScaleBar.Units设置代码之后。这样UnitLabel设置才有效果。
this._ScaleBar.Units = esriUnits.esriKilometers;
this._ScaleBar.UnitLabel = this.UnitLabel;
this._ScaleBar.UnitLabelGap = this.UnitLabelGap;
ITextSymbol myUnitLabelSymbol = new TextSymbolClass();
myFontDisp = myUnitLabelSymbol.Font;
myFontDisp.Name = "Times New Roman";
myUnitLabelSymbol.Font = myFontDisp;
myUnitLabelSymbol.Size = this.UnitLabelFontSize;
this._ScaleBar.UnitLabelSymbol = myUnitLabelSymbol;
this._ScaleBar.UnitLabelPosition = this.UnitLabelPosition;

IScaleMarks myScaleMarks = this._ScaleBar as IScaleMarks;
myScaleMarks.MarkFrequency = esriScaleBarFrequency.esriScaleBarDivisionsAndSubdivisions;
myScaleMarks.MarkPosition = this.MarkPosition;
myScaleMarks.DivisionMarkHeight = this.DivisionMarkHeight;
ILineSymbol myDivisionMarkSymbol = new SimpleLineSymbolClass();
myDivisionMarkSymbol.Color = new RgbColorClass() { Red = 0, Green = 0, Blue = 0 };
myDivisionMarkSymbol.Width = this.DivisionMarkWidth;
myScaleMarks.DivisionMarkSymbol = myDivisionMarkSymbol;
myScaleMarks.SubdivisionMarkHeight = this.SubdivisionMarkHeight;
ILineSymbol mySubdivisionMarkSymbol = new SimpleLineSymbolClass();
mySubdivisionMarkSymbol.Color = new RgbColorClass() { Red = 0, Green = 0, Blue = 0 };
mySubdivisionMarkSymbol.Width = this.SubdivisionMarkWidth;
myScaleMarks.SubdivisionMarkSymbol = mySubdivisionMarkSymbol;

IElement myElement = this._MapSurroundFrame as IElement;
IGeometry myGeometry = myElement.Geometry;
if (myGeometry != null && myGeometry.IsEmpty == false)
{
    IEnvelope myGeometryEnvelope = myGeometry.Envelope;
    this.Width = myGeometryEnvelope.Width;
    this.Height = myGeometryEnvelope.Height;
}
IEnvelope myEnvelope = new EnvelopeClass();
myEnvelope.PutCoords(this.X, this.Y, this.X + this.Width, this.Y + this.Height);
myElement.Geometry = myEnvelope;
myGraphicsContainer.AddElement(this._MapSurroundFrame as IElement, 0);

/// <summary>
/// 创建比例尺
/// </summary>
private IScaleBar CreateScaleBar()
{
    IScaleBar myScaleBar;
    if (this.BarType == "AlternatingScaleBar")
    {
        myScaleBar = new AlternatingScaleBar();
    }
    else if (this.BarType == "DoubleAlternatingScaleBar")
    {
        myScaleBar = new DoubleAlternatingScaleBar();
    }
    else if (this.BarType == "HollowScaleBar")
    {
        myScaleBar = new HollowScaleBar();
    }
    else if (this.BarType == "SteppedScaleLine")
    {
        myScaleBar = new SteppedScaleLine();
    }
    else
    {
        myScaleBar = new ScaleLine();
    }
    myScaleBar.Name = this.BarType;
    return myScaleBar;
}

3、普通Element

继承IElement接口的类如下图所示。

image1.png

这个列表中的Elment,除了MapFrame、MapSurroundFrame以及后面带()的外,其他的基本上都是我们常用的,例如点元素、线元素、面元素、圆元素、椭圆元素、各种图片元素等。

IElement有一个关键属性Geometry,如果是MarkerElement或者TextElement,那么Geometry就是IPoint类型,如果是LineElement,那么Geometry是IPolyline类型,PolygonElement的Geometry为IPolygon类型,图片Element一般传入IEnvelope。

一些特殊的Element例如椭圆的Geometry传什么呢?这样不确定的问题,我们可以去SDK帮助中查找,一般帮助中会解释的很清楚。

image2.png

帮助中说的比较明确,可以传递IEnvelope,这样会在IEnvelope生成 一个椭圆。也可以传由EllipticArc组成的Polygon对象。不过我们一般会传IEnvelope,主要原因是简单直观。

除了Geometry外,很对图形状的元素包含Symbol属性。例如MarkerElement可设置IMarkerSymbol,TextElement可以设置ITextSymbol,IPolylineElement可设置ILineSymbol,PolygonElement、CircleElement和EllipseElement可以设置IFillSymbol。

posted @ 2022-12-10 16:35  mytudousi  阅读(133)  评论(0编辑  收藏  举报