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

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

admin 楼主

2017-5-6 14:18:38

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

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

x
CATIA二次开发入门教程---16 通过点击屏幕创建线
5 w  m2 G% J6 x  g0 P/ M0 {* Q( T
和上面例子类似,这里需要手动添加一些action事件,在头文件里面新增加一个 indication,效果如下0 I2 J! e' z, i( T% \( w5 i, ]$ b- T
create line.PNG
# M! a. N2 k6 D5 [% [1 E2 j
% j- r# ?$ g8 I8 Y& B* X7 l; H代码如下:
# V+ p( [4 {: K+ I
8 i/ Q3 N. _; z+ E
( M' L( I* `6 ^//-------------------------------------------------------------------------/ h8 [+ j1 a3 G% B2 j7 R7 R" E5 v- c
// Constructor0 N* P1 ?. B- O, l! N
//-------------------------------------------------------------------------' w- K  z; N! S' D! B) k+ Z8 O
CreateLineCmd::CreateLineCmd() :
& c7 M9 d7 s2 x  CATStateCommand ("CreateLineCmd", CATDlgEngOneShot, CATCommandModeExclusive) + w- G7 `/ B! U! W, u9 L
//  Valid states are CATDlgEngOneShot and CATDlgEngRepeat
, ~7 l7 m( ^$ S' d* n  ,_IndicationPoint1(NULL) ,_IndicationPoint2(NULL)
- ]8 D2 q- i$ Z' i{8 a, x9 G' t0 J+ S
}) m; N; g6 E$ Y" M+ o1 ^
//-------------------------------------------------------------------------, f4 j8 Y2 f2 A8 l8 |7 ?
// Destructor8 g9 z: u, q$ `
//-------------------------------------------------------------------------
* T# r8 ]9 m1 q& y$ cCreateLineCmd::~CreateLineCmd()& e$ K1 F2 `) ^" T6 [( k# i
{, H9 w6 |& K4 j5 k- z
   if (_IndicationPoint1!= NULL) ' k$ h( }2 R$ j5 W
      _IndicationPoint1->RequestDelayedDestruction();- t: v4 U# l& b4 U( y+ ~
   if (_IndicationPoint2!= NULL) ' |" n* a' o& w
    _IndicationPoint2->RequestDelayedDestruction();
% f- @! Q6 n4 I* x4 Z
' m) v* A* U% A- Z5 U}# ]! g1 H  Z6 u% {1 B& }- Z- W* @
; j1 s) F* ]& C0 h& ^
//-------------------------------------------------------------------------$ F  `; b% x0 i: }& b
// BuildGraph()
' i7 K) x; j! }2 o//-------------------------------------------------------------------------
) g% U8 ]! A7 J& m6 ~& a% wvoid CreateLineCmd::BuildGraph()$ y/ ]0 d5 m3 t1 U; r7 B) t$ d  Z
{
# ^% x4 q; [* [7 J2 Y6 @7 E! L* k& I: A" M. R/ o2 ~
  // TODO: Define the StateChart 7 ~+ ]9 N$ D, `
  // ---------------------------
! p% i* `* o0 a  _IndicationPoint1 = new CATIndicationAgent ("IndicationOne");
: _0 F! Y9 i: y' f  AddCSOClient(_IndicationPoint1);
; z/ \( v% e- t% {/ w5 P- O# J% l  _IndicationPoint2 = new CATIndicationAgent ("IndicationTwo");9 W+ r" V$ P1 Q* ]; }9 e  I+ H
  CATMathPlane PlaneXY;1 Z4 g9 j6 n: b: }
  _IndicationPoint1 -> SetMathPlane (PlaneXY); 0 b0 B" a3 G4 m* t5 S
  _IndicationPoint2 -> SetMathPlane (PlaneXY);
( S4 q: K# r( s% |8 w8 V  CATDialogState * initialState1 = GetInitialState("select first point");
! S( l2 c$ a& P0 E! O9 ?& ]  initialState1 -> AddDialogAgent (_IndicationPoint1);
) g: b5 l& N' Y% [( ^2 n# `4 s# _; {6 F) v2 p
  CATDialogState * initialState2 = AddDialogState("select second point"); // add important3 c, w; B* E8 q* ~; X
  initialState2 -> AddDialogAgent (_IndicationPoint2);
, S- g$ L- Z- G( p* j
" B* i. t5 M& [" X9 I: A% s  3 \2 w* C" u# Y) K7 m  ^
  AddTransition( initialState1, initialState2, $ Q# u; {' s- L. W2 W6 i
                 IsOutputSetCondition (_IndicationPoint1),
8 r- I$ |' [9 o: t! L" j$ {                 Action ((ActionMethod) &CreateLineCmd::ActionOne));
, \& B: s7 F/ f1 V- h3 i+ m& M! g3 C4 h5 X6 `8 d+ X' S
  AddTransition( initialState2, NULL, 2 z0 B* }) w5 N$ t/ K
   IsOutputSetCondition (_IndicationPoint2),/ k2 m8 d! }$ @% F7 S+ m; w
   Action ((ActionMethod) &CreateLineCmd::ActionTwo));
6 y" X# V7 z: _& v5 ^9 D}+ L2 @, n! D7 K) t$ I9 V

0 g" k( r" s4 `- s( z+ M+ t//-------------------------------------------------------------------------
" A. O) s: p" ]& V// ActionOne ()
7 Z$ k2 d6 r6 a6 d( K3 S' R" o* ^//-------------------------------------------------------------------------/ O9 G/ J: M  ^& F9 C3 W, v
CATBoolean CreateLineCmd::ActionOne( void *data )6 H! ]1 |+ g& E$ b
{
, p7 x# T6 B1 o) W  // TODO: Define the action associated with the transition
( t% h) C5 z! W  // ------------------------------------------------------
4 f* J5 u1 a1 d: M% _* Y7 P( H  CATMathPoint2D point2D = _IndicationPoint1->GetValue();//获得一个2D的点# F/ M6 j5 f9 P2 b& G% g7 B
  CATMathPoint Point3D;
  ~4 J1 \) p* O& W1 @  CATMathPlane Plane = _IndicationPoint1->GetMathPlane();
8 V2 u  f( p+ N1 W. y  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点4 x* \9 r3 ^7 ?  {# z- v0 |
  //设置Container(非根节点)
0 r3 \2 Y( Q8 _2 X* g  //获得Editor6 k* z* B. z" \+ @" I7 Z  n1 \4 a
  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();" F" E' s/ b& c/ w
  //得到当前对象的文档
. L* ~2 S& x) j% ?6 X" L. i" E  b+ B  CATDocument * pDocument = NULL ;
  _& J, Y2 k1 m/ g, x1 {4 N- f  //取得当前活动对象5 f/ l- `1 ~# ]% c$ l' d
  CATPathElement activePath = pEditor->GetUIActiveObject();( p% U& p9 Q6 ?0 M- h
  //取得当前活动的product
) x/ N# y7 ~# P) l  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());5 Z7 y# e9 T  U, O5 X$ W
  //当前活动对象不存在" _( S/ g% ~5 K4 g' s- W4 i# I% z5 G
  if (pActiveProduct == NULL): |+ p% ]5 g; d4 b1 [
  {, K4 m& u& P6 m1 X( [% u! M
    pDocument = pEditor->GetDocument();
# g- E" {# I2 Q) ~; M  }
8 _2 K$ d8 Q$ A0 n% O& G. k4 c  else, o# \9 u) ~4 z6 T' b# K( p
  {
: G1 M0 l( A+ C: p4 x    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();
! ?9 A, o; N; H" q! ?7 N    //当前对象的引用对象是否存在
. k, `: l( T5 r/ i. P6 t/ Q    if ( NULL_var == spRef )3 {8 u1 ~! M* j% W$ c, N
    {5 @, [" s' N+ x# M( h8 T
      return FALSE;# I  c. y! r- i1 J0 a! S6 w
    }
, A! r4 n/ x& F2 ^. ]5 b" y    //当前对象的链接对象$ y1 u+ y- _' W  H" [
    CATILinkableObject * piLinkableObject = NULL;
. X5 z+ n+ c$ |& C& K! ]    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                            9 s3 E2 c- o$ {6 _
    if ( FAILED(rc) )
+ M; p! }% O# z. r" l: ?- B+ U    {8 b' {, t6 H- y" |) S6 V
      piLinkableObject->Release();% R1 `6 {* `7 e# K
      piLinkableObject = NULL ;
" E1 [- c; {  v# \- s      return FALSE;
  M& @' X$ w  M/ E6 ^) T9 B9 q    }* E. j3 j9 C$ e
    //得到当前对象的文档
' S; y; M& J5 X; e5 k& S' j    pDocument = piLinkableObject->GetDocument();
) Z% y4 E6 n$ h; ~    piLinkableObject->Release();7 S* @' K8 |  f1 G3 F( g) y3 z
    piLinkableObject = NULL ;8 P# L1 P' y; A3 K/ M& E
    if ( NULL == pDocument)
& N  r1 Y: o8 W8 V  g  v% n5 c    {+ l, @3 Q( n: r  G, n& V
      return FALSE;0 H& D% V. W7 L$ G" t; @0 P$ y1 H( f
    }1 ^  f/ X% U% [& p& v
  }! i. y7 L" T: g
  //得到文档容器集
. l  T. y, z3 J/ V  CATIContainerOfDocument * pIContainerOfDocument = NULL;
5 A! }2 b$ Z, d0 z5 U4 B# C! }* u  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);
! T* g; Y# U8 s: R; U  if (FAILED(rc))4 R9 E0 P9 i8 W( |5 o& `3 b' l
  {
: K3 c+ R- B- h7 z' q% B    //pIContainerOfDocument->Release();
. U  [/ C: ]  J" k' \' I    pIContainerOfDocument = NULL ;7 o- C- ]6 B" u, X# u& l* `
    return FALSE;
+ F0 i5 K1 X- q7 w+ Z  }
6 Y! r0 W* ^, T& S8 M* C& Q5 I  //获得Document
7 u" k' X: Q% ~* B& i  CATIContainer* _pContainer = NULL;
$ c  J. X3 l( C- J  //获得SpecContainer2 ?/ s( L2 Z0 W
  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);$ E  T& e6 ?) l' m& q: [5 H8 _! b
  cout << "container ok" <<endl;6 C1 B% \: G) H) d' l; N+ F7 E5 r

9 I2 ]. g. D& G: e) Q  //GSM工厂+ p0 q( N- N6 a; y5 W
  CATIGSMFactory_var spGSMFactory = NULL_var;
2 r! G7 R! Q8 _8 X  //设置工厂  # d7 T) x7 k6 `$ F4 Q% R1 [
  spGSMFactory = _pContainer;         
4 d4 y) Q( x5 }& [0 o( j0 j! q  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);
4 T; U. W7 B7 y1 x( k  spSpecPoint1= spPoint;     , f" P7 x  _7 }9 y: |
  CATIGSMProceduralView_var spSndPntObj = spSpecPoint1;6 O! e9 C0 h. W5 C& p
  //*将点显示在屏幕上
4 x) R4 [" P. U. i' L( d  spSndPntObj->InsertInProceduralView();- E' S. H  R( }+ b9 l0 r  S
cout << "create point ok" <<endl;( @0 C, b4 v8 k, m
  spSpecPoint1->Update();
% U$ [1 V0 G( M# }cout << "update point ok" <<endl;
. v9 \5 m& f8 x  return TRUE;
) X$ J+ D% R5 v; i! T8 X; O; X6 o}
, l/ v5 v; r' w1 O3 U' {6 `' q8 V7 ^( s4 l! S' F/ j( p( z/ k
2 A0 p4 P" M8 W2 C& J5 F3 X) ^

; Z* `& ^, X3 MCATBoolean CreateLineCmd::ActionTwo( void *data )# c! B- M, n' o& g) Q  `
{
5 a8 F5 z# w* L5 G7 q; A$ o+ O // TODO: Define the action associated with the transition
! X. C, ]6 Y  L% W; n1 [# U // ------------------------------------------------------" L5 @& M8 D9 O' a. {" y
    CATMathPoint2D point2D = _IndicationPoint2->GetValue();//获得一个2D的点' c% V* R, ~% ]  D+ Q  o: ^3 }6 Z
  CATMathPoint Point3D;
: p! s% x9 B- l$ ?  CATMathPlane Plane = _IndicationPoint2->GetMathPlane();
4 f  |* X. s) \$ q# d# c# d  Plane.EvalPoint(point2D.GetX(),point2D.GetY(),Point3D);  //将2D点转换为3D点  f/ {9 ?9 e8 @9 `* B, Y+ ^
  //设置Container(非根节点)
4 v$ X) r+ v3 h) W2 \) U  //获得Editor  \' m$ ]6 d0 e2 h
  CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
! \" v/ C$ @$ J" ?4 v( m& q  //得到当前对象的文档
- w* j. I4 {/ Z  w7 a  CATDocument * pDocument = NULL ;/ ~+ ^: X5 U! Z& f6 f* Z
  //取得当前活动对象
0 e) B1 [+ u) ^0 u  CATPathElement activePath = pEditor->GetUIActiveObject();
0 Y) u+ Z& n1 T# B2 M  //取得当前活动的product, P- R6 q! v9 |9 [& w, u6 V9 N
  CATIProduct *pActiveProduct = (CATIProduct *)activePath.SearchObject(CATIProduct::ClassName());4 S+ i7 C4 Y: u: l. `8 T
  //当前活动对象不存在% f3 I  m. b$ z  ]' ]
  if (pActiveProduct == NULL)
7 O; p1 ~6 p' @2 M) V6 M6 S  {, H* H2 g2 P  w% F# q% h
    pDocument = pEditor->GetDocument();
/ H7 ^: m7 @0 R  e: z  }
. K  {# |5 T* q  q  else
* S4 C. u" a: o- O  {) s+ g  J& |; R4 X1 \% S. S
    CATIProduct_var spRef = pActiveProduct->GetReferenceProduct();5 ~0 H( i4 w5 z  B0 W- e
    //当前对象的引用对象是否存在% v/ |5 Q/ g" @" E5 k/ H
    if ( NULL_var == spRef )
6 W1 I  k  s5 f) C! C3 I' S    {6 J, ~! N. N5 z0 y
      return FALSE;
5 p5 ~# J$ ?9 G  S+ x6 _    }0 ~1 [. i* q8 h
    //当前对象的链接对象
6 S" n: W" V4 P  y5 B! @    CATILinkableObject * piLinkableObject = NULL;
! P$ ?/ g& U5 G8 J3 p    HRESULT rc = spRef->QueryInterface( IID_CATILinkableObject, (void**)& piLinkableObject );                           
1 s$ P# J" V/ U3 W    if ( FAILED(rc) )8 A! ?3 T4 C) e
    {
0 N' ^3 d! j* P% j6 l      piLinkableObject->Release();
% e8 {, y* x- t+ }$ E8 a      piLinkableObject = NULL ;" y. w. Z, @9 M
      return FALSE;
5 k; P5 {# a" J. \    }
% g& e( G2 t# I3 F; z    //得到当前对象的文档
& i* `8 D$ `; B  h, h3 M7 [' N. L% T    pDocument = piLinkableObject->GetDocument();
8 @# {" Z4 n- p( Q6 H    piLinkableObject->Release();
: }  _' P  j. W6 k    piLinkableObject = NULL ;4 O. W' G1 F5 A9 S
    if ( NULL == pDocument)
2 e* I: V% Z, Z4 E    {
  i$ L, W/ N9 K# l# |- B      return FALSE;* z- [& X/ P9 ]+ a- O& b
    }" ]4 y& c; d+ J2 J: b
  }
2 f& Q) G# n  k7 M% [  //得到文档容器集6 c( C4 k* g7 i! e5 ~
  CATIContainerOfDocument * pIContainerOfDocument = NULL;8 n. G: I1 z( E7 G6 a( J
  HRESULT rc = pDocument->QueryInterface(IID_CATIContainerOfDocument, (void**)&pIContainerOfDocument);8 ^! d2 U' X1 w- o( w9 u4 O0 n
  if (FAILED(rc))$ h1 W% W4 q1 V. {1 c
  {7 f6 v$ E; k( ?5 A, e. B& @+ R7 t0 p
    //pIContainerOfDocument->Release();
' h  `% W; W$ ^5 z    pIContainerOfDocument = NULL ;
& ?( c2 E8 X/ ]. i% {    return FALSE;
0 v+ ?, `, E2 g  }
& z0 H7 m9 g) B  //获得Document7 l8 \$ S. K& O3 y
  CATIContainer* _pContainer = NULL;
0 m1 ~# y: Q7 F+ d  //获得SpecContainer
/ l- V$ V9 q- ~, B  HRESULT hr = pIContainerOfDocument->GetSpecContainer(_pContainer);3 z" r' Y9 ^. G7 V: A" X
( _8 T; I, q+ ^6 o, e  S8 s0 X9 J
  //GSM工厂( g8 l) F" Y& ~: H8 t0 k3 M* I. F
  CATIGSMFactory_var spGSMFactory = NULL_var;
5 Q2 g( |, C4 [* F! ~. u1 I: u  //设置工厂  & s! u$ K9 y' B, U
  spGSMFactory = _pContainer;         4 N% E; C6 n; d0 K
  CATIGSMPoint_var spPoint = spGSMFactory->CreatePoint(Point3D);9 f0 _" O. y3 B; g/ m6 U# ?2 s
  CATISpecObject_var spSpecPoint2= spPoint;     
; P6 x! I7 a% N8 g! \! z  CATIGSMProceduralView_var spSndPntObj = spSpecPoint2;$ @8 f' }, r: Z9 X4 L8 ^' `
  //*将点显示在屏幕上
# S: q4 a& B2 a: r8 ]9 T: a" u% t  spSndPntObj->InsertInProceduralView();
$ A9 f+ ?. c  {( F( s  spSpecPoint2->Update();
/ T: n# }1 F9 P: _0 n/ ]  //生成线& [! @# `& k2 n" f) E% h* f% ~( J
  CATIGSMLinePtPt_var spLine = spGSMFactory->CreateLine(spSpecPoint1, spSpecPoint2, NULL_var);6 R! c4 L/ ?7 D; s
  CATISpecObject_var spSpecLine= spLine;     
  K" h9 L( V- H4 V- W+ W1 r  CATIGSMProceduralView_var spSndPntObjLine = spSpecLine;
( S7 U5 F5 L; L4 X3 r& _$ z  //*将线显示在屏幕上
/ n2 R" O5 q: e& q6 ], w  spSndPntObjLine->InsertInProceduralView();
5 }/ `) r% D$ I  spSpecLine->Update();
4 e% q& c. L$ D6 A! Q" P4 t% ^, a0 t7 p# {4 a$ ^' Q$ C
return TRUE;
6 _& s) r; |4 J7 W/ E}
1 e) A9 @! f! j8 \/ l( Z) h* }+ @- _6 w0 ?+ g! N2 p2 _
8 ?- j! B/ ~7 [2 n' f

3 Y" r4 Z! @! r6 X! Z! `/ [" n& X
4 N; }3 {( S! f% z, A3 w
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了