binbin的思路很好, 我在ADN上也提了这个问题,很多人都是这么想的:
可以参考一下下面这段代码:
 - [font=courier new]
- // Append a new entity to the model space
- // and close it.
- void PostToMs(AcDbEntity *pEnt)
- {
- AcDbBlockTable *pTable;
- AcDbBlockTableRecord *pRecord; acdbCurDwg()->getBlockTable(pTable, AcDb::kForRead);
- pTable->getAt(ACDB_MODEL_SPACE, pRecord, AcDb::kForWrite);
- pTable->close();
- pRecord->appendAcDbEntity(pEnt);
- pRecord->close();
- pEnt->close();
- }// This function creates a new text style
- // for an MTEXT fragment.
- // NOTE: This function fails when using
- // TrueType fonts (the font and bigfont
- // member contains an empty string).
- AcDbObjectId CreateStyle(AcDbMTextFragment *frag)
- {
- if (!frag)
- return AcDbObjectId::kNull; static int counter = 0;
- char buffer[33];
- AcDbTextStyleTableRecord *pStyle = new AcDbTextStyleTableRecord; // Create and set name
- itoa(counter++, buffer, 10);
- pStyle->setName(buffer); // Set parameters
- if (frag->font)
- pStyle->setFileName(frag->font);
- else
- pStyle->setFileName("txt.shx");
- if (frag->bigfont)
- pStyle->setBigFontFileName(frag->bigfont); ads_printf("\nFont: %s", frag->font); // Append the new style to the style table
- AcDbObjectId styleId;
- AcDbTextStyleTable *pTable; acdbCurDwg()->getTextStyleTable(pTable, AcDb::kForWrite);
- pTable->add(styleId, pStyle);
- pTable->close();
- pStyle->close(); return styleId;
- }// This function is the callback function for
- // explodeFragments(). Here you will get the
- // MTEXT fragments
- int MTextEnum(AcDbMTextFragment *frag, void *param)
- {
- AcDbText *pText = new AcDbText;
- AcDbObjectId styleId = CreateStyle(frag); if (styleId == AcDbObjectId::kNull)
- return 1; pText->setTextString(frag->text);
- pText->setPosition(frag->location);
- pText->setNormal(frag->normal);
- pText->setOblique(frag->obliqueAngle);
- pText->setWidthFactor(frag->widthFactor);
- pText->setColorIndex(frag->colorIndex);
- pText->setHeight(frag->capsHeight);
- pText->setTextStyle(styleId); PostToMs(pText); return 1;
- }void expmtext()
- {
- // Let the user select an MTEXT entity ads_name ename;
- ads_point pt; if (RTNORM != acedEntSel("\nSelect a mtext entity: ", ename, pt))
- return; AcDbObjectId objId;
- AcDbEntity *pEnt; acdbGetObjectId(objId, ename);
- if (Acad::eOk != acdbOpenAcDbEntity(pEnt, objId, AcDb::kForRead)) {
- ads_printf("\nCannot open selected entity.");
- return;
- } if (!pEnt->isKindOf(AcDbMText::desc())) {
- ads_printf("\nSelected entity is not a mtext entity.");
- pEnt->close();
- return;
- } AcDbMText *pMText = (AcDbMText*) pEnt; // Get every MTEXT fragment
- pMText->explodeFragments(MTextEnum, NULL); pMText->close();
- }
- [/font]
|