PLM之家PLMHome-工业软件与AI结合践行者

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
" t3 N5 o' v$ Z# M, |8 a& l
NX二次开发源码: 装配中选点,并报告父对象1 C; g. M1 P5 Q, x, J

  1. " K/ z- L2 Z  \% E7 U7 v! z
  2. <P>Option Strict Off' Z: D  j/ F8 a, J9 r6 K1 u+ Y
  3. Imports System
    5 r: q3 {. D! y% R
  4. Imports NXOpen
    ( [1 E" Y1 Y9 M# w  b. p* M
  5. Imports NXOpen.UF
    1 ~! B! w: F0 k3 u
  6. 8 C) r8 m# y9 K/ H
  7. Module Module1
    ( {6 e# Y; x3 W: l4 {3 Y0 u- d
  8. 1 n" v4 @5 k( U' o# x6 v. [! ^1 b
  9.     Dim theSession As Session = Session.GetSession()
    $ A( @0 z4 G! @
  10.     Dim theUI As UI = UI.GetUI()" A9 H3 A5 q% h
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()3 E. W' `) g9 F' `' ]
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    $ J0 M) L4 ~7 B9 l
  13.     Dim pickedPoint As Point
    $ k2 j0 D/ ^! V" [" g$ D+ s9 i6 l
  14.     Dim myModelingTolerance As Double. O2 H% m  g  Y! `6 j# y

  15. ! ^3 J$ E# g! L
  16.     Sub Main(). K* j' ?! L5 Q" h

  17. ' ?2 |% t2 ]3 p5 }& f
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    ; G( e6 u6 E3 `

  19. . b: l. f8 F" h4 ~. r" d1 r$ T
  20.         Dim workPart As Part = theSession.Parts.Work
    . p' m) ^9 F) _- K+ c2 A& a
  21.         lw.Open()9 K6 a! X! F, u  f1 m
  22. * L! ~$ M) y0 j7 M6 z# E2 Z* w/ C
  23.         Dim myPointTag As Tag2 \/ B4 i7 O" w* j
  24. " |9 ]3 A' Z* b( T; M8 p9 b. J
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    ) w6 l2 j$ k% [! w4 s- s
  26.             Exit Sub9 a4 c) h% _6 }9 i/ [% u' K2 a
  27.         End If' T" h: O. u7 |& \7 N! m1 q) n% U, d

  28. : `9 ^0 J# K! Z& U
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)& _9 g! _7 m0 n' @+ B* L
  30. 0 s+ m, l7 Y# N  v. K0 L
  31.         AskParents(myPointTag)( b; g  \& I: g/ B' E9 J
  32. ' c/ \2 B; k, a0 @! w" _
  33.     End Sub/ \7 v* Q) d3 j
  34. + _6 S( M1 N4 D# o
  35.     Sub AskParents(ByVal objTag As Tag), [# [* |3 I7 K; O( @! ]
  36. / T3 Y2 B6 t1 o/ d6 u' K
  37.         Dim n_parents As Integer
    : b/ _; |% H2 l8 y" F, N  f5 z+ Z
  38.         Dim parentTags As Tag()
    9 L- I0 z4 _" ]  s
  39.         Dim myPoint As Point
    0 X0 f/ ^- W, C% d- R
  40.         Dim myEdge As Edge
    1 s6 E! ~) u4 s4 o, R0 o
  41.         Dim myXform As Xform; s4 T* k: D/ n4 {5 L. t' n4 H1 H. U
  42.         Dim myTaggedObject As TaggedObject
    # ^7 V4 B, u: _
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    : G0 a) u+ o* k3 ~

  44. & r7 J! ]& v9 J( k9 {& [# X
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then0 R  p2 p! Z+ U( K& ]5 ^0 Y
  46.             myPoint = myTaggedObject
    : o* v1 A, y& K9 [
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)* K2 R7 {  ^. p6 ]# L
  48.             lw.WriteLine("")- K( G' @8 a* c. ?; P

  49. 6 Z* b' l! ^& a2 ?' J
  50.             Try- B$ y" d3 @6 |8 K. U( t
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)" ]+ \' n. r+ `2 N1 c0 p; |) K  W
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)) C  w# M5 W, V0 X+ J1 w9 u
  53.                 For Each parentTag As Tag In parentTags3 h" X2 A3 q+ t+ y' F" v
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    . w, m  d+ K' ~  K6 d0 ^; l
  55. 2 v* H5 s( F: i1 g  D* y2 j
  56.                     AskParents(parent_object.Tag)% {1 @$ U+ L/ L* }6 U
  57. 1 {. M- `. c: i; B* t
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then. Z" L+ J, s0 a1 D
  59.                         myEdge = parent_object4 |/ E$ A8 [! H& d0 c6 c8 D2 c
  60.                         lw.WriteLine("")
    5 L; x4 g+ j, s7 Q; G! h
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)6 T; N2 w3 {7 b3 ?( c4 x
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)" ~' I: y( b; q7 V* Z7 y
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)% g2 E  k" k, Y; L- n5 G

  64. 4 p; L5 T2 j: c$ H& g3 `
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    + p: O+ \3 ]$ u6 `4 O8 R, K
  66.                         Dim occTags() As Tag4 ?( s  M' H# k+ L/ p: a
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)4 X1 s5 ]/ i1 l
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)& v" ^& y, O: Z! r3 ?
  69.                         For Each temp As Tag In occTags8 i3 @6 E4 ~' K. h! @
  70.                             Dim myComp As Assemblies.Component& h- G0 z, _% D3 U2 u) d$ u+ Y
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    ( U3 f4 B. S; S
  72.                             Dim myCompPos As Point3d  m9 Y$ }5 O& e+ N1 q  {" b2 ^
  73.                             Dim myCompOrientation As Matrix3x3
    2 N3 K& b8 E$ H5 Y
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)1 R& C! [- D7 y

  75. 0 i, ?) g. c3 W" ^/ ?
  76.                             lw.WriteLine("")* }2 v2 ~* i$ H6 {7 V: E
  77.                             lw.WriteLine("component name: " & myComp.Name)& U1 O1 e: I. A0 `2 `
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)1 C: U, ^" ]; C( y% z
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)' W0 ~! y6 H9 b% P3 G

  80. 9 E% R! y1 F9 b: D, |4 M1 R
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    0 S$ G1 D  ~: }0 k
  82.                                 lw.WriteLine("** This is the component that was picked **")
    " P* ]' D5 [6 J, `: l
  83.                             End If. y- J/ s8 |* u1 q5 c2 }
  84.   r. n0 _) z, X  N1 P7 g# ?+ M
  85.                             lw.WriteLine("")4 \9 }; V  @8 E" ^$ k5 g
  86.                         Next6 a' Y( ]/ x. ]! r
  87. - e4 ~# V1 S, t9 _+ S$ M
  88.                         lw.WriteLine("")
    8 r$ b1 m, @2 z/ P0 k. T/ G
  89.                     End If% z! ~; @; K3 Y# s
  90.                 Next  H+ I# t, q8 l
  91. , W8 G8 d: O9 h
  92.             CaTCh ex As Exception' {+ _. M% J, D" I3 X  V; ?
  93.                 ' NXOpen.NXException: Current object is not smart
    9 ~; Y9 |0 h& s1 ?* A; i$ o
  94.                 lw.WriteLine(" Error: " + ex.Message)( b8 b: G  X( C* s4 _
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    * B  t; a2 n1 q% B
  96.             End Try
    ' g5 X9 r' C  q: E6 F2 {* [7 S( D. {
  97. ( u6 i9 Y7 A2 |( r' Y' ]

  98. 1 D' Q; B0 s, b
  99.         End If& K$ ?( G6 U, V  ?" U

  100. # p- s& A3 |, v; v/ {

  101. - B, z6 o1 ^- T! b$ `* V) W& @
  102.     End Sub
    : a: B5 Y4 h2 Y% b- `, i" j' g

  103. ! g% ~6 x0 n2 o1 |
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer  x! ?1 B- @5 f, x$ k

  105. . Q7 y# q6 k+ A
  106.         Dim base_pt As Double() = New Double(2) {}3 Y0 R# Y+ h7 \: m/ e( u
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null+ j/ w: g, p& k5 Y1 G4 I% h
  108.         Dim response As Integer = 0, ?7 g0 N: m4 f2 b% w3 \- ^) S- P+ E
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt3 n: {1 n8 q) M

  110. ; p- T" G7 ^9 O7 \* n
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    5 P9 V5 u& i! b
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)* n, \- u3 l+ s8 K$ c
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
      ~2 I1 |  |8 Q. R! X: |

  114. # y( o2 t; e4 K( {
  115.         Dim pointLocation As Point3d
    ) [4 m1 \" H" G+ D
  116.         pointLocation.X = base_pt(0)
    / o  G5 R$ B7 |. h) b. P8 `
  117.         pointLocation.Y = base_pt(1), F( M! M: ?; q, R# \$ w3 ~0 r7 |
  118.         pointLocation.Z = base_pt(2)
    $ L# U8 f. `9 }8 k4 J$ g3 m
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    ! \, N# W3 @, v3 a, Q/ U* F
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    ' v4 A2 q( r* j4 ?5 Y5 k7 c
  121. , f0 V9 K' g& {1 b% D
  122.         Return response- u/ S# A3 N) S4 u* q2 H
  123. 4 J3 d- V( t, Q( V. H
  124.     End Function
    ) h* y; @& _( |& P
  125. 1 m0 N1 E7 [1 v3 [4 H+ ^2 [  M3 Q' v# s
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response) a7 @$ p; v' t* p2 P! _
  127. : u" c+ K- r5 @* Y' M; z
  128.         Dim selObj As TaggedObject
    4 g- ~3 `6 f" y4 z
  129.         Dim theUI As UI = UI.GetUI" ?) T2 G2 k) B+ H
  130.         Dim title As String = "Select a Point"
    5 |/ q2 q' l# c1 R0 B( D/ s
  131.         Dim includeFeatures As Boolean = False
    8 m: f+ A- I, M3 W. `9 v: n
  132.         Dim keepHighlighted As Boolean = False
    / ~* J1 O- N8 P3 A$ f
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific- I) s! {9 R2 Y- Z. l4 z
  134.         Dim cursor As Point3d
    ; A8 i; Y9 U1 g, y% h
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    ( s/ s! M- w$ W# z5 w  V
  136.         Dim selectionMask_array(0) As Selection.MaskTriple  Y  o' d8 L* G$ E3 n
  137. ) o6 m& G/ W& }- C3 Z8 J" e9 [; p5 h
  138.         With selectionMask_array(0)
    6 F& @0 c' _% m) e
  139.             .Type = UFConstants.UF_point_type
    % d  K: x, M: S& O# g
  140.             .Subtype = UFConstants.UF_all_subtype6 v6 }1 {" |/ v* ?/ Y; U: d
  141.         End With9 N' q4 U3 D$ B* Q( h% c
  142. ) {4 _: B8 p+ `2 V
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    # u( M* G7 B* d7 _
  144.          title, scope, selAction, _
    6 n, w/ l' u  B+ i. p$ }
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
      ^) o& D! g. T
  146.          selobj, cursor): `9 ?; d" T6 {" r
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then; l3 B2 o! j/ _8 b+ ~2 _
  148.             selPoint = selObj
    7 Y) t. x/ S' }8 E" P& z
  149.             Return Selection.Response.Ok
    ; A) @; H) ^- m& p# g4 O  |
  150.         Else
    ; W& m7 M% A! i. w( i9 @
  151.             Return Selection.Response.Cancel4 q9 I4 X; }1 a& \, v
  152.         End If. _; {0 V& i' U& p6 h& E9 `
  153. / K( P9 O3 q* g8 F' J( `3 Z
  154.     End Function
    5 j: v9 v- m1 J% |' @8 F4 \

  155. * t2 {  v6 N2 P4 |
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    4 p9 a/ G1 J3 F, D
  157. - x: n$ Y+ H$ a! e2 T
  158.         Dim result As Double/ I5 S$ e* J& Z0 i0 q& R

  159. 5 k) R/ g* z* J6 b
  160.         Try) E  P9 l) N8 R( Y# B6 _' N1 b
  161.             Dim nullNXObject As NXObject = Nothing" N; v" i' \' B# g+ q

  162. 7 Z; h% b$ N. o
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    6 Y/ g6 F* u# B( |- A( o  _
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject): F. ?% @) h/ G  }

  165. ) D  Q' v9 O$ z4 c' L; }
  166.             measureDistanceBuilder1.InfoWindow = False
    ( |/ p. @* {7 y  Q6 K7 f6 j
  167. 1 I! M3 o2 x2 o' C  c
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum6 a% c2 L  V9 n% G+ O
  169. " x' s2 C7 d+ _5 I# T
  170.             Dim nullUnit As Unit = Nothing/ t) ~) J1 e5 z9 _

  171. 4 k: z2 S) `% G9 L
  172.             Dim measureDistance1 As MeasureDistance
    8 a+ A0 v# k% a: G' L; }% y3 a% S
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)5 h7 Q1 H. X% r0 o: r
  174. $ i' ?& p* p" G, ]
  175.             result = measureDistance1.Value
    - {. r  ~- R  I& D1 K  @2 p

  176. , H9 D1 U7 f- O- y" K
  177.             'measureDistance1.Information()
    4 F* c7 b; R: @
  178. ) v& ]1 e6 M7 Y; P. |3 p; ~
  179.             measureDistance1.Dispose()3 i# k. {' U4 l8 d! }

  180. 0 ^& N) ~  U' T  S8 b0 l8 s5 }
  181.         Catch ex As NXException
    " g" L) K; x  x1 I9 L
  182.             MsgBox(ex.Message)! h4 Q% Y( g8 D8 F# X
  183.             Return Nothing) N0 w  G7 Y/ z  }# w0 w: u
  184. 3 r8 j! r; k/ Z/ H! `7 G, f* N
  185.         End Try: [4 n6 W" `7 x$ Z7 _
  186. 2 `0 t7 R; W6 v; O
  187. 4 J" x, h1 E* @1 a4 C' Z$ `0 T
  188.         Return result
    , w9 A7 p& f1 E" z  b8 v
  189. 1 f9 b6 g% i6 z# \. h
  190.     End Function5 a2 q8 K, h: V! ~: M* y5 H
  191. 9 D8 G! h* m3 g5 \- B

  192. & I0 R. H" V% U% C8 Q
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer& I, W% K# W5 Y/ L* E  Y
  194. + s/ i9 p+ M1 Q8 s4 Z' ]
  195.         'Unloads the image when the NX session terminates9 P" m8 Z+ L" G
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    + x7 X' p; c3 ?$ t3 x0 D' z
  197. * \$ @4 u8 a7 B, Z( K
  198.     End Function& g$ q7 F6 q( R' @

  199. # S( K+ o* O: ?. s. D. G
  200. End Module$ W/ {3 a$ C, [* G: r( ?
  201. </P>
复制代码

. G. u7 v, {! ^( Y' D: U6 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二次开发专题模块培训报名开始啦

    我知道了