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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x

7 @2 G- M& }8 k% Q8 ^- P. w* KNX二次开发源码: 装配中选点,并报告父对象
" [$ N% g" {& x8 r# d4 `

  1. # o8 Q! H9 e" l) M5 m8 Z5 @
  2. <P>Option Strict Off
    4 h$ h" L6 z& M8 O8 @2 n; u# i
  3. Imports System& x! c$ i4 z" C' P* f# b
  4. Imports NXOpen
    : t' K. h5 q2 L' K& I4 O% m# F
  5. Imports NXOpen.UF/ [: o- `' @  {

  6. 0 N, \. g' `( [. t! [$ U
  7. Module Module1
    . j5 p2 S! x' r# {
  8. # o- w% r0 f: \0 p
  9.     Dim theSession As Session = Session.GetSession()/ }$ ]" Q* o: }3 n9 q
  10.     Dim theUI As UI = UI.GetUI(); C# x4 {3 @9 J* l- r/ [2 X
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()% L0 a) ]8 F8 t  S7 y4 D
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    6 p2 x- V" e5 E; U0 [  ~! N3 G
  13.     Dim pickedPoint As Point
    5 ]' M0 x" J- h. ?
  14.     Dim myModelingTolerance As Double
    6 |- Q5 m" H7 N8 d: {7 @

  15.   M9 A+ I; r) Y6 R  f
  16.     Sub Main()$ x/ N% R  L- Y; x" i$ S1 R
  17. ( O5 |7 ~2 L+ q5 Z# T0 a
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)9 A( o  b4 l' H1 R* _  G
  19. : D9 q0 }; P0 L! s) b3 h! K" B0 @
  20.         Dim workPart As Part = theSession.Parts.Work, M  i. k" N" {, U0 |' I  ?
  21.         lw.Open()  i* x0 _. ?( f. A. P
  22. 8 A8 `( l9 N% i! q4 O$ D5 u* o
  23.         Dim myPointTag As Tag
    , L3 L# L* Y. M; u8 F

  24. 2 B6 X2 H8 k4 Z* k" H; L1 |5 z! w
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then3 ?7 }8 F/ A9 H) r0 u* {' `; C
  26.             Exit Sub! f5 y% H* I6 e. b' o
  27.         End If$ @/ J+ C( L7 z
  28.   y) J# M. z$ I& A5 }
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    $ Q* a% B! }0 S: L$ ~* q

  30. 1 x. j; M2 _8 V# V
  31.         AskParents(myPointTag), ^8 [7 Q# t9 J* a2 q1 G) T

  32. & x0 `* p- v  K) h8 ~, g$ g
  33.     End Sub0 e  H+ h. c, _) |( Q  r

  34. 8 s7 ~; ]' K" h4 ?- I! e, e
  35.     Sub AskParents(ByVal objTag As Tag)
    2 u. D: f5 F& w5 ~9 }

  36. % S; f7 \& _) h: C* W* r  {
  37.         Dim n_parents As Integer5 C& l2 Y, E4 _
  38.         Dim parentTags As Tag()& ?9 h: n2 U5 Q2 Z# L# H* V
  39.         Dim myPoint As Point
    . \) T0 s8 v: R# b1 K
  40.         Dim myEdge As Edge$ b( b/ c4 i* }, Z! a
  41.         Dim myXform As Xform
    ; x- T5 b- q9 H$ Y7 \- w
  42.         Dim myTaggedObject As TaggedObject# C) C* l5 q4 S! ?/ j6 l) M1 U  y
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
      C0 w3 R5 c# B6 A2 V
  44. ) X+ P, x# ~+ ]
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then( H6 v) g& E& {7 C: \
  46.             myPoint = myTaggedObject
    5 f  |7 D2 f6 \8 y, ~
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)% C/ W: e. D- S, r* C
  48.             lw.WriteLine("")
    % ^& @  A9 P$ J  ^# b4 r: b1 z% H
  49. , r2 F" Q* z. g: U5 x' x
  50.             Try
    # v9 G; b- \3 V. [6 o; ?
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)/ q0 L  `' Z; W/ }
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)  F/ e( T; W8 \! \% D$ R
  53.                 For Each parentTag As Tag In parentTags
    . W8 a- h9 u. ^# L) g: f; X
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)# k; `0 l7 |* F  z  q9 w4 O
  55. , \% }/ K" C, I4 X; X
  56.                     AskParents(parent_object.Tag)6 ^0 R+ Y3 h4 b3 n4 |& F! g
  57. $ q2 E( M. i+ \' ~$ R! U! _+ F0 Q2 _! z
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    2 [, i  U! `/ p3 |0 n) ~" d
  59.                         myEdge = parent_object8 g0 X7 i5 R( {+ T8 B: U# G
  60.                         lw.WriteLine("")
    " v) x9 X( e5 G: y/ l/ v: p4 o
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)3 Y. Z: [3 x  m; [
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)) |6 C6 K4 x- \5 X
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)" t4 M6 D8 U! ~* E1 g/ S: G7 L; U) `
  64. 8 v. @1 r+ y& l  s- F
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag% u; R, `/ ^: a$ @5 ^: }/ M
  66.                         Dim occTags() As Tag
    3 a: Q2 h; b/ \8 J- y( I$ G
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)( Y% w0 s! G7 J- y9 I; ]. b5 ^+ H
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    9 d) {# m- G/ `! f
  69.                         For Each temp As Tag In occTags. M1 J% N- q# h/ G9 E) q/ K  f! H
  70.                             Dim myComp As Assemblies.Component, P, {% D) L% T/ G9 }
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    8 |9 _* J9 V. S2 s: z
  72.                             Dim myCompPos As Point3d
    + V: m9 l7 f& C) C6 G+ Z; V8 o6 D
  73.                             Dim myCompOrientation As Matrix3x3
    & q" O0 L# l! T, y3 x
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)5 K% G) z3 Q; d$ f  V& N

  75. % m8 d1 q  K; O1 p; \3 h
  76.                             lw.WriteLine("")/ x4 t5 i5 }( Y+ v; }% y1 U8 n
  77.                             lw.WriteLine("component name: " & myComp.Name)$ x2 `5 b& Q4 {7 s6 {0 b2 z
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    , o. K5 y9 G3 q3 d6 T* Y0 B% o
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)* Z- x" M$ ?! m) ?" u( ~: f

  80. ! v  I& H6 ^3 U* m. Y
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    ! K: G3 g! _: p/ o
  82.                                 lw.WriteLine("** This is the component that was picked **")
    7 T9 b& b' \) X/ i/ _, B
  83.                             End If
    0 ]: U9 E3 _) H) l2 r( l5 L- S

  84. 8 @6 c; T. ?0 @) q7 |, X4 B& y0 ?
  85.                             lw.WriteLine("")! T+ H4 M' M/ J& x+ L
  86.                         Next
    4 A/ ?2 l0 j' V; z! K0 S

  87. / D2 s. p7 x' P8 y
  88.                         lw.WriteLine("")
    0 u# {& X) Q- H% o. x: d0 h
  89.                     End If" b# ^& F7 p& m  E1 J0 o( Q: B
  90.                 Next/ ]7 K( a, o9 r! |. d" `

  91. : e1 p8 s! z, v7 o
  92.             CaTCh ex As Exception
    3 S) S. O# m# y2 V
  93.                 ' NXOpen.NXException: Current object is not smart
    0 B/ W% \2 T6 b1 J- P
  94.                 lw.WriteLine(" Error: " + ex.Message)
    # p, L0 F8 N: p& T2 t9 d
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    + ]/ p2 x5 W/ l0 \) _
  96.             End Try& h+ z3 l1 S6 f* V- N% K4 I( \
  97. ! Q/ Q7 i" q5 V- d* |+ S
  98. ' Z1 E$ n4 a- H( b' i
  99.         End If7 c  {) b- M6 ?+ H5 j
  100. ( C- ?! X: N* p7 N5 g- G4 |

  101. , U7 d- a0 `/ S
  102.     End Sub
    3 ?' `$ i- w9 i0 l* ]( j" Z; |

  103. 9 {. @5 s3 P0 _2 U) u* l
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer9 E$ [, d7 b0 h% Q( U9 K

  105. 9 V9 c$ T, k  B$ u% r
  106.         Dim base_pt As Double() = New Double(2) {}4 y  ?6 ], ^  `( i: C; Z5 Z, c
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null: E" t7 z! Z3 x
  108.         Dim response As Integer = 07 h/ h6 X8 R8 ^
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt3 y% U; s1 B( R( ^9 V6 g0 X) f
  110. * J2 y( Z/ o* {2 @5 p4 j
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM), E; h6 j2 `5 v; ~6 K
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    % g. S/ f: Q7 W' r" q
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    7 c. @' Z- O* c: S6 v$ ?

  114. - m5 e8 A$ V6 \; W2 S, }, o
  115.         Dim pointLocation As Point3d/ x$ a2 V! X8 s0 n1 E1 R
  116.         pointLocation.X = base_pt(0)
    . y" g$ T, U9 @# u' x
  117.         pointLocation.Y = base_pt(1)& D* R: T. ^, V4 C! L7 \5 {
  118.         pointLocation.Z = base_pt(2)
    4 \4 w' w+ u& d) t
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    . L1 E' _. Y7 V& |6 p6 h
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)$ t. t8 }% T- E+ {6 v' H* j

  121.   Y3 s# ]+ M, o7 y3 g  C
  122.         Return response
    + t7 n/ k" e/ j' D( R
  123. ' S5 o0 ^* h. o$ Z( m1 G  v+ e
  124.     End Function7 R/ A2 `: A& E  _; N

  125. - z7 x' f( F/ @& D* F
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    . Z: c: j- R8 }5 f' s, l

  127. * b4 Q0 O, f# n6 O  r
  128.         Dim selObj As TaggedObject
    ! _, J0 k4 \- Q, X0 y6 e
  129.         Dim theUI As UI = UI.GetUI" b; R. _5 z5 \1 K
  130.         Dim title As String = "Select a Point"
    5 x" h. o# F5 r# u
  131.         Dim includeFeatures As Boolean = False5 [7 h8 r7 s4 s/ K( s+ J0 H: D% y
  132.         Dim keepHighlighted As Boolean = False
    , g3 z0 u$ b9 Z, J% l
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    % v$ x% l  v' u; K' [6 V! G. M
  134.         Dim cursor As Point3d' ~. @8 S: C5 u
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly' E' h: n2 [4 D, ~/ ^4 t
  136.         Dim selectionMask_array(0) As Selection.MaskTriple  d3 \1 p/ S2 @1 \" @

  137. 2 t3 k$ B/ X0 U2 ^7 V4 ]0 N3 `1 h
  138.         With selectionMask_array(0)$ W2 w, \4 _3 j, b1 X) x% O
  139.             .Type = UFConstants.UF_point_type
    ( k8 _  E3 J* g% z! B- P
  140.             .Subtype = UFConstants.UF_all_subtype7 y, Y/ u$ u4 p( e: F
  141.         End With
    * a* v& W) q& M7 ^$ T

  142. * m, q1 L  H! c. m" N
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    * Y: f6 H& ~1 c) n
  144.          title, scope, selAction, _
    0 z1 u1 c0 n3 p0 F3 {
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    $ I0 ~) w; T) J. o% J7 g) E7 P
  146.          selobj, cursor)9 ~8 ~' H- e+ \" ~" @5 A# P
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    ; m6 H- h9 q, c
  148.             selPoint = selObj
    0 R6 a! o; k& t! G: k2 C/ O+ T$ E' \
  149.             Return Selection.Response.Ok
    . b0 t' z8 W* O! v' J7 T
  150.         Else
    / ^0 Y/ `/ G! D' [
  151.             Return Selection.Response.Cancel" O% |( W$ q: m9 a) G0 C  T
  152.         End If
    7 Q$ j: Y$ V" {4 U6 d
  153. 6 |% N+ i+ m2 f0 U2 v7 f. V
  154.     End Function
      P9 M  O( S7 q' R

  155. , M* V1 x) M9 K1 L% i& C5 t+ D, T" g
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    1 G2 a* y6 z) x* W7 Y( \

  157. 0 B" k/ @' w  ]: }, |- K
  158.         Dim result As Double
    3 G! F; t. P; {9 L% n

  159. % w" I$ H" D" w- \$ N
  160.         Try2 R% r) k( @, u) {# {6 V1 E5 z2 ~1 a
  161.             Dim nullNXObject As NXObject = Nothing% p/ Q1 a' j) g7 M7 K" X; h1 I

  162. . g6 T, p' Q  T3 O0 p: ?: F; N
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    ; r% c& c8 [/ |) o, a0 E
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)# j' h; y, F; Q
  165. ( l4 o5 _2 b1 V$ c8 S
  166.             measureDistanceBuilder1.InfoWindow = False- g* ~1 T% Y/ s0 F5 F4 k
  167. 2 m" U% ]2 S  }
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum2 L& m6 f7 @1 D  Z
  169. $ w$ H* _, f4 n/ a- U
  170.             Dim nullUnit As Unit = Nothing
    0 {; S9 q% ?. v' t

  171. 2 `5 `6 ]9 l# e0 E* L- v
  172.             Dim measureDistance1 As MeasureDistance
    / S8 `) l6 d5 o, G: e9 b, L
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2); R. S' Q  ]8 S; o. X. l
  174. 5 T( Z$ U( {. p* o! [
  175.             result = measureDistance1.Value
    - t, z. \6 y3 V9 @" E

  176. 2 x( g+ g8 Y8 }  z. ~  W' `
  177.             'measureDistance1.Information()
    ! U" J& }. F  r6 P

  178. & z, @4 @" [1 e/ B" }. w! a
  179.             measureDistance1.Dispose()$ j( e) \; {& n  {) t1 A
  180. % D! H! j# Q) h$ k* z8 C% E
  181.         Catch ex As NXException% Q& z# P' e0 [( Y7 _. H, K7 a
  182.             MsgBox(ex.Message)
    5 U( T; _( \! F
  183.             Return Nothing" B6 Q0 Z0 N  X7 }

  184. : w+ x; o/ Z) V0 Y' e' s3 X
  185.         End Try& J8 E9 v, T) v3 ], q" s% n. |

  186. " `0 s# L  N4 m

  187. * E, R; m9 |% u% q' T1 w
  188.         Return result1 O8 u* Q9 O( t
  189. ' k# x/ B6 u& i% M
  190.     End Function
      E3 [1 A; A% ?, \7 P3 L

  191. 1 P! X- b' F+ F% R) r

  192. ; f* t6 I3 _+ R& S' y( o
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer! ^( n! i' j7 ~! r  j
  194.   H$ @6 f7 O: U4 U. ~$ P
  195.         'Unloads the image when the NX session terminates
    - ~% i, V% B" b- w  y$ F4 K
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    / x- \! u1 V9 T- |* c0 p

  197. ; D% e8 c: |: g9 g3 m
  198.     End Function
    + g4 Y/ r0 L& m5 ?  V, O) ?
  199. 0 I% Y3 x" B6 J( b2 ~6 N% n/ P, x  j- v
  200. End Module( D+ A3 R" v1 f
  201. </P>
复制代码

5 x# @, D0 I' Y7 N4 y" O9 Z
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com/ 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 网址 www.diantuankj.com/ doTeam.tech
回复 支持 反对

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了