PLM之家PLMHome-工业软件践行者

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x

; O7 H9 s8 i, c) l- X2 gNX二次开发源码: 装配中选点,并报告父对象
& t" L- G  T- G9 l: P0 M( }

  1. 4 {. l( k/ @+ ~  f
  2. <P>Option Strict Off
    & I" n  y; ~5 D) ?, x4 Z
  3. Imports System
    / b+ u* R: P/ x' ~% e
  4. Imports NXOpen& I7 q1 Z& v9 ^* }% V/ [0 D9 L
  5. Imports NXOpen.UF
    ; J; ]. v. Z- K% @, y
  6. 4 U, n. V0 y1 K; `; w5 ^/ a' y9 P
  7. Module Module1# }3 P. ~+ J! w6 }! m* r& `
  8. / c+ R5 K) T' r9 Y: p. |& U& k
  9.     Dim theSession As Session = Session.GetSession()+ ^/ n* r& [& w+ n
  10.     Dim theUI As UI = UI.GetUI(), K+ J: ^: L4 k/ v
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    ! b$ Q9 R9 C" }2 y
  12.     Dim lw As ListingWindow = theSession.ListingWindow" Q* i8 w( r4 H
  13.     Dim pickedPoint As Point
      @; f6 M, l* Q; N* X' z
  14.     Dim myModelingTolerance As Double
    8 L, S8 W4 }1 V4 i$ h
  15. ( }# k" F: h& N
  16.     Sub Main()
    8 a+ ^, W" `( [3 z% d5 |: b
  17. 9 ?# X, r6 Q, _7 t
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)3 o% @8 s' b$ x: Y

  19. - g5 h  z# {8 ]5 }4 |
  20.         Dim workPart As Part = theSession.Parts.Work) k4 i+ A  [4 k+ u3 J, v2 d' A( t8 L
  21.         lw.Open()
    ) H& I/ U* S' D0 n0 ^

  22. / J' N! O: V8 D3 X
  23.         Dim myPointTag As Tag
    $ [4 `( P. Q. P9 J: Z* t
  24. 9 J4 @5 l8 a1 d, d3 S7 t
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then. b; s. x5 \' Z, @5 C% R* w
  26.             Exit Sub, X, ?( d) [* T& Y
  27.         End If
    # g3 Q. [6 e( I6 a/ z
  28. 9 r6 g: K# C0 U8 X- ^+ l7 L
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    % _2 \6 ~- S) }& A; q

  30.   v, j7 s6 ^9 E, ]  {3 l; }
  31.         AskParents(myPointTag)# i" e' r' C# O- o8 S/ _7 i( D
  32. + I( ^, z7 y! Y& B  i/ G# I
  33.     End Sub
    ( B- K% X; e4 e& a6 {

  34. 1 p9 |3 H5 c& ~0 u
  35.     Sub AskParents(ByVal objTag As Tag)4 g% S% r' x7 w! R3 h% v

  36. ( R2 Y) U! |/ e) {
  37.         Dim n_parents As Integer# a" ^$ t! w+ A% P* G1 k2 A, b- U
  38.         Dim parentTags As Tag()3 O! O' ?4 ?& `) v7 n
  39.         Dim myPoint As Point- S; z# w; o5 [/ Q, y' Y9 X
  40.         Dim myEdge As Edge
    , o/ D4 V; V! p8 E
  41.         Dim myXform As Xform
    . F7 y- I- p6 d- d- s
  42.         Dim myTaggedObject As TaggedObject; Q1 l. J4 R0 u& ?# j! B
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag): ?9 x  P9 u% ]/ R. ]' S
  44. 8 _) I# b" C9 R( u5 q- U( G8 p
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    ( F9 P, v/ H$ h( u
  46.             myPoint = myTaggedObject- ]- M5 v" ?* R3 @" t0 T" N' b
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    : c  f7 _* Z# F5 l2 k: R
  48.             lw.WriteLine("")
    7 @1 k+ [5 k$ A# b5 y9 Q

  49. # y/ q% D+ A' X; b3 _
  50.             Try! p$ U& O6 u$ ^8 f# W! m
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)( Q! |, C* j* _+ n  K& R
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    ' G0 y: n. l/ ~% F
  53.                 For Each parentTag As Tag In parentTags: E0 U- r" e; q. R/ c
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)* ?, B( U; O, ?/ p. {5 Y7 D
  55. 5 d# u6 m9 \2 ^# V5 M5 u3 q
  56.                     AskParents(parent_object.Tag)4 `8 f2 U, |+ {) L* R: \: y, r
  57. 8 d/ Z% H3 R2 ]. S6 C+ M
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then1 [9 K8 e2 O2 f% L
  59.                         myEdge = parent_object
    1 R5 ~. Y3 v: v& K3 h8 I- l7 |$ ?
  60.                         lw.WriteLine("")
    % B% c" [! E4 B: M
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString); Q1 l  N4 _* m* x9 P+ T7 w
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)) K1 U1 M! p  s* S( Y: y9 J
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)5 t. B: c& Y. j2 M' G# j

  64. / B( o2 Z* ]; s3 C4 t2 ^
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag  }* f1 y3 ~* L
  66.                         Dim occTags() As Tag7 G  D5 N% Z: L" Y7 J5 \9 P
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    ; y( H' u0 k6 E8 _
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)- {! ~7 N0 y# {
  69.                         For Each temp As Tag In occTags6 Z- Z  R3 u8 W( F$ X( c* I9 M
  70.                             Dim myComp As Assemblies.Component
    / [9 \; [: B+ w3 g7 `
  71.                             myComp = Utilities.NXObjectManager.Get(temp)# V( I$ O* S# X) v  S0 k
  72.                             Dim myCompPos As Point3d2 H% ?8 R/ t' V2 z% S) ]6 G( I
  73.                             Dim myCompOrientation As Matrix3x3& u) P; s9 Y8 h' b
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)
    ( M: K, D$ y2 r
  75. , H/ f9 S& k& d3 Q- t0 w$ Q5 I- ?
  76.                             lw.WriteLine(""). f  m' y, |/ r0 y9 }& e0 j
  77.                             lw.WriteLine("component name: " & myComp.Name)3 C( H4 {0 w2 q
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)# Z* U0 \" c  g  c
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    . [# u; g" u( I5 p

  80. 8 s. ^! W+ i, ~
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    9 S; |3 ]! d3 H" F
  82.                                 lw.WriteLine("** This is the component that was picked **")
    " i! [& W' g- B
  83.                             End If9 _, c" o3 z1 o. U: t/ `

  84. 0 P. ~5 W: v4 N. \& l; a; l2 y
  85.                             lw.WriteLine("")9 N  A/ P; B- |6 k3 E- Q2 ]
  86.                         Next. F5 m6 |5 G- U6 f

  87. ! T% d8 E/ m: X& E' }8 V) h
  88.                         lw.WriteLine("")
    * i& ~2 u2 n) ]/ ^0 ~% \
  89.                     End If  Y, X( k: j( V7 z, D. L) D
  90.                 Next; u+ }# X. m3 s$ b
  91. ; V- h+ Q8 D% n
  92.             CaTCh ex As Exception
    0 J! O) l4 v7 |, z
  93.                 ' NXOpen.NXException: Current object is not smart
      n+ X/ u+ C- h# I
  94.                 lw.WriteLine(" Error: " + ex.Message)2 Q3 f# P# c2 m- `: B. I6 J
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString), l( E" I! G! n9 J9 G2 x* F
  96.             End Try
    0 X: h% [6 c+ U; v! w3 h" `% n

  97. : j5 T2 q3 f6 L5 d
  98. 2 |- t2 e* u; O: ~" @, z/ j
  99.         End If
    . T8 K  H/ X8 B' D

  100. : t% v( u- \, |# v  V+ _. r7 c

  101. ' r, t' K7 E4 m
  102.     End Sub  e; s9 o- |9 Z8 m# W) P* w$ P
  103. ! z) z4 k: E# T6 X
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer5 G9 |$ v: t( a! l4 r( m

  105. + S+ o) _+ j5 i: W
  106.         Dim base_pt As Double() = New Double(2) {}1 F# u, r2 K. Z4 t9 e! T
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    ! ?5 X; a. r. K' J! ^+ `! @
  108.         Dim response As Integer = 0* Y5 v, y; O+ s
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
    2 }3 l2 u/ A2 T% D' r& D0 Q3 l
  110. 6 C$ D: I& L/ Z" B# }7 m+ [
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    4 N- M/ N6 t" F& g" G
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    9 L7 u; J0 K3 D) W
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    ! |, T3 \: ^5 e: o! D

  114. 0 K; S0 M/ w: {
  115.         Dim pointLocation As Point3d
    9 j4 O+ q6 a& C( r- @& c7 ~
  116.         pointLocation.X = base_pt(0)7 r7 g8 u* S2 `' P' K) {
  117.         pointLocation.Y = base_pt(1)
    ) Y5 j( P8 u% h" M6 \; C
  118.         pointLocation.Z = base_pt(2)! ]3 }2 z# }; f6 J& X1 P& e$ s( `
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)3 n6 E2 ?% h# C& i. ^9 E. A: e! v
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    * J6 ~2 u  [% G
  121. 9 H- U2 m! s: D! Q% d4 S# @$ i
  122.         Return response& e+ l- ]- a2 r; C

  123. ' Q. A$ ~/ I9 j
  124.     End Function' ?5 G: J! ~0 }$ X; Y$ q

  125. ; e. x8 {5 R9 q2 g6 [7 M1 W0 l3 l
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    5 }0 ^' W! B+ q& a% D
  127. 4 i' c0 U0 H6 z8 @+ e
  128.         Dim selObj As TaggedObject" j* I' R1 v9 l# E& v
  129.         Dim theUI As UI = UI.GetUI  ^5 x; ?; b- m
  130.         Dim title As String = "Select a Point"
    : z! ]  h+ p/ ~# ?2 X7 c2 U; b9 X3 J
  131.         Dim includeFeatures As Boolean = False
    % v3 B. g& O" r( T" l
  132.         Dim keepHighlighted As Boolean = False
    # B! P: L$ g" Q. r5 B) G% C8 {  a
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific" U* A  m! @; g" Z  O* `) k
  134.         Dim cursor As Point3d6 q& T4 L; u) A
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    7 |/ @& d- Y8 k4 |/ ^
  136.         Dim selectionMask_array(0) As Selection.MaskTriple" Z: \7 }( `1 u6 J( d6 d

  137. ! N+ |3 K6 G8 ~' @; v
  138.         With selectionMask_array(0)
    9 \( Q. f2 e% I, P0 a- Q* j
  139.             .Type = UFConstants.UF_point_type
    9 V' M3 R6 @: q4 c0 M
  140.             .Subtype = UFConstants.UF_all_subtype! G* R+ D$ J: z0 m
  141.         End With, b8 `% z& M9 ?# h! A/ G  Z% @
  142. 8 n- G  A  N# q' A" i
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _( R' [3 ?3 W% S; Q# ]
  144.          title, scope, selAction, _' S$ P, H: u0 Z! K4 N1 I
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    , C( s! s; z* V" R
  146.          selobj, cursor)6 q& O; O7 t: |3 _: s
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then) p4 r9 l* z3 A& ?' S5 w5 c% ?
  148.             selPoint = selObj$ w( T4 Z7 m% D. @/ f
  149.             Return Selection.Response.Ok# V0 ~& Y4 S0 j4 P+ ^5 l3 J
  150.         Else0 @& b3 b; @1 w. R' R5 w
  151.             Return Selection.Response.Cancel
    # `2 W7 A9 k% X$ K, X& X9 Y
  152.         End If
    ) p' e" T. e, i# c. p
  153. ) {( S/ ^" s; j$ ^/ k
  154.     End Function
    , S: B, _% B; \- Y% A5 `
  155. 0 }, k  n5 d) W
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    9 F9 b) J6 ?. ]& i& E9 ~: y3 {! h
  157. 6 n# C  H: ]9 V+ _
  158.         Dim result As Double
    0 r% q* u" i9 i' s$ K1 n( ~. z
  159.   [8 X9 {+ \: y# H, C  G) ?- I
  160.         Try
    9 G1 c. C% O- p9 g% z" j
  161.             Dim nullNXObject As NXObject = Nothing& d6 v4 f, f3 H  c0 p+ f; F
  162. 5 b& |4 w& }( g/ f8 t  b: x1 m
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder- n$ x( r9 q( c) v  x7 q
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)& [8 }1 ^1 o$ W2 M+ b" Q+ H
  165. 1 F6 r: w6 [3 M4 `3 [% X3 f
  166.             measureDistanceBuilder1.InfoWindow = False
    8 p8 o' K4 o# g* `7 Q
  167. - O! `# M! Y& v$ C
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    - G0 S0 `% A5 r$ P9 J) }! B

  169. 4 _* z! ]' v% v7 h; ^" o& r
  170.             Dim nullUnit As Unit = Nothing
    6 b8 s, w  i7 }& @% u- }' }2 O! ?+ `
  171. 7 r- i0 e% I7 T
  172.             Dim measureDistance1 As MeasureDistance
    ( x- r8 b& X. O/ Q# e, y
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    5 T/ P5 S7 g' i' \) x0 |$ `
  174. & ?9 e+ }! Q+ I( P# s9 G( P" p
  175.             result = measureDistance1.Value
    : a. g# }4 l  a6 l, B. Z* P' z

  176. ( E" b8 K/ b3 I8 }& }3 W
  177.             'measureDistance1.Information()) I9 ^/ |, s# |: Z+ \- R# c; H6 b

  178. , e8 M6 i9 F7 G* R
  179.             measureDistance1.Dispose()
    + Y4 V: s% @/ e" {$ A

  180.   u5 d1 d, r, N: w0 P9 A3 E1 i
  181.         Catch ex As NXException7 `2 S  d; t& M: l* R
  182.             MsgBox(ex.Message)2 ]* K$ u) `0 q2 v- P' o4 E
  183.             Return Nothing
    1 p' ~; p9 ~2 J( i9 H% S# I. Z" U( Q

  184. " Q( A" m1 p( P2 w- N' m) @
  185.         End Try
      e* v' c+ h  C0 u5 g

  186. $ ~4 v1 E8 J' F3 i

  187.   V: `, E3 S$ q) m
  188.         Return result) L+ s4 o3 T$ O: m! R6 v

  189. ! R1 |7 x0 Q) I, M) F! ^/ O
  190.     End Function2 o! {1 [! O: o: @. N7 f" c

  191. % j. l3 s9 V1 u4 m8 m* i6 j

  192. : p7 r( d0 H3 k, ~- S! N
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
      F+ t. I- S6 P: V+ l& g
  194. ( y- Y" a  A8 H8 C8 L
  195.         'Unloads the image when the NX session terminates
    ) {- R6 Q5 i; G. w- y! x  l
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination+ h0 h) }! E  W* D- ~; y. D
  197. $ W* K: Z6 ^8 k9 c
  198.     End Function: d( w& d9 V% g3 e% m5 T; o

  199.   w1 I2 f/ o1 }% b7 e# P3 [8 a7 f
  200. End Module
    1 R% Y2 Y: f$ v% f3 h' k
  201. </P>
复制代码
" H6 p4 g9 C1 i7 k8 K0 V
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了