dhowes

Create line element with arrow symbol at end

Discussion created by dhowes on Mar 3, 2013
Latest reply on Jan 23, 2018 by shekhawatravi
I found this archived thread very helpful for creating a line with an arrow: http://forums.esri.com/Thread.asp?t=248367&c=93&f=1154&ESRISessionID=VVH6mNCepIZNJVUggkPd2w9AhfkhhyV-9o-mpxM95RD8PChBwOuHeXW3MUO_

So, for anyone who wants to do the same thing in C#, here's some equivalent code with a few extras. All it does is create a line element named "ArrowTest" which is a simple line with an arrow at one end.

The initial code looks for any existing elements with that name and deletes them so you can run this repeatedly and tweak it until you're ready to use it in production. Note that at the start I build a list of elements to delete and then delete them in a separate loop. DeleteElement isn't guaranteed to work properly if you try calling it within the while loop because you're altering the list over which you're looping.

I'm using ArcObjects 10.1, but it should work in earlier versions.

//Set the element name
string elementName = "ArrowTest";

//Get the graphics container from the page layout (set elsewhere)
IActiveView activeView = (IActiveView)pageLayout;
IGraphicsContainer graphicsContainer = (IGraphicsContainer)pageLayout;

//Find all existing elements with specified name
//Build a list of elements and then loop over the list to delete them
List<IElement> elementsToDelete = new List<IElement>();
graphicsContainer.Reset();
IElementProperties3 elementProperties3 = null;
IElement element = null;
while ((element = graphicsContainer.Next()) != null)
{
    elementProperties3 = (IElementProperties3)element;
    if (elementProperties3.Name == elementName)
    {
 elementsToDelete.Add(element);
    }
}

foreach (IElement elementToDelete in elementsToDelete)
{
    graphicsContainer.DeleteElement(elementToDelete);
}

//Define color
IRgbColor rgbColor = new RgbColorClass();
rgbColor.RGB = Color.Black.ToArgb();

//Define an arrow marker
IArrowMarkerSymbol arrowMarkerSymbol = new ArrowMarkerSymbolClass();
arrowMarkerSymbol.Color = rgbColor;
arrowMarkerSymbol.Size = 6;
arrowMarkerSymbol.Length = 8;
arrowMarkerSymbol.Width = 6;
//Add an offset to make sure the square end of the line is hidden
arrowMarkerSymbol.XOffset = 0.8;

//Create cartographic line symbol
ICartographicLineSymbol cartographicLineSymbol = new CartographicLineSymbolClass();
cartographicLineSymbol.Color = rgbColor;
cartographicLineSymbol.Width = 1;

//Define simple line decoration
ISimpleLineDecorationElement simpleLineDecorationElement = new SimpleLineDecorationElementClass();
//Place the arrow at the end of the line (the "To" point in the geometry below)
simpleLineDecorationElement.AddPosition(1);
simpleLineDecorationElement.MarkerSymbol = arrowMarkerSymbol;

//Define line decoration
ILineDecoration lineDecoration = new LineDecorationClass();
lineDecoration.AddElement(simpleLineDecorationElement);

//Set line properties
ILineProperties lineProperties = (ILineProperties)cartographicLineSymbol;
lineProperties.LineDecoration = lineDecoration;

//Define line element
ILineElement lineElement = new LineElementClass();
lineElement.Symbol = (ILineSymbol)cartographicLineSymbol;

//Create the line geometry
IPoint fromPoint = new PointClass();
fromPoint.X = 4.0;
fromPoint.Y = 0.8;

IPoint toPoint = new PointClass();
toPoint.X = 5.0;
toPoint.Y = 0.8;

IPolyline polyline = new PolylineClass();
polyline.FromPoint = fromPoint;
polyline.ToPoint = toPoint;

//Cast to Element and set geometry
element = (IElement)lineElement;
element.Geometry = polyline;

//Set the name
elementProperties3.Name = elementName;

//Add element to graphics container
graphicsContainer.AddElement(element, 0);

//Clear the graphics selection (graphics are selected when added)
IGraphicsContainerSelect graphicsContainerSelect = (IGraphicsContainerSelect)graphicsContainer;
graphicsContainerSelect.UnselectAllElements();

activeView.Refresh();

Outcomes