[每日一码] ARX插入块的实现代码
////添加块到块表中================static void TESTaddblkcmd(){
AcDbBlockTable* pBlkTbl;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl,AcDb::kForWrite);
////创建新块
AcDbBlockTableRecord* pBlkTblRcd;
pBlkTblRcd = new AcDbBlockTableRecord();
////根据用户的输入设置块表记录的名称
TCHAR blkName;
if(acedGetString(Adesk::kFalse,采用T("\n输入块名称"),blkName) != RTNORM){
pBlkTbl->close();
delete pBlkTblRcd;
return;
}
pBlkTblRcd->setName(blkName);
////将块表记录添加到块表中
AcDbObjectId blkDefId;
pBlkTbl->add(blkDefId,pBlkTblRcd);
pBlkTbl->close();
////向块表记录中添加实体
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();
}
////插入块参照
static void TESTaddblkrefcmd(){
////获得用户输入的块定义名称
CString strBlkDef;
if(acedGetString(Adesk::kFalse,采用T("\n输入块名称\n"),strBlkDef.GetBuffer()) != RTNORM){
return;
}
acutPrintf(strBlkDef);
////获得当前数据库的块表
AcDbBlockTable* pBlkTbl;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl,AcDb::kForWrite);
////查找用户指定的块定义是否存在
if(!pBlkTbl->has(strBlkDef)){
acutPrintf(采用T("\n当前图形中未包含块定义"));
pBlkTbl->close();
return;
}
////获得用户输入的块参照的插入点
ads采用point pt;
if(acedGetPoint(NULL,采用T("\n输入块参照的插入点"),pt) != RTNORM){
pBlkTbl->close();
return;
}
AcGePoint3d ptInsert = asPnt3d(pt);
////获得用户制定的块表记录
AcDbObjectId blkDefId;
pBlkTbl->getAt(strBlkDef,blkDefId);
////创建块参照对象
AcDbBlockReference* pBlkRef = new AcDbBlockReference(ptInsert,blkDefId);
////将块参照添加到模型空间
AcDbBlockTableRecord* pBlkTblRcd;
pBlkTbl->getAt(ACDB采用MODEL采用SPACE,pBlkTblRcd,AcDb::kForWrite);
AcDbObjectId entId;
pBlkTblRcd->appendAcDbEntity(entId,pBlkRef);
////关闭数据库对象
pBlkTbl->close();
pBlkTblRcd->close();
pBlkTbl->close();
}
////===================================================插入带有属性的块定义
static void TESTaddblk2cmd(){
////获得当前图形的数据库块表
AcDbBlockTable* pBlkTbl;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl,AcDb::kForWrite);
////创建新的块表记录
AcDbBlockTableRecord* pBlkTblRcd;
pBlkTblRcd = new AcDbBlockTableRecord();
////根据用户的输入设置块表记录的名称
CString blkName;
if(acedGetString(Adesk::kFalse,采用T("\n输入块表记录名称:"),blkName.GetBuffer()) != RTNORM){
pBlkTbl->close();
delete pBlkTblRcd;
return;
}
pBlkTblRcd->setName(blkName);
////将块表记录添加到块表中
AcDbObjectId blkDefId;
pBlkTbl->add(blkDefId,pBlkTblRcd);
pBlkTbl->close();
////向块表记录添加实体
AcGePoint3d ptStart(-10,0,0);
AcGePoint3d 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);
////创建一个属性,输入直径
AcDbAttributeDefinition* pAttDef = new AcDbAttributeDefinition(ptEnd,采用T("20"),采用T("直径"),采用T("输入直径"));
AcDbObjectId entId;
pBlkTblRcd->appendAcDbEntity(entId,pLine1);
pBlkTblRcd->appendAcDbEntity(entId,pLine2);
pBlkTblRcd->appendAcDbEntity(entId,pCircle);
pBlkTblRcd->appendAcDbEntity(entId,pAttDef);
////关闭实体和块表记录
pLine1->close();
pLine2->close();
pCircle->close();
pAttDef->close();
pBlkTblRcd->close();
}
页:
[1]