如何将用wblock命令导出的块添加到图纸中?
我在一个dwg图纸中做好了所有的块,然后我用wblock命令将这些块保存成dwg文件,那么我在需要的时候就可以直接将这些块添加到我的其他的dwg文件中,可是在程序中如何实现插入以dwg格式存放在磁盘某个位置的块呢? <P>问题我已经通过广大无私人事的帮助终于完满的解决了,再次公布出来给大家共同分享,同时感谢帮助我的朋友们!</P><P>Acad::ErrorStatus insertBolck(const char *blockName)<BR>{<BR> //将块从文件读出,添加到当前数据库中<BR> AcDbDatabase *pNewDb =new AcDbDatabase(Adesk::kFalse);</P>
<P> Acad::ErrorStatus es = pNewDb->readDwgFile("C:\\Documents and Settings\\pengsl\\My Documents\\ARX\\Step01\\TEL_CD.dwg" , _SH_DENYNO,false);<BR> if (es!=Acad::eOk)<BR> {<BR> acutPrintf("\nThe file cannot be opend");<BR> return es;<BR> }</P>
<P> AcDbDatabase *pDb =acdbHostApplicationServices()->workingDatabase ();<BR>// pDb->insert(AcGeMatrix3d::kIdentity, pNewDb );这样添加的块是打散的 ,而且也没有自定义属性。<BR> <BR> AcDbObjectId blockId;<BR> <BR> if((es=pDb->insert(blockId, "TEL_CD",pNewDb, true))==Acad::eOk)<BR> {<BR> acutPrintf("\ninsert ok\n");<BR> }<BR> else<BR> {<BR> acutPrintf("\nInsert failed");<BR> delete pNewDb;<BR> return es;<BR> }</P>
<P> //将块从当前数据库中添加到图纸中并且显示出来,此时块还不具备自定义属性<BR> AcGePoint3d ptOrg(10,10,0);<BR> AcGeScale3d XrefScale(10,10,1);</P>
<P> AcDbObjectId blockTelCdId;<BR> AcDbBlockTable *pBlockTable;<BR> pDb->getSymbolTable(pBlockTable, AcDb::kForRead);<BR> pBlockTable->getAt("TEL_CD", blockTelCdId);</P>
<P> AcDbBlockReference *pBlkRef = new AcDbBlockReference;<BR> <BR> pBlkRef->setBlockTableRecord(blockTelCdId);<BR> pBlkRef->setPosition(ptOrg);<BR> pBlkRef->setRotation(0.0);//设定转角<BR> pBlkRef->setScaleFactors(XrefScale);//设定放大比例</P>
<P> AcDbBlockTableRecord *pBlockTableRecord;<BR> <BR> pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);<BR> pBlockTable->close();<BR> <BR> AcDbObjectId newEntId;<BR> <BR> if((es = pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef)) != Acad::eOk)<BR> {<BR> acutPrintf("\n something is wrong");<BR> }<BR> <BR> //处理新加块的自定义属性值<BR> AcDbBlockTableRecord *pBlockDef; <BR> acdbOpenObject(pBlockDef, blockTelCdId, AcDb::kForWrite); <BR> AcDbBlockTableRecordIterator *pIterator; <BR> pBlockDef->newIterator(pIterator); <BR> AcGePoint3d basePoint; <BR> AcDbEntity *pEnt; <BR> AcDbAttributeDefinition *pAttdef; <BR> <BR> for (pIterator->start(); !pIterator->done(); pIterator->step())//将source.dwg中所有的Attibute进行遍历 <BR> { <BR> pIterator->getEntity(pEnt, AcDb::kForRead); <BR> pAttdef = AcDbAttributeDefinition::cast(pEnt); <BR> <BR> if (pAttdef != NULL && !pAttdef->isConstant())<BR> { <BR> AcDbAttribute *pAtt = new AcDbAttribute(); <BR> pAtt->setPropertiesFrom(pAttdef); <BR> pAtt->setInvisible(pAttdef->isInvisible()); <BR> basePoint = pAttdef->position(); <BR> basePoint += pBlkRef->position().asVector(); <BR> pAtt->setPosition(basePoint); <BR> pAtt->setHeight(pAttdef->height()); <BR> pAtt->setRotation(pAttdef->rotation()); <BR> pAtt->setTag("Tag"); <BR> pAtt->setFieldLength(25); <BR> char *pStr = pAttdef->tag(); <BR> pAtt->setTag(pStr); <BR> acutDelString(pStr); <BR> pAtt->setFieldLength(pAttdef->fieldLength()); <BR> pAtt->setTextString("J001/zg01/003"); <BR> AcDbObjectId attId; <BR> pBlkRef->appendAttribute(attId, pAtt); <BR> pAtt->close(); <BR> } <BR> pEnt->close(); // use pEnt... pAttdef might be NULL <BR> } <BR> delete pIterator;<BR> <BR> pBlockDef->close();<BR> pBlockTableRecord->close();<BR> pBlkRef->close();</P>
<P> delete pNewDb;<BR> return Acad::eOk;<BR>}</P> <P>这个函数封装的太差劲了</P> <P>^_^,我也承认封装的很差,这个函数只是用来完成功能的,没有考虑其它很多</P> jingelou发表于2006-6-14 0:34:00static/image/common/back.gif
这个函数封装的太差劲了
<BR>嫩为大家提供一个封装的较好的函数吗?
页:
[1]