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

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

[复制链接]

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

2470

主题

1275

回帖

8万

积分

管理员

PLM之家站长

积分
82162
QQ
发表于 2013-11-5 17:34:33 | 显示全部楼层 |阅读模式

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

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

x

8 j# t2 i3 u# ~& s7 WNX二次开发源码: 装配中选点,并报告父对象
; {- `# o$ J/ ~/ h; t- m
  1. 7 L/ G, l5 `+ L# O+ H3 p6 |1 ]4 }
  2. <P>Option Strict Off8 c" ~7 O9 u# F" g+ a& I
  3. Imports System
    - B* M5 i; F; }2 H1 `2 ~5 l# q0 g
  4. Imports NXOpen
    7 s1 T) T7 ]5 `. V& w* ~3 k) m
  5. Imports NXOpen.UF& j. W4 G( V/ O

  6. 0 [# S* R' K' i" b# A0 i1 y
  7. Module Module1
    0 X7 E; V+ I$ e- \/ R8 m

  8. 0 p7 g; Z2 z7 i7 m+ i2 r  {
  9.     Dim theSession As Session = Session.GetSession()
    8 {5 c; t- X, @
  10.     Dim theUI As UI = UI.GetUI()9 L5 |  J; f$ y4 c3 b$ L( z
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    ) S* V+ k) G6 ?' x; p
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    $ u0 ]  T9 t5 ?7 \
  13.     Dim pickedPoint As Point+ y4 e2 P: `& v( E5 D1 e9 z, f$ q
  14.     Dim myModelingTolerance As Double
    2 E6 P& J' n8 u3 Z5 {$ m0 N
  15. : g( L) }; x1 E) |  U- x1 |
  16.     Sub Main()
    9 a% g# t# A7 o' z/ H

  17. % S) t4 _& v. M
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    ( c* m+ }1 R0 f, M( O
  19. ; b8 M3 N! j1 I2 h; i% W7 G
  20.         Dim workPart As Part = theSession.Parts.Work: d6 O/ U- \) c1 ]
  21.         lw.Open()
    0 V* P7 z1 m1 f" o+ `$ k9 {4 Q( e
  22. 5 h: o+ L2 a1 a
  23.         Dim myPointTag As Tag, }% B% _! W7 d% ~4 D' V' o* h# b
  24. 0 N6 t" x3 R1 H3 \. k7 s% a
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    3 V7 g3 v$ |# B5 K; `. Q/ e1 W
  26.             Exit Sub& n- ?( {. J3 e, z' M/ S* w
  27.         End If
    7 Z9 D* s( k- v
  28. ! I- Y9 a& r( S; e* H! e3 S
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    / A7 B! _- f1 N. \( q! I3 Y. \- `, K

  30. % G" [2 ~7 a' E  n5 Q
  31.         AskParents(myPointTag)
    . @8 k: w- m: f& Y  f
  32. 2 ?# X) M2 E  U" y
  33.     End Sub! |/ y& g4 H# w) A. [

  34. ( A9 p0 v# x; @+ Q: B9 a! {3 g% C/ D5 W0 [
  35.     Sub AskParents(ByVal objTag As Tag)
    + o; O  g9 y) ^/ k2 d, U& A. C
  36. # ^$ v8 f, d. B" u7 O2 a
  37.         Dim n_parents As Integer
    7 B6 f8 L3 p; O: `3 N
  38.         Dim parentTags As Tag()
    2 R2 T4 M; f3 k0 B1 [
  39.         Dim myPoint As Point% O8 _2 A% f& Y3 H" z1 y8 B! m
  40.         Dim myEdge As Edge
    5 S( x. K$ z# k( u2 u3 n
  41.         Dim myXform As Xform
      P- u* X* C9 c* Y: x/ t+ ]% I
  42.         Dim myTaggedObject As TaggedObject
    . q: q' Z; ]% e2 Y
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)$ F, d" Y2 e/ X2 g: |; H

  44. 7 e1 U; s# @, u& q0 r
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    ( S2 C2 e% Z3 F) [! I
  46.             myPoint = myTaggedObject. ?# z0 V$ N) l! a* k0 }& j
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)* ?& B4 K+ S1 D0 H* ?7 b
  48.             lw.WriteLine("")* J' m4 C  K  o9 T. Y3 e( P
  49. 1 H3 b! o4 \% }% W& Q8 T
  50.             Try) ?: ]# V3 M2 A: a
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags); n7 s- Y- d* ]+ P; j
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)( E' e1 u+ @! J/ @
  53.                 For Each parentTag As Tag In parentTags
    " g$ Y- D: m4 Y
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    6 H/ s( s% x# N& C; c

  55. . _8 A. n- P1 ?% f+ c" t  |& s
  56.                     AskParents(parent_object.Tag)
    2 v% _/ P# n8 ^2 `* A4 o. _

  57. % E3 j9 a% {7 U/ V9 ^4 j% l# q
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then0 g5 R& P8 i1 H3 K! I7 ]+ T
  59.                         myEdge = parent_object; b1 h$ E, S% v) T: g6 H4 T, [+ z
  60.                         lw.WriteLine("")$ z: b3 y$ ?% B' g1 h$ x( F
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    , q6 D0 B. Y* O. w* U
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)* J3 L% T) ~5 G# s) s8 w
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
    2 k9 I' Q8 v" x( X4 }
  64. / G1 F. }) k  `. f) z# t; c
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag" @. G& ^+ v- E8 Q$ r2 f
  66.                         Dim occTags() As Tag
    7 K* a9 a/ ^0 Y% L, Y3 e
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)6 I* ~8 L; S* z5 k
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    ' i. D1 W; S, P7 ~  g: m/ R
  69.                         For Each temp As Tag In occTags# D( a" a2 U$ k) X
  70.                             Dim myComp As Assemblies.Component. [/ w2 j& V# B6 L
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    5 t7 r( L, S! y2 l- a; L' C3 W
  72.                             Dim myCompPos As Point3d# }- Z3 C3 C1 C0 `: z5 ]4 |
  73.                             Dim myCompOrientation As Matrix3x3/ d9 w! z" M3 Y: d: n4 D1 x
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)
    + _8 [5 T3 u$ J2 P

  75. 6 e2 ~4 ^. ~! O3 `
  76.                             lw.WriteLine(""): v8 a- W2 m6 V2 ]; x
  77.                             lw.WriteLine("component name: " & myComp.Name)( @, A1 z- W% R9 b+ Y
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)7 s# W! P7 J( K( {* ]) D" {
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    . T) \! e3 ]+ k8 r# |1 E) N

  80. & |& M8 _, N1 m! c+ i. V
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then: l5 m7 D, \5 l8 w+ F( Q6 I9 X
  82.                                 lw.WriteLine("** This is the component that was picked **")
    & {+ T8 f" v3 _
  83.                             End If  c2 T) L  w$ g% Q% L( s. g
  84. # w% z) d; s+ s, \7 h( `% |7 ^
  85.                             lw.WriteLine("")$ ?+ h7 ]% T- e* u9 z) f0 h: f
  86.                         Next
    % y& k! H* O0 M/ ^+ ?
  87. 2 _" W- ?* X7 N' x& V
  88.                         lw.WriteLine("")+ o6 R0 I. ]" i% q) u5 w; _& }
  89.                     End If
    9 k( [1 B& p, L
  90.                 Next* A( f9 x/ A; B2 T/ s( v% s

  91. . z# R7 S/ w; G- T
  92.             CaTCh ex As Exception; d! V" H: M) x2 B( `
  93.                 ' NXOpen.NXException: Current object is not smart) P' V: j+ i/ U" ]/ v  {- ?; o
  94.                 lw.WriteLine(" Error: " + ex.Message)2 c  r5 w  e% d) _! x' j" }9 i
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)- D* ?) V* \% J# E1 ?# z4 z' T
  96.             End Try% U* Q. `& A% |7 @4 W7 k) A

  97. 6 \! O3 S, a; O) _2 u% L4 K
  98. ' x" G4 `- j' x0 T' K0 y2 N
  99.         End If
    3 Z/ M, E5 r3 a) d) o
  100. ; }9 U7 ?: b* \8 q0 w
  101. 8 d+ t5 J6 S( P" W7 r3 ~
  102.     End Sub
    2 e, Y! s8 s& U# Y' R
  103. ; n1 P. A( e3 K" S, w7 b9 D! F
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer* G+ ]+ A  M/ e2 ]+ J

  105. 0 u/ \; p8 W6 }& M
  106.         Dim base_pt As Double() = New Double(2) {}
    ; P3 k2 @- h# x, D% I
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null4 S6 h5 f" q/ {3 J* q+ f0 e# A4 \
  108.         Dim response As Integer = 0  Q& m+ X, C) q, x. x, n
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
    3 y4 p5 u' J8 p0 Q  t

  110. 6 j( ~6 a5 Y; U
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)' z' P4 Z$ }; j  n/ h
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
      _7 o4 C- K$ l
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM): t3 _9 r( R" x* k+ ?
  114. 3 Y/ q$ s# \) E9 E
  115.         Dim pointLocation As Point3d# R+ R, n: k$ ^6 w, R! x) H1 U
  116.         pointLocation.X = base_pt(0)
    0 ^. ~" B* @. V. @. S5 n
  117.         pointLocation.Y = base_pt(1)6 t# {5 Z1 L; I/ M+ d
  118.         pointLocation.Z = base_pt(2)% h2 M4 U7 E2 F9 {+ B- E
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    7 D% g( R9 z' x+ d% r) P
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    & ?5 |- W, ^+ M5 O% r+ z

  121. 0 f, k8 o5 z5 r( ~, ^" p( `: B
  122.         Return response+ g4 K8 q- s0 W

  123. ' F) E6 r* [  P# j6 ?0 H
  124.     End Function( _2 c8 C: T8 v! r+ k" k' \

  125. : ~$ ]8 h9 ]' }5 [/ a
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response8 f9 x* m  n8 l! {3 C2 a* u

  127. 4 ], s& w( v) _7 M  c& y
  128.         Dim selObj As TaggedObject
    1 s) x! e& B; a3 Z' M5 V% E7 I
  129.         Dim theUI As UI = UI.GetUI
    : t! l$ F" U# o/ m
  130.         Dim title As String = "Select a Point"; r7 G6 G' O$ A0 b  q6 q
  131.         Dim includeFeatures As Boolean = False
    6 C( B- t' l3 n' t, }6 f5 T
  132.         Dim keepHighlighted As Boolean = False
    0 d( }1 a% e  ]2 K
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    3 U3 c1 s9 J& N9 T3 C. o/ \& ~
  134.         Dim cursor As Point3d
    6 p# a6 E3 f' |9 d7 o6 |' F
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    4 P  A% e6 P0 F6 k
  136.         Dim selectionMask_array(0) As Selection.MaskTriple
    , ?8 h' r2 R9 b
  137. ! G2 v. D" \# E
  138.         With selectionMask_array(0)
    ; u% f4 }7 M, A" E
  139.             .Type = UFConstants.UF_point_type
    ' Y- c4 K7 T* g( a5 F- h  `8 i
  140.             .Subtype = UFConstants.UF_all_subtype
    ) P$ ^* V" d3 j) o8 g# H+ W- l' b
  141.         End With
    9 }% A$ [! ?1 Q0 u; M7 c
  142. 9 y! |" j  m4 q) N6 S* L
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    ( a7 i# t9 O1 t) t
  144.          title, scope, selAction, _, |: C! G! R2 }# z
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    0 l8 s1 p1 x4 p+ S! M5 u' v: x
  146.          selobj, cursor)
    3 H$ t3 E' \! {8 W
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then! I7 m6 A: V$ E& j4 v- P1 H+ o3 p" x
  148.             selPoint = selObj/ F0 O# ?+ K8 |* Y7 U) l6 x9 C
  149.             Return Selection.Response.Ok* y; Z8 Y! m9 Y# s; V4 [
  150.         Else% U2 J: n7 `4 T  \2 s+ `0 y$ ]1 U
  151.             Return Selection.Response.Cancel
    8 Q5 m% `4 G5 t, ~/ B9 ~0 d6 V# m
  152.         End If
    ! s' k- @8 j: t$ m  A
  153. ( E3 A$ Z6 ]) I; E' u' K
  154.     End Function; }/ r+ n* Z8 v- T* j
  155. & P. |- C6 h; e
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    7 n4 {9 r# X4 C

  157. 8 b- h8 \2 \# }- P- R9 {- o
  158.         Dim result As Double8 e" y$ g% K0 ^% g, ^
  159. 9 y3 L  e) c/ Q# E
  160.         Try
    2 Q6 p- z- o# U" M. E+ _6 q
  161.             Dim nullNXObject As NXObject = Nothing
    ! z7 _6 z+ J; `1 H0 ?$ [& Y
  162. " R' C/ I5 k) a3 M6 D3 p1 V
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    $ F6 O4 V1 i' x1 h+ |7 O8 `* K
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    - m! y! _7 i& M: h/ Z* H+ [! ]- R

  165. * D% r6 ~# Q0 H  Z$ J
  166.             measureDistanceBuilder1.InfoWindow = False
    ' Z0 B& \+ Z$ w# N
  167. 7 k) {& L3 S: A* K7 F/ Z
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    ; ^# O. C6 B" x
  169. - L, K: ?( _6 u. t0 g2 y5 u. u
  170.             Dim nullUnit As Unit = Nothing
    . e$ S' z$ g3 _) ?% _! A

  171. ' i8 \% m# j3 p, s: i" K5 h
  172.             Dim measureDistance1 As MeasureDistance
    7 E" c+ F- D$ X! J; @
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)& N; ~4 Y, J: k4 v
  174. # }/ T  \3 v0 c9 J6 {" R) O
  175.             result = measureDistance1.Value
    2 |# Z/ z0 l* S* d

  176. ( ~5 Z7 w: [6 @3 j% M
  177.             'measureDistance1.Information()7 a- b; M" z2 t

  178. 7 G! n8 L$ j, O0 U
  179.             measureDistance1.Dispose()8 f, R6 q; e  _5 h( [. [8 ~9 u

  180. % s$ g* A  k: E) {- }) F
  181.         Catch ex As NXException/ G7 J. y5 h& J6 ~, Q# ^( n% F
  182.             MsgBox(ex.Message)" [! A. t8 h- u+ f# {2 N3 y; r( F
  183.             Return Nothing
    * ^+ Z% p' e. l! F
  184. 7 |# l$ z7 y0 c, {+ T
  185.         End Try" n4 n9 Q! X) P0 b- b" [; K2 |
  186. $ V$ i1 q" S! a& G; d  x

  187. $ u* x5 V3 x0 S0 ]
  188.         Return result% r1 \* A7 [. I6 A. h0 A$ V

  189. 4 `; N/ y: U5 w' ?8 B& E* r
  190.     End Function1 x1 V- K0 L- Y$ F% o" h
  191. $ T  c& [* t8 z- f  b9 \" I* C, L
  192. ( n0 L# t5 J: s2 ~" z! n
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    - ~/ k1 S; [' v' F1 e8 W" Q- }

  194. # V  x4 ^: A! @- M
  195.         'Unloads the image when the NX session terminates+ n. e7 K" C' ~) i# u$ l0 ^
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination4 }- m  q7 F' f" x1 p

  197. ' A" E: Z5 E; \& H
  198.     End Function" ?+ K& ~. ]: X7 j
  199. 8 ?; U1 r+ @2 }" P! T" @2 R8 S1 U
  200. End Module  v5 J" n" B1 z, m& V; h( u$ d: `
  201. </P>
复制代码

( R9 O/ @  K  E) I; I- d) }" `, x
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 doTeam.tech
回复

使用道具 举报

全部回复1

2470

主题

1275

回帖

8万

积分

管理员

PLM之家站长

积分
82162
QQ
 楼主| 发表于 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二次开发专题模块培训报名开始啦

    我知道了