Draw graphics in ArcMap from standalone application

1760
3
05-17-2011 05:05 AM
FossiG_
New Contributor III
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
0 Kudos
3 Replies
AlexanderGray
Occasional Contributor III
I suggest you have a look at this article:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000001nn000000

The problem I see, if you are creating geometries, symbols and graphic elements in the memory space of the process of your application and trying to add them into the map which is running in a different memory space in the ArcMap.exe process.  You need to use the objectFactory if you want to do that sort of thing.
0 Kudos
FossiG_
New Contributor III
Alexander,

thanx for the clarification. This explains, why nothings seems to happen at all. I'm still trying to understand the idea of ObjectFactory. :confused:

But before digging deeper into this - would it be a possibility to trigger an add-in from an external program? From my limited understanding an add-in would not have this problem, since it is in the same memory space, which explains why the code worked when using it as an add-in.

So can add-in code be called from an external program?

TIA,
Fossi
0 Kudos
AlexanderGray
Occasional Contributor III
Hi Fossi,

I have done this, triggering code inside ArcMap from outside.  I didn't use an addin, I used the old way of writing code registering the dll but I think it would be the same.  The way I did it was to write a command, then from the external process, through the approt, get the arcmap instance, then through application's document commandbars, you can find the command by classid and invoke it (call onclick.) 

If you need to pass in data to make whatever you need to happen, that is a little trickier.  I made an extension (extension instances are unique to the arcmap process) got a reference to extension from the external process through the application, set the values (literals only: string, integer, double..., no geometry, extent, etc.)  Then invoke the command, and the command retrieves the values from the extension and does its thing.

Trying the put the code in the extension and invoking a method on the extension, even by raising a custom event on the extension always caused me problems with the process memory space.
0 Kudos