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

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

[复制链接]

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

2470

主题

1275

回帖

8万

积分

管理员

PLM之家站长

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

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

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

x
: `3 \6 H) V4 ~! D) b
NX二次开发源码: 装配中选点,并报告父对象7 [9 |1 n# }8 c2 H1 f& e" X
  1. ) j7 N% N3 X7 D' P$ S6 T. s
  2. <P>Option Strict Off4 Z' N2 L0 m- k; h6 j* _
  3. Imports System7 R& E0 A9 w7 l  O- P# s
  4. Imports NXOpen6 W: f6 y7 B# P* Y' E! A, m; g
  5. Imports NXOpen.UF; H1 E* e) F# g- ]( ~

  6. % r$ |1 ^6 W+ \3 ^/ V
  7. Module Module1
    : ?" w7 [- M# p$ m8 C

  8. $ y' r1 k& [2 x. w: L4 f2 {8 ^
  9.     Dim theSession As Session = Session.GetSession()
    , W2 {4 R' @# n4 Y2 f  l+ [/ ?7 Y
  10.     Dim theUI As UI = UI.GetUI()$ D7 P( Y& \: L, I8 _1 f" g
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()4 g, y1 B- I5 k0 D7 F3 {" n1 a# z: F% N
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    6 m& m! a/ l) |* s
  13.     Dim pickedPoint As Point% a0 N# ?4 R5 V% D
  14.     Dim myModelingTolerance As Double
    4 Y! O* R; }* w/ w' Q/ r
  15. ' a+ Y2 J1 V" |. q% M
  16.     Sub Main()( |2 C3 u, }; k. O; B1 [1 X3 t

  17. ( U& E- v5 J% x: ~
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)! Q# }/ k8 N, \: ^: i

  19. ; p7 g1 H3 ?& b) W% b1 v
  20.         Dim workPart As Part = theSession.Parts.Work0 B5 i) H9 e0 i* ~% h
  21.         lw.Open()5 a" [' e3 D# b9 ]; ~
  22.   ~' i9 V: C- q6 ^8 N# @0 u
  23.         Dim myPointTag As Tag
    5 u! Y# ~# j! m

  24. ( E: B" @! h, |/ m) P7 Q  E1 E
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then. a" j8 B) g; p$ ^
  26.             Exit Sub
    8 ~! D7 Z( z0 c5 _, ~1 ]
  27.         End If
    % D( F  Y; Z$ O' C2 @3 b* @3 r
  28. ( b* ]2 Q% a9 [* C. @; r
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    / I& E! F% X4 {7 L6 V" X. |
  30. ( ?$ J- {9 p8 i' o1 j
  31.         AskParents(myPointTag)
    6 X" I9 ~, ^; @! g* P/ d/ k) n

  32. & P! U6 Q/ X( p6 S$ g, W7 B
  33.     End Sub  e: w$ S- f) `

  34. + Y: [; X/ b7 C0 b2 H/ F5 s
  35.     Sub AskParents(ByVal objTag As Tag)
    6 Y" G5 b" {8 V7 T+ V
  36. / q& w' H# s  u( V( f
  37.         Dim n_parents As Integer3 ]  Q! [' a/ Z4 s) w* v
  38.         Dim parentTags As Tag()
    - O$ T8 H" f( c/ C1 s' b
  39.         Dim myPoint As Point6 N; P: n: L3 g, `
  40.         Dim myEdge As Edge, G5 h' D) J% m0 D1 h* V4 q& R
  41.         Dim myXform As Xform+ D3 R) a3 j0 R" f8 I
  42.         Dim myTaggedObject As TaggedObject
    7 G6 [9 q- h# |
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    % @$ A* w1 t( n' T; w9 `" M1 t
  44. 5 Q8 d+ i$ l! [7 t( ~1 _/ |
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then  C4 U( Q8 G; }+ R' P" s8 C) U+ A( y
  46.             myPoint = myTaggedObject5 e% s9 O2 y" ^7 m+ U/ U
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    : G6 [3 O; p5 _
  48.             lw.WriteLine("")
    $ m0 ^# C  {4 n8 N1 ~7 B8 H7 z
  49. $ l) j$ Y. S/ b' G, @9 k4 N, i% f8 A
  50.             Try* V4 L* _) F7 u
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    2 c# {1 y- D5 B+ g
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)5 i' ~: u/ D. @7 \
  53.                 For Each parentTag As Tag In parentTags4 J0 [' d5 }  f" W
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    8 z) ]' B3 k- a$ f/ d! z1 Z

  55. * d% l9 m! f) y5 c" T: W
  56.                     AskParents(parent_object.Tag)
    9 ]# }5 q- ?/ F/ B$ {) J

  57. " g; {# K# d- c
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    ) ]! J! I+ X3 U2 Y. V$ F
  59.                         myEdge = parent_object* u- @- B1 V/ a( j* I2 R" U8 f
  60.                         lw.WriteLine("")
    , @" U9 {' r$ H* ^! U/ Z& A
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    0 `# D, }; F1 K' x
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    9 k, `+ u% ]1 S' Y- }5 c( V% V
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
    ) S* }% X* H5 a: X! C

  64. 9 n3 L" J  c) z! ^
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    ( V. f9 a) K% h# @4 p
  66.                         Dim occTags() As Tag
    , J; Y! @! A& U
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    0 z7 P& Y5 X! l4 h) p2 |
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString), b& k/ Z% K/ T3 a2 _  ?
  69.                         For Each temp As Tag In occTags
    ! u5 Q5 n6 `& L* [  }2 e0 {
  70.                             Dim myComp As Assemblies.Component9 p0 D! [% B; T9 |& e
  71.                             myComp = Utilities.NXObjectManager.Get(temp)& _4 ^! t1 ?+ p  m% o
  72.                             Dim myCompPos As Point3d; B% A( [" y3 D
  73.                             Dim myCompOrientation As Matrix3x3, W4 K7 R+ |. [6 ?( P5 c6 a
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)2 C/ h( v0 T2 h- p5 Z* d9 q8 O" d7 C
  75. . T6 `9 B  O6 r6 _8 a: Y: _! H7 d& f" M
  76.                             lw.WriteLine("")
    7 B* L& S1 T4 N9 r) k1 J) ^
  77.                             lw.WriteLine("component name: " & myComp.Name)
    1 _4 y, H& s7 V: J
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    * N* c- `  R4 Y4 l1 L
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    ; P3 Z: H0 |4 x% B

  80. 0 I5 P* R6 Z& x* F
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    3 I  q6 M: r: |1 M! f4 y! ]
  82.                                 lw.WriteLine("** This is the component that was picked **")/ C: l  t& c! {5 d- p
  83.                             End If
    6 Q( W7 V' ?% L& G% j- f

  84. 6 G9 W" r5 q& G/ u
  85.                             lw.WriteLine("")$ r9 N( ^5 L2 I. s" J( U
  86.                         Next
      c5 q% W. v0 ^4 b
  87. 7 n2 Q" [2 j$ G+ s( _
  88.                         lw.WriteLine("")
    ( n& E- P9 a! D
  89.                     End If0 g& }, Q4 k  A
  90.                 Next- K# O* F) B8 p! s7 K+ s

  91.   M0 F' R' ~' |; g
  92.             CaTCh ex As Exception4 Y9 l9 ?  Q( ?3 b" ]9 B, A  ]# u: ?; S
  93.                 ' NXOpen.NXException: Current object is not smart
    ( T8 `( [- P, e
  94.                 lw.WriteLine(" Error: " + ex.Message)
    * ~, E" i/ P1 U: C
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    / p9 e$ ?4 Q( J! I
  96.             End Try1 U" i' ?5 D( O! _
  97. , K# @7 A% z1 e6 U, M" x

  98.   ?8 n5 g$ L% X& x
  99.         End If
    4 P. Y" d8 ?% c0 f' s3 R
  100. 2 \2 l1 o. k" |- J
  101. - a  T  d6 i7 d5 B9 L# ?% p5 [
  102.     End Sub
    " v$ @) o; f3 p0 i1 g# Y
  103. ( E4 g, c2 ?. w
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer' u2 T8 s5 O8 Q/ B

  105. / Z6 d2 Q7 ?1 J/ v1 j3 X- m0 f  p
  106.         Dim base_pt As Double() = New Double(2) {}' c( j2 V0 r2 v4 Y
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    $ M  a* m8 ^" j. r* W3 `$ W" {- l) N
  108.         Dim response As Integer = 0+ }6 i) M& m2 i3 m% H
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
    : B: A6 _4 l, ~

  110. 4 |& Q2 R/ K% t3 R0 ~
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    # z6 E6 B% g8 M/ J
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)) p7 [- c2 F- A3 ^
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)/ O6 ?! ^; C# k
  114. ( A3 ], I8 I, O: g2 M% e
  115.         Dim pointLocation As Point3d
    ; Y0 _( `1 K' Q7 S  ]" T" g2 r# I
  116.         pointLocation.X = base_pt(0)* F7 t1 A5 N# h( x/ B  `
  117.         pointLocation.Y = base_pt(1)8 `0 r5 O, E: `& J- j
  118.         pointLocation.Z = base_pt(2)' W0 }3 a! i, h  v/ G
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    + w6 f& y5 @2 ~/ @3 J, u
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible), x$ q( i) P" s" L# a5 T

  121. 6 r9 o2 z" P2 a( d$ ^& U, Q
  122.         Return response
    3 X) \7 i9 J/ M" |, E) \& ~/ C

  123. 9 ^* D; O; \% k- C- \: ?( l6 D
  124.     End Function( P0 m% R9 C; y5 j* T% c0 c
  125. % O5 `! d& C$ l6 U) Z6 T% J
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response2 ~; O+ W8 l/ ]- q4 Q" j% T5 d
  127. ' l$ [4 N" U. j9 C
  128.         Dim selObj As TaggedObject* @9 O: y( K$ G) l  O  D
  129.         Dim theUI As UI = UI.GetUI- H$ j+ ^( k$ I- c1 i0 ^
  130.         Dim title As String = "Select a Point"9 A8 ]; Z2 s; {. R
  131.         Dim includeFeatures As Boolean = False
    7 D6 ^" _; H* r* I5 a
  132.         Dim keepHighlighted As Boolean = False4 Z3 k/ z: v' a/ a
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    ; V1 O0 J3 T9 L) ]
  134.         Dim cursor As Point3d
    & Z; n- U& p# R3 F" x/ i6 X
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    ; d( f. Y# H" }  p3 l
  136.         Dim selectionMask_array(0) As Selection.MaskTriple
    + L- u5 j% k% U0 R/ |# L
  137. * t" n' @& s2 k+ n3 b" d  L4 w
  138.         With selectionMask_array(0)
    + n6 z7 G) L1 }) `, f
  139.             .Type = UFConstants.UF_point_type
    0 b: P  @' H! O4 }7 H) K. u4 o
  140.             .Subtype = UFConstants.UF_all_subtype
    $ A  I& E. @6 E! M! m
  141.         End With( @5 _% d+ Z( _
  142. * a3 N- }# E% e# U
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _, _, a# O8 p0 _; u  n2 |
  144.          title, scope, selAction, _7 q" d' m- S! {9 J9 J
  145.          includeFeatures, keepHighlighted, selectionMask_array, _! R4 w- t& D4 H7 E
  146.          selobj, cursor)( E/ S; \) N7 y7 T* C
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then1 O/ \' N0 D, f5 v  Q, _  f) C. ]
  148.             selPoint = selObj5 I  B+ Q* K0 j; e
  149.             Return Selection.Response.Ok- G: g4 j" U+ D! l6 }- [& V
  150.         Else! H& V4 X7 S1 @1 A9 b; R
  151.             Return Selection.Response.Cancel
    0 `+ y- _" k8 D, W7 K7 i
  152.         End If
    3 ]) |8 @$ e, G) c# O
  153. - w. E5 {3 ~" e0 f- m. i
  154.     End Function
    : z3 e( m: b! m" l& K# i1 P
  155. ! L5 }. U5 W4 |+ w7 C
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double( w+ [  W0 B' }! K
  157. ; P' [, Y( @6 ^+ G2 J
  158.         Dim result As Double+ \% A) G  M3 ?- w7 A

  159. 3 C. s2 `4 T8 e# t8 h9 Z
  160.         Try5 I, e: n" q, }; m
  161.             Dim nullNXObject As NXObject = Nothing: }8 K4 q' h3 j+ k
  162. + Y: l: r/ p6 o4 _0 P3 e3 U
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder; E7 w! L" G3 M
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    & k! }* |6 r4 N- I) S7 V3 S) K
  165. 0 I  {. z! r' ]# e5 i2 M$ s
  166.             measureDistanceBuilder1.InfoWindow = False
    0 V7 k) y/ g' @) B. k7 I

  167. 2 s. u' s. _2 J( A" w! N$ @
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum) k0 Z1 h! N4 G* V4 M/ \$ a5 Y
  169. 6 F0 A5 \( M$ P. @: i9 K
  170.             Dim nullUnit As Unit = Nothing8 D' Y7 a% s) m. q2 W5 [
  171. : e: r" u: L4 J% C1 W, P$ x9 x  t
  172.             Dim measureDistance1 As MeasureDistance
    2 a# V- G; V$ k6 @- J& N: I
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    3 p, _' G  k- b

  174. . s5 T+ B+ x$ f
  175.             result = measureDistance1.Value7 ~' r" `- O. Q2 q8 S" H* Y
  176. 0 p. o/ L5 ~! n$ ^2 {+ f7 A
  177.             'measureDistance1.Information()
    ; v, `) |& c3 W# ]
  178. 5 @2 G( l5 D: L$ |* n( H
  179.             measureDistance1.Dispose(), l) B( [+ _7 D) Z4 e. f

  180. 2 \" F) s3 g- S1 I$ l# D
  181.         Catch ex As NXException
    / P, v6 I+ v+ F6 t: e
  182.             MsgBox(ex.Message)4 l! _' @/ j; W
  183.             Return Nothing5 U" E# T$ u8 H( Y8 v
  184. , t! S9 r) |0 D1 h! D  p* G
  185.         End Try
    ( Q" H" r4 i* }3 V, U; ]4 ?
  186. , W5 ?0 |" X: H1 \* Q

  187. ) z, g9 h# }9 s$ _) b
  188.         Return result
    0 N6 s3 R6 a$ ^0 f* m2 C$ |
  189. 3 n  g' ~& H5 @3 P9 M' r$ Z/ P
  190.     End Function
    & ^8 e5 a6 [" ]1 s% c7 T- h
  191. ( J/ B" y: H: [, A8 k! O8 T5 Z
  192. % d4 n* I! c/ B% E6 o# X4 N: y
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    1 l, b- E5 e/ `; y% d5 F

  194. * P# h0 \! `/ e: z6 U% z
  195.         'Unloads the image when the NX session terminates
    7 v; D* d/ Z+ _# \( U9 M
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination% }' W2 B! J% k. R. u& {  E
  197. : d" @* }4 d( o- Q( Q( C( M( x
  198.     End Function
    ' E; v8 O8 c+ \3 P

  199. 0 |* V/ C) \7 q# \
  200. End Module
    7 l! i" w4 y" q
  201. </P>
复制代码
9 z+ I- V! a) Q8 U! Z& S
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了