[每日一码] (1)AcDbText实体转换成AcDbMtext
//create a new AcDbMtext entity and copy properties from the selected AcDbTextvoid fTextToMText(AcDbObjectId mTextId)
{
//open the text entity for read
AcDbText *pText = NULL;
acdbOpenObject(pText,mTextId,AcDb::kForWrite);
if(NULL == pText)
{
acutPrintf("\nERROR: unable to open the text entity for write");
return;
}
//create a new AcDbMText
AcDbMText *pMText = new AcDbMText;
//set Mtext properties
pMText->setTextHeight(pText->height());
pMText->mirrorInX(pText->isMirroredInX());
pMText->mirrorInY(pText->isMirroredInY());
pMText->setNormal(pText->normal());
pMText->setOblique(pText->oblique());
pMText->setRotation(pText->rotation());
pMText->setContents(pText->textString());
pMText->setTextStyle(pText->textStyle());
pMText->setThickness(pText->thickness());
pMText->setLayer(pText->layerId());
pMText->setColorIndex(1); //change this later to pText->colorIndex()
//set the attachment point
pMText->setAttachment(fSelectMtextAlign(pText));
//set the location of MText
//get the bounding box of text entity
AcDbExtents mExtText;
pText->getGeomExtents(mExtText);
AcGePoint3d mMtxtInsPt = AcGePoint3d(0,0,0);
AcDbExtents mExtMText;
pMText->setLocation(mMtxtInsPt);
pMText->getGeomExtents(mExtMText);
mMtxtInsPt = mMtxtInsPt + (mExtText.maxPoint() - mExtMText.maxPoint());
pMText->setLocation(mMtxtInsPt);
//append Mtext Object to Database
AcDbBlockTableRecord *pBTR = NULL;
acdbOpenObject(pBTR, pText->blockId(), AcDb::kForWrite);
if(NULL == pBTR)
{
acutPrintf("\nERROR: unable to open the block table record");
pText->close();
delete pMText;
return;
}
pBTR->appendAcDbEntity(pMText);
pBTR->close();
//pText->erase(); //not erasing the AcDbText entity for comparision
pText->close();
pMText->close();
}
//function to map the vertical and horizontal mode from AcDbText to AcDbMText
AcDbMText::AttachmentPoint fSelectMtextAlign(AcDbText *pAttTxt)
{
AcDb::TextVertMode mVertMode;
AcDb::TextHorzMode mHorzMode;
mVertMode = pAttTxt->verticalMode();
mHorzMode = pAttTxt->horizontalMode();
if (AcDb::kTextBottom == mVertMode)
{
mVertMode = AcDb::kTextBase;
}
switch (mHorzMode)
{
case AcDb::kTextAlign:
return AcDbMText::AttachmentPoint::kBottomLeft;
case AcDb::kTextMid:
return AcDbMText::AttachmentPoint::kMiddleCenter;
case AcDb::kTextFit:
return AcDbMText::AttachmentPoint::kBottomLeft;
}
switch (mVertMode)
{
case AcDb::kTextTop:
switch (mHorzMode)
{
case AcDb::kTextLeft:
return AcDbMText::AttachmentPoint::kTopLeft;
case AcDb::kTextCenter:
return AcDbMText::AttachmentPoint::kTopCenter;
case AcDb::kTextRight:
return AcDbMText::AttachmentPoint::kTopRight;
break;
}
case AcDb::kTextVertMid:
switch (mHorzMode)
{
case AcDb::kTextLeft:
return AcDbMText::AttachmentPoint::kMiddleLeft;
case AcDb::kTextCenter:
return AcDbMText::AttachmentPoint::kMiddleCenter;
case AcDb::kTextRight:
return AcDbMText::AttachmentPoint::kMiddleRight;
}
case AcDb::kTextBase:
switch (mHorzMode)
{
case AcDb::kTextLeft:
return AcDbMText::AttachmentPoint::kBottomLeft;
case AcDb::kTextCenter:
return AcDbMText::AttachmentPoint::kBottomCenter;
case AcDb::kTextRight:
return AcDbMText::AttachmentPoint::kBottomRight;
break;
}
}
return AcDbMText::AttachmentPoint::kTopLeft;
} pMText->mirrorInX(pText->isMirroredInX());
pMText->mirrorInY(pText->isMirroredInY());
pMText->setOblique(pText->oblique());
pMText->setThickness(pText->thickness());
上述方法在2004以上版本已经没有了,没找到替代方法! 添加了写代码,以修正pText->isMirroredInY()和pText->isMirroredInY(),不知道是否适合所有情况,自己测试了下,暂时还没发现问题!
//set the attachment point
pMText->setAttachment(getMtextAlign(pText));
//以下我代码我添加,用以修正pText->isMirroredInY()和pText->isMirroredInY()
AcGeVector3d normal =pText->normal();
AcGeVector3d dirVec = pMText->direction();
if (pText->isMirroredInX())
{
normal.negate();
pMText->setNormal(normal);
dirVec.set(-dirVec.x,-dirVec.y,dirVec.z);
pMText->setDirection(dirVec);
}
if (pText->isMirroredInY())
{
normal.negate();
pMText->setNormal(normal);
pMText->setRotation(pText->rotation()+Pi);
}
//以上代码为我添加
//set the location of MText
//get the bounding box of text entity
AcDbExtents mExtText;
页:
[1]