如何用C++在CAD二次开发中,对标注AcDbDimension文字的高度与宽度的获取方法?
#include "acdb.h"#include "adslib.h"
#include "aced.h"
#include "dbmain.h"
#include "dbdim.h"
#include "dbtext.h"
// 获取标注文字高度和宽度
void GetDimensionTextWidthHeight(AcDbDimension* pDim, double& dTextHeight, double& dTextWidth)
{
// 获取标注样式
AcDbObjectId dimStyleId = pDim->dimensionStyle();
AcDbDimStyleTableRecord* pStyle = nullptr;
acdbOpenObject(pStyle, dimStyleId, AcDb::kForRead);
// 获取文字高度
dTextHeight = pStyle->dimtxt();
// 获取文字宽度需要计算
AcDbMText* pMText = pDim->dimensionText();
if (pMText != nullptr)
{
// 获取文字内容
CString textString;
pMText->getString(textString);
// 获取文字样式
AcDbObjectId textStyleId = pMText->textStyle();
AcDbTextStyleTableRecord* pTextStyle = nullptr;
acdbOpenObject(pTextStyle, textStyleId, AcDb::kForRead);
// 计算文字宽度
dTextWidth = textString.GetLength() * dTextHeight * pTextStyle->xScale();
// 释放资源
pTextStyle->close();
pMText->close();
}
// 释放资源
pStyle->close();
}
// 测试函数
void TestGetDimensionTextWidthHeight()
{
// 获取当前图形数据库
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
// 开始事务
AcDbBlockTableRecord* pBlockTableRecord;
pDb->getSymbolTable(pBlockTableRecord, AcDb::kForRead);
AcDbBlockTableRecordIterator* pIter;
pBlockTableRecord->newIterator(pIter);
AcTransaction* pTrans = pDb->transactionManager()->startTransaction();
// 遍历模型空间中的标注
for (pIter->start(); !pIter->done(); pIter->step())
{
AcDbObjectId entId = pIter->objectId();
AcDbEntity* pEnt;
if (acdbOpenObject(pEnt, entId, AcDb::kForRead) == Acad::eOk)
{
if (pEnt->isKindOf(AcDbDimension::desc()))
{
AcDbDimension* pDim = AcDbDimension::cast(pEnt);
double dTextHeight, dTextWidth;
GetDimensionTextWidthHeight(pDim, dTextHeight, dTextWidth);
// 打印标注文字高度和宽度
acutPrintf(_T("\n标注文字高度:%.2f"), dTextHeight);
acutPrintf(_T("\n标注文字宽度:%.2f\n"), dTextWidth);
}
pEnt->close();
}
}
// 结束事务
pTrans->commit();
delete pIter;
pBlockTableRecord->close();
}
页:
[1]