I'm trying to create a custom layer, nothing crazy just press a button and have something show up on the display and also have the layer display in the TOC.
Well, the layer shows in the TOC but nothing shows on the display and the resulting error in my Debug tells me
A first chance exception of type 'System.Runtime.InteropService.ComException' occured in MapControlApplication1.exe
I'm going with fairly straight forward code.
I've created a BaseCommand that ties into the toolbar. When I press the button on the toolbar it successfully goes to Onclick. Here is the meaningful code in the OnClick
CustomLayer2 layer = new CustomLayer2();
layer.Name = "Custom Layer";
mapControl1.AddLayer(layer , 0);
Here is the code for CustomLayer2
public class CustLayer2: BaseCustomLayer
{
private IPolyline m_Polyline = null;
public override void Draw(esriDrawPhase drawPhase, IDisplay Display, ITrackCancel trackCancel)
{
if (drawPhase != esriDrawPhase.esriDPGeography)
return;
if (m_Polyline == null)
{
IProjectedCoordinateSystem pcs = Display.DisplayTransformation.SpatialReference as IProjectedCoordinateSystem;
if (pcs != null)
{
m_Polyline = MakePolyline(pcs.GeographicCoordinateSystem);
m_Polyline.Project(pcs);
}
else
{
m_Polyline = MakePolyline(new UnknownCoordinateSystemClass());
}
}
Display.DrawPolyline((IGeometry)m_Polyline);
}
public static IPolyline MakePolyline(ISpatialReference sr)
{
IPointCollection pc = new PolylineClass();
object missing = Type.Missing; // (not needed with VB.NET)
pc.AddPoint(MakePoint(-98.0, 29.0, sr), ref missing, ref missing);
pc.AddPoint(MakePoint(-97.0, 28.0, sr), ref missing, ref missing);
pc.AddPoint(MakePoint(-96.0, 27.0, sr), ref missing, ref missing);
pc.AddPoint(MakePoint(-95.0, 26.0, sr), ref missing, ref missing);
IPolyline polyline = (IPolyline)pc;
polyline.SpatialReference = sr;
return polyline;
}
public static IPoint MakePoint(double x, double y, ISpatialReference sr)
{
IPoint p = new PointClass();
p.PutCoords(x, y);
p.SpatialReference = sr;
return p;
}
}
Any ideas on why this would error out? Note that a new layer is shown in the TOC but obviously the Draw never finishes. The error is occuring on the Display.DrawPolyline((IGeometry)m_Polyline);
Answer
Before calling Display.DrawPolyline((IGeometry)m_Polyline), call Display.Setsymbol and pass something that implements ILineSymbol, casting it as ISymbol.
No comments:
Post a Comment