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

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

admin 楼主

2017-5-6 14:18:38

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

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

x
CATIA二次开发入门教程---16 通过点击屏幕创建线
! Q0 j& w! W- V' w0 G0 J- e, a/ H& V+ M) y
和上面例子类似,这里需要手动添加一些action事件,在头文件里面新增加一个 indication,效果如下# n3 ?! E! K8 y3 j
create line.PNG & ?; o) q; b2 y' i* {
+ s- [; @; k0 k, l% S2 T5 ?7 j) y9 c
代码如下:
2 z! k1 ]7 M: I1 G0 I$ ~
2 C8 t5 d# q/ u5 }. \, j4 S# ]7 A; y5 J+ G
//-------------------------------------------------------------------------
  ~$ X& P1 a4 a// Constructor' U6 j/ h" w/ m& S( z7 G, B
//-------------------------------------------------------------------------
& h# L$ p7 _5 K, x, h! GCreateLineCmd::CreateLineCmd() :
9 h- O/ A4 s+ p& W3 ~  CATStateCommand ("CreateLineCmd", CATDlgEngOneShot, CATCommandModeExclusive) ( N7 w- S5 V1 J( ^8 f
//  Valid states are CATDlgEngOneShot and CATDlgEngRepeat/ X2 f3 \$ j. Q& P6 K
  ,_IndicationPoint1(NULL) ,_IndicationPoint2(NULL)
1 Y+ r3 d& B7 T  x{
9 u+ `& e% R6 |/ U4 V8 m}
$ `  q3 a$ Z. d4 o9 _/ D//-------------------------------------------------------------------------
7 c1 M! m) t4 E// Destructor
* H9 P# r7 K- O* A//-------------------------------------------------------------------------
( A3 S0 o, [: J7 }0 yCreateLineCmd::~CreateLineCmd()9 q: S$ g% Z4 s' j* r( k- ]+ E, F
{$ N3 d* Y$ k0 l( `7 o
   if (_IndicationPoint1!= NULL)
3 @5 m$ K! ~: p' u      _IndicationPoint1->RequestDelayedDestruction();1 o( _2 {) f3 _% m9 o) ]- n; ~
   if (_IndicationPoint2!= NULL) , \* S7 J; Y. w) ~) C6 _  F* I
    _IndicationPoint2->RequestDelayedDestruction();, A" G1 K' p% _) V, `+ r, h1 O
1 {0 B8 s2 D" ]) ~( r5 A$ X1 e
}. W( Q$ s2 r; \2 d9 L, m
, s/ S: h3 `1 U  N# r( {. g7 }# S
//-------------------------------------------------------------------------" j6 W" R% s) L2 X
// BuildGraph()
& `) B8 p1 d! G; H& [8 B; c//-------------------------------------------------------------------------
, D& W3 z/ R- U3 I  }5 Pvoid CreateLineCmd::BuildGraph()
) ]- a6 z6 L) k{
5 D" Y: k3 O0 t4 g- a2 \4 q- k3 }1 k5 Q# N
  // TODO: Define the StateChart
' L' J5 F8 W9 O7 U+ X/ N+ r) Q4 ~! R  U  // ---------------------------
% J2 ]! W/ K' s6 B& s/ G  _IndicationPoint1 = new CATIndicationAgent ("IndicationOne");
: d5 c. B' t4 s- Q  AddCSOClient(_IndicationPoint1);
' x0 N3 A# y  m4 b! f: k; Z3 d  _IndicationPoint2 = new CATIndicationAgent ("IndicationTwo");
$ n3 d. W0 z, ~- D4 I  CATMathPlane PlaneXY;
2 I) P# v4 W" v  _IndicationPoint1 -> SetMathPlane (PlaneXY);
' Z, b' l$ X0 i& }- x. z% @' l  _IndicationPoint2 -> SetMathPlane (PlaneXY);  C! w5 S& V5 d9 ~( ?1 G9 ?
  CATDialogState * initialState1 = GetInitialState("select first point");' k9 f- q' P8 r/ U
  initialState1 -> AddDialogAgent (_IndicationPoint1);
, S+ @, V; x$ {& W( L
" D; g! Q) Z+ D* g+ B  CATDialogState * initialState2 = AddDialogState("select second point"); // add important
' T" a# H4 j! ]( R3 ]- o# k" J  initialState2 -> AddDialogAgent (_IndicationPoint2);/ v; y! e3 [, X; ?9 j

# @" m- {$ V* p+ v  
2 W' x6 `$ _! Z# m3 U# |  AddTransition( initialState1, initialState2,
# V  h" |7 Q8 p0 {0 ?4 W# z* w" \                 IsOutputSetCondition (_IndicationPoint1),
$ J) @/ g1 y# ]/ c" b                 Action ((ActionMethod) &CreateLineCmd::ActionOne));) t+ Y9 I- G/ s% J

; }8 J' i$ e! F) Y' K# [  AddTransition( initialState2, NULL, ' l) ?) C7 h3 e: Q1 U$ k9 t( @
   IsOutputSetCondition (_IndicationPoint2)," A9 T' ]$ ~: {" u  b* [0 ^
   Action ((ActionMethod) &CreateLineCmd::ActionTwo));
; V6 l) y. z' l/ n  g5 W0 m1 b% U3 ]}
- `, v) o% ]7 Q& C1 ]+ A2 A- s0 @+ _; M% _4 {! U5 O  Z
//-------------------------------------------------------------------------' P, F; x$ ?# R- U! s5 B
// ActionOne ()
3 q2 Y1 a( W" w//-------------------------------------------------------------------------
: t4 i: Q# S% \2 q- S$ q& jCATBoolean CreateLineCmd::ActionOne( void *data )
' y6 M! q/ Y' o$ J' i- ]{
' D+ e: x- {1 o6 H! N+ @  // TODO: Define the action associated with the transition
  S2 r, }( L0 ~% C0 N  // ------------------------------------------------------
7 z3 S6 G, V% ^  U  CATMathPoint2D point2D = _IndicationPoint1->GetValue();//获得一个2D的点
" l$ o0 W; \0 n1 p2 t  CATMathPoint Point3D;7 ]. ?0 L9 l; p' S! [. z$ p- F! G
  CATMathPlane Plane = _IndicationPoint1->GetMathPlane();, s5 [0 g  X$ N" U- }
  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点
5 I0 l& b; u- M* A7 R) I" C# g# E  //设置Container(非根节点)
6 K5 J5 M0 a3 H- h. Q# ]) q1 \8 J  //获得Editor
1 h9 Z4 U8 _: {$ N" T  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();% T# T3 L% Y6 G  t. [2 l: B  w6 n
  //得到当前对象的文档4 y, p! y9 j  T) Q
  CATDocument * pDocument = NULL ;+ d8 H! |( I9 z' ?& i- J. G% K- o
  //取得当前活动对象
+ v* [, @  p/ w" C" E, I  CATPathElement activePath = pEditor->GetUIActiveObject();
9 k1 h& q! }! W$ V% ]  //取得当前活动的product' s9 }* N. t) S2 N5 [
  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());
% b# l6 `2 V; e$ `" p1 G  //当前活动对象不存在9 R7 h' _' I0 T+ D, o
  if (pActiveProduct == NULL)( r. q8 r9 B# X1 |% A
  {  n* f' O/ B) p5 y
    pDocument = pEditor->GetDocument();
, P- l" {0 Q6 e; p) Q* n  }5 c5 l: ?+ T& b
  else
7 u, I" u- h. l" @% W/ r  {7 q% h' J3 S5 [, S/ n- \3 B' t
    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();
' T. y6 [- x* C. v    //当前对象的引用对象是否存在
6 v, |7 v  N  x6 A    if ( NULL_var == spRef ); H) a2 ]2 ]- Z; g. I
    {- K* ^' t  A0 N
      return FALSE;  e2 R' B2 q5 }8 @6 I5 p4 H# R
    }0 E! k1 K- {1 G  y# F! f3 F' |
    //当前对象的链接对象  e/ e) S3 |3 f; [+ V) N' g3 A
    CATILinkableObject * piLinkableObject = NULL;
' ~4 a$ t1 b  p' \    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                           
4 K6 T% e7 `$ [+ o4 u" T( B    if ( FAILED(rc) )9 R6 [, l$ v% S7 X
    {( s6 J! h0 E6 U) b
      piLinkableObject->Release();
$ K% P/ G: Z2 B2 f/ d0 f      piLinkableObject = NULL ;8 _/ K6 ]7 i8 d0 n* e
      return FALSE;
$ A7 o, T! o& V2 s    }
9 u. e7 ?( f" z0 F8 w4 n    //得到当前对象的文档
% Z6 }0 t5 v/ R  l- W    pDocument = piLinkableObject->GetDocument();0 F8 G3 g7 d/ Q1 z; C$ k1 X
    piLinkableObject->Release();
! K2 M, A$ ~) z    piLinkableObject = NULL ;
0 V5 X2 G' w2 v2 T% \    if ( NULL == pDocument)- J8 I# Q$ Z  t  D
    {
4 S( a; B% c% y; f- n' {& r      return FALSE;( D% P' y* k+ R; a6 i& U
    }
1 X& N. T8 I$ X1 x7 `0 ]3 ]1 Q% ~1 W  }3 I1 b% ?# _3 w, G8 `4 C4 J
  //得到文档容器集& ^; ?1 a4 I6 O
  CATIContainerOfDocument * pIContainerOfDocument = NULL;
* a: w' |) C5 Z8 P- a1 p" z( [4 y  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);
6 g; A! f+ q% [8 n, x6 B! [  if (FAILED(rc))
. |. d! h% U' {' G0 G$ [$ y  {- E7 l, J9 E& \. N
    //pIContainerOfDocument->Release();( w1 v" ^9 V! q7 ?, k( N
    pIContainerOfDocument = NULL ;
  U3 I5 e: a1 C+ E    return FALSE;. i- v: v' u% v
  }
0 r: M0 q) Q2 d( Q  //获得Document+ E& K5 o. q* d) G* q4 z* R. j/ u
  CATIContainer* _pContainer = NULL;
, V9 R, x. h4 B' |5 t  //获得SpecContainer
* f( F+ P  X2 x( t+ e, r# G$ L, q  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);
0 |2 m: U- ^7 V' \& [" @  cout << "container ok" <<endl;
, H4 }+ {; S) l( {
  F1 T6 c% N, m& g  //GSM工厂' b" s. f; ^/ ?( |0 `! ]8 ^$ Y3 R
  CATIGSMFactory_var spGSMFactory = NULL_var;
% P4 i, y* K0 D9 L5 k- w7 L  //设置工厂  & L/ R: n3 B5 t
  spGSMFactory = _pContainer;         9 w5 L/ b3 K/ z( ?$ \: G
  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);+ h) o( @& b( V. l; x4 z: V; o# e- Y
  spSpecPoint1= spPoint;     0 y5 q% N7 C2 D  f" P
  CATIGSMProceduralView_var spSndPntObj = spSpecPoint1;* Q8 E' j( `8 l+ B3 A
  //*将点显示在屏幕上" M7 Y% f' X* D# l& w
  spSndPntObj->InsertInProceduralView();
4 n9 e% B# L, G cout << "create point ok" <<endl;% f/ l6 z$ P' S2 \& G1 z
  spSpecPoint1->Update();# P" D2 I/ R; M5 l5 K, x9 B: s
cout << "update point ok" <<endl;
, P# x8 A$ W7 |$ y  ^  return TRUE;  s; ~4 B: b$ Q# E" ?9 G
}
% u) }3 X1 x% z8 Z: @, j  |/ J! d  c4 E5 `
  u8 m, W3 q$ B* F4 Q
+ L! s, ?1 v5 n/ A
CATBoolean CreateLineCmd::ActionTwo( void *data )
9 q/ m9 p; F9 q" W{
" u1 U, h. s; G- m& ] // TODO: Define the action associated with the transition + E; J3 p( a1 b% C* S
// ------------------------------------------------------
* k' O7 s  Z7 \  T. g4 Y    CATMathPoint2D point2D = _IndicationPoint2->GetValue();//获得一个2D的点
  {! J0 _) g6 `8 B, z& ^( W& ?' E  CATMathPoint Point3D;8 f0 C( g9 J/ m! r) E
  CATMathPlane Plane = _IndicationPoint2->GetMathPlane();4 G" h2 H0 L/ ^; N8 k- Q
  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点
% O+ q% o7 Y/ R2 c  //设置Container(非根节点)
" T* t$ X# ]3 E8 L% X  //获得Editor
" R1 m& \3 O# [; C8 o7 a  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
# a+ z8 y/ p( w, E# R  //得到当前对象的文档1 |+ t) J5 a  n, @( l3 Z7 F
  CATDocument * pDocument = NULL ;5 d& _  [  D7 }; S4 Y7 n7 _
  //取得当前活动对象$ I6 V3 s- z: ?2 v
  CATPathElement activePath = pEditor->GetUIActiveObject();
. A4 d5 |5 ]. A2 ]1 q4 b  //取得当前活动的product: C. d  J# ]- v. o6 _5 N9 N* j
  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());1 q5 X+ g6 f) u. [: B+ |# F
  //当前活动对象不存在. t/ i2 }1 {- |# M' ?0 y9 A3 d; \# {
  if (pActiveProduct == NULL)5 K3 y1 Q8 E# r+ t
  {$ ~7 I1 |! h2 u' K8 T
    pDocument = pEditor->GetDocument();
5 `, y- V% |5 F' W  }
& _2 A  H$ O. h8 b! i/ Y7 l) [. K  else
, g- ?0 W' H% N5 o4 T. b, f  {/ U! H# T! y0 E" H1 P: t
    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();
6 m4 R# {2 @4 b0 e2 }$ U+ B9 O) F    //当前对象的引用对象是否存在+ {+ _- C( W8 h4 c/ D
    if ( NULL_var == spRef )8 @/ \, w6 Z5 h! E- \: M% Z
    {: K' v) U3 A8 e  l$ w
      return FALSE;
& p8 S* Q3 x1 I8 T    }, n/ X4 @5 a9 e$ a! e
    //当前对象的链接对象+ l* [  d. T. k8 N' x3 z4 ]
    CATILinkableObject * piLinkableObject = NULL;
* P8 _% j# N" s    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                            3 i( A$ F0 Q, z6 r/ b
    if ( FAILED(rc) )
& Z( ^" ~; b. U; g* G& N3 m    {+ R1 M# Q; H" _8 I
      piLinkableObject->Release();% g: k( o( {. E  h  W% ~
      piLinkableObject = NULL ;
8 G6 U$ o9 z  l4 L! R0 z4 r8 B& W& {      return FALSE;$ D, G! ^7 V9 x+ M  Z8 k
    }' H7 @: R1 ^* d3 x
    //得到当前对象的文档
0 I1 C. Y0 y. R1 d4 s+ j% Z    pDocument = piLinkableObject->GetDocument();
/ [% I8 ^; p0 X$ R6 B$ M    piLinkableObject->Release();8 K! U" [9 r( W7 M% z3 b( F
    piLinkableObject = NULL ;
. [8 j9 z; X6 g; k/ A- B% u    if ( NULL == pDocument)
0 G/ n7 `8 o8 m1 p# m* z- e    {
& E; h9 [$ ~5 |$ L" J/ K& a$ t      return FALSE;5 H! P% d1 ~, |: F5 e' w
    }# V& v" C) g5 o( @1 X
  }
( j- W8 W! i8 L! d, \  //得到文档容器集
1 h3 K1 }! ^* \: d$ U9 Q  CATIContainerOfDocument * pIContainerOfDocument = NULL;
1 @1 A% O1 d; g+ ]; z  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);
$ i; r' g4 h, T  o+ f, A  if (FAILED(rc))
$ H& D! V  ?) B/ |; m2 P  {
) ~  D# u) ~$ p% I. q; U$ r    //pIContainerOfDocument->Release();  r  i( f4 b/ |0 v; j( U# c1 \
    pIContainerOfDocument = NULL ;
2 w7 _4 ~, V# s& B& t( q& p' q) k    return FALSE;  C  [* m3 x& q( O( n, h4 q
  }- t$ R/ A: g& r' O2 |* \' _. [
  //获得Document0 r! Z1 p/ P0 V) V0 R$ H# p
  CATIContainer* _pContainer = NULL; % r5 L8 r' L0 {6 J4 ?! I, t- H. x
  //获得SpecContainer* Q) Z- _# M  P3 v
  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);
- K& h9 h' |- H! [( m5 Q4 y8 O! a( ~
4 X/ F4 l" W( e* I( x  //GSM工厂
5 b0 p) H; g# h! {  CATIGSMFactory_var spGSMFactory = NULL_var;) n3 ^: L0 U; H, B
  //设置工厂  6 d! e& z- O& a: m0 y
  spGSMFactory = _pContainer;         
  k! c. \7 v4 k( H  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);- t& k/ g0 Z' ]. v1 M$ X5 Q" B
  CATISpecObject_var spSpecPoint2= spPoint;     , p. \$ ?% D* `
  CATIGSMProceduralView_var spSndPntObj = spSpecPoint2;" s2 e# J* K. g% z$ p- s& y
  //*将点显示在屏幕上
$ A9 M: L# n9 d9 g4 p% z0 F  spSndPntObj->InsertInProceduralView();
. V1 @" O- Q" x& n4 z  spSpecPoint2->Update();' K0 T  b# A0 G( {4 m& \- p- f
  //生成线
' k. d6 T# F% I6 D  CATIGSMLinePtPt_var spLine = spGSMFactory->CreateLine(spSpecPoint1, spSpecPoint2, NULL_var);
9 g9 o2 b; o8 P! E- }  CATISpecObject_var spSpecLine= spLine;     : g% Z3 h) Q5 |- T, C& d' C2 f$ K0 ~
  CATIGSMProceduralView_var spSndPntObjLine = spSpecLine;8 n6 b, Y, c2 ~. J, _# g( X
  //*将线显示在屏幕上0 U8 v( P5 I% _; \. f- y2 b! L9 \
  spSndPntObjLine->InsertInProceduralView();
6 ?. C4 N1 G. P( a& p! c& W! c  spSpecLine->Update();
0 R# W4 H# E+ t1 H7 F  f. _" h  `: q
return TRUE;
: }' r4 `2 F- l& @) X}5 L6 V, u- ]5 D1 |* z
0 K) W1 q4 v* O. Q$ t
3 T6 Y+ [6 `8 U( V& k# e/ O
" x% n  _( M8 v' o* Y

/ {, D  v  ^! X; E7 H* h. e" J- x" C7 e
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了