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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
; `5 Z% s9 ?' a
NX二次开发源码: 装配中选点,并报告父对象7 @  ~1 q+ l2 c, B; ^

  1. 3 M! a* @: a1 X( \& E0 V3 A  ?; g% T
  2. <P>Option Strict Off
    % D. \! x; n- y- @8 Q  @1 p, n
  3. Imports System
    # B* c* Y$ k) j8 N* G# ~0 W$ C* c
  4. Imports NXOpen
    # B* ~1 }. j1 c1 K, A+ M. K
  5. Imports NXOpen.UF& z, \$ M% J, g; D
  6. % J* L( u7 v4 E
  7. Module Module1- L0 N2 X( t; V& J2 V
  8. ! S9 g2 k. z- y1 n- N( R
  9.     Dim theSession As Session = Session.GetSession()
    : S  z0 x& ^7 Y% x; I3 _7 M) ~: A2 y
  10.     Dim theUI As UI = UI.GetUI()" X) F9 b" k1 M) i8 ]9 P- P
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()+ m3 S$ B7 G' s" S  W
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    5 f. r" r+ `) i" L/ F8 G2 y
  13.     Dim pickedPoint As Point" _9 C( S) M  G, {) k9 _) u
  14.     Dim myModelingTolerance As Double
    7 P0 w* n6 o! i3 C" T5 m

  15. 0 a9 M3 U1 U6 L* i) ?: n2 b2 q& i( }
  16.     Sub Main()
    ) o0 ?' p/ b' O) ~$ i: `$ X
  17. ! [( F$ g; d3 P  c" u7 x, Z& S
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)( s" n' C' n6 t6 Q, \8 X+ g" a
  19. 8 a' B& n0 ~" y3 z4 G. L' X
  20.         Dim workPart As Part = theSession.Parts.Work
    $ e: z4 m8 Z. \) D  v
  21.         lw.Open()1 i/ U, n6 @1 n7 g- j
  22. 4 @* l4 I2 g6 ~  a: N: u
  23.         Dim myPointTag As Tag+ E: }" K. p* @: i& J: Q

  24. 8 ?8 U! l6 O9 A% H
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    ; R  B! y4 O5 }0 M$ {
  26.             Exit Sub) g9 J" Q" ^6 M& [) E
  27.         End If. @  m) \- h3 d/ A/ _1 p

  28. # l  j8 o% ~& e0 q
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)( {2 n$ F# ]. v. j; I

  30. ' R3 \  R# ]$ B( e. O% }
  31.         AskParents(myPointTag)
      K& U- O& ]/ A5 Q$ G! @

  32. 5 q2 r, e( d8 U) k8 R8 }
  33.     End Sub
    . v0 ^- j+ r/ l" X6 L% j8 A* l9 b' a
  34. 0 Z5 G/ W- A: U+ M: t. X+ o
  35.     Sub AskParents(ByVal objTag As Tag)( w! E: [; a/ O
  36. , g. K1 t5 [3 Q# i
  37.         Dim n_parents As Integer
    # O! f, k/ m+ K8 @& U& l; y5 {
  38.         Dim parentTags As Tag()
    4 S* A/ ^4 X8 N4 ]
  39.         Dim myPoint As Point8 M. H7 a( m$ A! w, x! z0 U4 ^
  40.         Dim myEdge As Edge$ t: E) K; _+ q( e: ^" V8 M8 M( q
  41.         Dim myXform As Xform
    # H3 \. B5 H  t, z+ R
  42.         Dim myTaggedObject As TaggedObject7 c# D4 R5 N. ~1 G+ H
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    + v: s- Z8 e! D5 R

  44. ! l4 t" Q0 r# X! a
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then9 o4 Q- M1 B/ w, `- f% D
  46.             myPoint = myTaggedObject; t+ }7 B' ?/ ~5 u' f
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)' ^. A( J- _  U8 e( u
  48.             lw.WriteLine("")  _4 A& N8 E, ]9 L8 A5 ~

  49. ' P% m1 P8 R, ~
  50.             Try
    / O& a( v. l# K7 R3 g5 Q
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    2 p, ]; w5 G6 M3 }" _( e
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)& o1 O0 V! [6 ^! C; c; o; `
  53.                 For Each parentTag As Tag In parentTags1 G+ Z  W6 j- Y+ C0 [
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    + E) U+ W+ [  A: h# J7 K& K. k2 i
  55. 6 L; {9 z* q$ X9 R/ c
  56.                     AskParents(parent_object.Tag)
    2 x' O5 z& s( x6 l9 ?: G

  57. & Q  N! r. I* [) t) V1 U
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    2 w- S9 k, V) P; @
  59.                         myEdge = parent_object
    - j, `8 @; ~7 \
  60.                         lw.WriteLine("")
    2 s9 C$ u. s0 X# c
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)+ A+ u# A8 {! U1 X8 A; a4 J
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
      {! z/ v/ x2 I+ X3 o+ S" ]/ B. A
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)' T: E" T& X- L1 H
  64. 3 t1 L- F# Z' x; E2 P' D3 ]
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    9 @$ w; Z. Q1 f; r) K! X
  66.                         Dim occTags() As Tag* e9 c8 F$ O$ E, `1 S- J
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    / B# s8 F: j0 f
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    & a' _& e7 ]; {/ n5 u
  69.                         For Each temp As Tag In occTags
    * X: w2 g: y  t5 o- F" \
  70.                             Dim myComp As Assemblies.Component
    ' N2 n9 m# ^1 j2 j  T0 V. V# v
  71.                             myComp = Utilities.NXObjectManager.Get(temp)* v- z" y( C4 |, A6 c8 R0 C
  72.                             Dim myCompPos As Point3d% I6 K& f/ B' L
  73.                             Dim myCompOrientation As Matrix3x3- ~! r4 \5 R' g9 c8 J
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)) m& n. t$ k* l; r9 O/ Q6 S
  75. 3 {8 @1 S$ Q6 n8 f9 g) U$ f
  76.                             lw.WriteLine("")
    ; X3 [, d. t: p) o9 k+ f1 k: @
  77.                             lw.WriteLine("component name: " & myComp.Name)
    4 @1 [: ^' M/ W3 z6 _' k
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    $ b: f2 ~- ?1 y0 v2 N5 ~/ E
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    5 a4 Z- l7 [) i4 J# H, x/ o

  80. & l2 v& ?' \) V
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then; \# _7 F5 `) {! @+ H+ t
  82.                                 lw.WriteLine("** This is the component that was picked **")
      _7 ?2 o& I% F2 M
  83.                             End If6 @- n8 |* i! v" y7 J- P! D
  84. $ V. o1 M8 s: V8 y# V, O
  85.                             lw.WriteLine("")4 J3 Y* d9 n& y2 k: u" r
  86.                         Next( G; U  k. o  T2 t! G9 B- M1 Y

  87. 9 e( q! w0 C2 K) }' i: l
  88.                         lw.WriteLine("")
    . O6 L+ j+ r  j  o! k! D( P
  89.                     End If
    / E" |: ?! Q0 B" v) i
  90.                 Next
    & |9 A% i$ g1 _  m- X5 a% \
  91. : t& V7 C$ v! C2 E+ |
  92.             CaTCh ex As Exception7 E9 p7 j; Z, P9 ~
  93.                 ' NXOpen.NXException: Current object is not smart6 c7 b- H/ v1 a; o1 @3 B
  94.                 lw.WriteLine(" Error: " + ex.Message)
    0 {. B1 K, ?6 N1 G2 S; \% N& Y
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)9 f' ~  j& w5 @. a
  96.             End Try* U3 }1 ]% U) v4 s

  97. 1 \2 @1 s9 t  ]: K- b' C8 r* V
  98. 7 T. z& W+ ?' ?
  99.         End If
    / v0 d( i( p" i/ m0 Z- z

  100. % C3 I( z: h/ b& i

  101. , ]7 `" U7 t2 t5 D( G  M
  102.     End Sub" l& f$ V8 D0 y9 }, O# K
  103. - `. T6 v! @; [
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    7 c; F9 L! {; I
  105. : G. Y& V* A9 {" |
  106.         Dim base_pt As Double() = New Double(2) {}% I) O7 g. r: t% R
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null' e( R9 l+ m3 w" c- y
  108.         Dim response As Integer = 0; m: |1 W3 M1 u2 o8 m( [8 T9 a
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt4 M" Z$ B$ J# K) n
  110. 7 A' E8 F0 ?4 G* w9 K
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)9 C5 @0 {. d+ Y* h0 t
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    ! M+ l1 y8 V2 m: M% A& o$ b  o
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)$ e6 r+ e' X, g5 }  w1 B
  114. 4 Y3 e7 y6 I, \# _
  115.         Dim pointLocation As Point3d
    ' E) x; ^7 p$ u# d8 |
  116.         pointLocation.X = base_pt(0)3 c3 f, w5 g6 g
  117.         pointLocation.Y = base_pt(1)2 V8 k# a6 q" h, D  Z& p
  118.         pointLocation.Z = base_pt(2)
    % M7 T' M' G7 s  o) O9 d6 i
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)/ V# ]1 m" T5 M. a# r! X
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    4 g7 K/ V# W* J  |1 V

  121. - j! L1 B* g* P1 e3 _9 M' Z7 G% R
  122.         Return response  Z& s% x# R; C/ z* l  t

  123. 0 U( O0 ^- Y3 Q( S- I3 W! ?5 l. p
  124.     End Function! T- S# \/ f( N1 v1 A8 z; ^+ S
  125. ; ^1 x+ g$ N2 x
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response! J* @* |* `1 L

  127. 4 l% n; {4 ^: Z3 Y' E. q/ G1 ^
  128.         Dim selObj As TaggedObject
    7 J4 S; f. W9 R0 m. v1 G: R
  129.         Dim theUI As UI = UI.GetUI
    ( Y3 P5 l1 s8 m; c* ~
  130.         Dim title As String = "Select a Point"
    , c& N) r2 y  D# ]- K# N
  131.         Dim includeFeatures As Boolean = False
    ' O' e1 p- O5 `0 j# }
  132.         Dim keepHighlighted As Boolean = False
    * o" c' H. M) d; v
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
      M. ]. q, ]3 x% C6 V
  134.         Dim cursor As Point3d3 {5 u+ ]* K" J/ ^
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly9 K6 M  o# O. X7 H
  136.         Dim selectionMask_array(0) As Selection.MaskTriple0 g* @/ e. L& e* C+ {: w1 p
  137. + y9 `$ }2 ?# o3 d
  138.         With selectionMask_array(0)
    $ b2 P6 \) l6 O
  139.             .Type = UFConstants.UF_point_type2 ?9 f9 e) P* P9 U% f" r
  140.             .Subtype = UFConstants.UF_all_subtype+ e3 \4 g/ A# ?5 M
  141.         End With; r& F' D: f5 M, ?, z0 j* d, g
  142. ) q$ w4 j, s: n, d
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    " H$ Z5 X/ o2 n! u- L  K
  144.          title, scope, selAction, _& e6 w% M: ~8 e0 a
  145.          includeFeatures, keepHighlighted, selectionMask_array, _5 P* x3 Q7 |5 v% q
  146.          selobj, cursor)
    8 Q& B( q, w* Z% v1 H
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then$ A) O: |) u# B/ x3 w6 N
  148.             selPoint = selObj2 [% O# E5 A/ l
  149.             Return Selection.Response.Ok8 }, k( u% K3 N
  150.         Else+ j& N, v* b9 Y5 b. `
  151.             Return Selection.Response.Cancel' a% }# P0 m/ l
  152.         End If# L8 U2 Z3 T$ X
  153. & z# t: J4 R- o
  154.     End Function
    2 F: I+ \: @  B3 ^2 W" I' s( i0 B
  155. ) t; ~4 T+ C4 @7 A
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    8 O$ E) z0 ^! R0 p) O' {

  157. 4 g+ T9 u/ \9 d' f( P
  158.         Dim result As Double
    . ]9 k  H( C, s# w2 y% b1 x, |

  159. - C! \0 G  h1 ^1 F; a$ e! a
  160.         Try6 ^  L9 J' Q( Y7 B" `
  161.             Dim nullNXObject As NXObject = Nothing! v- ~6 z0 \/ Z/ B5 w0 X0 _, r

  162. / T2 x' v: c5 I2 }" r
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    ' @0 ]) [9 [' N8 V, Y4 X; Q
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    : [' Y- z. R. p5 O

  165. ; U4 L! s5 _1 a* d5 B
  166.             measureDistanceBuilder1.InfoWindow = False( e3 {* G# S; I; l" X

  167. $ A. }# R9 y) R; M0 _7 a- c
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    0 K6 u5 T! i/ J7 e

  169. ) y5 x# o3 _4 G' v
  170.             Dim nullUnit As Unit = Nothing
    5 r& [3 \7 d5 s7 H- t) |
  171. / k" I, v' C+ ?
  172.             Dim measureDistance1 As MeasureDistance. m0 p$ @% B0 W
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    ( A9 c' g, F/ Q( x+ E
  174. 2 A- S& q6 v1 z6 v& O0 w: L
  175.             result = measureDistance1.Value; Y5 G5 _+ T6 q' R
  176. * X% {, S4 B0 _( O! o$ O% t. [! ]) ~1 b
  177.             'measureDistance1.Information()
    : ~" ^+ t3 J3 F9 F# z7 |
  178. ) h5 J% Q3 i+ s! i5 R$ b  b
  179.             measureDistance1.Dispose()
    0 @2 s5 }( |& W9 L7 c

  180. ! g5 M; K( J1 R" u# W
  181.         Catch ex As NXException
    2 v$ C8 @1 D4 c* U
  182.             MsgBox(ex.Message)
    + {$ Q- w2 \: w& J8 J
  183.             Return Nothing# Q+ k. o, b1 Y$ }, E# v1 U# B0 J& t
  184. " r5 s8 q+ i4 H" S+ s
  185.         End Try: a, @+ u6 q' s3 ^

  186. * x/ h# v+ }6 O$ P6 _0 h
  187. + `) V  Y' V5 }+ ^
  188.         Return result1 {( m- z: l7 Z; n) B& F

  189. 5 c; m9 P- a$ H/ G
  190.     End Function! E! B$ x3 ?1 D1 S- P" _- g1 q$ B

  191. 5 a/ F& a1 @7 d7 r9 U5 {

  192. 0 y3 Z8 ^) R( u. a
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer) C0 W8 m- V, _3 {: E0 G! }
  194. 1 X3 F) u% Y6 j  Y$ R5 O2 u
  195.         'Unloads the image when the NX session terminates+ v' V# U0 F8 V
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination  }/ }1 [1 H: i% k( y" r
  197. ; G; f1 K. J( W, {; X1 y
  198.     End Function
    ) M% E7 Y+ X1 }$ y
  199. " Y; i: X# X( e* ]4 s3 @3 @. D9 Y. b
  200. End Module
    # a2 T0 \! E. F) ^
  201. </P>
复制代码
1 y5 a8 ]$ m" \( J5 h: E
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了