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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x

* ~/ K( l$ C" {! k# U- Z4 h! Y, \6 yNX二次开发源码: 装配中选点,并报告父对象
4 g4 F( ]3 |8 o

  1. ; G8 o% R, @- T- s# f7 b
  2. <P>Option Strict Off: b" ]: U7 U2 ]/ _$ p) P
  3. Imports System& g  l) O. j  x- q, Q. W
  4. Imports NXOpen/ i6 F/ f& |. ]. J% ~$ f# d+ ]
  5. Imports NXOpen.UF7 k4 v5 ^, D. a6 S1 H

  6. + R5 r  i4 ]/ a2 x6 D- ]
  7. Module Module1
    7 R3 e5 @0 [1 L1 ]3 @7 }9 D" D
  8. ! \* H# X* @/ I5 i3 b9 k
  9.     Dim theSession As Session = Session.GetSession(); I9 _- ?' T8 I/ ?0 x
  10.     Dim theUI As UI = UI.GetUI()
    1 k2 y# }, ^  _0 y4 a/ [7 X- q5 ?
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()/ h: e6 B( [- k3 \; o- R% r" j
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    . ?: g) f: z( C; A" {; A
  13.     Dim pickedPoint As Point5 H& S5 n. q: k; C; H4 m
  14.     Dim myModelingTolerance As Double0 W  S% n# g/ r# z

  15. , K: v6 w- L* H: G; o% u/ I# n
  16.     Sub Main()
      {* s2 `; ^/ g& c2 T

  17. 1 x, u1 ~& O8 {, S/ H, G
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    , K3 k8 B+ w% x

  19. , K4 l, N) Q7 O' i
  20.         Dim workPart As Part = theSession.Parts.Work, \: O1 q! [* \, M
  21.         lw.Open()" n: _  t9 \7 S8 O5 z) j

  22. / y' U/ n0 L4 g7 A1 e
  23.         Dim myPointTag As Tag
    : V& T7 U& p% f  N
  24. - z7 W7 L: z9 C/ I- B+ U5 E0 I) Q
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    % s. v' J& I" m5 c
  26.             Exit Sub
    . C: m  A% a( @# [  `
  27.         End If
    . T: V, j( {9 B, O) ~

  28. , [; l+ U: Y5 Y2 h
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)( z& q% J9 q) K

  30. 3 O0 t* {) q, f5 r
  31.         AskParents(myPointTag)
    ! n6 D9 D/ M& W7 M

  32. + U( ~: I" i. i" ^4 A9 d  L
  33.     End Sub2 d0 H3 O8 w; a7 v- x, l& b
  34. / b" G8 e" e  _' G* M; w
  35.     Sub AskParents(ByVal objTag As Tag)
    0 |3 K: K: p7 y

  36. " x5 l) A" }; @7 y( U5 s% v
  37.         Dim n_parents As Integer  g: b9 r. m# U) T0 C$ `
  38.         Dim parentTags As Tag()
    ( O- X$ P5 m6 {/ R  [
  39.         Dim myPoint As Point
      d+ w9 \) z5 \' @" d1 i2 q
  40.         Dim myEdge As Edge
    4 m6 c* q0 Q" u" j. i
  41.         Dim myXform As Xform3 ]) ^$ s, n: K- y: ~! p
  42.         Dim myTaggedObject As TaggedObject
    % F( N" k" P" f# `- A! [. f. v
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)/ n9 `% B0 N2 A5 ?
  44. + |. _/ [7 [2 Y- j7 |1 g
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    & U3 _# i; _6 p# Q. b
  46.             myPoint = myTaggedObject# T; r: y6 j* q! n
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)/ w) P; c- W! {/ `6 z' S
  48.             lw.WriteLine("")
    7 X; t# Z7 T: i% R- b
  49. + s: m- l4 i, f+ a
  50.             Try1 L3 F7 G% \# a) \1 a2 I- b
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)7 E  {4 N- j0 P
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    0 }& H% e- y4 C: N5 J) o& c
  53.                 For Each parentTag As Tag In parentTags
    + ?7 c# |! E3 |& e7 Q
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    6 L5 u" y+ }7 u

  55. 9 J8 Q$ J6 d5 o
  56.                     AskParents(parent_object.Tag)
    " a0 d# t2 C) {8 `
  57. 5 `* e  ?" i. L8 U
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then+ A9 V  S3 L0 U8 D5 |; u
  59.                         myEdge = parent_object
    3 t1 J! N! V, U) n' a  R( P9 _% B& E
  60.                         lw.WriteLine("")" T# C% o& j8 D* B. o; x' p( c
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    : f8 h' p' e2 a7 L2 @- R; M: ~8 a
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)( }, s2 p) i" P0 [7 f* N
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)( o" _' }5 e# M( M) R& j+ T

  64. , S% Z; z7 E: C. \- v
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    9 r  D! N3 n, ^5 `
  66.                         Dim occTags() As Tag
    ) e7 C/ F* B, n: O
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
      B- T3 ]3 X6 ], }
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    6 T. d  t+ l$ k1 U/ Q
  69.                         For Each temp As Tag In occTags
    5 A: S# {4 R$ G6 D8 ^6 ~6 s
  70.                             Dim myComp As Assemblies.Component
    # o7 @$ Z! v0 Z
  71.                             myComp = Utilities.NXObjectManager.Get(temp): v9 o+ J( {6 ]4 n* f
  72.                             Dim myCompPos As Point3d5 w8 m6 T- i$ W& R( E8 W
  73.                             Dim myCompOrientation As Matrix3x3
    ; Z+ J$ z# `. P0 r, `. T
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)! ?1 z) F6 c8 e

  75. ( f1 _) `! B( N5 @' T
  76.                             lw.WriteLine("")
    6 a0 R, V* S. E
  77.                             lw.WriteLine("component name: " & myComp.Name)- d3 M5 D* Y3 |  u: g
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    ) [$ T" m8 M! x5 @: I' P2 }! K0 @8 ^, ~
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    : z8 }& d! j7 h4 `

  80. ! ~# ^/ ]0 e( @! _- ~* z+ @: S4 G
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    & i6 h5 f' T$ d$ z5 ]
  82.                                 lw.WriteLine("** This is the component that was picked **")% ]4 t4 J5 r4 u( f+ ]1 m
  83.                             End If1 F3 K7 Z# u, a: n0 w

  84. + X: P, a- v9 C" w
  85.                             lw.WriteLine("")
    6 x& y. R9 l( i+ O- p! X8 u
  86.                         Next
    6 D0 S% p9 C6 q/ q
  87. 5 o6 }! ^0 L, ]6 Z; j1 A2 S7 U) j" ~
  88.                         lw.WriteLine("")6 K5 t1 D! @- d" w3 h7 o! \
  89.                     End If7 B9 k# M: X& Z8 q- @- E
  90.                 Next, _, M/ o8 _+ I; ~1 F( D

  91. ) I& u: v" G* j) j
  92.             CaTCh ex As Exception) O* ?6 r6 K0 C6 I( V
  93.                 ' NXOpen.NXException: Current object is not smart
    6 v1 J1 w9 d/ n1 y
  94.                 lw.WriteLine(" Error: " + ex.Message). b7 s* L  w! f% T4 W* Z
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    $ T1 N( B* h$ W/ h
  96.             End Try
    1 g% c" g- e' d5 k

  97. 6 R9 o' |, Y3 f
  98. & B! D% w; ]/ I( Z& n
  99.         End If
    ( a4 e8 M( \$ R! l

  100. ( @! Z) o4 M+ h" }8 a( X! |

  101. : `! X2 ^! I- w, F  s5 o3 V
  102.     End Sub* P; d2 {; E/ E: Z# T
  103. ; U* Q" B/ [- a# R" n% b' ?+ }! g
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer7 j* ~/ Y5 f7 t5 Y( b. S
  105. # T) ~- P; _6 S- h$ N7 M, C
  106.         Dim base_pt As Double() = New Double(2) {}2 P6 P; E6 k0 q
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    / ]' q+ M, ?$ O% i+ m& N8 g
  108.         Dim response As Integer = 08 z, g! T7 _4 o9 ]. H4 e. P
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
    : q& G% ~; [8 b/ j
  110. # ~  z: H/ u7 U9 @& C9 W
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)# P% D5 l/ `, m9 P
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)7 P, S1 Q6 p/ f2 o" ~
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)- i; }1 \$ m/ D) G% l" V

  114. 2 g7 Z6 e: {& i: T; v! L
  115.         Dim pointLocation As Point3d
    2 Q+ b2 h" }2 q& U3 o& F: {: z
  116.         pointLocation.X = base_pt(0)* M9 g: A" e" v/ S9 R! k
  117.         pointLocation.Y = base_pt(1), _  p  Q! A3 N5 F2 y1 M
  118.         pointLocation.Z = base_pt(2)0 E1 H: ~: B$ U
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    1 O6 w! w; W+ T! R  j4 I2 V* g
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    + W3 U3 [; |- |1 x
  121. # C) K' v& ?: J" L6 {6 R  }
  122.         Return response
    7 E2 I* Y6 m4 [6 d

  123. " g# I$ }0 Y* \6 o* }
  124.     End Function
    , V! y) I- f5 }/ O' H

  125. 3 ]6 `: r; C- c; i3 s1 E" A7 H
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    5 z: y5 y) B* H* ]5 T) U
  127. ( t( M; e, ]' @- I0 ?6 e+ C
  128.         Dim selObj As TaggedObject
    1 w! [- D8 z8 F. z0 C
  129.         Dim theUI As UI = UI.GetUI
    " y& Y/ l% K5 l* D
  130.         Dim title As String = "Select a Point"
    ; s# k7 f# m0 r- _
  131.         Dim includeFeatures As Boolean = False
    ; Q, v: g+ q4 n* S& U8 C9 F2 [
  132.         Dim keepHighlighted As Boolean = False
      H6 l2 \7 t' ]+ i: h+ Z
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific1 D0 M! V, x, v
  134.         Dim cursor As Point3d' [" e, l- t1 i5 x. r* ^! |
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly9 M. s' E* \2 m
  136.         Dim selectionMask_array(0) As Selection.MaskTriple! O+ P! N# Z2 A) I' G

  137. ( a' s0 F; w; W- w& D
  138.         With selectionMask_array(0)
    5 \+ ~0 d& ?" Z7 b- C& W* M; G
  139.             .Type = UFConstants.UF_point_type2 d+ t/ a9 Y0 a; h  s' W
  140.             .Subtype = UFConstants.UF_all_subtype$ R. S, E3 a& R
  141.         End With
    / |" `6 l2 L% X# O; x+ X; r% W) `% o
  142. $ T& I& r' F, M, d1 O& U9 g0 J2 \- c
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    7 f' [" A2 T# e8 b
  144.          title, scope, selAction, _
    2 ^5 O! R& X$ g8 H7 ?4 Y9 w6 O
  145.          includeFeatures, keepHighlighted, selectionMask_array, _- `% b8 i, M; P. S
  146.          selobj, cursor)0 i4 O. O7 T3 {; S2 B2 D
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then2 k5 @' S5 A, T
  148.             selPoint = selObj
    " l/ q; C) Z0 W( E3 ]: n* p
  149.             Return Selection.Response.Ok# K0 p' x8 x2 G- k
  150.         Else
    4 ?% z" [$ L, c* h- a; H8 V
  151.             Return Selection.Response.Cancel1 L1 Z$ G$ n6 L  i/ w1 |) @  j
  152.         End If
    # h! B2 W) a3 l) ^
  153. - d2 V6 `  P) w& Y
  154.     End Function8 y2 O' P/ p& T* p, s8 Y

  155. # z1 q' Q) o; t4 E
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double$ }3 H  h6 P' s7 y: ?0 y3 n7 l, S$ Q
  157. + t; y- Z2 ?8 k: W/ K# g1 [
  158.         Dim result As Double# U1 I8 r5 l# ~* S& Z7 f, V  U2 l
  159. / W! F0 Q3 u0 H* M
  160.         Try
    2 ]8 O  c3 K3 P7 f  Y, W# H( |
  161.             Dim nullNXObject As NXObject = Nothing9 z! Z2 K7 m0 Z: H! h6 n
  162. : P3 z5 Q1 K5 @* b. [
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder. s& f. k* X' x. a& M  T! Y
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    5 f# S+ O' d$ `& b5 Y

  165. 5 p2 d* E  f2 A# G9 G
  166.             measureDistanceBuilder1.InfoWindow = False
    * E5 R$ m6 d, Z7 O
  167. 8 o' Q: e: Q2 M) o/ u* r6 G0 K
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    % H* m( M1 ^- U9 m6 @) ~: S6 a
  169. 2 B4 p: h- e+ n- V6 c' C9 K
  170.             Dim nullUnit As Unit = Nothing
    5 y; P7 i3 j; ]6 X- x9 S. h! T
  171. 7 }$ b  }7 |8 n1 r5 ^4 @  i
  172.             Dim measureDistance1 As MeasureDistance5 L  A. @; S1 Y8 L) }+ {
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    8 v7 A. n; Y- n# ~2 l+ K% F& E
  174. 3 N# J# N! F- I+ u$ j& c& K( N- G/ l
  175.             result = measureDistance1.Value1 _2 Q( Q$ h4 {; J0 x- E, l
  176. * `8 k: [- W! f& j& O: Y
  177.             'measureDistance1.Information()7 e; H, J1 r* x3 I8 Y% j

  178. 9 }  t  l. i. x1 ]- w9 H5 C4 J
  179.             measureDistance1.Dispose()" l2 }. U, T" U" L* a0 r
  180. % [, B- s) @3 b1 t0 L) {% l3 w
  181.         Catch ex As NXException4 Q, W9 ^4 K+ ?, }
  182.             MsgBox(ex.Message)" P* e* n7 u& w" F
  183.             Return Nothing9 F* e$ G# g* K
  184. 5 W1 _! m" I6 o* I! r9 I
  185.         End Try9 T/ r% Y8 m/ C4 W

  186. 0 a0 K! O. s' u1 n4 ?% l5 D

  187. 6 Z5 J9 y' ]2 n, z9 d/ h( a9 Q. P0 J" h. G
  188.         Return result
    9 M, X. o% a6 y0 T
  189. * K" i& V8 ?" _0 m. L) r# N
  190.     End Function
    6 B3 N( B# V! I$ V  H9 U( Y
  191. 1 s* X2 I3 e' ~8 l4 v

  192. # i+ \7 w! G: ~2 V' r  g( q0 k& |  Y
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer8 X& |8 ~" l! l% v$ S7 C& ?
  194. 4 G1 n- B% t) |' V
  195.         'Unloads the image when the NX session terminates% ?  f: d" ~% C8 l, x; m7 |
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    3 T2 G, V- \- k, ]( {4 s# D
  197. ' ~5 \& A$ M  {' \1 m; H# ^
  198.     End Function
    * `' W; ^, I  ~6 F2 `! D/ E
  199. : \  L- k  p5 z( ~
  200. End Module
    6 T7 K" H; j- \+ l4 i6 {
  201. </P>
复制代码
7 Y, f7 |0 r" x* 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二次开发专题模块培训报名开始啦

    我知道了