ARX修改文本对齐方式
void changeTextInnerAlign(){
//使用以下代码可以更换文本对齐方式,而不改变文字位置:
ads采用name name,ent;
AcDbEntity *pObj;
AcDbObjectId ObjId;
long length=0;
acutPrintf(采用T("\n选择需要对齐的文本实体:"));
if(acedSSGet(NULL,NULL,NULL,NULL,name)!=RTNORM) return;
acedSSLength(name,&length);
ArxDbgUiPrKeyWordDef prAlignType(采用T("修改后的对齐方式[左对齐(L)/水平居中(M)/右对齐(R)]"),
采用T("L M R"), 采用T("L"));
if (prAlignType.go() != ArxDbgUiPrBase::kOk)
return;
CString Justify;
if (prAlignType.isKeyWordPicked(采用T("L")))
Justify = "左对齐";
else if (prAlignType.isKeyWordPicked(采用T("M")))
Justify = "水平居中";
else if (prAlignType.isKeyWordPicked(采用T("R")))
Justify = "右对齐";
else
{
ASSERT(0);
return ;
}
int i; for( i=0;i<length;i++)
{
acedSSName(name,i,ent);
acdbGetObjectId(ObjId,ent);
acdbOpenObject(pObj,ObjId,AcDb::kForWrite,Adesk::kFalse);
AcDbExtents extents1, extents2;
if(pObj->isKindOf(AcDbText::desc()))//普通文本
{
AcDbText *pText=AcDbText::cast(pObj);
pText->getGeomExtents(extents1);
if(Justify==采用T("左对齐")) pText->setHorizontalMode(AcDb::kTextLeft);
else if(Justify==采用T("右对齐")) pText->setHorizontalMode(AcDb::kTextRight);
else if(Justify==采用T("水平居中")) pText->setHorizontalMode(AcDb::kTextCenter);
else pText->setHorizontalMode(AcDb::kTextLeft);
pText->setVerticalMode(AcDb::kTextBase);
}
else if(pObj->isKindOf(AcDbMText::desc()))//多行文本
{
AcDbMText *pMText=AcDbMText::cast(pObj);
pMText->getGeomExtents(extents1);
if(Justify==采用T("左对齐"))
{
if(pMText->attachment()==AcDbMText::kTopCenter ||
pMText->attachment()==AcDbMText::kTopRight)
pMText->setAttachment(AcDbMText::kTopLeft);
else if(pMText->attachment()==AcDbMText::kMiddleCenter ||
pMText->attachment()==AcDbMText::kMiddleRight)
pMText->setAttachment(AcDbMText::kMiddleLeft);
else if(pMText->attachment()==AcDbMText::kBottomCenter ||
pMText->attachment()==AcDbMText::kBottomRight)
pMText->setAttachment(AcDbMText::kBottomLeft);
}
else if(Justify==采用T("水平居中"))
{
if(pMText->attachment()==AcDbMText::kTopLeft ||
pMText->attachment()==AcDbMText::kTopRight)
pMText->setAttachment(AcDbMText::kTopCenter);
else if(pMText->attachment()==AcDbMText::kMiddleLeft ||
pMText->attachment()==AcDbMText::kMiddleRight)
pMText->setAttachment(AcDbMText::kMiddleCenter);
else if(pMText->attachment()==AcDbMText::kBottomLeft ||
pMText->attachment()==AcDbMText::kBottomRight)
pMText->setAttachment(AcDbMText::kBottomCenter);
}
else if(Justify==采用T("右对齐"))
{
if(pMText->attachment()==AcDbMText::kTopLeft ||
pMText->attachment()==AcDbMText::kTopCenter)
pMText->setAttachment(AcDbMText::kTopRight);
else if(pMText->attachment()==AcDbMText::kMiddleLeft ||
pMText->attachment()==AcDbMText::kMiddleCenter)
pMText->setAttachment(AcDbMText::kMiddleRight);
else if(pMText->attachment()==AcDbMText::kBottomLeft ||
pMText->attachment()==AcDbMText::kBottomCenter)
pMText->setAttachment(AcDbMText::kBottomRight);
}
}
pObj->close();
AcDbEntity *pEnt = NULL;
acdbOpenObject(pEnt, ObjId, AcDb::kForWrite);
if (pEnt == NULL)
return;
pEnt->getGeomExtents(extents2);
AcGeVector3d vec = extents1.minPoint() - extents2.minPoint();
pEnt->transformBy(AcGeMatrix3d::translation(vec));
pEnt->close();
}
}
页:
[1]