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

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

[复制链接]

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

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

admin 楼主

2013-11-6 18:54:58

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

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

x
通过这个代码,可以获取选择面的相邻面的信息
7 {1 ~6 L" n- E1 ~1 t% }NX open C++中使用了 选择对象 selectobject 函数,通过面获取边数据,通过边获取面,从而得到信息!
7 ^+ N# a1 M9 j9 R8 Z: m" w% z: ]
  1. #include <uf.h>
    ( o2 Q, u' {2 A, m
  2. #include <uf_ui.h>
    - q" U& O+ T! v' Y4 y. K$ T
  3. #include <uf_object_types.h>0 v. H5 ~, p9 r1 ~7 Q$ O

  4. 2 B# W% x. x/ A  ?' {4 y
  5. #include <NXOpen/Edge.hxx>
    - [: i% H7 i: h
  6. #include <NXOpen/Face.hxx>
      M5 G  I* e& o6 A0 k
  7. & p; z0 L- g& J
  8. #include <NXOpen/ListingWindow.hxx>: I. [9 y- ^" t4 c
  9. #include <NXOpen/NXException.hxx>
    : d% T; ?# n& S2 I
  10. #include <NXOpen/NXMessageBox.hxx>6 o) K* X+ Z% N7 F5 Q$ A9 Y) p  g
  11. #include <NXOpen/NXObject.hxx>( l: R. N( Q( p4 @. l/ j
  12. #include <NXOpen/Part.hxx>/ p; R, @1 t/ i' Y$ }4 T+ A1 D- j
  13. #include <NXOpen/ParTCollection.hxx>3 j. M1 Z7 K( p& J% Q: A& S0 i6 O
  14. #include <NXOpen/SelectDisplayableObject.hxx>
    / J" l' w& |* k
  15. #include <NXOpen/SelectDisplayableObjectList.hxx>; h- n5 Z. d% {( }3 u( J8 d. M
  16. #include <NXOpen/SelectObject.hxx>
    ! n% A& e  i1 I" I. S* R
  17. #include <NXOpen/SelectObjectList.hxx>
    , `1 u* D# l9 h  W# }/ P. S. b# q
  18. #include <NXOpen/Selection.hxx>7 y( u7 T7 A  X) l: I1 f" Y) \
  19. #include <NXOpen/Session.hxx>
      k$ M- M: Q  D* E( B% J) i) \. A
  20. #include <NXOpen/UI.hxx># k1 l' y( E0 J- W! `
  21. #include <NXOpen/View.hxx>! J& k/ |; \  H
  22. 8 a5 o* ^/ r4 j0 ~
  23. #include <iostream>- v: S) x$ J5 q5 Z1 S! H& _! ]4 q
  24. #include <sstream>
    # g2 M9 G& g1 ]
  25. #include <list>+ f+ `. F8 Y; f. T6 n

  26. 6 i6 Y( h( V% i4 N' t, |
  27. using namespace NXOpen;
    : t) W; v" f8 r. P6 v
  28. using namespace NXOpen::Annotations;
    9 s+ C7 H  {! ]8 C7 L7 }
  29. using namespace std;4 v2 z) G4 h' b

  30. % R8 p3 t( N6 m& M
  31. class Gtac  v! l. A8 s. S* R0 X
  32. {0 U  ?8 z' U* n
  33.     // class members( a6 \7 a$ W1 J
  34. public:) Z! S/ C2 q! V9 o0 @
  35.     static Session *theSession;% b0 I1 A% p5 V( d1 H4 i& K0 T
  36.     static UI *theUI;2 b. y/ }( [: J% z) E

  37. ! s- Y4 j* C+ h
  38.     Gtac();+ B" t- k- ~0 Y
  39.     ~Gtac();" K4 r' p$ a& A5 k

  40. , _/ Z' h# H. a6 n. B  E: f) B( o4 T
  41.     void do_it();
    * \- m# b; f! h3 G

  42. 8 J6 Z0 \, ^# w' w& i' @
  43. private:/ `9 \+ I3 ^7 A' Y" M3 Z3 p
  44.     Part *workPart, *displayPart;) o: U5 G, y7 o$ T1 Y0 C9 j" ?
  45.     Selection *sm;
    1 s( H6 V. y, K# e7 ]
  46.     NXMessageBox *mb;
    : b' `% v3 E8 }; T+ z
  47.     ListingWindow *lw;1 ~; c0 c3 E( H* G! C5 `
  48. " y9 N, C+ H, Z! Q' R) R
  49.     Face* select_a_face();  |- c/ O- y0 h$ D& F

  50. 8 i2 R$ C/ p# N- _, J/ l
  51. };1 J4 @# S8 |" {( J
  52. 0 k: O1 S" Y# o6 d- K
  53. //------------------------------------------------------------------------------
    ! J6 z' `4 s9 w) `
  54. // Initialize static variables; }) [3 I; D- m5 @! o8 m. b8 N1 L
  55. //------------------------------------------------------------------------------
    ( U" b" k. p1 s2 @# w1 `; b9 h
  56. Session *(Gtac::theSession) = NULL;: k2 h3 y2 Q" \8 D5 o( J' G
  57. UI *(Gtac::theUI) = NULL;  ]0 x4 |9 i% h( u$ }
  58. * O0 q! u% k2 P( d& A- a, G) }
  59. //------------------------------------------------------------------------------
    8 X4 p% X& f) @8 o! A; o
  60. // Declaration of global variables
    $ }/ |) w" H8 s, L& @
  61. //------------------------------------------------------------------------------6 Q5 R7 I! |4 G6 v
  62. Gtac *theGtac;
    " i- m( Y- _; `: r# \3 d# S1 Y1 }

  63. , r: T# V3 \2 u: }& `2 P
  64. //------------------------------------------------------------------------------
    2 H7 e8 p' J* Q1 y$ j( S5 a. |( G9 s
  65. // Constructor
    ; K' c$ g# ^/ G) V5 L
  66. //------------------------------------------------------------------------------
    + _, Z1 d: S/ \9 n9 t
  67. Gtac::Gtac()4 P4 j; v. w9 ~* B2 J
  68. {
    7 j( j5 M( n& E0 w$ L7 x) ~& O
  69.     // Initialize the NX Open C++ API environment# `; {+ N+ m; ]1 R- @+ J
  70.     Gtac::theSession = NXOpen::Session::GetSession();
    * E( x' v+ L: p
  71.     Gtac::theUI = UI::GetUI();
    5 R" Z4 g3 D, A3 ~' L, I
  72. : h0 P# T) z3 |, a. N" B
  73.     sm = theUI->SelectionManager();
    4 n4 I. `. x( q% A
  74.     mb = theUI->NXMessageBox();
    ( Q. E% B3 _# c  d) d( l6 l, R
  75.     lw = theSession->ListingWindow();+ y. \4 s9 g' M& M
  76.   [0 T. L* C2 p. ]3 z
  77.     workPart = 0;  C- }; }. n- x/ F
  78.     displayPart = 0;
    8 @* }$ g. a/ o, u0 |
  79. }7 D4 z1 f; r! N; H1 K! l6 \. _
  80. 0 R  g( B9 h  j
  81. //------------------------------------------------------------------------------2 ]; x1 D! S! z6 g( a# i; }) _
  82. // Destructor
    0 d7 y: M0 ^1 g2 P  T8 N; s
  83. //------------------------------------------------------------------------------
    ; y9 }, d# y! X& E1 Y
  84. Gtac::~Gtac()* K6 N8 U  s) K4 s6 J* |( D! u
  85. {
    ) U2 X6 w1 a2 _9 `$ B% Y# _
  86. }
    ' U/ y; A* ^! ?6 @. e! x' F6 e+ L/ p

  87. 8 k5 x! w0 j2 \5 R# w! y
  88. //------------------------------------------------------------------------------
    $ J$ i( m( o$ [; Q, q8 W: O. g
  89. // Selection
    8 U1 S5 c& @6 u3 _
  90. //------------------------------------------------------------------------------
    8 }8 J3 _4 ^/ X
  91. Face* Gtac::select_a_face()
    - d, |5 i6 v: `- h" A4 @& S2 u
  92. {
    ' B$ v& V) B+ J) i$ F
  93.     // ask user to select a face
    5 i6 q9 W7 L6 A  y: g8 x
  94.     UI *ui = UI::GetUI();
    ' J& @- d: j- ?1 V! c0 T/ t
  95.     Selection *sm = ui->SelectionManager();) U; c6 _5 n; A3 n- t/ X! ]
  96.     NXMessageBox *mb = ui->NXMessageBox(); // as of NX5
    # k6 N% l. T9 e0 I5 Q
  97. 0 r. R; P: H9 |7 O! m
  98.     NXString message("Select Face:");) T* V' |7 h9 n8 Z8 e" {6 [
  99.     NXString title("Select Face");% z7 R. \7 ?; \: d. ^. H4 h/ v/ W
  100.     Selection::SelectionScope scope = Selection::SelectionScopeUseDefault;- s& f" n. A! P8 @
  101.     Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;9 \1 D4 q; U( e$ s/ Y: f# L
  102.     bool include_features = 0;
    % t  q* B1 _/ r% O% T6 f. [
  103.     bool keep_highlighted = 0;7 X) i( l% t+ W
  104. ( z: O: M: e1 u+ R+ t) ?* T' K1 ?1 ?
  105.     // Define the mask triple(s)
    ; T* c- j+ n; I! k  w1 g3 ?6 k
  106.     std::vector<Selection::MaskTriple> mask(1);7 F  D& @1 P+ C5 h0 G
  107.     mask[0] = Selection::MaskTriple( UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE  );5 ?- A: s9 w! T5 Z! N
  108.     Point3d cursor;
    ) v- u  C8 }$ S. U- ?
  109.     NXObject *object;
    5 e* e: n& r$ p* y5 Z" `

  110. ; Y& S0 ?9 P9 s/ L! {3 P6 v& w
  111.     // Select objects using filter defined by mask triples
    + D$ w' H: l1 ~, t3 |0 M% E
  112.     Selection::Response res = sm->SelectObject(# {1 [2 b$ n* s2 {( g( ^7 W8 c
  113.         message, title, scope, action, include_features,1 C! U* i! B3 e. M' P  i
  114.             keep_highlighted, mask, &object, &cursor );
    % _5 K6 g* Z8 B" ~4 j. q7 T
  115. 5 Y& v$ M, @- ~* U4 n9 a
  116.     if( res == Selection::ResponseObjectSelected )
    / @+ H5 U& D! e0 W* R- A
  117.     {3 A8 ~6 m, A3 ?1 \/ k
  118.         Face *aFace;
    1 U+ d5 t# ~; a* u5 {
  119.         aFace = (Face *)(object);% Z4 |: i5 ?- B" G1 H
  120.         return aFace;
    ) c& i8 W" O  R3 w$ f' }4 D  a
  121.     }8 ^5 u2 R( l( K/ \  o0 ?

  122. " j1 f. G5 \- a4 Z. E" U6 }" ~
  123.     return 0;, d# X- H: b0 k1 b1 J. z
  124. }
    5 ?+ p9 d7 O' ]. O& I

  125. * \/ a* N; a2 U& ^2 H  i. I+ Y, ]
  126. //------------------------------------------------------------------------------
    2 _( H- ~' ]4 E+ z1 r# h
  127. // start with our job...
    # ]2 n; ^3 s( ~; J) y- [
  128. //------------------------------------------------------------------------------+ \- M3 Q( {; P0 r
  129. void Gtac::do_it()0 p4 g7 M7 }, H
  130. {
    . m1 j4 c, \8 v4 b' z( J
  131.     workPart = theSession->Parts()->Work();
    % D4 {5 v1 X7 ?
  132.     Part *displayPart = theSession->Parts()->Display();( o4 J  G  w3 v1 Z
  133.     stringstream out;+ R8 z( e" W- {9 K# d+ v

  134. ! ~* Z& P  u* B9 V* F( O$ ^7 \- g' u
  135.     Face *theFace = 0;* ~' D' A. p$ e6 L; r
  136.     std::list<Face*> myfaces;0 Z+ G0 T3 ^' _# h
  137.    
    3 `# @* Q+ z% M, ?
  138.     while( (theFace=select_a_face()) != 0 )
    5 ~* `8 g/ \# X- m5 A0 h5 Q1 U
  139.     {
    7 k+ r8 H- J& m" x: d, s
  140.         if(! lw->IsOpen() ) lw->Open();3 s% W, t& \2 O5 Z
  141. 5 R# @/ @( N* W1 |
  142.         out << "Selected Object: " << theFace->Tag() << endl;$ ~' r0 ^7 {; Y2 L
  143.         lw->WriteLine(out.str().c_str());
    # ^7 U; C) p; y; v9 i" w$ N/ T

  144. # x0 [+ h5 i3 |- e; ]7 g
  145.         std::vector<Edge*> edges = theFace->GetEdges();. m6 I8 {5 d! [. o$ t* c
  146.         out.str(""); out.clear();% ^/ a8 V4 @; h4 f2 h: K8 M9 d
  147.         out << " Number of edges: " << edges.size() << endl;( N2 }) i; t* ~, }, f- b
  148.         lw->WriteLine(out.str().c_str());" s, Q+ H' d# _: g1 t& P

  149. & {$ g8 g8 G# Y4 F. H
  150.         for( int ii = 0; ii < edges.size(); ii++)
      F' X" c7 b& x: E5 w. V
  151.         {
    & ?% L) E1 g0 z5 d" g# `( r
  152.             out.str(""); out.clear();
    8 O4 C/ \3 ~5 `$ s& z- i1 k1 P
  153.             out << " Edge: " << edges[ii]->Tag() << endl;! Z- R/ s; O2 `7 m! ^  r, F) T: o
  154.             lw->WriteLine(out.str().c_str());
    + G7 ~7 s5 G' t1 E' w, Q, X
  155. 6 r3 u( C) C( f7 H! e5 T( A
  156.             std::vector< NXOpen::Face * > adj_faces = edges[ii]->GetFaces();1 U* s1 D5 z$ W7 n. Q1 x
  157.             out.str(""); out.clear();: f0 h6 R" E, n0 L; L: ?6 w2 v
  158.             out << " Number of adjacent faces: " << adj_faces.size() << endl;  ?; t. q2 S' s. K, y1 e
  159.             lw->WriteLine(out.str().c_str());8 q% H* l* Z( q( M4 _/ P
  160. : z, m" |. B1 c& s
  161.             for( int jj = 0; jj < adj_faces.size(); jj++)" D. e& y4 l( n- i4 G' I! |
  162.             {
    : Y5 {/ V# l7 |- l" P, t
  163.                 out.str(""); out.clear();; @. H2 v. o5 D& r
  164.                 out << " Adjacent face: " << adj_faces[jj]->Tag() << endl;
    % t8 Q  D1 U* S! g# M
  165.                 lw->WriteLine(out.str().c_str());
    8 y' y7 y# L# P, T1 K& u% r  q! n* b% `
  166. + A" U* N& E8 T- \( M  s
  167.                 myfaces.push_back(adj_faces[jj]);2 c* }, k) [  d* m* E& E
  168. % C# _, Z; z2 L  y( h
  169.             } // jj
    # h  D# m) w8 j) R

  170. ) J+ {' c2 @. U
  171.         } // ii
    4 m1 ?1 U' b- l6 \. a/ U' i
  172. ( a" V, r; [+ B0 Y: ~0 ~
  173.         myfaces.unique();) I* q& @$ t  f/ Z
  174.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)
    ; e* @. V0 G% A; ~/ M
  175.         {
    * q1 W& D3 ~* }: \2 _: b4 y
  176.             (*it)->Highlight();
    ; C3 }/ i5 I% }, u; q4 q4 \
  177.         }" i0 o' y/ V1 r( K; k. s
  178.         Gtac::theUI->NXMessageBox()->Show("Adjacent Faces", NXOpen::NXMessageBox::DialogTypeInformation, "Confirm");& y/ k/ `8 v) L7 N$ W
  179.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)) n: U! G* L8 v
  180.         {5 e$ t0 P3 s: y4 n
  181.             (*it)->Unhighlight();4 i$ ~9 |2 p7 p/ \
  182.         }
    # R) V  e- _  s. Z6 [
  183.         myfaces.clear();
    , H! L$ A9 y0 \0 Q0 E
  184. * b/ U5 Q" c& @, a. m
  185. 2 i6 o# z# s. s; j) L: o9 R+ |( ~
  186.     } // while
    , q+ I, D$ F' ]! _9 M) m" B) }
  187. }
    % z: Z5 B# F; o) b& S, I
  188. ( @; c+ k: a" r
  189. //------------------------------------------------------------------------------
    ; y* C0 z6 a; h* M3 }0 r% X8 `  J% c
  190. // Entry point for our unmanaged internal C++ program
    / {" m' M* `2 Q6 U8 |) c
  191. //------------------------------------------------------------------------------: C% q% s$ e1 @+ z+ A
  192. extern DllExport void ufusr( char *parm, int *returnCode, int rlen )" M$ K3 Q  y' p- \( S" v6 `4 W
  193. {
    1 H+ y+ N/ T( B3 j  x% p
  194.     try
    . K. B( K; N* l9 d) w* Y# q; _
  195.     {$ d! {) n5 V$ B3 v# b
  196.         theGtac = new Gtac();7 a& X) H' c# P: V
  197.         theGtac->do_it();0 C  y3 l# h! P) U: ^. s8 p
  198.         delete theGtac;
    3 b: |* D+ B4 v: m- _) ~1 j
  199.     }
    % p% m+ o5 n9 W2 f- B
  200.     catch (const NXOpen::NXException& ex): X$ m" D% Y+ C/ h, u5 [! M% _' p
  201.     {
    6 k0 n4 n  V7 {$ b6 i
  202.         // ---- Enter your exception handling code here -----8 {: r" t% l5 A0 @
  203.         Gtac::theUI->NXMessageBox()->Show("CaUGht Error", NXOpen::NXMessageBox::DialogTypeError, ex.Message());
    $ M% G3 s. a# Q
  204.     }. P9 y4 u4 u% p' c
  205. }' _. I$ @5 b6 R9 @# q
  206. 1 T% g1 B2 G- p: J

  207. " P; v5 \) \9 I
  208. //------------------------------------------------------------------------------
    9 t) N! R, ?6 _+ \$ x8 ]1 H
  209. // Unload Handler6 h8 v$ ?- c& `. v7 m$ H
  210. //------------------------------------------------------------------------------9 p* \: S4 a1 u5 B% B: g0 N
  211. extern "C" DllExport int ufusr_ask_unload()5 G7 M( _$ u9 L/ c, D+ U6 _
  212. {
    ( T9 N( n( {) `  y7 X
  213. return (int)NXOpen::Session::LibraryUnloadOptionImmediately;; a2 e) D' }8 m& M: f6 I/ ]( J7 x
  214. }
    & M8 f' y& y1 g) J

  215. # T4 a9 S1 s1 M2 h- W
复制代码
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了