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 4039 0

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

admin 楼主

2017-5-6 14:18:38

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

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

x
CATIA二次开发入门教程---16 通过点击屏幕创建线, U3 @) ^8 ~( m2 ?$ H
0 O( O( e) J; _' T5 h5 g- r* A
和上面例子类似,这里需要手动添加一些action事件,在头文件里面新增加一个 indication,效果如下/ U8 z: S0 \( z- }  l
create line.PNG
" ?8 Y0 m( T! P& S: E( m: V
8 }; y3 S7 ]  \1 f代码如下:- b/ H% G$ z- ?8 J- ]9 B

6 F6 ?: ~" @: o! d3 l8 j4 v- O6 L3 z* X( w9 p( j
//-------------------------------------------------------------------------; Q/ l1 \4 e6 E. V5 ^
// Constructor# z4 c0 e; x; R7 x0 n
//-------------------------------------------------------------------------
: U( B+ U8 \% C2 p- P5 hCreateLineCmd::CreateLineCmd() :, j0 x7 v- q& h4 {0 @
  CATStateCommand ("CreateLineCmd", CATDlgEngOneShot, CATCommandModeExclusive) : M7 P! k) x: V+ d/ F4 S$ w, P2 ]
//  Valid states are CATDlgEngOneShot and CATDlgEngRepeat
  {. v1 _# ?3 z; G. L3 u/ S1 M  ,_IndicationPoint1(NULL) ,_IndicationPoint2(NULL)
. I7 H' Y4 r1 }3 h% F{
3 I  s8 P0 c1 P- g  f}
' F. r3 `: Z) z0 P9 [" l1 |//-------------------------------------------------------------------------
  `$ z0 U& ]6 h; Q/ U// Destructor
% g; }' ]8 }/ g. l8 |2 d5 t1 p8 B//-------------------------------------------------------------------------7 P- b0 M! |0 m2 T. r/ s: `  R- {
CreateLineCmd::~CreateLineCmd()& W( I: o( [5 Z2 n2 j
{$ I, e# x6 v! V1 B
   if (_IndicationPoint1!= NULL) - u7 o" s4 s1 _* {+ b$ k
      _IndicationPoint1->RequestDelayedDestruction();9 U1 @/ E6 k$ T0 z! t
   if (_IndicationPoint2!= NULL)
7 c# K* W* ~" r( d' s1 Y    _IndicationPoint2->RequestDelayedDestruction();3 s+ T# X' p$ c' S$ B/ B. f
5 X" `( R6 g- N, c% h% H: u' l8 V
}
% }5 ?" Y8 A& A( w9 |
$ |$ g/ G& R' L/ V8 `+ i$ E( P//-------------------------------------------------------------------------
3 O  H/ s/ ^/ c' O! a( Q- v// BuildGraph()
' [! k! S/ U. F( `% X//-------------------------------------------------------------------------2 A) D/ L/ J& O0 T$ q( }! Q0 e
void CreateLineCmd::BuildGraph()
% C2 B4 S1 o2 s" L{$ V# f' C4 d* _3 _4 O
& [+ i* g7 k2 V# [; J' @) v
  // TODO: Define the StateChart / \& B0 g" Y0 r8 I- S! c
  // ---------------------------
* V3 V8 K( ~$ I4 t8 t' u  _IndicationPoint1 = new CATIndicationAgent ("IndicationOne");
1 p% |) t  P4 S  d  H. c  AddCSOClient(_IndicationPoint1);
, H/ F. O; q5 x' ]! \8 K7 g; z3 c  _IndicationPoint2 = new CATIndicationAgent ("IndicationTwo");
. Y5 e1 E+ b& {) x  m4 Q2 c  CATMathPlane PlaneXY;
' A2 `# h6 [3 A8 b  \: c  v' d, B+ Y  _IndicationPoint1 -> SetMathPlane (PlaneXY);
7 u$ [) M# w3 o8 w, B  _IndicationPoint2 -> SetMathPlane (PlaneXY);: e* R$ k- Z- u
  CATDialogState * initialState1 = GetInitialState("select first point");% I- ]( }" Y; s/ c# Y
  initialState1 -> AddDialogAgent (_IndicationPoint1);
- q: X  s7 G/ L" r
5 K5 n) a2 \  a) |9 z2 B  CATDialogState * initialState2 = AddDialogState("select second point"); // add important
' l; ^6 T' c( Y! l; w) \2 T5 K. V  initialState2 -> AddDialogAgent (_IndicationPoint2);, l" _0 k% D& D0 ^, Q

5 t4 F. t/ f6 R$ @1 f3 J/ v& `  
3 k1 F) w! n! T- @" Y/ v0 I3 p5 J" Z  AddTransition( initialState1, initialState2,
4 I; n* S2 [/ c" k                 IsOutputSetCondition (_IndicationPoint1),
$ v7 b2 X7 K2 P# J6 l( P                 Action ((ActionMethod) &CreateLineCmd::ActionOne));% y- G  l/ b) B2 i, {
- S" f: `/ i2 Q
  AddTransition( initialState2, NULL, 1 X7 ^$ i! ~2 l/ W) [  ]
   IsOutputSetCondition (_IndicationPoint2),
- {. g4 d' [4 c0 X9 h: y  C   Action ((ActionMethod) &CreateLineCmd::ActionTwo));
! M  m* s7 V/ y}4 u8 n: I& f8 X/ n
- i8 D( j8 }! ?$ T5 u; H# j
//-------------------------------------------------------------------------! o, ]# B4 l# O2 g. @
// ActionOne ()
2 M, g6 k$ {8 e& S# p; a8 N//-------------------------------------------------------------------------" h6 h# F2 ~& v7 E' e
CATBoolean CreateLineCmd::ActionOne( void *data )  F% R0 ]4 O6 p% M6 [! ?- u1 X
{
/ l# e; J, p# L& g2 m& U' S  // TODO: Define the action associated with the transition ) r; N- Z; ^% M! ?
  // ------------------------------------------------------. m, c2 `6 s& h2 b+ p% J" u. B
  CATMathPoint2D point2D = _IndicationPoint1->GetValue();//获得一个2D的点5 ?" @& F6 [8 c  W0 i
  CATMathPoint Point3D;' \3 i% B* b+ P+ i
  CATMathPlane Plane = _IndicationPoint1->GetMathPlane();+ u8 y( k9 m# p( }8 n- }
  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点" h! ]6 P/ Z& n$ S
  //设置Container(非根节点)
2 n8 d; ^2 l( C- ?' @- U# Z. w$ t  //获得Editor0 E3 h9 `2 \9 Z  W, T9 n
  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
3 X7 O- P& I% i! T  //得到当前对象的文档; k6 r$ F& a1 ]
  CATDocument * pDocument = NULL ;
$ i0 e6 E7 Y& x  //取得当前活动对象) H  k3 {/ C' `, q0 R0 s
  CATPathElement activePath = pEditor->GetUIActiveObject();4 R7 m; o4 u+ n4 E5 x) Q% z3 l0 ^
  //取得当前活动的product
! Z3 f; z8 R% U% c3 a/ A8 W  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());
& a& v! l3 Z( i# L: T5 R0 \  //当前活动对象不存在
; _% l* I% o+ W- u: @! J( \$ V0 Q  if (pActiveProduct == NULL): f2 w1 ^5 X# a9 h0 I. B. G* {
  {& `1 h( s* K/ p) c; \( Y5 ?9 l/ o
    pDocument = pEditor->GetDocument();4 m7 _5 |3 X& b3 w0 y* W
  }
4 ~0 w* w9 d8 Q3 @3 J! v* Y  else: Z5 g  w3 u. z- _
  {0 Z- |% B( v0 u- Q# u8 w2 z; X  K
    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();
5 Q2 `, c" P: m    //当前对象的引用对象是否存在
: E# y) ~; a( L7 h# i! P    if ( NULL_var == spRef )
% w/ n7 Q) z+ P$ {( W  w$ g( h( I    {- j$ w( Y: m: u; l! L$ s& W
      return FALSE;5 @3 @* t0 l7 u9 b; C* C# c
    }
" F0 `: \0 i" ?7 f5 G! M    //当前对象的链接对象
+ x5 }) s2 X+ A1 R4 A0 y& |# @$ i    CATILinkableObject * piLinkableObject = NULL;( @) c  B  f9 P; F) c  _
    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                           
1 |5 L8 ^& H  R6 {) f  X4 u$ E    if ( FAILED(rc) )
! a4 ?: U/ R! w" [7 E( C    {6 F& g$ A. I$ a7 u4 t' ?
      piLinkableObject->Release();
* t& p0 ]  b, O& W5 u& H! `      piLinkableObject = NULL ;
7 P) d$ d7 _8 k* R8 G! V      return FALSE;
  H# i. _8 f1 J' m4 {; x5 V6 T    }, k  ^% g/ b3 P' j+ w, D
    //得到当前对象的文档3 p, t" A# F7 H$ E0 k
    pDocument = piLinkableObject->GetDocument();0 c  c- x) i7 z) u: X: _* U% u3 }# Q
    piLinkableObject->Release();# e2 `+ A# A8 |2 D  ^% o
    piLinkableObject = NULL ;& p: z3 @9 o1 ?2 e5 E8 G
    if ( NULL == pDocument): T  j9 q" _6 ]' n' B, i9 p* Z
    {
" B2 Q: w4 g3 M, I8 ^6 s6 m# T      return FALSE;
& ]% j2 m6 n" M' C% X) G1 _    }+ }1 d1 V  H5 o
  }
' v2 ]' Z' e: }; Y' ]% `5 D  //得到文档容器集
8 a/ z8 d. R- A/ x8 r  CATIContainerOfDocument * pIContainerOfDocument = NULL;
8 n( W' F$ {) j7 H' ^) h( A  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);: @7 P. {: l7 Z2 v! ]
  if (FAILED(rc))6 ~# x2 o9 L% b; S: @( {
  {
7 Z9 X9 |0 s; r9 c  _    //pIContainerOfDocument->Release();7 k( Y3 e. F3 R/ Q$ K6 h  I) y
    pIContainerOfDocument = NULL ;
; _5 V. F/ a* j% u* X    return FALSE;
6 r0 t$ C* h1 f4 T% `5 R  }6 E4 ?" r  ]0 y- t+ t
  //获得Document
* ]$ p& z2 I7 g) `) o$ C, f  CATIContainer* _pContainer = NULL;
! d* z& w3 R3 p  //获得SpecContainer+ X$ o* G; Z1 {: a
  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);/ e; ?7 A3 f+ ]
  cout << "container ok" <<endl;8 N, u: K3 [( A' ]7 t  p. b
+ l% ^& t! R4 m8 T: K. D
  //GSM工厂0 o+ R# {$ i+ l% p' |: w) O7 \
  CATIGSMFactory_var spGSMFactory = NULL_var;
, S* g# N# t/ E7 G  //设置工厂  * ~# q3 X' r4 T
  spGSMFactory = _pContainer;         8 {2 g+ G' f' S( e" b+ g+ ?
  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);# w7 b* p: t$ q( b; f1 h# @
  spSpecPoint1= spPoint;     - [! u3 y: g" X
  CATIGSMProceduralView_var spSndPntObj = spSpecPoint1;
6 H% C( u& I, \! _6 z  //*将点显示在屏幕上
: p% @* l3 H: P. x* c7 Z* F  spSndPntObj->InsertInProceduralView();0 [  O- q& P, F$ l, Z$ j
cout << "create point ok" <<endl;
+ G  O. R) c* C9 o# w* p  spSpecPoint1->Update();
# |: M4 R! h! W& K8 m4 L# Ocout << "update point ok" <<endl;
/ B  O8 e1 u1 i4 a& ]+ c% l7 q  return TRUE;' ~3 H# i/ z1 S0 u3 M9 ?
}$ N+ H5 F; L% K9 E# D3 {
/ c+ k  K+ ?: h( r" a  E( B1 Q

3 m7 P- ?% y, m, p6 x; T
8 d9 H0 a2 E* LCATBoolean CreateLineCmd::ActionTwo( void *data ). C/ q& i( v* {
{( Z2 n9 S4 D3 S7 V
// TODO: Define the action associated with the transition
( j7 q* E  ?7 V5 i4 E6 L4 K // ------------------------------------------------------
  j" l% f( l2 ^6 ]( P8 N    CATMathPoint2D point2D = _IndicationPoint2->GetValue();//获得一个2D的点
, @$ b9 g9 r4 }" q  CATMathPoint Point3D;
( t4 C, Q- X9 d! Z# Q; |+ t* J  CATMathPlane Plane = _IndicationPoint2->GetMathPlane();
* S% I% x* K+ H4 m' d! O  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点& g* \$ W$ S! ~+ _7 e
  //设置Container(非根节点)* Z  u$ H) n' K/ e
  //获得Editor
/ E- s" s3 O4 u# A/ H  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
, X9 y5 F; [% r  //得到当前对象的文档
) ~3 S. x2 |, [5 P  CATDocument * pDocument = NULL ;
  r' }' f  e" s* s. _  J  //取得当前活动对象* [# {0 h  }" Q3 Y  b/ J
  CATPathElement activePath = pEditor->GetUIActiveObject();+ D) g7 d3 ]( @+ O6 ]
  //取得当前活动的product7 [; b$ b9 H0 W  t* ]6 y
  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());
6 }6 a9 b* V9 F/ Q8 g, ]+ z  //当前活动对象不存在8 I5 h1 k+ C1 H. n. C
  if (pActiveProduct == NULL)
  [. x! U+ [) H! p1 {  {
% n7 g: E* {- R    pDocument = pEditor->GetDocument();
; b  B7 E. I0 i  }- j) i4 @+ k- N1 J/ g
  else' v; H& z  I4 D% V1 w& {
  {
% E! Y: q# a1 O" `# V" V) ^. f    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();
+ y# C4 r1 y2 g' Y: [1 @3 S    //当前对象的引用对象是否存在! n" f4 X; ?7 t+ M
    if ( NULL_var == spRef )
: i& {. b( u8 Y, Q+ N    {
) D3 Q+ U9 M! ~* N      return FALSE;5 \3 q% Q( B$ t
    }2 q: G( ^; m1 r( t) P/ E2 Q% {1 n+ ^/ N
    //当前对象的链接对象
& t+ w, ?  k7 C/ z6 X6 E    CATILinkableObject * piLinkableObject = NULL;- V: Z" d* j4 D
    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                            0 k6 ^7 w4 b$ L) a0 C/ V
    if ( FAILED(rc) )
: v- L' w2 R3 D8 N    {8 H0 W; B  h0 u) x% F+ j
      piLinkableObject->Release();
& l; V) e. p5 X/ p: O      piLinkableObject = NULL ;
4 V/ T8 Q! w- Y/ L& O. U0 S      return FALSE;
+ {' S0 d+ z+ O* I" u    }
+ p( m1 K6 _) G- V. L5 N) c    //得到当前对象的文档
7 N3 ^; h1 A8 d    pDocument = piLinkableObject->GetDocument();
3 C& c/ r% r% S( s2 l% e    piLinkableObject->Release();4 W; `. ~( f8 m# d* h9 i& b9 V
    piLinkableObject = NULL ;
% C/ i* q* L9 V' U8 C" F' ~    if ( NULL == pDocument)) o0 {) e" w3 E7 L8 U5 n+ t
    {
2 h7 s% U% ?( u  v' T      return FALSE;( ]/ P: T7 f# D2 H6 e3 d
    }
, g' b4 Z/ f6 N  }8 ?6 ]& ^: Q7 @0 {+ e
  //得到文档容器集
& S( S$ o! m* r" c6 X  CATIContainerOfDocument * pIContainerOfDocument = NULL;7 k" c: S2 f  o  c6 A$ e: Y
  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);1 V1 u; |) d% }: n
  if (FAILED(rc))
4 T. h3 D+ S! B1 T: S. W- ]% E% k  {
" R$ j2 `& A1 T    //pIContainerOfDocument->Release();# n& Y& ~4 g' t# g2 X3 v3 f3 f3 [
    pIContainerOfDocument = NULL ;7 H8 i. Q. b6 j3 I* ?
    return FALSE;; T; ~) H0 l0 K1 l6 _3 g# O
  }
$ g  M" @* a) B  //获得Document
  g; Q7 u8 ^  Y7 Q7 x/ r. A  CATIContainer* _pContainer = NULL;
' g/ U! ^8 Y: W0 D5 z( p0 Q  //获得SpecContainer5 ]) |5 @6 y+ k( T
  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);
$ |0 A# l# {8 \$ Q' O5 j8 ^
) U1 R4 B6 t7 B% S/ T5 ~  //GSM工厂4 [. h# A1 G( j$ E! O
  CATIGSMFactory_var spGSMFactory = NULL_var;
/ b. L- Z) r. z3 D8 D2 l' j  //设置工厂  . N  \( z6 R/ `6 j1 r: b9 i
  spGSMFactory = _pContainer;         
0 ]; U4 @, D) G9 i* {/ c  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);7 b' s8 }' e, w
  CATISpecObject_var spSpecPoint2= spPoint;     . R' G5 q& R8 ~% r0 ~/ J% m
  CATIGSMProceduralView_var spSndPntObj = spSpecPoint2;$ u, ?8 i6 k+ J, s
  //*将点显示在屏幕上0 |4 ^0 D& C" N% F
  spSndPntObj->InsertInProceduralView();
- V, M% B# o7 ?# o+ p  spSpecPoint2->Update();
6 I% e, m7 y: D$ U( d  //生成线- H. U7 R3 D3 }! r7 Z6 M
  CATIGSMLinePtPt_var spLine = spGSMFactory->CreateLine(spSpecPoint1, spSpecPoint2, NULL_var);
" V/ K3 ?/ h3 Q8 F" g  CATISpecObject_var spSpecLine= spLine;     ; T" y" j& c' ^+ u4 C) V% p: v+ G- Y% N
  CATIGSMProceduralView_var spSndPntObjLine = spSpecLine;8 |7 @8 r: ?2 R% Y( N. e- e& @
  //*将线显示在屏幕上
# K* f- ]* C5 s0 F  spSndPntObjLine->InsertInProceduralView();
1 \/ y; G/ M/ t* L- _  spSpecLine->Update();+ l9 E8 e( V; G9 _  F9 T

% R  z6 t8 c$ s return TRUE;
- L# p( _- m5 R. m7 U* c}
% S$ Y6 N0 I5 q; e& Q
% m/ ~- N! Q& l  ]8 v0 o$ N2 s: }) @! ^

- f2 i" i# N  V2 w5 |
0 d- O/ |7 Z- `3 q6 o; k7 X3 F; j. k" s  O& A; P
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了