朋友们帮忙看看错在哪里
下面是两段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();
先不说其他的问题,就说这个吧,
es = pDatabase->getBlockTable(pBlockTable,McDb::kForWrite);
if(es != Acad::eOk){
AfxMessageBox(_T("获得块表失败"));
}
es = pDatabase->getBlockTable(pBlockTable,McDb::kForRead);
if(es != Acad::eOk){
AfxMessageBox(_T("获得块表失败"));
}
这里,es肯定是eWasOpenForWrite luowy 发表于 2014-11-20 10:41 static/image/common/back.gif
先不说其他的问题,就说这个吧,
es = pDatabase->getBlockTable(pBlockTable,McDb::kForWrite);
...
这个位置 es == Acad::eOk 下图是我跟踪的结果
pBTableRec1->appendAcDbEntity(newEntId0,pLine);
pBTableRec1->appendAcDbEntity(newEntId1,pLine1);
pLine和pLine1没有关闭 yanguiming 发表于 2014-11-20 13:13 static/image/common/back.gif
pBTableRec1->appendAcDbEntity(newEntId0,pLine);
pBTableRec1->appendAcDbEntity(newEntId1,pLi ...
我尝试着把这两个对象关闭,还是没成功。 顶起来,别沉下去 那错误提示呢? netsky2580 发表于 2014-11-20 15:39 static/image/common/back.gif
顶起来,别沉下去
没有错误,程序可以从头执行到尾,但就是图块显示不出来。 本帖最后由 三水合一 于 2014-11-21 08:58 编辑
//定义一个图块,包括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();
//////////////分析//////////////////////////////////////
1、先把新的块表记录加到块表中,关闭;
2、在打开刚才新建的块表记录,吧你创建的实体(也就是你的那些直线)加到新块表中,关闭实体和块表记录。
3、再执行下面的操作试试。
我试了下
替换你的上面的这段代码:
//定义一个图块,包括2条直线
AcDbObjectId id0;
AcDbBlockTableRecord * pBTableRec1;
pBTableRec1 = new AcDbBlockTableRecord();
pBTableRec1->setName(_T("testBlock2"));
pBlockTable->add(id0,pBTableRec1);
pBTableRec1->close();
pBlockTable->close();
es = pDatabase->getBlockTable(pBlockTable,AcDb::kForRead);
if (es != Acad::eOk)
{
delete pLine;
delete pLine1;
return;
}
es = pBlockTable->getAt(_T("testBlock2"), pBTableRec1,AcDb::kForWrite);
if (es != Acad::eOk)
{
delete pLine;
delete pLine1;
return;
}
pBTableRec1->appendAcDbEntity(newEntId0,pLine);
pBTableRec1->appendAcDbEntity(newEntId1,pLine1);
pBTableRec1->close();
pBlockTable->close();
再执行下面的操作,OK,ACAD2012测试,没问题,你试试
下面附一张效果图:
页:
[1]
2