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

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

admin 楼主

2017-5-6 14:18:38

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

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

x
CATIA二次开发入门教程---16 通过点击屏幕创建线
6 t* J+ u. k( h, M# }7 ]
; i& F2 I$ ^9 H! A/ i# H+ {/ O2 U和上面例子类似,这里需要手动添加一些action事件,在头文件里面新增加一个 indication,效果如下  m0 w. U9 F; V! s. z
create line.PNG
, o$ u% Z& w. }! D0 d8 E# L7 F
) ?. C2 w6 F; O代码如下:
2 X- {  o* S, P, A
0 G- x* }: c& ?3 ?% I& C
2 l  a+ n" V! g  i( j( h//-------------------------------------------------------------------------
, z1 k. Y* C, T7 g+ x: K// Constructor  N3 g, T5 T( \: c4 _( O
//-------------------------------------------------------------------------2 H( M4 Y& c! x, z( ^; d
CreateLineCmd::CreateLineCmd() :
3 z# C( C& V; x  CATStateCommand ("CreateLineCmd", CATDlgEngOneShot, CATCommandModeExclusive)   A! `1 u5 C( I: w6 T
//  Valid states are CATDlgEngOneShot and CATDlgEngRepeat
8 Q) V: o' {* {  ,_IndicationPoint1(NULL) ,_IndicationPoint2(NULL)
0 C6 j6 [% I: q8 C, r{
( R6 e3 w, t& L8 v/ F. ?! L5 a}
7 ^! J+ j0 f4 e4 e0 h//-------------------------------------------------------------------------
% w+ Y" _4 L- d0 p0 `// Destructor& b% c& x& y# i
//-------------------------------------------------------------------------
5 D+ }1 R& |2 z2 ]0 w* YCreateLineCmd::~CreateLineCmd()3 g  R: u+ ?' K5 s5 ?
{
4 i8 j7 G  J  h5 R. t. I* j* N   if (_IndicationPoint1!= NULL) - k9 x% K) z( c6 J+ X
      _IndicationPoint1->RequestDelayedDestruction();+ W- h! D" ]$ {# m, V/ O5 o
   if (_IndicationPoint2!= NULL)
+ s7 Y, @. d+ b1 q; }    _IndicationPoint2->RequestDelayedDestruction();
) Q8 R: ?* \- J0 Z( n" N# e8 X1 j1 ?1 ^" j( p7 H
}" ]$ H7 `  J, @0 ~: z. v

, Q5 L; M- {& g( q. k2 i! u, e) O  ~8 ^0 g//-------------------------------------------------------------------------
% ~  E8 G7 l( q) D7 ~// BuildGraph(): q4 G* R- c: ^1 T8 f1 M" ?
//-------------------------------------------------------------------------2 a$ M5 v% r  `8 m7 x0 x5 b
void CreateLineCmd::BuildGraph()
4 q# _1 |* O5 B{
& b  X. e; k# K9 |  e& [$ h2 h
. l$ q8 u% K) d2 g  // TODO: Define the StateChart 3 E2 ^3 t  U8 F% w
  // ---------------------------" S) F5 h" c8 E) r
  _IndicationPoint1 = new CATIndicationAgent ("IndicationOne");
/ e* E4 @, j1 I) N  AddCSOClient(_IndicationPoint1);
% l4 e& K0 ^/ M% ?  _IndicationPoint2 = new CATIndicationAgent ("IndicationTwo");, i) Z+ g9 J: \/ u
  CATMathPlane PlaneXY;
: |5 n, L/ y0 M6 v1 ^/ ^- [  _IndicationPoint1 -> SetMathPlane (PlaneXY); 4 g- t7 {' n9 Q( B) q; D) E
  _IndicationPoint2 -> SetMathPlane (PlaneXY);
- t+ \2 h" R. o5 M, `& G3 d  CATDialogState * initialState1 = GetInitialState("select first point");
4 Q! T; u4 k) |  initialState1 -> AddDialogAgent (_IndicationPoint1);* g# I0 B) L. O7 ?- f& [* m$ m

8 @0 f3 G4 H  U9 @; w2 m7 n  CATDialogState * initialState2 = AddDialogState("select second point"); // add important9 h6 T: D  n/ |5 ?/ H# q) g
  initialState2 -> AddDialogAgent (_IndicationPoint2);
3 y* p7 p. [$ k3 d3 c; Q& ]% ^9 I8 H4 S6 V2 j4 k
  ! E9 P1 P4 ]" u$ s8 P% Z! ~% j
  AddTransition( initialState1, initialState2, ( A& g6 G6 ]; Y0 X+ n( l' [: L
                 IsOutputSetCondition (_IndicationPoint1),9 t5 m! S5 I2 d4 i. N
                 Action ((ActionMethod) &CreateLineCmd::ActionOne));0 B; m/ n# ^- E) q
( Q4 z  p; k. @2 m5 H
  AddTransition( initialState2, NULL, ) u! r5 x, c4 ?7 l  ?" C
   IsOutputSetCondition (_IndicationPoint2),5 V" p! _+ i0 T+ J/ `. N
   Action ((ActionMethod) &CreateLineCmd::ActionTwo));! u- w0 x2 U  |; l- ?+ F7 ]
}
7 `* }+ I* W  A* x9 _9 P* T- e+ y( h8 \6 |9 \3 T2 C* l$ u" d
//-------------------------------------------------------------------------
0 z9 F: g! o5 {' `' o3 B// ActionOne ()
* s6 z4 S" N+ Y: {! i- b) F; ^//-------------------------------------------------------------------------  ^1 M- f) J  N$ M7 X# O4 b6 N
CATBoolean CreateLineCmd::ActionOne( void *data )$ a9 ?# ^/ d7 k) U; j
{
0 w" y$ W* u; a, D, y9 i4 U  // TODO: Define the action associated with the transition
" e: ]0 @8 j/ e  // ------------------------------------------------------
& T: T2 ]4 ?% o8 Y/ x/ [- |7 E4 j  CATMathPoint2D point2D = _IndicationPoint1->GetValue();//获得一个2D的点  T# ~" H- g$ C; z3 S
  CATMathPoint Point3D;5 R) X' X$ u( q* v
  CATMathPlane Plane = _IndicationPoint1->GetMathPlane();
  l1 `& x6 w0 I& r4 D- c7 K  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点
, p% `5 D, X# `# {. I  //设置Container(非根节点)
) w& q4 ]/ n: Z! I. v* ?  //获得Editor  k- ?) n( x9 W* c7 W1 q
  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
9 q: F9 I. \$ H  //得到当前对象的文档- O* j$ D- @4 [
  CATDocument * pDocument = NULL ;1 h( I! z3 T# ~
  //取得当前活动对象. A: B5 s1 q# t
  CATPathElement activePath = pEditor->GetUIActiveObject();
; _! O2 v* K6 N  //取得当前活动的product
& R* A% p3 F+ h+ @' a  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());
8 K9 Z3 b' f' s2 |  //当前活动对象不存在; t& G9 a, A: y2 D2 }/ x! x/ \
  if (pActiveProduct == NULL)
2 Q* l: X2 d7 g7 Z; S, s  {
! s; k! H4 m. Z, ~    pDocument = pEditor->GetDocument();3 m# H: X; P1 o' Y8 `, w2 N
  }
9 M& E* W+ Y  ]  else8 a. D5 r, o( x. N
  {  c1 p* J3 n' q; e, T) P6 q
    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();9 }7 q: N, a" u: T4 k9 p+ ]% ~
    //当前对象的引用对象是否存在2 I9 `6 v7 @9 Q% J$ }. B- g9 J
    if ( NULL_var == spRef )$ @' O! _8 M, s/ o
    {6 d' V0 @! w' t# I$ n
      return FALSE;
- S7 l) i- ]# ^( G5 K/ `    }
; _, v$ P# X" H& |' X' f  R2 y    //当前对象的链接对象
4 y  h& D" a, s  X' W2 ]& F    CATILinkableObject * piLinkableObject = NULL;; @* t  w9 v% {8 [$ j- l
    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                           
: _, J7 x3 T( U    if ( FAILED(rc) )
! M0 @, X- f( ^* l; w    {7 T8 s, {- Z9 N2 f! ]4 B1 F: R
      piLinkableObject->Release();% M; [+ \! U  O) n4 }
      piLinkableObject = NULL ;
5 Z* M/ `+ q4 k2 K" a$ n! F      return FALSE;
1 {& k9 a( ^# _% h2 R7 ~    }
) ~0 P3 k4 |4 s* o    //得到当前对象的文档. l; y/ X$ j8 j) f- J
    pDocument = piLinkableObject->GetDocument();  d# L# F1 D, m8 z$ }+ z0 R
    piLinkableObject->Release();
9 z6 e2 `* ^, m+ U. d: u    piLinkableObject = NULL ;5 t% R9 B# `( D+ {' j
    if ( NULL == pDocument)4 C( G; Q2 T" }, f' o
    {
/ Z- o; p; f, {, l3 Y2 V/ W      return FALSE;
# S+ z9 I4 B( z+ `& U! ~, t2 k    }% I# C1 |. D- U4 D
  }4 H7 g! G. h9 e$ w
  //得到文档容器集+ Y+ ?0 J! V5 C  a6 d3 d, u( D' `
  CATIContainerOfDocument * pIContainerOfDocument = NULL;: O9 g; h2 m$ j4 B' Q9 ^
  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);
* R. J, u5 @3 p7 A  S) O, W5 s  if (FAILED(rc))1 E* R0 }" E' s4 m) V
  {
+ [. g- ^; \2 [3 ?& c8 q" v& D) C2 R    //pIContainerOfDocument->Release();/ \% _$ x" p. ?4 V& i
    pIContainerOfDocument = NULL ;# F/ |# P7 G0 M0 D3 I
    return FALSE;
& U% M: ?1 c+ G  q, P. L  }
; t! l; f6 u/ y3 y2 Y3 A  //获得Document7 b( ]0 e4 \" l2 X5 f* }! P. f
  CATIContainer* _pContainer = NULL;
4 j+ h( v4 A( m3 A5 A  //获得SpecContainer! P7 k  p( @  n
  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);' y# r3 f4 H) a) ~2 u9 U' N5 N2 W
  cout << "container ok" <<endl;
9 o0 J8 W- R7 b( H" A : B1 b. l' \  i
  //GSM工厂
; s  z1 q7 k3 b  CATIGSMFactory_var spGSMFactory = NULL_var;# E5 p% t' _# W; ~& X+ p5 s) V
  //设置工厂  
" e0 r! Q$ d8 n( b" p  spGSMFactory = _pContainer;         . ], \3 I& w5 G: _7 G
  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);5 V7 q; X) U7 H9 l$ {( ?( z$ P/ [
  spSpecPoint1= spPoint;     - s1 F; z  m  x- q1 N; A. D
  CATIGSMProceduralView_var spSndPntObj = spSpecPoint1;8 k+ N% Y+ e6 L8 m0 c3 S* G+ L, i7 ?
  //*将点显示在屏幕上0 {6 x+ o) Y, U
  spSndPntObj->InsertInProceduralView();( z: S( ?  h9 D
cout << "create point ok" <<endl;
( Q: B0 W7 L: M: N2 h2 I9 w  spSpecPoint1->Update();
8 ^8 v/ z2 H2 j' i/ \: @" x) wcout << "update point ok" <<endl;
3 E1 s- }' Q  L' s8 Q; \  return TRUE;' H: N; D( A* r( a6 S5 E3 u+ R* j, J6 B
}
! t. k' ?: X  J5 c) \1 m+ H8 Y/ f% m1 j
4 c# Y7 Y& u3 y) A
1 {/ }' l/ ~  n2 Z: z7 Q& E$ _; A* _
CATBoolean CreateLineCmd::ActionTwo( void *data )2 P, l3 p3 Y5 m. B8 ~+ {9 |
{- B3 c$ `9 w, D, b/ s5 |) V
// TODO: Define the action associated with the transition ! x; K; m; ^! t' @, k
// ------------------------------------------------------& p& K  X3 x) E8 ?# P% D9 S6 F
    CATMathPoint2D point2D = _IndicationPoint2->GetValue();//获得一个2D的点. z& q+ A" i( @9 ~, ~3 c
  CATMathPoint Point3D;. S, o# z( u; Q, q! V' [
  CATMathPlane Plane = _IndicationPoint2->GetMathPlane();' r( p4 J" k, H
  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点. Z4 q$ T0 }: R* N1 j
  //设置Container(非根节点)
' R: |# |, H1 A7 C( {  //获得Editor: q0 z6 l0 J$ a" K7 `1 [/ H
  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
" E" F6 p5 P5 g1 g7 n2 V+ x* w  //得到当前对象的文档
5 C, {" i, @' `5 c0 R/ j  CATDocument * pDocument = NULL ;" A" L! t, N0 ]! w0 ^; K
  //取得当前活动对象
! q1 U9 ]8 v  T) j9 x% Q3 Z  CATPathElement activePath = pEditor->GetUIActiveObject();& B! |3 q2 c( X$ f; x9 t
  //取得当前活动的product: |$ k; o- _4 q" J3 C$ ~6 M
  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());; l1 K- `& o* @
  //当前活动对象不存在) ?: ^6 @- H6 x" G7 i
  if (pActiveProduct == NULL)% H2 t# G2 {! x2 I. o4 l
  {
  r+ y  U0 F4 E5 C! j# }0 h    pDocument = pEditor->GetDocument();
$ i: _: m4 H, t. j! B, F  }; j0 e/ t( ^$ m1 t6 k$ p
  else
6 a* ]) B  j5 c( z7 E) f6 {  {
) X: f7 I5 G; j6 a# d, O' n5 e    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();6 M5 m, u) M" R( H
    //当前对象的引用对象是否存在$ r6 Z' U# R0 ^' U# {( @
    if ( NULL_var == spRef ). U& U/ N0 ^1 Z4 G1 L
    {) Z: W- N1 y/ l4 L& r3 ?
      return FALSE;
! V- Y, r1 p# |* U; ?    }
" R  L# n0 R" J9 |% z    //当前对象的链接对象1 U* P0 |% @, j7 j+ t  \. n
    CATILinkableObject * piLinkableObject = NULL;
* s7 y) I* t( F( b7 |: v" g+ u+ Y/ C    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                           
/ H+ Z, }2 r' D4 \. v& g9 b    if ( FAILED(rc) )
0 B, q& v8 Q( o4 W    {6 ~& b9 y( T' U. P
      piLinkableObject->Release();5 ~& p% p& O' I& p1 ~/ y  O
      piLinkableObject = NULL ;
) g% `$ @. X( x# `      return FALSE;
2 v, ^9 p' m: ]    }
& a; W1 V/ ?2 |- R/ F; r    //得到当前对象的文档; m7 O6 @( ]" V& E
    pDocument = piLinkableObject->GetDocument();
; j" G4 r3 f5 F. X3 U1 E+ H' }; x    piLinkableObject->Release();# u3 _: x' e/ |5 I! u' n
    piLinkableObject = NULL ;
5 A5 u9 M: q9 f* K$ ?6 c    if ( NULL == pDocument)2 q' y# P0 ^  C3 I) b
    {  l- d6 X5 i2 Z% ^) Q
      return FALSE;
8 E* g3 k* S) e4 g, u    }+ ^5 K; [, P4 B0 Q* q
  }
0 h+ w2 o& q* {0 P& ^  w/ U4 b  //得到文档容器集
% R4 I1 Z& f# W  CATIContainerOfDocument * pIContainerOfDocument = NULL;
" J4 R6 F4 g9 i9 C  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);0 y9 W4 i# y1 S. m" i4 g
  if (FAILED(rc))+ N/ M' l9 c% J
  {
( f: \- C' h  M9 F! C    //pIContainerOfDocument->Release();) l( `, n$ V& a
    pIContainerOfDocument = NULL ;' t" D" z5 T6 u
    return FALSE;
6 a3 v8 q4 a4 E* G8 E  }
+ u# @6 x9 X2 Y  x  //获得Document3 D/ f( r& _* d# s
  CATIContainer* _pContainer = NULL;   v+ O4 O# b  t- J. r/ y* M
  //获得SpecContainer
0 G6 A2 p0 a3 h6 |/ h  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);
9 ~- x% a( K8 P, j) t9 R9 D
; K9 G; s" E+ I) h: i2 G* i- L  //GSM工厂
8 l' k. F/ j  x2 ~9 d  CATIGSMFactory_var spGSMFactory = NULL_var;
2 n( {+ J3 n& h9 z$ M  //设置工厂  
$ d. w1 F9 o, O, p$ X& ?  ?  spGSMFactory = _pContainer;         . S2 a( M& F; D4 L
  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);
- a0 W5 y$ |; U) X  CATISpecObject_var spSpecPoint2= spPoint;     
% M# U4 p9 b( e, d  CATIGSMProceduralView_var spSndPntObj = spSpecPoint2;8 ^% v5 ~3 [; w; }/ ?: V: E0 @0 K% A
  //*将点显示在屏幕上. @, T% D- ^, N1 a3 |
  spSndPntObj->InsertInProceduralView();/ y  W7 d5 S+ G# B
  spSpecPoint2->Update();8 h. x3 s7 O* u, A
  //生成线, P) Q% k0 P, u, S3 i
  CATIGSMLinePtPt_var spLine = spGSMFactory->CreateLine(spSpecPoint1, spSpecPoint2, NULL_var);
7 e! z( _+ y/ e4 b* K  CATISpecObject_var spSpecLine= spLine;     ; w3 q6 m5 o' ?9 U: a: I9 p
  CATIGSMProceduralView_var spSndPntObjLine = spSpecLine;+ a# a- U" [. E9 j2 {0 J* R4 E0 b
  //*将线显示在屏幕上1 W$ o6 B0 l, I) f* q
  spSndPntObjLine->InsertInProceduralView();0 s9 m% {! g/ s; i% b& T! r
  spSpecLine->Update();
  q, [" B# b+ `
) W, p6 w; `& _ return TRUE;
  P# r& w- g$ v/ L" i: b8 t0 b}: H4 O5 L( M8 q+ [9 l% t9 B: [- O6 X

( N1 t$ j/ |  t' ~# o+ _
. J) t$ D9 U" S$ @5 _( B
, y$ u% z5 G# u7 t2 N$ J' f6 k: K% c5 c: L% s: [; X, T1 `6 d# b3 \: _
$ q1 D  ~5 d+ T  j2 i
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了