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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x

3 I6 x, @" T3 o/ W/ m# h8 }1 YNX二次开发源码: 装配中选点,并报告父对象
# t; i; X7 z5 s$ _

  1. 3 u/ N/ W; N0 J( v$ ]
  2. <P>Option Strict Off& V4 ?2 k. u7 c2 Q
  3. Imports System5 \# P5 T& S5 i% ^' n! N  p' g
  4. Imports NXOpen
    ! `  ^1 Y# w4 W% W+ C
  5. Imports NXOpen.UF/ H, x  H6 d1 }- C  Q* `/ q; M% ?

  6. 5 ^6 o& r9 B$ A% u, Y3 B
  7. Module Module1- ?% h4 E6 z4 Z, a2 S2 B

  8. 8 @$ [  C# B& X* q' ]
  9.     Dim theSession As Session = Session.GetSession()1 a8 k' D) L2 C( G' t7 i9 h+ W
  10.     Dim theUI As UI = UI.GetUI()5 u7 L% @& P" {
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    / r1 k6 r  L6 g
  12.     Dim lw As ListingWindow = theSession.ListingWindow. h" y5 x6 X+ p. s7 ]4 D5 G
  13.     Dim pickedPoint As Point
    3 Y/ K4 u) p) }( v8 u7 y3 I: S
  14.     Dim myModelingTolerance As Double) a1 c% ^7 w% ]  p$ h

  15.   Z% {* }" M( v4 |
  16.     Sub Main()
    + q( d8 ?" G0 [3 [' c/ C

  17. / A9 p$ N! B! C6 z5 w1 f
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)9 S0 K. {& D- K  F" B

  19. / G# B( j' S6 j1 L  t( t
  20.         Dim workPart As Part = theSession.Parts.Work* g  a% P6 @: C
  21.         lw.Open()1 S/ s  I* e, @7 d+ Z' W
  22. 1 _" ]3 ]: p. j7 F1 y
  23.         Dim myPointTag As Tag3 @/ Q2 P( w8 C8 ?& A

  24. , q% Z* Q$ ]- w5 u8 ^
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    1 p) a" D) z7 V& t( c8 D+ D/ f
  26.             Exit Sub, G+ I/ ?4 i8 {; B
  27.         End If4 z. x* K% @" F. e+ e% V

  28. 4 _) y) O8 S: Z# ^) f, I  v
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    . E9 ?! I  r% m. A+ S7 U

  30. / J" `2 z# o2 w8 b$ ]) K1 ?
  31.         AskParents(myPointTag)
    * }2 u6 ~, e8 Q$ Q5 W

  32. " X# j9 }* I2 ?2 w: L7 q. @
  33.     End Sub
    ; ?- \# D* i" B; u) Z

  34.   P2 Q2 `6 k7 m' E0 W
  35.     Sub AskParents(ByVal objTag As Tag)
    , `, S* F, u+ @- F* W) R
  36. & Q5 h  k8 D7 x1 L( ^/ v
  37.         Dim n_parents As Integer5 `5 }, F6 F/ A, P# R
  38.         Dim parentTags As Tag()7 t' b2 ~' e6 y* h) y0 r
  39.         Dim myPoint As Point
    8 W* G8 e: }6 e+ R( J( h) a
  40.         Dim myEdge As Edge
    " |- G* h- A. ?  l  N, r
  41.         Dim myXform As Xform
    " t6 d& e) L$ L' S- z
  42.         Dim myTaggedObject As TaggedObject
    + U3 l- H7 \  @
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    . [( K5 z0 a- [8 v

  44. 1 a. q2 R2 p. G' V7 {, M  Y
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    0 h; T0 t7 b2 h/ m( ?" L
  46.             myPoint = myTaggedObject
    8 _$ Z( `1 f" q
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)* `8 |6 |, _0 R: F. E
  48.             lw.WriteLine("")! l3 P; m7 ]2 r5 Y' O/ Y2 o$ E

  49. ! S. U' n, F; b0 c
  50.             Try
    5 a% f5 y- Z, }  y
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)6 i4 P# {4 v* v) t( D' p
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)- s$ G% p* o+ J( u$ i
  53.                 For Each parentTag As Tag In parentTags
    . Y% d, E+ J) g- E
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)8 F  a. }6 D8 x$ r7 |, W4 E* _

  55. 0 m1 ?) l7 [: R' c1 c
  56.                     AskParents(parent_object.Tag)
    9 X  G  L8 \( @) l) l5 Q. |

  57. 2 G* F* @% e! C# ]7 S5 C4 L
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    3 C8 A, p! @7 I% X
  59.                         myEdge = parent_object6 j, I% b/ D1 o& t: d  U. b" P
  60.                         lw.WriteLine("")
    % H; Y. A! d  b6 s* _# ]
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    " e7 A4 Y0 c) w, h; V8 _
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)* U1 T5 |- z  U) ^2 q3 @. k
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)# x- {7 B* ~/ u- X
  64. " k5 n  [! ?- x5 T5 |# [$ S9 e
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag6 e3 z1 S& v' b. c* Q5 w2 b3 m
  66.                         Dim occTags() As Tag. A# R. p3 g  b. \
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    $ L! d) d& |( e
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    5 o& k  R  Q5 N0 |6 k4 N/ I% O
  69.                         For Each temp As Tag In occTags0 j) @' C8 i9 {
  70.                             Dim myComp As Assemblies.Component, O1 [" r3 c4 h2 t: t/ X) r
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    . V9 b$ d) t6 d1 F( y4 x
  72.                             Dim myCompPos As Point3d
    8 X) H: G$ c( k3 M5 x$ o
  73.                             Dim myCompOrientation As Matrix3x3+ u* H. x- n  }$ `# Z( Z# _6 Y
  74.                             myComp.GetPosition(myCompPos, myCompOrientation): }* p/ j& D. y

  75. # ?: b- e* f  h+ \6 w/ ?. [) O
  76.                             lw.WriteLine("")- G3 b; a1 J8 J* W: M! R
  77.                             lw.WriteLine("component name: " & myComp.Name)
    % q2 v) ^( y1 O- B' z
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName); L7 L3 b6 a. Z; j6 [3 Y$ k/ K( E' ?
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    0 i$ c/ d: y5 ^# f. U* r

  80. 4 g1 r% ?! x% t7 L6 C. |
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then" R$ I7 x0 B, i2 B  T' X8 D
  82.                                 lw.WriteLine("** This is the component that was picked **")
    / B* d8 U; E! z6 |5 d
  83.                             End If5 l3 V6 m) Y8 E& y
  84. : g$ u7 s# P" G5 k' n. I' K/ O
  85.                             lw.WriteLine("")
    0 V3 T4 {4 x( p+ I. L  R
  86.                         Next
    $ {1 g9 H+ ]0 [0 O7 Q& N
  87. $ _* q, [' J. ]( b
  88.                         lw.WriteLine("")
    + j) T3 ~! j# F7 l8 d2 _( U
  89.                     End If0 D6 I9 w& @$ y1 k3 F0 G; S. j
  90.                 Next0 }0 g" V& C7 |4 |1 c

  91. * h: l% C: U' c. p
  92.             CaTCh ex As Exception( l0 W7 B8 |: k
  93.                 ' NXOpen.NXException: Current object is not smart
    8 F, r# p% f# \! ^5 t9 G6 `
  94.                 lw.WriteLine(" Error: " + ex.Message)& X  }( s6 |+ \, T# I+ C. A
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    ) u) B% L- @6 o! K* x1 z, x
  96.             End Try
    & e$ p* g; {4 |& ~/ ?
  97. ! T. |$ D& p4 R, _! s1 \$ B. B
  98. / Z; c( f# S  y2 H0 J  p- S1 a$ k
  99.         End If# b" N- O( [4 B3 h$ N& i3 s0 y

  100. 2 w* X; R7 t3 C; ^

  101. 8 m+ c/ A: ]3 N- h# q; l
  102.     End Sub
    7 Y( c# x* a, W4 }

  103. / }: Z* T7 `. J% c+ `1 w$ F8 j+ ]
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    : O2 c. g, ]/ m( |. M# U
  105. + b$ x; }0 M" ~( h5 L, B
  106.         Dim base_pt As Double() = New Double(2) {}
    # s  b9 V+ Q  F' Q5 x( z" |
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    9 l( ?" k4 o- H2 u3 p
  108.         Dim response As Integer = 0$ p+ B5 c- p' B+ b2 [# r) C
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt/ \, V! H% `0 G1 R5 K- A
  110. , X1 E4 |+ }9 s( h% I3 \6 d  x
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)9 a0 @1 p0 }* _% Z
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)6 _; A& g" G: }- o% A3 c6 K
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM); R+ v4 G* W" r# P% y

  114. 1 N/ T# P) I' f7 h' z
  115.         Dim pointLocation As Point3d% D2 Q6 |' m' e) N
  116.         pointLocation.X = base_pt(0). `/ u. ?9 H% E6 i5 A5 W# e
  117.         pointLocation.Y = base_pt(1)
    0 x% e) S& o2 @. ]/ l  y2 H' p" A
  118.         pointLocation.Z = base_pt(2)3 b, `  \4 I, ?' J" ]3 {1 z. J
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    1 q0 j% y4 c: d6 h
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible); n9 U. R% w: e$ u9 c
  121. 4 |8 P% [8 D" w1 C# ?0 V9 p
  122.         Return response
      z# x: y4 @6 c) r9 Y& ~# v% k) l

  123. 4 T4 k$ A9 f, k+ l4 i4 V* Z  p
  124.     End Function' U2 f: ]& Q& O

  125. ) ^) k1 c3 _% m# o6 ^) y
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response" w3 E( ?0 o0 T& U+ Q6 _

  127. ! U" \- j3 ^- B5 G. Y& S' [
  128.         Dim selObj As TaggedObject# u( Y! |3 K5 T, J7 Y
  129.         Dim theUI As UI = UI.GetUI
    ) y: |) G. \, X- F' a
  130.         Dim title As String = "Select a Point"
    & L; t+ ^6 e6 P, j5 J& f; q0 d& s
  131.         Dim includeFeatures As Boolean = False
    : e1 e$ u' f3 e) H# g' |$ C
  132.         Dim keepHighlighted As Boolean = False5 m" p# ~* i$ E3 W* k5 M  h
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific/ g9 Z0 ]  Z, z3 w
  134.         Dim cursor As Point3d
    5 S: d4 _/ x9 h0 F4 }$ V
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    7 C4 f6 x% K9 d9 I
  136.         Dim selectionMask_array(0) As Selection.MaskTriple# s' \% _* G8 L' p! O: Z1 A
  137. 3 ^8 |9 S$ x) \# }! J, o5 E
  138.         With selectionMask_array(0)6 ?( a4 ~) X* S. x7 w9 a
  139.             .Type = UFConstants.UF_point_type1 J! {* J& s! a4 }* U- D
  140.             .Subtype = UFConstants.UF_all_subtype9 T* a* a# t# F; E
  141.         End With  E& k8 @- n/ R; D
  142. . @: N/ z. d" c% j* }
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _5 V/ R& {7 N2 O$ T4 R
  144.          title, scope, selAction, _
    ( B4 \7 K$ D- z$ W# c  [
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    . n) ?& s  L& }. x0 {8 [
  146.          selobj, cursor)- f5 A8 M" m9 _' ]
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    - m  @4 W9 d- F  i2 M4 d6 D5 k
  148.             selPoint = selObj/ k7 F, |5 k3 [  U
  149.             Return Selection.Response.Ok
    * O! q6 v* W+ Z7 K  r" y7 U
  150.         Else( a7 p; o  k: f$ B8 G0 z
  151.             Return Selection.Response.Cancel
    & z" F: t" i# c
  152.         End If
    3 _9 {+ k) [( Z8 i: e) @
  153. ( L. U. z8 ?& _/ A% }& B
  154.     End Function
    " h+ b* T, C, D" r: Y

  155. # _$ B, i0 F4 V
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double3 L/ `) `+ a. [0 W
  157. : A9 B" X" x* X/ ^0 Z$ n% d; I
  158.         Dim result As Double
    ) s8 |& C9 P9 c; z
  159. * \# \/ x$ p3 t3 Z$ ?
  160.         Try( a6 q3 g* S1 A& ?. v
  161.             Dim nullNXObject As NXObject = Nothing6 `+ L2 H: e& I% N# f5 E

  162. / L) U# R. d8 l* C
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    . W# w  s9 V+ j& P! \( ?
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    ! Q7 V+ N9 y, \7 n; w! Z) O  Y% o
  165. ! N+ S  x9 N& Z
  166.             measureDistanceBuilder1.InfoWindow = False
    ) b1 F  x$ ^. E% {  y% d

  167. # H5 v/ O  l9 N7 G' B3 k- M
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum' _  L% s7 N- G* O  @

  169. 8 f: ^, D# ]' h8 \- {$ A, ^1 e
  170.             Dim nullUnit As Unit = Nothing% k& R8 X6 \  i4 A* `; r- G) u7 h

  171. " G. K# y8 r0 O+ d5 K9 z/ [
  172.             Dim measureDistance1 As MeasureDistance
    7 D6 x* M1 L8 Q7 c
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    ' @. q% f7 u* Q& X7 ?# P9 W$ J
  174. $ e" x+ _- l) K& N' _# h' R6 e1 ?
  175.             result = measureDistance1.Value
    4 Y2 D& F2 j' O# s
  176. : C2 E* A& ~# b, N) \+ V% Z1 y
  177.             'measureDistance1.Information()
    ( i1 N& V6 V9 W0 \
  178. # q6 V: R6 d) k
  179.             measureDistance1.Dispose()5 N1 X) Q# o' A' w) ?* a

  180. 4 _( k. ^  C/ n9 E
  181.         Catch ex As NXException. l* N- l( T! I( i# J3 P: y0 Q" l
  182.             MsgBox(ex.Message)0 m6 F% B9 M  {% a% K- f
  183.             Return Nothing/ c' o. m* t4 S2 a6 E

  184. ! E* E8 t1 a) o6 z! F7 j6 |9 E
  185.         End Try
    1 S4 n& J" Y- \2 j5 {

  186. 6 d( c0 G8 @+ n5 z, x2 E6 Y
  187. & L/ j5 d1 n* g. ~& i
  188.         Return result
    8 {3 @, e7 L: P3 r' b: c
  189. + t; O, D# |5 K
  190.     End Function; X2 j: v. T9 a' f" a/ q) k

  191. ) `3 t. w' d, y" `# B/ r
  192. ' ]9 Q! Z' [3 Q, ^9 h
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    0 e3 e4 v+ L4 A
  194. 5 o* u5 P4 f) I! u, ^' d( y
  195.         'Unloads the image when the NX session terminates
    & D; n4 K/ N) ^6 Q) n
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    : m" @9 H/ n1 n6 a: Y
  197. * X$ T4 i, w  S+ i
  198.     End Function
    / z& h/ N8 A, b1 Q+ l
  199. 9 N1 b' q& |7 o+ ~. }. z
  200. End Module
      v; f# _+ z2 ~: c
  201. </P>
复制代码

. C6 K( f0 ]8 S/ L3 b) R9 Z+ w
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了