在ProE二次开发中,时常需要遍历ProSolid下的面、点、轴等几何元素。我们知道,ProToolkit下的遍历函数还是有点小麻烦的,而ProWebLink中就简单很多,比如要遍历某ProSolid下的所有Group,代码如下:
1 var groupList = sld.ListGroups();2 for (var i=0; i
那么,ProToolkit下是否可以封装类似的代码呢?当然可以。
先贴一段我封装之后遍历访问ProSolid下所有Datum Point的代码:
1 ProError err; 2 ProMdl mdl_curr; 3 err = ProMdlCurrentGet(&mdl_curr); 4 5 try 6 { 7 // 利用当前Model构造CProSolid对象,并遍历访问其下的所有Datum Point 8 CProSolid sld((ProSolid)mdl_curr); 9 CProListpntList = sld.ListPoints();10 CString cstr;11 cstr.Format(TEXT("%d"), pntList.size());12 AfxMessageBox(cstr);13 14 // 对每个Datum Point执行操作15 for (int i=0; i
再贴上我封装的代码:
1、CProArray.h 在前一篇博文<<类似vector的ProArray封装类CProArray>>中已贴出。
2、CProList.h
View Code
1 #ifndef _C_PRO_LIST_H_ 2 #define _C_PRO_LIST_H_ 3 4 #include5 #include "CProArray.h" 6 #include 7 #include 8 9 using std::exception; 10 using std::out_of_range; 11 using std::bad_alloc; 12 13 template 14 class CProList 15 { 16 public: 17 CProList() 18 //throw(bad_alloc) 19 : m_pArr(new CProArray ()) 20 {} 21 22 CProList(const CProList& rhs) 23 : m_pArr(rhs.m_pArr) 24 { 25 ++(m_pArr->use); 26 } 27 28 CProList& operator=(const CProList& rhs) 29 { 30 if (--(m_pArr->use) == 0) 31 delete m_pArr; 32 ++(rhs.m_pArr->use); 33 m_pArr = rhs.m_pArr; 34 } 35 36 ~CProList() 37 { 38 if (--(m_pArr->use) == 0) 39 delete m_pArr; 40 } 41 42 public: 43 size_t size() const 44 { 45 return m_pArr->size(); 46 } 47 48 bool is_empty() const 49 { 50 return m_pArr->is_empty(); 51 } 52 53 void clear() 54 { 55 m_pArr->clear(); 56 } 57 58 void push_back(const TYPE& val) 59 { 60 m_pArr->push_back(val); 61 } 62 63 void pop_back() 64 { 65 m_pArr->pop_back(); 66 } 67 68 const TYPE& front() const 69 //throw(out_of_range) 70 { 71 try 72 { 73 return m_pArr->front(); 74 } 75 catch (const out_of_range&) 76 { 77 throw out_of_range("empty CProList."); 78 } 79 catch (...) 80 { 81 throw; 82 } 83 } 84 85 TYPE& front() 86 //throw(out_of_range) 87 { 88 return const_cast (const_cast (this)->front()); 89 } 90 91 const TYPE& back() const 92 //throw(out_of_range) 93 { 94 try 95 { 96 return m_pArr->back(); 97 } 98 catch (const out_of_range&) 99 {100 throw out_of_range("empty CProList.");101 }102 catch (...)103 {104 throw;105 }106 }107 108 TYPE& back() 109 //throw(out_of_range)110 {111 return const_cast (const_cast (this)->back());112 }113 114 const TYPE& operator[](size_t index) const 115 //throw(out_of_range)116 {117 if (is_empty())118 throw out_of_range("empty CProList.");119 try120 {121 return m_pArr->operator[](index);122 }123 catch (const out_of_range&)124 {125 throw out_of_range("invalid index of CProList.");126 }127 catch (...)128 {129 throw;130 }131 }132 133 TYPE& operator[](size_t index)134 //throw(out_of_range)135 {136 return const_cast (const_cast (this)->operator[](index));137 }138 139 const TYPE& at(size_t index) const 140 //throw(out_of_range)141 {142 if (is_empty())143 throw out_of_range("empty CProList.");144 try145 {146 return m_pArr->at(index);147 }148 catch (const out_of_range&)149 {150 throw out_of_range("invalid index of CProList.");151 }152 catch (...)153 {154 throw;155 }156 }157 158 TYPE& at(size_t index) 159 //throw(out_of_range)160 {161 return const_cast (const_cast (this)->at(index));162 }163 164 void insert_at(size_t index, const TYPE& val) 165 //throw(out_of_range, bad_alloc)166 {167 if (size() < index)168 throw out_of_range("invalid index of CProList.");169 170 m_pArr->insert_at(index, val);171 }172 173 void insert_at(size_t index, size_t cnt, const TYPE *pVal) 174 //throw(out_of_range, bad_alloc)175 {176 if (size() < index)177 throw out_of_range("invalid index of CProList.");178 179 m_pArr->insert_at(index, cnt, pVal);180 }181 182 void remove_at(size_t index, size_t cnt = 1) 183 //throw(out_of_range)184 {185 if (size() <= index)186 throw out_of_range("invalid index of CProList.");187 if (size() - index < cnt)188 throw out_of_range("count to remove is out of range");189 190 m_pArr->remove_at(index, cnt);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.h
View Code
1 #ifndef _C_PRO_SOLID_H_ 2 #define _C_PRO_SOLID_H_ 3 4 #include5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include "CProList.h" 14 #include 15 16 ProError _VisitSolidAllCsyses_(ProCsys p_csys, 17 ProAppData app_data); 18 19 ProError _VisitSolidAllSurfs_(ProSurface p_surface, 20 ProAppData app_data); 21 22 ProError _VisitSolidAllAxises_(ProAxis p_axis, 23 ProAppData app_data); 24 25 ProError _VisitSolidAllQuilts_(ProQuilt p_quilt, 26 ProAppData app_data); 27 28 ProError _VisitSolidAllFeats_(ProFeature* p_feature, 29 ProAppData app_data); 30 31 ProError _VisitFeatAllPoints_(ProGeomitem *p_handle, 32 ProAppData app_data); 33 34 ProError _VisitQuiltAllSurfaces_(ProSurface surface, 35 ProAppData app_data); 36 37 ProError _VisitSolidAllDatumPlaneFeats_(ProFeature* p_feature, 38 ProAppData app_data); 39 40 class CProSolid 41 { 42 public: 43 explicit CProSolid(ProSolid sld) : m_sld(sld) {} 44 45 // 46 // 列出所有的坐标系 47 // 48 CProList ListCsyses() 49 { 50 ProError err; 51 CProList csysList; 52 err = ProSolidCsysVisit(m_sld, NULL, _VisitSolidAllCsyses_, &csysList); 53 if (err != PRO_TK_NO_ERROR 54 && err != PRO_TK_E_NOT_FOUND) 55 throw exception("ListCsyses() failed."); 56 57 return csysList; 58 } 59 60 // 61 // 列出所有的soild surface 62 // 63 CProList ListSurfaces() 64 { 65 ProError err; 66 CProList surfList; 67 err = ProSolidSurfaceVisit(m_sld, NULL, _VisitSolidAllSurfs_, &surfList); 68 if (err != PRO_TK_NO_ERROR 69 && err != PRO_TK_E_NOT_FOUND) 70 throw exception("ListSurfaces failed."); 71 72 return surfList; 73 } 74 75 // 76 // 列出所有的datum surface 77 // 78 CProList ListDatumSurfaces() 79 { 80 ProError err; 81 CProList datumSurfList; 82 83 // get all quilts 84 CProList quiltList; 85 err = ProSolidQuiltVisit(m_sld, NULL, _VisitSolidAllQuilts_, &quiltList); 86 if (err != PRO_TK_NO_ERROR 87 && err != PRO_TK_E_NOT_FOUND) 88 throw exception("ListDatumSurfaces failed."); 89 90 // get all datum surfaces in every quilt 91 for (int i=0; i ListDatumPlanes()107 {108 ProError err;109 CProList datumPlaneList;110 111 // get all feats which feat type is PRO_FEAT_DATUM112 CProList datumPlaneFeatList;113 err = ProSolidFeatVisit(m_sld, NULL, _VisitSolidAllDatumPlaneFeats_, &datumPlaneFeatList);114 if (err != PRO_TK_NO_ERROR115 && err != PRO_TK_E_NOT_FOUND)116 throw exception("ListDatumPlanes failed.");117 118 // get datum plane in every datum plane feat119 // but exclude which is inactive120 for (int i=0; i ListAxises()141 {142 ProError err;143 CProList axisList;144 err = ProSolidAxisVisit(m_sld, NULL, _VisitSolidAllAxises_, &axisList);145 if (err != PRO_TK_NO_ERROR146 && err != PRO_TK_E_NOT_FOUND)147 throw exception("ListAxises failed.");148 149 return axisList;150 }151 152 //153 // 列出所有的Datum Points154 // Note: 不包含Inactive的Datum Point155 //156 CProList ListPoints()157 {158 ProError err;159 CProList pntList;160 161 // get all feats162 CProList featList;163 err = ProSolidFeatVisit(m_sld, NULL, _VisitSolidAllFeats_, &featList);164 if (err != PRO_TK_NO_ERROR165 && err != PRO_TK_E_NOT_FOUND)166 throw exception("ListPoints failed.");167 168 // get all points in every feat but exclude which is inactive169 for (int i=0; i pntGeomitemList;172 err = ProFeatureGeomitemVisit(&featList[i], PRO_POINT, NULL, 173 _VisitFeatAllPoints_, &pntGeomitemList);174 if (err != PRO_TK_NO_ERROR175 && err != PRO_TK_E_NOT_FOUND)176 throw exception("ListPoints failed.");177 for (int i=0; i ListQuilts()196 {197 ProError err;198 CProList quiltList;199 err = ProSolidQuiltVisit(m_sld, NULL, _VisitSolidAllQuilts_, &quiltList);200 if (err != PRO_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);215 if (err != PRO_TK_NO_ERROR216 && err != PRO_TK_E_NOT_FOUND)217 throw exception("ListFeats failed.");218 219 return featList;220 }221 222 public:223 ProSolid m_sld;224 };225 226 #endif
4、CProSolid.cpp
View Code
1 #include "CProSolid.h" 2 3 ProError _VisitSolidAllCsyses_(ProCsys p_csys, 4 ProAppData app_data) 5 { 6 CProList*pCsysList = (CProList *)app_data; 7 pCsysList->push_back(p_csys); 8 9 return PRO_TK_NO_ERROR;10 }11 12 ProError _VisitSolidAllSurfs_(ProSurface p_surface,13 ProAppData app_data)14 {15 CProList *pSurfList = (CProList *)app_data;16 pSurfList->push_back(p_surface);17 18 return PRO_TK_NO_ERROR;19 }20 21 ProError _VisitSolidAllAxises_(ProAxis p_axis,22 ProAppData app_data)23 {24 CProList *pAxisList = (CProList *)app_data;25 pAxisList->push_back(p_axis);26 27 return PRO_TK_NO_ERROR;28 }29 30 ProError _VisitSolidAllQuilts_(ProQuilt p_quilt,31 ProAppData app_data)32 {33 CProList *pQuiltList = (CProList *)app_data;34 pQuiltList->push_back(p_quilt);35 36 return PRO_TK_NO_ERROR;37 }38 39 ProError _VisitSolidAllFeats_(ProFeature* p_feature,40 ProAppData app_data)41 {42 CProList *pFeatList = (CProList *)app_data;43 pFeatList->push_back(*p_feature);44 45 return PRO_TK_NO_ERROR;46 }47 48 ProError _VisitFeatAllPoints_(ProGeomitem *p_handle,49 ProAppData app_data)50 {51 CProList *pPntGeomitemList = (CProList *)app_data;52 pPntGeomitemList->push_back(*p_handle);53 54 return PRO_TK_NO_ERROR;55 }56 57 ProError _VisitQuiltAllSurfaces_(ProSurface p_surface,58 ProAppData app_data)59 {60 CProList *pDatumSurfaceList = (CProList *)app_data;61 pDatumSurfaceList->push_back(p_surface);62 63 return PRO_TK_NO_ERROR;64 }65 66 ProError _VisitSolidAllDatumPlaneFeats_(ProFeature* p_feature,67 ProAppData app_data)68 {69 CProList *pDatumPlaneFeatList = (CProList *)app_data;70 ProError err;71 ProFeattype feat_type;72 err = ProFeatureTypeGet(p_feature, &feat_type);73 if (feat_type == PRO_FEAT_DATUM)74 pDatumPlaneFeatList->push_back(*p_feature);75 76 return PRO_TK_NO_ERROR;77 }
使用及代码说明:
1、使用时请包含以下头文件即可:
#include "CProArray.h"
#include "CProList.h"
#include "CProSolid.h"
2、之所以可以向WebLink那样使用,关键是CProList类的封装,该类有一个CProArray类的指针,并且CProArray类中包含一个引用计数,表明当前有多少个CProList对象引用了CProArray对象,这样,CProList对象的直接赋值实际上并没有重新分配数组对象,仅仅是增加了引用的数组CProArray的引用计数。
3、CProList对象会自动释放申请的数组内存,当引用计数变为0时。所以,使用者无需自己释放数组内存。