[讨论]按指定点插入外图图块
<p>如题,如何在当前“模型空间” 或者一个“块”中,按照指定的点插入别图图块,我用了以下的方法,请大家看看是否有什么不妥,是不是还有别的方法。</p><p> </p>
<p><font face="Verdana">插入别图图块到当前数据库:<br/>1、创建一个新数据库:此时必须创建一个空的数据库。AcDbDatabase ExDb(Adesk::kFalse)或者以指针的型式创建一个<br/>AcDbDatabase* pExDb=new AcDbDatabase(Adesk::kFalse)<br/>2、使用 readDwgFile函数读取源文件:es=pExDb->readDwgFile(strSourceDwgFile);<br/>3、取得数据库名为"BlockName"的块表与块表记录:<br/>AcDbBlockTable *pBlockTable;<br/>AcDbBlockTableRecord * pBlockTableRecord;<br/>es=pExDb->getBlockTable(pBlockTable,AcDb::kForRead);<br/>es=pBlockTable->getAt(BlockName,pBlockTableRecord,AcDb::kForRead);<br/>pBlockTable->close();<br/>4、创建一个保存实体ID的数组:AcDbObjectIdArray list;// 导出到临时图形数据库的实体数组<br/>5、获得块表记录中的实体: 此时有两种方法<br/> ①:创建一个块表记录遍历器(块表中所有的实体):<br/> AcDbBlockTableRecordIterator *pItr;<br/> es=pBlkTblRcd->newIterator(pItr));<br/> for (pItr->start(); !pItr->done(); pItr->step())<br/> {<br/> AcDbObjectId blkRefId;<br/> if (Acad::eOk == pItr->getEntityId(blkRefId))<br/> {<br/> list.append(blkRefId);<br/> /*break;*/<br/> }<br/> }<br/> delete pItr;<br/> pBlkTblRcd->close();<br/> ②:直接获取该块中的块参照实体id(块参照中所有实体,此时块名"BlockName"不能是模型空间):<br/> es=pBlkTblRcd->getBlockReferenceIds(list, false,true);<br/> pBlkTblRcd->close();<br/>6、创建临时数据库,将list数组中包含的实体输出到一个临时图形数据库中:<br/> AcDbDatabase *pTempDb;<br/> es=pExDb->wblock( pTempDb, list, AcGePoint3d::kOrigin ));<br/>#附加步骤:重新设置块的原点,转角,比例:<br/>⑴:设置点坐标:<br/> AcGePoint3d pt(500,200,0);<br/>⑵:得到临时数据库的块表与块表记录:<br/> pTempDb->getSymbolTable(pBlockTable,AcDb::kForRead);<br/> pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForRead);<br/> pBlockTable->close();<br/>⑶创建块表记录遍历器:<br/> AcDbBlockTableRecordIterator* prcdit;<br/> pBlockTable->newIterator(prcdit);<br/> for (prcdit->start(); !prcdit->done(); prcdit->step())<br/> {<br/> AcDbEntity* penty;<br/> AcDbBlockReference *pblkref;<br/> prcdit->getEntity(penty,AcDb::kForWrite);<br/> if (penty->isKindOf(AcDbBlockReference::desc()))<br/> {<br/> pblkref=(AcDbBlockReference*)penty;<br/> pblkref->setPosition(pt);<br/> pblkref->setRotation(2*atan(1.0));<br/> penty->close();<br/> break;<br/> }<br/> }<br/> pBlockTable->close();<br/> delete prcdit; <br/>7、将临时数据库的内容插入到当前图形数据库:<br/> es=acdbHostApplicationServices()->workingDatabase()<br/> ->insert(AcGeMatrix3d::kIdentity, pTempDb));<br/> delete pTempDb;<br/> delete pExDb;</font></p> 无需临时数据库
Acad::ErrorStatus err;
const ACHAR *PathName = _T("E:\\12345.dwg");
const ACHAR *BlockName = _T("aaa");
AcDbDatabase * pExDb = new AcDbDatabase(Adesk::kFalse);
AcDbDatabase * pCurDb = acdbHostApplicationServices()->workingDatabase();
AcDbObjectId BlockId;
pExDb ->readDwgFile(PathName);
err = pCurDb ->insert(BlockId, BlockName, NULL,pExDb);
delete pExDb;
if (err != Acad::eOk) //克隆失败,退出
return;
AcDbBlockTable * pBlockTable;
err = pCurDb -> getSymbolTable(pBlockTable,AcDb::kForRead);
pBlockTable ->getAt(BlockName, BlockId);
AcDbBlockReference *pBlockRef = new AcDbBlockReference;
pBlockRef ->setBlockTableRecord(BlockId);
pBlockRef ->setPosition(AcGePoint3d(500, 20, 0));
pBlockRef ->setRotation(2*atan(1.0));
AcDbBlockTableRecord * pBlock;
err = pBlockTable -> getAt(ACDB_MODEL_SPACE,pBlock,AcDb::kForWrite);
pBlockTable -> close();
if (err != Acad::eOk)
{
delete pBlockRef;
return;
}
err = pBlock -> appendAcDbEntity(pBlockRef);
pBlockRef ->close();
pBlock -> close();
<p>好像你的这个方法把原图形中的所有块都会复制到目标图形中去</p> 2楼的兄弟没有理解LZ的意思,你的方式是把别图的整个数据库作为当前图的一个快参照进行插入了。 研究学习感谢楼主
页:
[1]