# ArcMap polyline renders incorrectly unless project reloaded

I am trying to get a graphic polyline to render over top of an imported shapefile.

I'm using C# with ArcMap 10.2.2.

The issue I'm having is that the first time the polyline is created, it merely draws a red dot in the top left corner of the map like so:

I believed this to be a projection issue, but after doing a File > Save As and saving it the .mxd file and re-opening it, I am greeted with an appropriately drawn polyline:

It would appear that something isn't getting initialized properly when I am initially creating my polyline.

Creating and adding polyline as element to graphics container:

            IMxDocument mxDocument = (IMxDocument)this.Document;
IActiveView activeView = mxDocument.ActiveView;
IScreenDisplay screenDisplay = activeView.ScreenDisplay;
IGraphicsContainer graphicsContainer = activeView.GraphicsContainer;

IGeographicCoordinateSystem geoCoordinateSystem =
screenDisplay.DisplayTransformation.SpatialReference as IGeographicCoordinateSystem;

// segments are just two geo coordinates
// each a lat/long pair represented as
// doubles, see the MakePolyline method
// for more details
var segments = dataProvider.GetSegments();

IPolyline line = this.MakePolyline(segments, geoCoordinateSystem);

var lineSymbol = new SimpleLineSymbolClass();
lineSymbol.Width = 3;
lineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
lineSymbol.Color = new RgbColorClass { Red = 255, Green = 0, Blue = 0 };

ILineElement lineElement = new LineElementClass();
lineElement.Symbol = lineSymbol;

IElement element = (IElement)lineElement;
element.Geometry = line;

activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);


MakePolyline method:

    private IPolyline MakePolyline(IEnumerable<IGeoPolylineSegment> segments, ISpatialReference spatialReference)
{
IPointCollection4 pointCollection = new PolylineClass();
object missing = Type.Missing;

foreach (IGeoPolylineSegment segment in segments)
{
this.MakePoint(segment.Start.Longitude, segment.Start.Latitude, spatialReference),
ref missing,
ref missing);
this.MakePoint(segment.End.Longitude, segment.End.Latitude, spatialReference),
ref missing,
ref missing);
}

IPolyline polyLine = (IPolyline)pointCollection;
polyLine.SpatialReference = spatialReference;
return polyLine;
}


MakePoint method

private IPoint MakePoint(double x, double y, ISpatialReference spatialReference)
{
IPoint p = new PointClass();
p.PutCoords(x, y);
p.SpatialReference = spatialReference;
return p;
}

• It happens a lot gis.stackexchange.com/questions/121882/… sorry no idea why – FelixIP Feb 9 '16 at 22:34
• @FelixIP that's a bummer. Thanks for the link though, might lead me in another direction to find a work-around. Cheers. – Nick Funk Feb 9 '16 at 22:40
• I just copy it to another feature class, and it works – FelixIP Feb 9 '16 at 22:42
• Just an observation and may be worth testing but you get the graphics container from the activeview which is not guaranteed to be the map. I assume that you want to add the graphic to the map and not pagelayout? If you look at the Add Graphic to Map Snippet in the API help they get it from the Map object directly. – Hornbydd Feb 9 '16 at 23:17
• Then there's the option of talking to Esri Tech Support on the matter. In the meantime, please edit the question include the images locally, since far fewer people will look at them as links, and the question itself is less useful without them if they go missing. – Vince Feb 10 '16 at 2:17

I believe I have found the solution to my problem, as the polylines are now rendering correctly.

Instead of doing this to make, say a polyline:

IPolyline polyline = new PolylineClass();


You need to use an instantiated IObjectFactory from the IApplication to generate the object in the COM memory space that ArcMap manages:

IObjectFactory objectFactory = (IObjectFactory)ArcMap.Application;
IPolyline polyline = (IPolyline)objectFactory.Create("esriCore.Polyline");


This must be done for all objects not previously generated by ArcMap so that when the COM handle to that object is used to find objects within ArcMap, a valid handle is returned and ArcMap will know how to use the external (add-in generated) objects.

The reason why before, after closing and reloading the project, the polyline would render, is because when the project was reloaded, ArcMap would re-generate the polyline with geometry in the COM memory space, resulting in a properly rendered polyline (but only after closing and re-opening).

I hope this helps save some other poor soul in the future.