PLM之家精品课程培训

PLM之家精品课程培训

联系电话:18301858168   |   QQ咨询:939801026
NX二次开发培训

NX二次开发培训

UFUN/NXOpen C++和实战案例

适合初级入门或想深入了解二次开发的工程师,本培训结合ufun,NXOpen C++,大量的实例及官方内部的开发技术。
公众号二维码

关注公众号

点击扫描二维码免费在线高清教程

课程详情
Catia二次开发培训

Catia二次开发培训

市场需求大,掌握核心技术前景广阔

Catia二次开发的市场需求大,人才稀缺。掌握开发技能潜力巨大,随着经验积累将在汽车、航空等领域有所作为。
B站二维码

在线原创B站视频

点击关注工业软件传道士主页

课程详情
Teamcenter培训

Teamcenter培训

全方位培训,从基础应用到高级开发全覆盖

涵盖用户应用基础培训、管理员基础培训、管理员高级培训及二次开发培训等全方位内容,由多年经验讲师打造。
QQ群二维码

加入同行交流

点击扫描二维码加入QQ群

课程详情
×

PLM之家plmhome公众号

课程涵盖: PLM之家所有原创视频

×

关注B站视频

所有高清视频一览无余,全部在线播放学习

×

加入PLM之家QQ群

同行交流,疑问解答,更多互助

PLM之家PLMHome-国产软件践行者

[原创] CATIA二次开发入门教程---16 通过点击屏幕创建线

[复制链接]

2017-5-6 14:18:38 4017 0

admin 发表于 2017-5-6 14:18:38 |阅读模式

admin 楼主

2017-5-6 14:18:38

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

x
CATIA二次开发入门教程---16 通过点击屏幕创建线
9 p9 s# X2 N$ p$ N- Z8 W3 c3 c9 c' n2 w# Y# i% E
和上面例子类似,这里需要手动添加一些action事件,在头文件里面新增加一个 indication,效果如下
. g5 [4 S! y5 P5 S) G/ C create line.PNG 1 i# S+ V7 \: S9 t) q7 e

; y# Y, e0 L5 {/ i; |代码如下:2 q6 w& H7 K3 _& j' R

$ y. B/ K2 \6 {4 L2 u. _! t
6 _( C8 w' R9 M) a- b2 e& D6 N4 Q//-------------------------------------------------------------------------
5 ]& u2 B, V" I* f' X6 z! i// Constructor& H* S. v6 Z( `2 Z' t5 C* n* |* E
//-------------------------------------------------------------------------% ?* _1 g: O# ^
CreateLineCmd::CreateLineCmd() :8 s6 b1 g( j7 A$ e
  CATStateCommand ("CreateLineCmd", CATDlgEngOneShot, CATCommandModeExclusive)
, K! h6 M9 _, h/ C$ g' b6 i//  Valid states are CATDlgEngOneShot and CATDlgEngRepeat
( E  j: w" g" j( V. k5 P7 H9 [  ,_IndicationPoint1(NULL) ,_IndicationPoint2(NULL)" u5 c2 b1 }& L, M2 ^# a% D0 m
{8 o) m+ P. E5 V2 n& a& x1 b) a
}
3 ]8 `9 ~9 E# T" r5 d% b//-------------------------------------------------------------------------5 z0 a% H+ n! g) X& p$ [+ \5 `2 c
// Destructor  }# {2 X& S8 n2 Z  |1 `" j& c
//-------------------------------------------------------------------------9 S1 a& h2 K3 |/ w0 O
CreateLineCmd::~CreateLineCmd()7 |' g9 y8 w( s4 l' H& _
{
0 C6 {  Y1 {3 `6 k! V8 M. a6 m+ C   if (_IndicationPoint1!= NULL) - O8 G$ }/ x& A7 N9 e* I8 z' C1 J
      _IndicationPoint1->RequestDelayedDestruction();+ T5 k, ~0 t( e
   if (_IndicationPoint2!= NULL)
: x# c# A) V% U  ?7 ]% |1 C    _IndicationPoint2->RequestDelayedDestruction();( n- e! E5 n3 k4 u
* v! T/ y' N' h
}
/ g: h( ^6 w) X* \2 C! j
) g  d$ K" D4 S//-------------------------------------------------------------------------
7 ^6 l+ l7 S% t( y) _// BuildGraph()( B6 u$ f! A0 K" y
//-------------------------------------------------------------------------
% K7 V8 Q0 a  B& v' ?5 Qvoid CreateLineCmd::BuildGraph()% ]0 m0 P& h) R9 P$ b* V
{
+ T+ E! @; v* u0 ^' [' {3 X4 L) t4 x8 G5 P" B3 ?
  // TODO: Define the StateChart 2 r  u' \3 I$ j1 u: |0 q" \7 `
  // ---------------------------6 d  M) h9 Z% ^* N
  _IndicationPoint1 = new CATIndicationAgent ("IndicationOne");: U. K6 A( S- I' z
  AddCSOClient(_IndicationPoint1);
5 D, S. N) Q4 M3 @9 L: b  _IndicationPoint2 = new CATIndicationAgent ("IndicationTwo");0 r4 u: Z( t8 n5 N
  CATMathPlane PlaneXY;
# ?8 M/ m0 ]) V& O  _IndicationPoint1 -> SetMathPlane (PlaneXY); - r' E! z8 L3 @" r) N# F  O
  _IndicationPoint2 -> SetMathPlane (PlaneXY);4 V/ m1 [* c0 J& x: g
  CATDialogState * initialState1 = GetInitialState("select first point");
1 b* n' x" B. D0 C$ A  initialState1 -> AddDialogAgent (_IndicationPoint1);
/ g! X- e5 q( u7 r
1 Q( g% y' g1 b  v' i; c  CATDialogState * initialState2 = AddDialogState("select second point"); // add important
1 I8 ]1 s& D) k, R6 s) a8 m, u  initialState2 -> AddDialogAgent (_IndicationPoint2);
* y7 x; X. v' ^- y$ z! m1 L
5 s4 r4 [- s7 \8 L7 m. F5 O  ; M0 x9 O5 o' O* b& e1 Y
  AddTransition( initialState1, initialState2, : p( g# q; u& |+ A9 m6 C' _& c
                 IsOutputSetCondition (_IndicationPoint1),
1 K) q' z5 j; c& n* v                 Action ((ActionMethod) &CreateLineCmd::ActionOne));
* ?/ b6 q4 H5 t9 p: s6 W$ f
$ T) f8 U! Q3 o5 Y- J  T# G  AddTransition( initialState2, NULL, 2 F& ~0 w3 l$ {8 }2 b0 n) ]0 b
   IsOutputSetCondition (_IndicationPoint2),
% F4 Y  \& T0 i0 |9 E, _- u3 e   Action ((ActionMethod) &CreateLineCmd::ActionTwo));0 P- v& z; _$ S
}5 N# _$ D9 I0 u+ a" p+ f, i

: M! B4 @, J4 N. `4 Q6 Q//-------------------------------------------------------------------------
1 ~) H1 }3 f9 L  P+ C// ActionOne ()
0 g# E. T3 W4 Q. k; M, F//-------------------------------------------------------------------------
, l6 O0 F- q' E' j5 U+ CCATBoolean CreateLineCmd::ActionOne( void *data )& r0 {" ?( G2 E; S! t& i
{. \9 \: k  F) ]  s2 ?
  // TODO: Define the action associated with the transition
4 l/ Q; Q- r6 v2 P; y$ U  // ------------------------------------------------------3 f( K7 `- u" B' n+ W' y
  CATMathPoint2D point2D = _IndicationPoint1->GetValue();//获得一个2D的点! Z; @* t3 _9 m! o
  CATMathPoint Point3D;
4 y4 b- u8 ?4 S: I, K1 {0 I0 V6 e  CATMathPlane Plane = _IndicationPoint1->GetMathPlane();
8 G" |1 n# k5 o* V! I4 X- O# M  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点! e8 ?- T3 ~' C
  //设置Container(非根节点)
6 l/ J: E: S* N. K' t" H% W  //获得Editor
, ?/ u" `$ K6 i4 q# V  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();. ?; J( A' K& X/ v3 w: I
  //得到当前对象的文档; T, e2 m. k+ g9 W; v! _1 e4 n
  CATDocument * pDocument = NULL ;
( S8 b# ~( ?2 R, G$ i7 [* F  //取得当前活动对象# J5 X6 q5 z" L8 z7 e9 d
  CATPathElement activePath = pEditor->GetUIActiveObject();
5 ?# d0 u6 U6 |! g$ \  //取得当前活动的product
2 ?2 q6 I# ]- |' e6 a- }* N/ g  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());
( L# C! c: q+ K$ A  V) Q" ?  //当前活动对象不存在. b8 K1 l8 P% g, r0 x; }
  if (pActiveProduct == NULL); r; k5 ^$ @$ C0 h. ~0 p( z' l9 B
  {0 w3 B9 T' E& g% e
    pDocument = pEditor->GetDocument();
5 R3 e+ c/ g# m0 z8 _$ G* i4 I  }* h8 B- h# ?( z' f% f
  else: k* W& P+ t4 i% F& V+ j8 {* f; \
  {0 `8 N4 V5 ^+ d/ G
    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();
% N2 s) Q" M/ Z/ }: }  l/ U3 M    //当前对象的引用对象是否存在
: q- M0 ^2 e! y, z    if ( NULL_var == spRef )4 E1 Q& D$ Z0 p0 t; ]
    {6 `$ Q7 N; x" A& w( U+ V- D6 K# w7 y
      return FALSE;
( B1 e, C1 |9 R( G& x. o9 \# k    }) K& \, q- W6 ~0 ~- E; N5 n
    //当前对象的链接对象$ z0 ?! c+ w4 m8 ]& d
    CATILinkableObject * piLinkableObject = NULL;
0 ~* [* `& {4 n3 n    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                           
# r3 \" j& x! N0 b    if ( FAILED(rc) )8 P& K$ f# o* E) G+ g9 p7 Y3 Y9 r
    {( ]4 W0 f) C5 a8 U- A4 h
      piLinkableObject->Release();
6 c+ j4 O' Y! T1 y% B3 c  c5 U      piLinkableObject = NULL ;3 \) g' V6 \: x3 k+ C
      return FALSE;
% k* Y# \) Y. l2 e+ b6 |$ g! w    }
, Y8 p: ?6 ~, X$ Q1 c+ U    //得到当前对象的文档
0 k' [* n4 U1 T3 V) i3 ?& K    pDocument = piLinkableObject->GetDocument();
# l9 Z9 o" K7 k! [5 R. T& j- L# p    piLinkableObject->Release();
) r; d5 b) ~. |  c1 E: |! E    piLinkableObject = NULL ;- @' \6 R% l+ s* P5 L/ z+ N
    if ( NULL == pDocument)" b5 N' d, P) @$ x8 p; W; C& g
    {
  u* E) L9 ^/ {: E& l% R  C: F      return FALSE;
7 N/ H) J8 Y+ b# m    }) x& n% ~6 O) b0 E7 h* g, C
  }( F# S7 n1 e9 I8 T" R0 h
  //得到文档容器集* n& B: r/ [, L: C2 E, D
  CATIContainerOfDocument * pIContainerOfDocument = NULL;
0 v3 C2 l" g) S( w1 X  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);2 o+ R6 r) [2 ^3 b- X  j
  if (FAILED(rc))
8 G0 j* A' ^* k  {
+ r( g" Y' N% J    //pIContainerOfDocument->Release();1 Z  G  g% @: |2 X
    pIContainerOfDocument = NULL ;
4 c$ u9 F3 F* c7 _& H2 ^6 \5 t    return FALSE;2 R0 O" ]2 K- {" w8 a( X
  }3 ?, L; K; Y* Y; w# e  J
  //获得Document; w8 P0 V$ F1 I2 O) l9 ]5 u
  CATIContainer* _pContainer = NULL; 8 v$ ^5 j# A# l# I/ T
  //获得SpecContainer
/ D8 D: u* g9 {6 V5 r  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);. D$ f! \! T5 I( b2 R% Q! Y
  cout << "container ok" <<endl;
" F9 e. t* C5 w3 a: R. Z* h
, j* @( D0 M, P  {. a. m! s& a3 Q  //GSM工厂
, I* {6 [; E, J  n1 [6 D  CATIGSMFactory_var spGSMFactory = NULL_var;. H* R0 d* {& n- F0 Q% s& }3 i
  //设置工厂  
. R' B4 z) v7 R8 r' u  spGSMFactory = _pContainer;         
+ q- B1 @+ K8 s' d8 w. ^8 j  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);% f! A' Y. }8 `' C1 u* e; x
  spSpecPoint1= spPoint;     6 G2 n# F6 s& y! E9 n* w0 {7 D& @
  CATIGSMProceduralView_var spSndPntObj = spSpecPoint1;% Y4 E/ |2 ]9 g' ~0 A
  //*将点显示在屏幕上- b* }  t1 A. R1 }- Y" C" ?# V
  spSndPntObj->InsertInProceduralView();
0 `$ ]2 j9 x; U- `6 V4 q6 [9 h* M cout << "create point ok" <<endl;+ F/ t8 v% }# h+ m
  spSpecPoint1->Update();
0 n: P' K: G4 t, i3 J. h0 |3 Y& C- W( mcout << "update point ok" <<endl;3 W) a. X5 f  @9 H
  return TRUE;% O- b) |# e! O3 R7 ~
}; X( G# b1 ?1 m( @  w
- G+ v* ?; N: s

9 w* G( o0 ^2 l: P
: s7 ]) Y6 e2 ^- R/ B7 j1 e. vCATBoolean CreateLineCmd::ActionTwo( void *data )
+ Y3 @0 [$ n) F! c{) u9 f/ \, B, u/ A2 n4 G& ]
// TODO: Define the action associated with the transition - B$ Y. b9 Z( e. S6 S& ~6 q! q
// ------------------------------------------------------
" B1 p) _# o# d0 J1 {. {, b    CATMathPoint2D point2D = _IndicationPoint2->GetValue();//获得一个2D的点
3 \5 Q1 }' U' u0 r# B$ t  CATMathPoint Point3D;+ q( @; W( t! A( M$ o
  CATMathPlane Plane = _IndicationPoint2->GetMathPlane();% F: G, K% X( O
  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点
1 W; U# A$ G9 o3 C  //设置Container(非根节点)
: N# z; R5 H1 R9 ?  //获得Editor- R- N% q1 G9 m
  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
  S6 h  v* ?" X, a. N6 |+ y  //得到当前对象的文档
8 J. D9 `4 ^% o8 v0 z1 Q1 S  CATDocument * pDocument = NULL ;" \, j2 V/ q) h& D. g
  //取得当前活动对象
! j/ M7 t" I* H7 j  CATPathElement activePath = pEditor->GetUIActiveObject();% z, n" K7 \) j+ L) X& N
  //取得当前活动的product6 u0 T* k# w3 U( Z
  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());& \, \2 n! N3 u% U
  //当前活动对象不存在
) p; Z9 G- h' R1 ~" A2 E6 O* A  if (pActiveProduct == NULL)
$ c7 Z; ?4 e" Q4 \2 c/ a% ]2 f1 u  {
1 ~5 n' q! M( }) W    pDocument = pEditor->GetDocument();. a: U- }- c- V6 N* c" B2 |
  }
' n$ \% H, j. c2 \6 F# Z: U9 Z  j  else/ U4 t* j+ f  D! d% z/ x+ n
  {% i0 p; _! _1 ^" p
    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();
3 j3 o7 b) G5 o8 f    //当前对象的引用对象是否存在% K2 n0 R4 X3 m' h0 O0 C; H' G
    if ( NULL_var == spRef )  q, _! x7 y. v! _1 ^% D
    {
# \, e  t9 n: n6 r0 _! n; @      return FALSE;
5 d2 L' Z& u7 x9 M. i/ g+ m* ]    }
) o+ h# ^6 X) R7 H/ H    //当前对象的链接对象
: d3 w1 h3 R( {3 v+ F' Q* s9 j7 b& r4 P    CATILinkableObject * piLinkableObject = NULL;7 K( m: D4 @& h2 O9 U- u- K! t4 U
    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                           
: h9 x! \7 w+ N& C( j    if ( FAILED(rc) )/ e/ d: @( R2 z( V2 G# {" k' M3 R
    {
! x4 A$ [7 d' Q' e/ N; z      piLinkableObject->Release();
0 h! b5 \  w4 |: x5 l& _8 O5 A; n# ~      piLinkableObject = NULL ;
5 Y/ J( {' A# M: O4 e      return FALSE;
/ u9 L5 g1 _+ u. z1 A' p    }) u& H9 O3 x% f; J( T0 y
    //得到当前对象的文档
: Y; ~, F3 J3 O4 O8 y4 u: F    pDocument = piLinkableObject->GetDocument();+ m( o; v! h8 {! P3 |( P% Q
    piLinkableObject->Release();; o7 q2 M( z) D6 \
    piLinkableObject = NULL ;* a& V/ P/ b5 ^" }7 b. U; i; v1 r. s9 c
    if ( NULL == pDocument)+ d& d8 V" m, N; D0 q, V  H  Z
    {
. g% U$ ?3 y6 E; H- I+ R4 Q+ j      return FALSE;
( O2 Y6 p5 w# O% B2 f9 n* l( `    }& q, s0 {1 z) Q$ F
  }+ \4 h) _& c8 m9 c
  //得到文档容器集" v& X; s% Y6 c' Q) K8 Z
  CATIContainerOfDocument * pIContainerOfDocument = NULL;( x2 t# F9 M& v# |! I$ P1 q% C
  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);
% b9 M8 Y* v# I, a! e; V  if (FAILED(rc))
- N  X, q4 x. l4 g7 E; m2 t  {
9 f! U: ~  L; w- x& U+ }, A4 R+ \    //pIContainerOfDocument->Release();
& m4 Y/ [/ A0 L    pIContainerOfDocument = NULL ;
) M) q) h- `/ r) N# Q% d    return FALSE;
- D3 H+ P6 q2 t* v  v: m  }! x! y4 w8 z- [
  //获得Document8 J& x& `) M# ^
  CATIContainer* _pContainer = NULL;
( }8 c9 u& a- [6 a- G/ \' b" s  //获得SpecContainer% o' v- d2 W. E" S) U
  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);
! |7 X8 b. ~* u9 D+ n9 w- s
8 L/ I/ f0 y1 h" z$ t! e  //GSM工厂
4 Y6 ~; Q) Y: U* p  j5 ]  CATIGSMFactory_var spGSMFactory = NULL_var;2 H4 [; c' c/ z3 I( d. X
  //设置工厂  9 F. m  o* l4 `" Z
  spGSMFactory = _pContainer;         
+ r, k  O4 O: \# [  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);. I# L) l+ Q. K# a1 l
  CATISpecObject_var spSpecPoint2= spPoint;     
4 T' t  a- `. ]( [/ J3 k/ d( V  CATIGSMProceduralView_var spSndPntObj = spSpecPoint2;" U2 u" G- s" h. |
  //*将点显示在屏幕上
3 M7 z! ~% l/ }( x8 `  spSndPntObj->InsertInProceduralView();
: ^- ^0 r- R- U" Y  spSpecPoint2->Update();
8 }7 i2 b! _: U+ s4 |* n" H: U! F& f  //生成线
$ m1 S9 W/ f4 @( l  CATIGSMLinePtPt_var spLine = spGSMFactory->CreateLine(spSpecPoint1, spSpecPoint2, NULL_var);
' S9 B& J/ Q6 o  CATISpecObject_var spSpecLine= spLine;     
( n& H) E7 ?1 [+ ?1 Y) H9 V7 A  CATIGSMProceduralView_var spSndPntObjLine = spSpecLine;. F5 ~* O6 G2 P3 ?2 _$ t
  //*将线显示在屏幕上7 E2 H# v7 y# {+ d0 e% P/ ?
  spSndPntObjLine->InsertInProceduralView();
- c2 W' {: k' \/ U  spSpecLine->Update();
0 l2 _8 d5 A2 x
. t7 n- f2 c; c return TRUE;; U6 x8 Q7 n3 J+ |- h
}4 ~; s. x2 J8 ~( g$ g* a* v; [; A

, h( A+ \5 `1 e. t7 f- H# Y
* T3 N" w: W8 g5 e/ a& V3 X# R1 _5 m% i( y3 F& K( F7 ?- A5 V

8 u4 W! X! Q) Q3 k) h& m- `- D% E1 c2 |) u! B
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com/ doTeam.tech
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了