PLM之家PLMHome-工业软件与AI结合践行者

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
+ e+ I. M: P* ]- R3 E( W" V: r
NX二次开发源码: 装配中选点,并报告父对象
+ f/ I3 c! V8 a9 p) \0 I
  1. " |& t6 \- h+ M* s
  2. <P>Option Strict Off/ h. j9 _* q% a$ t9 z8 Y
  3. Imports System  H& B9 ^$ t; O7 v+ u1 z* m
  4. Imports NXOpen% m6 w2 A4 k8 V1 l( U4 n
  5. Imports NXOpen.UF) z: F$ F" T) m+ `

  6. 4 M  a+ N$ c* G' L/ A1 L
  7. Module Module1: r9 g4 m& ^; b# W: P
  8. $ n; |2 N( Y) m9 T0 H
  9.     Dim theSession As Session = Session.GetSession()8 s+ I2 V, Z$ T5 y' i" D
  10.     Dim theUI As UI = UI.GetUI()5 M" w( F- o6 T  D
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()' X# c, t, |0 P8 M
  12.     Dim lw As ListingWindow = theSession.ListingWindow: y. U& h4 ]: H3 ?! j5 g
  13.     Dim pickedPoint As Point( T  X4 _8 `; D9 W0 M
  14.     Dim myModelingTolerance As Double8 |: g5 y4 G0 }6 |7 H
  15. + I- F& n8 v; l1 t
  16.     Sub Main()
    - u5 h9 ?' f: b2 B

  17. " |* r$ E, _- i- y  M# P
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    8 I" z  p7 ^6 P) l3 E: C$ {; M

  19. $ g' c. g) q$ t$ j- T, Y* O; H
  20.         Dim workPart As Part = theSession.Parts.Work
    7 J/ D/ d6 x+ S& C; T: |' a$ h7 M
  21.         lw.Open()4 r+ h# v" o! N- E
  22. 4 t0 V. R' ^! B; c4 p- d/ [: Q# ?# F8 k
  23.         Dim myPointTag As Tag
    ' s/ @, V- v& P% ~+ P1 v. c

  24. + W% j3 H4 O' N
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then+ i" |7 G; n1 w' I4 N; V
  26.             Exit Sub5 F& [: s7 D3 X& _7 s( q
  27.         End If
    / w; x8 J; Z3 l# j
  28. 3 ~' e- @# W" @" x% q) j
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
      S9 }8 }5 T  M1 G5 I' y

  30. 6 p7 o' W- u+ H' c2 v: D: L+ C
  31.         AskParents(myPointTag)
    ' Y6 q6 V0 ^9 I( l
  32. $ _" v5 L9 y# x% z" w
  33.     End Sub
    ( q8 I6 B& h0 D8 p
  34. . P  T" u( e9 Z" M8 I5 s/ F. A; k
  35.     Sub AskParents(ByVal objTag As Tag)
    9 B6 d* ^+ S5 I$ P# m5 N: p6 T

  36. / @8 T+ g2 w, e/ Y* {
  37.         Dim n_parents As Integer0 N2 W2 E# |3 z. V; u
  38.         Dim parentTags As Tag()7 }+ O0 M9 U+ @7 D* C/ G+ j7 Z
  39.         Dim myPoint As Point
    % d. P3 s+ \& p4 F- ~# \
  40.         Dim myEdge As Edge
    * x1 q  L2 R3 h0 L7 D
  41.         Dim myXform As Xform, x& W1 e; a) K( V; r1 ~; _3 M
  42.         Dim myTaggedObject As TaggedObject
    3 p* a8 Q" _$ h  L
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    + q+ [) S' S6 I) d( W& \5 h

  44. " b+ d$ H& I+ n, d
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then& N* L. N5 D" g
  46.             myPoint = myTaggedObject' y- J' y2 Q  n% C# O( L6 S
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    / j& ]' F. A+ u% m8 O+ J9 _" U! F6 q
  48.             lw.WriteLine("")
    . m$ P! V, Q# l, E% L, J: a

  49. / S. k4 ]8 S' W. j, L) d
  50.             Try
    : D) [% t' }8 I* P
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)# c) T' N2 T0 ]! A7 b$ M. R4 N
  52.                 lw.WriteLine("num parents: " & n_parents.ToString), U$ C9 u1 h/ a
  53.                 For Each parentTag As Tag In parentTags6 G0 e" l& h1 l: h
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)9 \2 \) X: z7 ]
  55. ) `9 }0 X% C# k. x. E
  56.                     AskParents(parent_object.Tag)
    $ v+ P( S3 }9 m- @& s

  57. / J+ B' ~6 U6 N( a( p0 C
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then, c% ~# Y3 [2 S0 u4 ]4 @& ?
  59.                         myEdge = parent_object
    - \0 S- v' M& Z% m- p! |- x
  60.                         lw.WriteLine("")' }& _' P# T2 J4 H8 m; Y% {% Y
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString), f& ^3 M" v# d$ P+ Y
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString); _% Z1 ~" q) J
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
    $ D& S! V7 a( b& }7 t; y( X. n, Z6 T
  64. & b6 ~3 q2 m5 l9 h$ c/ r, ^8 b
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    & V# C0 D8 Z6 P& ?9 i$ `6 p# ^0 e# d
  66.                         Dim occTags() As Tag
    , E# u* d  A# ^' o
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)6 T4 E  a$ ?/ _# D& N0 _
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    % C* e' m  O. g0 c% ^
  69.                         For Each temp As Tag In occTags
    ) ^; n+ ]+ D" |' j: Z: M  y0 J
  70.                             Dim myComp As Assemblies.Component, z, u" z' n% |. }
  71.                             myComp = Utilities.NXObjectManager.Get(temp)6 ~2 s7 [. a; p- s
  72.                             Dim myCompPos As Point3d
    # e5 }6 f% @* F
  73.                             Dim myCompOrientation As Matrix3x3* {& `) B  S8 \- |' S( ]- V
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)
    9 }% ~4 r6 U9 J% k1 s
  75. 7 ^' r$ U& }, U% r3 p+ T1 j7 X
  76.                             lw.WriteLine("")
    $ k3 O  e8 @' h; k. \
  77.                             lw.WriteLine("component name: " & myComp.Name)3 F  P) \  k# o# t6 x* A
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)6 y2 C; d0 y* r8 J
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    1 _4 G: Y7 }4 b9 `/ k- z

  80. 6 W2 i( W4 D/ W( O, D3 S+ J
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then# [/ D8 l% Y. y: u0 n; n  E
  82.                                 lw.WriteLine("** This is the component that was picked **")
    . Y: T4 m2 r( o( W# j
  83.                             End If
    + h3 C5 {0 B8 \
  84. ' Q" |" \  w: N
  85.                             lw.WriteLine("")
    1 r7 B5 s- U5 v3 c- h% Q) w
  86.                         Next
    # W' @6 M! m1 _2 ^; U8 t% d

  87. " u: t5 u% G6 c
  88.                         lw.WriteLine("")% B) j) f6 m# `8 p) }
  89.                     End If
    / B2 \! A. ^  K2 a+ O3 ~) F7 {
  90.                 Next) j- b3 B2 M9 C% m8 l  X2 e
  91. * L3 K- a2 `1 [" f" P. s" {0 U4 }
  92.             CaTCh ex As Exception
    , _3 I% G; b. Q; Y$ Y. i
  93.                 ' NXOpen.NXException: Current object is not smart
    & A; b, I# v6 ]! y6 e
  94.                 lw.WriteLine(" Error: " + ex.Message)0 `) [  L, R6 T6 S% }
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)9 b- l8 b* o$ t" }9 `2 Q
  96.             End Try
    6 ^2 u- S. l; q

  97. + x! `$ T0 N" {( f! q5 \! n
  98. " D3 I4 W- B( g6 I- h6 J. h
  99.         End If
    , [6 a0 v' N# h# N3 _2 R) w1 |
  100. 8 t, g4 {2 ^/ k& [+ ~; h  L
  101. ( I( O+ f0 X5 D# J. d
  102.     End Sub
    & i" V/ x) T8 X/ P6 Y

  103. $ G, q( W9 i- }: m3 [0 E
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    ; m+ T7 L4 |! k; V& l1 R
  105. 8 m; d4 x7 G5 K
  106.         Dim base_pt As Double() = New Double(2) {}
    9 w( N, g  e3 T" y  @; @
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null& s8 j  Y/ ], h. n1 }% ~
  108.         Dim response As Integer = 01 K( ?  ?! T' z5 m+ p2 o) N3 C
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt) M7 r$ W! D$ S2 _

  110. + @& x. s5 W( `" I
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)  }1 q; y; L0 F7 H8 W
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)+ J0 y* l, R- K
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)  V- I) ?7 S; _: y9 G
  114. 0 s' J. t; x. ^& m
  115.         Dim pointLocation As Point3d4 f" k& m" W2 a8 j+ @* d" l. D
  116.         pointLocation.X = base_pt(0)6 V4 ^# e, H, D) }; C& n
  117.         pointLocation.Y = base_pt(1)
    4 f9 `: _. \6 ?
  118.         pointLocation.Z = base_pt(2)0 |: a3 j/ c7 F/ B; C
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)! `9 I. w3 p% L! g: r5 a) U
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    + H0 c) T( n1 z+ R  i

  121. " }9 i# q  j) ^: C* r
  122.         Return response
    * \/ @9 }5 a9 a5 J
  123. 5 C- }- d! j0 [( k* m# p
  124.     End Function
    # p- {) ^# n! ]0 s+ H

  125. 9 E/ N5 D0 F3 p. c- {) S
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response+ m7 f' D5 z: V1 j. q. v

  127. 9 t/ l- x8 M: _+ w4 D7 S: e
  128.         Dim selObj As TaggedObject, Y8 J/ P8 R# i5 P9 K% T
  129.         Dim theUI As UI = UI.GetUI, Z: ?/ f1 b# c2 u+ p- x- f* u3 t
  130.         Dim title As String = "Select a Point"7 h. w# N0 [' J8 U% {$ u: b
  131.         Dim includeFeatures As Boolean = False/ y" e. |$ w$ E0 r: t
  132.         Dim keepHighlighted As Boolean = False- a$ e- G% y; I3 d/ t" P2 w
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    0 x; n. ]8 [4 H* x% W7 x6 o2 k
  134.         Dim cursor As Point3d1 ^- w2 h& C. f
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    4 `9 G1 i" t/ b7 a; ^; q5 k: s
  136.         Dim selectionMask_array(0) As Selection.MaskTriple
    ' a, @' Y1 \& U, u4 z$ s
  137. , b/ J- a# g% w+ G$ L
  138.         With selectionMask_array(0). L: R+ V% f3 g
  139.             .Type = UFConstants.UF_point_type
    ( `3 h) u' X8 G' T: P
  140.             .Subtype = UFConstants.UF_all_subtype
    $ Q9 X, _7 e, P" o1 R; P4 h8 C/ d, B( w+ E
  141.         End With
    " g) X2 E& C4 e7 R0 y

  142. / v: ~# G+ p/ ?& H* K1 l
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    . l( J+ z6 j- w- p, e4 i7 i
  144.          title, scope, selAction, _
    9 p* f5 g7 y: [: x' A4 g2 }
  145.          includeFeatures, keepHighlighted, selectionMask_array, _/ M) z; }+ o8 b9 j  B+ F
  146.          selobj, cursor)
    4 E& e/ W: y  s/ Y4 ^- |
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    " u& Y" Q/ b" H4 `9 B; S8 g
  148.             selPoint = selObj; x, q* {$ c& m: f8 Q8 x# t
  149.             Return Selection.Response.Ok8 O! {1 @' T4 c2 h! l3 I
  150.         Else
    & k9 J) z( O+ z1 h+ {+ X
  151.             Return Selection.Response.Cancel
    * R4 w8 h7 [5 \# {* P
  152.         End If4 t2 T( G$ u8 U- j( W. T" p
  153. % r; h$ E5 `; Q% N% ^
  154.     End Function2 ^) W6 w( \1 n4 Z' K0 H: _
  155. % c, B" y% q' I4 L4 B
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double7 i3 e0 F4 g% n) L

  157. ; H6 a; N* G9 f6 H
  158.         Dim result As Double4 c& @. f9 l. h
  159. + I: a. r  {# S
  160.         Try2 k+ q" d/ S! o' n2 Q$ h9 _
  161.             Dim nullNXObject As NXObject = Nothing
      R, f4 r- I/ Q* I0 |

  162. 2 q  f3 u; N  v4 n/ W# L, e
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder/ ?: x. {0 H! ~! t* _" x% |7 S( e1 _$ i
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject). C5 q7 k5 r* _! u0 ?' ^5 S

  165. * e$ w( S( l# r3 e; C- e' V4 i
  166.             measureDistanceBuilder1.InfoWindow = False
    2 w2 I  h, h' ?# ]
  167. ' n$ x0 m1 P5 d& G# ~
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    3 H& i; r& p% t8 o! [3 z( }

  169. $ h$ ]& ~, D9 y
  170.             Dim nullUnit As Unit = Nothing
    ! q. N3 r" J$ ?! P# S2 N" g

  171. ! l) G9 [0 ?5 A
  172.             Dim measureDistance1 As MeasureDistance
    # f! H7 N7 J0 M& X
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)- q) a+ e8 Z3 Z- e% B6 l
  174. , A3 G, e% X. P/ r: {4 b6 S
  175.             result = measureDistance1.Value  y. G9 }8 ~3 E; G* O4 P: [

  176. $ f3 R% Q# H! f9 C6 }; }3 K' Z
  177.             'measureDistance1.Information()
    , z5 Q( a3 o; W$ }3 G& Q
  178. 9 x% W! P4 j+ k; @  p
  179.             measureDistance1.Dispose()( Z2 W1 o1 v2 n; C- l, y
  180. ' h# C% h3 Q) X: M! [- Q! Y9 }
  181.         Catch ex As NXException7 P2 |7 c+ ]; a0 d
  182.             MsgBox(ex.Message)9 O: K4 H9 u2 |2 [3 g9 Z  }1 e
  183.             Return Nothing( u" {! K6 \: A# H. L: s! x
  184. 0 I8 h' v& _  f8 s
  185.         End Try
    ) A9 Z# |0 a; x3 `" S. r6 @: U
  186. ' D2 ^. ]; {( w# R9 ?' C0 [$ B6 C8 o
  187. # ]; ]/ H7 `  C( w2 L
  188.         Return result
    - \  f% c2 a' [: q0 {- ?- O, B

  189. 1 C+ [2 ~+ J( a$ ?
  190.     End Function+ m3 T1 \, |2 x. l, F: ^5 u
  191. , W8 R/ O& a+ p' f9 f8 P; _

  192. 5 \4 P9 U: @" x6 @1 h/ Y( \  U
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer$ H7 B: K- M( _

  194. 2 e* w* G0 B& t* S9 H& {
  195.         'Unloads the image when the NX session terminates0 G, B2 Y; v+ A  C1 ?
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    ' U$ u5 p+ e) S5 s. ?: Q- K0 i0 H8 A

  197. $ [  y/ s4 d" ~& }
  198.     End Function4 z1 s7 k2 e8 b) Q3 C
  199. ' O4 x  H; k. J" \5 I
  200. End Module
    9 Q4 H4 J) M9 E" }+ _$ f
  201. </P>
复制代码

( g8 u# }+ k* l+ V5 D6 p
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了