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

[二次开发源码] NX二次开发源码: 获取面的相邻面的信息

[复制链接]

2013-11-6 18:54:58 6382 0

admin 发表于 2013-11-6 18:54:58 |阅读模式

admin 楼主

2013-11-6 18:54:58

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

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

x
通过这个代码,可以获取选择面的相邻面的信息! w( B0 Y! C/ c! n
NX open C++中使用了 选择对象 selectobject 函数,通过面获取边数据,通过边获取面,从而得到信息!: S  K. m0 A+ ^
; w6 R; z0 t9 V
  1. #include <uf.h>
    , P1 r7 A7 {$ K
  2. #include <uf_ui.h>4 q4 `4 ]4 W! \% `, z$ f) E7 S
  3. #include <uf_object_types.h>
      b( M) M+ ]4 W

  4. / L6 a& C( N  o  |4 m
  5. #include <NXOpen/Edge.hxx>+ w& \5 }# u& S% t( ^( ~0 `8 _
  6. #include <NXOpen/Face.hxx>$ M+ f% Y6 o& i: e: M# t& l% q

  7. 4 U- ]4 B4 A" l) s8 L- \
  8. #include <NXOpen/ListingWindow.hxx>: |" w+ _2 T6 Z
  9. #include <NXOpen/NXException.hxx>/ t0 E" v4 G9 B# d: A
  10. #include <NXOpen/NXMessageBox.hxx>( ^8 X  e! s" C2 N' q; l* P
  11. #include <NXOpen/NXObject.hxx>
    ( p- X4 l) p1 w9 [$ d, I
  12. #include <NXOpen/Part.hxx>
    4 U2 V6 ?, g4 |9 u' Q
  13. #include <NXOpen/ParTCollection.hxx>
    9 [! k2 n) [9 _2 p% u
  14. #include <NXOpen/SelectDisplayableObject.hxx>% |- g' p$ U* m7 [6 {* ?
  15. #include <NXOpen/SelectDisplayableObjectList.hxx>
    ' c' U& Y7 \; j2 p  r: c& v
  16. #include <NXOpen/SelectObject.hxx>- \  ~' m/ h+ \" ~0 s5 ]1 c
  17. #include <NXOpen/SelectObjectList.hxx>
    1 l3 N0 F3 g$ e3 W) r! F6 g% l
  18. #include <NXOpen/Selection.hxx>( }" b) L% _+ s* D
  19. #include <NXOpen/Session.hxx>
    " C  H1 ~7 g/ i- v3 V
  20. #include <NXOpen/UI.hxx>
    2 {1 C8 W& a. S/ B# u. ~
  21. #include <NXOpen/View.hxx>% ^5 Z9 X6 c# n6 W; l# `
  22. ) T# G; `. f8 O& p$ F9 w% ?
  23. #include <iostream>$ ?# ~1 u$ w, k6 U
  24. #include <sstream>/ F9 e, S; Q0 {2 h
  25. #include <list>
    : t; F. M0 z: w9 N. J
  26. , V  }4 c% p# d  c* s) r8 g9 y7 B
  27. using namespace NXOpen;2 v* M: ^# Z. t+ S+ g5 A3 A9 p
  28. using namespace NXOpen::Annotations;# A& g7 m7 k4 Y  Q3 z! F0 F
  29. using namespace std;
    % v# K4 `* t0 \0 }/ G! p. J
  30. 4 d) w  i8 x# U* b( v
  31. class Gtac
    6 \1 g& f6 V8 K2 `" J* x. j; `
  32. {/ @/ C6 C6 ~$ v  @$ O* G
  33.     // class members
    : Z- F; P" a( z; _+ X
  34. public:( q( z% J- b2 {: ~4 @/ _
  35.     static Session *theSession;+ l+ b) I: ^  ~0 R, Z0 b
  36.     static UI *theUI;5 K/ R) L- Q6 i# o& P( y" U

  37. 1 c2 b. C+ e  H+ q, A% ]
  38.     Gtac();
    4 A0 B) P# e, H- e" ]" o
  39.     ~Gtac();
    + Y% F( k3 }7 L8 F- G# E6 Y
  40. - e1 p0 a! S% }2 [" z  t
  41.     void do_it();
    7 l8 t& v- ]. }, b

  42. ! I3 O* B! s9 H) P: V1 g' f
  43. private:8 F+ s: }1 r8 h
  44.     Part *workPart, *displayPart;
    : ^# j1 M, |" l
  45.     Selection *sm;, A6 s1 Q7 `9 u$ L. Y7 L) \
  46.     NXMessageBox *mb;
    ; @: ]- K0 R* `- h+ ?3 s
  47.     ListingWindow *lw;6 W# T, n! |1 C  c- F" ^
  48. 9 D2 Q5 U9 d& B6 M1 I5 v
  49.     Face* select_a_face();7 ?$ n4 }9 f. m1 t

  50. * k" R. W0 R, r  A3 e5 l- V# m
  51. };' `; b4 C; Y9 E/ Z
  52. - _# f3 x: ?" _; Q* N  \
  53. //------------------------------------------------------------------------------
    ( M. ?1 Q3 O( G! u+ M
  54. // Initialize static variables$ J: X; X3 D  l& k
  55. //------------------------------------------------------------------------------
    8 o- J+ m3 X% o
  56. Session *(Gtac::theSession) = NULL;: {( g/ S) ?  L+ ?; A. b
  57. UI *(Gtac::theUI) = NULL;
    9 z5 k8 o0 M- ?( k. V+ |

  58. 7 n8 g( H2 @6 {
  59. //------------------------------------------------------------------------------, S# R: H4 J7 i5 d8 }
  60. // Declaration of global variables
      f  E# t/ P6 P3 c2 L; p
  61. //------------------------------------------------------------------------------
    & O. m5 Z4 U8 W) S
  62. Gtac *theGtac;
    $ \- I' ?. ^4 X/ z
  63. . Z7 G7 r% A+ B
  64. //------------------------------------------------------------------------------
    9 Y( F9 Z& u- Z; q9 \3 P5 [
  65. // Constructor) E$ \! j$ N. C/ d7 c
  66. //------------------------------------------------------------------------------
    ; m* [! k- _8 m  @. \0 p4 B6 r
  67. Gtac::Gtac()- i8 X0 D' r5 S# ^
  68. {
    + j$ W) ~3 b& Y
  69.     // Initialize the NX Open C++ API environment
    & ?8 p, q" _/ y# N8 k  N% u
  70.     Gtac::theSession = NXOpen::Session::GetSession();8 ?* g) U* o  a* h5 N4 j: r7 I
  71.     Gtac::theUI = UI::GetUI();
    + p& O; s  F& g0 o
  72. : R+ r8 r( K8 L* P' N( H4 ]& W: M
  73.     sm = theUI->SelectionManager();' Y! I3 n# v. V
  74.     mb = theUI->NXMessageBox();% [& k7 U: E. V! f: c% f. E
  75.     lw = theSession->ListingWindow();! M! ]! U  a; J: S5 q1 G5 C& i
  76. % [% T. h+ c" H  C& M( ^/ u
  77.     workPart = 0;
    ! G& A, o* |8 ?
  78.     displayPart = 0;
    ' [$ T3 [8 K1 c9 p0 G
  79. }6 |) k: i0 Q( l( ~/ p6 y3 c; W

  80. 5 |& t0 F" o! m! c( w4 y4 I% B
  81. //------------------------------------------------------------------------------" z( D+ G( U2 h& P! g
  82. // Destructor) |( y) s; n/ M3 `' l; d1 p4 n
  83. //------------------------------------------------------------------------------
    ! U' U. J$ ^9 k
  84. Gtac::~Gtac()" v1 n8 b) p) r
  85. {
    6 O" U* L' u  w7 G
  86. }" w7 G. o# ~+ p2 _* P

  87. & {3 ?& q4 R8 O# h( x2 O/ Y
  88. //------------------------------------------------------------------------------
    + F- ~# o8 |( o" M" i
  89. // Selection# V8 x3 w% F4 U
  90. //------------------------------------------------------------------------------
    ! k0 `4 M6 l, K
  91. Face* Gtac::select_a_face()  c) P$ V8 |# o5 K
  92. {: E, u# \. J* Q  q" X9 A
  93.     // ask user to select a face
    2 W# m5 t5 n: q/ V( G
  94.     UI *ui = UI::GetUI();
    : G& `& g$ b( R: t8 N: l" b9 K
  95.     Selection *sm = ui->SelectionManager();
    3 m) _2 Z8 M' \* r/ d
  96.     NXMessageBox *mb = ui->NXMessageBox(); // as of NX5; H* A8 k1 g; `2 q9 S& @

  97. & a# T5 v0 p6 f, @8 v
  98.     NXString message("Select Face:");4 v# [: ~! t3 H3 x
  99.     NXString title("Select Face");
    % `2 Q+ _- |; L8 ]
  100.     Selection::SelectionScope scope = Selection::SelectionScopeUseDefault;
    + a) |2 M5 m! x0 j
  101.     Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;- c. S; M0 H  u
  102.     bool include_features = 0;5 C3 o4 E- v% `. S, U2 U5 c  a/ {3 h
  103.     bool keep_highlighted = 0;4 M6 K* J( o( W3 G/ {
  104. 7 Z6 y8 M% F/ @6 \  l% v4 w3 N5 {. ^
  105.     // Define the mask triple(s)
    ' U' }, B/ G- `. U
  106.     std::vector<Selection::MaskTriple> mask(1);
    / u' m3 I8 \: K9 x
  107.     mask[0] = Selection::MaskTriple( UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE  );
    ) X$ u! G! R5 E9 {& }! `( r! L
  108.     Point3d cursor;7 P& L8 p7 x5 l  ?0 o" A
  109.     NXObject *object;" `$ Q' B  u9 D( S( n6 ~
  110. 8 P' g( U4 W9 g# `$ i6 S
  111.     // Select objects using filter defined by mask triples
    ' ~7 Y+ c; A# j1 ]6 F" Z* x
  112.     Selection::Response res = sm->SelectObject(
    : F& p8 S0 Y6 D' J
  113.         message, title, scope, action, include_features,6 g2 c2 s$ Z+ o) \/ G/ \
  114.             keep_highlighted, mask, &object, &cursor );) e6 r% y& v% X# i6 k6 S/ W
  115. . @. Y! O1 v: D! H( H$ a" C
  116.     if( res == Selection::ResponseObjectSelected )( i, o+ l3 r. Z7 s+ P$ C
  117.     {
    % o8 u- ~9 _- Y, p
  118.         Face *aFace;' j( i  t, U; ~0 h4 w. S; Y5 T
  119.         aFace = (Face *)(object);8 L0 w/ W- Y. r6 S0 S
  120.         return aFace;
    ; W" h" N. C# q' C/ s% f
  121.     }
    $ f, J" G/ t$ s
  122. " l- N: |) k/ `3 B9 R+ }# E+ w
  123.     return 0;; t5 P" @1 V2 r, Q1 @& r
  124. }
    . X& P8 c" D* `6 v& u" J. J
  125. 6 G& \8 h/ U) B" o$ f6 e0 ]" i( y, g
  126. //------------------------------------------------------------------------------
    9 {6 v1 L' S. w& \0 e6 c
  127. // start with our job...* [. x  C: u) `% f9 k6 B/ b
  128. //------------------------------------------------------------------------------" G8 b! I$ H2 ~* o2 Z
  129. void Gtac::do_it()
    9 V# ]* f1 Y7 V6 h0 m" m5 B/ @
  130. {. e' Z9 q7 {5 \/ k- m; g! S8 l
  131.     workPart = theSession->Parts()->Work();
    ) z  w3 o7 Y3 n
  132.     Part *displayPart = theSession->Parts()->Display();4 e. X$ s3 K- Z/ R3 q
  133.     stringstream out;
    # s# E3 _% j+ ^2 i9 A! l
  134. , m+ b, |6 J) |/ x' y, k
  135.     Face *theFace = 0;
    & W* {2 j8 B4 R3 i
  136.     std::list<Face*> myfaces;$ h/ Z% n* c' v, s7 t/ x
  137.     $ B. h5 v% Q; s* ^) w. e
  138.     while( (theFace=select_a_face()) != 0 )! a+ s  A0 ~( p6 k* e- E  m- q* u9 u
  139.     {; j0 |* B0 h7 D. m. m" h5 E
  140.         if(! lw->IsOpen() ) lw->Open();7 O. j5 _! R8 B# m( ^* E% x' t
  141. / g1 s5 {4 f2 b: ?8 S! e  D+ z
  142.         out << "Selected Object: " << theFace->Tag() << endl;3 U# F" l* J% X+ q& ~' p
  143.         lw->WriteLine(out.str().c_str());! ?2 s" u2 E: r- @1 u, k

  144. / Q0 F5 o; }+ ?* d, h
  145.         std::vector<Edge*> edges = theFace->GetEdges();( }$ n' d& ~# h' \8 O" `) ~3 }* n
  146.         out.str(""); out.clear();
    ) T. [- @' G* ~) c1 b: s4 A
  147.         out << " Number of edges: " << edges.size() << endl;$ G- q* s( P0 B  K
  148.         lw->WriteLine(out.str().c_str());
    + {% |) x, a. s2 ]- h
  149.   j2 O# E) |6 q' ~' ^- l% M5 D
  150.         for( int ii = 0; ii < edges.size(); ii++)2 m% k6 W1 ~2 x2 {# i. V
  151.         {' n6 S% |, x5 d! A; w9 d, n
  152.             out.str(""); out.clear();
    3 G( G3 j; Y' ^1 [0 L4 t
  153.             out << " Edge: " << edges[ii]->Tag() << endl;( F: N: D  l7 G$ \  e
  154.             lw->WriteLine(out.str().c_str());- J( K3 @+ }( a0 ?( ^+ {' d) Y

  155. . R6 {* s* T* `9 e& x& O
  156.             std::vector< NXOpen::Face * > adj_faces = edges[ii]->GetFaces();
    : ?( S8 q+ T  w' _$ ?! L
  157.             out.str(""); out.clear();
    " E# j: \- i/ s( D7 y- A
  158.             out << " Number of adjacent faces: " << adj_faces.size() << endl;
    ( O" r6 D0 Y2 [5 `! z& U5 T
  159.             lw->WriteLine(out.str().c_str());- H* M# Z) P% q9 x- q

  160. " f  S# C- `* u) I
  161.             for( int jj = 0; jj < adj_faces.size(); jj++)
    ' T3 N6 B+ P% F1 {( n4 v, l* g8 G1 j
  162.             {  ?& f. ^# \! s3 a3 s% x
  163.                 out.str(""); out.clear();& n8 d$ Y1 h2 y% ?. p5 u
  164.                 out << " Adjacent face: " << adj_faces[jj]->Tag() << endl;
    % l/ s- Y. c  @. i) E+ b6 l
  165.                 lw->WriteLine(out.str().c_str());
    1 I2 d. L" S7 h8 m4 ?% K* U$ y

  166. ! ]* k) \4 X! J
  167.                 myfaces.push_back(adj_faces[jj]);  U0 A* r% w# \+ b5 @, u
  168. 3 r, y& h: a# H) y: Z
  169.             } // jj, @6 y- e7 u6 e0 x

  170. 1 \- B4 i" [0 z
  171.         } // ii9 o$ S$ t: U, h1 o. D+ Y1 g4 N- i

  172. 3 {2 e* e) ~& H
  173.         myfaces.unique();0 ]3 y  B5 E. ^/ }! A
  174.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)2 w% s9 `+ y; P2 [5 _( Q$ h5 p& F
  175.         {
    / D. ~- r1 d2 s7 V6 \, n
  176.             (*it)->Highlight();
      ~% z7 X& b9 F% o+ E' ]
  177.         }
    ( s6 I2 m; ~- m* y/ p; R- ^4 x- N
  178.         Gtac::theUI->NXMessageBox()->Show("Adjacent Faces", NXOpen::NXMessageBox::DialogTypeInformation, "Confirm");: w1 W9 u0 t# T- z' v! O
  179.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)
      [9 V6 l+ ~. p' v$ [8 e7 f
  180.         {  g& W& g$ w" h/ @. E3 \* H4 p' s
  181.             (*it)->Unhighlight();
    ; L$ V3 m5 q, j8 p' W
  182.         }' N9 I# p3 F3 s
  183.         myfaces.clear();
    : B- b9 `1 j8 v3 D6 w6 g2 {
  184. , ?' f$ t4 I" B, R3 b7 X( J
  185. ' b3 p2 T- Y) \# q  l& b  ]
  186.     } // while
    " q6 V4 W9 ]4 C0 K7 V- Y2 a
  187. }
    " V: j- F- p3 C4 @4 s
  188. , J3 t  s) ^+ {$ f% ]2 H5 G0 e/ Z
  189. //------------------------------------------------------------------------------
    " }" U5 i/ O1 T* _5 r: f$ a5 q" g( R2 p
  190. // Entry point for our unmanaged internal C++ program+ q6 ^4 j& |" l! J" ]' b
  191. //------------------------------------------------------------------------------
    ; r. Y/ S: R5 d. G" S0 l
  192. extern DllExport void ufusr( char *parm, int *returnCode, int rlen )) ]; p- t( V5 k3 H& U5 V
  193. {
    1 ~- R$ c+ g) h5 p0 I
  194.     try
    7 j- O# ]7 f4 r; {7 b/ J; l6 I6 ]3 n
  195.     {
    4 m+ b' T- @) Z. F* M  p
  196.         theGtac = new Gtac();
    ) T: i* c$ C" H2 Z- n# ]* c* g5 r$ M
  197.         theGtac->do_it();
    0 u, Z* @  ~) H, r5 p8 U
  198.         delete theGtac;
    + A$ X% i  o/ X) y' r& {6 A
  199.     }  ]2 j: \! }* Q1 a
  200.     catch (const NXOpen::NXException& ex)3 C' c9 p' ?" d- D$ p
  201.     {( ]1 m, |& U* ?( b
  202.         // ---- Enter your exception handling code here -----
    ' r7 I# I5 m! W2 f4 C
  203.         Gtac::theUI->NXMessageBox()->Show("CaUGht Error", NXOpen::NXMessageBox::DialogTypeError, ex.Message());' d2 h8 e; o+ F5 t+ i& n: s
  204.     }
    , w( _  V" d* z4 @
  205. }/ J) b$ F2 C3 u( P# B: s' A

  206. ( p; m( Z) ~0 ]+ j$ U3 E* y0 U6 ]

  207. 3 N( W7 }9 k' y( _$ L  q
  208. //------------------------------------------------------------------------------
    8 q6 R3 k' @, f/ J6 L: X
  209. // Unload Handler
    8 V+ F* ?, y( d/ F' q' l% E
  210. //------------------------------------------------------------------------------( F% E, d7 r% g* w: l  f
  211. extern "C" DllExport int ufusr_ask_unload()4 H! l, u( X4 s+ s2 o
  212. {. j" y: X/ w" e( E
  213. return (int)NXOpen::Session::LibraryUnloadOptionImmediately;' G/ m; k: Z. N" I& t
  214. }: q0 m6 ~* v, ^& T2 h% R. W
  215. 1 }3 D, ~+ Z; @9 A5 v7 q7 z" H
复制代码
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 doTeam.tech
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了