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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x

3 t& p" D- f; G; V. K6 p! G/ x2 PNX二次开发源码: 装配中选点,并报告父对象
  c; g8 j/ [) A  S
  1. - d* W1 Z" c1 U  A
  2. <P>Option Strict Off: V) h0 {) X* j* S5 B; y9 O6 O; `
  3. Imports System1 z5 w) ]7 H+ ^+ h
  4. Imports NXOpen  V' @, {4 d. y
  5. Imports NXOpen.UF
    ! |% f4 ]0 g: g, B

  6. 1 E' f! Z, U  M' f5 N
  7. Module Module1
    9 T. G/ x2 o3 o1 C# A

  8. + }' r* e& c1 t5 T4 r5 x
  9.     Dim theSession As Session = Session.GetSession()* d8 v6 c- N% C/ b/ m
  10.     Dim theUI As UI = UI.GetUI()
    ' x" S; ?8 a, I  f- s- g
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    ' _3 f) E: ]: }! ]! \8 k& c
  12.     Dim lw As ListingWindow = theSession.ListingWindow2 U$ ~6 V3 p) ], C
  13.     Dim pickedPoint As Point" e; X1 i" W5 S9 r
  14.     Dim myModelingTolerance As Double6 L. |& d7 l2 F/ \

  15. : r* M. H1 a7 q5 H
  16.     Sub Main()
    * \8 A4 M7 @1 K

  17. & g8 h% ]% Z2 G' j: M8 W
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    / _7 X0 g: I$ T0 l5 |# D
  19. 1 }- f: W3 {% I% Z
  20.         Dim workPart As Part = theSession.Parts.Work
    + p# P2 d7 X% f8 ]
  21.         lw.Open()& Q4 ~3 |, u. }6 V
  22. - |0 i1 @+ u4 P2 r$ y
  23.         Dim myPointTag As Tag! I# d0 _$ C: T9 L% c2 \" J

  24. / D7 W0 W/ u) E2 z+ r0 q& w# M
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    ( E4 s% p0 B( ~$ M! C# _  h
  26.             Exit Sub+ o, @' a: b! t2 L# m/ Y
  27.         End If$ f! c! I1 x! _# S

  28. ! g  E* ]3 r! U$ Y8 h7 h
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    7 [' i7 h5 w# `; P3 A. X

  30. % _( v6 ?& @) m4 r4 N* E, J0 h3 d
  31.         AskParents(myPointTag)
    * I& J3 j, y/ `% O
  32. ! `1 i6 J  n" C, s
  33.     End Sub
    ) |' I- s7 P2 v3 p7 y; l

  34. ! H* Y% Y9 o+ z' _' ^$ k9 M
  35.     Sub AskParents(ByVal objTag As Tag)
    ( ~; b2 ]' U9 H! ~7 o$ ^

  36. . B* R) d' [9 {  l/ y7 o
  37.         Dim n_parents As Integer$ i' B& g) V' R+ U
  38.         Dim parentTags As Tag()
    # F8 M% L& N1 p( J) J
  39.         Dim myPoint As Point
    ( n6 {; h8 D0 w6 R; F
  40.         Dim myEdge As Edge( d, t2 D% w' ^; ^  X( r' {: W/ H
  41.         Dim myXform As Xform
    1 [3 x* K, g6 a& f
  42.         Dim myTaggedObject As TaggedObject# u9 y' u( P! \/ }' ?/ v
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    / J% S% I, Z% b9 p* B+ v; M9 u
  44. 6 P3 O- V& K& I! R5 u
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then6 B0 i1 X, E. \8 P: }
  46.             myPoint = myTaggedObject  `; }3 `* U1 {1 [) h. t
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)8 F6 G( ~: \% E: m  P0 I% r
  48.             lw.WriteLine("")+ s* I5 g, [. f; p9 f
  49. # \  `, T2 B' t1 e( A. a
  50.             Try
    3 r# K4 }- W7 R: ?. K% m% b0 A/ c
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    0 U/ i( Z5 z/ N& ]. o) g( p
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    ( J- D! @; V7 E
  53.                 For Each parentTag As Tag In parentTags" s  E; }3 v) e! C1 Q2 ?7 ~4 o
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag), y8 B. ~% b; [, ], ~
  55. * o3 i& Y! T( G7 j
  56.                     AskParents(parent_object.Tag)3 F4 j; _! M/ H* a* J4 J

  57. 2 _5 n  P$ Y( `2 L  f9 a) u& G
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    % }' M1 C/ V6 [4 {" M
  59.                         myEdge = parent_object2 x% ?7 I" v$ X4 L7 t- q9 A
  60.                         lw.WriteLine("")
    % S5 w* N( `# a  j  e
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)! a& k) {9 r3 Q5 h
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    ; P" r6 K  K: @3 s( |2 i
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
    / E2 \$ {" a6 a; [; _8 `- M5 O
  64. 8 e: s/ X& |* n' }, U! g# j' [
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag7 P- v: v) _# i
  66.                         Dim occTags() As Tag
    % I% t; ~. D$ c4 D9 A" V% x9 p: w
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    4 b0 L' Y% \  _( ^. M6 p
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)8 |3 h9 T( ]7 A7 Z7 [
  69.                         For Each temp As Tag In occTags1 q0 C+ S: `' J' e' n( d  Z: ?
  70.                             Dim myComp As Assemblies.Component2 F0 q( C. M  ~
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    ' G( S( m: ?) n# _2 `# f
  72.                             Dim myCompPos As Point3d
    + ~9 d5 T- M% ^2 l! D& ~; R
  73.                             Dim myCompOrientation As Matrix3x3- A6 E; o# L4 G& @
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)
    2 k/ w. K4 P7 a; I( `

  75. # ?: U' S3 H! B% K8 j, o: U
  76.                             lw.WriteLine("")
    ; ^" h! k0 k2 v: r% _
  77.                             lw.WriteLine("component name: " & myComp.Name)( Q$ v$ E% U* A9 M: W/ D
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    $ H1 }2 J, a: u3 {
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)6 [( O3 u4 v) D, @$ h
  80. & C* @- N& O; M6 G- G
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then9 {# H- D; \  l  [5 K
  82.                                 lw.WriteLine("** This is the component that was picked **")
    2 j+ r. }+ M5 N. @; V7 U$ Q
  83.                             End If) V; t# _! N% _

  84. 1 {) A* ]% Q$ R
  85.                             lw.WriteLine("")
    . U' S8 u- @$ U' y/ D( a) @
  86.                         Next$ g; {6 v% f8 J
  87. ) x( }& y9 [/ y: F
  88.                         lw.WriteLine("")- F  H, @) R) K4 g" T
  89.                     End If
    $ b- e6 _6 |7 A. H+ ]
  90.                 Next' ]: o9 R9 R7 E4 g& e" s! e5 X
  91. 1 L# w% M. k% m1 x
  92.             CaTCh ex As Exception
    3 t- ^  F; E# n9 u  [, u
  93.                 ' NXOpen.NXException: Current object is not smart' a- I& i& X: G& A% G0 Z& d
  94.                 lw.WriteLine(" Error: " + ex.Message)" Q3 p4 S$ ?$ P+ _) u& Q5 v5 _! v
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    " b0 b5 B) X! y- _! @
  96.             End Try
    6 c- {1 d: E- Z$ c, B
  97. - N2 G/ p" [% Y2 e, v
  98. " s! ~) s$ j  p0 \2 E3 q4 p
  99.         End If0 t( {# I8 L. B+ W! |& t4 I) C

  100. & V0 m2 Y' b) Q# Z$ q

  101. 6 K. R2 U) W4 p# Y4 `1 l/ F
  102.     End Sub5 m' O+ y3 h. J# r

  103. / j/ [' C6 _3 v
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer8 J7 I1 P1 e5 B/ g; e, [* Y
  105. # {; P8 h* w/ Y+ y) I
  106.         Dim base_pt As Double() = New Double(2) {}
    ; I' V0 O9 T7 A* @. w& Z
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null; I; r8 k- ^$ T5 C  Y
  108.         Dim response As Integer = 0
    ' L0 V0 j- O" \# z, E! q% P- u
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt, t8 q& ^( S, d( j" I% B
  110. * n! Q, h7 X4 e1 |+ x
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)" A2 o  S+ G4 k; ]8 ^
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    & p5 \# i; I0 `( s+ I2 ~
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    ! `6 Q/ p; P- O" l- m
  114. * l$ u( ~, ~+ z: F9 D0 f, Y# o
  115.         Dim pointLocation As Point3d; n; N; ^) N- y
  116.         pointLocation.X = base_pt(0)
    % ]" S. C. L  s1 ?6 J/ F/ J3 M
  117.         pointLocation.Y = base_pt(1)
    3 N2 ?- i, f5 N- `
  118.         pointLocation.Z = base_pt(2)
    ; [5 I6 N  R6 }" {
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    1 k2 ^; x( ]* M
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    $ ~0 U' G: @, u' R9 r4 A' J

  121. 9 ~  [7 ~) j% u$ u
  122.         Return response2 s2 y' C* T/ f9 T& H

  123. : _1 N3 e( |0 ]
  124.     End Function! E% ?7 G& C. T

  125. ! e* K) c' i* k% @; F/ J4 J( p
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
      p% j$ z9 I2 k: C# M0 A/ s9 Q0 J

  127. % x2 {5 b% j7 V& f1 {0 y( I
  128.         Dim selObj As TaggedObject; X5 W0 c8 E) F% t( _
  129.         Dim theUI As UI = UI.GetUI: N7 N* h' g0 q1 Q/ [$ t3 x8 j
  130.         Dim title As String = "Select a Point"
    ) M, V! p- r* U3 F! ?
  131.         Dim includeFeatures As Boolean = False! x: E: k. P1 h" O% J8 s; l
  132.         Dim keepHighlighted As Boolean = False% i6 w4 V+ f( y9 Y  y
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific2 y) b1 N1 J6 Q3 ]" l% I
  134.         Dim cursor As Point3d
    0 e4 Z% L$ Y. J8 ~2 I5 k
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly( v/ [- p7 O3 K) b& O$ k" l4 E' C/ Q
  136.         Dim selectionMask_array(0) As Selection.MaskTriple
    3 p3 w- \6 D  @0 o3 G6 a& Z& r- Q  _

  137. 4 k) F+ [0 s, `/ I( T
  138.         With selectionMask_array(0)5 k: ?5 V/ @* N9 J" L
  139.             .Type = UFConstants.UF_point_type6 P) R9 k$ v' |4 P
  140.             .Subtype = UFConstants.UF_all_subtype$ B- z4 g) z7 c
  141.         End With
    6 M0 m) X, L0 v/ H: O2 ^

  142. 9 U( ]- E. B$ Q4 ]
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _) e) Z& b2 Q# U
  144.          title, scope, selAction, _) s: y- ?3 p; s& l; {- @  c
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    9 L- M9 L1 Z" H( [9 |3 f/ D) u
  146.          selobj, cursor)
    9 Y3 ?4 o7 I7 n- C/ }
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    ) V6 w6 v5 }+ E
  148.             selPoint = selObj( ]: Q4 p2 p7 [  L& Q# I
  149.             Return Selection.Response.Ok
    2 \0 \1 [0 m  {9 n
  150.         Else
    2 |7 z1 ?0 m( |, t, S( k
  151.             Return Selection.Response.Cancel; x# J) ]/ f. u/ [4 ^2 H
  152.         End If% Y# E' M; d9 I9 Y7 v  z7 P3 H, ~
  153. , k4 t" ?# }% p7 q! r, b1 b( B; s
  154.     End Function7 B/ A; F0 G8 n" `% L

  155. - h: N' e8 k# D) I- y/ c2 p
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    9 Q* h8 |: k! U* {
  157.   Q. B/ `/ ]% W$ }/ y  K
  158.         Dim result As Double- R, d' {- p+ \* h
  159. ' W; K- [$ G, L7 o2 s1 j1 k
  160.         Try# }7 ~/ @" K  c% q. b4 H5 r) |. q
  161.             Dim nullNXObject As NXObject = Nothing
    : L$ f; [0 W! y2 W0 {
  162. , J% Z' L1 e; D; u
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder, O* H$ ~+ Z$ s* w
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)* {. b7 X5 U7 U) ?  P
  165. # e; B' z1 r: h3 }
  166.             measureDistanceBuilder1.InfoWindow = False
    # }9 J; h/ R/ K+ H* @9 |( b
  167. 0 p3 Y) G: u4 B7 n+ s4 z# @
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    ' N- N1 L  |7 d' v2 P
  169. : f* [- d) R" a7 Z3 C5 z2 S
  170.             Dim nullUnit As Unit = Nothing( {- Y1 X, T2 K4 Z/ B# h% ?3 r
  171. # Y' W( t4 D- x7 ]2 q) ^8 g: Q
  172.             Dim measureDistance1 As MeasureDistance
    3 b/ L$ a1 w+ b7 _! y
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)9 M' Q! r* l! N- T7 `
  174. - e! W" L" i* T; a! s3 Q' L: g- j, h
  175.             result = measureDistance1.Value1 b3 q6 @6 _9 ?( I" m1 g% Q
  176. , j- {6 l# O/ y: e% y: `7 Q$ l& H8 N6 ]
  177.             'measureDistance1.Information()
    1 B( k* Q0 ^/ P9 D! S

  178. : N5 H9 G  P# ?
  179.             measureDistance1.Dispose()
    7 k0 N. X$ o/ s0 k* u; o. M

  180. " h0 b! N3 z. o) k9 u3 @0 M5 s
  181.         Catch ex As NXException
    + l1 G$ i* I% S
  182.             MsgBox(ex.Message)
    / c4 X/ q" @8 R2 z) g% B
  183.             Return Nothing
    ) E+ J: }+ {" i6 J
  184. 6 W( [9 v! C( @) r  L1 z% n4 T  z/ j
  185.         End Try' k) I4 ^1 l! v% ^2 e2 y
  186. ( b7 k: F+ |9 h( g9 v# C
  187. 8 Y. x6 }2 q4 L# M
  188.         Return result
    ) @& A" [" n5 V2 |+ [# n& Y7 O
  189. % \4 i2 X+ R( j; n, \" B/ m# e
  190.     End Function
    + I7 O5 m+ C$ A0 F3 E9 s

  191. + p) i0 E1 A$ J  O; s8 t
  192. ; v# j: Z  l2 J( l) G
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    * h2 z1 ?3 k" H4 o

  194. 6 t* G- J' ^/ e0 H& {0 z3 I
  195.         'Unloads the image when the NX session terminates
    8 q- }# f; |2 M
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    7 K' }: w7 H; t2 F) D. f* Z

  197. # y2 X6 ?! c7 r% E
  198.     End Function
    ' f5 C, L' J7 D# @

  199. + f* H: I- U1 B$ H- l5 [$ T
  200. End Module
    $ X1 T; P% L1 ?- `7 c. e" s" U$ [
  201. </P>
复制代码

1 W- d5 G9 U" R+ C2 i! D
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了