在ProE二次开发中,时常需要遍历ProSolid下的面、点、轴等几何元素。我们知道,ProToolkit下的遍历函数还是有点小麻烦的,而ProWebLink中就简单很多,比如要遍历某ProSolid下的所有Group,代码如下:
1 var groupList = sld.ListGroups(); 2 for (var i=0; i<groupList.Count; ++i) 3 { 4 ... 5 }
那么,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 CProList<ProPoint> pntList = 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<pntList.size(); ++i) 16 { 17 ProPoint pnt = pntList[i]; 18 int id; 19 err = ProPointIdGet(pnt, &id); 20 CString cstr; 21 cstr.Format(TEXT("Point Id: %d."), id); 22 AfxMessageBox(cstr); 23 } 24 25 } 26 catch (exception& e) 27 { 28 AfxMessageBox(TEXT("exception.")); 29 }
再贴上我封装的代码:
1、CProArray.h 在前一篇博文<<类似vector的ProArray封装类CProArray>>中已贴出。
2、CProList.h
1 #ifndef _C_PRO_LIST_H_ 2 #define _C_PRO_LIST_H_ 3 4 #include <ProToolkit.h> 5 #include "CProArray.h" 6 #include <exception> 7 #include <stdexcept> 8 9 using std::exception; 10 using std::out_of_range; 11 using std::bad_alloc; 12 13 template<class TYPE, size_t reallocationSize = 5> 14 class CProList 15 { 16 public: 17 CProList() 18 //throw(bad_alloc) 19 : m_pArr(new CProArray<TYPE, reallocationSize>()) 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<TYPE&>(const_cast<const CProList*>(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<TYPE&>(const_cast<const CProList*>(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 try 120 { 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<TYPE&>(const_cast<const CProList*>(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 try 145 { 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<TYPE&>(const_cast<const CProList*>(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() const 194 { 195 return m_pArr->operator ProArray(); 196 } 197 198 private: 199 CProArray<TYPE, reallocationSize> *m_pArr; 200 }; 201 202 #endif
3、CProSolid.h
1 #ifndef _C_PRO_SOLID_H_ 2 #define _C_PRO_SOLID_H_ 3 4 #include <ProToolkit.h> 5 #include <ProSolid.h> 6 #include <ProCsys.h> 7 #include <ProSurface.h> 8 #include <ProAxis.h> 9 #include <ProPoint.h> 10 #include <ProQuilt.h> 11 #include <ProFeature.h> 12 #include <profeattype.h> 13 #include "CProList.h" 14 #include <exception> 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<ProCsys> ListCsyses() 49 { 50 ProError err; 51 CProList<ProCsys> 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<ProSurface> ListSurfaces() 64 { 65 ProError err; 66 CProList<ProSurface> 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<ProSurface> ListDatumSurfaces() 79 { 80 ProError err; 81 CProList<ProSurface> datumSurfList; 82 83 // get all quilts 84 CProList<ProQuilt> 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<quiltList.size(); ++i) 92 { 93 err = ProQuiltSurfaceVisit(quiltList[i], NULL, 94 _VisitQuiltAllSurfaces_, &datumSurfList); 95 if (err != PRO_TK_NO_ERROR 96 && err != PRO_TK_E_NOT_FOUND) 97 throw exception("ListDatumSurfaces failed."); 98 } 99 100 return datumSurfList; 101 } 102 103 // 104 // 列出所有的datum plane 105 // 106 CProList<ProSurface> ListDatumPlanes() 107 { 108 ProError err; 109 CProList<ProSurface> datumPlaneList; 110 111 // get all feats which feat type is PRO_FEAT_DATUM 112 CProList<ProFeature> datumPlaneFeatList; 113 err = ProSolidFeatVisit(m_sld, NULL, _VisitSolidAllDatumPlaneFeats_, &datumPlaneFeatList); 114 if (err != PRO_TK_NO_ERROR 115 && err != PRO_TK_E_NOT_FOUND) 116 throw exception("ListDatumPlanes failed."); 117 118 // get datum plane in every datum plane feat 119 // but exclude which is inactive 120 for (int i=0; i<datumPlaneFeatList.size(); ++i) 121 { 122 ProSurface datumPlane; 123 err = ProSurfaceInit(datumPlaneFeatList[i].owner, datumPlaneFeatList[i].id+1, 124 &datumPlane); 125 ProGeomitem surf_geom_item; 126 err = ProSurfaceToGeomitem(m_sld, datumPlane, &surf_geom_item); 127 ProBoolean bIsInActive; 128 err = ProGeomitemIsInactive(&surf_geom_item, &bIsInActive); 129 130 if (bIsInActive == PRO_B_FALSE) 131 datumPlaneList.push_back(datumPlane); 132 } 133 134 return datumPlaneList; 135 } 136 137 // 138 // 列出所有的轴 139 // 140 CProList<ProAxis> ListAxises() 141 { 142 ProError err; 143 CProList<ProAxis> axisList; 144 err = ProSolidAxisVisit(m_sld, NULL, _VisitSolidAllAxises_, &axisList); 145 if (err != PRO_TK_NO_ERROR 146 && err != PRO_TK_E_NOT_FOUND) 147 throw exception("ListAxises failed."); 148 149 return axisList; 150 } 151 152 // 153 // 列出所有的Datum Points 154 // Note: 不包含Inactive的Datum Point 155 // 156 CProList<ProPoint> ListPoints() 157 { 158 ProError err; 159 CProList<ProPoint> pntList; 160 161 // get all feats 162 CProList<ProFeature> featList; 163 err = ProSolidFeatVisit(m_sld, NULL, _VisitSolidAllFeats_, &featList); 164 if (err != PRO_TK_NO_ERROR 165 && err != PRO_TK_E_NOT_FOUND) 166 throw exception("ListPoints failed."); 167 168 // get all points in every feat but exclude which is inactive 169 for (int i=0; i<featList.size(); ++i) 170 { 171 CProList<ProGeomitem> pntGeomitemList; 172 err = ProFeatureGeomitemVisit(&featList[i], PRO_POINT, NULL, 173 _VisitFeatAllPoints_, &pntGeomitemList); 174 if (err != PRO_TK_NO_ERROR 175 && err != PRO_TK_E_NOT_FOUND) 176 throw exception("ListPoints failed."); 177 for (int i=0; i<pntGeomitemList.size(); ++i) 178 { 179 ProBoolean bIsInActive; 180 ProGeomitemIsInactive(&pntGeomitemList[i], &bIsInActive); 181 if (bIsInActive == PRO_B_TRUE) 182 break; 183 ProPoint pnt; 184 ProGeomitemToPoint(&pntGeomitemList[i], &pnt); 185 pntList.push_back(pnt); 186 } 187 } 188 189 return pntList; 190 } 191 192 // 193 // 列出所有的面組 194 // 195 CProList<ProQuilt> ListQuilts() 196 { 197 ProError err; 198 CProList<ProQuilt> quiltList; 199 err = ProSolidQuiltVisit(m_sld, NULL, _VisitSolidAllQuilts_, &quiltList); 200 if (err != PRO_TK_NO_ERROR 201 && err != PRO_TK_E_NOT_FOUND) 202 throw exception("ListQuilts failed."); 203 204 return quiltList; 205 } 206 207 // 208 // 列出所有的特征 209 // 210 CProList<ProFeature> ListFeats() 211 { 212 ProError err; 213 CProList<ProFeature> featList; 214 err = ProSolidFeatVisit(m_sld, NULL, _VisitSolidAllFeats_, &featList); 215 if (err != PRO_TK_NO_ERROR 216 && 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
1 #include "CProSolid.h" 2 3 ProError _VisitSolidAllCsyses_(ProCsys p_csys, 4 ProAppData app_data) 5 { 6 CProList<ProCsys> *pCsysList = (CProList<ProCsys>*)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<ProSurface> *pSurfList = (CProList<ProSurface>*)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<ProAxis> *pAxisList = (CProList<ProAxis>*)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<ProQuilt> *pQuiltList = (CProList<ProQuilt>*)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<ProFeature> *pFeatList = (CProList<ProFeature>*)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<ProGeomitem> *pPntGeomitemList = (CProList<ProGeomitem>*)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<ProSurface> *pDatumSurfaceList = (CProList<ProSurface>*)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<ProFeature> *pDatumPlaneFeatList = (CProList<ProFeature>*)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时。所以,使用者无需自己释放数组内存。