4

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);

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 13, 2010 at 19:09

1 Answer 1

8

Before calling Display.DrawPolyline((IGeometry)m_Polyline), call Display.Setsymbol and pass something that implements ILineSymbol, casting it as ISymbol.

answered Oct 13, 2010 at 19:25
4
  • So now in my TableOfContents I have a CustomLayer Layer. However, there is nothing below it. Shouldn't I be seeing something on the screen? Or am I missing another call? Commented Oct 13, 2010 at 19:36
  • Not sure what you mean by "nothing below it", but if you want any symbols to be visible in the TOC, your custom layer will need to implement ILegendInfo interface. Commented Oct 13, 2010 at 19:40
  • That's what I meant...still very green to the ArcGIS stuff. Thanks! Commented Oct 13, 2010 at 19:41
  • Not sure, but I think you'll need to have your custom layer implement ILegendInfo. The easiest way to do that might be to have a member variable with an ISimpleRenderer, and cast it to ILegendInfo and forward all calls to it. Commented Oct 13, 2010 at 19:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.