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

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

[复制链接]

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

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

admin 楼主

2013-11-6 18:54:58

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

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

x
通过这个代码,可以获取选择面的相邻面的信息/ D$ c( G5 h$ _4 ~
NX open C++中使用了 选择对象 selectobject 函数,通过面获取边数据,通过边获取面,从而得到信息!
1 M* @) d) W: @' f
; |4 ?7 m& F. W4 w) l  s
  1. #include <uf.h>
    6 ]+ G5 ~7 D( s; l# T
  2. #include <uf_ui.h>7 p; u9 J1 Y9 O5 m: ^
  3. #include <uf_object_types.h>
    6 Q  l6 T$ {: ~) X
  4. % k9 d9 Y+ s' V& W, w' v6 k+ S- B
  5. #include <NXOpen/Edge.hxx>
    * X: N% T- S% u0 G3 m
  6. #include <NXOpen/Face.hxx>5 K$ N7 c- v2 T# O  Y  t( h

  7. ( g4 R. e8 w8 p! ^$ V
  8. #include <NXOpen/ListingWindow.hxx>
    . b" p& z  a. S8 S, h+ S2 m  _
  9. #include <NXOpen/NXException.hxx>! T- F+ k* T8 V1 y
  10. #include <NXOpen/NXMessageBox.hxx>( S1 Z$ f) }+ p) F% q( Z, Y
  11. #include <NXOpen/NXObject.hxx>
    ' |! F8 W, T! `9 F
  12. #include <NXOpen/Part.hxx>
    - J" ]' l; U9 s; r" L  Z
  13. #include <NXOpen/ParTCollection.hxx>, t, z# w. k6 p/ F$ b
  14. #include <NXOpen/SelectDisplayableObject.hxx>2 Y* K* T! f  o
  15. #include <NXOpen/SelectDisplayableObjectList.hxx>
    & f; D8 N& |# V( _2 m
  16. #include <NXOpen/SelectObject.hxx>% s) J0 m" W5 z' U
  17. #include <NXOpen/SelectObjectList.hxx>
    % ?- t* D. L# c7 d) J
  18. #include <NXOpen/Selection.hxx>
    # V9 J' @& a- A
  19. #include <NXOpen/Session.hxx>  I! Q$ T" I! G* ?' e* s
  20. #include <NXOpen/UI.hxx>0 p/ r' v0 |1 B& o/ s7 r9 ^. x
  21. #include <NXOpen/View.hxx>" {) P' H8 P' t3 Y3 @! h, f
  22. 4 R5 m' B% f2 f4 n7 G
  23. #include <iostream>  C% D+ j) {5 w: r; x- l
  24. #include <sstream>! Q$ ]9 L0 U" ?
  25. #include <list>
    - I8 u6 R9 r) L7 _; L# ]8 X- H

  26. : Y3 Z6 p8 ^4 {3 k4 Y  d1 m
  27. using namespace NXOpen;
    ) ^, L  A4 D: y+ }2 g9 m* z' _2 _3 P
  28. using namespace NXOpen::Annotations;! p& g# w: u6 }6 n" p7 V" A/ A; E
  29. using namespace std;
    # u! Q9 Q; R9 G! o; J. _+ `) M4 x

  30. # U; Q0 f! I  |& v4 {  Z& e7 Z
  31. class Gtac% |. @0 F  T4 P+ y% F
  32. {
    ; a3 g& f# c3 n0 b& s
  33.     // class members
    $ m2 \0 l# s$ P3 F! ^3 S- g
  34. public:" {$ H7 X, |. j9 S( ^$ O7 r2 N
  35.     static Session *theSession;
    0 I% v  N. m2 ~0 f" @- {# Y* E% M
  36.     static UI *theUI;
    , d2 A+ C& k  a6 R

  37. 1 m0 C. ?& }3 e, D% B1 z# l" w; u5 z
  38.     Gtac();
    ; j. E; I( O  L
  39.     ~Gtac();8 @2 I( g7 b* _) E; [+ j7 V
  40. 0 p+ X: @3 z3 E. G# D
  41.     void do_it();# J- i0 D* @3 v2 b
  42. / E  }9 `8 c/ @8 {$ Q) `
  43. private:
    ' q/ J, w, _! f) X
  44.     Part *workPart, *displayPart;0 ^8 Z+ S# g- m0 z3 y. r9 {
  45.     Selection *sm;4 b' I+ K6 n2 e' _. x9 K* r
  46.     NXMessageBox *mb;
    , _& T2 i9 L2 [! p, H
  47.     ListingWindow *lw;
    5 L/ j& y5 q" q4 z

  48. / A5 M* f( T! p* w6 G
  49.     Face* select_a_face();
    % }  _" c( b* U4 `% ^# g) Z

  50. 9 M+ c. G* A3 R
  51. };5 I; i8 i* V) m" i! b

  52. 9 g' f: L6 S4 ?$ u  q! P. s( s  R
  53. //------------------------------------------------------------------------------. y: q9 \' G" _. Z
  54. // Initialize static variables
    2 n1 `8 ?: T! D9 n) B) ?# `+ |. A! S
  55. //------------------------------------------------------------------------------
    ; J5 _# H2 P" E1 ]" o; a
  56. Session *(Gtac::theSession) = NULL;
    ( G# \) V4 n4 t7 {- u; _# k
  57. UI *(Gtac::theUI) = NULL;( z, U6 R( w0 v7 p
  58. , @# d: [% i1 r) m+ ~* E
  59. //------------------------------------------------------------------------------$ |* v7 x+ P" ?6 o1 e2 t
  60. // Declaration of global variables
    : d# ]( g% J, D
  61. //------------------------------------------------------------------------------
    , a. C) z" G# N9 }
  62. Gtac *theGtac;
    # a7 S$ E- m& x7 I' c9 N
  63. 4 ^+ B7 d/ E- p
  64. //------------------------------------------------------------------------------! {! h# H+ K8 e: H2 c
  65. // Constructor2 U! a" G9 C% P9 p3 o
  66. //------------------------------------------------------------------------------
    # u3 q" h! ^# u# ?8 `4 ^
  67. Gtac::Gtac()$ D( c5 i8 H0 m  C  P
  68. {
    3 _5 `; e. h. x5 I; d
  69.     // Initialize the NX Open C++ API environment" ?0 z+ }$ u3 S- ^( ]) |6 {
  70.     Gtac::theSession = NXOpen::Session::GetSession();
    9 K) ]: O0 U; @/ v* _$ w4 U1 I
  71.     Gtac::theUI = UI::GetUI();
    1 Z0 @4 ~( M" k
  72. , m! |. Y' a, v1 |+ J7 y1 P
  73.     sm = theUI->SelectionManager();" Y; |5 A, F( ~/ l- J
  74.     mb = theUI->NXMessageBox();
    ' d1 y8 d; j: H4 S, j
  75.     lw = theSession->ListingWindow();
    " m+ O  b6 }, m( _; I
  76. - d: b% u: _0 f- _$ b
  77.     workPart = 0;9 C  o- S+ g/ e+ M, m
  78.     displayPart = 0;' K! ?% [0 k6 C& g" Q
  79. }
    3 ?$ U+ m4 @1 i5 u6 @" w# F
  80. & Q( j$ r% c8 }
  81. //------------------------------------------------------------------------------6 k2 s' [2 i' @" n
  82. // Destructor$ C, l4 m+ c, a5 I% g8 y0 o
  83. //------------------------------------------------------------------------------
    : y" `6 g! M. p! z3 V( \3 o# ]
  84. Gtac::~Gtac()
    " ~+ X0 f: d" h& j  b* D7 e
  85. {, \. g! w& o$ F+ z* K
  86. }
    + U+ w% t" R& A# X- q5 p

  87. 1 w) X+ _3 {2 n6 p; ?) ?% e6 x' S
  88. //------------------------------------------------------------------------------3 K4 n6 \; o4 s) k
  89. // Selection
    0 W: j. T! S8 N9 x' @9 Q
  90. //------------------------------------------------------------------------------, f" B( u' A3 n& u. h# n2 ?' L/ a! b
  91. Face* Gtac::select_a_face()
    ' Y( U0 ?" C  Z. W
  92. {
    + d8 X, j. r: n& }
  93.     // ask user to select a face7 z- Q) R: n& V3 G8 j% e9 v9 K
  94.     UI *ui = UI::GetUI();
    * R' S: u5 ?) \. l% {1 ^. M
  95.     Selection *sm = ui->SelectionManager();# W; s- N/ o8 c0 V* X
  96.     NXMessageBox *mb = ui->NXMessageBox(); // as of NX5
    - M$ z, H1 L# w1 z
  97. 0 s1 V  y* N6 k! ]' A" a
  98.     NXString message("Select Face:");- P7 k/ d1 g& T
  99.     NXString title("Select Face");
    4 J3 E' q7 Y+ H4 @# o+ ]4 v( A* C
  100.     Selection::SelectionScope scope = Selection::SelectionScopeUseDefault;% t- }9 U. B. n1 N" p
  101.     Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
    & P1 \& A% p% R2 h" e  T
  102.     bool include_features = 0;
    ) d. K) \8 k* ]6 f% Q
  103.     bool keep_highlighted = 0;
    6 x- Y. ^7 [6 P/ b, @* [( Q! f

  104. ' e2 @7 e) {0 t6 E
  105.     // Define the mask triple(s)9 u+ Z. f) |' Z7 s1 v1 i0 o
  106.     std::vector<Selection::MaskTriple> mask(1);
    . Q5 f* r) g1 _  ^
  107.     mask[0] = Selection::MaskTriple( UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE  );
    - L- y  q5 Y2 \" t! ]# g  K9 ]  [
  108.     Point3d cursor;& a, Y: \5 d1 k% P2 H0 z" t
  109.     NXObject *object;1 D5 M$ D; A9 N  C& {0 [

  110. 6 w# a/ a/ p, N" M8 ?2 G# G
  111.     // Select objects using filter defined by mask triples9 p6 Q1 h8 X' ]$ A, Z
  112.     Selection::Response res = sm->SelectObject(5 r) c! Q- o6 d$ m- J" U
  113.         message, title, scope, action, include_features,
    7 _) n- |/ l3 b$ G: ~* x
  114.             keep_highlighted, mask, &object, &cursor );7 q/ W5 _! F0 u! w9 q3 t# s
  115. 8 D/ f2 ]6 Y  ]# K
  116.     if( res == Selection::ResponseObjectSelected ). q2 r; C  ~/ \
  117.     {* \9 a3 q2 c3 s, S) p" l& \
  118.         Face *aFace;  Z1 `5 `6 M% W7 J) q1 ~) M
  119.         aFace = (Face *)(object);
    1 I' z4 ?& c) }$ o, P
  120.         return aFace;0 ~( Y% Q4 N; c2 ]+ a
  121.     }1 u( b. U4 c0 `9 D- Z3 }) C* {' Q9 |

  122. 2 Y0 m/ L, @* p
  123.     return 0;
    / Q  f; \* ]4 ?7 E) M
  124. }
    9 B  x0 b, `4 A+ T* J) b4 @6 I
  125. 4 R/ U. L! d* Q! m6 V
  126. //------------------------------------------------------------------------------# V! D+ [* G' ^4 ?9 {. l1 I
  127. // start with our job...
    2 j* \; k/ d, K+ u
  128. //------------------------------------------------------------------------------/ ~3 T" l0 x# u5 D  H
  129. void Gtac::do_it()
    9 y- n6 A/ K' f
  130. {
    : \7 M8 L/ M% }& k
  131.     workPart = theSession->Parts()->Work();
    - C( `3 Y$ ]5 Y$ A
  132.     Part *displayPart = theSession->Parts()->Display();7 C& K) j1 u9 s: |, l) H* o+ v
  133.     stringstream out;
    / W; I% L$ c' ?
  134. . `( z9 N- I9 Q9 ^
  135.     Face *theFace = 0;3 i' S2 ~8 L4 x$ o, @( G
  136.     std::list<Face*> myfaces;$ R2 P: ~+ m6 g% n% M+ W
  137.     * R+ E3 l1 F1 e7 m( m, j
  138.     while( (theFace=select_a_face()) != 0 )
    & `8 e" S2 @# G& v
  139.     {
    5 a3 b# [% b, ]
  140.         if(! lw->IsOpen() ) lw->Open();+ N+ H, i- a3 ?
  141. ) c, Y( O( I% w/ w
  142.         out << "Selected Object: " << theFace->Tag() << endl;6 M$ m4 h, c8 y! W9 K
  143.         lw->WriteLine(out.str().c_str());
    / Q8 ?: w  I) R4 L6 N) {
  144.   F% K8 h6 c$ @9 `' z* N& Z
  145.         std::vector<Edge*> edges = theFace->GetEdges();; R" U! |, q) I* [8 [) R
  146.         out.str(""); out.clear();
    , `- ^$ W$ u- f# R& @
  147.         out << " Number of edges: " << edges.size() << endl;
    5 D2 Q: \% x, c
  148.         lw->WriteLine(out.str().c_str());" C, w/ {+ u* Q/ |; ]) W( M
  149. 7 m; d5 e% j& A# F
  150.         for( int ii = 0; ii < edges.size(); ii++): ^( j2 Y( f8 A' u
  151.         {
    # d- S0 v& D7 f# L8 h
  152.             out.str(""); out.clear();
    & e7 N$ f7 I3 o- J1 }) ]* d
  153.             out << " Edge: " << edges[ii]->Tag() << endl;
    4 l# @3 m& ]9 f, g. p7 U
  154.             lw->WriteLine(out.str().c_str());" w/ J0 r5 {- u. M+ P
  155. 1 T8 ?+ P5 J  ~! t, w% f
  156.             std::vector< NXOpen::Face * > adj_faces = edges[ii]->GetFaces();' V1 w6 L8 n, H, f
  157.             out.str(""); out.clear();
    ( b! O& t; }, H& U) i, K: M
  158.             out << " Number of adjacent faces: " << adj_faces.size() << endl;
    ! k. [1 s9 K+ [! i4 H" h  ~
  159.             lw->WriteLine(out.str().c_str());
    ; Q" e; c; w1 P9 \' a2 `8 D! r
  160. ) r7 I! @7 [) G9 O& c
  161.             for( int jj = 0; jj < adj_faces.size(); jj++)
    & q6 z( L6 c& B: N' ~6 T. |2 _$ Z
  162.             {3 t9 [4 L6 ~  Y4 d' y
  163.                 out.str(""); out.clear();* q& E& i+ I2 y: d; `7 P
  164.                 out << " Adjacent face: " << adj_faces[jj]->Tag() << endl;% I$ {/ k! u% H; h7 ]7 x, w7 g$ j
  165.                 lw->WriteLine(out.str().c_str());
    ( I1 {3 G0 p1 I( h# s

  166. & W* n  w% b6 g0 ?( M! h. W
  167.                 myfaces.push_back(adj_faces[jj]);
      }# v; s2 c& ^+ r7 X
  168. 2 X+ D. W9 i* C9 C2 V
  169.             } // jj
    * G& k! N7 D0 Z) n* h8 X- m: G4 `
  170. ; a* T4 }4 `  f: B& n" R- r: C
  171.         } // ii
    & F, L" s1 I8 D' }1 o
  172. 8 V5 G& e6 O' V# F1 _# v
  173.         myfaces.unique();
    ) R! m7 P8 C! E+ W
  174.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)
    0 u! w1 d- [$ `3 t; E
  175.         {+ S' J$ \& c8 H$ J2 Y) D* c
  176.             (*it)->Highlight();
    9 v; a! _% o. Y& }8 j
  177.         }4 F) y1 q/ Q' ^9 Z
  178.         Gtac::theUI->NXMessageBox()->Show("Adjacent Faces", NXOpen::NXMessageBox::DialogTypeInformation, "Confirm");3 G6 }# H" ]. f2 C1 Y" f* O
  179.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)
    % X2 ^9 u2 E" s
  180.         {
    " N% {, T/ J5 I% {
  181.             (*it)->Unhighlight();
    , X/ S* E6 @! {0 J) _
  182.         }( I8 q; w" N7 o1 i( S
  183.         myfaces.clear();, w& ^, i7 J+ d

  184. ! |& M9 _, W8 H8 [( d
  185. 7 |& j! y, X  K3 k1 W
  186.     } // while) C6 J" X8 S6 v: M
  187. }
    ( ]3 F! Q3 Y# y! c
  188. % O3 f7 Z  n8 j, S) ?& R
  189. //------------------------------------------------------------------------------
    # o2 ]% |1 ?% x5 `" O& k
  190. // Entry point for our unmanaged internal C++ program- i+ O3 {7 N6 I* h5 A% p' L3 \
  191. //------------------------------------------------------------------------------1 n' [0 [! t; }: `
  192. extern DllExport void ufusr( char *parm, int *returnCode, int rlen )8 X9 A. W* Z0 L6 D$ V2 P
  193. {
    . B9 g2 h1 j' |8 _+ ]6 Q% F% d+ ~
  194.     try8 {  k: ~1 S. U' H+ @$ B  I
  195.     {
    / s# E" X( `5 @- U5 h( z; d
  196.         theGtac = new Gtac();
    " y# Z: d5 `3 p6 Y
  197.         theGtac->do_it();0 v6 t- O! ^* L& M3 J- a% B- b: P
  198.         delete theGtac;
    ' o: v& i) }7 A6 R$ ]+ I2 w
  199.     }
    6 F. D* Z/ Z! I2 a/ \+ T- o
  200.     catch (const NXOpen::NXException& ex). B. K* P! l+ y" U
  201.     {( E2 v/ ^' {9 _% `* X$ F
  202.         // ---- Enter your exception handling code here -----: ?  C4 K- G4 k/ M; j
  203.         Gtac::theUI->NXMessageBox()->Show("CaUGht Error", NXOpen::NXMessageBox::DialogTypeError, ex.Message());
    & U$ {& l$ @7 W- I4 l" I
  204.     }
    & y. m3 Z: f) Y  |# L
  205. }
    3 z, Q7 k) H& A
  206. " Z7 s# a, F0 w2 ]1 D

  207. 7 U' g& J/ {9 F9 r6 n
  208. //------------------------------------------------------------------------------
    ( M* p- e3 O. M8 w
  209. // Unload Handler# R0 _, H, I+ i4 ?% a9 O4 w
  210. //------------------------------------------------------------------------------/ @. P! z* y7 U6 ?
  211. extern "C" DllExport int ufusr_ask_unload()
    : L2 b3 f2 }5 |% p( n9 R/ _
  212. {3 G/ W) N( S5 A/ D1 o4 D1 f; t
  213. return (int)NXOpen::Session::LibraryUnloadOptionImmediately;  J" S* E4 l2 h- c5 f
  214. }1 \% d+ R. a8 z6 A, o. y% a: Y
  215. ; \* Q3 [% S/ \: d
复制代码
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了