PLM之家PLMHome-工业软件践行者

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

[复制链接]

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

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

admin 楼主

2013-11-6 18:54:58

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

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

x
通过这个代码,可以获取选择面的相邻面的信息8 v  G# }( E; Q" i3 m
NX open C++中使用了 选择对象 selectobject 函数,通过面获取边数据,通过边获取面,从而得到信息!+ _4 H' V+ c5 ^# C

% B  k7 p' `3 ?
  1. #include <uf.h>* Y* \' b0 J! z4 u; u4 L
  2. #include <uf_ui.h>% g# k6 V; w& L9 X6 F% O) ]/ @2 E! E# B
  3. #include <uf_object_types.h>
    / @- ^+ K6 N# w; L

  4. 5 s& |: U: n; @4 o- [
  5. #include <NXOpen/Edge.hxx>
    % U% C* r" C$ f) b* `8 L5 R! r2 \
  6. #include <NXOpen/Face.hxx>
    % A$ X) R$ z8 W& v

  7. 5 @4 ^! _3 V! X8 v* W' B
  8. #include <NXOpen/ListingWindow.hxx>
    7 C9 K1 k0 K4 s( H6 I1 E0 K3 N
  9. #include <NXOpen/NXException.hxx>
      G4 S) t4 E4 @5 s/ j2 k% T
  10. #include <NXOpen/NXMessageBox.hxx>
    ) ?2 L6 {' B, `" O' C* a! }# H
  11. #include <NXOpen/NXObject.hxx>  f* a- i9 }! Y; K2 j) J
  12. #include <NXOpen/Part.hxx>% M: c2 N: i8 {+ N1 z
  13. #include <NXOpen/ParTCollection.hxx>+ I. v  m+ r! S1 _
  14. #include <NXOpen/SelectDisplayableObject.hxx>
    1 P! V0 E6 k) g2 D# ^4 a/ w7 M% h
  15. #include <NXOpen/SelectDisplayableObjectList.hxx>
    ; D$ X0 K: T) m$ ?
  16. #include <NXOpen/SelectObject.hxx>
    " w  k) @  ?2 A: v9 Q3 x
  17. #include <NXOpen/SelectObjectList.hxx>
    $ C2 s5 q' H/ i3 b! z. u$ \
  18. #include <NXOpen/Selection.hxx>
    3 D, ~% v$ b. L
  19. #include <NXOpen/Session.hxx>9 T. c* O8 o7 h3 k/ T
  20. #include <NXOpen/UI.hxx>
    , t! S* W& O" \7 p
  21. #include <NXOpen/View.hxx>$ n+ p6 ?! H6 G; B: G" ?: y

  22. 2 F+ Q+ J' w) d5 ~7 _
  23. #include <iostream>
    8 b% E% {, Q. P3 [( j+ |* q
  24. #include <sstream>; \0 c( M4 o' j, |3 \5 P
  25. #include <list>
    + {8 B: @8 `* e7 `
  26. & Q5 B' {/ @8 K1 ^7 I- l& y6 \
  27. using namespace NXOpen;0 j6 d3 g0 t; x( d' B# B& H9 C
  28. using namespace NXOpen::Annotations;2 g( g. `0 u0 b
  29. using namespace std;
    / V- o9 H- {( U; d/ ^  u5 r2 M4 X

  30. % J4 v; y0 p6 @# k  q! J, m- ?
  31. class Gtac
    ; j6 k. K3 R% l" j- \4 C
  32. {! [; L0 j# T2 Z3 ?
  33.     // class members, D9 q# |6 F0 c6 @( F$ @5 p; @$ f  `' l
  34. public:
    , c/ b  P: u* ?, V
  35.     static Session *theSession;, r8 u' D# ~8 G' a/ V# u
  36.     static UI *theUI;  i( [  d$ S' a0 |7 V

  37. 5 {: F* Y. F: U5 q/ I8 z
  38.     Gtac();
    $ J: ?# \+ ^3 \  f' w& f
  39.     ~Gtac();: `8 V( w% G: q% l7 D) O( X5 y1 Y
  40. 8 y* m  j2 ]* p2 Q# c
  41.     void do_it();9 D& Q1 M0 g7 }0 V
  42. 3 T0 f) _1 M& r& B
  43. private:( e5 o$ n- W/ A" {
  44.     Part *workPart, *displayPart;
    6 \2 L; N/ ~. q0 Q  r( E4 o
  45.     Selection *sm;5 d3 F% @. r- M+ a- X5 L. j" o& p3 x
  46.     NXMessageBox *mb;
      C* i, U, k$ L3 R: ]* i
  47.     ListingWindow *lw;
    ; B: f1 v$ |; R" G

  48. ; B  t" ^: g8 `* X8 ~$ a
  49.     Face* select_a_face();' B7 g7 c% B3 |3 X  @

  50. ( d4 r, }- ?1 Y  j
  51. };
    : e: E+ I* a! E) W* |1 }

  52. 9 z2 G$ I" b8 j7 }) H
  53. //------------------------------------------------------------------------------
    5 y8 @# U/ |  u, p8 Q
  54. // Initialize static variables+ B, g. ^' s0 S2 [
  55. //------------------------------------------------------------------------------/ P0 ~) W  K0 W( a( C% l
  56. Session *(Gtac::theSession) = NULL;
    4 m0 v$ L" n# B' C8 `) V
  57. UI *(Gtac::theUI) = NULL;$ y/ V+ W# b7 S: i4 s6 Z

  58. : X. B  g4 C6 @* z4 k2 z
  59. //------------------------------------------------------------------------------
    : K6 Q% b4 L! V* Z% i9 i* L
  60. // Declaration of global variables* A$ Y, d$ |# V7 Z2 S
  61. //------------------------------------------------------------------------------6 |' T8 m$ `7 i3 _
  62. Gtac *theGtac;
    8 ~7 q- D, F  @+ W, `

  63. , s) ^5 u) Y5 A) ?3 x
  64. //------------------------------------------------------------------------------
    0 i" q3 v# h- A/ R& J: G  R
  65. // Constructor; |7 u, u7 H7 W% w& e2 Q
  66. //------------------------------------------------------------------------------
    1 t% D# f) o! U2 V) k% E
  67. Gtac::Gtac()6 N( z9 y4 N2 G: g+ m- }
  68. {
    5 [7 _5 S0 P; F  d4 o; e
  69.     // Initialize the NX Open C++ API environment1 X: T# E( v" o3 d2 i* a8 \5 e# ?
  70.     Gtac::theSession = NXOpen::Session::GetSession();
    / H' F8 c! p, @/ H) w4 ]
  71.     Gtac::theUI = UI::GetUI();
    . X9 P6 E# t9 W3 G

  72. & N1 r4 q; u9 h$ s9 Q  M) V
  73.     sm = theUI->SelectionManager();/ ?. i* x2 T9 Y8 t5 E3 c& r4 V
  74.     mb = theUI->NXMessageBox();
    ; }4 I' B; q. d7 a+ K; ^! u  m7 n
  75.     lw = theSession->ListingWindow();5 y9 o& v; N5 i
  76. * [. G& R/ w  L% i
  77.     workPart = 0;; u3 H5 p) z# d# [; Q2 p
  78.     displayPart = 0;# `; a2 t! ^$ h# \
  79. }- x" c( S( s, g0 e; Y; z

  80. # k4 t) a9 l& Q, V
  81. //------------------------------------------------------------------------------# Y. ?% W( Y) H) n( y+ s7 N
  82. // Destructor
    : u1 J. q4 ^* h' G- X4 M1 M/ k
  83. //------------------------------------------------------------------------------
    5 b( T8 {4 B6 l) `
  84. Gtac::~Gtac()8 d( n+ `  q7 r5 T3 Z5 I! @
  85. {& n" I5 q% r7 [
  86. }
    " v; D4 J3 _* \, l
  87. ( I9 I/ L" M; o9 N, b" j% q
  88. //------------------------------------------------------------------------------
      E  W6 C7 k) E* o* l
  89. // Selection: e) u7 a5 M/ a: W* E$ ]/ j
  90. //------------------------------------------------------------------------------, o) [4 ]  J  g/ I
  91. Face* Gtac::select_a_face()! X, P( f! n) w0 H
  92. {
    7 v( e9 G- m: k* t+ \5 P
  93.     // ask user to select a face* o2 v6 c7 _, Q4 K3 a1 i
  94.     UI *ui = UI::GetUI();
    " A3 C% ~: O0 V: k
  95.     Selection *sm = ui->SelectionManager();) F' D: d. H: ^5 u% ], F( ~9 @6 S* N
  96.     NXMessageBox *mb = ui->NXMessageBox(); // as of NX5  l3 ~+ l6 K$ y+ S9 q8 F! j

  97. ' _8 O* c+ q  t+ a. y: s
  98.     NXString message("Select Face:");, [  s, g4 x8 n# E/ s9 b
  99.     NXString title("Select Face");& V" r% ]0 T* t, V% G! T: W
  100.     Selection::SelectionScope scope = Selection::SelectionScopeUseDefault;
    ; [- m4 R) @# j0 K) m  Q( |$ O  f
  101.     Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
    ( z: N" x* h, A3 g: b
  102.     bool include_features = 0;
    ) s6 m' U* {! r) K/ w" C$ C5 I
  103.     bool keep_highlighted = 0;
    : A$ r! t6 d" n1 S
  104. , ~4 F; n! M/ [+ N- t5 Z5 ?: D. K
  105.     // Define the mask triple(s)
    4 {  p9 c6 {+ D  t
  106.     std::vector<Selection::MaskTriple> mask(1);# F# h% M, F6 H% M1 P
  107.     mask[0] = Selection::MaskTriple( UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE  );
    : X! b9 S& Z7 H- C: l+ n
  108.     Point3d cursor;  b+ J6 T+ G3 ~9 F' Q0 T
  109.     NXObject *object;* r4 k+ ~& M" F1 Q# P# E

  110. , k5 p. P( w/ a7 f  i1 \
  111.     // Select objects using filter defined by mask triples+ q& o2 N3 N. A! p5 m' Q" M; h
  112.     Selection::Response res = sm->SelectObject(* H9 |3 y1 p. ~7 b/ k+ Y6 Z
  113.         message, title, scope, action, include_features,  O0 K% I! _' q" y
  114.             keep_highlighted, mask, &object, &cursor );: {( z: y  C9 }& s' l
  115. $ U9 m# V/ p; L" t+ w9 c: h
  116.     if( res == Selection::ResponseObjectSelected )
    4 D  d0 |4 ?' b. H
  117.     {
    - n+ M0 Q$ n  [; z( I2 K& U6 L
  118.         Face *aFace;. \, z1 A6 X) N/ i+ h* Z2 c/ t
  119.         aFace = (Face *)(object);; D4 n+ Q, D7 ]( w
  120.         return aFace;4 _3 h" U% R4 l8 q8 x" I' I9 U
  121.     }
    # J8 x( S) c+ U5 E" z0 U6 F

  122. 3 j- Y1 f6 D6 K8 Z
  123.     return 0;
    7 p4 {6 \$ e7 C- U( }
  124. }) p' R$ E5 i" J/ N; _. ^* V

  125. 7 T# K" _& f- h% w. Z* p2 c
  126. //------------------------------------------------------------------------------9 K9 Q+ I- \( P- W2 ^7 g: V4 l
  127. // start with our job...
    2 Z" D- x: A! Z# K6 ~) u. I
  128. //------------------------------------------------------------------------------
    ' g5 x5 A) c2 n6 |7 F% P& g
  129. void Gtac::do_it()
    + m# N" S; a3 P( Y& ^4 M, E
  130. {3 I3 F; p) P( t5 h: [# Y! c
  131.     workPart = theSession->Parts()->Work();
    2 ?6 V. O  n$ X& C$ r; H
  132.     Part *displayPart = theSession->Parts()->Display();
    2 ^' X! Q8 o, w- t/ D
  133.     stringstream out;8 I7 r& ~0 X1 F

  134. 6 `1 L, Q3 ]+ f8 U) W: K
  135.     Face *theFace = 0;+ l. N, f& W) u, w9 @" Q" S
  136.     std::list<Face*> myfaces;$ [; o! l. Z  s* D; K; J
  137.    
    & i! J- k0 _; L! V, g
  138.     while( (theFace=select_a_face()) != 0 )
    ; y9 _, z: w5 |; {) Z7 I& h
  139.     {
    ' e; @  h0 w5 |6 _% y
  140.         if(! lw->IsOpen() ) lw->Open();
    ; n- |1 H& D% d5 b

  141. / |7 z3 ^" a" i5 q! H
  142.         out << "Selected Object: " << theFace->Tag() << endl;
    3 k  o" H6 x- t! A: G+ u
  143.         lw->WriteLine(out.str().c_str());; i$ @6 X4 l5 ^0 G/ Y" i! ^+ |. `
  144. : k1 [; v4 D9 w! w6 E% G
  145.         std::vector<Edge*> edges = theFace->GetEdges();. N8 J9 W3 i9 {' x0 G9 l
  146.         out.str(""); out.clear();
    . F9 |' y7 ~+ o
  147.         out << " Number of edges: " << edges.size() << endl;
    # f8 z2 |0 D0 R! D2 K
  148.         lw->WriteLine(out.str().c_str());
    * T% N5 o, t/ s7 ]6 @5 W
  149. ( U: W% g. G0 [* {  h" }; `, u
  150.         for( int ii = 0; ii < edges.size(); ii++)
    7 ^- x, O9 |3 T4 e5 G
  151.         {6 Y/ `/ ~6 O2 {% U
  152.             out.str(""); out.clear();
    + D* B; x- ]- F/ n' [4 M) g
  153.             out << " Edge: " << edges[ii]->Tag() << endl;
    , c& G" r3 A/ U* l5 B. Y
  154.             lw->WriteLine(out.str().c_str());" ~/ }+ t& a3 _* {, ~

  155.   t  |" G/ H* P/ J2 c& ?0 Z8 Y6 z
  156.             std::vector< NXOpen::Face * > adj_faces = edges[ii]->GetFaces();
    " @, s2 {- {! W. x
  157.             out.str(""); out.clear();* E4 t, f. U, }1 O" j: v+ r
  158.             out << " Number of adjacent faces: " << adj_faces.size() << endl;# N3 F" r+ q' Z
  159.             lw->WriteLine(out.str().c_str());% U6 A- `5 s5 M7 r! g  Q

  160. * X) {4 I' t$ Y1 r
  161.             for( int jj = 0; jj < adj_faces.size(); jj++)- m( b) f; `: q. j
  162.             {
      `; `5 N7 q2 S$ s( w0 m
  163.                 out.str(""); out.clear();# a; {5 ]! H! b8 P# U8 g
  164.                 out << " Adjacent face: " << adj_faces[jj]->Tag() << endl;4 c; j) G$ I7 b0 H0 D& y
  165.                 lw->WriteLine(out.str().c_str());( B  S$ ~/ R4 r0 ]9 C

  166. # `% L0 Z( X+ K8 E* B+ D
  167.                 myfaces.push_back(adj_faces[jj]);2 t$ R& l. g# f/ }% s/ Q

  168. 2 I1 e0 R6 {* l. B: c$ t7 Q3 m
  169.             } // jj
    $ _9 v" H  V1 z/ D' E( f/ h
  170. $ x2 \- d- P2 G$ ^" `
  171.         } // ii
    & H5 {' _0 L3 ^% F# Q3 _

  172. / ]( d( R( D2 }/ D
  173.         myfaces.unique();
    5 ~8 Z7 [! s# I2 D$ F( L
  174.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)
    ( z$ G7 u5 u( q- h% ~. @  M7 Y
  175.         {$ P, ?- `6 A. j$ d' ~) e
  176.             (*it)->Highlight();* v! ^1 ]- G, w$ T
  177.         }
    # q4 I  R. N# k
  178.         Gtac::theUI->NXMessageBox()->Show("Adjacent Faces", NXOpen::NXMessageBox::DialogTypeInformation, "Confirm");
    5 Q7 B7 u6 l: w& ~4 t
  179.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)1 S1 T# m  a8 i( D) k% U
  180.         {, B! W( y" n' r
  181.             (*it)->Unhighlight();
    ) |# Y- z7 s; U2 v1 x( {' k, }, e# w
  182.         }9 J+ G! v- S+ e8 P3 k
  183.         myfaces.clear();2 s9 B: Q8 D. C% B  `. ?- J* P7 h6 T; h% g
  184. - b; S1 E" F5 t8 U" j( H7 `- P! E
  185. : C3 N, w- }' o; b, o+ @& g
  186.     } // while
    0 k& `: U  i2 R4 D6 h/ j3 Z
  187. }
    3 H" I1 x+ b3 z( l! J$ t0 d& T
  188. 0 R' |& i) `, O
  189. //------------------------------------------------------------------------------& I) ?( E3 `' s9 i9 o% Q& K. }
  190. // Entry point for our unmanaged internal C++ program- i' ~' N+ R7 M! T( ~
  191. //------------------------------------------------------------------------------
    ( M" i& E  Y& ^! t
  192. extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
    0 W5 m( D7 q+ {6 e2 Q1 @6 P+ p
  193. {
    2 E9 g" d& F. y. L2 f- I6 |/ N( T
  194.     try8 m5 ^$ f, p/ Q+ k
  195.     {
    % B7 y  `$ ~/ [- R+ W% o
  196.         theGtac = new Gtac();
    % n8 a  h# |1 t' c, n
  197.         theGtac->do_it();
    - B" c3 ~+ {# w0 w
  198.         delete theGtac;
    ' Q  _# ^$ w. G' z2 Q' V4 J* L+ Q
  199.     }
    & X, n1 @" |( V3 T% O4 M
  200.     catch (const NXOpen::NXException& ex)  w2 u* t9 W7 K9 S# F
  201.     {1 c  n! j3 `. n- N' E
  202.         // ---- Enter your exception handling code here -----( j4 a8 y% k2 j
  203.         Gtac::theUI->NXMessageBox()->Show("CaUGht Error", NXOpen::NXMessageBox::DialogTypeError, ex.Message());
    7 R9 q% @& t1 X' \$ q" r( \- ~
  204.     }4 N* ~( g! F3 P1 ~  g1 _  t+ Z5 B
  205. }
    . l5 L+ w: Y7 |* l9 q3 A
  206. . O1 x* F9 P8 c( J3 r
  207. 9 P/ N) f5 B1 b( _
  208. //------------------------------------------------------------------------------
    $ g5 \7 `# V( n
  209. // Unload Handler$ o, h  E2 A& t5 s
  210. //------------------------------------------------------------------------------) ~' o  t' ^1 h+ O6 a' r) S
  211. extern "C" DllExport int ufusr_ask_unload()
    0 H8 m5 g. `2 ]' A7 I; k4 d) I
  212. {3 A0 N8 ]' S: C, j5 s% n3 Q
  213. return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
      P7 d* B4 |& _) B8 Q' c
  214. }
    - X4 q7 S% F* v1 q% D

  215. 2 M4 N. o8 p5 j% v( m7 A& H" r
复制代码
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了