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

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

[复制链接]

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

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

admin 楼主

2013-11-6 18:54:58

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

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

x
通过这个代码,可以获取选择面的相邻面的信息/ R! s8 R4 H# i/ C6 i# N
NX open C++中使用了 选择对象 selectobject 函数,通过面获取边数据,通过边获取面,从而得到信息!
' C9 K* s5 F+ W8 N! _5 e4 D2 E: X  Z; o. O) L
  1. #include <uf.h>; P; w  b7 Q. I, i" z; L
  2. #include <uf_ui.h>$ A6 l# p% b9 _7 G( y2 s+ B
  3. #include <uf_object_types.h>3 q" u" B2 p* y' I

  4. " T6 f2 \$ O0 D0 ?$ E
  5. #include <NXOpen/Edge.hxx>7 A7 U0 N; c8 G/ k$ z. F/ c, a
  6. #include <NXOpen/Face.hxx>
    - \) M. l# L9 J
  7. $ K1 o, e) ~6 C# ^8 P/ W' e
  8. #include <NXOpen/ListingWindow.hxx>
    # Q" m  P2 e' h$ e) w2 E
  9. #include <NXOpen/NXException.hxx>
    7 N# j4 v  j- v/ x1 u1 j; K
  10. #include <NXOpen/NXMessageBox.hxx>/ O( M& K' y  V' X: u* o
  11. #include <NXOpen/NXObject.hxx>1 y" d2 L7 ?3 K& V
  12. #include <NXOpen/Part.hxx>
    / ^+ I: D' b+ j( z( Y/ y. @( Q6 I
  13. #include <NXOpen/ParTCollection.hxx>
    ' a6 ?: e0 B( j* m$ w% N. ~+ X3 p) L; X
  14. #include <NXOpen/SelectDisplayableObject.hxx>
      V, g4 B/ s% K* h4 e
  15. #include <NXOpen/SelectDisplayableObjectList.hxx>
    , S/ m" B! h9 P2 ^; W6 r! `
  16. #include <NXOpen/SelectObject.hxx>- a4 `9 P- n! o- ~: n! `9 r9 c
  17. #include <NXOpen/SelectObjectList.hxx>6 I7 _- p$ J- Q$ _$ J
  18. #include <NXOpen/Selection.hxx>( @/ F3 O! b8 n- ?" ?3 j& F
  19. #include <NXOpen/Session.hxx>
    9 R+ ]9 `% F, p+ v* Y; d1 _. L2 T
  20. #include <NXOpen/UI.hxx>5 Q2 j  a2 y( v3 y
  21. #include <NXOpen/View.hxx>( w$ ], f4 d  ]6 ]7 g& p

  22. " B# U- G# ?" ]8 _
  23. #include <iostream>6 T# i( B( B* ~7 Y( v% b
  24. #include <sstream>
    $ c5 G' z+ L, x! s) A: x
  25. #include <list>
    & I4 }  i$ Q3 p
  26. 6 I, ?; o0 U* S( z' G
  27. using namespace NXOpen;
    ( O, B3 b4 d- {/ ^1 Z
  28. using namespace NXOpen::Annotations;
    5 I4 E6 @) ^  L$ l
  29. using namespace std;! w* j+ S* B" u: Y* D

  30. 3 J" E% g+ j$ \4 ?0 m
  31. class Gtac
    9 z* |' N1 U( I3 W  W8 ~6 s6 i
  32. {- C  V. D/ Z9 J0 ~! n- Z1 D- \
  33.     // class members
    ; L( m* s% P! f) Q4 x; `
  34. public:2 Z& L% y, q) h/ Q7 y  s& h% n
  35.     static Session *theSession;
    7 f/ G& x( X8 j& a# Y8 b9 B
  36.     static UI *theUI;! C$ i6 `" A/ m4 `2 S; ?

  37. 4 @. p+ a/ T, z% d0 `; A. M# P2 k9 ?
  38.     Gtac();
    ! u# z9 ?; i+ b; |
  39.     ~Gtac();
    3 h# I2 ^& C/ L2 w; S

  40. 8 W4 Y6 W0 y" F, T! p
  41.     void do_it();
    ' e- J9 t$ v4 {- L
  42. & \: ]( N* D, T4 W) J
  43. private:2 N4 O* ~% L6 F7 m& T' v( C
  44.     Part *workPart, *displayPart;4 |, i' F- G" U' c) P; p
  45.     Selection *sm;2 }+ z4 d7 x' W  b' i1 v
  46.     NXMessageBox *mb;
    # N8 i. q, J! O4 X. U$ n# X* r( X
  47.     ListingWindow *lw;# E& ]3 ^8 y* O1 V( V1 S) |5 X
  48. 6 Z4 F- R2 d2 t! m/ L
  49.     Face* select_a_face();
    " F4 q, t2 K5 B5 |! W/ C6 d2 O

  50. 6 Z1 B) e/ [7 A( z
  51. };
    : n+ K) y3 z* y% o

  52. 6 n' Z) y5 [8 X) l* d
  53. //------------------------------------------------------------------------------
    + `0 l" b: m, E. T# i
  54. // Initialize static variables/ ~$ Y7 b( Q$ {8 f0 e" _, ]
  55. //------------------------------------------------------------------------------
    ; F! l3 f0 Y) C) z* d4 o
  56. Session *(Gtac::theSession) = NULL;
      n- v- @; t# r: D
  57. UI *(Gtac::theUI) = NULL;
    ; Y! P! z; S, ~# i3 d" ^5 {- m
  58. ' l/ ^6 o4 X+ u
  59. //------------------------------------------------------------------------------0 ^0 C* y; t- y& W& G& ~
  60. // Declaration of global variables9 w+ x* L, U/ U$ @: Z2 N
  61. //------------------------------------------------------------------------------
    * `9 N! S& v2 e" i, F: }6 Y
  62. Gtac *theGtac;9 s' t+ c$ D5 Q, i# P$ m

  63. ! Y' ?6 J1 e( d: p- l
  64. //------------------------------------------------------------------------------
    1 K5 O' S7 @9 K) S4 n
  65. // Constructor5 J7 B$ V" Z' P+ g. m8 B' Q' B
  66. //------------------------------------------------------------------------------4 c  c" t( u+ v9 _
  67. Gtac::Gtac()4 d+ F+ o  G6 B9 r) s7 N% |
  68. {
    * e; W- _) K. T" Q* V+ ~# m
  69.     // Initialize the NX Open C++ API environment
    ; p& @/ n; V, L1 |: o
  70.     Gtac::theSession = NXOpen::Session::GetSession();0 T/ x  A8 E: `! W
  71.     Gtac::theUI = UI::GetUI();0 I9 W- U6 A3 p# W6 x
  72. , _1 D8 D5 E/ q$ [- F6 M8 h
  73.     sm = theUI->SelectionManager();$ y' h0 x( N4 N) ]* g
  74.     mb = theUI->NXMessageBox();5 H! ~: I% e8 |. O7 c
  75.     lw = theSession->ListingWindow();
    + Y+ U9 G- v" L, _4 f1 i! t
  76. ( k2 I5 s, g1 B% m. B# o- t
  77.     workPart = 0;
    * K  k1 X, k/ ]/ L
  78.     displayPart = 0;
    ( h  ~3 A0 v# G: I2 o' o
  79. }
    6 h$ k2 u0 m0 r2 t  F9 s
  80. 4 ^: C# s6 z  ]" }  V9 T
  81. //------------------------------------------------------------------------------) E6 ]# P- q3 a9 f$ H: `# }
  82. // Destructor
    4 q: e: [1 P2 U9 Q; L+ g
  83. //------------------------------------------------------------------------------* H. H$ s( G2 c! Z0 p: P0 u
  84. Gtac::~Gtac()0 i) _( y2 w- @7 A9 |4 F1 e( ]
  85. {
    + l+ V6 V1 X7 X/ U7 u
  86. }
    % f* V; i3 j% T* V. Q8 s

  87. ; ?, o4 r6 A7 L9 }& \4 k3 l
  88. //------------------------------------------------------------------------------6 q. J& q4 A. @8 T3 m% f& U0 P
  89. // Selection7 T' B9 [2 f, C) y
  90. //------------------------------------------------------------------------------
    * Z/ a, ~+ ]% s( G
  91. Face* Gtac::select_a_face()! Z. Z; A4 C# h% v8 g7 K" c, c
  92. {
    4 i3 |* B4 T2 @  M$ Y7 I
  93.     // ask user to select a face( I% i+ R& I. C3 K$ T" _
  94.     UI *ui = UI::GetUI();9 l$ o; K5 O, y* ^: W) w
  95.     Selection *sm = ui->SelectionManager();
    5 D, Y! e- v& t9 R& F. o/ \* }% g  ?
  96.     NXMessageBox *mb = ui->NXMessageBox(); // as of NX5
    # T1 O% n; }5 d3 D! K
  97. . n! E3 F4 J4 Q$ r
  98.     NXString message("Select Face:");& W7 E: O% ]( \: _% F
  99.     NXString title("Select Face");, G0 X" I; @' N# Y) `# v& y+ i9 N
  100.     Selection::SelectionScope scope = Selection::SelectionScopeUseDefault;; q# W: v- U! H" g9 o- Q
  101.     Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
    ; e( b. w. o$ |$ v" K
  102.     bool include_features = 0;. d3 ]0 y$ k' a" {1 A$ A, N
  103.     bool keep_highlighted = 0;
    : N  W' m" R" j$ b- m2 Y

  104. ) O+ c: M1 F; U. l
  105.     // Define the mask triple(s)- I, x$ J! ]4 z: _. J3 I
  106.     std::vector<Selection::MaskTriple> mask(1);8 x; O3 V1 ?$ S  w7 h
  107.     mask[0] = Selection::MaskTriple( UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE  );: f# l3 T# S  O/ k: j$ ~: R
  108.     Point3d cursor;& Z1 J% N! J& i3 |8 N" C
  109.     NXObject *object;
    7 A' z4 Y9 t5 s, a% s0 `% M
  110. 2 p% z9 c1 J2 _( V6 e
  111.     // Select objects using filter defined by mask triples1 M5 c2 A+ g3 R8 q/ E
  112.     Selection::Response res = sm->SelectObject(* P" k5 b; [) J6 [* k
  113.         message, title, scope, action, include_features,2 p, ?6 A8 k" w! X! y
  114.             keep_highlighted, mask, &object, &cursor );
    0 q0 Q1 q6 r# c! e' Q9 S& C
  115. . h2 B( u+ j: C' _% w! a- k  c
  116.     if( res == Selection::ResponseObjectSelected )
    & C; N1 h5 @) j- S; t
  117.     {  d+ C6 P6 G7 P* W; S, S2 J
  118.         Face *aFace;! Z) l3 s9 V" E" c
  119.         aFace = (Face *)(object);/ e3 e! W0 ^& |8 ~
  120.         return aFace;' `( A1 l7 |1 L( `* ]* a
  121.     }4 x: Q. H) X4 B% `

  122. ! T0 d9 p# b6 O' {) Z
  123.     return 0;
    ( g! D+ j% l  j0 g$ \! B( d
  124. }$ {; |$ H* [4 T5 ~" a) `

  125. & I1 h$ o: ~& B  g0 M
  126. //------------------------------------------------------------------------------; P$ O' x# |8 E- h  C; V
  127. // start with our job...
    8 P4 `# t2 k, S" L& @
  128. //------------------------------------------------------------------------------
    6 {, a' i# b9 L( d1 X8 i* `
  129. void Gtac::do_it()
    0 R5 T8 @. [" G& U3 R2 V8 }
  130. {
      H  L* }; M4 ~+ k* K5 t
  131.     workPart = theSession->Parts()->Work();) ]% a' y& @, f- w: U* l3 R
  132.     Part *displayPart = theSession->Parts()->Display();, g0 g( [$ e1 Y  w$ Y" J
  133.     stringstream out;5 E. ?- }$ Z9 ?4 b0 D

  134. ( [! B% p, k: ]% S( U1 u. Q
  135.     Face *theFace = 0;
    : t' [7 E9 |3 L$ A
  136.     std::list<Face*> myfaces;
      n2 M$ f! y6 x8 T& x3 r, H
  137.    
    ; A+ W# h! G6 V5 r2 P- j
  138.     while( (theFace=select_a_face()) != 0 )- }9 W7 H/ F5 ]1 ~& c0 K
  139.     {8 M. Q' L1 {* M. F! _( j
  140.         if(! lw->IsOpen() ) lw->Open();
    % H. D5 d* Y+ F1 l

  141. # ^2 q& W3 _( I- n) Y5 P
  142.         out << "Selected Object: " << theFace->Tag() << endl;  t- F# ]9 X& X$ [
  143.         lw->WriteLine(out.str().c_str());  P( m% {: R- @$ M* m
  144. & `4 ]5 ?' q' i4 G
  145.         std::vector<Edge*> edges = theFace->GetEdges();
    " H6 X' X, q3 ^8 Q! ]
  146.         out.str(""); out.clear();
    8 R8 J( i, a- ?' w3 b
  147.         out << " Number of edges: " << edges.size() << endl;
    ! S5 H% c6 h0 |. }/ C3 e) J0 }
  148.         lw->WriteLine(out.str().c_str());+ f( E. V* f5 }( F- N% C
  149. 0 c$ M+ H* |7 Q' a3 i6 u
  150.         for( int ii = 0; ii < edges.size(); ii++)$ B( `& c! w8 C( K2 e9 Y, y
  151.         {7 K8 f& M9 _% u  ~; D' [
  152.             out.str(""); out.clear();, \. ]; n. J' O8 P& e
  153.             out << " Edge: " << edges[ii]->Tag() << endl;" `8 e/ [# |8 }6 V5 ^: [6 a
  154.             lw->WriteLine(out.str().c_str());
    * s# H5 E3 M$ |1 W4 {- p$ {, G
  155. # p$ v. ?" X7 ^# \, A3 w
  156.             std::vector< NXOpen::Face * > adj_faces = edges[ii]->GetFaces();
    1 z, w' q/ Y1 o! [2 V+ T
  157.             out.str(""); out.clear();
    # b9 F3 k4 Y5 i1 i% U# M3 e
  158.             out << " Number of adjacent faces: " << adj_faces.size() << endl;& j% Y& l4 b$ s6 l9 s9 k" B
  159.             lw->WriteLine(out.str().c_str());
    # v" l: h, W. y7 z0 @

  160. ; g8 Y, p: y  f$ K1 q% ?9 |
  161.             for( int jj = 0; jj < adj_faces.size(); jj++)+ Q; N6 m5 D2 ~2 z
  162.             {
    - Z4 `+ m0 z- R2 e6 s9 Q  S/ S
  163.                 out.str(""); out.clear();; r; H! _8 f9 ~+ d8 y
  164.                 out << " Adjacent face: " << adj_faces[jj]->Tag() << endl;1 C7 m" e" c, t
  165.                 lw->WriteLine(out.str().c_str());
      k# y/ k" }* F; f4 p" |# T
  166. " g/ L; ~  p2 C$ M' q
  167.                 myfaces.push_back(adj_faces[jj]);
    6 Q0 E( m, R( x; i' ?

  168. : i- I, e, a" s, @5 t
  169.             } // jj- O' |! Q" n8 l. e0 `  J6 q/ I4 R( @
  170. 7 P2 ?, Z$ i) z
  171.         } // ii
    0 Y0 t6 \2 d0 t' Q/ D9 \: b
  172. 9 B  v$ k& H* ]" Z7 E& O2 ?: g
  173.         myfaces.unique();5 B) S2 S2 F* g4 ^, a+ j0 }
  174.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)
    ' x: N! r& j  o9 X6 I2 L1 w3 F# r
  175.         {
    7 P8 \9 ~0 E+ L3 S. ]9 ?3 ?
  176.             (*it)->Highlight();
    2 c0 J* g- V! y$ [" F" c. X4 r
  177.         }- u" q! Q. z+ `& Y$ [  |
  178.         Gtac::theUI->NXMessageBox()->Show("Adjacent Faces", NXOpen::NXMessageBox::DialogTypeInformation, "Confirm");7 A7 h# V( Y9 z
  179.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it). t: l' R7 f$ b* h* V9 x0 c$ @  V
  180.         {
    1 U) _7 o7 H8 ?( s
  181.             (*it)->Unhighlight();  f4 D# O2 H- A& j/ x3 ?- f
  182.         }
    . N  J$ M0 C: M7 f0 b  [
  183.         myfaces.clear();) k9 N! y5 C+ j) ]- V+ `

  184. ' V/ g4 O0 U/ q& u- z: l

  185. ; s5 e% r& e% p) `# x' E& X- Y
  186.     } // while
    & j8 `8 F; @1 b) Q9 g
  187. }
    " b: c$ J7 p8 u7 K2 h4 B

  188. + M7 A$ Z! W9 W" p5 R2 t
  189. //------------------------------------------------------------------------------
    ! f+ p- X6 o% T/ z
  190. // Entry point for our unmanaged internal C++ program1 E2 G1 N8 K4 [# O# M
  191. //------------------------------------------------------------------------------( s; Q: @1 H7 q, p. B
  192. extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
    * C% K! s$ A9 u% n6 n
  193. {
    5 N1 C$ b( u- z' w) D
  194.     try
    : B: _- }3 q7 c) v: y  `# a
  195.     {6 G+ v4 n- V0 ?8 d
  196.         theGtac = new Gtac();# G7 m7 d7 R5 e: V7 d8 ?5 ^
  197.         theGtac->do_it();
    . I$ R2 r* o, [: b. T$ B& i! ]8 L, i9 K
  198.         delete theGtac;+ Y+ Y6 ~! Z. W) P
  199.     }
    3 t2 i- {" n. K1 W# L+ m
  200.     catch (const NXOpen::NXException& ex)# c! O& z' B, G3 \
  201.     {5 C0 j4 d6 e! c/ a6 K; _
  202.         // ---- Enter your exception handling code here -----" O0 Y9 M( a+ L, u4 `# O
  203.         Gtac::theUI->NXMessageBox()->Show("CaUGht Error", NXOpen::NXMessageBox::DialogTypeError, ex.Message());
    / I+ @' w# J! h. _* B# A- Y* B
  204.     }
    5 }$ P9 O" q1 X
  205. }
    0 Q) W: j6 Q% M4 z2 B

  206. 6 ~: `# z6 f) z1 N5 O

  207.   x5 O9 `5 H* R4 i6 s& ?
  208. //------------------------------------------------------------------------------! \* L, J0 ?2 q$ s* b
  209. // Unload Handler5 q5 x/ ]+ U6 v0 D. T
  210. //------------------------------------------------------------------------------
    # Z: a% P! e+ X# Q
  211. extern "C" DllExport int ufusr_ask_unload()2 Z" _3 n; k% z: h( H! d( U
  212. {& R/ T. {7 _! p8 p8 _
  213. return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
    + I, t7 ]3 o" Z% w) H9 ^
  214. }' M0 H, E. H( A# A+ P

  215. * u  }& B' p  e) `7 J% X
复制代码
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了