pengyh 发表于 2006-5-31 22:17:00

如何将用wblock命令导出的块添加到图纸中?

我在一个dwg图纸中做好了所有的块,然后我用wblock命令将这些块保存成dwg文件,那么我在需要的时候就可以直接将这些块添加到我的其他的dwg文件中,可是在程序中如何实现插入以dwg格式存放在磁盘某个位置的块呢?

pengyh 发表于 2006-6-2 17:35:00

<P>问题我已经通过广大无私人事的帮助终于完满的解决了,再次公布出来给大家共同分享,同时感谢帮助我的朋友们!</P>
<P>Acad::ErrorStatus insertBolck(const char *blockName)<BR>{<BR>&nbsp;//将块从文件读出,添加到当前数据库中<BR>&nbsp;AcDbDatabase *pNewDb =new AcDbDatabase(Adesk::kFalse);</P>
<P>&nbsp;Acad::ErrorStatus es = pNewDb-&gt;readDwgFile("C:\\Documents and Settings\\pengsl\\My Documents\\ARX\\Step01\\TEL_CD.dwg" , _SH_DENYNO,false);<BR>&nbsp;if (es!=Acad::eOk)<BR>&nbsp;{<BR>&nbsp;&nbsp;acutPrintf("\nThe file&nbsp; cannot be opend");<BR>&nbsp;&nbsp;return es;<BR>&nbsp;}</P>
<P>&nbsp;AcDbDatabase *pDb =acdbHostApplicationServices()-&gt;workingDatabase ();<BR>//&nbsp;pDb-&gt;insert(AcGeMatrix3d::kIdentity, pNewDb );这样添加的块是打散的 ,而且也没有自定义属性。<BR>&nbsp;<BR>&nbsp;AcDbObjectId blockId;<BR>&nbsp;<BR>&nbsp;if((es=pDb-&gt;insert(blockId, "TEL_CD",pNewDb, true))==Acad::eOk)<BR>&nbsp;{<BR>&nbsp;&nbsp;acutPrintf("\ninsert ok\n");<BR>&nbsp;}<BR>&nbsp;else<BR>&nbsp;{<BR>&nbsp;&nbsp;acutPrintf("\nInsert failed");<BR>&nbsp;&nbsp;delete pNewDb;<BR>&nbsp;&nbsp;return es;<BR>&nbsp;}</P>
<P>&nbsp;//将块从当前数据库中添加到图纸中并且显示出来,此时块还不具备自定义属性<BR>&nbsp;AcGePoint3d ptOrg(10,10,0);<BR>&nbsp;&nbsp;&nbsp; AcGeScale3d XrefScale(10,10,1);</P>
<P>&nbsp;AcDbObjectId blockTelCdId;<BR>&nbsp;AcDbBlockTable *pBlockTable;<BR>&nbsp;pDb-&gt;getSymbolTable(pBlockTable, AcDb::kForRead);<BR>&nbsp;pBlockTable-&gt;getAt("TEL_CD", blockTelCdId);</P>
<P>&nbsp;AcDbBlockReference *pBlkRef = new AcDbBlockReference;<BR>&nbsp;<BR>&nbsp;pBlkRef-&gt;setBlockTableRecord(blockTelCdId);<BR>&nbsp;pBlkRef-&gt;setPosition(ptOrg);<BR>&nbsp;pBlkRef-&gt;setRotation(0.0);//设定转角<BR>&nbsp;pBlkRef-&gt;setScaleFactors(XrefScale);//设定放大比例</P>
<P>&nbsp;AcDbBlockTableRecord *pBlockTableRecord;<BR>&nbsp;<BR>&nbsp;pBlockTable-&gt;getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);<BR>&nbsp;pBlockTable-&gt;close();<BR>&nbsp;<BR>&nbsp;AcDbObjectId newEntId;<BR>&nbsp;<BR>&nbsp;if((es = pBlockTableRecord-&gt;appendAcDbEntity(newEntId, pBlkRef)) != Acad::eOk)<BR>&nbsp;{<BR>&nbsp;&nbsp;acutPrintf("\n something is wrong");<BR>&nbsp;}<BR>&nbsp;<BR>&nbsp;//处理新加块的自定义属性值<BR>&nbsp;AcDbBlockTableRecord *pBlockDef; <BR>&nbsp;acdbOpenObject(pBlockDef, blockTelCdId, AcDb::kForWrite); <BR>&nbsp;AcDbBlockTableRecordIterator *pIterator; <BR>&nbsp;pBlockDef-&gt;newIterator(pIterator); <BR>&nbsp;AcGePoint3d basePoint; <BR>&nbsp;AcDbEntity *pEnt; <BR>&nbsp;AcDbAttributeDefinition *pAttdef; <BR>&nbsp;<BR>&nbsp;for (pIterator-&gt;start(); !pIterator-&gt;done(); pIterator-&gt;step())//将source.dwg中所有的Attibute进行遍历 <BR>&nbsp;{ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pIterator-&gt;getEntity(pEnt, AcDb::kForRead); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAttdef = AcDbAttributeDefinition::cast(pEnt); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;if (pAttdef != NULL &amp;&amp; !pAttdef-&gt;isConstant())<BR>&nbsp;&nbsp;{ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcDbAttribute *pAtt = new AcDbAttribute(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setPropertiesFrom(pAttdef); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setInvisible(pAttdef-&gt;isInvisible()); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; basePoint = pAttdef-&gt;position(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; basePoint += pBlkRef-&gt;position().asVector(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setPosition(basePoint); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setHeight(pAttdef-&gt;height()); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setRotation(pAttdef-&gt;rotation()); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setTag("Tag"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setFieldLength(25); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *pStr = pAttdef-&gt;tag(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setTag(pStr); <BR>&nbsp;&nbsp;&nbsp;acutDelString(pStr); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setFieldLength(pAttdef-&gt;fieldLength()); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;setTextString("J001/zg01/003"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcDbObjectId attId; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pBlkRef-&gt;appendAttribute(attId, pAtt); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pAtt-&gt;close(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pEnt-&gt;close(); // use pEnt... pAttdef might be NULL <BR>&nbsp;&nbsp;&nbsp; } <BR>&nbsp;delete pIterator;<BR>&nbsp;<BR>&nbsp;pBlockDef-&gt;close();<BR>&nbsp;pBlockTableRecord-&gt;close();<BR>&nbsp;pBlkRef-&gt;close();</P>
<P>&nbsp;delete pNewDb;<BR>&nbsp;return Acad::eOk;<BR>}</P>

jingelou 发表于 2006-6-14 00:34:00

<P>这个函数封装的太差劲了</P>

pengyh 发表于 2006-6-14 10:59:00

<P>^_^,我也承认封装的很差,这个函数只是用来完成功能的,没有考虑其它很多</P>

松竹 发表于 2006-6-14 13:38:00

jingelou发表于2006-6-14 0:34:00static/image/common/back.gif
这个函数封装的太差劲了

<BR>嫩为大家提供一个封装的较好的函数吗?
页: [1]
查看完整版本: 如何将用wblock命令导出的块添加到图纸中?