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

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

[复制链接]

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

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

admin 楼主

2013-11-6 18:54:58

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

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

x
通过这个代码,可以获取选择面的相邻面的信息8 M7 c8 i: r& }4 x/ R& M' L! }
NX open C++中使用了 选择对象 selectobject 函数,通过面获取边数据,通过边获取面,从而得到信息!& V& n( @( |- w9 [

% W1 i- Z! E4 T' g- \
  1. #include <uf.h>7 J2 ?: }8 D  K
  2. #include <uf_ui.h>; \3 y: y- r/ F4 j' n* ~% W$ B
  3. #include <uf_object_types.h>9 F0 ?2 x$ t6 s9 g- `9 E$ M

  4. 6 E) p6 _% f3 ^7 L' a) Q; G- X
  5. #include <NXOpen/Edge.hxx>
    ( }# ?4 t& P6 }3 k  r% N
  6. #include <NXOpen/Face.hxx>
    ( L0 {$ Q! P9 ]' l/ I( B
  7. / b# j% ?% m, I. l9 X: d
  8. #include <NXOpen/ListingWindow.hxx>
    % s. n0 Q  _! b; |
  9. #include <NXOpen/NXException.hxx>6 @2 t- I9 s, {0 T; S& @4 }& X
  10. #include <NXOpen/NXMessageBox.hxx>8 w9 G3 G4 s1 ?1 a1 d* u
  11. #include <NXOpen/NXObject.hxx>  h, Y7 ?; [- f' u# K" d$ @
  12. #include <NXOpen/Part.hxx>
    / j) s* k6 i. l* e2 P! K& m
  13. #include <NXOpen/ParTCollection.hxx>' i8 V2 r3 a& D4 z7 _7 D0 p
  14. #include <NXOpen/SelectDisplayableObject.hxx>2 V) e- ^; |2 a+ J* H& e
  15. #include <NXOpen/SelectDisplayableObjectList.hxx>! e$ s  Q2 }$ b
  16. #include <NXOpen/SelectObject.hxx>0 n  [# w7 C3 i( x/ I2 w/ Y
  17. #include <NXOpen/SelectObjectList.hxx>
    5 l) y! S) m2 n
  18. #include <NXOpen/Selection.hxx>
    - S1 R% x8 V0 W- G7 b
  19. #include <NXOpen/Session.hxx>
    + b2 G/ _+ A/ {$ F1 n8 ?) t
  20. #include <NXOpen/UI.hxx>
    9 s( ?, W# X' r0 x5 P
  21. #include <NXOpen/View.hxx>  k" C5 d/ l) b1 ^7 A5 _" g

  22. 2 l( n5 N$ b+ Q
  23. #include <iostream>/ O/ b3 Y1 W  Q6 Q5 i1 Y9 q- \
  24. #include <sstream>1 o+ r( u0 ^! U+ `: J' A
  25. #include <list>0 E" t6 H$ P6 O( X, y

  26. / ?& t! w7 |; S2 `
  27. using namespace NXOpen;4 B; O+ O0 O4 }5 X1 e7 W
  28. using namespace NXOpen::Annotations;
    ! v! w' o8 q! l- h4 Q0 @
  29. using namespace std;: b2 G  K0 {1 h

  30. * z0 u- {8 r/ k2 I9 w2 m5 Q+ j% j3 B
  31. class Gtac4 {$ i" x! r" @3 m/ _5 h! Y
  32. {7 }! r  J" f$ F- O. n# a1 u
  33.     // class members9 C, B1 a  p4 L, |% B9 A
  34. public:' R+ A& Y" @. M- c5 I- A
  35.     static Session *theSession;
    ( x+ E4 o- F# d* ^- z: T, M
  36.     static UI *theUI;& Z8 ~, c, G; _9 i+ K

  37. 9 R$ f: B* A0 G2 u1 M, V. f
  38.     Gtac();8 P0 g# }* z0 j& v) g
  39.     ~Gtac();1 c2 S  y$ V1 a% S) [2 o$ u
  40. # i- A* N8 P2 H
  41.     void do_it();9 e4 O( G& D0 p+ v

  42. + X& n3 o9 c' ^5 |* c8 T5 ^
  43. private:& J/ T0 z2 j7 C
  44.     Part *workPart, *displayPart;
    . {1 K1 @, H8 c( y2 n6 e
  45.     Selection *sm;' y; R( X# Z7 Z; M3 I$ b& o# ^
  46.     NXMessageBox *mb;
    9 S5 }" A1 S8 U8 Q7 a) P
  47.     ListingWindow *lw;
    6 s! r- g. ^4 W
  48. 0 ?% R! J. t. a
  49.     Face* select_a_face();
    % M% D. ^4 h. [3 ~1 U( b8 ]) z

  50. ' i+ ~/ T! k% ]: g
  51. };
    - A: r- l- o- W1 ^
  52. 2 {4 Z0 \! i1 j0 w
  53. //------------------------------------------------------------------------------
    2 G7 f1 B/ n) [3 j
  54. // Initialize static variables' |( \# {3 \; W! y. n
  55. //------------------------------------------------------------------------------3 t! h4 a9 ~; _1 T
  56. Session *(Gtac::theSession) = NULL;
    + c2 {4 n8 f9 [
  57. UI *(Gtac::theUI) = NULL;. }/ \+ F1 }% g7 A1 Q. H/ ~3 w
  58. 3 P8 G) Y) Y( q7 R7 o$ j
  59. //------------------------------------------------------------------------------, k4 T( b3 ?% Z" J1 ?# x
  60. // Declaration of global variables
    & Z' q, L7 `9 K# ?( v9 I# m
  61. //------------------------------------------------------------------------------
    / @6 p! r, A: c
  62. Gtac *theGtac;6 ]( O- a4 V. c: s+ ?
  63. $ u$ U/ s. L, ]2 W+ S
  64. //------------------------------------------------------------------------------
    # T0 g# \; i! a) O7 \6 x3 `
  65. // Constructor
    7 A9 `4 b6 K  X& G" `5 V' ^  R
  66. //------------------------------------------------------------------------------
    7 _" e. i, d% e9 g
  67. Gtac::Gtac()& `2 y  H. a9 b7 z& T( G
  68. {# q/ @& S% \8 Y8 t' |) y# y
  69.     // Initialize the NX Open C++ API environment
    ' \& y7 A0 g3 o1 t, r
  70.     Gtac::theSession = NXOpen::Session::GetSession();) b2 [, ], l4 O  e
  71.     Gtac::theUI = UI::GetUI();
    5 ?' k; `6 z, t; C" z
  72. ' L! R+ F9 u/ Q
  73.     sm = theUI->SelectionManager();
    $ E! z( g, `8 W
  74.     mb = theUI->NXMessageBox();
    1 s& ]- V3 f. ]! j# A( S
  75.     lw = theSession->ListingWindow();2 q; N( p' P. y/ g8 x8 s
  76. 7 z; Y: W3 t$ l$ u4 v
  77.     workPart = 0;
    ) a" W' |* j& t( F
  78.     displayPart = 0;
    # e; W1 o4 V% Z$ @8 t- ~# v
  79. }
    3 S; W& ]- f3 N  ^
  80. % r7 b0 ^4 L9 [8 K6 }; y$ A" Y8 {
  81. //------------------------------------------------------------------------------
    % u/ L3 ~4 j; f9 B9 s9 t  [
  82. // Destructor
    3 A/ l4 H5 W6 l! m7 g
  83. //------------------------------------------------------------------------------# j& f# Q1 M" j
  84. Gtac::~Gtac()
    " Q0 Y: m( y! q+ F+ M8 V
  85. {$ C' n5 ]3 d8 ~9 j
  86. }4 s* t+ s$ N8 x% [: _* x$ ]2 j% _

  87. 4 S( ?6 Z6 o1 z) l5 }6 [/ R$ t
  88. //------------------------------------------------------------------------------
    3 d( H7 X4 f0 @; o' x
  89. // Selection
    , }: N3 m" x* H  G8 _. {$ {
  90. //------------------------------------------------------------------------------7 G6 [# A. s) h0 t" M8 F+ k$ {
  91. Face* Gtac::select_a_face()& r6 q$ ]) X; i, Y) e5 |6 W  E( ]
  92. {
    : A8 P. @5 o2 ~4 l( b* n/ c
  93.     // ask user to select a face0 E  h0 Y* K& B, o8 H
  94.     UI *ui = UI::GetUI();, k! d( }2 I  a1 a( g: S  }5 P
  95.     Selection *sm = ui->SelectionManager();5 G& I+ e! O1 H# m+ O5 |& ~! J
  96.     NXMessageBox *mb = ui->NXMessageBox(); // as of NX5
    ( u8 o: |) E* c5 T) t+ |
  97. 6 h( m. i% P0 U
  98.     NXString message("Select Face:");& z3 H3 r+ D, D; R
  99.     NXString title("Select Face");
    + o3 v( o* @# L; _6 O5 V7 `
  100.     Selection::SelectionScope scope = Selection::SelectionScopeUseDefault;
    * R2 @1 x/ O: H4 J7 z2 |" p; z. @
  101.     Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
    9 d4 B7 M8 p; ^5 V# Q, `
  102.     bool include_features = 0;( Q( [3 R. w& j/ k# g
  103.     bool keep_highlighted = 0;" l& W) b( A; Z% R( j1 ]& A; l0 ^- D

  104. % F! r3 R0 Y8 w4 ~' r! y; ^8 K$ D
  105.     // Define the mask triple(s)
    2 h" v7 n! {: @5 [0 m- M# r
  106.     std::vector<Selection::MaskTriple> mask(1);- t5 l. W. d: c6 o9 w& h
  107.     mask[0] = Selection::MaskTriple( UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE  );
    6 f/ @+ Y1 h& t8 s1 n: T8 o
  108.     Point3d cursor;
      T- ?/ H% z  C9 B! \" Z
  109.     NXObject *object;
    " b6 p, b% _. ~2 J

  110. - q: f( z3 G3 o4 A9 G8 K) O, D& l
  111.     // Select objects using filter defined by mask triples
    : h* E( Z7 }6 x8 [" Z  ~4 T
  112.     Selection::Response res = sm->SelectObject(" `' r3 D! e7 G
  113.         message, title, scope, action, include_features,3 \# Q2 n! y8 T" x/ J/ k
  114.             keep_highlighted, mask, &object, &cursor );- }0 b: l; z8 f7 _, }

  115. ( I3 D# \3 `. s7 z2 c
  116.     if( res == Selection::ResponseObjectSelected )' d/ ]  z% n2 v" o. C: w7 c1 a
  117.     {8 [: b. |! L. n$ g
  118.         Face *aFace;
    ; Y, L' r7 N. j& X0 z- ~
  119.         aFace = (Face *)(object);- S' m' v( q" |. T( L
  120.         return aFace;& K) m6 {. t7 ~6 j% ?4 j
  121.     }
    4 S, N2 p3 F. `5 |' k

  122. , @" I6 d+ h9 b( P
  123.     return 0;
    8 |2 ^/ k, V) c) Z3 y& S6 m& D7 Q
  124. }
    + X% i" m3 `% k% c

  125. 3 q& u: }! s3 c% C# f1 {
  126. //------------------------------------------------------------------------------; M5 i& b' f% X3 ^. F0 C
  127. // start with our job...
    ) T/ ?0 A2 T; i2 W3 \+ G
  128. //------------------------------------------------------------------------------
    ) u* V/ j4 y4 }  J( u9 s$ d- u
  129. void Gtac::do_it()- K0 K! b4 P: v7 p+ o: B
  130. {# N$ Z& [. Z' Q
  131.     workPart = theSession->Parts()->Work();
    8 k8 _: D% e8 p) e4 _) H
  132.     Part *displayPart = theSession->Parts()->Display();
    $ t9 }3 N3 m4 }
  133.     stringstream out;. Q. U7 B+ k  F& v/ l
  134. ' q* p& x% q, E
  135.     Face *theFace = 0;
    # \. B: x5 r" A- N* o* |5 c
  136.     std::list<Face*> myfaces;
    8 c$ \% c/ M: v5 `' N9 C
  137.     8 ]& F2 Y- g( \+ `
  138.     while( (theFace=select_a_face()) != 0 )$ Z: L% [& N+ b% {( B
  139.     {- w( t+ G6 ~3 R' y" S* X. q
  140.         if(! lw->IsOpen() ) lw->Open();# d! X. Y" R- v- G2 Z0 w8 c
  141. 0 j  z( W8 M" P( R& C2 z* W& H
  142.         out << "Selected Object: " << theFace->Tag() << endl;1 n- h7 L9 C! W
  143.         lw->WriteLine(out.str().c_str());9 `6 I" I2 c, o* V

  144. 4 {7 E, S' ^4 k. i
  145.         std::vector<Edge*> edges = theFace->GetEdges();8 \4 r$ S9 [6 o% q. O% h
  146.         out.str(""); out.clear();" v2 }  _: o$ l2 l/ n
  147.         out << " Number of edges: " << edges.size() << endl;
    4 [( H2 ]; f/ g5 B- t4 }- J
  148.         lw->WriteLine(out.str().c_str());- b5 r) Z9 f$ ~7 l

  149. 6 F- S. Z+ v7 V$ b8 D! j8 N& t
  150.         for( int ii = 0; ii < edges.size(); ii++)
    ' u" ~0 v) C8 k! ^" f
  151.         {) x" ~9 @; z% B) e2 `6 g* r9 M
  152.             out.str(""); out.clear();
    $ B0 J8 a4 p: o$ ^. |$ ~! O
  153.             out << " Edge: " << edges[ii]->Tag() << endl;2 f- u3 V; @: _! D- h4 T/ a
  154.             lw->WriteLine(out.str().c_str());
    . a  g* @$ R. Q4 y" _4 @8 z4 h
  155. " }+ f, G& i# b7 ?
  156.             std::vector< NXOpen::Face * > adj_faces = edges[ii]->GetFaces();
    ( `  {+ |+ u  r, w* X2 h& y
  157.             out.str(""); out.clear();0 `2 H% L; v" L; O7 J4 ^6 v" T
  158.             out << " Number of adjacent faces: " << adj_faces.size() << endl;
    % k. ~4 S0 _# o& y6 ]! U
  159.             lw->WriteLine(out.str().c_str());' Q# W0 M/ ~1 E& F
  160. : f) a8 V1 q' Q" b
  161.             for( int jj = 0; jj < adj_faces.size(); jj++): ^: V9 I' {  }& i
  162.             {$ U  s1 D$ e9 p8 M$ W* b
  163.                 out.str(""); out.clear();
    , L# N7 u8 p  w4 s$ Y1 A
  164.                 out << " Adjacent face: " << adj_faces[jj]->Tag() << endl;
    ' ?# C1 _( s  U, I
  165.                 lw->WriteLine(out.str().c_str());
    # q4 U9 _  Y( X, l

  166. 0 }7 f+ }8 x) w. l) e; q
  167.                 myfaces.push_back(adj_faces[jj]);
    6 L3 b1 U4 r/ B# i

  168. 7 u+ ^# R' J0 T1 x* Z/ q" l
  169.             } // jj
    0 \% {6 r# @+ H* v

  170. " }/ x/ x  I/ X0 `6 G! X" R. b9 Z" _
  171.         } // ii! _6 F  m. c6 p3 A$ O

  172. 5 W8 }) L/ L+ J# W8 g
  173.         myfaces.unique();2 a6 |0 y& n$ O
  174.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)9 Q  p7 A4 A) s/ w, B
  175.         {& m! o# i- k# P7 Q5 Q
  176.             (*it)->Highlight();
    7 ?7 {; P- k7 [6 {/ c& z, s) e7 q
  177.         }
    3 T  V! O- t) h+ f: N2 j" b2 f
  178.         Gtac::theUI->NXMessageBox()->Show("Adjacent Faces", NXOpen::NXMessageBox::DialogTypeInformation, "Confirm");2 V6 f+ Q% f5 J% Q
  179.         for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)
    ( u8 Q7 Z# L7 Q: W
  180.         {% G! b4 S. A9 m, y  h7 h
  181.             (*it)->Unhighlight();
    6 w  Z* \1 T% K& K9 i* z3 M7 y
  182.         }
    ; P$ I! A, B" ~+ Y) b- ?5 W- ?" q! C* U
  183.         myfaces.clear();7 X. ^8 {0 o, O! G: n) z: [

  184. 9 W0 m- ]- P" A. m+ ?$ r5 P, Y

  185.   |! {8 h6 R& Q0 q0 c
  186.     } // while9 O1 }! @: n( f1 X: N" |8 i0 ~# ^5 ~
  187. }) F: M( Y. H: T

  188. / x" V4 a9 a! N8 W0 P1 f2 |' t
  189. //------------------------------------------------------------------------------
    + U  P* R+ ^( U
  190. // Entry point for our unmanaged internal C++ program
    3 q3 A; ?: J5 [  i- ?; _" w9 \
  191. //------------------------------------------------------------------------------
    ( B! f' F; ~3 p
  192. extern DllExport void ufusr( char *parm, int *returnCode, int rlen )9 f! k/ H: M3 e4 l1 |
  193. {
    / w5 {, F$ Y; W: ?
  194.     try
    # ]: K; N) _# P6 Q
  195.     {
      M5 V  F3 g+ E! t1 K; C
  196.         theGtac = new Gtac();
    + N* L5 B( q  y  h% @
  197.         theGtac->do_it();) p" _) t' R9 L& q
  198.         delete theGtac;) t& w  c( ~6 V
  199.     }6 }9 e0 [' Z4 M% [9 B; f# `; s
  200.     catch (const NXOpen::NXException& ex)3 d9 b6 O& z: {! y5 \7 P* Q5 c
  201.     {3 n; w$ N$ k. \3 [
  202.         // ---- Enter your exception handling code here -----2 v: D5 c4 }, O  x
  203.         Gtac::theUI->NXMessageBox()->Show("CaUGht Error", NXOpen::NXMessageBox::DialogTypeError, ex.Message());6 W/ ]: Y( k% ~
  204.     }
    , D8 v7 C% Q" X
  205. }, N2 R- K6 z) h5 f  f3 N

  206. 1 A6 |- b8 z- @; h- L$ m
  207. , W. k3 P: [2 [' s# A9 I
  208. //------------------------------------------------------------------------------
    ' g  u) j2 u( N* K- {, L( f1 f9 c
  209. // Unload Handler+ {, w5 L/ ^6 {3 [- n
  210. //------------------------------------------------------------------------------
    # R+ f' v# ?- m* S! _# A, K
  211. extern "C" DllExport int ufusr_ask_unload()
    ) |- {$ r& l+ w
  212. {% D4 ^- c. X' D" }9 M
  213. return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
    7 v( @. f) s. X9 X+ Q  i6 q
  214. }
    " y6 \/ M" z4 M8 h& N' B0 [( N

  215. ) N& m: Y. K6 y( k0 ~% j% b
复制代码
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了