定义一个全局命令,功能:使用类库创建一个块表记录,并插入块;
static void TESTaddblkcmd(){
////根据用户的输入设置块表记录的名称
CString blkName;
if (acedGetString(Adesk::kFalse,采用T("\n输入块名称"),blkName.GetBuffer(20)) != RTNORM)
{
return;
}
////用完getBuffer要及时release
blkName.ReleaseBuffer();
AcDbBlockTable* pBlkTbl;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl,AcDb::kForWrite);
AcDbObjectId rcdId ;
////如果没有,添加新的
if (!pBlkTbl->has(blkName))
{
////创建新块
AcDbBlockTableRecord* pBlkTblRcd;
pBlkTblRcd = new AcDbBlockTableRecord();
pBlkTblRcd->setName(blkName);
////将块表记录添加到块表中,设置ID
pBlkTbl->add(rcdId,pBlkTblRcd);
////向块表记录中添加实体
AcGePoint3d ptStart(-10,0,0),ptEnd(10,0,0);
AcDbLine* pLine1 = new AcDbLine(ptStart,ptEnd);
ptStart.set(0,-10,0);
ptEnd.set(0,10,0);
AcDbLine *pLine2 = new AcDbLine(ptStart,ptEnd);
AcGeVector3d vecNormal(0,0,1);
AcDbCircle *pCircle = new AcDbCircle(AcGePoint3d::kOrigin,vecNormal,6);
AcDbObjectId entId;
pBlkTblRcd->appendAcDbEntity(entId,pLine1);
pBlkTblRcd->appendAcDbEntity(entId,pLine2);
pBlkTblRcd->appendAcDbEntity(entId,pCircle);
////关闭实体块表记录
pLine1->close();
pLine2->close();
pCircle->close();
pBlkTblRcd->close();
}////如果已经有了,那就根据名字读出RECORDID,插入块参照时会用到
else
{
pBlkTbl->getAt(blkName,rcdId,false);////以读的方式获取
}
////在和用户交互之前,需要关闭块表,否则容易出错
pBlkTbl->close();
////插入块
////获得用户输入的块参照的插入点
ads采用point pt;
if(acedGetPoint(NULL,采用T("\n输入块参照的插入点"),pt) != RTNORM)
{
pBlkTbl->close();
return;
}
AcGePoint3d ptInsert = asPnt3d(pt);
////创建块参照
AcDbBlockReference* pBlkRef = new AcDbBlockReference(ptInsert,rcdId);
////将块参照添加到模型空间
AcDbBlockTableRecord* pBlkTblRcd2;
pBlkTbl->getAt(ACDB采用MODEL采用SPACE,pBlkTblRcd2,AcDb::kForWrite);
pBlkTblRcd2->appendAcDbEntity(pBlkRef);
////关闭数据库对象
pBlkRef->close();
pBlkTblRcd2->close();
pBlkTbl->close();
}
页:
[1]