NEtiger 发表于 2008-10-23 13:58:00

共享一些代码(转自其他的论坛)

<h2>共享一些常用的代码</h2><p>(此代码的出自是c3p论坛的,单纯为了知识,让知识更集中起来,如果有不妥,麻烦告诉我呀)</p><p></p><p></p><div class="t_msgfont" id="postmessage_127855">//获得所有的实体<br/>Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray&amp; IdArr,<br/>const AcArray&amp; layerNameArr)<br/>{<br/>Acad::ErrorStatus es=Acad::eOk;<br/>ASSERT(pDb);<br/>if(pDb==NULL)<br/>return Acad::eInvalidInput;<br/>AcDbBlockTable *pBlkTable=NULL;<br/>if((es=pDb-&gt;getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表<br/>{<br/>acedAlert("打开块表失败");<br/>return es;<br/>}<br/>AcDbBlockTableRecord *pBlkTableRecord=NULL;<br/>if((es=pBlkTable-&gt;getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录<br/>{<br/>acedAlert("打开块表记录失败");<br/>pBlkTable-&gt;close();<br/>return es;<br/>}<br/>pBlkTable-&gt;close();//关闭块表<br/><br/>AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器<br/>if((es=pBlkTableRecord-&gt;newIterator(pIterator))!=Acad::eOk)<br/>{<br/>pBlkTableRecord-&gt;close();<br/>return es;<br/>}<br/><br/>CAcModuleResourceOverride resoverride;<br/>CProgressDlg progress;<br/>progress.Create();<br/>progress.SetPos(0);<br/>progress.SetWindowText("正在检测图形中所有实体...");<br/><br/>for(pIterator-&gt;start();!pIterator-&gt;done();pIterator-&gt;step())//遍历整个数据库<br/>{<br/>AcDbEntity *entity=NULL;<br/>es=pIterator-&gt;getEntity(entity,AcDb::kForRead); //打开实体<br/><br/>if(es==Acad::eLockViolation)<br/>{<br/>acedAlert("内存锁定");<br/>}<br/>else if(es==Acad::eWasOpenForWrite)<br/>{<br/>acedAlert("实体以写方式打开");<br/>}<br/>else if(es==Acad::eWasOpenForRead)<br/>{<br/>acedAlert("实体以读方式打开");<br/>}<br/>else<br/>{ <br/>if(layerNameArr.contains(entity-&gt;layer()))<br/>IdArr.append(entity-&gt;objectId());<br/>entity-&gt;close();<br/>}<br/>progress.StepIt();<br/>}<br/>delete pIterator;pIterator=NULL;<br/>pBlkTableRecord-&gt;close();<br/>acutPrintf("eend");<br/>return es;<br/>}<br/><br/>//得到指定层上指定颜色的所有实体<br/>Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray&amp; IdArr,<br/>const AcArray&amp; layerNameArr,Adesk::UInt16 colorIndex)<br/>{<br/>Acad::ErrorStatus es=Acad::eOk;<br/>ASSERT(pDb);<br/>if(pDb==NULL)<br/>return Acad::eInvalidInput;<br/>AcDbBlockTable *pBlkTable=NULL;<br/>if((es=pDb-&gt;getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表<br/>{<br/>acedAlert("打开块表失败");<br/>return es;<br/>}<br/>AcDbBlockTableRecord *pBlkTableRecord=NULL;<br/>if((es=pBlkTable-&gt;getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录<br/>{<br/>acedAlert("打开块表记录失败");<br/>pBlkTable-&gt;close();<br/>return es;<br/>}<br/>pBlkTable-&gt;close();//关闭块表<br/><br/>AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器<br/>if((es=pBlkTableRecord-&gt;newIterator(pIterator))!=Acad::eOk)<br/>{<br/>pBlkTableRecord-&gt;close();<br/>return es;<br/>}<br/><br/>CAcModuleResourceOverride resoverride;<br/>CProgressDlg progress;<br/>progress.Create();<br/>progress.SetPos(0);<br/>progress.SetWindowText("正在检测图形中所有实体...");<br/><br/>for(pIterator-&gt;start();!pIterator-&gt;done();pIterator-&gt;step())//遍历整个数据库<br/>{<br/>AcDbEntity *entity=NULL;<br/>if((es=pIterator-&gt;getEntity(entity,AcDb::kForRead))!=Acad::eOk)//打开实体<br/>{<br/>delete pIterator;pIterator=NULL;<br/>pBlkTableRecord-&gt;close();<br/>return es;<br/>}<br/>if(layerNameArr.contains(entity-&gt;layer())&amp;&amp;entity-&gt;colorIndex()==colorIndex)<br/>IdArr.append(entity-&gt;objectId());<br/>entity-&gt;close();<br/>progress.StepIt();<br/>}<br/>delete pIterator;pIterator=NULL;<br/>pBlkTableRecord-&gt;close();<br/>return es;<br/>}<br/><br/>//得到图形中所有的层<br/>Acad::ErrorStatus CDrawFunction::getAllLayerName(AcDbDatabase *pDb,CStringArray&amp; layerArray)<br/>{<br/>Acad::ErrorStatus es=Acad::eOk;<br/>if(pDb==NULL)<br/>return Acad::eInvalidInput;<br/>layerArray.RemoveAll();<br/>AcDbLayerTable *pLayerTable=NULL;<br/>if((es=pDb-&gt;getSymbolTable(pLayerTable,AcDb::kForRead))!=Acad::eOk)<br/>{<br/>pLayerTable-&gt;close();<br/>return es;<br/>}<br/>//创建一个层表迭代器<br/>AcDbLayerTableIterator *pLayerTableIterator;<br/>pLayerTable-&gt;newIterator(pLayerTableIterator);<br/>pLayerTable-&gt;close();<br/><br/>char *pLayerName=NULL;<br/>CString name;<br/>for(int i=0;!pLayerTableIterator-&gt;done();pLayerTableIterator-&gt;step(),i++)<br/>{<br/>AcDbLayerTableRecord *pLayerTableRecord=NULL;<br/>pLayerTableIterator-&gt;getRecord(pLayerTableRecord,AcDb::kForRead);<br/>pLayerTableRecord-&gt;getName(pLayerName);<br/>name.Format("%s",pLayerName);<br/>pLayerTableRecord-&gt;close();<br/>layerArray.Add(name);<br/><br/>}<br/><br/>if(pLayerName) acutDelString(pLayerName);<br/>delete pLayerTableIterator;pLayerTableIterator=NULL;<br/>return es;<br/>}<br/><br/><br/>//*************根据线形名得到线型ID********************//<br/>//******************************************************//<br/>BOOL CDrawFunction::getLinetypeIdFromString(const char* str, AcDbObjectId&amp; id)<br/>{<br/>//----查找安装目录----//<br/>CString AcadInstallPath;<br/>FindAcadInstallPath(AcadInstallPath);<br/>CString File=AcadInstallPath+"\\linetype\\user.lin";<br/>//-----查找完毕--------// <br/>ASSERT(str!=NULL);<br/>AcDbLinetypeTable *pLinetypeTable;<br/>acdbHostApplicationServices()-&gt;workingDatabase()-&gt;getSymbolTable(pLinetypeTable, AcDb::kForRead);<br/>Acad::ErrorStatus mess;<br/>mess=pLinetypeTable-&gt;getAt(str,id);<br/>if(mess==Acad::eKeyNotFound||mess==Acad::ePermanentlyErased)<br/>{<br/>pLinetypeTable-&gt;close();<br/>Acad::ErrorStatus error;<br/><br/>error=acdbLoadLineTypeFile(str,File.GetBuffer(0),acdbHostApplicationServices()-&gt;workingDatabase());<br/>if(error==Acad::eNullObjectPointer)<br/>{<br/>AcDbLinetypeTable *pLinetypeTable;<br/>acdbHostApplicationServices()-&gt;workingDatabase()-&gt;getSymbolTable(pLinetypeTable, AcDb::kForRead);<br/>pLinetypeTable-&gt;getAt("CONTINUOUS", id);<br/>pLinetypeTable-&gt;close();<br/>return FALSE;<br/>}<br/>else if(error==Acad::eFileSystemErr)<br/>{<br/>AfxMessageBox("the specified file cannot be opened");<br/>return FALSE;<br/>}<br/>else if(error==Acad::eUndefinedLineType)<br/>{<br/>AcDbLinetypeTable *pLinetypeTable;<br/>acdbHostApplicationServices()-&gt;workingDatabase()-&gt;getSymbolTable(pLinetypeTable, AcDb::kForRead);<br/>AfxMessageBox("the linetype name specified by ltname is not found in the file");<br/>pLinetypeTable-&gt;getAt("CONTINUOUS", id);<br/>pLinetypeTable-&gt;close();<br/>return TRUE;<br/>}<br/>AcDbLinetypeTable *pLinetypeTable;<br/>acdbHostApplicationServices()-&gt;workingDatabase()-&gt;getSymbolTable(pLinetypeTable, AcDb::kForRead);<br/>pLinetypeTable-&gt;getAt(str,id);<br/>pLinetypeTable-&gt;close();<br/>return TRUE;<br/>}<br/>pLinetypeTable-&gt;close();<br/>return TRUE;<br/>}<br/><br/><br/><br/>void CDrawFunction::FindAcadInstallPath(CString &amp;AcadInstallPath)<br/>{<br/>//查找样式目录安装路径<br/>TCHAR AcadPath;<br/>HKEY hKey;<br/>if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE\\线路处开发组\\RDS2002\\1.00"),0,KEY_QUERY_VALUE,&amp;hKey)!=ERROR_SUCCESS)<br/>{<br/>::AfxMessageBox("注册路径不对");<br/>return ;<br/>}<br/>DWORD dwDataType=REG_SZ;<br/>DWORD dwLength=255;<br/>LONG lRet=RegQueryValueEx(hKey,TEXT("path"),NULL,NULL,(LPBYTE)AcadPath,&amp;dwLength);<br/>RegCloseKey(hKey);<br/>if(lRet!=ERROR_SUCCESS)<br/>{<br/>acutPrintf("Read failed\n");<br/>return ;<br/>}<br/>//-----查找完毕--------//<br/>AcadInstallPath.Format("%s",AcadPath);<br/>}<br/><br/>AcDbObjectId CDrawFunction::createTextStyle(CString fontName,CString bigFontName,CString textStyleName)<br/>{<br/>AcGiTextStyle *TextStyle=new AcGiTextStyle<br/>(fontName,<br/>bigFontName,<br/>0,<br/>0.67,<br/>0,<br/>0,<br/>Adesk::kFalse,<br/>Adesk::kFalse,<br/>Adesk::kFalse,<br/>Adesk::kFalse,<br/>Adesk::kFalse,<br/>textStyleName); //字体名 <br/>AcDbObjectId textStyleId;<br/>toAcDbTextStyle(*TextStyle,textStyleId);<br/>return textStyleId;<br/>}<br/><br/><br/><br/>//*******************wuweifan2002.12.12*************************//<br/>//********************插入多行文本***************************//<br/>//************************************************************//<br/>AcDbObjectId CDrawFunction::createMutiText(AcGePoint3d BasePoint,AcDb::TextHorzMode hMode,AcDb::TextVertMode vMode,CString Text,double texthight,double widthfactor,double angle,int color,CString smallFontName,CString bigFontName,CString layerName)<br/>{<br/>ASSERT(Text!=NULL);<br/>AcDbMText *pMText=new AcDbMText();<br/>if(pMText==NULL)<br/>throw Acad::eOutOfMemory;<br/>AcDbObjectId TextStyleId;<br/>TextStyleId=createTextStyle(smallFontName,bigFontName,"xianlu");<br/>pMText-&gt;setTextStyle(TextStyleId);<br/>pMText-&gt;setContents(Text.GetBuffer(Text.GetLength()));<br/>pMText-&gt;setTextHeight(texthight);<br/>pMText-&gt;setRotation(angle);<br/>pMText-&gt;setLineSpacingFactor(0.8);<br/>pMText-&gt;setColorIndex(color);<br/>if(layerName!="")<br/>pMText-&gt;setLayer(layerName.GetBuffer(0));<br/>AcDbObjectId MTextId;<br/>addToModelSpace(MTextId, pMText);<br/>pMText-&gt;close();<br/>return MTextId;<br/>}<br/><div class="notice" style="WIDTH: 500px;">本帖隐藏的内容需要回复才可以浏览</div><br/>//**********************扩展记录操作(2002.12.12)***************//<br/>//****************打开扩展记录为写******************************//<br/>//**************************************************************//<br/>AcDbDictionary* CDrawFunction:<img alt="" src="http://www.cadcaecam.com/forum/images/smilies/default/shocked.gif" border="0" smilieid="6"/>penDictionaryForWrite(LPCTSTR dictName, <br/>bool createIfNotFound,AcDbDictionary* parentDict)<br/>{<br/>ASSERT(dictName != NULL);<br/>ASSERT(parentDict != NULL);<br/>ASSERT(parentDict-&gt;isWriteEnabled()); <br/>AcDbDictionary* dict = NULL;<br/>AcDbObject* obj;<br/>Acad::ErrorStatus es;<br/>es = parentDict-&gt;getAt(dictName, obj, AcDb::kForWrite);<br/>if (es == Acad::eOk) <br/>{<br/>dict = AcDbDictionary::cast(obj);<br/>}<br/>else if (es == Acad::eKeyNotFound) <br/>{<br/>if (createIfNotFound) <br/>{<br/>dict = new AcDbDictionary;<br/>AcDbObjectId dictId;<br/>es = parentDict-&gt;setAt(dictName, dict, dictId);<br/>if (es != Acad::eOk)<br/>{<br/>delete dict;dict = NULL;<br/>}<br/>}<br/>}<br/>return dict;<br/>} <br/><br/></div>

NEtiger 发表于 2008-10-24 19:12:00

虽然很简单,但是也需要大家捧捧呀。。太打击新人信心了。。。有时候你不经意的一句话,一个微小的举动,或许是对别人来说是很大的鼓舞和鼓励,尤其是对于我们这些新人。。。比如说:你很平常的一个字,好,不错,再接再厉。。那样才至于不让我们有:班门弄斧的感觉。。。

fpiw 发表于 2008-10-24 23:47:00

支持一下!

gg850416 发表于 2008-11-5 16:15:00

<p>不错,对新手很有帮助</p>

xinxirong 发表于 2008-11-10 08:35:00

好,继续

wlxwyh 发表于 2008-11-13 11:57:00

<p>请教高手一个问题呀!我想用OBJECTARX2008在VS2005平台上把我原来(ADS程序)移植过来(很多年前做的一些小功能),但是现在编译时总是提示这样的问题:</p><p>error C2664: 'Acad::ErrorStatus AcDbSymbolTableRecord::getName(ACHAR *&amp;) const' : cannot convert parameter 1 from 'char *' to 'ACHAR *&amp;'</p><p>原代码为:</p><p>if ( acdbOpenObject ((AcDbObject *&amp;)pCurEntBlock, blockId, AcDb::kForRead) == Acad::eOk ) <br/>&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pCurEntBlock-&gt;getName(blockName);。。。。</p><p>而且后面还有很多这样的问题,请高手指点一下,鲜花献上!</p>

xy0xy2 发表于 2008-11-13 13:12:00

<p>if ( acdbOpenObject ((AcDbObject *&amp;)pCurEntBlock, blockId, AcDb::kForRead) == Acad::eOk ) <br/>&nbsp;&nbsp; {</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //TCHAR* blockName;blockName 类型的问题。在2005下用的是unicode<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pCurEntBlock-&gt;getName(blockName);</p>

xywl168 发表于 2008-11-23 14:25:00

<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LAYOUT-GRID-MODE: char; mso-pagination: widow-orphan; mso-layout-grid-align: none;">/得到<span lang="EN-US">ARX当前位置<p></p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LAYOUT-GRID-MODE: char; mso-pagination: widow-orphan; mso-layout-grid-align: none;">g_strCurPath = acedGetAppName();<p></p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LAYOUT-GRID-MODE: char; mso-pagination: widow-orphan; mso-layout-grid-align: none;">int nPos = g_strCurPath.ReverseFind('\\');<p></p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LAYOUT-GRID-MODE: char; mso-pagination: widow-orphan; mso-layout-grid-align: none;">CString strDbxPath = g_strCurPath.Left(nPos+1);<p></p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LAYOUT-GRID-MODE: char; mso-pagination: widow-orphan; mso-layout-grid-align: none;">#include "acdocman.h"<br/>//得到文档位置<span lang="EN-US"><p></p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LAYOUT-GRID-MODE: char; mso-pagination: widow-orphan; mso-layout-grid-align: none;">AcApDocument* pDoc = acDocManager-&gt;curDocument();<p></p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LAYOUT-GRID-MODE: char; mso-pagination: widow-orphan; mso-layout-grid-align: none;">CString path=pDoc-&gt;fileName();<p></p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LAYOUT-GRID-MODE: char; mso-pagination: widow-orphan; mso-layout-grid-align: none;">获取当前<span lang="EN-US">AutoCAD的路径<p></p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LAYOUT-GRID-MODE: char; mso-pagination: widow-orphan; mso-layout-grid-align: none;">string filename = Process.GetCurrentProcess().MainModule.FileName;<br/>得到<span lang="EN-US">AutoCAD的完整路径,包含acad.exe文件名,如C:\Program Files\AutoCAD 2008\acad.exe<br/>string path = Path.GetDirectoryName(filename);<br/>得到AutoCAD的路径,如C:\Program Files\AutoCAD 2008<p></p></span></p>

庞琛虹 发表于 2008-12-28 10:17:00

gmy2009 发表于 2009-4-1 19:38:00

太好了,顶
页: [1] 2
查看完整版本: 共享一些代码(转自其他的论坛)