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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
$ _- P8 J; E& P3 C9 l$ T2 ]2 Y
NX二次开发源码: 装配中选点,并报告父对象
5 E& S9 x) g* P- N. N3 ^3 W' \
  1. 9 b0 T% ]# j9 J& d$ M2 p
  2. <P>Option Strict Off6 g" r9 ~: d7 W$ D7 E$ k0 F  S
  3. Imports System" T$ _4 M$ {2 i3 q
  4. Imports NXOpen
    4 ~1 W! t2 g, N- T, \  V5 `
  5. Imports NXOpen.UF
    . @" Q! {/ _9 e  @8 Y
  6. 2 q4 R% {3 h8 c4 s+ o* j$ ?
  7. Module Module1  |' X- A7 i) W8 Y5 u

  8.   q1 e, V! x' V' B9 q
  9.     Dim theSession As Session = Session.GetSession()6 x! H  ]5 j; ]# c6 ]
  10.     Dim theUI As UI = UI.GetUI()
    8 N. x1 s7 E& w: b, a
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    6 [' B8 F; ~, V) Q3 I' ~) h
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    & e' F  U& H/ d2 r6 t* r4 [
  13.     Dim pickedPoint As Point5 g' p/ p+ i, K" h- m$ V# N
  14.     Dim myModelingTolerance As Double
    + x) E2 \0 `" `# T% H+ Q9 _  i2 X
  15. ( I$ i) g9 L! A% n- \( K8 a
  16.     Sub Main()0 s7 ~. o. a/ {4 K4 Y7 x$ L
  17. $ t1 o$ q0 ~9 [* e
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    + e2 F5 Q; f2 y) I) }
  19. ' H" b) t% ~9 ^
  20.         Dim workPart As Part = theSession.Parts.Work2 c6 D( @' ^- ?, k  W7 M# T: M; W
  21.         lw.Open()& D3 @$ L+ r$ f+ |' ^8 L! M
  22. ' t& O% _4 v! p: m
  23.         Dim myPointTag As Tag
    1 f4 k) W: c5 q2 T: H
  24. / w2 v8 P  X' q; @# i
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    - m+ V) F8 \& X3 y: K! l7 u
  26.             Exit Sub( L! w( s0 L5 j
  27.         End If
    / t2 E2 s3 e" x; F( l! I% ]
  28. 0 U9 B! L7 n6 n* m$ Y
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)4 C3 L* V2 s, v" ?

  30. 2 c7 v; a7 C3 b  n: G4 F  e* `, X% _
  31.         AskParents(myPointTag)* ?+ C: Y- e; x$ B* c, ^
  32. " V& V1 P: \. E' F# d7 e
  33.     End Sub- A0 C5 T+ T# O- R* c& l7 }  p
  34. : J0 B1 r. e6 [$ U
  35.     Sub AskParents(ByVal objTag As Tag)" F( W3 V7 X9 g

  36.   o7 I$ g4 t+ b& q4 Z
  37.         Dim n_parents As Integer
    + H0 N) y3 U+ H& U5 L
  38.         Dim parentTags As Tag()
    7 l' w% g4 m, \6 ^2 f
  39.         Dim myPoint As Point* P* h& @! n6 m! p9 ^8 y& l
  40.         Dim myEdge As Edge! p. w% R. m6 J, C6 \  f
  41.         Dim myXform As Xform
    5 p8 W. U) q& U/ N) ?8 `  P  Y+ H
  42.         Dim myTaggedObject As TaggedObject# t" P4 T$ S1 e
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)+ Y! E" T' P: C  U; Z

  44. ( }: ~; S, e5 j6 J2 U+ z
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then, K+ {, _4 R# C  z# Q
  46.             myPoint = myTaggedObject
    % N7 G& x5 ]# [' b8 B  x/ u" u
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    ) J0 S$ @/ s$ C9 n6 t& J: ^. }! M
  48.             lw.WriteLine("")& G6 i& ]0 P% m6 z
  49. 7 s* j: A* f8 X) y4 ?
  50.             Try
    * D# L& D% o: Y% b# [" h  c
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    1 A1 w6 b5 e: t0 R) H- n
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)9 S5 e4 V* P1 c$ o* p% X+ r( H! v
  53.                 For Each parentTag As Tag In parentTags
    / p9 ]0 M" R. q# Z) L, h2 x" r
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)1 s% D3 ^3 m( c+ i+ Z: p) B
  55. ; b1 N( A# o, w' _# k
  56.                     AskParents(parent_object.Tag)
    ( s2 b4 \8 q+ x( a- ]

  57. - F. f, u% Q2 ^/ w- r
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    : d- Q: O; I9 n' R) c
  59.                         myEdge = parent_object
    / `# V  \+ {' v
  60.                         lw.WriteLine("")
    " i6 `5 d. W" T" m) Q8 i" m! \. y* a
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    2 r2 f8 v$ n* u6 G/ \2 G+ [
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    & U* l% D. X& c
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)( D8 r% c6 G( U( W- I0 S

  64. 9 f. l* H8 @* g4 I# M$ T6 r
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    7 ?# u! B! @0 F( _* s1 i
  66.                         Dim occTags() As Tag6 q* X* N7 s% X( F
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    - o$ f5 ?" M5 y  }$ ?  L
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    ( t$ q/ }9 u/ H9 @% B/ D! H
  69.                         For Each temp As Tag In occTags4 ~0 D3 n  p  n" T. t
  70.                             Dim myComp As Assemblies.Component* |) _6 d+ m8 ]( f# o# D
  71.                             myComp = Utilities.NXObjectManager.Get(temp)2 l, M9 z8 v. n
  72.                             Dim myCompPos As Point3d, X8 h6 e: D# W
  73.                             Dim myCompOrientation As Matrix3x3
    " g: S% i0 {4 `6 [$ Y! k6 j
  74.                             myComp.GetPosition(myCompPos, myCompOrientation): {" w" Y" s, `; i$ V
  75. 6 W5 i1 j9 a' ]4 Q# L
  76.                             lw.WriteLine("")
    1 M, k5 q; E/ F: w8 N
  77.                             lw.WriteLine("component name: " & myComp.Name)0 o& H# C9 y! j8 ~* \( C- t$ B: g
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    # I  T/ N, w' `# c# Q$ C9 G3 V
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)* U1 ~+ M% s0 i/ w
  80. 7 o$ _9 B& S& d4 ^- K
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then# y/ Z( x) B# Z- R' k% O: K
  82.                                 lw.WriteLine("** This is the component that was picked **")
    ! u: Z+ f" V0 ^1 X2 u8 p( ^; e
  83.                             End If
    ) Z' M7 ^" K' `/ |9 ^5 _

  84. 4 r" H: i$ E. v' `0 o6 h. G( J( v0 o
  85.                             lw.WriteLine("")+ N( G0 C- P1 _- Q  Q  D5 h/ i. J
  86.                         Next3 _; {# C5 U: N  ]

  87. 8 _* l; n, C; m
  88.                         lw.WriteLine("")
    ' V4 ?  b' M1 B& L8 P: d% Y8 F
  89.                     End If
    8 i1 y2 ?+ v& h; V
  90.                 Next
    # l9 B- K5 K! X/ F4 l0 y0 e
  91. $ g- ^* ]7 A7 M# f
  92.             CaTCh ex As Exception0 k( D! f% S5 u# y0 I! [# t1 A" |8 X
  93.                 ' NXOpen.NXException: Current object is not smart8 P+ g; K1 u% z  `: G$ M0 o
  94.                 lw.WriteLine(" Error: " + ex.Message)
    + h3 \$ A# G) c# A
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)3 g& k+ t/ y. Q4 {) @
  96.             End Try
    7 ?6 y# n6 n; }* X; H# y! g
  97. 3 k9 j2 |: R9 A4 p6 a, A/ }/ O1 o
  98. 4 y, {7 I, V  g) }& I; C
  99.         End If
    ; q$ O: f0 _, B2 \# a8 K

  100. 8 h( E# Q# ?% v6 L; w0 N- o
  101. ' l4 i) r/ w( n, c4 k3 V
  102.     End Sub
    ; e$ `! v. |3 c' ]; j* K* I
  103. 5 F3 X' a& }8 t& Z
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer) o7 ?4 ?" [. T

  105. ! V4 f' H* _/ ]9 X
  106.         Dim base_pt As Double() = New Double(2) {}
    ) t" V; N; }4 X2 D: o. P3 q
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null! a& K0 N; `* E5 K! g6 [( ?! R
  108.         Dim response As Integer = 0$ c. c  p6 q3 b- ]: Q$ q
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt- h6 n# `+ c  a1 q0 d
  110. ; N. K& h- Z  I4 @; R  q9 ?8 P) T
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    5 E2 q. V' V* J
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
      _2 |0 `# U5 g1 o, E
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    " F: D  B. N! X# d1 J, S2 _' R1 U
  114. 7 D: P0 {) S$ K
  115.         Dim pointLocation As Point3d7 w& G: [8 v$ M# |
  116.         pointLocation.X = base_pt(0)2 k! y$ I6 ], i9 N+ ~
  117.         pointLocation.Y = base_pt(1)
    5 X8 w- C8 s4 ?! G% D
  118.         pointLocation.Z = base_pt(2)
    8 W* K: \3 _" [6 w( [( v
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    $ {; ]7 d8 s2 m' p3 h
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)! @# ~& j- ^) U5 ?7 |3 \
  121. 8 q1 ]/ p) n* K! `6 V- K1 P. P
  122.         Return response$ _/ A3 n6 V0 r
  123. & X" m) t# d9 C/ W
  124.     End Function
      s+ c" g' S8 j) `2 Z
  125. + O: @+ ]+ M  w
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    ; a: ^1 W4 M% z, g: [
  127. ! E6 j7 a6 \8 S; a! Z0 g
  128.         Dim selObj As TaggedObject
    9 Z. j* m/ ?1 p: Q% [1 v
  129.         Dim theUI As UI = UI.GetUI
    - ]4 U& x" Q5 o- S
  130.         Dim title As String = "Select a Point"
    8 i2 s' s( u( {7 g1 F
  131.         Dim includeFeatures As Boolean = False, c8 ^6 M" Q0 j+ @$ I
  132.         Dim keepHighlighted As Boolean = False8 E/ r# i7 e" u. u# H
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific( f& A, K" Y1 y( n
  134.         Dim cursor As Point3d0 L2 b4 z+ e1 \1 r- a. p/ T
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    ) d4 _) Q$ [, @( b8 o# ]
  136.         Dim selectionMask_array(0) As Selection.MaskTriple
    # Z: h1 D6 S* c. @. n4 h

  137. ! R+ u) b/ s3 ]# _6 D" I4 o! [, q
  138.         With selectionMask_array(0)
    ; @; D- f- l2 B6 h, n
  139.             .Type = UFConstants.UF_point_type0 _; r( p9 v5 s/ H1 |
  140.             .Subtype = UFConstants.UF_all_subtype
    0 k( t4 _8 v- \- E; A+ X7 ^6 T' K
  141.         End With6 \$ ?# y1 U6 U! M3 j$ A& C

  142. 5 A( p/ D0 C: \# N
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    2 ^) s8 R. t5 y# n, ]" \
  144.          title, scope, selAction, _
    6 C4 J8 |( S" s8 ~9 C0 I
  145.          includeFeatures, keepHighlighted, selectionMask_array, _5 p! \' b& T, f" N  h6 h- i
  146.          selobj, cursor)
    . U% U3 F/ i( W, P: w
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then% {' o" ~, n) v5 W. f$ u" ]
  148.             selPoint = selObj
    ' E: Q1 G' ?6 C+ Q- I
  149.             Return Selection.Response.Ok
    / }% P7 m7 `5 P; K; \6 \
  150.         Else
      E7 ?( {# e( z
  151.             Return Selection.Response.Cancel6 G3 W; y# l* f' b8 D
  152.         End If, z8 j: R4 Q1 c8 M
  153. # C1 ], v: s' a# y' c8 W: Y5 q
  154.     End Function
    % I2 ^* T  e* H7 W0 D

  155. 5 E0 r0 z$ j' D% _( b0 G8 |* Y& S
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    : S' X2 l( z- N1 A7 R" A
  157. 8 ~4 O6 P: {. K2 L6 V7 Z; N% [
  158.         Dim result As Double2 g3 X. `/ j. B' ?5 u. B
  159. / ^( S% f1 P: k3 h: [' I6 A& r
  160.         Try, {  K3 I1 {6 k1 ?9 _6 {
  161.             Dim nullNXObject As NXObject = Nothing
    9 P/ A& a4 v" L- W' }: O

  162.   G; a- s3 M; ~* O
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    0 y- s  X7 C% u' u
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)* a7 g' z  _* S: L1 t% [5 Z3 e% \

  165. 3 O8 O" k" [5 y. W/ H
  166.             measureDistanceBuilder1.InfoWindow = False1 R$ L  \1 k; D5 Y

  167. $ e& X7 c5 G! x' D. [. V- B0 o8 n
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    ! s+ @6 ]: D$ ?6 A, a' J; D
  169. ; U2 j; l* }# `" V) ^& p( B
  170.             Dim nullUnit As Unit = Nothing5 o& i& [* l! N( X
  171. # `0 m. D! _. n/ @
  172.             Dim measureDistance1 As MeasureDistance
    ; F7 U9 @: |, \. b' n9 ~4 t6 ^
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    9 \( T- [! h( X4 e+ j/ r. s+ R
  174. 0 x" ?2 C/ U5 X' h. I. g( A
  175.             result = measureDistance1.Value) b0 W8 B( R# `6 {7 a
  176. 0 z* M) c& y7 M- r+ A  g1 P9 B
  177.             'measureDistance1.Information()  s1 a* l- R6 a
  178. ' n: f0 x( N2 e+ N- o  m( a
  179.             measureDistance1.Dispose()
    . H2 m  ^1 Y/ K

  180. 4 b% i. L0 Q  S8 O
  181.         Catch ex As NXException0 ~3 Y: k0 N7 X3 w7 j0 q& e8 \
  182.             MsgBox(ex.Message)
    3 T2 L/ |9 G: Y  w- _7 ?7 {) A
  183.             Return Nothing: e( L  t; [8 g* {. ^

  184. 7 [& ~# p" S8 U( e# H& f8 q7 |
  185.         End Try
      j8 H: t; a& `1 g
  186. 9 j; y+ n: g9 d, \
  187. 7 V9 v0 J; `- _( ]& F
  188.         Return result5 `) Q9 P; b3 I  R; j7 ~

  189. 9 U  [, ]" [) r6 a8 c; e
  190.     End Function1 G: M; w4 f" p1 x3 B
  191. % \& T' i2 x% N

  192. $ |) ]! d" z  c  e6 S* l( j
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    ! ~# R! ^' G& `' u; O
  194. 6 f8 n! V% [! _
  195.         'Unloads the image when the NX session terminates' x9 c; w- ?6 L# Z3 g
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    . _0 I9 y" I9 M5 d/ h" {8 ?

  197. 2 R. F) P" g, B! L6 T0 c  k! _
  198.     End Function
    1 }7 N) l: `& F" b+ B

  199. 2 y* r: I( x- k5 m  C0 g
  200. End Module
    ) j( f6 D& Q* q+ i- A2 L; W4 I
  201. </P>
复制代码
4 b' b" [6 y$ C& W
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com/ 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 网址 www.diantuankj.com/ doTeam.tech
回复 支持 反对

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了