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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
7 G5 u5 n7 o$ Z( V+ M+ J
NX二次开发源码: 装配中选点,并报告父对象
  z8 g$ B4 f; F/ \! i( ]
  1. 0 s# \+ P8 }- M2 X
  2. <P>Option Strict Off3 ~& }& g$ ?9 y7 A9 _
  3. Imports System, ?% g0 M. ~! [, c# f/ y- @
  4. Imports NXOpen! c1 s" |& x3 o& ^9 x6 \
  5. Imports NXOpen.UF  v' B8 U2 ]) w3 }

  6. ) {8 |) v5 k) x) j$ \; Q
  7. Module Module1: e4 Y; n3 q: O1 |  `3 G
  8. - M( z" x0 f/ V- x/ {! c3 `
  9.     Dim theSession As Session = Session.GetSession()
    , I  ^: P6 B( ^- q
  10.     Dim theUI As UI = UI.GetUI()
    , I" A' f6 B; X8 T- }( d
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()+ ?  w4 E& ?0 |( z- G' G
  12.     Dim lw As ListingWindow = theSession.ListingWindow4 ~; s. ]. j3 d- M; w( w
  13.     Dim pickedPoint As Point
    3 R3 C1 R8 B! f7 ~$ ~* l& g" |
  14.     Dim myModelingTolerance As Double! W3 B  E7 d, G/ c" }

  15. & W& V) @4 ^0 ]2 e/ d) M/ u6 _; G2 Z" y
  16.     Sub Main()! |9 \" d9 ?' U9 k2 U  e

  17. ( H; |5 g7 Z. K) C- `
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)' K9 r' D5 P8 h4 G* [# E
  19. 7 H2 \( |' |- w( c0 }# s( F9 }
  20.         Dim workPart As Part = theSession.Parts.Work% G7 s( _% g* ~
  21.         lw.Open()
    7 z) W/ c7 s& b+ d6 z( T! ^

  22. 6 i; v8 p0 c, r7 @& G
  23.         Dim myPointTag As Tag
    * {0 v. s, R. z% _! X2 q, Q

  24. 3 M8 I* @7 @) v7 @# \8 @) R
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then- I! h4 u& C$ S, k5 R8 u* l3 a/ e
  26.             Exit Sub4 b! T1 v( j/ S. [4 I% L( z9 [6 x
  27.         End If
    0 j- C" F1 V' E! S
  28. 1 V* A( H: Y, ^: H0 X
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    + j6 E% N+ {% X+ e: _+ ]
  30. ( ~0 k) V% X, q0 F( g( h. K( ~
  31.         AskParents(myPointTag)
    $ j) X# S4 q+ j% ?3 ^
  32. 5 V9 b+ I( x9 e6 n# y: l
  33.     End Sub
    ' z/ c0 s1 l" {! P9 G2 _
  34. ( m9 p  h# ], X) {
  35.     Sub AskParents(ByVal objTag As Tag)* S( ~# h+ J8 g3 B8 w

  36. ) j- }% P- r$ ]4 t% G
  37.         Dim n_parents As Integer
      L3 ~: a$ G0 c* w1 P$ H6 l2 }
  38.         Dim parentTags As Tag()
    - ]; [/ n9 C$ {! n1 ~
  39.         Dim myPoint As Point" B5 B, |" @: N. f, Z% J
  40.         Dim myEdge As Edge
    + [: U3 R6 R. i) b! X& @
  41.         Dim myXform As Xform
    7 `* ]: g6 m3 p( H1 w
  42.         Dim myTaggedObject As TaggedObject
    2 K6 x# A. y' J2 m& v( h
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)# k# ~7 j6 K( |% c: x6 A

  44. / d( ~4 m% y0 m
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    1 R: }4 i- G6 _% q
  46.             myPoint = myTaggedObject
    . L# f' U9 c$ H; G( i
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)7 }8 g$ w& a' `  F2 j5 I
  48.             lw.WriteLine(""): T' X# i! W7 S( f

  49. / u  u5 y8 `5 z  F* h" R
  50.             Try
    1 H, U( [+ k; ?0 k! X& R' J' S
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    ' w/ t" |% A* Y% v7 ?" S8 x0 K
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    " M& W. V4 G  p/ f9 Q
  53.                 For Each parentTag As Tag In parentTags
      i' Y1 P  H! G# ]. n$ I; r
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    " O# F; N# y8 i# N+ H: a

  55. ; l9 j! R3 B, s2 V. N
  56.                     AskParents(parent_object.Tag)# _* m4 W, T( B5 J! `, k( J

  57. 9 t# M8 f6 K: r6 c" O( |
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    + I- ]+ E' D  C% o1 w. Q
  59.                         myEdge = parent_object
    2 Q! y7 U$ l1 l8 [
  60.                         lw.WriteLine("")
    : R: b; w5 {& `5 O/ s
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)1 O7 {  L9 ]" v# E4 K
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
      j9 c1 O& _) k- j# W9 I2 ^
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)! b! v* _0 j  U6 N. j" B
  64. 9 R. p- p9 Y6 p
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    2 f, s# [( E: G/ }
  66.                         Dim occTags() As Tag! t. y5 d( x: \5 ]0 ~
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    ; e7 p1 E$ m) o. ?2 z2 \# b
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    , r! B6 W2 Y$ S- v' [7 E/ B: V7 }
  69.                         For Each temp As Tag In occTags" w/ e6 \' X' U7 C3 ?
  70.                             Dim myComp As Assemblies.Component9 v1 l0 u  [) n* o- I) ]
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    ) {' [( y  v( H$ ?) |* M; v+ D
  72.                             Dim myCompPos As Point3d
    3 t9 F( G1 G" T- A+ m3 ], |
  73.                             Dim myCompOrientation As Matrix3x3
    ; O: P* V5 o+ W' p  G* y
  74.                             myComp.GetPosition(myCompPos, myCompOrientation); u% f9 h5 m, M* J

  75. 5 A$ r5 ?3 d( H# s* n
  76.                             lw.WriteLine("")' d% o& c! l: [& B$ S9 P# Q5 J
  77.                             lw.WriteLine("component name: " & myComp.Name). n( d8 J6 }5 U. v0 w' m" R
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    + v& Y/ u; s# c0 g$ Q
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    * n/ ^  d3 Q$ Y2 {/ ~$ Q/ q0 U6 f
  80. ! o7 T1 j0 C, r  n( d( P
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    ; D/ y+ W# Z8 Q3 |; F
  82.                                 lw.WriteLine("** This is the component that was picked **")
    / [4 U  r8 x0 s
  83.                             End If
    * L% J; U) s- p0 d
  84. 9 ^( y  }1 h1 o; |& D& _
  85.                             lw.WriteLine("")
    8 T6 x8 i5 x& N3 _
  86.                         Next
    5 \& n" }& ?0 p

  87. 2 R6 B" o  {) P1 _4 s; S
  88.                         lw.WriteLine("")% P& z4 W0 T. |+ f, D
  89.                     End If
    2 u: S1 r- ~2 w4 X# e
  90.                 Next
    2 G$ {! _! y% W' }! L' ?! d- Q! l9 C

  91. $ ~0 U4 A3 l; @8 r' ?, z& m4 O! H
  92.             CaTCh ex As Exception' N8 K2 o: H, _
  93.                 ' NXOpen.NXException: Current object is not smart
    , Q+ V5 L2 |/ L, _, c
  94.                 lw.WriteLine(" Error: " + ex.Message)# _6 _* r( I$ N8 ?' d3 U
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    & Y" ~. L% W6 ?( P% ^0 h1 _7 x2 o
  96.             End Try, `- a; C$ v4 z: ^9 }) @
  97. 4 w7 |0 c3 g$ d$ V$ j% l. O5 W

  98. + m# ?  y; N  a  t$ d+ B
  99.         End If7 `9 L2 n( U% _& r% |+ \

  100. . ^8 \- V' J9 ?  c$ a+ i7 E
  101. ) h9 x/ F+ F% X& t! {$ I
  102.     End Sub
    8 Y: a8 y( N1 d9 g8 R' L( U- Z  P" e, T
  103. 5 v$ M6 b( b# _4 a
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer: `4 X( A# }2 K, F5 I5 J0 g

  105. / A0 u+ `* `8 x; ?$ R/ `* v
  106.         Dim base_pt As Double() = New Double(2) {}% ?* E9 ?8 C9 [4 z1 n( W
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    6 n' M& g2 J& d% g- q- M
  108.         Dim response As Integer = 0
    4 v! }3 R+ `5 H8 o- D4 ~; C
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
    % o% S! D! T; ~0 F, @

  110. , l7 P) y% Y5 A6 T9 U
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    9 \" I' S) K9 K- Y" I# ~
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    8 f2 @7 I4 v* n. \2 R7 O* Y1 L7 \$ {
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)8 K$ e% M2 P+ |! \$ O# a
  114. 2 w2 }$ I+ l" S3 j; t
  115.         Dim pointLocation As Point3d# H$ M. s% y0 ]* ?- p) w: d
  116.         pointLocation.X = base_pt(0): M7 b4 N$ u  W  G; M$ g! v
  117.         pointLocation.Y = base_pt(1)
    . D. H5 q3 B" B
  118.         pointLocation.Z = base_pt(2)- C  }8 r0 ]# ?( z2 J, p; n1 m
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)( L% V' X' P6 z& p
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    7 r/ w4 ]6 ]$ l9 b* S7 T9 V
  121. : H; g6 N) d) Q' w8 |. v% s
  122.         Return response2 h* l# W, @( T* U* ?' @

  123. 8 f! U3 e( [+ _
  124.     End Function
    / I3 A0 k( s9 U  r0 T: H' _
  125. 4 n& V9 F; o4 Z! {6 b- V
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    2 {& p' }, N/ B& U* p
  127. + }' _0 M  H, J6 K6 H9 P/ K
  128.         Dim selObj As TaggedObject- |/ ^9 w' N2 j" `+ A: j: v
  129.         Dim theUI As UI = UI.GetUI- t7 n1 N* X9 U4 v9 U$ S- N
  130.         Dim title As String = "Select a Point"9 W* \  |3 h- Z; K( E" R
  131.         Dim includeFeatures As Boolean = False3 U+ G5 G4 j1 Q. y
  132.         Dim keepHighlighted As Boolean = False& r' ?) ]. `6 R: u4 H
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    + }! t, [* P1 g
  134.         Dim cursor As Point3d" H. m' k* g% a9 k, K, g* x7 ?3 d
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly$ E" a7 D- ]- [* i) ?, H) |* z
  136.         Dim selectionMask_array(0) As Selection.MaskTriple" {/ a, ]0 H+ v3 v

  137. # i8 @% Z# V2 r1 V7 F6 J, O$ o6 X
  138.         With selectionMask_array(0)+ t' b9 W5 _" G# i
  139.             .Type = UFConstants.UF_point_type
    6 B2 |0 T4 d, b4 o
  140.             .Subtype = UFConstants.UF_all_subtype. Z) K* s# q) u& V# r4 [, f0 c0 N/ {
  141.         End With( m3 J8 `! ]# p- H7 Z$ |

  142. 4 C& H4 K' N) x- n4 E0 v
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    1 `. u" S" P; ^- E
  144.          title, scope, selAction, _; _/ b9 Y$ T* r4 a' U4 ?0 S1 F
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    ' P' ^: K( [# _, h5 n5 a' i4 R
  146.          selobj, cursor)
    % k$ i% ~: @: X' Z0 Q! N& G
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then; G0 [; o0 p1 c; k6 s
  148.             selPoint = selObj1 l! p# \- \; t. ]# X! ]
  149.             Return Selection.Response.Ok# N3 C7 x! d1 Y0 y; D4 U
  150.         Else; A2 M' S" y% l2 _
  151.             Return Selection.Response.Cancel
    " |. I& v) f2 Q5 Z* O5 X) F( g- Y
  152.         End If
    ! Z$ s+ w3 q2 K$ {) |

  153. 4 _$ P! L% B$ R
  154.     End Function8 u- Y2 h' D+ L( I3 E
  155. / o) t; C$ F' l7 u
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    5 _6 z. z/ @5 _# n

  157. 8 |/ u. @9 c- b1 R- `9 I$ ?
  158.         Dim result As Double
    - Z9 h5 Y+ r. r" A% Y! f9 I9 Z3 A
  159. 8 ~1 F" p! U2 [- \3 A0 n
  160.         Try
    8 T0 b7 z6 l1 a9 ^
  161.             Dim nullNXObject As NXObject = Nothing
    1 w( r' T# t+ A: H$ v  i4 @9 X7 H
  162. - s) J8 {, E' |* N+ ]
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    5 h% |$ a( R  N
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)3 P" n3 Q# W! u, S3 v8 \$ D
  165.   K  T. s8 ^" a, h1 u1 V/ e
  166.             measureDistanceBuilder1.InfoWindow = False
    ! R; O5 i2 X( P5 \/ v9 N  R

  167. / L7 S+ R: d+ k- t/ X
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    5 t) e0 H2 Y8 Y4 Y0 h( l  u# n
  169. + L2 Q- P  o2 V- s6 q
  170.             Dim nullUnit As Unit = Nothing! f  h+ K+ L: j! G9 n0 Q
  171. ' n3 h! S- x* J) j6 `
  172.             Dim measureDistance1 As MeasureDistance
    * C8 X$ [  j' A3 g  {
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    ' _, U0 R% T# g4 D& H- ]
  174. 2 o- `; I  `2 e6 V* S/ F' T
  175.             result = measureDistance1.Value! y' K+ k! Q) _" Q
  176. 4 u/ l8 `2 q* b' I
  177.             'measureDistance1.Information()2 v# q! H; K+ f
  178. ( f+ X5 `, s- Y* N0 O
  179.             measureDistance1.Dispose()% q6 a, V( _' T2 h
  180. 3 j8 j1 _6 V0 ^) p2 `8 T3 R- T
  181.         Catch ex As NXException
    4 Q" {- Q* |0 Y, d- H% T
  182.             MsgBox(ex.Message)
    ) W! j2 b- `& ]% M: l5 a
  183.             Return Nothing
    ' {; x! o* e1 w3 V0 U

  184. , G7 r$ U; B( @* J6 u0 G# W
  185.         End Try9 Y8 k; B, `" [- p; N
  186. * D, v, j( d; A+ _9 [% _

  187. 8 l: h# c& s: x: I0 P
  188.         Return result
    5 f5 f: d5 m( Q/ X8 E

  189. 5 O' @: A2 W6 Z' S# N3 n$ x
  190.     End Function
    # y6 i% `+ q- q
  191. 2 N) u$ z! I, r% ?
  192. 2 j, h% J  {: n. V  n8 m
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer; x$ p8 c3 d! |$ Y  M+ B1 V3 g

  194. * p' b$ O7 X" m/ C
  195.         'Unloads the image when the NX session terminates6 C8 u/ K, t1 c: F9 C
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination# w% [+ f' ]# _( H4 J2 a, y. J

  197.   ]; G& A8 O1 h+ D6 Q: b6 j+ b
  198.     End Function
    2 |2 L. V8 |! x- I

  199. " s- b9 s2 o/ |1 R7 L. I+ a
  200. End Module
    2 c: U- a; v. {- T$ b- ]; p# F* j
  201. </P>
复制代码
) C$ A' ^2 a. o/ ~3 |1 I1 K0 c" C" X
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了