找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 973|回复: 0

[分享] 使用AcGiTextStyle得到字体的准确宽度

[复制链接]

已领礼包: 13个

财富等级: 恭喜发财

发表于 2016-6-8 21:37:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  由于使用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
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-15 08:52 , Processed in 0.165355 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表