PLM之家PLMHome-工业软件与AI结合践行者

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

[复制链接]

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

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

admin 楼主

2013-11-6 18:54:58

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

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

x
通过这个代码,可以获取选择面的相邻面的信息
2 t1 G1 \' c* i5 p: _% z5 d8 fNX open C++中使用了 选择对象 selectobject 函数,通过面获取边数据,通过边获取面,从而得到信息!: j+ y- e8 `' L( t
2 h  a$ ?( k4 e. L8 ^) B
  1. #include <uf.h>( U& }# f/ J" l2 T
  2. #include <uf_ui.h>; w9 W, G) ~3 l& b6 {$ U! h
  3. #include <uf_object_types.h>8 r" g: ^. `* X# `/ ?
  4. # x, @) n6 B$ E3 M  f
  5. #include <NXOpen/Edge.hxx>
    + R* z. }; G* n) m8 @$ N2 \% }
  6. #include <NXOpen/Face.hxx>
    6 u5 [7 _/ y/ y4 M. m5 S/ @6 X

  7. ! H. l9 i; D+ u2 E; W
  8. #include <NXOpen/ListingWindow.hxx>
    , O, B) ?5 _. d2 z8 l$ @
  9. #include <NXOpen/NXException.hxx>
    8 ]$ i! |! X) d0 c
  10. #include <NXOpen/NXMessageBox.hxx>- J+ q: a" ~3 H+ x% C/ b; q
  11. #include <NXOpen/NXObject.hxx>
    . h/ v1 J  m* ]# ~& ]
  12. #include <NXOpen/Part.hxx>
    7 t5 h* S  }* T9 j/ O
  13. #include <NXOpen/ParTCollection.hxx>
      L8 K5 v7 T! _5 O( F! {- g8 G, b
  14. #include <NXOpen/SelectDisplayableObject.hxx>& s4 s/ U1 d( j" F
  15. #include <NXOpen/SelectDisplayableObjectList.hxx>: U) A6 W( f* c+ U& b# t( A
  16. #include <NXOpen/SelectObject.hxx>
    0 D8 [0 ]  t5 c- q
  17. #include <NXOpen/SelectObjectList.hxx>
    $ L  U8 `+ M) H: i
  18. #include <NXOpen/Selection.hxx>+ j/ g8 U* r! \4 {
  19. #include <NXOpen/Session.hxx>; U+ e/ g3 a& [  e, Y" Q5 s
  20. #include <NXOpen/UI.hxx>" H& m6 W) @6 L: G0 a3 m
  21. #include <NXOpen/View.hxx>0 r% u4 `' T# B. B7 D

  22. 6 {: m3 B6 p. k+ I  ?9 Q5 w. o
  23. #include <iostream>
    ( [9 }& k* u& w# R- N. c
  24. #include <sstream>8 N6 L4 x5 D6 I$ v- K: J
  25. #include <list>& o$ h/ K  G6 N' ^9 V& o4 Z& z
  26. * @. p# K& C7 L" S* a! A/ x  r7 N
  27. using namespace NXOpen;
    - r3 o8 q  h2 h  Z$ h" W
  28. using namespace NXOpen::Annotations;
    # s/ p4 o0 R( u4 H- `' N
  29. using namespace std;. Y: K- h9 J  d- G- k/ T
  30. . a/ I  |/ Z. A3 H& y3 q5 M
  31. class Gtac
    ! V( K6 J# m: Y' ]9 x
  32. {0 }: g6 P+ N& E  D. Y2 I, O
  33.     // class members
    5 s9 e' Y" C6 v6 }: x8 _) [& p
  34. public:
    ( g4 H1 i7 b$ m3 U
  35.     static Session *theSession;
    8 I9 z; i* ~& \) N/ h+ R# {
  36.     static UI *theUI;
    , f1 {! N# z) v! S2 u

  37. / O% N/ E8 s" Z7 I; V# q* W
  38.     Gtac();
    " c4 X5 R* Z/ M) i) N
  39.     ~Gtac();$ J! G$ e2 k* `! s9 }. e, k
  40. 7 y0 L* v  o( L! R
  41.     void do_it();
    9 q9 M. U5 Q  x4 ?1 M; q4 a
  42. , S+ E% v7 h$ ]
  43. private:5 ~2 V# |- c* x( G% Y4 o/ s
  44.     Part *workPart, *displayPart;) [  s4 m! @  a2 ?( ^3 v
  45.     Selection *sm;4 W* c) m7 q$ d
  46.     NXMessageBox *mb;
    1 ]3 p$ W' e% l* p- Y2 q: s8 j
  47.     ListingWindow *lw;" F; b1 x# f5 T/ G8 b0 ?

  48. ( _" X- _% i1 W  N
  49.     Face* select_a_face();0 ]9 S0 T: L: c& U9 j, o: q0 G
  50. # o8 K4 c0 c1 B$ p
  51. };
    / v( q& y6 R% \: r% i8 |7 t

  52. % }4 I' j* r; \/ J4 l
  53. //------------------------------------------------------------------------------, K. O0 c. j6 I/ a4 t
  54. // Initialize static variables
    % M& v, J& n$ Q! y
  55. //------------------------------------------------------------------------------9 _. o) `3 r' I3 Y* \& D
  56. Session *(Gtac::theSession) = NULL;
    9 n3 I0 Q9 B$ |8 ~. M# o9 }
  57. UI *(Gtac::theUI) = NULL;
    " e2 G2 M6 z- {
  58. " u. n' d% q- [) k
  59. //------------------------------------------------------------------------------  d& S, {) T/ n  @- z
  60. // Declaration of global variables. f- I4 G, u, |# o3 y1 w) H9 d0 \
  61. //------------------------------------------------------------------------------7 S, z- T; f8 k2 |( |8 T1 p4 w
  62. Gtac *theGtac;4 o0 \9 k5 _* l5 \: e) [$ v  v, G

  63. . }4 Q- S2 H/ N3 c# l1 F! b
  64. //------------------------------------------------------------------------------
    " y% i( b4 v! ^0 Y
  65. // Constructor
    & P/ t0 L7 m. D
  66. //------------------------------------------------------------------------------
    , F& r- {" D9 v0 F. d
  67. Gtac::Gtac()
    1 v  s; r1 B0 C6 j2 x
  68. {
    ( e  O. U; w( ?) n( V! h" K3 U
  69.     // Initialize the NX Open C++ API environment) _) b6 P6 ]( S! C* I  I
  70.     Gtac::theSession = NXOpen::Session::GetSession();
    ! F/ Q6 Y% e. Q# O: [4 T
  71.     Gtac::theUI = UI::GetUI();
    . r7 j7 w  U  N* I% s& u
  72. ' D4 @5 F% i+ s0 O) G4 B* H6 j
  73.     sm = theUI->SelectionManager();
    0 B& U+ @. U0 x* }
  74.     mb = theUI->NXMessageBox();
    " V+ k: H; ~/ h! c3 Q5 X& Q' w- v
  75.     lw = theSession->ListingWindow();$ F5 H% v/ n% h: C

  76. . x- n! n- n0 h( i$ j
  77.     workPart = 0;" C  _" {' [( E: ~1 G, D
  78.     displayPart = 0;3 W" f; u& A* u; {, R7 S
  79. }
    * O" ?& u' t5 H, c9 v' r

  80. 4 p; l/ d. K% U9 J. p, |
  81. //------------------------------------------------------------------------------" V" q9 A; i/ Z. I% d% a& ]+ `0 c
  82. // Destructor& L: x' J( M) i* D% y
  83. //------------------------------------------------------------------------------
    9 x. X) X& s$ C* P
  84. Gtac::~Gtac()
    ! @0 I, [  r5 z6 z7 r; c- N
  85. {
    : h+ k% x! O$ M& U6 t' r9 }4 r% d
  86. }) w- Q, ]5 V0 z3 a

  87. 9 j9 O6 O# b5 t9 M, M/ |9 }
  88. //------------------------------------------------------------------------------* h/ D7 g* h- o0 Q
  89. // Selection+ z1 e$ B& z6 C% S" u1 |7 A2 D
  90. //------------------------------------------------------------------------------
    : H2 T6 {6 O1 r3 W- p
  91. Face* Gtac::select_a_face()- l3 V9 c2 Y3 s0 Z9 P
  92. {9 F9 a2 N( Z7 T9 `7 ]) V
  93.     // ask user to select a face
    ! N2 J$ X9 @2 `- o
  94.     UI *ui = UI::GetUI();
    : |8 F. b9 ~/ k& H
  95.     Selection *sm = ui->SelectionManager();
    & N$ v9 E3 a% D* J; v3 q3 L. A, G
  96.     NXMessageBox *mb = ui->NXMessageBox(); // as of NX5' l' y$ F" s# ~% G" N

  97. % h( k" O- r0 T) ~7 B: o
  98.     NXString message("Select Face:");
    ; _# X# Z& @' B8 A# W% _
  99.     NXString title("Select Face");5 j# r* ^0 o. X4 k0 x
  100.     Selection::SelectionScope scope = Selection::SelectionScopeUseDefault;
    1 U9 Y( d$ D4 O" S: A  @
  101.     Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;( u3 I" X$ |* r1 @! w& J: j& m
  102.     bool include_features = 0;
    , n7 w9 }' k% d4 b( E9 z! x4 V
  103.     bool keep_highlighted = 0;
    5 o6 e! P: N4 Z% @: [; g: r

  104. $ D$ e% S" u+ U/ t
  105.     // Define the mask triple(s)8 l# B# H  ?  m# J7 i
  106.     std::vector<Selection::MaskTriple> mask(1);$ [6 W* [: f% }( y9 p1 }
  107.     mask[0] = Selection::MaskTriple( UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE  );
    ) _, W7 c$ d0 x
  108.     Point3d cursor;
    0 b7 X1 j5 [  Y5 Z) b0 t: q: q! Y
  109.     NXObject *object;3 @5 g  l5 L2 g6 o9 n  M; l
  110. ! R$ z! ?4 \6 w( F( h
  111.     // Select objects using filter defined by mask triples
    9 ~( \5 e" q, a5 I# ~* j( Y
  112.     Selection::Response res = sm->SelectObject(  o( P1 J% P2 p( i' ^/ Q1 ~: z5 _
  113.         message, title, scope, action, include_features,4 ?9 U1 Q! K: _7 D
  114.             keep_highlighted, mask, &object, &cursor );, v+ {9 S- a3 \% S8 c8 W0 Q7 C

  115. : a$ H8 g" w0 H7 P( n) a& f
  116.     if( res == Selection::ResponseObjectSelected )
    0 u6 z* V4 \: h! A* N. l+ J, [
  117.     {
      Q9 k2 f3 }  F8 O+ }
  118.         Face *aFace;
    9 o& R. |+ w8 O
  119.         aFace = (Face *)(object);
    1 W3 R1 }. N4 S- Q+ [
  120.         return aFace;; C9 `6 n% n, c/ a# i' g2 F
  121.     }3 G( n; c+ ~$ j+ o& G( _' k

  122. . ?- _1 g, I% c5 z
  123.     return 0;: q/ G0 `- F1 ~3 T6 Z7 J4 T
  124. }
    - A  Z5 O1 {3 z9 K
  125. * t5 }  p# _9 `3 L
  126. //------------------------------------------------------------------------------
    5 N+ ^0 o! d# a: C$ w4 {& O0 u% Q
  127. // start with our job...' ?& ]3 u! d# ^4 d
  128. //------------------------------------------------------------------------------
    : F2 d) d* `5 l% |) K- T! g$ m9 |
  129. void Gtac::do_it()
    7 @; `# }% F2 a' T( R+ l
  130. {
    4 h6 c) T6 }' n& E
  131.     workPart = theSession->Parts()->Work();6 N  S) w8 h: M5 ~% K# L
  132.     Part *displayPart = theSession->Parts()->Display();
    ! X# i$ ?) s9 d, ~- d
  133.     stringstream out;
    8 ~. @! \" ~* {5 |, j8 d

  134. $ L& u- |/ Z1 L0 B
  135.     Face *theFace = 0;
    . I6 I% B2 |6 e/ j" I! I
  136.     std::list<Face*> myfaces;0 D; K/ b1 b0 f6 Y5 \
  137.     1 y/ T- h. c" s/ d8 n
  138.     while( (theFace=select_a_face()) != 0 )
    / t5 p* a& w2 H; ^! e; b0 m
  139.     {. {! Z/ n  N, |' V
  140.         if(! lw->IsOpen() ) lw->Open();
    8 B. n5 i, z1 A

  141. 6 F( {. O. U" Y7 F! R
  142.         out << "Selected Object: " << theFace->Tag() << endl;
    + R' T8 Q3 a2 G  }* Y# @; N& O" o. O
  143.         lw->WriteLine(out.str().c_str());
    4 `0 s0 p3 T$ c3 m
  144. ; V1 v1 A' _: j) M
  145.         std::vector<Edge*> edges = theFace->GetEdges();
    + T5 f, V$ d$ z  Q' c
  146.         out.str(""); out.clear();" S* _: ]* T+ S* B4 Y1 X& _* m
  147.         out << " Number of edges: " << edges.size() << endl;
    4 c) n# ~' I5 O$ x9 [* f
  148.         lw->WriteLine(out.str().c_str());' l8 t# N) @* F3 b4 G3 o7 {7 J

  149. ! [8 p* }! Z# D1 f& K! I
  150.         for( int ii = 0; ii < edges.size(); ii++)
    + `! L7 Z3 J8 H9 ]+ N
  151.         {
    ' j; k- ], |' @! a  `/ u. x$ `
  152.             out.str(""); out.clear();; Z2 ^5 ^3 n, B2 r
  153.             out << " Edge: " << edges[ii]->Tag() << endl;
    ; @: t: Y/ T9 w7 m
  154.             lw->WriteLine(out.str().c_str());
    , T4 t2 h: |( q+ q5 f

  155.   A8 a' k8 i6 S) K, @
  156.             std::vector< NXOpen::Face * > adj_faces = edges[ii]->GetFaces();; n$ |4 \1 }$ c& A# P# _4 Q+ @
  157.             out.str(""); out.clear();& B/ K$ {% }8 Z1 Y. n6 N: ]
  158.             out << " Number of adjacent faces: " << adj_faces.size() << endl;5 p: F9 @4 Q' G/ T4 a
  159.             lw->WriteLine(out.str().c_str());) h" K( `/ t; X1 S: I% o- [% S( D
  160. . J) F% N  {9 }) ]0 o( o9 p& B; _
  161.             for( int jj = 0; jj < adj_faces.size(); jj++)
    # z+ K4 H$ F( n! _. O' i
  162.             {
    1 g: z3 R5 G% C9 @. e! D& F
  163.                 out.str(""); out.clear();
    " H% z# X- [; E) @
  164.                 out << " Adjacent face: " << adj_faces[jj]->Tag() << endl;2 L( @. t% |8 p1 w& |' D- _* t' x
  165.                 lw->WriteLine(out.str().c_str());
    % x) t3 ]$ c% X

  166. & Z8 }$ v7 H2 @8 m
  167.                 myfaces.push_back(adj_faces[jj]);7 h8 R# q4 t! t5 z  f) g3 G, Q
  168. # C2 h0 Y: N& K2 G
  169.             } // jj
    $ g- Y9 y5 G4 j0 }
  170. & r0 q3 W" e4 K( [5 `
  171.         } // ii8 N% |/ ?' W( Z# F
  172. - D# A- C. j1 ^& |0 P. I4 r
  173.         myfaces.unique();
    " |: \* ~  E, a( t+ d* \6 Y2 z+ q
  174.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)& W( O( P/ h- u3 X, a# X
  175.         {9 h& |. T$ Y9 y3 M0 F8 T& l8 \
  176.             (*it)->Highlight();
    - O; k- R" v6 o; H7 S9 |) z' {
  177.         }5 ?& [% j: F% o9 L* h
  178.         Gtac::theUI->NXMessageBox()->Show("Adjacent Faces", NXOpen::NXMessageBox::DialogTypeInformation, "Confirm");
    # ^  x# s. W! e6 h+ [: j# p- u
  179.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)  @3 b( @) }! `/ d. z( t0 W4 \; }: Y8 C; T
  180.         {9 U% q$ N/ |! l- I" y0 R
  181.             (*it)->Unhighlight();9 R& T& w" w+ y% a0 n( ]4 e; Z! R
  182.         }+ H+ E- k/ U; `* n+ W
  183.         myfaces.clear();
    ! u$ ]% _! `" r& n; |# Z
  184. * D3 B1 Q  k* b8 @- _( ~5 @
  185. % B5 S& n+ U0 k' B8 T
  186.     } // while
    . r4 a8 C+ E- w6 J6 _& v& h' H; @
  187. }
    2 O! \# Y6 W5 W
  188. ! ?9 Y6 z: l( N5 a: s. w$ }' b9 I
  189. //------------------------------------------------------------------------------, Y/ ^+ F- h( t, L7 ^3 `
  190. // Entry point for our unmanaged internal C++ program6 t+ s3 Z' v( R+ _+ P, O
  191. //------------------------------------------------------------------------------/ n# k: B0 ~4 G
  192. extern DllExport void ufusr( char *parm, int *returnCode, int rlen )+ O0 [) H7 ^, W8 M7 I3 C' o
  193. {2 I4 M+ P2 [" [; |+ m- b/ X( R
  194.     try
    , I- S$ Q" j5 a4 \( V" \5 p
  195.     {
    / u5 N* i5 }7 i
  196.         theGtac = new Gtac();
      I* S: Z& \! c7 |9 h, @7 K/ o
  197.         theGtac->do_it();  T8 T8 F" e7 J, M5 w
  198.         delete theGtac;
    , j$ D% w# @, u8 A% }; W
  199.     }1 E, g( U! e4 y
  200.     catch (const NXOpen::NXException& ex)
    " {, w5 b  O: t6 Z
  201.     {: E, ]0 i8 r. C7 x1 _3 T
  202.         // ---- Enter your exception handling code here -----/ F9 Q6 c% [0 [
  203.         Gtac::theUI->NXMessageBox()->Show("CaUGht Error", NXOpen::NXMessageBox::DialogTypeError, ex.Message());
    : _2 ^4 Y  S/ w/ @3 S7 k5 M; Y
  204.     }
    ; y" K# H% V4 U6 ]0 b7 j" K: Y  W
  205. }: H4 A2 H1 c, H! T8 Y: S6 n3 D
  206. . }$ |9 u. p! |+ D
  207. ! o" w& E6 P( t. j8 p# T' P
  208. //------------------------------------------------------------------------------
    ' I1 N+ ]$ L  g' e* B
  209. // Unload Handler
    8 ^6 m( e8 ~, X: v6 j
  210. //------------------------------------------------------------------------------8 n2 h2 l: @' o) F: l* y
  211. extern "C" DllExport int ufusr_ask_unload()
    9 \/ Z" D5 T5 W$ d( \7 [- o& t
  212. {
    ; N+ d% z' ^& S. k; Z" D
  213. return (int)NXOpen::Session::LibraryUnloadOptionImmediately;! z' ?" d5 v3 X- @, y
  214. }4 x! M$ O( t7 _& r

  215. 4 o+ m6 ^$ a* w5 ?8 k) ^- t
复制代码
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了