使用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 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值