Python ArcObjects – Drawing Graphics Onscreen using Draw, Refresh, and PartialRefresh Methods

arcobjectspython

I find myself woefully lacking in understanding of when, why, and how (which type) to do screen draw/refresh/partial refresh. For instance, in a standalone Python script outside the application boundary, I have a function that is trying to draw graphic elements in the data view. Specifically, the elements will be a TextElement on top of a RectangleElement. The function loads the two elements into a GroupElement, which is in turn added to the ActiveView's GraphicsContainer for drawing on the screen. (As far as I can tell the graphic elements are valid.) I am really not understanding what Draw/Refresh/ParitalRefresh methods to put where. If anyone can give/point to a good explanation or tutorial about screen drawing/refreshing I would appreciate it. Below is a bit of selected code from my function. I have a PartialRefresh in the last line, but I don't know if that is correct or not.

pGroupElement = NewObj(esriCarto.GroupElement, esriCarto.IGroupElement3)
pTextElement = NewObj(esriCarto.TextElement, esriCarto.ITextElement)
pRectElement = NewObj(esriCarto.RectangleElement, esriCarto.IElement)
.
.
(configure TextElement)  [tedious code removed]
.
.
(configure RectElement)
.
.
ptxtElem = CType(pTextElement, esriCarto.IElement)

pGroupElement.AddElement(ptxtElem)
pGroupElement.AddElement(pRectElement)

pContainer = mxDoc.ActiveView.GraphicsContainer

pGrpElem = CType(pGroupElement, esriCarto.IElement)

pContainer.AddElement(pGrpElem, 0)

mxDoc.ActiveView.PartialRefresh(esriCarto.esriViewGraphics, None, None)

Added: So, is creating objects with the following code – as I do now – not creating them in process?

def NewObj(MyClass, MyInterface):
    """Creates a new comtypes POINTER object where\n\
    MyClass is the class to be instantiated,\n\
    MyInterface is the interface to be assigned"""
    from comtypes.client import CreateObject
    try:
        ptr = CreateObject(MyClass, interface=MyInterface)
        return ptr
    except:
        return None

If not, I gather I must use IObjectFactory. Assuming that my current objects are not in process, I must say I haven't seen any great performance hit, but I guess I'm just asking for trouble forcing lots of interprocess calls.

Thank you, all, for the help. I shall experiment when next I get the chance and let you know the results.

Best Answer

If you're "in a standalone Python script outside the application boundary" I think you'll need to use IObjectFactory. See page 13 of http://www.pierssen.com/arcgis/upload/misc/python_arcobjects.pdf

Update Also, if you're trying to refresh the display across a process boundary, you may need to send some extra windows messages to force a refresh. Here's a good explanation.

Related Question