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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x

9 N# X" n, i9 c4 C( y6 ~NX二次开发源码: 装配中选点,并报告父对象
) L% }0 {1 O5 P6 ]& q( q5 |; r5 I

  1. 2 |5 e- r% O# K% T/ }
  2. <P>Option Strict Off2 c; J" r: U- q7 U5 `
  3. Imports System
    ; q4 [" e8 Q1 ]. y0 t
  4. Imports NXOpen
    , g+ ?/ M6 |9 S/ s& ~" w
  5. Imports NXOpen.UF
    2 v% y8 ]7 H% b& t' V" T. o+ X0 n

  6. , ?9 T! ]. D( B7 k9 n) X: }5 ]
  7. Module Module13 G8 L% ~) n9 V2 b: e* V0 j- T
  8. 6 e6 D, O- X- a" S) y. ~1 X/ ^
  9.     Dim theSession As Session = Session.GetSession()
    % l; |) ]6 b; p& d) f0 c
  10.     Dim theUI As UI = UI.GetUI()
    , X- h! z9 d( u
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()' J) E& `: z1 \' q4 T
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    ( I. n/ l( Q9 `% q& p0 D: v8 V5 f) u
  13.     Dim pickedPoint As Point2 i% I5 u$ F( A) k: x" |! W
  14.     Dim myModelingTolerance As Double! V: m6 x0 e" x  z  Q. X1 c

  15. * M6 r- L9 L. ]! u- X
  16.     Sub Main()
    + E+ y6 W; p# A3 P  ^) G
  17. 4 C9 P# N4 z2 F% n
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)4 F+ a( ]9 H; ]( J
  19. $ E/ _# s5 d: r. t# K
  20.         Dim workPart As Part = theSession.Parts.Work* E* Q9 O5 ^5 p. O
  21.         lw.Open(); O1 d$ _  o- R! Z

  22.   M+ h& Z9 d6 y. F
  23.         Dim myPointTag As Tag
    : n. M9 r& K9 k/ E1 F, k* P. w

  24. ; l+ W- c4 g  F3 q" D: P$ ~
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    ! q: c3 P+ `4 l% M
  26.             Exit Sub# d# d" |) P0 \( `- _2 \3 k
  27.         End If4 j7 b1 q. q6 T
  28. / i7 y: F. K2 X( k& @* f- N9 o0 ]5 ]: s
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)% ]5 c" r) k* Y) o0 ^" C% ?! k

  30. 5 E" ?0 V+ _8 l& p' D. n
  31.         AskParents(myPointTag)5 \# F0 t& x% ~6 A6 [' K

  32. ! L' k* Z; Q# c6 \, ^9 P% J
  33.     End Sub. s& ]$ U+ t4 l# r
  34. 4 A; h6 _4 K; [
  35.     Sub AskParents(ByVal objTag As Tag)8 a. E2 \, S' I& W7 Y3 {

  36. / g9 a* r  e0 i4 E+ @2 F. f' h+ l
  37.         Dim n_parents As Integer0 E2 h! y8 E0 B# b7 r
  38.         Dim parentTags As Tag()2 l& n& y6 n% P5 D
  39.         Dim myPoint As Point( G0 Y: T3 p  ?% C% x+ L9 z: x1 x
  40.         Dim myEdge As Edge6 n! ?8 L6 i* e1 x! ^
  41.         Dim myXform As Xform' Q' e$ @" b/ a2 r/ A0 Q/ I" B3 a
  42.         Dim myTaggedObject As TaggedObject
    9 Q" _) |/ _5 o3 V
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    ! _7 B, I0 E6 `, P2 H/ C9 @* z
  44. & b7 u+ [+ T# H
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    % L+ x# u9 z5 ?+ A- b, m' r; @
  46.             myPoint = myTaggedObject' K$ a% _4 J& g( n) R
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)- f4 i8 {. B( _5 u( }; ~
  48.             lw.WriteLine("")2 Q- }6 ~2 n2 q

  49. ( O" G" z9 b( t( a) b
  50.             Try5 f3 Y0 ]  v# ~" C6 p! K4 }+ B1 a
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)* `& a1 L) w8 w2 m) K5 D+ [
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)1 m& z' x* q3 V, Q( S* k
  53.                 For Each parentTag As Tag In parentTags
    6 C0 d4 |+ O) b+ i, B* d
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    3 B* V% O, {2 n% k5 Y) ?
  55. + t3 }0 e+ H, Y+ Z8 y9 s
  56.                     AskParents(parent_object.Tag)
    & }8 w  c1 Q4 J- n
  57. ' k. N4 S% \( Y  U; f
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then3 q# }  y4 i; u# S
  59.                         myEdge = parent_object
    2 V3 v3 w6 A7 G
  60.                         lw.WriteLine("")4 y3 J: ]; G$ t% ^8 I2 v
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    : ]5 Q3 h  m+ Y" b( j4 ^
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    . N& q3 E% t8 q
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)) x" ~! l/ Z6 }$ v
  64. 2 Y( O8 i# ]/ q& ^% J
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag4 @- e7 `1 ^. z
  66.                         Dim occTags() As Tag
    , v7 V7 t1 `0 Y" m, J9 L
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    ; r  y+ L; I9 w# d1 x# B! H
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    / n- s5 _' C: v1 ^
  69.                         For Each temp As Tag In occTags
    0 F1 R/ p8 b7 v4 }
  70.                             Dim myComp As Assemblies.Component/ I* ~) y3 E2 T( ~% A- u
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
      ^' _; h3 X  q  S
  72.                             Dim myCompPos As Point3d8 C% _2 a5 Z7 \$ V  M# D9 h& R
  73.                             Dim myCompOrientation As Matrix3x3
    3 |  a) l: X( {9 q6 q
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)/ m( A+ p3 N" L' P0 ]* c7 s- m
  75. 8 Q, I5 l2 R3 t' o1 `* F
  76.                             lw.WriteLine(""): n8 ~' }$ q, B# \$ N
  77.                             lw.WriteLine("component name: " & myComp.Name)
    : P! L- ^1 z5 C& F5 s; e, T. F: X+ U
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    4 r3 t- |, K7 K) n& c8 B+ Y) v& N
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)5 r; u3 S/ O! l8 n3 z# g! V' V; u$ w
  80. ' {0 R% R' e8 ^* u5 Q
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then6 S7 W0 l" b5 P
  82.                                 lw.WriteLine("** This is the component that was picked **")+ I: ^8 q- ~! X# n
  83.                             End If
    0 i# _  C" n& Q) }( K1 F0 ?: I! Q
  84. ! ?8 y: z! q: B2 B3 F
  85.                             lw.WriteLine("")7 Y1 R5 ]% ^) K7 u. S
  86.                         Next: g  \& ~' n$ {+ \6 H5 n
  87. 7 m4 P; ~$ c, y
  88.                         lw.WriteLine("")) H! a$ ]& T; Z( a
  89.                     End If" @  n9 x. T4 \
  90.                 Next
    6 ?$ y4 y! |' s

  91. 6 f" s6 K) s5 U7 A
  92.             CaTCh ex As Exception& E9 U! W1 {" s* F
  93.                 ' NXOpen.NXException: Current object is not smart) S7 K! ?* a- H
  94.                 lw.WriteLine(" Error: " + ex.Message)
    / ^% x! e  y# q$ Z: h; b  q
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    ; c$ \+ A/ O* {: S, L% M
  96.             End Try4 K- U& ~5 N( @8 h! o
  97. " V" p# M# `: l- C$ x: t
  98. . U! y5 L$ O0 H1 o1 ?
  99.         End If8 Y' Q, D8 G2 X

  100. ! a: x/ o, R' w. ?' a1 R

  101. $ g$ ?- o4 d& ^3 ?% n
  102.     End Sub
    5 I# @, p& Z% u/ B8 P  ^9 d# @
  103. * u# T1 ~1 O9 l, \# g& L
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer3 q' u5 H5 ~( C  u2 p% H- i

  105. ( J9 V& f& T" K1 V
  106.         Dim base_pt As Double() = New Double(2) {}: s2 l+ y! f8 J& d- b
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    # |, o7 O. P1 W  R
  108.         Dim response As Integer = 0- U. g$ r, s! I2 o9 o' |4 L. ?) I
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt; Y2 j5 ?6 H: R

  110. ) v- w- T; m: _& b! T
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    ( E0 `1 b' O8 b, }
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    5 I# C* F% w; G) ]& l+ d* K
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)! l; j9 S" A$ I& V
  114. . s, v% t2 e0 d3 u+ K/ B& o
  115.         Dim pointLocation As Point3d
    4 T/ l/ @2 I) v
  116.         pointLocation.X = base_pt(0)
    * O# Q9 q# F1 V& L
  117.         pointLocation.Y = base_pt(1)4 r# ~) h( Z) I8 q; A; \* K
  118.         pointLocation.Z = base_pt(2)7 C, r9 i) |6 l( w
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    3 K( f4 t, }$ y' |) y6 F
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)3 }7 k& g* a. H: v
  121. 7 D+ J( {% C& Y" `" I7 D( w0 z
  122.         Return response+ j# ]  `  J3 h; U/ D% z
  123. : q3 s: l7 n& C! D
  124.     End Function
    + J! e- @) V& f& H0 f% F
  125.   g% l6 E3 W6 y( p3 z- i  ~+ D" Q
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response$ V) _; |- J# R. N7 r( M
  127. & T' L6 r7 f! y3 f# L( l! G& }
  128.         Dim selObj As TaggedObject
    ' _5 q8 [6 i3 q2 w
  129.         Dim theUI As UI = UI.GetUI9 ]) Y4 `/ u; ^6 x
  130.         Dim title As String = "Select a Point"
    % S! _' @1 A) |" z
  131.         Dim includeFeatures As Boolean = False1 d" b) S) Q2 F4 F
  132.         Dim keepHighlighted As Boolean = False
    8 H* y6 g7 T6 W; s/ ~
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    3 e' ~7 I& _; D& e+ ^9 {" {
  134.         Dim cursor As Point3d
    , @" `+ X" A: `) h/ N6 w
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly# t- q( U4 `% y/ |; d
  136.         Dim selectionMask_array(0) As Selection.MaskTriple+ l9 N0 G5 k$ S& T9 v1 j' T
  137. * h( t5 n# a  }+ `
  138.         With selectionMask_array(0)
    ; a. m' J: C# v0 e# \
  139.             .Type = UFConstants.UF_point_type/ J6 z% X; M; N% U
  140.             .Subtype = UFConstants.UF_all_subtype7 @" j- s! f+ ^# X0 O" s
  141.         End With, J* E% O5 Z( g1 w& e3 W; J

  142. & B% k9 Z) B9 T
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _6 q6 j3 s  W- z" k
  144.          title, scope, selAction, _$ S" j+ l4 M- Z2 Q& I$ F
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    " ~& `/ |7 o4 O
  146.          selobj, cursor)
    9 Z* i+ T2 e5 b. z. D3 I
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    % n4 B. X8 \1 ~
  148.             selPoint = selObj
    3 o( b( Z( J- Z' b
  149.             Return Selection.Response.Ok4 t- q0 ^5 {4 F; G
  150.         Else; F9 X, \4 N8 K6 E
  151.             Return Selection.Response.Cancel1 G4 F' b& g& S
  152.         End If
    ' |% }, S2 b2 e" z& |+ ]* D) f( Z
  153. " Q$ V* ~# t* K& L' K# ]
  154.     End Function
      m3 C6 y. d& j1 c9 D7 H: z+ B
  155. " N- [  r1 A8 u, E; k
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    , c" l' Z* |* q
  157. - t+ m5 s+ x/ N, m- d6 m( l0 h* _
  158.         Dim result As Double
    , W, n( @- `6 l. `* W) M1 @' L  s

  159. : Y, A6 H: [9 }1 q
  160.         Try) Y* U4 W2 o" X% k: b; r3 l
  161.             Dim nullNXObject As NXObject = Nothing
    ' W$ R% D. a4 N3 o, [

  162. ; R9 b1 i, y1 H
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder; U' g1 [( j! H" O* |  r/ }8 O. u
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    & ?  w3 J- R; ~  [1 n  _8 b

  165. 7 S! C( M  B" K
  166.             measureDistanceBuilder1.InfoWindow = False
    : k) K! x8 P2 Y! |- [
  167. / ]8 K: V- i5 G, [5 w
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum) d7 d) m! P; {3 Q' O2 S  F% A
  169. 5 f. h9 A1 u- P) g4 b. t1 W
  170.             Dim nullUnit As Unit = Nothing. M0 ]4 S& j6 G; a  o: k
  171. ) _0 Z% @  Y3 k; G3 O
  172.             Dim measureDistance1 As MeasureDistance
    + [) F' c  ?. s* F) X/ [
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)+ d# [& U2 V6 r) A: i+ Z
  174. 4 ]( E9 E; M/ a
  175.             result = measureDistance1.Value
    9 z% f4 r1 f4 k8 {$ t$ V7 Z
  176. & K# C; }0 `' ~7 F3 h7 n
  177.             'measureDistance1.Information()
    ; ^" }9 b2 F9 T6 _

  178. % Y: R! \! J8 h2 C
  179.             measureDistance1.Dispose()
    4 M  u. |" v8 B9 N( q

  180. ; U7 t# w2 W0 q$ c0 {; w$ @
  181.         Catch ex As NXException6 D# Z4 P( G# O/ H# C3 `
  182.             MsgBox(ex.Message); V* A! M# k! h
  183.             Return Nothing" e1 J1 Q) k7 t" r4 o1 N

  184. * x. p/ t0 s3 V9 L4 n6 d$ {2 T
  185.         End Try: D: s; ~" X5 B+ |1 \

  186. . c& x" i0 T' B$ e; R* S& ^

  187. $ [  k: Q3 a6 K0 m; f9 J! H
  188.         Return result
    4 I4 J. E: Q) M3 n4 h3 H. ]

  189. & D9 W6 R7 k7 ?- Q
  190.     End Function
    # C9 ~- t' d+ L  i7 S; D& s

  191. % v& _; l$ C; W0 g, R

  192. / p0 Q; q5 T8 J  K8 J9 b4 H
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    . ?' A; _* M+ K, W. L- s- Y
  194. : R8 K/ j6 E' L- o8 D- ^" k. h/ G
  195.         'Unloads the image when the NX session terminates
    / ]% s' Y( J% E& J1 }! f; q. _
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    - u' ?+ ?/ }5 k2 X) t
  197. 4 }  `3 X0 ~6 H
  198.     End Function- ^- s' \4 E# e: Q! l
  199. & ~& c2 A: H0 `& U+ _; _% K
  200. End Module
    " N7 q+ W. R5 Y+ l) r$ y& P
  201. </P>
复制代码
" M  ]+ L, t% `5 a7 \
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了