xhsd 发表于 2010-7-8 22:41:00

插入块问题求助?

<font face="Verdana">
<p><font face="Verdana">//====================================================================================================================<br/>//功能:从源文件 插入一个块 到 目标文件中<br/>//参数:<br/>//返回:<br/>//====================================================================================================================<br/>AcDbObjectId CXXFile::InsertBlkDef(AcDbDatabase* pDb, CString SourceDwgFile, CString SourceBlockName, CString DestinationBlockName)<br/>{ <br/>&nbsp;AcDbDatabase* pExternalDb;<br/>&nbsp;Acad::ErrorStatus es; <br/>&nbsp;<br/>&nbsp;//打开外部文件<br/>&nbsp;pExternalDb = new AcDbDatabase(Adesk::kFalse);<br/>&nbsp;es = pExternalDb-&gt;readDwgFile(SourceDwgFile);<br/>&nbsp;if (es!=Acad::eOk) <br/>&nbsp;{<br/>&nbsp;&nbsp;delete pExternalDb;<br/>&nbsp;&nbsp;//return;<br/>&nbsp;}<br/>&nbsp;<br/>&nbsp;// 获得名称为Blk的块表记录 <br/>&nbsp;&nbsp;&nbsp; AcDbBlockTable* pBlkTbl;<br/>&nbsp;&nbsp;&nbsp; if (Acad::eOk != pExternalDb-&gt;getBlockTable(pBlkTbl, AcDb::kForRead)) <br/>&nbsp;&nbsp;&nbsp; { <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acedAlert( "获得块表失败!" ); <br/>&nbsp;&nbsp;//return;<br/>&nbsp;}</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; AcDbBlockTableRecord* pBlkTblRcd; <br/>&nbsp;&nbsp;&nbsp; es = pBlkTbl-&gt;getAt(SourceBlockName, pBlkTblRcd, AcDb::kForRead); <br/>&nbsp;&nbsp;&nbsp; pBlkTbl-&gt;close(); <br/>&nbsp;&nbsp;&nbsp; if (Acad::eOk != es)<br/>&nbsp;{ <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acedAlert("获得指定的块表记录失败!"); <br/>&nbsp;&nbsp;//return;<br/>&nbsp;&nbsp;&nbsp; } <br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;// 创建块参照遍历器<br/>&nbsp;AcDbBlockReferenceIdIterator *pItr;<br/>&nbsp;if (Acad::eOk != pBlkTblRcd-&gt;newBlockReferenceIdIterator(pItr))<br/>&nbsp;{ <br/>&nbsp;&nbsp;acedAlert("创建遍历器失败!");<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pBlkTblRcd-&gt;close();<br/>&nbsp;&nbsp;// return;<br/>&nbsp;}<br/>&nbsp; <br/>&nbsp;// 找到图形中的第一个复合要求的块参照,将其添加到ObjectId数组中 <br/>&nbsp;AcDbObjectIdArray list;&nbsp; // 导出到临时图形数据库的实体数组 <br/>&nbsp;for (pItr-&gt;start(); !pItr-&gt;done(); pItr-&gt;step()) <br/>&nbsp;{<br/>&nbsp;&nbsp;AcDbObjectId blkRefId;<br/>&nbsp;&nbsp;if (Acad::eOk == pItr-&gt;getBlockReferenceId(blkRefId))<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;list.append(blkRefId);<br/>&nbsp;&nbsp;&nbsp;break;<br/>&nbsp;&nbsp;}<br/>&nbsp;}<br/>&nbsp;delete pItr;</font></p>
<p><font face="Verdana">&nbsp;pBlkTblRcd-&gt;close();<br/>&nbsp; <br/>&nbsp;&nbsp;&nbsp; if (list.isEmpty())<br/>&nbsp;{ <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acedAlert("实体数组中未包含任何实体!");<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // return; <br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; <br/>&nbsp;&nbsp;&nbsp; AcDbDatabase *pTempDb;// 临时图形数据库 <br/>&nbsp;AcGePoint3d ptBase(0, 0, 0);<br/>&nbsp;// 将list数组中包含的实体输出到一个临时图形数据库中<br/>&nbsp;&nbsp;&nbsp; if (Acad::eOk != pExternalDb-&gt;wblock(pTempDb, list, ptBase))<br/>&nbsp;&nbsp;&nbsp; { <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acedAlert("wblock操作失败!");<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //return;<br/>&nbsp;&nbsp;&nbsp; } </font></p>
<p><font face="Verdana">&nbsp;////////////////////////////////////////////////////////////////////////<br/>&nbsp;&nbsp;// 将临时数据库的内容插入到当前图形数据库<br/>&nbsp;if (Acad::eOk != pDb-&gt;insert(AcGeMatrix3d::kIdentity, pTempDb))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acedAlert("insert操作失败!");<br/>&nbsp;delete pTempDb;<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;// 如果不需要保留块参照,将模型空间中的最后一个对象删除即可 <br/>&nbsp;ads_name lastEnt; <br/>&nbsp;if (acdbEntLast(lastEnt) != RTNORM)<br/>&nbsp;{<br/>&nbsp;&nbsp;acedAlert("获得模型空间最后一个实体失败!");<br/>&nbsp;&nbsp;//return;<br/>&nbsp;}<br/>&nbsp;<br/>&nbsp;AcDbObjectId entId;<br/>&nbsp;es = acdbGetObjectId(entId, lastEnt);<br/>&nbsp;AcDbEntity *pEnt;<br/>&nbsp;es = acdbOpenAcDbEntity(pEnt, entId, AcDb::kForWrite);</font></p>
<p><font face="Verdana">&nbsp;//<br/>&nbsp;pEnt-&gt;erase();<br/>&nbsp;pEnt-&gt;close();</font></p>
<p><font face="Verdana">&nbsp;return entId;/**/<br/>}</font></p>
<p><font face="Verdana"></font>&nbsp;</p>
<p><font face="Verdana">这段代码能实现从一个文件中 找到一个块 并且插入到 图形数据库pDb中, 但是不能实现在指定位置插入,请问该如何解决</p>
<p><br/></p></font></font>

luowy 发表于 2010-7-9 09:28:00

不能在指定位置呢,估计是坐标系的问题,你转到ucs下看看

xhsd 发表于 2010-7-10 22:06:00

<p>经张帆老师指点,问题解决,是<font face="Verdana">insert(AcGeMatrix3d::kIdentity, pTempDb)) 的问题,设置矩阵变换</font></p>
<p><font face="Verdana">AcGeMatrix3d xform;&nbsp; <br/>&nbsp;AcGeVector3d vec(ptDest.x - ptBase.x, ptDest.y - ptBase.y,&nbsp; <br/>&nbsp; ptDest.z - ptBase.z); <br/>&nbsp;xform.setToTranslation(vec);</font></p>
<p>&nbsp;</p>
<p>if (Acad::eOk != pDb-&gt;insert(xform, pTempDb))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; acedAlert("insert操作失败!");<br/>&nbsp;delete pTempDb;<br/></p>

hourui800720 发表于 2011-7-14 00:21:35

感谢,感谢啊
页: [1]
查看完整版本: 插入块问题求助?