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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
0 ?+ B; v* D4 ?& ~! {
NX二次开发源码: 装配中选点,并报告父对象
3 N! U; n' d" I2 U- b" X8 u. u+ k

  1. " C  A: X: o% b* I6 O
  2. <P>Option Strict Off
    0 L/ T" O* {5 W& w
  3. Imports System9 s5 L* {1 C2 z( O* z8 i
  4. Imports NXOpen+ E2 J8 h$ I0 Z; r) s& t# D9 _+ T
  5. Imports NXOpen.UF
    7 ~0 W7 p2 t0 q- q& Z6 x* N

  6. 6 ]2 H' V1 ~- G* A% l
  7. Module Module1" K! w0 s" N5 b+ k& f+ I

  8. # T3 ^& v! `# ]1 t! G4 h
  9.     Dim theSession As Session = Session.GetSession()
    1 L' d; ~3 i1 d/ @
  10.     Dim theUI As UI = UI.GetUI()
      J( W+ u$ ?" k/ n1 d3 P. s5 t+ E9 \4 y
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()# O3 A9 G" V5 B5 J
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    : X, L1 u2 e; I9 N; q# J! k
  13.     Dim pickedPoint As Point
    ( U1 u: T5 W) _% W
  14.     Dim myModelingTolerance As Double8 x. f' f) \+ K$ W

  15. 6 c- c+ h* y- T" M" O3 V
  16.     Sub Main()
    % j4 V: y8 X' _. S' D( `; x9 i
  17. 3 g$ h% M) n& \, S) f
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    6 D3 X  x$ _: N4 }% y# F: ^- u7 q. Y
  19. 1 a) P& k4 `6 Z, Z7 W. j
  20.         Dim workPart As Part = theSession.Parts.Work, W( i8 u9 ~9 t, D$ P2 |+ }
  21.         lw.Open()
    9 x- O3 p+ E# p% a

  22. 0 C! H0 _+ ~8 q
  23.         Dim myPointTag As Tag, T# o/ n' e4 i, y8 r
  24. * ^. Z: F3 I- _+ e2 ]* u& S
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    9 T+ @* n2 O$ G- s) t
  26.             Exit Sub( o  m4 K( c: G! b9 G7 g2 ?0 Q
  27.         End If% P' f/ F" L( W$ C

  28. . {5 M# U2 O+ Q0 o' N3 j* O! X
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    + o9 s: d/ M2 Q8 v7 ?

  30. / E0 A: ]# m( f5 b
  31.         AskParents(myPointTag)3 |" S/ U( C9 \2 h0 ~# Z

  32. ' l9 S4 y% E, y: }! E
  33.     End Sub
    - G% |, `6 h- u9 R1 U/ N3 m: \; Q

  34. ) u+ d& D* K- U. V' N
  35.     Sub AskParents(ByVal objTag As Tag)
    7 T" g$ {% M0 L- I; I; d
  36. ) c! ^1 v# f+ n6 R: c
  37.         Dim n_parents As Integer
    3 ]1 m" i: K8 S: x3 q& @& J
  38.         Dim parentTags As Tag()
    * Y$ U1 @# P% P
  39.         Dim myPoint As Point
      S5 g8 k6 M- L+ e9 \+ n1 A* o- r
  40.         Dim myEdge As Edge0 K+ D/ L9 A+ w0 p0 }) e5 [4 z7 i. c
  41.         Dim myXform As Xform
    9 m7 D4 t4 Z% ]3 w7 ^& S: b
  42.         Dim myTaggedObject As TaggedObject
    ) a8 ^9 M5 ]6 a# F5 p8 z% n* x
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)$ G4 R8 k0 G# ^8 ]
  44. ! Z$ y" v' C3 T: i/ J7 }7 z- j
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then/ S7 A  K& T1 B" `& U
  46.             myPoint = myTaggedObject8 k# G1 g9 E. X  F2 h9 Q! d% j" P: b
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)' u7 r8 ?/ r6 ], f
  48.             lw.WriteLine("")
    1 C" S' {, C0 Y0 w* S3 I2 {- j

  49. $ K/ }1 A- ~# J
  50.             Try6 o+ n/ j0 K! j) s8 _) {- W
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)* V7 P. j9 @+ M4 R
  52.                 lw.WriteLine("num parents: " & n_parents.ToString), x8 t0 g8 o  q! {8 M3 |
  53.                 For Each parentTag As Tag In parentTags1 R; O4 N0 s/ i5 g, ]
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    $ i/ ?2 x: z9 a. p: y
  55. 7 c8 F) V7 A9 S  [- M
  56.                     AskParents(parent_object.Tag)* T8 ~, S0 h/ x. O2 C$ }4 ?
  57. 9 V" g2 k# N6 P8 G" B9 T( {) G
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    5 I5 ^3 S( m1 t# E: {
  59.                         myEdge = parent_object
      E+ u8 v, k3 w& Y7 I
  60.                         lw.WriteLine("")
    ( Q0 q5 z, u9 _+ Z
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString): s/ A" B% ]# U
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)( |) f: w& [# v( x
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
    . g( T+ x6 j4 A  A

  64. " d* U% u$ y$ }/ m  D# a
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    7 w2 U% V" I3 N7 K9 M8 P) R0 j$ _
  66.                         Dim occTags() As Tag
    0 P  x  ]- Q( ]; s4 x" q% V
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    * B, Q5 i; P$ n) Q
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString); V$ B- U- c& p, c& z
  69.                         For Each temp As Tag In occTags! ^# }) j6 P5 O
  70.                             Dim myComp As Assemblies.Component
    9 s1 P; z9 t. C; r
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    # _+ V( y% Z- F7 _7 h: d
  72.                             Dim myCompPos As Point3d0 W7 ~' F: n5 g; ]
  73.                             Dim myCompOrientation As Matrix3x3- }3 W, s4 Y  w. L! f
  74.                             myComp.GetPosition(myCompPos, myCompOrientation): V  R9 D( Q* j; }, w
  75.   i0 y! W) g' G! f8 H0 d8 V3 A3 j
  76.                             lw.WriteLine("")
    7 x" Z% Z* `, Q7 x2 v. B- @5 s
  77.                             lw.WriteLine("component name: " & myComp.Name)
    6 ^. T0 v9 {/ o0 f( K4 J& d
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
      d! L, E) X" R% E/ c
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)& f7 k7 M/ w" S; o1 c) }- N, M

  80. % I3 n0 \1 j1 X  P  b( k" m
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    : g3 Y( l( ]+ G" u4 L" h8 V) }
  82.                                 lw.WriteLine("** This is the component that was picked **")
    - T0 ^& |5 u9 U9 w, `; _; m% I" S
  83.                             End If" O( q; e7 I7 A+ L; m2 T
  84. 9 Y- p! V% N9 G/ k" N6 ?
  85.                             lw.WriteLine("")
    9 ?6 d% f( I$ \8 p5 V
  86.                         Next
    & u) j  p2 @1 n3 \. A; F
  87. 9 @8 o. i8 v/ o9 G: k
  88.                         lw.WriteLine("")3 ~( b4 M6 z7 z' B! ~
  89.                     End If
    + B6 a0 J) B1 t
  90.                 Next# ~8 A/ A/ d8 Z% j" d

  91. / U+ S6 k$ h" u) v( ^1 r; l
  92.             CaTCh ex As Exception, |! F0 V, _1 a/ L9 B$ f
  93.                 ' NXOpen.NXException: Current object is not smart
    5 L* g9 @! w3 z( R" S8 s
  94.                 lw.WriteLine(" Error: " + ex.Message)
    : R1 }1 v$ y% K" L+ U. f
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)7 \) T: c  `3 c1 ~1 r( w+ E
  96.             End Try6 {+ h8 l2 u6 a1 D/ G1 p- }- z! h

  97. 1 K. s5 D  q* s: {- N

  98. 1 O2 Y0 [3 B! l# o+ Y
  99.         End If
    ( ~; I0 r1 L% x# V

  100. # O1 w, }0 D9 w. x) g

  101. 0 v% P1 N& T7 C
  102.     End Sub: ^- P0 q! u: E- Y( E: ~: \9 i6 B
  103. * P  [. E2 _2 g1 X
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    6 _' d. {. W( e: r7 E% l
  105. * S- u* T7 j" C6 b) m" G, p8 `
  106.         Dim base_pt As Double() = New Double(2) {}
    : B- B+ H- d: j  k: ]
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    - H4 ?% x! F* r' I& J
  108.         Dim response As Integer = 03 i6 j# m& u9 f' }  y7 S0 p5 c. O- e
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt" _+ D1 d4 C* [
  110. # n. Q" s( }0 H+ ^# ~- n; v
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    . w" J- d' p( ~4 w0 Q
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    1 @" Y2 e+ p% C6 X, T; }# B+ M
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    ) y& M; R$ n3 U2 I

  114. 1 x  c; m, D1 D
  115.         Dim pointLocation As Point3d
    * H* g" N" N5 o. }: n2 i
  116.         pointLocation.X = base_pt(0)
    ) L- w7 j4 o# L' I# |2 m6 m
  117.         pointLocation.Y = base_pt(1)! N6 z/ Q* \& I' ]( ^* y, n
  118.         pointLocation.Z = base_pt(2)
    $ i/ A0 F3 t) b; w$ M' K# E  V
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)' M* `" i6 g2 s* A9 a
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)! s7 {- g: l, E( Z0 U- A

  121. " ~. t, Y$ N# X, W3 v. e. c$ a" k
  122.         Return response5 m$ a  x0 l: N! I
  123. 8 l- u- m5 Z% m' t
  124.     End Function
    $ |+ d9 n$ N% I& L

  125. 7 K- H; G, I1 e
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    4 {* x% D% B4 e  R3 n  K( b" x
  127. ( _% D6 ]! Z2 ^3 H
  128.         Dim selObj As TaggedObject
    . k- W: g0 \# Q9 i3 m
  129.         Dim theUI As UI = UI.GetUI
    $ g' |$ B  Y! j, K* M6 ~' t8 `# {
  130.         Dim title As String = "Select a Point"
    0 M7 x' B8 J2 y8 E
  131.         Dim includeFeatures As Boolean = False7 N& {, t1 `) i
  132.         Dim keepHighlighted As Boolean = False
    3 z# C; r  x% m" I' z6 Q# ^( l
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    7 ]- g; S& `5 S, [3 s
  134.         Dim cursor As Point3d: L; O/ \4 e& K! l
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    " V$ X1 r) `" z
  136.         Dim selectionMask_array(0) As Selection.MaskTriple9 E, c6 `, |( n  C5 [! E# _
  137. 3 _/ \3 R/ m  I2 z0 a
  138.         With selectionMask_array(0)
    # i) ~! z; ?4 _$ h7 e5 V' Y# I! X  j) w
  139.             .Type = UFConstants.UF_point_type* C; z( s& ^6 ^0 P
  140.             .Subtype = UFConstants.UF_all_subtype
    ) s+ Z# M7 m/ U7 ~. P1 h
  141.         End With$ h" E; ~+ Z: F+ [

  142. $ ~, H, P: H) J  r% W; r
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    + [, v7 D2 `8 _, `1 L5 U  i& }
  144.          title, scope, selAction, _
    ' J; I4 y/ a' |, h6 |, u
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    7 w+ P& G1 n& V$ _- u' T" j' U
  146.          selobj, cursor)
    4 r4 Y( t6 i1 Q/ Q1 q9 @! a
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    $ B: Q+ F$ X) @! F/ d+ q0 s) A1 A
  148.             selPoint = selObj7 V! {. X) K  I1 |# t7 I$ X4 p
  149.             Return Selection.Response.Ok% B4 ^7 \) q: s8 }. @
  150.         Else$ u/ `& @" _. f" g# @/ x
  151.             Return Selection.Response.Cancel
    & _: O+ \) `! W  f  z4 t# D  l
  152.         End If1 {) a& O9 ]8 l' s. v% j4 l

  153. ; H& t" O* d) c; i/ l: S8 I: W% J
  154.     End Function
    6 k! \3 O  `- D( o5 Z  G: m

  155. " z% c/ N* e/ f  Q
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    & q/ U! E& g2 |: Y

  157. 2 x9 a" C  q* F* V6 U) Y; H
  158.         Dim result As Double
    " f% K/ K; ~& \+ X- @' _
  159. 8 C" f* B- v7 n* q
  160.         Try' R9 }; t- M, V9 N. |- \2 z2 X. r5 b
  161.             Dim nullNXObject As NXObject = Nothing
    4 K( |! y) v8 @  _. Z  L- ^

  162. ; s# S4 x0 X* j
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder* x2 O# u" C+ r, [0 ^0 D  V
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject); Q: z* j1 H7 U5 s* x1 M% U; U7 U9 ~
  165. 3 ^- R8 ^$ P; m1 D  R6 F
  166.             measureDistanceBuilder1.InfoWindow = False
      k" l* w% w- f( }
  167. 0 C) ^# l1 |5 C, d3 u% A
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum# o8 P# N8 M: P- K4 Q; T+ v( }

  169. ; }  g  l- w3 X6 I
  170.             Dim nullUnit As Unit = Nothing6 f- y% @2 I0 s0 j- g

  171. , K- h' ^6 v2 q8 h0 l
  172.             Dim measureDistance1 As MeasureDistance
    $ w& Q8 J1 p' Q" }8 g: m
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    ! g" `" S, Y$ J

  174. % {0 D$ s, [+ G! A+ P, F
  175.             result = measureDistance1.Value
    ' }* N) B+ g9 {
  176. ' k* j7 n1 L( U2 W9 C+ j3 ]: ^
  177.             'measureDistance1.Information()% T( B2 s/ R. M/ z2 U

  178. % s0 J! D/ j0 _  E* o0 F! O. S, S
  179.             measureDistance1.Dispose(). w3 R; Z0 q- d
  180. " B/ i; b, h+ W4 s% X, X( c) h6 M
  181.         Catch ex As NXException
    ' b5 X! h' h( k5 o0 i
  182.             MsgBox(ex.Message)
    1 y5 ?$ H$ V" y% ]
  183.             Return Nothing* @7 b" D+ r& A9 _3 s  e/ C6 ]
  184. ( ~6 V/ Y$ K) [* H1 u* ~5 X4 X
  185.         End Try
    4 s0 Z/ O8 A& _" s; H; P0 S$ K
  186. ) D& l& R5 }' y) \5 }! a

  187. ) q6 z" k1 y2 _9 M: T- j/ X. h
  188.         Return result& n- _- H& ]- Y: L( Z+ M7 E% }

  189. * d: T4 P: h5 v
  190.     End Function8 ?, N& {6 p5 [* w

  191. % K" G- l# g9 c* _

  192.   V, Y+ e2 s: l: e
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    ' g) H1 x, N, i. |" w- a9 ]5 l

  194. 8 U0 q7 \/ M- a& c/ S
  195.         'Unloads the image when the NX session terminates' g% K  A. `8 g- D
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    8 S% E8 L  ^! [5 v. f  ?1 v

  197. : Z: F) U' |& ?4 p( n& p
  198.     End Function8 r0 H1 m! D. P) B8 w& p9 Y

  199. + D$ d7 t/ j0 E# e5 h' x$ p0 n
  200. End Module& C! }1 K8 M. ], j1 O2 |3 T
  201. </P>
复制代码

: G' Q# R/ v7 J
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了