ProSolid下的遍历访问封装代码
在ProE二次开发中,时常需要遍历ProSolid下的面、点、轴等几何元素。我们知道,ProToolkit
下的遍历函数还是有点小麻烦的,而ProWebLink中就简单很多,比如要遍历某ProSolid下
的所有Group,代码如下:
upList=oups();
(vari=0;i<;++i)
3.{
5.}
那么,ProToolkit下是否可以封装类似的代码呢?当然可以。
先贴一段我封装之后遍历访问ProSolid下所有DatumPoint的代码:
orerr;
mdl_curr;
=ProMdlCurrentGet(&mdl_curr);
4.
6.{
7.//利用当前Model构造CProSolid对象,并遍历访问其下的所有DatumPoint
lidsld((ProSolid)mdl_curr);
st
gcstr;
(TEXT("%d"),());
sageBox(cstr);
13.
14.//对每个DatumPoint执行操作
(inti=0;i<();++i)
16.{
ntpnt=pntList[i];
;
=ProPointIdGet(pnt,&id);
gcstr;
(TEXT("PointId:%d."),id);
sageBox(cstr);
23.}
24.
25.}
(exception&e)
27.{
sageBox(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.#inclu春节联欢晚会重播
de
5.#include"CProArray.h"
6.#include
7.#include
8.
td::exception;
td::out_of_range;
td::bad_alloc;
12.
te
ProList
15.{
:
st()
18.//throw(bad_alloc)
19.:m_pArr(newCProArray
20.{}
21.
st(constCProList&rhs)
23.:m_pArr(rhs.m_pArr)
24.{
25.++(m_pArr->u);
26.}
27.
st&operator=(constCProList&rhs)
29.{
(--(m_pArr->u)==0)
m_pArr;
32.++(rhs.m_pArr->u);
33.m_pArr=rhs.m_pArr;
34.}
35.
36.~CProList()
37.{
(--(m_pArr->u)==0)
m_pArr;
40.}
41.
:
_tsi如何早睡 ze()const
44.{
m_pArr->size();
46.}
47.
_empty()const
49.{
m_pArr->is_empty();
51.}
52.
ear()
54.{
55.m_pArr->clear();
56.}
57.
sh_back(constTYPE&val)
59.{
60.m_pArr->push_back(val);
61.}
62.
p_back()
64.{
65.m_pArr->pop_back();
66.}
67.
YPE&front()const
69.//throw(out_of_range)
70.{
72.{
m_pArr->fro系统测试工程师 nt();
74.}
(constout_of_range&)
76.{
ut_of_range("emptyCProList.");
78.}
(...)
80.{
;
82.}
83.}
84.
&front()
86.//throw(out_of_range)
87.{
const_cast
;
89.}
90.
YPE&back()const
92.//throw(out_of_range)
93.{
95.{
m_pArr->back();
97.}
(constout_of_range&)
99.{
ut_of_range("emptyCProList.");
101.}
(...)
103.{
;
105.}
106.}
107.
&back()
109.//throw(out_of_range)
110.{
const_cast
;
112.}
113.
YPE&operator[](size_tindex)const
115.//throw(out_of_range)
116.{
(is_empty())
ut_of_range("emptyCProList.");
120.{
m_pArr->operator[](index);
122.}
(constout_of_range&)
124.{
ut_of_range("invalidindexofCProList.");
126.}
(...)
128.{
;
130.}
131.}
132.
&operator[](size_tindex)
134.//throw(out_of_range)
135.{
const_cast
r[](index));
137.}
138.
YPE&at(size_tindex)const
140.//throw(out_of_range)
141.{
(is_empty())
ut_of_range("emptyCProList.");
145.{
m_pArr->at(index);
147.}
(constout_of_range&)
149.{
ut_of_range("invalidindexofCProList.");
151.}
(...)
153.{
;
155.}
156.}
157.
&at(size_tindex)
159.//throw(out_of_range)
160.{
const_cast
x));
162.}
163.
rt_at(size_tindex,constTYPE&val)
165.//throw(out_of_range,bad_alloc)
166.{
(size()
ut_of_range("invalidindexofCProList.");
169.
170.m_pArr->inrt_at(index,val);
171.}
172.
rt_at(size_tindex,size_tcnt,constTYPE*pVal)
174.//throw(out_of_range,bad_alloc)
175.{
(size()
ut_of_range("invalidindexofCProList.");
178.
179.m_pArr->inrt_at(index,cnt,pVal);
180.}
181.
move_at(size_tindex,size_tcnt=1)
183.//throw(out_of_range)
1肠道出血怎么办 84.{
(size()<=index)
ut_of_range("invalidindexofCProList.");
(size()-index
ut_of_range("counttoremoveisoutofrange");
189.
190.m_pArr->remove_at(index,cnt);
191.}
192.
orProArray()const
194.{
m_pArr->operatorProArray();
196.}
197.
e:
ray
200.};
201.
202.#endif
3、CProSolid.h
1.#ifndef_C_PRO_SOLID_H_
2.#define_C_PRO_SOLID_H_
3.
4.#include
5.#include
6.#include
7.#include
8.#include
9.#include
10.#include
11.#include
12.#include
13.#include"CProList.h"
14.#include
15.
or_VisitSolidAllCsys_(ProCsysp_csys,
Dataapp_data);
18.
or_VisitSolidAllSurfs_(ProSurfacep_surface,
Dataapp_data);
21.
or_VisitSolidAllAxis_(ProAxisp_axis,
Dataapp_data);
24.
or_VisitSolidAllQuilts_(ProQuiltp_quilt,
Dataapp_data);
27.
or_VisitSolidAllFeats_(ProFeature*p_feature,
Dataapp_data);
30.
or_VisitFeatAllPoints_(ProGeomitem*p_handle,
Dataapp_data);
33.
or_VisitQuiltAllSurfaces_(ProSurfacesurface,
Dataapp_data);
36.
or_VisitSolidAllDatumPlaneFeats_(ProFeature*p_feature,
Dataapp_da炸汤圆怎么做 ta);
39.
ProSolid
41.{
:
itCProSolid(ProSolidsld):m_sld(sld){}
44.
45.//
46.//列出所有的坐标系
47.//
st
49.{
orerr;
st
=ProSolidCsysVisit(m_sld,NULL,_VisitSolidAllCsys_,&csysLis
t);
(err!=PRO_TK_NO_ERROR
54.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListCsys()failed.");
56.
csysList;
58.}
59.
60.//
61.//列出所有的soildsurface
62.//
st
64.{
orerr;
st
=ProSolidSurfaceVisit(m_sld,NULL,_VisitSolidAllSurfs_,&surfL
ist);
(err!=PRO_TK_NO_ERROR
69.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListSurfacesfailed.");
71.
surfList;
73.}
74.
75.//
76.//列出所有的datumsurface
77.//
st
79.{
orerr;
st
82.
83.//getallquilts
st
=ProSolidQuiltVisit(m_sld,NULL,_VisitSolidAllQuilts_,&quiltL
ist);
(err!=PRO_TK_NO_ERROR
87.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListDatumSurfacesfailed.");
89.
90.//getalldatumsurfacesineveryquilt
(inti=0;i<();++i)
92.{
=ProQuiltSurfaceVisit(quiltList[i],NULL,
94._VisitQuiltAllSurfaces_,&datumSurfList);
(err!=PRO_TK_NO_ERROR
96.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListDatumSurfacesfailed.");
98.}
99.
datumSurfList;
101.}
102.
103.//
104.//列出所有的datumplane
105.//
st
107.{
orerr;
st
110.
111.//getallfeatswhichfeattypeisPRO_FEAT_DATUM
st
=ProSolidFeatVisit(m_sld,NULL,_VisitSolidAllDatumPlaneFeats_
,&datumPlaneFeatList);
(err!=PRO_TK_NO_ERROR
115.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListDatumPlanesfailed.");
117.
118.//getdatumplaneineverydatumplanefeat
119.//butexcludewhichisinactive
(inti=0;i<();++i)
121.{
facedatumPlane;
=ProSurfaceInit(datumPlaneFeatList[i].owner,datumPlaneFea
tList[i].id+1,
124.&datumPlane);
mitemsurf_geom_item;
=ProSurfaceToGeomitem(m_sld,datumPlane,&surf_geom_item);
leanbIsInActive;
=ProGeomitemIsInactive(&surf_geom_item,&bIsInActive);
129.
(bIsInActive==PRO_B_FALSE)
_back(datumPlane);
132.}
133.
datumPlaneList;
135.}
136.
137.//
138.//列出所有的轴
139.//
st
141.{
orerr;
st
=ProSolidAxisVisit(m_sld,NULL,_VisitSolidAllAxis_,&axisLi
st);
(err!=PRO_TK_NO_ERROR
146.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListAxisfailed.");
148.
axisList;
150.}
151.
152.//
153.//列出所有的DatumPoints
154.//Note:不包含Inactive的DatumPoint
155.//
st
157.{
orerr;
st
160.
161.//getallfeats
st
=ProSolidFeatVisit(m_sld,NULL,_VisitSolidAllFeats_,&featLis
t);
(err!=PRO_TK_NO_ERROR
165.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListPointsfailed.");
167.
168.//getallpointsineveryfeatbutexcludewhichisinactive
(inti=0;i<();++i)
170.{
st
=ProFeatureGeomitemVisit(&featList[i],PRO_POINT,NULL,
173._VisitFeatAllPoints_,&pntGeomitemList);
(err!=PRO_TK_NO_ERROR
175.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListPointsfailed.");
(inti=0;i<();++i)
178.{
leanbIsInActive;
mitemIsInactive(&pntGeomitemList[i],&bIsInActive);
(bIsInActive==PRO_B_TRUE)
;
ntpnt;
mitemToPoint(&pntGeomitemList[i],&pnt);
_back(pnt);
186.}
187.}
188.
pntList;
190.}
191.
192.//
193.//列出所有的面組
194.//
st
196.{
orerr;
st
=ProSoli白居易是诗魔吗 dQuiltVisit(m_sld,NULL,个人健康承诺书 _VisitSolidAllQuilts_,&quilt
List);
(err!=PRO_TK_NO_ERROR
201.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListQuiltsfailed.");
203.
quiltList;
205.}
206.
207.//
208.//列出所有的特征
209.//
st
211.{
orerr;
st
=ProSolidFeatVisit(m_sld,NULL,_VisitSolidAllFeats_,&featLis
t);
(err!=PRO_TK_NO_ERROR
216.&&err!=PRO_TK_E_NOT_FOUND)
xception("ListFeatsfailed.");
218.
featList;
220.}
221.
:
idm_sld;
224.};
225.
226.#endif
4、
1.#include"CProSolid.h"
2.
or_VisitSolidAllCsys_(ProCsysp_csys,
Dataapp_data)
5.{
st
ist->push_back(p_csys);
8.
PRO_TK_NO_ERROR;
10.}
11.
or_VisitSolidAllSurfs_(ProSurfacep_surface,
Dataapp_data)
14.{
st
ist->push_back(p_surface);
17.
PRO_TK_NO_ERROR;
19.}
20.
or_VisitSolidAllAxis_(ProAxisp_axis,
Dataapp_data)
23.{
st
ist->push_back(p_axis);
26.
PRO_TK_NO_ERROR;
28.}
29.
or_VisitSolidAllQuilts_(ProQuiltp_quilt,
Dataapp_data)
32.{
st
List->push_back(p_quilt);
35.
PRO_TK_NO_ERROR;
37.}
38.
or_VisitSolidAllFeats_(ProFeature*p_feature,
Dataapp_data)
41.{
st
ist->push_back(*p_feature);
44.
PRO_TK_NO_ERROR;
46.}
47.
or_VisitFeatAllPoints_(ProGeomitem*p_handle,
Dataapp_data)
50.{
st
ta;
omitemList->push_back(*p_handle);
53.
PRO_TK_NO_ERROR;
55.}
56.
or_VisitQuiltAllSurfaces_(ProSurfacep_surface,
Dataapp_data)
59.{
st
a;
SurfaceList->push_back(p_surface);
62.
PRO_TK_NO_ERROR;
64.}
65.
or_VisitSolidAllDatumPlaneFeats_(ProFeature*p_feature,
Dataapp_data)
68.{
st
ata;
orerr;
ttypefeat_type;
=ProFeatureTypeGet(p_feature,&feat_type);
(feat_type==PRO_FEAT_DATUM)
PlaneFeatList->push_back(*p_feature);
75.
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时。所以,使用者无需自己
释放数组内存。
类似vector的ProArray封装类CproArray
在做ProE二次开发的时候,时常会用到数组ProArray,虽然掌握之后并不难,但是用法毕
竟不简洁,有时候难免出错,比如用ProArrayAlloc申请了一数组,但忘了释放,这就会导致
内存泄露,等等。
我们知道,C++的标准库STL中有vector,完全可以替代C中难用的原生数组,所以我
想,可不可以写一个类似vector的类来封装ProArray呢?
经过一段时间的编码,终于完成了这个类似vector的CProArray类,代码如下:
CProArray.h
1.#ifndef_C_PRO_ARRAY_H_
2.#define_C_PRO_ARRAY_H_
3.
4.#include
5.#include
6.#include
7.#include
8.
td::exception;
td::out_of_range;
td::bad_alloc;
12.
te
ProList;
15.
te
ProArray
18.{
classCProList
20.
:
ray()
23.//throw(bad_alloc)
24.:m_arr(NULL),m_nCntAdd(reallocationSize),u(1)
25.{
(reallocationSize==0)m_nCntAdd=5;
(PRO_TK_OUT_OF_MEMORY==ProArrayAlloc(0,sizeof(TYPE),m_nCntAdd
,&m_arr))
ad_alloc("outofmemory.");
29.}
30.
31.~CProArray()
32.{
ayFree(&m_arr);
34.}
35.
ray(constCProArray&arrSrc)
37.//throw(bad_alloc)
38.:m_arr(NULL),m_nCntAdd(reallocationSize),u(1)
39.{
(reallocationSize==0)m_nCntAdd=5;
(PRO_TK_OUT_OF_MEMORY==ProArrayAlloc(0,sizeof(TYPE),m_nCntAdd
,&m_arr))
ad_alloc("outofmemory.");
_tnSizeSrc=();
(0!=nSizeSrc)
45.{
(PRO_TK_OUT_OF_MEMORY==ProArrayObjectAdd(&m_arr,-1,nSizeS
rc,arrSrc.m_arr))
ad_alloc("outofmemory.");
48.}
49.}
50.
ray&operator=(constCProArray&arrSrc)
52.//throw(bad_alloc)
53.{
();
_tnSizeSrc=();
(0!=nSizeSrc)
57.{
(PRO_TK_OUT_OF_MEMORY==ProArrayObjectAdd(&m_arr,-1,nSizeS
rc,arrSrc.m_arr))
ad_alloc("outofmemory.");
60.}
61.}
62.
:
_tsize()const
65.{
ze;
aySizeGet(m_arr,&nSize);
nSize;
69.}
70.
_empty()const
72.{
!size();
74.}
75.
ear()
77.{
(0!=size())
ayObjectRemove(&m_arr,0,size());
80.}
81.
sh_back(constTYPE&val)
83.//throw(bad_alloc)
84.{
(PRO_TK_OUT_OF_MEMORY==ProArrayObjectAdd(&m_arr,-1,1,(void*)
(&val)))
ad_alloc("outofmemory.");
87.}
88.
p_back()
90.{
(!is_empty())
ayObjectRemove(&m_arr,-1,1);
93.}
94.
YPE&front()const
96.//throw(out_of_range)
97.{
(is_empty())
ut_of_range("emptyCProArray.");
reinterpret_cast
101.}
102.
&front()
104.//throw(out_of_range)
105.{
const_cast
));
107.}
108.
YPE&back()const
110.//throw(out_of_range)
111.{
(is_empty())
ut_of_range("emptyCProArray.");
reinterpret_cast
115.}
116.
&back()
118.//throw(out_of_range)
119.{
const_cast
);
121.}
122.
YPE&operator[](size_tindex)const
124.//throw(out_of_range)
125.{
(is_empty())
ut_of_range("emptyCProArray.");
(size()<=index)
ut_of_range("invalidindexofCProArray.");
reinterpret_cast
131.}
132.
&operator[](size_tindex)
134.//throw(out_of_range)
135.{
const_cast
or[](index));
137.}
138.
YPE&at(size_tindex)const
140.//throw(out_of_range)
141.{
(is_empty())
ut_of_range("emptyCProArray.");
(size()<=index)
ut_of_range("invalidindexofCProArray.");
reinterpret_cast
147.}
148.
&at(size_tindex)
150.//throw(out_of_range)
151.{
const_cast
ex));
153.}
154.
rt_at(size_tindex,constTYPE&val)
156.//throw(out_of_range,bad_alloc)
157.{
(size()
ut_of_range("invalidindexofCProArray.");
(size()==index)
=-1;
(PRO_TK_OUT_OF_MEMORY==ProArrayObjectAdd(&m_arr,index,1,(vo
id*)(&val)))
ad_alloc("outofmemory.");
164.}
165.
rt_at(size_tindex,size_tcnt,constTYPE*pVal)
167.//throw(out_of_range,bad_alloc)
168.{
(size()
ut_of_range("invalidindexofCProArray.");
(size()==index)
=-1;
(PRO_TK_OUT_OF_MEMORY==ProArrayObjectAdd(&m_arr,index,cnt,(
void*)pVal))
ad_alloc("outofmemory.");
175.}
176.
move_at(size_tindex,size_tcnt=1)
178.//throw(out_of_range)
179.{
(size()<=index)
ut_of_range("invalidindexofCProArray.");
(size()-index
ut_of_range("counttoremoveisoutofrange.");
ayObjectRemove(&m_arr,index,cnt);
185.}
186.
orProArray()const
188.{
m_arr;
190.}
191.
:
aym_arr;
e:
_nCntAdd;
_tu;
197.};
198.
199.#endif
代码说明:
1、该类代码经过我反复测试,应该不存在Bug。
2、该类的方法名完全参考vector类的方法名,内部全部是用ProArray的原生方法实现的。
3、在构造函数中通过ProArrayAlloc申请了内存,在析构函数中通过ProArrayFree释放了内
存,所以避免了内存泄露。
4、该类有属性为public的ProArray成员m_arr,如果在某些代码处,需要通过ProArray原
生方法操作数组,直接获取该成员即可。
5、该类在使用过程中,若出现访问越界等错误,会抛出异常。
6、该类使用C++的模板元编程实现。
7、由于每个方法代码都很短,所以全部写在头文件中,即全部为内联函数。
使用Demo:
ray
_back(6);
_back(7);
(inti=0;i<();++i)
5.{
gcstr;
(TEXT("%d"),nArr[i]);
sageBox(cstr);
9.}
本文发布于:2023-03-17 01:56:35,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/41f8703623c6938817c201a50058968b.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:icreo.doc
本文 PDF 下载地址:icreo.pdf
留言与评论(共有 0 条评论) |