CAD控件目前提供常用的ARX编程接口,下面演示一些ARX编程接口的使用。
获得当前活动的McDbDatabase数据库的代码如下:
McDbDatabase * pDatabase = Mx::mcdbHostApplicationServices()->workingDatabase();
打开数据库中的模型空间块表记录的代码如下(使用智能指针的方式打开):
ter spBlockRecord(ACDB_MODEL_SPACE, pDatabase, AcDb::kForRead); if (Acad::eOk == spBlockRecord.openStatus()) return;
创建块表记录遍历使用的迭代器的代码如下:
AcDbBlockTableRecordIterator * pIter = nullptr; spBlockRecord->newIterator(pIter); for (pIter->start();!pIter->done();pIter->step()) { AcDbEntity * pEnt = nullptr; if (pIter->getEntity(pEnt, AcDb::kForRead) != Acad::eOk) continue; //对实体的信息的使用... pEnt->close(); }
释放迭代器的内存,打开实体的代码如下:
McDbObjectPointer<McDbEntity> spEnt(id,McDb::kForRead);
判断实体是否是一个圆的代码如下:
if(McDbCircle::cast(spEnt.object()) != NULL) { //....... }
绘表过程,就是往McDbDatabase数据库添加实体的过程,详细代码可参考CAD控件\Src\MxDraw5.2\samples\Edit实例的代码,运行效果如下所示:
绘表算法这里不再赘述,若需要可参见上述的实例,这里将演示部分关于ARX的代码。 把图元对象添加到数据库的模型空间中的代码如下:
AcDbObjectId CDrawTable::AddToModelSpace(AcDbEntity* pEntity,AcDbDatabase* pDatabase) { AcDbObjectId objId; AcDbBlockTableRecordPointer spBlkRec(ACDB_MODEL_SPACE,pDatabase,AcDb::kForWrite); if(spBlkRec.openStatus() != Acad::eOk) return AcDbObjectId::kNull; spBlkRec->appendAcDbEntity(objId, pEntity); pEntity->close(); return objId; }
往数据库中画一条直线的代码如下:
AcDbObjectId CDrawTable::DrawLine(AcGePoint3d pt1,AcGePoint3d pt2,double dLineWidth,AcDbDatabase* pDatabase) { if(fabs(dLineWidth) <= 0.00001) { AcDbLine* pLine = new AcDbLine(pt1,pt2); // test //pLine->setLineWeight(McDb::kLnWt015); return AddToModelSpace(pLine,pDatabase); } else { AcDbPolyline* pPL = new AcDbPolyline; pPL->setConstantWidth(dLineWidth); pPL->addVertexAt(0,pt1.convert2d(AcGePlane::kXYPlane)); pPL->addVertexAt(1,pt2.convert2d(AcGePlane::kXYPlane)); return AddToModelSpace(pPL,pDatabase); } }
居中方式绘制一段文字的代码如下:
AcDbObjectId CDrawTable::DrawMCText(AcGePoint3d pt,const CString& sTxt,double dTextHeight,AcDbDatabase* pDatabase) { AcDbText *pText = NULL; pText = new AcDbText; pText->setHorizontalMode(AcDb::kTextCenter); pText->setVerticalMode(AcDb::kTextVertMid); //pText->setVerticalMode(AcDb::kTextBottom); pText->setPosition(pt); pText->setAlignmentPoint(pt); pText->setTextString(sTxt); pText->setHeight(dTextHeight ); pText->setColorIndex(MrxDbgUtils::kGreen); // test //pText->setRotation(3.14159265 / 4.0); return AddToModelSpace(pText,pDatabase); }