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

[二次开发源码] NX二次开发源码: 装配中选点,并报告父对象

[复制链接]

2013-11-5 17:35:29 5662 1

admin 发表于 2013-11-5 17:34:33 |阅读模式

admin 楼主

2013-11-5 17:34:33

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

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

x
, d1 J: k) R& v) w. {. W) G5 L
NX二次开发源码: 装配中选点,并报告父对象+ b) @- q- L2 l$ z
  1. ) D" S( k3 A+ Z, K. R1 c: x
  2. <P>Option Strict Off
    2 P2 l) }  b9 |1 F) h
  3. Imports System
    : Q+ H1 p7 F1 M9 L8 W7 y. W7 u
  4. Imports NXOpen. Q2 Z$ M4 G8 @0 P' H2 V' F
  5. Imports NXOpen.UF
    ' g% ?) L( K8 J1 J) ]
  6. 6 `6 `- ?) b' M" Q( W
  7. Module Module1
    : `9 V4 g9 W* k9 d) {6 r
  8.   E2 d3 O- l& p- c5 Y
  9.     Dim theSession As Session = Session.GetSession()
    5 P( s; T) k+ l5 f: i3 a' o# d( D7 I
  10.     Dim theUI As UI = UI.GetUI()/ w  L% T. |& |: l" x  s
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    4 {9 y( y+ x' G. b0 j- T
  12.     Dim lw As ListingWindow = theSession.ListingWindow7 k+ ]  a) I- v
  13.     Dim pickedPoint As Point
    0 q7 W+ ~. m( o& {! K
  14.     Dim myModelingTolerance As Double
    * m( m, x) ^& g- H
  15. " O. i) F$ k$ O9 Z3 u( |+ @
  16.     Sub Main()" d1 E  I$ m8 n1 r+ M" h' Q& T1 g

  17. / ^- c) K9 Y& Z+ n* D3 _5 F
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    - P5 N5 I: f2 l4 Y

  19. ! y) ?) u- b  @! {
  20.         Dim workPart As Part = theSession.Parts.Work
    5 c- a, ?9 v! K9 s/ ?; O* y- [. \! S
  21.         lw.Open()' P3 \- {/ w) {3 W5 v9 K  L
  22. 5 w3 E3 W- v4 V" m
  23.         Dim myPointTag As Tag
    : i1 P  x; r! }. F
  24. # p1 @! @$ J3 W2 `$ R! c$ [
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then* ^/ a1 j( c' T
  26.             Exit Sub
      O9 I/ j6 s* f
  27.         End If1 I4 N0 r8 G4 q3 ^' d- b* y
  28. / A! }0 S; m! @2 O
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    $ l; [& m" b& x" B0 p) a

  30. ; B9 i6 X' w' d5 }0 `  i! w. G3 W0 v
  31.         AskParents(myPointTag), A: w$ C' @: m, ^" x1 x  H
  32. ) x$ U' \! ?  @+ z! O/ B" b
  33.     End Sub% K$ ^6 G: g8 S' n

  34.   O9 Q' P- f! y6 k  k; L& ?& R
  35.     Sub AskParents(ByVal objTag As Tag)
    * Y- [/ I! Y! }. n4 Z' X$ w$ o

  36. - ?# }3 A0 F& C# }0 e" R4 m+ W
  37.         Dim n_parents As Integer
    * `- e/ d9 N; i8 d
  38.         Dim parentTags As Tag()
    6 X5 g! T5 O2 ~. c  \/ g
  39.         Dim myPoint As Point
    , R1 r' G  }7 Q/ Q( h
  40.         Dim myEdge As Edge% r% @: g" Y6 q3 K6 L
  41.         Dim myXform As Xform
    / [+ ]6 x, y  t( a2 i" k
  42.         Dim myTaggedObject As TaggedObject) p: C0 \- m& _8 g& e+ r  o
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    # B0 d$ j. Z+ Q( C, n4 D$ `

  44. 9 B! @- P: @! z
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then0 H& B& m+ ^7 l) _) U2 H
  46.             myPoint = myTaggedObject6 m, F$ M# y* G# }& @' f
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    + L  M8 W, d8 P4 U* e
  48.             lw.WriteLine("")
    + A) |. X: k' U2 I. _
  49. * A9 o8 O- D+ @
  50.             Try
    5 B2 g/ q' |) ^9 D- [" {3 M/ h
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    2 {7 ~2 \# v8 X/ B; J; I9 F$ @
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    2 b* O# S, \* ?6 v0 @; a% G9 y
  53.                 For Each parentTag As Tag In parentTags
    5 K/ L# q; w3 k8 m& t9 b
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)  q2 |& z% }& ]
  55. 1 f  ~9 V5 ^7 _" G$ Y
  56.                     AskParents(parent_object.Tag)  Z3 n2 ^' c% Z' s* P. t
  57.   |8 u8 b+ K( {2 a2 j
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then: O- |8 }$ e8 g
  59.                         myEdge = parent_object1 U+ N! R+ k9 A  H0 y8 O: m
  60.                         lw.WriteLine("")
    9 [( J9 X% P# |/ _* m* p( H
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    5 H8 Q" \$ E6 P1 z2 C
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    , l; x) |- a5 J( o
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
    & k) |; e5 k+ z  f- c) j

  64. 3 w3 _2 J: G* e6 {$ n: B% ?6 y6 W
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    ; P& J! N: n7 [9 a- `$ m
  66.                         Dim occTags() As Tag/ r" A% }/ I* E  \) h5 f
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags); L) _. g. d, [
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)( H/ `4 C  q/ c2 ~4 S
  69.                         For Each temp As Tag In occTags
    % q' e: C9 ^% d6 J
  70.                             Dim myComp As Assemblies.Component
    / D( i6 A% A: h& l* B1 a
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    / p, b( c0 t* J$ v$ v
  72.                             Dim myCompPos As Point3d' c" F4 ?- p6 _$ H1 q
  73.                             Dim myCompOrientation As Matrix3x3/ g0 [5 \/ e6 H7 M( j3 y
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)( E4 B/ G2 ~, V+ X8 X1 D

  75. ! g1 E5 _- V  k: ]+ p
  76.                             lw.WriteLine("")$ o' p7 v. P4 g/ P! Q
  77.                             lw.WriteLine("component name: " & myComp.Name)
    $ X1 n3 u, [: {
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    ; k# h: k: B2 I1 a" K
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    & v9 f; f  }* Q; a" _
  80. - c# @" b  |$ h3 e( l% A
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    ; T3 [5 R. m$ w4 H3 h9 h6 o
  82.                                 lw.WriteLine("** This is the component that was picked **")1 w& J9 L. q# f4 |( I
  83.                             End If
    1 {- ?1 V2 J. t/ v9 M' L4 n: E! G

  84. 4 r6 V+ \) K$ e: |6 g3 `6 I7 v
  85.                             lw.WriteLine("")& x: I1 s/ m2 l4 ?8 `; S& A) T
  86.                         Next6 @' ?& Z* d0 a' O
  87. 1 A+ q9 W9 i8 Q2 h$ m, C0 F6 N
  88.                         lw.WriteLine("")
    , ?* Y. F( D  V
  89.                     End If0 f' S+ [* y* ^1 X
  90.                 Next
    : d3 B9 Q, K2 G) [

  91. 2 W" f2 J1 d0 v, S( v  F; v
  92.             CaTCh ex As Exception
      H8 X  r0 Y# o# {1 j
  93.                 ' NXOpen.NXException: Current object is not smart' r2 }* [* R$ x4 k* `/ f5 P; X
  94.                 lw.WriteLine(" Error: " + ex.Message)
    8 S. u- y8 O8 j9 p; u/ Y$ @& h
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)9 m0 o9 n' I; W8 G( w' D/ d! H2 g+ Q
  96.             End Try2 {# ~( D; b! q; U( `& T

  97. % B. h  }  \) D/ E* D2 M; u: G- N9 C" f
  98. , G$ T' X/ R9 g
  99.         End If7 M! H/ u8 F0 p
  100. 0 ~0 [1 N& U# w0 Z

  101. ) i: t) v1 T: z/ `) \$ _4 P7 {
  102.     End Sub: l3 g5 w# w& v% s1 Y
  103. 4 u, v+ i/ w. t. C0 g
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    5 G6 @' o/ Z7 p# s1 e- B( F9 O
  105. % N6 t: d+ [) ?  d5 E
  106.         Dim base_pt As Double() = New Double(2) {}# Q6 d: M! O1 {& \) u( v" a
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null0 G9 _' P0 V0 a9 a: ?% S
  108.         Dim response As Integer = 0/ V1 d% l8 a( W4 Z
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt0 N( ?7 w, w! v9 p6 _6 x) o" |2 D

  110. , F' B, |7 ~! i+ i- }1 j! X$ H
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)/ ]# J2 f' ?2 s! @' J
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)4 c2 h& v3 ]& V- Y, o* j
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)2 M+ d4 z- ^# e& P) f6 _

  114. ( ~" S. F# r2 W: x% U
  115.         Dim pointLocation As Point3d) P0 ]% D4 h3 W, R& b
  116.         pointLocation.X = base_pt(0)+ |: `& a9 y' h) b  w
  117.         pointLocation.Y = base_pt(1). g- L7 a* d$ z9 x! R
  118.         pointLocation.Z = base_pt(2)
    ( q# Y- B5 l" c$ N7 e; u& t. t
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation). R" v3 D! V2 p
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)/ x& H$ Y0 G1 w  s  j' b

  121. % D/ X& k/ t& @0 u1 w
  122.         Return response
    5 a5 L+ E3 O( t7 B- {6 R
  123. . ?* d. m; _0 i: z
  124.     End Function
    $ o  o5 t8 F5 ]4 D1 o3 S
  125. 8 d- s8 R0 P, q  |5 \3 M- _
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    ! u# ]1 |' ~/ K5 Z  v* {& r

  127. 2 q% m* a1 k% x9 c7 v
  128.         Dim selObj As TaggedObject
    # h2 r4 c7 }4 ~$ E
  129.         Dim theUI As UI = UI.GetUI% r. b9 b; c# e2 b% T' |
  130.         Dim title As String = "Select a Point"' h- M0 B/ A1 z+ M, ^: e2 h" k, x
  131.         Dim includeFeatures As Boolean = False$ @) w9 s0 W$ m; f  }
  132.         Dim keepHighlighted As Boolean = False# H  _6 N# }' N, N
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    9 e0 x  M+ D* z# O0 i
  134.         Dim cursor As Point3d% l$ {( s6 w& I
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly! u5 d$ j& f' c6 [4 {' I) k- ~
  136.         Dim selectionMask_array(0) As Selection.MaskTriple) f0 d( P. [. \0 x, y

  137. " i  ^/ k' W. @, L# s( l0 `7 t4 t
  138.         With selectionMask_array(0)6 Z" S% g1 c9 A& `+ `
  139.             .Type = UFConstants.UF_point_type/ _+ u& ~9 ]/ J9 _. H2 g* F
  140.             .Subtype = UFConstants.UF_all_subtype* i: `$ ~3 W! m8 D; Q" A+ y
  141.         End With5 [/ N! v# e) \# |* Q

  142. " @6 M- A* f6 L. j7 y
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    ; C: `8 F8 a0 z3 }  b; O  u# e' n
  144.          title, scope, selAction, _$ @' M9 p1 {: O6 z" b! h
  145.          includeFeatures, keepHighlighted, selectionMask_array, _, P/ X! A" q$ F( s! g8 m/ A0 L+ o2 f
  146.          selobj, cursor)
    1 X' Y  S4 j& ?( b( x% [; U1 `
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    ' c' f3 M8 E" x$ p  O. z
  148.             selPoint = selObj
    . o: ]# _& j. p
  149.             Return Selection.Response.Ok
    4 S4 u. f; N) B! J
  150.         Else
    0 d; x* N& d9 [1 E# k# k
  151.             Return Selection.Response.Cancel. B! x! |% ?) u4 Z: l  J) h
  152.         End If
    4 e1 q1 }- d; Z) y
  153. $ F0 F- |& [1 g4 x/ w% L1 x
  154.     End Function" @. r, j3 Q. Y1 k8 s

  155. + p/ m% E8 U# ?- D# x
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    % C$ l0 ?6 n0 f
  157. , r* w: O' b+ a. }! s
  158.         Dim result As Double
    # a8 X2 A3 {& N. P0 i) H
  159. - t! s% j. U4 h8 k1 U* S
  160.         Try
    $ e) _: m5 R. I+ z1 M; u
  161.             Dim nullNXObject As NXObject = Nothing
    8 E% |+ i$ E! R, Q, x! e# h

  162. 5 j' B! e/ G# f7 z( ~& d
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder8 \) ]' p+ a3 @! r6 C/ C
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)* b& D2 x4 d+ k1 V1 a

  165. , g9 y6 ^3 ~5 I6 d$ j, `
  166.             measureDistanceBuilder1.InfoWindow = False# }0 I; ?9 `1 U! G  A
  167. 5 b/ j" y6 n; \& S  Q! N
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
      j9 V" Q  ^8 M+ j
  169. ' {5 L7 [' G" l8 d8 I3 l: m
  170.             Dim nullUnit As Unit = Nothing9 y# d* j0 e( B/ ?8 ~  t, r0 W
  171. * \- ~$ Z( B) |0 A
  172.             Dim measureDistance1 As MeasureDistance4 u- I! T/ F  _
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)" ?: C1 c8 O. @' ~1 X" I  f

  174. 6 I+ U" T+ \7 Z% m- l  C; x
  175.             result = measureDistance1.Value8 e- \) v0 P" D& M; V9 @

  176. $ Z/ J# w% [# c7 J4 h) S' {  a: x
  177.             'measureDistance1.Information()
    2 w; Z  u+ f& ~2 y% G$ @: m) C
  178.   q% f5 o$ Z' D; _; ~& U8 s# {
  179.             measureDistance1.Dispose()
    1 c" {0 L6 E3 J, H5 T
  180. " @% W, W3 i6 z9 g, [
  181.         Catch ex As NXException
    1 J( {% w* j; W' i3 v; f
  182.             MsgBox(ex.Message)
      b8 R& t5 C1 ]* h. w3 Q2 v0 s2 u
  183.             Return Nothing
    5 `' r8 G, e. v

  184. . ?  p, C9 T: L4 D7 F
  185.         End Try7 u% m$ g7 [2 k9 W9 X  h

  186. 7 J0 |, q0 ^8 ]7 r1 E  b3 A
  187. 3 b2 h/ m$ P6 F5 x3 q: S2 V6 |  N
  188.         Return result
    # N- G1 d! T0 U" I5 e: g, V5 F4 c* p

  189. & p9 R$ j. J( f, H# M$ @
  190.     End Function& g, j* Z/ P% z- ]/ u; ~/ \% Z

  191. ' R7 G9 l& ^9 N' W1 @7 ]
  192. # e3 u2 n! ~" ?! p( v
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer1 _# U; @0 q2 K$ E
  194. 6 j5 b4 P) @0 _( Y7 K/ }
  195.         'Unloads the image when the NX session terminates
    : `4 ~: k) ?4 l/ o5 ?4 @) B; g
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination! j4 Q1 K. e3 o: H+ O7 t# b/ D

  197.   w9 s% a) {2 ?! P# h4 {
  198.     End Function
    # C/ i8 J4 U% R/ {9 c7 F$ o

  199.   M* r% V6 T9 b( a
  200. End Module
    9 W8 d( k8 T# e! Y7 y( J- j5 c  H
  201. </P>
复制代码

9 D- k9 O% E  l- S! T- w( M
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 doTeam.tech
回复

使用道具 举报

全部回复1

admin 发表于 2013-11-5 17:35:29

admin 沙发

2013-11-5 17:35:29

直接 跑下 Journal 就可以看结果了
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了