Fossi

Draw graphics in ArcMap from standalone application

Discussion created by Fossi on May 17, 2011
Latest reply on May 18, 2011 by agray1
Hi,

I want to highlight features in an running ArcMap instance by drawing graphics on the map.
This is what I put together so far:
public IElement AddGraphicToMap(IMap map, IGeometry geometry, IRgbColor rgbColor, IRgbColor outlineRgbColor)
{
    IGraphicsContainer graphicsContainer = (IGraphicsContainer)map; // Explicit Cast

    IElement element = null;
    if ((geometry.GeometryType) == esriGeometryType.esriGeometryPoint)
    {
        // Marker symbols
        ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
        simpleMarkerSymbol.Color = rgbColor;
        simpleMarkerSymbol.Outline = true;
        simpleMarkerSymbol.OutlineColor = outlineRgbColor;
        simpleMarkerSymbol.OutlineSize = 1.5;
        simpleMarkerSymbol.Size = 10;
        simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;

        IMarkerElement markerElement = new MarkerElementClass();
        markerElement.Symbol = simpleMarkerSymbol;
        element = (IElement)markerElement; // Explicit Cast
    }
    else if ((geometry.GeometryType) == esriGeometryType.esriGeometryPolyline)
    {
        //  Line elements
        ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
        simpleLineSymbol.Color = rgbColor;
        simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDot;   //.esriSLSSolid;
        simpleLineSymbol.Width = 1;

        ILineElement lineElement = new LineElementClass();
        lineElement.Symbol = simpleLineSymbol;
        element = (IElement)lineElement; // Explicit Cast
    }
    else if ((geometry.GeometryType) == esriGeometryType.esriGeometryPolygon)
    {
        // Polygon elements
        ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
        simpleFillSymbol.Color = rgbColor;
        simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSForwardDiagonal;
        IFillShapeElement fillShapeElement = new PolygonElementClass();
        fillShapeElement.Symbol = simpleFillSymbol;
        element = (IElement)fillShapeElement; // Explicit Cast
    }
    if (!(element == null))
    {
        element.Geometry = geometry;
        graphicsContainer.AddElement(element, 0);
    }
    return element;
}

private void button_Click(object sender, EventArgs e)
{
    //get running arcmap-instance
    IApplication application = null;
    IAppROT appRot = new AppROTClass();
    for (int i = 0; i < appRot.Count; i++)
    {
        if (appRot.get_Item(i) is IMxApplication)
        {
            application = appRot.get_Item(i);
        }
    }
    
    //get document handle
    IMxDocument mxDocument = (IMxDocument)application.Document;

    //create points
    IPoint fromPoint = new PointClass();
    IPoint toPoint = new PointClass();
    fromPoint.X = 0;
    fromPoint.Y = 0;
    toPoint.X = 9999999;
    toPoint.Y = 9999999;

    //create line
    IPolyline line = new PolylineClass();
    line.FromPoint = fromPoint;
    line.ToPoint = toPoint;

    //define color
    IRgbColor colorGreen = new RgbColorClass();
    colorGreen.Green = 200;

    //draw points and lines on the map
    AddGraphicToMap(mxDocument.ActiveView.FocusMap, fromPoint, colorGreen, colorGreen);
    AddGraphicToMap(mxDocument.ActiveView.FocusMap, line, colorGreen, colorGreen);
    AddGraphicToMap(mxDocument.ActiveView.FocusMap, toPoint, colorGreen, colorGreen);
    //refresh map to make changes visible
    mxDocument.ActiveView.Refresh();
}


I don't get any errors, it just happens nothing at all.

I used the AddGraphicToMap-snippet (http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//004900000067000000) before when building an add-in without any problems. So I assume there is something wrong with the handle mxDocument.

I racked my brain and I fear, I do not to see the wood for the trees. Could someone of the experts tell a noob what he is overlooking? ;)

Any hint will be appreciated.

Thanx in advance,
Fossi

Outcomes