Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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. –  Jason Scheirer Jan 12 '11 at 21:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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