creo二次開發(fā)遍歷封裝_第1頁
creo二次開發(fā)遍歷封裝_第2頁
creo二次開發(fā)遍歷封裝_第3頁
creo二次開發(fā)遍歷封裝_第4頁
creo二次開發(fā)遍歷封裝_第5頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、..7.28.ProSolid 下的遍歷訪問封裝代碼在ProE二次開發(fā)中,時(shí)常需要遍歷ProSolid下的面、點(diǎn)、軸等幾何元素。我們知道,ProToolkit 下的遍歷函數(shù)還是有點(diǎn)小麻煩的,而ProWebLink中就簡(jiǎn)單很多,比如要遍歷某ProSolid下 的所有Group,代碼如下:var groupList = sld.ListGroups();for (var i=0; igroupList.Count; +i) TOC o 1-5 h z .那么,Pr

2、oToolkit下是否可以封裝類似的代碼呢?當(dāng)然可以。先貼一段我封裝之后遍歷訪問ProSolid下所有Datum Point的代碼:ProError err;ProMdl mdl_curr;err = ProMdlCurrentGet(&mdl_curr);try/利用當(dāng)前Model構(gòu)造CProSolid對(duì)象,并遍歷訪問其下的所有Datum PointCProSolid sld(ProSolid)mdl_curr);CProList pntList = sld.ListPoints();CString cstr;cstr.Format(TEXT(%d), pntList.size();AfxM

3、essageBox(cstr);/對(duì)每個(gè)Datum Point執(zhí)行操作for (int i=0; ipntList.size(); +i)ProPoint pnt = pntListi;int id;err = ProPointIdGet(pnt, &id);CString cstr;cstr.Format(TEXT(Point Id: %d.), id);AfxMessageBox(cstr);catch (exception& e)AfxMessageBox(TEXT(exception.);再貼上我封裝的代碼:1、CProArray.h 在前一篇博文類似vector的ProArray封裝

4、類CProArray中已貼出。2、CProList.h#ifndef _C_PRO_LIST_H_#define _C_PRO_LIST_H_3.#include #include CProArray.h#include #include 8.using std:exception;using std:out_of_range;using std:bad_alloc;12.templateclass CProList16. public:6.37./throw(bad

5、_alloc):m_pArr(new CProArray() CProList(const CProList& rhs):m_pArr(rhs.m_pArr)+(m_pArr-use);CProList& operator=(const CProList& rhs)if (-(m_pArr-use) = 0)delete m_pArr;+(rhs.m_pArr-use);m_pArr = rhs.m_pArr;CProList()5

6、.1.if (-(m_pArr-use) = 0) delete m_pArr;public:size_t size() constreturn m_pArr-size();bool is_empty() constreturn m_pArr-is_empty();void clear()m_pArr-clear();void push_back(const TYPE& val)m_pArr-push_back(val);void pop_back()m_pArr-pop_back();const TY

7、PE& front() const/throw(out_of_range)tryreturn m_pArr-front();catch (const out_of_range&)throw out_of_range(empty CProList.);catch (.)throw;000021.122.123.T

8、YPE& front()/throw(out_of_range)return const_cast(const_cast(this)-front()const TYPE& back() const/throw(out_of_range)tryreturn m_pArr-back();catch (const out_of_range&)throw out_of_range(empty CProList.);catch (.)throw;TYPE& back()/throw(out_of_range)return const_cast(const_cast(this)-back()const T

9、YPE& operator(size_t index) const/throw(out_of_range)if (is_empty()throw out_of_range(empty CProList.);tryreturn m_pArr-operator(index);catch (const out_of_range&)124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165throw out_o

10、f_range(invalid index of CProList.);, catch (.),throw;, TYPE& operator(size_t index),/throw(out_of_range), return const_cast(const_cast(this)-operato r(index);, const TYPE& at(size_t index) const,/throw(out_of_range), if (is_empty()throw out_of_range(empty CProList.);try TOC o 1-5 h z ,return m_pArr

11、-at(index);,catch (const out_of_range&),throw out_of_range(invalid index of CProList.);,catch (.),throw;, TYPE& at(size_t index),/throw(out_of_range), return const_cast(const_cast(this)-at(index);, void insert_at(size_t index, const TYPE& val),/throw(out_of_range, bad_alloc)166.167.if (size() insert

12、_at(index, val);171.172.173.void insert_at(size_t index, size_t ent, const TYPE *pVal)174./throw(out_of_range, bad_alloe)175.176.if (size() insert_at(index, ent, pVal);180.181.182.void remove_at(size_t index, size_t ent = 1)183./throw(out_of_range)184.185.if (size() = index)186.throw out_of_range(in

13、valid index of CProList.);187.if (size() - index remove_at(index, ent);191.192.193.operator ProArray() const194.195.return m_pArr-operator ProArray();196.197.198.private:199.CProArray *m_pArr;200.;201.202.#endif”);3、CProSolid.h1.#ifndef _C_PRO_SOLID_H_2.#define _C_PRO_SOLID_H_3.4.#include.9.1

14、6.47.48.#include #include #include #include #include #include #include #include #include CProList.h#include ProError _VisitSolidAllCsyses_(ProCsys p_csys,ProAppData app_data);ProError _VisitSo

15、lidAllSurfs_(ProSurface p_surface,ProAppData app_data);ProError _VisitSolidAllAxises_(ProAxis p_axis,ProAppData app_data);ProError _VisitSolidAllQuilts_(ProQuilt p_quilt,ProAppData app_data);ProError _VisitSolidAllFeats_(ProFeature* p_feature,ProAppData app_data);ProError _VisitFeatAllPoints_(ProGeo

16、mitem *p_handle,ProAppData app_data);ProError _VisitQuiltAllSurfaces_(ProSurface surface,ProAppData app_data);ProError _VisitSolidAllDatumPlaneFeats_(ProFeature* p_feature,ProAppData app_data);class CProSolidpublic:explicit CProSolid(ProSolid sld) : m_sld(sld) /列出所有的坐標(biāo)系/CProList ListCsyses()49.50.51

17、.8.89.ProError err;CProList csysList;err = ProSolidCsysVisit(m_sld, NULL, _VisitSolidAllCsyses_, &csysLis t);if (err != PRO_TK_NO_ERROR& err != PRO_TK_E_NOT_FOUND)throw exception(ListCsyses()

18、 failed.);return csysList;/列出所有的soild surface/CProList ListSurfaces()ProError err;CProList surfList;err = ProSolidSurfaceVisit(m_sld, NULL, _VisitSolidAllSurfs_, &surfL ist);if (err != PRO_TK_NO_ERROR& err != PRO_TK_E_NOT_FOUND)throw exception(ListSurfaces failed.);return surfList;/列出所有的datum surfac

19、e/CProList ListDatumSurfaces()ProError err;CProList datumSurfList;/ get all quiltsCProList quiltList;err = ProSolidQuiltVisit(m_sld, NULL, _VisitSolidAllQuilts_, &quiltL ist);if (err != PRO_TK_NO_ERROR& err != PRO_TK_E_NOT_FOUND)throw exception(ListDatumSurfaces failed.);/ get all datum surfaces in

20、every quiltfor (int i=0; iquiltList.size(); +i)err = ProQuiltSurfaceVisit(quiltListi, NULL,_VisitQuiltAllSurfaces_, &datumSurfList);if (err != PRO_TK_NO_ERROR& err != PRO_TK_E_NOT_FOUND)throw exception(ListDatumSurfaces failed.);return datumSurfList; TOC o 1-5 h z ,/./列出所有的datum plane,/,CProList Lis

21、tDatumPlanes(),,ProError err;,CProListdatumPlaneList;,/ get all feats which feat type is PRO_FEAT_DATUM,CProListdatumPlaneFeatList;,err = ProSolidFeatVisit(m_sld, NULL, _VisitSolidAllDatumPlaneFeats_,&datumPlaneFeatList);if (err != PRO_TK_NO_ERROR,& err != PRO_TK_E_NOT_FOUND)throw exception(ListDatu

22、mPlanes failed.);,/ get datum plane in every datum plane feat,/ but exclude which is inactivefor (int i=0; idatumPlaneFeatList.size(); +i),,ProSurface datumPlane;,err = ProSurfaceInit(datumPlaneFeatListi.owner, datumPlaneFeatListi.id+1,&datumPlane);,ProGeomitem surf_geom_item;,err = ProSurfaceToGeom

23、item(m_sld, datumPlane, &surf_geom_item);,ProBoolean bIsInActive;9.100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129,err = ProGeomitemIsInactive(&surf_geom_item, &bIsInActive);1321321331341351361371381391401411421431441451461471481

24、49150151152153154155156157158159160161162163164165166167168169170171172, return datumPlaneList; TOC o 1-5 h z ,/,/列出所有的軸,/,CProList ListAxises(),,ProError err;,CProList axisList;,err = ProSolidAxisVisit(m_sld,NULL, _VisitSolidAllAxises_,&axisList);if (err != PRO_TK_NO_ERROR,& err != PRO_TK_E_NOT_FOU

25、ND)throw exception(ListAxises failed.);return axisList; TOC o 1-5 h z ,/,/列出所有的Datum Points,/Note:不包含Inactive的 Datum Point,/,CProListListPoints(),,ProError err;,CProList pntList;,/ get all feats,CProList featList;,err = ProSolidFeatVisit(m_sld, NULL, _VisitSolidAllFeats_, &featList);if (err != PRO_T

26、K_NO_ERROR,& err != PRO_TK_E_NOT_FOUND)throw exception(ListPoints failed.);,/ get all points in every feat but exclude which is inactivefor (int i=0; ifeatList.size(); +i), ,CProList pntGeomitemList;,err = ProFeatureGeomitemVisit(&featListi, PRO_POINT, NULL,173._VisitFeatAllPoints_, &pntGeomitemList

27、);174.if (err != PRO_TK_NO_ERROR175.& err != PRO_TK_E_NOT_FOUND)176.throw exception(ListPoints failed.);177.for (int i=0; ipntGeomitemList.size(); +i)178.179.ProBoolean bIsInActive;180.ProGeomitemIsInactive(&pntGeomitemListi, &bIsInActive);181.if (bIsInActive = PRO_B_TRUE)182.break;183.ProPoint pnt;

28、184.ProGeomitemToPoint(&pntGeomitemListi, &pnt);185.pntList.push_back(pnt);89.return pntList;190.191.192./193./列出所有的面組194./195.CProList ListQuilts()196.197.ProError err;198.CProList quiltList;199.err = ProSolidQuiltVisit(m_sld, NULL, _VisitSolidAllQuilts_, &quiltLis t);200.if (err != PR

29、O_TK_NO_ERROR201.& err != PRO_TK_E_NOT_FOUND)202.throw exception(ListQuilts failed.);203.204.return quiltList;205.206.207./208./列出所有的特征209./210.CProList ListFeats()211.212.ProError err;213.CProList featList;214.err = ProSolidFeatVisit(m_sld, NULL, _VisitSolidAllFeats_, &featList);if (err != PRO_TK_N

30、O_ERROR& err != PRO_TK_E_NOT_FOUND)throw exception(ListFeats failed.);218.return featList;221.public:ProSolid m_sld;225.226. #endif4、CProSolid.cpp#include CProSolid.h2.ProError _VisitSolidAllCsyses_(ProCsys p_csys,ProAppData app_data)CProList *pCsysList = (CProList*)app_data;pCsysList-push_back(p_cs

31、ys);8.return PRO_TK_NO_ERROR;11.ProError _VisitSolidAllSurfs_(ProSurface p_surface,ProAppData app_data)CProList *pSurfList = (CProList*)app_data;pSurfList-push_back(p_surface);17.return PRO_TK_NO_ERROR;20.ProError _VisitSolidAllAxises_(ProAxis p_axis,ProAppData app_data)CProList *pAxisList = (CProLi

32、st*)app_data;pAxisList-push_back(p_axis);26.return PRO_TK_NO_ERROR;ProError _VisitSolidAllQuilts_(ProQuilt p_quilt,ProAppData app_data)CProList *pQuiltList = (CProList*)app_data;pQuiltList-push_back(p_quilt);35.return PRO_TK_NO_ERROR;38.ProError _VisitSolidAllFeats_(ProFeature* p_feature,ProAppData

33、app_data)CProList *pFeatList =(CProList*)app_data;pFeatList-push_back(*p_feature);44.return PRO_TK_NO_ERROR;47.ProError _VisitFeatAllPoints_(ProGeomitem *p_handle,ProAppData app_data)CProList *pPntGeomitemList = (CProList*)app_da ta;pPntGeomitemList-push_back(*p_handle);53.return PRO_TK_NO_ERROR;56.

34、ProError _VisitQuiltAllSurfaces_(ProSurface p_surface,ProAppData app_data)CProList *pDatumSurfaceList = (CProList*)app_dat a;pDatumSurfaceList-push_back(p_surface);62.return PRO_TK_NO_ERROR;65.ProError _VisitSolidAllDatumPlaneFeats_(ProFeature* p_feature,ProAppData app_data)CProList *pDatumPlaneFeat

35、List = (CProList*)app_d ata;ProError err;5.76.err = ProFeatureTypeGet(p_feature, &feat_type); if (feat_type = PRO_FEAT_DATUM)pDatumPlaneFeatList-push_back(*p_feature);return PRO_TK_NO_ERROR;使用及代碼說明:1、使用時(shí)請(qǐng)包含以下頭文件即可:#include CProArray.h#include CProList.h#include CProSolid.h2、之所以可以向

36、WebLink那樣使用,關(guān)鍵是CProList類的封裝,該類有一個(gè)CProArray類的 指針,并且 CProArray 類中包含一個(gè)引用計(jì)數(shù),表明當(dāng)前有多少個(gè) CProList 對(duì)象引用了 CProArray對(duì)象,這樣,CProList對(duì)象的直接賦值實(shí)際上并沒有重新分配數(shù)組對(duì)象,僅僅是增 加了引用的數(shù)組CProArray的引用計(jì)數(shù)。3、CProList對(duì)象會(huì)自動(dòng)釋放申請(qǐng)的數(shù)組內(nèi)存,當(dāng)引用計(jì)數(shù)變?yōu)?時(shí)。所以,使用者無需自己 釋放數(shù)組內(nèi)存。類似vector的ProArray封裝類邙roArray在做ProE二次開發(fā)的時(shí)候,時(shí)常會(huì)用到數(shù)組ProArray,雖然掌握之后并不難,但是用法畢 竟不簡(jiǎn)潔,

37、有時(shí)候難免出錯(cuò),比如用ProArrayAlloc申請(qǐng)了一數(shù)組,但忘了釋放,這就會(huì)導(dǎo)致 內(nèi)存泄露,等等。我們知道,C+的標(biāo)準(zhǔn)庫STL中有vector,完全可以替代C中難用的原生數(shù)組,所以我 想,可不可以寫一個(gè)類似vector的類來封裝ProArray呢?經(jīng)過一段時(shí)間的編碼,終于完成了這個(gè)類似vector的CProArray類,代碼如下:CProArray.h#ifndef _C_PRO_ARRAY_H_#define _C_PRO_ARRAY_H_3.5.#include6.#include7.#include8.4.#include using std:exception;using std:

38、out_of_range;using std:bad_alloc;12.templateclass CProList;15.templateclass CProArrayfriend class CProList;20.public:CProArray()/throw(bad_alloc): m_arr(NULL), m_nCntAdd(reallocationSize), use(1),&m_arr)42.,&m_arr)42.throw bad_alloc(out of memory.);25.26.if (reallocationSize = 0) m_nCntAdd = 5;27.if

39、 (PRO_TK_OUT_OF_MEMORY = ProArrayAlloc(0, sizeof(TYPE),m_nCntAdd&m_arr)28.throw bad_alloc(out of memory.);29.30.31.CProArray()32.33.ProArrayFree(&m_arr);34.35.36.CProArray(const CProArray& arrSrc)37./throw(bad_alloc)38.:m_arr(NULL), m_nCntAdd(reallocationSize), use(1)39.40.if (reallocationSize = 0)m

40、_nCntAdd = 5;41.if (PRO_TK_OUT_OF_MEMORY = ProArrayAlloc(0, sizeof(TYPE),m_nCntAddsize_t nSizeSrc = arrSrc.size();if (0 != nSizeSrc)45.46.if (PRO_TK_OUT_OF_MEMORY =ProArrayObjectAdd(&m_arr, -1, nSizeSrc, arrSrc.m_arr)47.throw bad_alloc(out ofmemory.);1.CProArray& operator=(const CProArray&

41、 arrSrc)52./throw(bad_alloc)53.54.clear();55.size_t nSizeSrc = arrSrc.size();56.if (0 != nSizeSrc)57.58.if (PRO_TK_OUT_OF_MEMORY =ProArrayObjectAdd(&m_arr, -1, nSizeSrc, arrSrc.m_arr)59.throw bad_alloc(out ofmemory.);3.public:64.size_t size() nSize;67.ProArraySizeGet(m_arr,

42、&nSize);68.return nSize;69.70.71.bool is_empty() const72.73.return !size();74.75.76.void clear()77.78.if (0 != size()79.ProArrayObjectRemove(&m_arr,0, size();80.81.82.void push_back(const TYPE& val)83./throw(bad_alloc)84.85.if (PRO_TK_OUT_OF_MEMORY = ProArrayObjectAdd(&m_arr, -1, 1, (void*)(&val)86.

43、9.100101102103104105106107108109110111112113114115116117118119120121122123124125126127throw bad_alloc(”out of memory.);void pop_back()if (!is_empty()ProArrayObjectRemove(&m_arr, -1, 1);const TYPE& front() const/throw(out_of_range)if (is_empty()throw out_of_range(

44、empty CProArray.);return reinterpret_cast(m_arr)0;TYPE& front()/throw(out_of_range)return const_cast(const_cast(this)-front();const TYPE& back() const/throw(out_of_range)if (is_empty()throw out_of_range(empty CProArray.);return reinterpret_cast(m_arr)size()-1;TYPE& back()/throw(out_of_range)return c

45、onst_cast(const_cast(this)-back();const TYPE& operator(size_t index) const/throw(out_of_range)if (is_empty()throw out_of_range(empty CProArray.);128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168if (size() = index)throw out_of_

46、range(invalid index of CProArray.);return reinterpret_cast(m_arr)index; TYPE& operator(size_t index)/throw(out_of_range) return const_cast(const_cast(this)-operator(index); const TYPE& at(size_t index) const/throw(out_of_range) if (is_empty()throw out_of_range(empty CProArray.);if (size() = index)throw out_of_range(invalid index of CProArray.);return reinterpret_cast(m_arr)index; TYPE& at(size

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論