- 积分
- 211
- 明经币
- 个
- 注册时间
- 2014-8-20
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
下面是两段ARX定义并引用图块的代码,第一种方式成功了,但第二种未成功,请帮忙分析一下错误在哪,是语法错了、思路错了、理解错了?不多说了,贴代码:
第一种:(成功了)
BOOL TestDlg::testBlock1()
{
Acad::ErrorStatus es;
AcDbDatabase* pDatabase;
AcDbDatabase *extDb;
AcDbObjectId blockId0;
AcDbObjectId blockId1;
extDb = new AcDbDatabase;
extDb->readDwgFile(_T("d:\\rsk_rect.dwg"));
pDatabase = acdbHostApplicationServices()->workingDatabase();
es = pDatabase->insert(blockId0,_T("testBlock1"),extDb,true);
if(es != Acad::eOk){
return false;
}
/*********以上为图块定义,以下为图块引用******************/
AcDbBlockTableRecordPointer spBlkRec( _T("testBlock1"),pDatabase, AcDb::kForRead );
if( spBlkRec.openStatus() == Acad::eOk )
{
blockId1 = spBlkRec->objectId();
spBlkRec->close();
}
AcGePoint3d ptInsert(100,100,0);
AcDbBlockReference *pBlkRef =new AcDbBlockReference() ;
pBlkRef->setBlockTableRecord(blockId1);
pBlkRef->setPosition(ptInsert);
//McGeScale3d scale(dScale);//放大比例
//pBlkRef->setScaleFactors(scale);
//pBlkRef->setRotation(dRotation*PI/180) ;//旋转弧度,单位为弧度,不是角度(弧长=半径,弧度为1)
AcDbBlockTable *pBlockTable;
es = pDatabase->getBlockTable(pBlockTable,McDb::kForRead);
if(es != Acad::eOk){
AfxMessageBox(_T("获得块表失败"));
}
AcDbBlockTableRecord * pBTableRec1;
es = pBlockTable->getAt(MCDB_MODEL_SPACE, pBTableRec1,McDb::kForWrite);
if(es != Acad::eOk){
AfxMessageBox(_T("获得块记录失败"));
}
AcDbObjectId newEntId;
es = pBTableRec1->appendAcDbEntity(newEntId,pBlkRef);
if(es != Acad::eOk){
AfxMessageBox(_T("插入实体失败"));
}
pBTableRec1->close();
pBlockTable->close();
pBlkRef->close();
return true;
}
第二种方式:(失败了)
BOOL TestDlg::testBlock2()
{
Acad::ErrorStatus es;
AcDbDatabase* pDatabase;
AcDbObjectId blockId0;
AcDbObjectId blockId1;
AcDbBlockTable *pBlockTable;
AcDbObjectId newEntId0;
AcDbObjectId newEntId1;
AcGePoint3d start0(0,0,0);
AcGePoint3d end0(10,10,0);
AcGePoint3d start1(0,0,0);
AcGePoint3d end1(-10,10,0);
AcDbLine *pLine=new AcDbLine(start0,end0);
AcDbLine *pLine1=new AcDbLine(start1,end1);
pDatabase = acdbHostApplicationServices()->workingDatabase();
es = pDatabase->getBlockTable(pBlockTable,McDb::kForWrite);
if(es != Acad::eOk){
AfxMessageBox(_T("获得块表失败"));
}
//定义一个图块,包括2条直线
AcDbObjectId id0;
AcDbBlockTableRecord * pBTableRec1;
pBTableRec1 = new AcDbBlockTableRecord();
pBTableRec1->setName(_T("testBlock2"));
pBTableRec1->appendAcDbEntity(newEntId0,pLine);
pBTableRec1->appendAcDbEntity(newEntId1,pLine1);
pBlockTable->add(id0,pBTableRec1);
pBTableRec1->close();
pBlockTable->close();
/*********以上为图块定义,以下为图块引用******************/
AcDbBlockTableRecordPointer spBlkRec( _T("testBlock2"),pDatabase, AcDb::kForRead );
if( spBlkRec.openStatus() == Acad::eOk )
{
blockId1 = spBlkRec->objectId();
spBlkRec->close();
}
AcGePoint3d ptInsert(100,100,0);
AcDbBlockReference *pBlkRef =new AcDbBlockReference() ;
pBlkRef->setBlockTableRecord(blockId1);
pBlkRef->setPosition(ptInsert);
AcGeScale3d scale(3);//放大比例
pBlkRef->setScaleFactors(scale);
//pBlkRef->setRotation(dRotation*PI/180) ;//旋转弧度,单位为弧度,不是角度(弧长=半径,弧度为1)
es = pDatabase->getBlockTable(pBlockTable,McDb::kForRead);
if(es != Acad::eOk){
AfxMessageBox(_T("获得块表失败"));
}
es = pBlockTable->getAt(MCDB_MODEL_SPACE, pBTableRec1,McDb::kForWrite);
if(es != Acad::eOk){
AfxMessageBox(_T("获得块记录失败"));
}
AcDbObjectId newEntId;
es = pBTableRec1->appendAcDbEntity(newEntId,pBlkRef);
if(es != Acad::eOk){
AfxMessageBox(_T("插入实体失败"));
}
pBTableRec1->close();
pBlockTable->close();
pBlkRef->close();
return true;
}
调用:testBlock1(); 或 testBlock2();
|
|