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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
( F6 a1 D8 ~' l5 ~* t
NX二次开发源码: 装配中选点,并报告父对象3 g+ |* A6 _9 ^- ]7 F- N
  1. 6 p. s7 r6 I# b) o5 j
  2. <P>Option Strict Off! G5 G% S( q: _; A0 s+ B3 i
  3. Imports System( p3 ?; c9 I6 v& r; p
  4. Imports NXOpen' I/ i0 P% o: z1 e$ [& {1 n
  5. Imports NXOpen.UF) j: H2 i/ I: h+ Y
  6. ! B( G6 }3 K8 @  f
  7. Module Module16 ~9 ~. C) v* V2 l) e

  8. ( t1 @# {/ z0 T" p1 S
  9.     Dim theSession As Session = Session.GetSession()/ j! e+ @9 m2 w8 W/ |
  10.     Dim theUI As UI = UI.GetUI()/ O$ H6 w8 F. m8 F
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()& G( q" x) O! w1 |' E
  12.     Dim lw As ListingWindow = theSession.ListingWindow3 P6 Y3 G% f! Y: y; m' R1 r0 s
  13.     Dim pickedPoint As Point
    9 D9 H) U7 L  f% v' [! T" R8 T! l
  14.     Dim myModelingTolerance As Double; D' }& p) z0 E7 D
  15. . P. f5 ^  P$ h" W
  16.     Sub Main()
    3 s! x) ^. v. d5 M: D

  17. % \% h4 B6 F2 R% N/ {  {
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance): c& e1 Z6 N0 h9 d4 g

  19. 0 M$ {- t) ]4 ?* `" K) x
  20.         Dim workPart As Part = theSession.Parts.Work
    & I4 n& E' W  w& m, r
  21.         lw.Open()  j/ ?1 l5 y6 o+ G7 Z1 c2 Q
  22. ' f6 {  x1 T) \4 W% h
  23.         Dim myPointTag As Tag
    ( k. u# T. R6 P8 u6 \. c
  24. 6 U8 ]& Q$ o4 \: M  r" l
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then  O7 k, P2 q) C* Y
  26.             Exit Sub
    6 i; u- L0 w! A4 A- `; T1 s; S
  27.         End If
    8 R1 V! b# y5 V" Q! s! k$ G
  28. 6 T$ Q& O' @8 L( j
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    & e8 a+ D. Z; X" a

  30. & C4 P8 \8 R/ A# {( @: e9 w
  31.         AskParents(myPointTag)
    & c! ^) z! c5 t0 O# {4 H+ [

  32. 9 z7 P  T) c) k6 T- _- `9 N, e
  33.     End Sub6 G# L( C2 h& ~: Y# g$ J

  34. 4 h6 b1 v0 T" o2 T0 {) g/ q
  35.     Sub AskParents(ByVal objTag As Tag)% E8 o5 H  c4 d% |6 h
  36. 4 M' ]4 R+ D* ]8 n! @( ]+ Q2 I9 o$ W# V
  37.         Dim n_parents As Integer( G6 z5 ^6 w/ H, N2 b' F5 z1 l
  38.         Dim parentTags As Tag()
    # w& X. K4 ~7 M8 i) c: Q
  39.         Dim myPoint As Point
    " c; x2 K& R0 s4 }+ O
  40.         Dim myEdge As Edge
    0 Z6 X  ?- V; s) I
  41.         Dim myXform As Xform5 Z- s1 S" W! Z; A- Q7 t& O6 _
  42.         Dim myTaggedObject As TaggedObject
    5 _' p2 p3 i( \2 w
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    ; X7 ~5 t9 H* d: s% l+ {
  44. / d2 O9 O6 d0 W2 W* T" x
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    9 h! X) s/ X4 C) Z7 |8 o$ ~# ?
  46.             myPoint = myTaggedObject
    ; ~' \' l! s8 \. [
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    ' H  f; h0 O  @# v; U
  48.             lw.WriteLine(""), ^/ ~9 y3 h1 e
  49. 3 K3 e0 n+ u3 r
  50.             Try
    1 }: e8 q- q' m" K
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)- t  C, Y7 O9 s* B  u) M
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)1 S- Q  O- @6 y7 R: V) o
  53.                 For Each parentTag As Tag In parentTags& s% K! F9 l3 u) k; V
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    0 ^& U- n9 ^, C3 B% t2 R

  55. 2 s7 `7 I" e8 D
  56.                     AskParents(parent_object.Tag)# w8 c- q6 c3 f& t

  57. $ O; {, l9 A# e% `
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    ' Q& P' D) ?: e$ h
  59.                         myEdge = parent_object
    0 D9 q1 [4 M$ x# G/ `. ~" q
  60.                         lw.WriteLine("")# d7 K  r. D0 |5 v+ t, F
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    ! u( j& U1 P. i
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)2 u0 _% ~+ f) n, V
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)) f* g+ r3 b6 P) R& c2 o
  64. 3 a$ T; x6 p: K7 V9 p; Y8 U  R
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    ) B9 [6 g3 ^3 ~
  66.                         Dim occTags() As Tag, s. b9 _$ P5 c# H7 E4 H7 v2 {9 u
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    , A9 z0 Z1 f8 e6 ]
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)0 S- R$ Y8 i" ?0 h* u
  69.                         For Each temp As Tag In occTags
    6 g2 p+ _1 }1 z9 K
  70.                             Dim myComp As Assemblies.Component3 L* v8 q! l- V
  71.                             myComp = Utilities.NXObjectManager.Get(temp); t$ l  G4 f- F1 N: f: W
  72.                             Dim myCompPos As Point3d$ N7 l. r+ k0 ?7 `* B% ]0 D
  73.                             Dim myCompOrientation As Matrix3x3: ?! e& t- [. W; J
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)3 _- Z! p3 |* |' v/ z$ U% b
  75. " Y( `1 S8 Q( X) A
  76.                             lw.WriteLine("")
    - q5 b. H: L3 O, y- _
  77.                             lw.WriteLine("component name: " & myComp.Name)4 j/ ]6 o% X# K( r: J) E
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName). b  ?$ ~$ z: W+ J2 r* w7 @1 V
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    2 u' y$ F/ i$ m9 M  V

  80. 4 z8 I9 I& r+ G# _* C! e' I( t, Y
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then1 i2 a. {( u, j3 J7 N8 t
  82.                                 lw.WriteLine("** This is the component that was picked **")
    - i0 K( A. w* l  t" K: o+ s
  83.                             End If
    , F+ G- A5 U) M" f  L6 Q- y6 o

  84. - v8 W# ]8 c# k  }0 r" O+ x
  85.                             lw.WriteLine("")
    2 S6 V9 ]0 a- {% e: f0 Z% v* w, F4 W
  86.                         Next
    5 ~( O  R+ ?  o" E" Q

  87. 6 r! |, x1 r( I- j, a& D: Z2 O
  88.                         lw.WriteLine("")
    4 Z9 A. M/ d0 l2 n
  89.                     End If5 B8 K$ X' o, I  b5 K: G
  90.                 Next0 p. E& G5 m+ D3 r3 K+ w9 e' v

  91. 8 s4 H* Z& l/ w1 U
  92.             CaTCh ex As Exception- Y+ ^7 V& y# b+ x6 d! X, z6 l
  93.                 ' NXOpen.NXException: Current object is not smart
    5 W* Y4 o2 S6 K: \6 Y: v3 B- ?# ?
  94.                 lw.WriteLine(" Error: " + ex.Message)8 _9 u3 m* g( [2 Y
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)' J& u$ L  i( I1 `/ Y' A! B6 P
  96.             End Try3 [1 B& [- K  J: y; {/ f1 I- r; N* z7 q+ N
  97. 9 D' _4 ~/ C' }, w( N

  98. 7 @) b+ i6 P) T( B8 q: W
  99.         End If0 r0 q) A! M7 L' j5 h

  100.   O, ^# q( U' C  E6 V$ A" V6 @3 {
  101. + P" G) C1 S, }- h( X2 u6 h
  102.     End Sub1 b$ [* L0 V9 O
  103. - o/ j$ o: O; w/ g) Y' V  f( M
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    ( y6 S. m, |7 C* b% t; J6 H
  105. # o+ r  A0 i4 a( {0 }" N; G) I
  106.         Dim base_pt As Double() = New Double(2) {}( g! t  D" m+ @# v$ e. I3 n
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    ( A3 d9 W; @2 A( b3 l4 B7 F
  108.         Dim response As Integer = 0
    / q( g. }; t  A" w" T
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt  T; N4 n$ Q9 M! t$ a( O; `  d

  110. ' b% A0 P5 E0 P# g; ]0 V
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    " t; P& v" l2 s
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response); ]( `. F. H; s4 |
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)0 D! q: ?( O: y) i1 _

  114. ( I# b$ t' c0 |/ Z1 @9 K
  115.         Dim pointLocation As Point3d1 b, e( m8 f7 v5 `& L
  116.         pointLocation.X = base_pt(0)
    " Q0 |! b: C9 j
  117.         pointLocation.Y = base_pt(1)
    5 J" k. B' M# N0 p* v0 A
  118.         pointLocation.Z = base_pt(2)# Q" k& a, S+ V- r+ H( P/ _  n) Z
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)+ I$ J9 e( L- c
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)1 e! A% o% ^# x8 w9 ~

  121. . b* |- z& i) \! C( I* B4 i* b
  122.         Return response
    $ _9 N" ~6 F* Q! x) ?, y1 n) E& q
  123. * H3 z; l, ?/ G; Y4 g6 i) Y+ u
  124.     End Function
    - L1 J# y+ o4 K
  125. ! @" ~& r! v, N& [* ?
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    - ]) g, }- K3 X
  127. % L& w4 G" z: q$ |+ \
  128.         Dim selObj As TaggedObject
    ; Y- _( d- E4 }0 p! g
  129.         Dim theUI As UI = UI.GetUI
    ! G) _4 B; g2 n, `8 a6 ^! ?6 d+ [2 r
  130.         Dim title As String = "Select a Point"/ x5 Y, v1 L0 |2 f7 F* D
  131.         Dim includeFeatures As Boolean = False. M6 U. R  o: J
  132.         Dim keepHighlighted As Boolean = False! b$ o. Q/ d& ]0 `& a
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    $ F8 j. _/ W% \
  134.         Dim cursor As Point3d  H( K1 H" I: O1 \7 p
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    , P. m9 j8 O' d9 Z2 L
  136.         Dim selectionMask_array(0) As Selection.MaskTriple- x8 e2 H' y; u5 l. m5 V) F
  137. : ^8 W, O& O7 }& W9 g
  138.         With selectionMask_array(0)% C1 n3 G( p: B* N9 u. W  `7 t  e
  139.             .Type = UFConstants.UF_point_type
    + O$ v$ U! W% W, \* k% u' K* |4 O
  140.             .Subtype = UFConstants.UF_all_subtype
    5 Q/ a- T) @% I- t
  141.         End With5 Z2 P9 u0 z# u- ]; E
  142. , y$ L# W7 G( ]3 a. C
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    ) Y+ X8 y0 k, W$ w0 {$ a4 l
  144.          title, scope, selAction, _; m- U5 T0 ]+ u& A; J$ ~5 G
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    8 c* ^" H( b! `, Y
  146.          selobj, cursor)
    6 b2 n2 m' y$ l( @- m7 N# U
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then  r" p( \7 l* {. W" t( [
  148.             selPoint = selObj
    % G. I" S$ V0 o  r+ E2 y$ x. S/ o4 k
  149.             Return Selection.Response.Ok
    : }8 f5 h5 I. D* g8 j$ w  r
  150.         Else
    # p, i7 c* a3 K# R. D2 a8 c5 ~
  151.             Return Selection.Response.Cancel
    6 T+ z  Y( z7 [2 r# b
  152.         End If7 o) e5 B( G1 p4 \
  153. 5 z, {$ s9 Y: [9 i3 g4 ^- O2 I5 G
  154.     End Function
    " `$ T+ D5 Q$ G) T: K# ?+ j' V
  155. + r2 ]& H/ c  {
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double! [6 r: D$ v' }/ D! o" k$ c
  157. : z: G+ x2 |3 ~; F" d: P
  158.         Dim result As Double
    5 T/ ~; W5 {* X- E
  159. / b9 G7 b7 P6 `' H0 o2 a
  160.         Try
    . }' p  C4 V- }
  161.             Dim nullNXObject As NXObject = Nothing$ Z+ }1 f8 b2 Z5 F2 {2 s( f. |" Z. {3 W

  162. / ^$ ]/ b: O0 ]& v$ {2 F8 J
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    8 n6 z! I- u; l* B+ i
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject), r+ ~, I# x# e- g! K' j8 @. C
  165. + q6 `7 M1 R: C$ n1 I. {8 y- k9 S
  166.             measureDistanceBuilder1.InfoWindow = False
    " D$ N' O; S0 h3 `
  167. $ y6 t& Q6 t; B" X) z( r
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum$ U3 |" i5 n0 Q5 j% @  U
  169. 2 R) ~: Z5 P# o$ l+ B/ `7 V
  170.             Dim nullUnit As Unit = Nothing; i* c, G* ^, y. T. {! Y, e
  171. # D! s2 P5 e- n0 L* [# T, Z
  172.             Dim measureDistance1 As MeasureDistance. W% A5 w; R$ n3 P& n( R% P
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    6 H! g+ R* V0 K
  174. 7 s$ f3 s( e& y* P8 Q6 D
  175.             result = measureDistance1.Value9 C+ {' l& u# r
  176. 2 f$ I# Z. J, g+ A
  177.             'measureDistance1.Information()! G' p) W& E- n; h

  178. % y3 a- C( i$ Z: D# `
  179.             measureDistance1.Dispose(). {7 V/ i$ d8 b* @
  180. + F7 F0 x* w' u9 |. e* L
  181.         Catch ex As NXException6 C% f1 Z8 u3 f* G, C5 C& W. s
  182.             MsgBox(ex.Message)0 I" F" r2 ?+ [' y
  183.             Return Nothing8 |1 W; R% A, X; v" a

  184. # l. s) j- N, x; y$ e
  185.         End Try
    ( K& r! q( |2 ?# i" l

  186. ) A/ V; l  J$ ^: X6 U

  187. 4 {. {) }- M# ]8 _& X. U
  188.         Return result1 ], P/ [" A3 C0 l+ d
  189. % n) K! ~. ?, a$ Y4 \
  190.     End Function
    6 s; h& x1 a2 P8 j  C

  191. % k. v6 a6 {; \' m. l. p, b2 e+ [

  192. 3 K/ i' S0 n; g
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer! ]0 P$ }, b$ S+ c+ c% [( {! E; K

  194. $ b( E3 Y2 z. d+ @" k- E' n
  195.         'Unloads the image when the NX session terminates
    ; o3 g$ G$ T; N9 q* B% C: V
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    ( t5 ?8 {6 j% ]9 p- D2 t  x+ L
  197. 8 l& u/ J3 t. {! @
  198.     End Function
    * S( }) ]: I% D5 O
  199.   }& U, O1 o" J
  200. End Module
    : A# Y% e7 e" K3 G& N: }% k
  201. </P>
复制代码
$ D. d& p/ h9 \8 |
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了