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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
  u" S) C- A3 a9 t& Z
NX二次开发源码: 装配中选点,并报告父对象5 D7 I" g4 x# C# A
  1. 5 u' b2 r% G& E' K1 b* f
  2. <P>Option Strict Off
    ; ^6 E$ x+ V" E2 d- L" ?1 z
  3. Imports System3 ~( h& \4 S1 H, |- }
  4. Imports NXOpen
    * e5 y" h8 \+ w
  5. Imports NXOpen.UF
    - T( o5 d* B. I, g  J  J
  6. " c- u' n8 ^' h  G4 p0 e; C( I
  7. Module Module1" K( c' K5 H5 _& U2 s8 B

  8.   S4 d- w5 T7 s
  9.     Dim theSession As Session = Session.GetSession()
    & X! j+ R4 O- ^8 U. m
  10.     Dim theUI As UI = UI.GetUI()# N' ~% f7 {' w) z  m: j
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()  @8 S( X' W7 f4 [7 A# H2 I
  12.     Dim lw As ListingWindow = theSession.ListingWindow& |6 N+ j! O5 g+ ?/ u
  13.     Dim pickedPoint As Point/ M. Z' J7 z$ X
  14.     Dim myModelingTolerance As Double7 \- E% ~0 @2 |  O
  15. ( z. u8 m) X- \! q2 ?2 k
  16.     Sub Main()  Y) d8 K$ n. J. a; d9 {$ [) O

  17. ' c4 |- j& {( V5 W
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    ) V. ^- ?4 e! s. F

  19. 1 M2 W5 q; T1 b8 K
  20.         Dim workPart As Part = theSession.Parts.Work" J; A. _! S1 K
  21.         lw.Open()
    ! _7 Q! a# b' U& v# x& I1 v
  22. 6 K; S1 P( f1 Z; Y4 G
  23.         Dim myPointTag As Tag, G1 v# S. d. o

  24. ! u5 G3 H" ^: V. n, e
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    - x) n0 [4 j' T1 j& G7 O# L' {8 |
  26.             Exit Sub
    , d# n8 ?. R& q7 G1 R1 T
  27.         End If4 q+ z* e' S! R

  28. 8 R$ Z* j) f. [6 F2 F3 \3 v
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    6 c6 n* P/ T' I5 T. b5 k
  30. ( Q  U  `; b5 o
  31.         AskParents(myPointTag)+ s% W+ _3 I( T0 E' `  W  m, u# I3 g

  32.   ?$ i: |7 b. v- y
  33.     End Sub* T0 o2 ?: [9 b4 U  P# `! r  k

  34. ) U6 X* a  b2 C% J3 E4 u6 }
  35.     Sub AskParents(ByVal objTag As Tag)
    9 K6 i& k* ?  s3 |
  36. 8 G: _- J: Q0 Z* h$ ^
  37.         Dim n_parents As Integer
    / S9 d; o3 ~5 b! t* \/ U% I
  38.         Dim parentTags As Tag()4 f" T5 G; b+ E0 b+ a, D* Q; p, o2 ~0 d
  39.         Dim myPoint As Point
    9 i$ d( ]% D  I' u2 ]& s
  40.         Dim myEdge As Edge
    * Z& j4 _! C! G4 [) K! A
  41.         Dim myXform As Xform9 R9 h: I9 ?; E9 f; R5 x
  42.         Dim myTaggedObject As TaggedObject
    7 W9 J1 A3 f+ G& r. o! Z
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)9 s  F9 `  W* @4 e! v

  44. ( V- o# w$ o2 [! j, X
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    & C9 n! }9 Y. t  B/ o, s7 ]; p
  46.             myPoint = myTaggedObject1 v; B+ i0 f7 \+ p: c5 h  N
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    " z. x' k+ N- y- i$ b5 [
  48.             lw.WriteLine("")' t$ D) m- i$ D1 [, h9 J

  49. - U# Y# `# X. L* L
  50.             Try" V9 j) G  j3 e3 W. f$ \1 C
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)$ R; d- [% H% Y+ a% G' j
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    % h) V9 ]/ C8 I! K' g
  53.                 For Each parentTag As Tag In parentTags
    9 g! k, v% y, |/ I% M- S
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)* R# C7 p" W5 M' X" P& p8 n0 h8 y# H

  55. 1 f0 a1 F1 B( t5 [6 l) f8 i1 \
  56.                     AskParents(parent_object.Tag)1 t" }/ j1 d9 }) z; \

  57. / f/ \; j5 _% D3 J% t
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    3 _% }+ ~& b0 u: M) o0 @3 R
  59.                         myEdge = parent_object) N3 [" r. p5 L4 m: A. E
  60.                         lw.WriteLine("")3 s9 @. ^! M; r
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
      P. Z" S; q( P- O% ?6 B/ \0 K- z8 y7 [
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)0 s; ~6 K6 Y) R7 n  S0 Y* V
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)5 H( k. h8 Y: S, P9 e
  64. ( D( ]1 `& L) F) _" M9 @8 u7 f
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    ( K2 o8 I: w- f& k  b) V
  66.                         Dim occTags() As Tag
    & b; q" S2 ?, F% Z0 |
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    % T8 V7 k1 j+ S. x0 J
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)5 X' K6 T3 n% ^5 Y6 v- d+ J
  69.                         For Each temp As Tag In occTags8 v* l$ C9 G  k' n0 V2 ]# G
  70.                             Dim myComp As Assemblies.Component
    " X$ I' f+ i# |5 h9 G
  71.                             myComp = Utilities.NXObjectManager.Get(temp)* A0 M, U- w  p  }, ^
  72.                             Dim myCompPos As Point3d
    - ]4 X; A" j0 C# I: D; t1 o; W2 e6 |
  73.                             Dim myCompOrientation As Matrix3x3& _* s7 I% S$ G; g0 c  p
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)
    2 h' i2 _# ?1 t

  75. $ A; O9 [. a9 R2 g
  76.                             lw.WriteLine("")+ \2 K; M8 N0 s9 s/ k& g4 L
  77.                             lw.WriteLine("component name: " & myComp.Name)6 b8 A$ Y$ C0 V* U
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    ) i1 I4 S7 b% c: s3 O; l
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    : Y+ K5 |( V  z% U+ J) ^

  80. ! L- E1 u; W- v) }
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then2 R. |" D& E! ?$ k, `
  82.                                 lw.WriteLine("** This is the component that was picked **")
    - ~, K, `, ^$ ]% p
  83.                             End If# Y: |- ~+ Z7 C% A
  84. ! s5 `; f1 x0 s( h! w# M/ X, ]. {
  85.                             lw.WriteLine("")) E. A/ U; i' O. V: s0 w* R2 b$ q8 Y
  86.                         Next
    , g$ q" J( h, F& ?
  87. % {8 C" N2 d! i( a7 I, O8 B
  88.                         lw.WriteLine("")
    : W; e' v+ W8 D- }
  89.                     End If
    & w6 j' w  D# d8 O
  90.                 Next
    $ y2 x# L0 e" z( J1 @

  91. 1 d+ L+ _" j2 m9 C$ X$ P( j0 T& ~
  92.             CaTCh ex As Exception
    " r# n: Z* ^/ n" q
  93.                 ' NXOpen.NXException: Current object is not smart
    4 h( X8 Y2 j: |
  94.                 lw.WriteLine(" Error: " + ex.Message)8 l+ h; m  P" y4 Z6 r
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    0 C2 x( g# ^* z* J% Y
  96.             End Try. k. O% x  V; L; `

  97. * f; z# O* ^; f1 U+ R5 b, k  S$ c
  98. ) d1 |; d- Z/ }) D0 k
  99.         End If
      c2 S# p+ J- @9 H/ z8 G

  100. . L# k4 j0 E# I" U+ s* Q+ q
  101. . H+ `% _% t) u  S  S0 t& L
  102.     End Sub; J& U( t% Z/ l/ v* w* ~/ k. D

  103. 4 F# M) j3 J# R7 u  f+ v# l1 w
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    2 A# T! d( e9 T9 h

  105. / ^# O  }1 c7 }' e( N- _
  106.         Dim base_pt As Double() = New Double(2) {}* S- y& V- j+ Q" s8 J
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    5 Z# h2 V- k8 ~5 u6 V
  108.         Dim response As Integer = 0
    ) |$ g4 N8 J2 F! h5 h
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
    # y8 {  U$ v/ G: K( n
  110. # e5 s# p3 T6 r, i9 X
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)( e) [! b( O8 k3 z+ a: M
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    ) Z- f2 q2 p( n
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)/ `9 p" ^; H! P
  114. $ r7 A6 B8 o) y7 Y2 Z: Z! F4 a, V
  115.         Dim pointLocation As Point3d
    + N! e" F" \: D/ V
  116.         pointLocation.X = base_pt(0), \& v) A1 x2 e9 u4 h2 Z
  117.         pointLocation.Y = base_pt(1)0 d, r5 R1 i8 N+ ?) q: h
  118.         pointLocation.Z = base_pt(2)
    1 L' q4 ?1 ~' x/ ~4 S% G
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)% e* n& p: T" B  X
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)+ G( h3 e: h, u6 q9 {. ?

  121. 2 U4 e& g% x' K1 `/ M5 v; g+ P, Q
  122.         Return response
    + }! s3 I! j+ Y8 H( L% G

  123. 5 f- V( z. P4 L0 ~/ D* S7 G
  124.     End Function( B4 l, L9 L9 J1 _+ `) E

  125. ! v0 h9 W* Y) ]6 ?6 s7 [' \
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    0 ~3 H8 b; X7 z* Q
  127. 7 m- G) _" d8 n
  128.         Dim selObj As TaggedObject3 E* ]" n6 I6 f% `+ T' h
  129.         Dim theUI As UI = UI.GetUI0 i7 Y, t9 }, i) \7 f
  130.         Dim title As String = "Select a Point"$ p$ j2 @! A% f  p0 U0 S
  131.         Dim includeFeatures As Boolean = False9 w# V7 l  E4 Q
  132.         Dim keepHighlighted As Boolean = False0 f6 N* O5 e) [- t, |  P/ H
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific' O6 G  H$ M0 E; d( j
  134.         Dim cursor As Point3d' |/ D9 ^3 b& p2 f& ^5 ]
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    3 n; A1 {( e- S( x, [
  136.         Dim selectionMask_array(0) As Selection.MaskTriple
    ! B8 t  D1 `  n' T$ q

  137. & |# d( e$ P3 }. |5 E! d
  138.         With selectionMask_array(0)5 `6 ~- @, Z, z2 W7 T7 {5 \' ^) @6 i
  139.             .Type = UFConstants.UF_point_type* X; Z, }7 {. w, [) e
  140.             .Subtype = UFConstants.UF_all_subtype/ s' i; t* D; F' T# q5 \) [
  141.         End With
    9 s8 @: m# L8 l' R) u  ?1 D0 @

  142. 0 L# v- f; w+ D2 }
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    1 i4 e3 Z0 H# ]3 m7 C
  144.          title, scope, selAction, _; g% Q- }, @' {, d+ H
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    ( y& v7 j) k& T# L$ R2 }$ ^
  146.          selobj, cursor)
    1 K( H$ s7 g# e7 Y
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    - |# S5 V  k* A2 d2 e/ C
  148.             selPoint = selObj
    $ ?% j6 n& ~# j# g
  149.             Return Selection.Response.Ok/ r0 G5 S* E4 ]  y
  150.         Else1 N& Z) v# W2 l4 O! X
  151.             Return Selection.Response.Cancel
    3 g. a5 h: w* d( x& Z, Z. V0 a
  152.         End If. b# @$ }( g1 n# c8 S) a0 A# G

  153. 2 A- s  N, {# B" J$ G3 ?2 N
  154.     End Function
    2 |' c9 o$ T" D  g9 g. P, d

  155. 1 h8 @8 V& [$ w
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    * f# V  S( r8 v7 r

  157. # Y! G. ]9 A; U8 m( g$ k/ l
  158.         Dim result As Double
    7 z  @! z1 X5 q

  159. * @: M% C, v8 h; g$ H' Z
  160.         Try7 }- I3 E* l6 C
  161.             Dim nullNXObject As NXObject = Nothing' j/ |( \! }+ e1 j" s4 \, D
  162. ! M# b+ U  Q# F- O; p+ @; j
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    4 l, \  p; [) y- |% L/ Q0 T
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)6 c. x' Z" O! H
  165. 6 U0 z# A9 U! M4 p# `" i0 a
  166.             measureDistanceBuilder1.InfoWindow = False' q/ k; d/ `6 w# `6 o8 ]
  167. - B; u/ Y! c! p' q  U
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    " ]# Q- W" w) n8 z! Y

  169. : {7 b4 K2 |& _% W
  170.             Dim nullUnit As Unit = Nothing! x! o) n  e3 b* f3 a

  171. ( W$ `% C6 ?: Y
  172.             Dim measureDistance1 As MeasureDistance0 G5 I0 Q0 [+ `2 S
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    : ^6 l( j7 n  |, Q2 R) X/ f7 Q

  174. ! E6 p: V0 b0 w, j
  175.             result = measureDistance1.Value
    4 p# z  O  p( ?* K3 u8 S  D

  176. 4 g) v: n7 O0 [) n
  177.             'measureDistance1.Information()
    $ E* X; H$ ~5 o2 d: K

  178. 4 }3 b$ C8 a  e0 Y# f  j7 `# ~) }) u! l
  179.             measureDistance1.Dispose(). ^7 N2 w( o5 |3 g; Q4 j, E

  180. 2 N) t! g: r, `& ?3 t
  181.         Catch ex As NXException% X; z1 ^3 I3 [' h1 |
  182.             MsgBox(ex.Message)" n4 t6 N5 U4 l% r  G. m
  183.             Return Nothing
    + j/ P4 K& ]3 J$ t# N, L, V7 [
  184. / \: r% k- e4 V! }( h7 X
  185.         End Try3 i/ W: E% ^5 ], m. J9 |
  186. ' X) z9 ?7 `& r& Y. ~
  187. 3 h7 d9 m4 v% P) ]) _" z
  188.         Return result! l2 s6 I1 x; n# n" l
  189. 1 V5 s% T8 M7 I
  190.     End Function
    0 C1 l- L0 Q; T8 v
  191. & j0 N) H1 L7 [; }
  192. ; z) Q) j7 x8 s0 t
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    : j- P3 [  D) O  r7 k

  194. # }5 L6 X8 U6 [7 e( c
  195.         'Unloads the image when the NX session terminates
    " k9 _' n/ r# I8 I1 S2 u$ S+ P
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    6 u+ w9 u* Z/ u. ^4 F* n7 M

  197. ( i, P4 ?8 J9 K) v
  198.     End Function
    . `/ `7 p3 h5 Z8 E

  199. ! M  g2 _) b, o) k- j) l- b0 g! N( X
  200. End Module
    0 t. @# I2 o' S* ^9 @+ a- z5 D; b
  201. </P>
复制代码
3 ~* L- B2 H. N5 J$ I: y& f% G
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了