Select to view content in your preferred language

Drawing a Line on the ScreenDisplay

1747
4
09-23-2010 07:18 AM
BBulla
by
Regular Contributor
Hi,

I'm using the following code to draw a line on the screen (ie. not a feature).  I'm using essentially the same methodolgy to successfully draw a point , but for some reason it won't work for a line.  I don't get any error messages, I just don't see anything on the screen.

I'm thinking it might be how I am setting the geometry of the line, but logically it seems like it should work.

Any ideas??

public static void DrawPolyline(ESRI.ArcGIS.Carto.IActiveView activeView, IPoint fromPoint, IPoint toPoint)
        {
            if (activeView == null)
            {
                return;
            }

            //Get the screen display
            ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

            //Start drawing
            screenDisplay.StartDrawing(screenDisplay.hDC, 
                (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache);

            //Create the default line symbol
            ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = new
                ESRI.ArcGIS.Display.SimpleLineSymbolClass();
            ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
            rgbColor.Red = 255;
            ESRI.ArcGIS.Display.IColor color = rgbColor;
            simpleLineSymbol.Color = color;
            simpleLineSymbol.Width = 4;

            //Create the symbol that will be drawn as the default line symbol
            ESRI.ArcGIS.Display.ISymbol symbol = (ESRI.ArcGIS.Display.ISymbol)simpleLineSymbol;

            //Create a geometry using the to and from points
            ILine line = new LineClass();
            line.PutCoords(fromPoint, toPoint);
            ESRI.ArcGIS.Geometry.IGeometry geometry = (ESRI.ArcGIS.Geometry.IGeometry)line;

            //Finally, draw the line using the geometry
            screenDisplay.SetSymbol(symbol);
            screenDisplay.DrawPolyline(geometry);
            screenDisplay.FinishDrawing();
        }
0 Kudos
4 Replies
NeilClemmons
Honored Contributor
You're creating a Line instead of a Polyline.  A Line and a Polyline are not the same thing and cannot be substituted one for the other.  Try creating a new Polyline and adding the Line as a segment, then pass the Polyline to the DrawPolyline method.
0 Kudos
BBulla
by
Regular Contributor
Hi Neil,

Thanks once again for your help.  Using:

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


works like a charm.

One question though:  both Line and Polyline inherit IGeometry.  So why does one work and not the other??  Is there something in the SDK Help that I should look for when troubleshooting these sorts of things, or is it just something I will learn with time.

Thanks again.
0 Kudos
NeilClemmons
Honored Contributor
The Geometry object classes fall into 2 categories:  upper level geometry types and lower level geometry types.  The upper level types are Point, MultiPoint, Polyline, and Polygon.  The lower level types are classes such as Line, Segment, Path, and Ring.  In many cases, high level ArcObjects interfaces and/or methods only work on high level geometries.  The documentation usually tells you what will work.  In this case, the method is named DrawPolyline (which is a good indicator) and the documentation states that it draws a Polyline object.  It can get confusing sometimes though because the documentation is incorrect in some places.  Again, in this instance the description of the DrawPolyline method on the IScreenDisplay interface help topic states it draws a line (instead of a polyline) whereas is states Polyline in the Remarks section on the actual method help topic.  My general approach is to always read the help first.  If it doesn't shed any light, then I try it with both a high level and a low level object and see what happens.  The main thing is just to be aware that there are two classes of geometries so that when something like this happens you know to try a different geometry type.
0 Kudos
BBulla
by
Regular Contributor
Hi Neil,

Thanks for the great explanation!  I should have figured from the .DrawPolyline method that I should be using a Polyline, but I just figured that a geometry was a geometry regardless of Line or Polyline.  Thanks for clarifying!

You should write some books or do some custom training.  You always have good answers and obviously know your stuff inside-out.
0 Kudos