使用AcGiTextStyle获得MTEXT的准确的宽度

  由于使用AcDbMtext的extents()方法,只能得到MTEXT的外框,得不到字体的准确宽度,下面是用AcGiTextStyle的解决方法,得到准确的字体宽度。 


HOW TO GET A TEXT STRING'S ACTUAL WIDTH 
Product AUTOCAD Author LEE,HENRY 
Date 26-MAR-99 Document ID 29095 
Expiration date Attachments 
Keywords API_R14_OK; API_TAHOE_OK; ARX 

Developer Consulting Group technical solution. Autodesk confidential, for ADN members only. Please read the disclaimer 

Question 


We are trying to determine the actual width of the text in an AcDbMText 
entity. There is an extents() function which gives the width of the bounding 
box. However What I need is the length of the text itself and not its 
bounding box. Many times it so happens that the bounding box is much 
larger than the actual text itself. Is there a way of doing that? 


Answer 


Yes, there is a way but not directly from AcDbMText methods. What you said is 
true so we must look else where to find an solution. It is from AcGiTextStyle 
which has a method called extents(). It gives you the actual width in an 
AcGePoint2d. 

Here is a code snippet which demonstrates how to obtain the string width from 
a user input string. 


void getTextStrWidth() 

char str[132]; 
int rt = acedGetString(true, "/nEnter a string: ", str); 
if(rt != RTNORM) 

acutPrintf("/nInvalid input, try again."); 
return; 


AcGiTextStyle iStyle; 
AcDbTextStyleTable* pTable = NULL; 
AcDbTextStyleTableRecord* pRecord = NULL; 
try{ 
ARXOK(curDoc()->database()->getTextStyleTable(pTable, 
AcDb::kForRead)); 
const char styleName[] = "STANDARD"; 
ARXOK(pTable->getAt(styleName, pRecord, AcDb::kForRead)); 
ARXOK(fromAcDbTextStyle(iStyle, pRecord->objectId())); 
pRecord->close(); 
pTable->close(); 

catch(const Acad::ErrorStatus es) 

acutPrintf("/nError: %s", acadErrorStatusText(es)); 
pRecord->close(); 
pTable->close(); 

AcGePoint2d pt = iStyle.extents(str, Adesk::kFalse, _tcslen(str), 
Adesk::kTrue); 
// get the width 
acutPrintf("/nText string width is: /t.", pt.x); 
     // get the height too 
     acutPrintf("/nText string height is: /t.", pt.y); 


//declare the following inline function in a header file 
inline void ARXOK(Acad::ErrorStatus what) throw(Acad::ErrorStatus) 

    if (what!=Acad::eOk) 
     throw what; 



Note: You have to strip some format characters off an AcDbMText text string 
which is some extra work. Otherwise, the width calculation above will not be 
accurate. 

To make the dimension meaningful for OUTLINETEXTMETRIC, you'll need to 
translate the point from WCS to the screen pixel size using 
acedCoordFromWorldToPixel(). 


Disclaimer 

This information is derived from a response to a specific question sent to the Developer Consulting Group at Autodesk, and thus given its nature, is subject to change without notice. The information is provided to you on an "as is" basis, and you may use it only at your own risk. Autodesk is not responsible for its quality, nor shall it be liable for any damage or loss caused by your use of this information. 

Top 

你可能感兴趣的:(ARX学习)