在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

View Code

  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

View Code

  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

View Code

 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时。所以,使用者无需自己释放数组内存。

转载于:https://www.cnblogs.com/Hisin/archive/2012/07/29/2613698.html

ProSolid下的遍历访问封装代码相关推荐

  1. 用Java线程获取优异性能(II)——使用同步连载线程访问关键代码部份

    摘要 开发者有时创建的多线程程序会生成错误值或产生其它奇怪的行为.古怪行为一般出现在一个多线程程序没使用同步连载线程访问关键代码部份的时候.同步连载线程访问关键代码部份是什么意思呢?在这篇文章中解释了 ...

  2. 012 背包二叉树遍历分析和代码编写

    文章目录 背包属性遍历 物品名字库遍历 数据整理 代码编写 背包属性遍历 从物品数量入手,搜索2字节 筛选出唯一的值 下两字节的访问断点,鼠标移动到物品上面,断点断下 物品数量=r14+0x10 这里 ...

  3. 解开 Windows 下的临界区中的代码死锁

    解开 Windows 下的临界区中的代码死锁 发布日期: 1/13/2005 | 更新日期: 1/13/2005 Matt Pietrek和 Russ Osterlund 本文假定您熟悉 Win32. ...

  4. ajax json 封装,Ajax--json(Ajax调用返回json封装代码、格式及注意事项)

    Ajax调用json封装代码: //Ajax调用返回JSON public function JsonQuery($sql,$type=1,$db="mydb") { //定义数据 ...

  5. 风云的银光志Silverlight4.0教程之遍历访问客户端用户的本地文件

    微软于PDC2009上发布Silverlight 4 Beta版,微软在Silverlight 4版本中处理了约8000个的Silverlight终端用户的请求,加入了一系列另开发人员兴奋的新特性,最 ...

  6. Joomla目录遍历及远程代码(CVE-2021-23132)--避坑指南

    目录 漏洞描述 漏洞复现 搭建环境 开始复现 漏洞描述 Joomla!是使用PHP语言加上MySQL数据库所开发的软件系统,是全球知名的一套内容管理系统(CMS). 本漏洞涉及到目录遍历及进一步导致的 ...

  7. 手把手:四色猜想、七桥问题…程序员眼里的图论,了解下?(附大量代码和手绘)...

    长文预警!本文作者Vardan Grigoryan是一名后端程序员,但他认为图论(应用数学的一个分支)的思维应该成为程序员必备. 本文从七桥问题引入,将会讲到图论在Airbnb房屋查询.推特推送更新时 ...

  8. 连通分量 java_Java编程实现深度优先遍历与连通分量代码示例

    深度优先遍历 深度优先遍历类似于一个人走迷宫: 如图所示,从起点开始选择一条边走到下一个顶点,没到一个顶点便标记此顶点已到达. 当来到一个标记过的顶点时回退到上一个顶点,再选择一条没有到达过的顶点. ...

  9. 封装成vla函数_不知道怎么封装代码?看看这几种设计模式吧!

    为什么要封装代码? 我们经常听说:"写代码要有良好的封装,要高内聚,低耦合".那怎样才算良好的封装,我们为什么要封装呢?其实封装有这样几个好处: 封装好的代码,内部变量不会污染外部 ...

最新文章

  1. axure按钮切换颜色_如何用Axure画出Web产品的列表组件:基础画法
  2. rsync配置与应用(转)
  3. C语言自学《四》---- 循 环
  4. 【Android 性能优化】布局渲染优化 ( GPU 过度绘制优化总结 | CPU 渲染过程 | Layout Inspector 工具 | View Tree 分析 | 布局组件层级分析 )
  5. svn查看登录过的账号密码
  6. python入门之函数调用educoder_Educode Python入门之函数调用
  7. python爬取糗事百科
  8. 免费福利 | 送你一份免费音频,让你躺着也能学习葡萄酒知识!
  9. 为什么企业需要采用混合云战略?
  10. 努比亚红魔3开启预约:鲁大师跑分破47万
  11. 中英文对照 —— 色彩的描述
  12. css base64 图片背景
  13. Atitit 图像资料文档分类器 netpic image 网络图片与人像图片分类 微信图片分类 D:\0workspace\atiplat_img\src\com\attilax\img\ut
  14. Three.js地图轮廓分割效果
  15. 清理系统垃圾缓存BAT
  16. 接口测试常用工具及测试方法
  17. ue4-材质编辑器material
  18. Apache Struts2远程代码执行漏洞(S2-019)复现
  19. kafka 使用及学习过程中的爬坑记录
  20. 自动驾驶仿真软件SCANeR studio(四)scenario模式下脚本

热门文章

  1. 21个ui设计技巧,让你的设计不落伍
  2. ip访问php $_files空,PHP中表单没有问题但$_FILES为空怎么办?
  3. 数据库---分组查询
  4. ui设计 网络错误_UI设计人员常犯的10个错误
  5. 初学者也能看懂的 Vue3 源码中那些实用的基础工具函数
  6. 各个线程顺序循环执行
  7. 判断一个字符串是否为另外一个字符串旋转之后的字符串。
  8. 前台jsp页面向后台传汉字出现乱码问题解决办法
  9. 谷歌街景新功能——帮警方抓毒贩
  10. 用html编写ASCII表,HTML ASCII