4

This is related to the question I asked earlier about the syntax for this method under Python and comtypes. Now that I know the syntax and am trying to use it, the returned out params are equal to 0. Why is this? Here is my code:

#make TextElement
pTextElement = NewObj(esriCarto.TextElement, esriCarto.ITextElement)
pTextFont = NewObj(stdole.StdFont, stdole.IFontDisp)
pTextFont.Name = 'Arial'
pTextFont.Size = 34.0
pTextSymbol = NewObj(esriDisplay.TextSymbol, esriDisplay.IFormattedTextSymbol)
pTextSymbol.Font = pTextFont
pTextSymbol.HorizontalAlignment = esriDisplay.esriTHACenter
pTextSymbol.VerticalAlignment = esriDisplay.esriTVACenter
txt = 'T>'   #((( hard coded for testing )))
pScrDisp = mxDoc.ActiveView.ScreenDisplay
pDisplay = CType(pScrDisp, esriDisplay.IDisplay)
#Get dimensions of text string
pDisplay.StartDrawing(0, -1)
dblX, dblY = pTextSymbol.GetTextSize(pDisplay.hDC, pDisplay.DisplayTransformation, txt)
pDisplay.FinishDrawing

The out parameters dblX and dblY both equal 0. The hDC and DisplayTransformation inputs are valid. I checked. So, there is something I'm missing. Any insights?

1 Answer 1

3

If your script is not running inside the ArcMap's process, I think this is what's happening: The text symbol is created in the python's process, while the display object (map's or layout's screen display) inside the ArcMap's process. Processes in Windows cannot (by design) access GDI resources (like device contexts - the hDC in your code) which do not belong to them. Thus in the context of GetTextSize() call, the passed device context handle is invalid and measurement cannot be performed.

In order words, using IObjectFactory to create the symbols/elements in ArcMap's process (as discussed in other questions) will probably do the trick.

1
  • Exactly right, anything involving layout elements needs a drawing context to correctly get/set element metrics. arcpy.mapping does a lot of this behind the scenes, perhaps a hybridized approach here would work best. Commented Jan 12, 2011 at 21:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.