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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
! P5 C' l3 ^9 w9 z- |
NX二次开发源码: 装配中选点,并报告父对象
( W* P; d+ f5 B

  1. * {9 k0 q! b$ H, J* W
  2. <P>Option Strict Off* c) p$ V8 J9 s3 t: k+ |8 Z
  3. Imports System. j- w2 m$ u5 G" V8 S" F8 R
  4. Imports NXOpen  g' b8 P9 y. R, p  a0 M7 _7 i; w
  5. Imports NXOpen.UF) ]- g* c" O$ D/ R/ F: F

  6. : A7 i; \% n9 `+ ]$ j
  7. Module Module16 C; T0 w( }0 \8 _% G% s$ m

  8. % ?' K! ?) A0 T7 o' Y
  9.     Dim theSession As Session = Session.GetSession()3 f6 A9 Y) ~% v9 t$ s0 U3 x
  10.     Dim theUI As UI = UI.GetUI()% [% D$ e$ D* a4 c' Y9 ~- @
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()5 b, o+ O: d4 C$ R
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    5 ^: I2 V0 K- i
  13.     Dim pickedPoint As Point) n7 r3 S3 \" {9 G
  14.     Dim myModelingTolerance As Double" z' f6 U2 I; _8 e

  15. / e8 p: B% S6 F. X4 x( T
  16.     Sub Main()
    5 G  v) Y" k4 p3 E0 P

  17. " O) l% R" }& U5 V' Y( n& ?' {
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)- J( m9 z  U7 n( R- y: r9 ?7 L

  19. / H6 Z6 N8 r$ R5 ~# U2 S
  20.         Dim workPart As Part = theSession.Parts.Work
    # E" R* v* w; p/ P2 S
  21.         lw.Open()
    # w- V1 B% q4 H1 A; g
  22. 0 f$ U& s; l% v% G
  23.         Dim myPointTag As Tag
      E; h6 K7 E- Q! a: H1 E

  24. : s3 {5 B4 m8 p; ^1 I% w6 A
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then9 l3 @; h/ G" R3 w
  26.             Exit Sub6 O0 `( A5 N0 L, b* K% D
  27.         End If7 H# ^8 q- w& O4 h3 D; G+ |* Z+ m3 v

  28. 8 r( C  R2 P" l, g6 O. G5 @. @2 K/ |
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)5 I5 ~, Z; A; c
  30. 6 T( _& n! S9 P
  31.         AskParents(myPointTag)
    % a; H% G" `5 A9 b) z0 k: V
  32. $ s, V" B! k4 L- O# ^) F
  33.     End Sub0 p; A  x5 \( S0 r$ \
  34. ! o2 O; t9 I. s5 L+ m
  35.     Sub AskParents(ByVal objTag As Tag)9 V: ~- B! N: b" g7 _! w! t  O- c
  36. & q8 N2 F' M& w0 w* [4 A
  37.         Dim n_parents As Integer2 P  F( n) o: ^5 I1 X
  38.         Dim parentTags As Tag()
    0 D0 j! ]/ u: w9 m0 w+ _% W
  39.         Dim myPoint As Point
    ( _% g8 A" A/ G: }
  40.         Dim myEdge As Edge
    ; V- A$ a( r0 Q* b; I8 {# g
  41.         Dim myXform As Xform
    : y, B; T0 e3 E; u2 ^7 {- T. U- m
  42.         Dim myTaggedObject As TaggedObject
    : ~0 |3 z3 s7 M5 r1 o
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    8 `& B* C" C' L+ G3 O

  44. 8 t. U: T# n5 s
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    ( q% Y1 t: Q/ w9 o3 K
  46.             myPoint = myTaggedObject4 w5 I3 ]0 |" \
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)6 v" q2 p- X4 @9 c" _
  48.             lw.WriteLine("")8 j4 {% \7 B: }7 q
  49. + {! y; F1 c9 S. b7 x
  50.             Try, m+ w) T# Z% ]' P
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    $ I# x" X( p3 D5 g( C
  52.                 lw.WriteLine("num parents: " & n_parents.ToString): z# V5 g  e6 O2 F
  53.                 For Each parentTag As Tag In parentTags' F6 j7 _1 o9 z
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    # u$ l' X6 s3 t2 \" G( e9 q# N

  55. 1 Z; `- {2 T9 g5 N7 G9 V/ L
  56.                     AskParents(parent_object.Tag)) E7 _4 B3 c4 }$ J! R) F' N5 E

  57. . V+ n( a$ d8 h8 m& W8 e# X" |, A0 ~/ B" L
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then* i; r0 z( C- _4 m0 ]& f
  59.                         myEdge = parent_object
    + r5 K% `: P; k
  60.                         lw.WriteLine("")' u$ q: |  y; L
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)& d  y) B/ m- Z0 g6 Q' K: W
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)8 E; e4 R7 C  E+ i
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
    + a5 j" d  F8 F1 a' t, E4 \3 s
  64. 1 W. z; }( ^+ }- R# I
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag+ c/ m/ t# _4 |
  66.                         Dim occTags() As Tag
    , k+ ^4 n; |, `7 I6 L. }' k' @
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    ' Z: T* {& k9 x: A: q. h' k
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    ! Q" J; f. Q/ A. }2 R" a9 O$ t& |2 g  X
  69.                         For Each temp As Tag In occTags
    8 _# D# B  S* [  Z$ D2 \( o" M7 s
  70.                             Dim myComp As Assemblies.Component' x7 B9 F2 C$ ^- M
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    4 y% q0 U. `) }: E
  72.                             Dim myCompPos As Point3d: k5 P5 T6 h+ s  ?/ q
  73.                             Dim myCompOrientation As Matrix3x3/ Q9 c. ^& t3 |
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)
    7 ~0 v% K% U/ v2 F
  75. , C8 \; {( W* w: u4 W+ c* K5 e
  76.                             lw.WriteLine("")/ {+ D# c- \; d% q7 j
  77.                             lw.WriteLine("component name: " & myComp.Name)7 l9 U3 P! [8 l9 D
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    * w1 {1 l" ~) a* s- w4 t9 `$ N; g
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)$ x9 @" a, o0 y& H; J( C

  80. 4 R" P: p; U1 B" j! o7 s* [
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then) A, E1 f# f1 ]. G
  82.                                 lw.WriteLine("** This is the component that was picked **")3 n. z) K: U( B6 E5 V
  83.                             End If
    4 v) J; l) P3 d6 w
  84. 4 J, z2 k, V' }' r6 m8 S; y9 }
  85.                             lw.WriteLine("")
    7 I0 v: N! h9 F2 e) ~
  86.                         Next  E# w) m* m/ I

  87. 7 Z# ^2 ~: s4 [: H
  88.                         lw.WriteLine(""): O0 u3 D5 L1 c
  89.                     End If# r8 U, K* i( j* p8 Y& s/ q
  90.                 Next
    9 g6 T2 @. z: j" d+ C# Z

  91. ! l8 Y# @! e6 @; z
  92.             CaTCh ex As Exception
    ! u5 U- m7 Q& _: R5 ]0 K5 `
  93.                 ' NXOpen.NXException: Current object is not smart8 T% P* _9 h5 f1 Z" e5 N! O
  94.                 lw.WriteLine(" Error: " + ex.Message); _8 l0 `8 ^) X9 y+ k3 l, p% X
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString). z9 Q0 h2 X2 U- [/ H/ x) w
  96.             End Try5 x2 y! D( [- q. e
  97. - x) ]0 D" c6 q) j8 {3 i4 J

  98. " q- f6 L0 u7 |
  99.         End If
    9 h$ i% D6 j) t% E8 [6 N

  100. 2 e" [/ ~( C6 P9 ~$ q' a% E

  101. 1 S/ N' ^6 G6 F4 w: _/ `: R. q
  102.     End Sub4 b% V' Q7 _( j3 n: o% |7 ~
  103. # c$ q& v% I3 @! ~5 t
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer6 _5 I5 a3 H+ f6 O, [
  105. ( ^0 k% L2 O9 x  p
  106.         Dim base_pt As Double() = New Double(2) {}
    3 o) E" x: x& E' |0 u9 C
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    & p5 g5 B" H# l; ?! f/ L# W+ \* J
  108.         Dim response As Integer = 0" H+ y, |0 e0 o. F
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt% h  {9 C" Y8 p) k9 Q

  110. ! D$ u( ^2 v% y4 w; h) l" K6 @
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    ; e/ A" r8 t; _
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)8 w7 q; @) ]1 V
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM); C% b* o4 Z5 g3 G5 V! `
  114. . p* h3 b; L9 @; d
  115.         Dim pointLocation As Point3d
    % F6 U) F" M, D$ q, Y$ u
  116.         pointLocation.X = base_pt(0)
    ! n+ r7 Z, L1 E* o' m
  117.         pointLocation.Y = base_pt(1)
    7 {' e+ k  A# K) C
  118.         pointLocation.Z = base_pt(2), `- v, \  J) I8 P* l
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    ! @( h/ X* S) O! V, }9 V
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    4 T8 B7 m; [+ b& l8 i8 o
  121. * [1 u; P% y2 V  z4 X8 w
  122.         Return response% p! }, f# {( {# K) ]- e

  123. + S" o5 g  O! `& Q
  124.     End Function
    # h( M  q3 M& O0 ^+ b$ F) ?, D
  125. " D+ m% o! J, @9 L% y  B6 p  E! ]
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
      ~! @% {7 u1 V: Z# V2 h+ N! d

  127. 4 G' T$ l) w0 {, {- R
  128.         Dim selObj As TaggedObject
    ! Z( s) f: y* E. P. a/ n
  129.         Dim theUI As UI = UI.GetUI
    * }5 _" b  f* j
  130.         Dim title As String = "Select a Point"
    2 h+ |! u6 L4 R" Y7 e
  131.         Dim includeFeatures As Boolean = False
    9 W! U) \' p+ o3 i/ U" B
  132.         Dim keepHighlighted As Boolean = False
    ; O) G# f; i- n& c
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    : _( i4 k, T* X7 b7 j6 E
  134.         Dim cursor As Point3d
    5 _' V5 H* B7 J
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    0 I" i# U) T0 ~
  136.         Dim selectionMask_array(0) As Selection.MaskTriple- c/ z3 [3 Q* B" s6 x
  137. & n$ K# p7 J3 t& G, R
  138.         With selectionMask_array(0)
    ; j4 I( F* E8 u* i* q
  139.             .Type = UFConstants.UF_point_type
    ! o. J; [* B& g$ s1 h# E
  140.             .Subtype = UFConstants.UF_all_subtype3 m: G% Q7 e! ?  m2 S& M. Z
  141.         End With6 Z/ h  k* o. t1 X- Z# a
  142. 1 N& ]+ F! h  e0 V
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    . @+ X6 k2 i3 t# S) b. i; m' U* M) ^& r
  144.          title, scope, selAction, _
    ' B" ]3 S8 }+ T2 M$ |
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    4 K5 S& c5 f7 }! ]$ g
  146.          selobj, cursor)
    ( c! Y/ F6 w7 b: t
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then6 ~3 P5 e3 j: j8 g. |
  148.             selPoint = selObj
    : {, P& G1 {( f$ E9 F( Q
  149.             Return Selection.Response.Ok* t# X: N* U. ^3 U) J
  150.         Else
    % H) m4 s1 c  Q+ g8 z, ^1 `
  151.             Return Selection.Response.Cancel; |0 ~3 y2 L: i
  152.         End If
    # \: z- O7 B( w7 z) f# z# o2 w

  153. ) W1 @5 ?9 W: o
  154.     End Function
    ) w3 I; |) G/ w# b' s& P
  155. 4 }  T8 b% X4 l) D
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double+ z8 [3 q0 ?7 e" D( H4 x6 ?; f

  157. 2 E6 w2 |# Q4 b6 z4 [: g
  158.         Dim result As Double7 h8 G9 C) g, x

  159. . v# \: F; v, f+ ]6 Z
  160.         Try
    $ z3 J* f1 T/ p8 `
  161.             Dim nullNXObject As NXObject = Nothing
    + X% O3 y5 |8 T% O$ `
  162. & C& w% Q. Q( H* }% X6 m; z' O
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder! b. w# G; r6 z" X1 S) l
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    4 f; H* V. a) j  X
  165. " W+ Y; Q8 K0 i: g
  166.             measureDistanceBuilder1.InfoWindow = False2 B* i! p; @* z  T
  167. & ^. Z/ T% s* ?+ }$ T- c
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum' C9 G. g% |% q# c5 `
  169. % G: V: b" l! L6 t% ]( @' Q
  170.             Dim nullUnit As Unit = Nothing
    2 A1 y3 |7 g. Z
  171. : o- H) j9 X! e$ ~# p3 [3 q, H
  172.             Dim measureDistance1 As MeasureDistance
    ( [$ A% f" G) h+ i5 o1 o* @$ T
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)% e! q- P2 [& ?' A/ i
  174. 6 U6 _: l4 C. W  @% M
  175.             result = measureDistance1.Value
    # {3 O6 b' f: H) V/ ?; P* o

  176. / B0 D0 Z9 j( Z0 S
  177.             'measureDistance1.Information()
    + D, i3 v1 Y( q; B

  178. 2 g& D  H, q. T, u7 Q
  179.             measureDistance1.Dispose()
    " u) Q* M0 r; {# t0 s

  180. " d" R- x$ ^' W6 c
  181.         Catch ex As NXException
    + n2 g) c4 b# c/ i* A7 a3 Q
  182.             MsgBox(ex.Message)
    / p" K9 Y' \9 v5 z
  183.             Return Nothing
    - a, R) x; m$ k* ~# j6 D1 E3 h  S  O

  184. $ i1 T! y# R8 X' v$ X
  185.         End Try
    & L8 f+ ^# U" @9 F
  186. , x5 I& C7 }2 ]; O
  187. , i8 T- q) E0 z5 S# X; K6 }# g+ s
  188.         Return result* {/ O9 p) j  `# ?

  189. , f- K0 b4 x8 O8 t) \
  190.     End Function+ ~( r6 J! H! l8 q
  191. . \5 i1 ^* p: z4 b

  192. % {  h5 L+ l& Z- X; A. [8 ]
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer/ x  U6 l# ]/ `" c
  194. * `1 m/ X1 c2 t7 W
  195.         'Unloads the image when the NX session terminates% p: w: r5 }9 T! A1 C- ?9 h8 U  b
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination- {. C8 T4 L* F

  197. ! v0 J) [9 a+ H
  198.     End Function
    1 z, R8 \2 x& v5 F; o2 Y
  199. / _9 N( ], Y* K) b
  200. End Module
    : n9 v7 R: ~: K& s
  201. </P>
复制代码
' x9 H" b' J( |$ Y; b
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了