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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x

. S+ s: d, \/ X  NNX二次开发源码: 装配中选点,并报告父对象7 a! ^3 m: \6 Q

  1. + i$ [! G& a# N% p4 s7 l
  2. <P>Option Strict Off: D( n7 ~# k. I: _
  3. Imports System3 K' w& c) K/ `. {" y* G# f& J
  4. Imports NXOpen! |1 q1 D+ Q7 O/ c- c) h6 V5 F
  5. Imports NXOpen.UF# \$ I0 H- P3 K6 w5 u

  6. 8 C- p: Y% {( ?8 J
  7. Module Module18 |6 W0 Y* i+ F  l

  8. , N! J/ \* O9 ]# S
  9.     Dim theSession As Session = Session.GetSession()
    6 ~) O0 U6 Z2 |. i
  10.     Dim theUI As UI = UI.GetUI()
    ! F, b  T) D; ]; W; M
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    3 k5 W7 h( M, u* F  s" }4 y
  12.     Dim lw As ListingWindow = theSession.ListingWindow9 f8 `6 a! O: V! d3 \* @  X
  13.     Dim pickedPoint As Point
    6 N! y. n3 w; _# u% M! k0 L
  14.     Dim myModelingTolerance As Double- i( I1 ?) c9 ~2 v) Y

  15. % r, u, a& `9 G: ~
  16.     Sub Main()
      Q6 W  S1 }0 h7 l, H

  17. 0 M, W- E4 L7 D: u2 P! h2 W( y( h
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance): s% I' n0 a, @9 e* ]

  19. 4 [& e( p& i7 N* U- R
  20.         Dim workPart As Part = theSession.Parts.Work
    + O; m, W9 i$ Q. [0 q
  21.         lw.Open()
    % a& B2 V( d3 t0 @7 g, c& d

  22. & ]" K6 W7 p9 d* g
  23.         Dim myPointTag As Tag, X& H3 P) @! Z# K' Q7 v
  24. 0 n* q# ?+ X  K# R/ B
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    ' w9 Q) D( Z% P! C& U
  26.             Exit Sub
    . G* x& [0 @# [9 l
  27.         End If! H( f- u$ r6 y; |

  28. 2 q. W3 V0 ^! x8 `
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    9 V; D6 D3 {3 Y

  30. ( Z; D# [! M# e( I5 U$ H( A4 A3 Z
  31.         AskParents(myPointTag)
    3 r" P6 _# ^. D# G% z3 P

  32. 1 }* X1 B) ?5 [3 g9 w
  33.     End Sub
    # e; I$ c5 [/ j
  34. 3 Z3 d  k- J  T; n! l5 s
  35.     Sub AskParents(ByVal objTag As Tag)
    . _# h2 N: |  T3 _

  36. 6 i# P& y/ U  I+ r8 F3 g# c
  37.         Dim n_parents As Integer
    7 x  B# n1 N2 N( B6 o+ v8 W; @
  38.         Dim parentTags As Tag()% O* N' t! G# U  P8 |$ K9 c
  39.         Dim myPoint As Point
      c1 B& |* ?$ N' a/ r4 x4 C
  40.         Dim myEdge As Edge
    ; B9 D; ^7 d9 }5 q2 A) \
  41.         Dim myXform As Xform
    2 P* M  d: o8 U5 Y
  42.         Dim myTaggedObject As TaggedObject% a$ q0 c2 J2 v( l- D+ o
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    . v# K8 u. [  ^7 u# k. |( L
  44. 0 U. R; U. t1 _
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then0 |2 q0 i4 y3 |% ^
  46.             myPoint = myTaggedObject
    5 p) x4 e- Q9 `* [6 I
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)2 e: s# Q: T+ z" x& G6 e
  48.             lw.WriteLine("")
    3 g  A) s3 f. }; l, [+ d5 ^

  49. 4 A: V+ q7 v3 j0 x3 F
  50.             Try  l( n# e2 v; K
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    + A& g+ j# b. G( w; E' J0 Q$ F
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)% H9 W7 n4 O: J% S6 Z# ^
  53.                 For Each parentTag As Tag In parentTags0 s- Y$ R7 p$ I. ~: x' C
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    % Z  i  k, H/ ?4 u5 M" b+ z

  55. 8 m' U, d( i, x: g5 Z
  56.                     AskParents(parent_object.Tag): K/ C3 A; J* I  M

  57. * G; [$ b3 e" N6 U
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    1 C( R4 l4 Z5 U5 r  S, F$ h( `1 _
  59.                         myEdge = parent_object, }: \+ Q% W1 k$ R0 \2 U
  60.                         lw.WriteLine("")6 a' G0 O) u' Z. s$ Z' c
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)3 [; O) J& k% {
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    $ i: k' H: T7 o& q% @7 E; s" E9 \
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)" \! X0 P& H. Q  Z8 `6 `; @* `% g
  64. 9 V  F) {  C) e) I! _
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag8 b& `7 m  N! b# Q; g
  66.                         Dim occTags() As Tag
    / J0 a- ?/ L+ y+ t/ @! n
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    3 Q/ C! d& \) p% i# }. U
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    ; K& a' [& P  P
  69.                         For Each temp As Tag In occTags
    6 P- w( X, J' s6 G. T
  70.                             Dim myComp As Assemblies.Component
    ) ?6 f3 h% s& N: s0 F7 H
  71.                             myComp = Utilities.NXObjectManager.Get(temp)3 i3 T5 M; d+ s2 x: X3 @
  72.                             Dim myCompPos As Point3d
    / Q; z7 \: }% t1 C1 z
  73.                             Dim myCompOrientation As Matrix3x38 [1 q0 E; Y' o3 S  D3 t
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)- d' ~: m. D  O, _4 [
  75. ' g$ a; j; o4 a* U2 c' d3 t% I% x
  76.                             lw.WriteLine("")
    9 u/ w% l; k' Y! k$ Z; Y3 G  B  T$ p! \  ]
  77.                             lw.WriteLine("component name: " & myComp.Name)' |7 ^9 d5 z) b8 \/ N+ {* h, m
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)/ Z+ Y( f6 l5 |9 v6 F
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)/ t4 V. s$ [& X3 ~3 A4 _* q6 `5 f

  80. 5 a3 k6 b9 W6 F( s( Z
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then" |; O: ]! ?- C
  82.                                 lw.WriteLine("** This is the component that was picked **")2 g2 a+ b) a1 ?5 Z$ G5 N. `1 r/ u
  83.                             End If
    7 t; M1 @# H% c; C* q
  84. . a0 ~1 a1 ~* E# M) I6 ^
  85.                             lw.WriteLine(""): F# m4 \. \: a0 ?' R5 s
  86.                         Next
    ) V# j/ |# w9 f+ V

  87. - P" p; P, F+ ]4 |3 Z( f5 a
  88.                         lw.WriteLine("")6 O0 J" {/ V- Q7 ?( g# g1 ~
  89.                     End If% z8 E' r) ^+ q3 ^- a) l2 r% r( N
  90.                 Next, n% \- v) N& R- U
  91. " D, j( K7 C1 i1 {1 P- q
  92.             CaTCh ex As Exception: |" Y& |. S6 M6 F1 k. Q. W- r- j
  93.                 ' NXOpen.NXException: Current object is not smart& H% n$ h! J1 r6 p
  94.                 lw.WriteLine(" Error: " + ex.Message)( A4 R* j2 S8 V; _3 Y' c
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)/ c' ?+ O7 c+ _2 |+ k- m
  96.             End Try
      v* z, t- l" m% g9 s6 V8 d
  97. # d6 |8 N- U7 d8 o4 k& p( L, b; m
  98. / o) P# y+ R8 j, I+ A& j; S
  99.         End If
    . G$ I( ^# k1 v. F4 h5 _

  100. 5 D/ l5 u) b2 _
  101. * c7 |: F- ?" u$ D9 s
  102.     End Sub
    - v) v3 v; m8 v9 P. M0 G- p, S

  103. 1 K! E0 g& n' K  _  ^) M: n
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer# `; V% i! q' B! N) n$ k
  105. - ]% T, f5 N2 Y' w0 V, ~& E
  106.         Dim base_pt As Double() = New Double(2) {}
    & l6 f8 R1 k& i* s3 I. x* c
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    # h. m$ d. g1 W! ^
  108.         Dim response As Integer = 0
    ; @7 ?8 A6 E7 K; W. L
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt& m  E3 B5 J9 j6 `8 T" e, m
  110. 7 w) g! o9 Z5 ^" Z0 l5 n
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    ) h5 o  n0 R9 W2 }5 z" b% q
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)* Q- c. L- U/ Y4 I
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    - ~5 d7 a8 X8 O, p  H
  114. 2 k: l  R$ L" b& X# w, t1 `
  115.         Dim pointLocation As Point3d
    & m$ `2 V" ?' r$ i; r# c
  116.         pointLocation.X = base_pt(0)  a& F, D. Q7 T  j, @
  117.         pointLocation.Y = base_pt(1)
    8 {0 P3 J/ \( J$ E" g8 l
  118.         pointLocation.Z = base_pt(2)4 Y4 \6 e/ i' B9 }; A
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    9 q2 {9 Z6 j* Z5 n
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)/ ^3 [4 S0 [0 U7 \

  121. ; _7 b1 a2 J, Z& y. B7 h& x
  122.         Return response
    + w0 _' q6 m; Z$ t9 |
  123. 5 I4 ]& o7 s5 M9 G# R2 ~
  124.     End Function, |7 S* K: Z" J* E% I6 O
  125. ! v. H9 a0 n3 C9 f
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response( {5 H. R- b& z' Z* N% A
  127. 7 z3 A: O$ e/ }. g
  128.         Dim selObj As TaggedObject, C- d; [% Z5 x! \! B# W9 f$ o
  129.         Dim theUI As UI = UI.GetUI
    - t, n0 R. N/ b; q) x3 m# n
  130.         Dim title As String = "Select a Point"  s4 f3 D, \' T( f
  131.         Dim includeFeatures As Boolean = False1 d3 R2 ]$ j2 d+ m- m# Y% p; L
  132.         Dim keepHighlighted As Boolean = False
    1 k3 O1 G) h# D) J! j5 o
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    0 U* q, d) `; I4 r
  134.         Dim cursor As Point3d
    + [# m. q% J3 p  U" h
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    : O- o- k/ U( I. z6 B) R2 v
  136.         Dim selectionMask_array(0) As Selection.MaskTriple
    % \0 V6 t+ L6 |& Y2 O$ C4 ^( k% |

  137. ( R& Q2 r# z0 ]+ V+ y& \; O7 u. j
  138.         With selectionMask_array(0)
    5 ]& q8 ?' k4 W4 G( ^8 d3 ]
  139.             .Type = UFConstants.UF_point_type
    / y9 N- J3 W) O9 v1 v/ P# M# a
  140.             .Subtype = UFConstants.UF_all_subtype
    / P! c9 [, F* p8 z
  141.         End With
    / \* c/ O9 w  u, k2 w# t* f

  142. # Q. G3 D5 `# p, \
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    2 V- k3 M; G$ N' O; U! S" T
  144.          title, scope, selAction, _
    % a+ ^. g6 E8 l. D: A. o( G! G0 Q7 d
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    7 a# t4 m8 _! P8 R/ H: a7 K
  146.          selobj, cursor); J5 F3 G: X- P( w0 |
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    1 p; T; N1 G' y
  148.             selPoint = selObj& J, E6 @4 ^1 y; C/ T
  149.             Return Selection.Response.Ok
    9 L6 ]# e5 `* K5 |
  150.         Else. l8 t! T* g8 _- ^: E% E! L
  151.             Return Selection.Response.Cancel$ k* X- y- n8 T6 g$ A
  152.         End If% [! y6 I6 V2 J

  153. & m# i% m9 f& O5 D  M% |
  154.     End Function, U# ^/ G  P; f4 l: X5 {

  155. , c$ A# _! c0 j6 C5 `1 c
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    * k) @, Z  E4 {$ @0 E3 t! u
  157. ( W, P0 E. ^; |3 g) o: ]1 H3 W
  158.         Dim result As Double
    * H5 k- m5 s9 t' C) b

  159. $ S: p3 C5 |% s) t$ M/ W
  160.         Try
    2 Z7 f' g+ P  w% I% {/ j3 Z+ U
  161.             Dim nullNXObject As NXObject = Nothing
    , N1 v3 Z0 {6 C% ]
  162. . j! @' [5 S% X1 t7 V1 C1 b
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    / V' G$ X& y: k# }. `4 r: \; U
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    2 Y4 a$ Y4 p6 b4 k

  165. , A  K" p/ |$ g
  166.             measureDistanceBuilder1.InfoWindow = False
    5 Z% u5 @# q8 f3 P8 F9 G& k' n( X

  167. # Z1 O( I. G5 }6 w( S
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    0 h+ O  Y, x, I7 p9 u% n

  169. ! f2 @  g# }$ Z8 F6 p/ F
  170.             Dim nullUnit As Unit = Nothing5 v+ _, Y: o1 \9 c( I! g

  171. 9 |" o+ T  G8 P0 E' d9 S. q; c
  172.             Dim measureDistance1 As MeasureDistance
    9 w1 l9 f: L8 m1 y9 L; Q: s6 N# R# T% z. [
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)7 g- V% o3 R  l" u8 X- n

  174. 3 g& E9 g; e7 r  s+ m/ R
  175.             result = measureDistance1.Value! o' g, I( S" D- U/ J

  176. 6 w2 a' A/ T8 M  X! d
  177.             'measureDistance1.Information(). k& D+ k: w1 V" |$ P; x

  178. + ]3 j. G* h' T1 t- h! G; |6 D' |
  179.             measureDistance1.Dispose()& G/ H; b9 n! O2 T

  180. : p& O+ R7 }3 z( j$ b/ ]4 Y# [
  181.         Catch ex As NXException
    / [' O1 S+ n  j* `8 _
  182.             MsgBox(ex.Message), V! c8 l( w2 p6 }, _) a5 K
  183.             Return Nothing" G9 F% f0 |$ ?" Z% Q' A+ ?
  184. $ y4 ~3 f3 B3 [) H: M. T
  185.         End Try1 g1 }1 W: a6 {& k& W. O/ I

  186. 0 W% I9 f* b' ~9 c; `7 v
  187. , t  B8 ~9 j. m! m7 j7 V. P8 [
  188.         Return result
    + G% I- K/ `, l. C. r

  189. 2 S" G* x" B1 B' E. ?( R! D/ H
  190.     End Function5 p* A$ y' N2 H9 q

  191. 4 V9 Q3 |1 y, g4 ^* v3 W

  192. & m% \, Z4 ]4 L
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    1 w% Q" t0 O6 e/ j  m
  194. # K: J4 [0 K7 m3 N4 t. f+ D- n
  195.         'Unloads the image when the NX session terminates( v# q3 g' e) k, H. _4 V  O* k
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    7 i) [. M4 [) B/ {; |- L1 E, _
  197. + Z2 I& u% m; o" o- z& o9 D
  198.     End Function9 k' j) S, l- J  n  q4 G/ F8 `2 j
  199. $ a5 h4 {* }5 n* Z- k0 H' h
  200. End Module2 U3 l5 K8 ]0 l; a3 x
  201. </P>
复制代码
& K/ [& F" K1 \" b( a/ j( s
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了