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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x

4 G5 O0 Z5 G7 H! K- O( XNX二次开发源码: 装配中选点,并报告父对象
8 t6 c+ H) t( j5 @1 u$ L
  1. 0 I0 T7 H8 n+ X& k. q6 g* d
  2. <P>Option Strict Off5 p, ^, J" Q) @/ k( m
  3. Imports System
    % r' @( t! s! n( p; ~
  4. Imports NXOpen% k9 ~6 x, u: L
  5. Imports NXOpen.UF
    ! o; ~( v& r3 h# P) i4 ?
  6. ' T8 ~- I" b- c! ]8 B; i& E0 L2 V
  7. Module Module19 w! u- X1 H$ f  F
  8. " u8 S6 O5 k: ]/ O, P' A
  9.     Dim theSession As Session = Session.GetSession()- o' y1 E4 N; r/ i) e
  10.     Dim theUI As UI = UI.GetUI()" F8 w' |/ R0 x
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    - q# \  P0 q  h
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    2 D/ \* W& q  N6 e/ k! y. F! t0 }
  13.     Dim pickedPoint As Point
    # o$ g5 @' s) L& H, Z/ x- `# o+ @
  14.     Dim myModelingTolerance As Double* S/ N4 e+ M4 L' _+ _* T
  15.   a2 |2 l& B( D2 F% p
  16.     Sub Main()
    8 i% z# [% L4 o- K

  17. 0 b5 A# E1 Y  ^- z
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)' J1 J; e& T3 }- A: l, }
  19.   v" f8 G1 D3 A$ ]; V  D% x
  20.         Dim workPart As Part = theSession.Parts.Work
    ) A) x. s3 N0 H! F+ O1 u: B/ _
  21.         lw.Open()0 C/ A4 J) _, T0 z
  22. * X( k5 x$ v. P$ H  P$ e# U
  23.         Dim myPointTag As Tag
    0 g5 L! C# [7 m+ `$ [- h
  24. , x6 S3 ?1 x# `
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    & T) S  o+ _3 R# S
  26.             Exit Sub
    ' y' \! `0 U/ \; B, B: ?
  27.         End If
    " E( J$ y" w9 n! d8 ~9 j$ n' Z

  28. / o/ j4 G+ A# ?, i
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)! a9 @; q/ S( Y5 W. B; }* s3 L
  30. + p3 N: D+ p# n8 d
  31.         AskParents(myPointTag)
    3 f; p2 Z! W& |( p3 w  c4 j! g5 @

  32. * m8 `. S3 E! g
  33.     End Sub
    & |. t3 m4 d; N/ i8 p
  34. 3 o' e6 _& b2 L. ~+ P
  35.     Sub AskParents(ByVal objTag As Tag)  B: Y5 {6 X& t2 X- g4 O

  36. " A2 h. Z# n0 C) A! ^$ V5 r
  37.         Dim n_parents As Integer
    % [( F" K$ E$ Q6 Y3 A
  38.         Dim parentTags As Tag()
      ~( Y9 p. e6 g; k" B1 L7 q9 P9 J
  39.         Dim myPoint As Point
    8 ~/ K# B* Y. j# S% i: f9 B, [
  40.         Dim myEdge As Edge
    - d& ~; F# n* d
  41.         Dim myXform As Xform
    3 g8 J$ n/ W: H" a
  42.         Dim myTaggedObject As TaggedObject+ V& Z+ R9 Z5 Z( G. Z$ q& s7 e2 q
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    ! N; M' f1 ~: a! K

  44. 7 _( F  w3 ^! \* m9 M, s' K, M, _
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then3 r. e9 D- u0 O
  46.             myPoint = myTaggedObject- c6 R6 ]9 A0 N& ?! T
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)3 }6 W2 G- d, A' V
  48.             lw.WriteLine("")
    & x; k& c5 M1 _' o
  49. 1 I6 U: f( K- o4 Y# g! `4 i: \
  50.             Try
    ; R0 N: {' n( g2 x0 a
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    ' y8 b/ X3 t, }9 O- D5 X6 O' L
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    0 ]7 L/ k1 B/ A
  53.                 For Each parentTag As Tag In parentTags
    9 }* K( D+ [/ V. ]# g, m8 S
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)/ m% ], D- E: t5 _  }, {

  55. & I/ [0 F6 u# r: C) G7 ~5 r
  56.                     AskParents(parent_object.Tag)
    . M  q+ t( b% O1 H/ {

  57. 6 }: M6 P4 O/ ~; g
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then) _5 s4 S! s7 e8 W' k. F7 \0 s
  59.                         myEdge = parent_object
    ) y5 F1 e7 b9 M- d' d& F, U4 H# B
  60.                         lw.WriteLine("")( g1 b2 t+ `5 u. o+ y' _
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    ! W7 j! ^# Z, W: g. \8 e" Q/ X
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)3 k* B' q9 l( J; i5 C1 G' c# L
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
    3 ?" {1 d% b' P" j0 A( B) k9 D
  64. / Z. O9 T5 M/ V: A6 q8 B
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    + a- C5 k; p: t+ v
  66.                         Dim occTags() As Tag
    ) \% n0 w8 W# {8 q# L" ^  I" U
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    + B$ U/ W0 {$ b: V$ v
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    7 r& G! `  G5 n
  69.                         For Each temp As Tag In occTags# g3 ?' k# q/ L( W7 k
  70.                             Dim myComp As Assemblies.Component$ v& o9 z9 b& ~+ A; Z
  71.                             myComp = Utilities.NXObjectManager.Get(temp)2 w  V( [/ g$ E4 G
  72.                             Dim myCompPos As Point3d
    ' k) a; E8 p, J' ^
  73.                             Dim myCompOrientation As Matrix3x3" T. W$ r6 Q; t( q
  74.                             myComp.GetPosition(myCompPos, myCompOrientation): C" l) [5 Y+ H. D  P

  75. * N. s: u# g4 _  y. N
  76.                             lw.WriteLine("")
    * K7 M- b' T8 f
  77.                             lw.WriteLine("component name: " & myComp.Name)
    , A. P( C9 z" g9 x" L; U2 G+ p
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)' @& X% j& Y% a. t( Q
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
      o! X0 e$ s: x" \% M$ c' I
  80. ; b4 D, [6 C, R, y# Z
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then3 v5 R% \6 Q/ T+ i% D+ g4 T
  82.                                 lw.WriteLine("** This is the component that was picked **")! }- e2 B  m9 ^; i: N
  83.                             End If
    2 \8 y. S; [* r1 j0 h

  84. 2 l0 E9 t- b/ t
  85.                             lw.WriteLine("")
      G( o$ h$ v' q
  86.                         Next
    $ k" e6 e! f4 v. P0 s8 O

  87. 9 a. l9 d- ^* e' K8 Y5 C3 ^
  88.                         lw.WriteLine("")
    * R8 v2 [/ a2 R( x
  89.                     End If, W4 ~! l+ O- t, h0 I
  90.                 Next
    " W! T2 |' g# R: _8 k
  91. 2 A/ h2 U; z( q
  92.             CaTCh ex As Exception  R+ S8 Y0 s9 Y5 Z% g. A* Q5 `
  93.                 ' NXOpen.NXException: Current object is not smart. V* {' Q5 _' W7 C  ]
  94.                 lw.WriteLine(" Error: " + ex.Message)! Z" l8 C+ j  n! k9 |. O6 \0 X! a( F; ?
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    3 h& e3 d1 R$ T, Q
  96.             End Try4 X. G5 I8 `% _
  97. 1 p6 E+ H! G* T$ |* {0 [

  98. " i% S: p" N% _
  99.         End If+ B) p+ A6 q! B% ]$ ]: ?
  100. + h. W  d+ f0 W& ^6 C! U, _

  101. 8 S* T) |+ d- L+ a# g" w; Y9 j
  102.     End Sub
    " D" H" j: g; ?# ]& D

  103. : {" j0 |' e, p0 ^9 @6 ?
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    - O" _: I8 f2 g  Y& [0 {

  105. 6 X. D, Y1 O# S9 n( e
  106.         Dim base_pt As Double() = New Double(2) {}
    4 v$ J  N! n/ m/ }; o" r
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null" s$ `7 l* g! k; {; Q+ C( h$ W. j5 x
  108.         Dim response As Integer = 04 v+ s! c. P. y- K7 g0 \
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt+ S  T8 e& }0 I  e! _- E) L; N1 {5 H

  110. - Q! Y4 n9 |; {( u+ ?8 K
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    ' U1 `! j& D5 S2 {
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response): V+ j' @& Y; g7 f
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    . ]& o" X+ a9 J1 W

  114. - y2 W) k6 G3 @# V; h* |$ F% o' w& O6 C
  115.         Dim pointLocation As Point3d5 T/ E; z2 u% @2 @, ?; a
  116.         pointLocation.X = base_pt(0)
    . M9 e5 `; m. I) j) y2 L# ?( h4 k2 o. `* d
  117.         pointLocation.Y = base_pt(1)6 p% P8 X0 N  O! b- _, @9 z1 f2 p- Z- p
  118.         pointLocation.Z = base_pt(2)
    ) F5 }7 _% [4 T! k4 f7 A# n) w
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation); H# ?. B- c* v7 R; w
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    & s  C* K- l0 ^! a% m6 z
  121. 9 u$ r4 a: [2 D, v5 W: g
  122.         Return response
    % a' i4 q) N3 E& ^' ~! T

  123. 9 d& v7 n! J" T7 D4 C0 w& W7 y& r: {
  124.     End Function' c3 Q, \/ t% G! l5 ~; }

  125. " ?7 E8 E6 q- b' r1 B7 f0 k5 o9 m$ ^
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    & B* P) w! h! `& h$ C, V* U

  127. 2 i* c5 ^( g7 j* Z4 T6 F+ o
  128.         Dim selObj As TaggedObject2 ]$ Z5 @( @# J7 i' D) U/ E4 w& H; X
  129.         Dim theUI As UI = UI.GetUI
    $ s2 k/ l6 T3 K" \0 S: O2 n
  130.         Dim title As String = "Select a Point"# |' |  }: H4 C$ S% ~4 q+ C: I
  131.         Dim includeFeatures As Boolean = False
    ' S/ k1 G7 J  D1 b# z; o) n
  132.         Dim keepHighlighted As Boolean = False
    # H0 |% d. N% Q9 d
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    : g# I7 B' F) Z0 P! q  G
  134.         Dim cursor As Point3d0 ^! e% |8 v2 F3 M9 r* X
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    , ^  K# M+ @$ w# X6 G' K8 C: I# B* p7 x
  136.         Dim selectionMask_array(0) As Selection.MaskTriple
    & f6 f, s% t7 L! B  _; Q# i+ A5 N

  137. ! `- z/ s: {2 ^% n2 E9 x/ n8 g
  138.         With selectionMask_array(0)
    ) \& c! o3 n5 ?. s( g2 e' v  M5 b
  139.             .Type = UFConstants.UF_point_type! {6 W/ ?& m0 p% ?3 N
  140.             .Subtype = UFConstants.UF_all_subtype/ T! A4 N4 c  x6 z* W, c9 B3 x) M
  141.         End With2 ^9 _- `! Y$ J! w: k
  142. 4 b  ]. Q  c4 U5 s% t
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    5 Z4 V+ D- L8 W7 _0 D) q
  144.          title, scope, selAction, _1 S' A; M, D6 D' Z% X8 P+ s5 ~
  145.          includeFeatures, keepHighlighted, selectionMask_array, _2 p: ?8 Y! Z+ r* M
  146.          selobj, cursor)
    ' q9 B$ Q% n/ x3 h; n) V1 \! {
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    " V6 N7 I, W( k$ Q
  148.             selPoint = selObj
    $ o* |! Z& G6 F( v: a5 G
  149.             Return Selection.Response.Ok
    ' T" A! D9 Q3 `
  150.         Else  g4 o* L! i) B1 K+ n
  151.             Return Selection.Response.Cancel
    6 `% M1 a& e& q) V0 }" C5 M
  152.         End If  F9 ~1 R  k( {8 c: v
  153. 3 j3 L7 M9 y3 W5 {# o- A
  154.     End Function
    , F  b& u* m9 \, I7 r

  155. 7 r7 L+ ~% t0 N3 y, ?( Y
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double0 c: A8 a  a/ h* o' k
  157. # C9 U1 W. [3 p$ U# R+ \2 a/ W3 H! q
  158.         Dim result As Double3 a* D4 X% l) J5 }

  159. " w; S' U' _. x2 O! r+ K+ ^; R
  160.         Try
    ! j1 Y. s# |, X* h" L% [
  161.             Dim nullNXObject As NXObject = Nothing
    4 g! z( z8 a- A& ~( M$ A4 t/ \9 v

  162. : ]! P* }# s% K9 q- R
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    ) ?0 t/ O1 F6 L
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)  f; d' j1 y8 l
  165. # m1 f- C/ r# |* S- c, B, a; I
  166.             measureDistanceBuilder1.InfoWindow = False$ ]0 M! E* t8 \3 _
  167. % b% Y( F/ G) v( n/ f8 f( k
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    0 Y0 l6 t* w' a7 @1 g& C; M" ^

  169. 9 z1 O/ H/ m3 x3 w. E/ H* B
  170.             Dim nullUnit As Unit = Nothing  V) M0 q) K/ J# Y  @9 x9 C
  171. + R  \- Z. N# b, i% T6 X  A. Z; ]
  172.             Dim measureDistance1 As MeasureDistance
    , l3 q# b# c0 k9 T. S' r! p7 e
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)! _0 {7 T9 G- \* e. ^( M- o2 v: M
  174. 2 K! g4 o6 ~4 x: o. a! N1 q
  175.             result = measureDistance1.Value6 k/ k  N" i9 y2 S2 g

  176. + X$ U/ h, f: H9 f" _3 b- n& j  n
  177.             'measureDistance1.Information()6 Y6 j0 h" O% r* O
  178. ! L# _/ a7 n! W8 g  V
  179.             measureDistance1.Dispose()* F2 M- O, ?% j- a5 W# l
  180. 5 r0 r- K6 V5 Z0 H! j
  181.         Catch ex As NXException
    ' M3 ~2 c8 z; _2 h# b7 y! {* U) ^; C2 L: @: K
  182.             MsgBox(ex.Message)8 D' q: d3 Q& C$ h) H- F- [
  183.             Return Nothing
    $ C* p* l/ J/ H' Q, G& W
  184. , n, E: [( Z- q3 P3 o
  185.         End Try7 k) Y3 y! X( i0 o
  186. & [  [0 L  H% D. v/ I: c
  187. * s3 H0 M( X% N+ @- K% r
  188.         Return result
    / q9 o1 p" |# B) |- a5 @

  189. + x: {; D% h( c: F/ [6 U. m
  190.     End Function- M( p+ p( C& G0 E: h; B. p
  191. / g. n: l3 N2 s+ a) M6 D
  192. , x' ^6 d7 i& R! M( ~3 K
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer; d3 h# a: V" Y, g  F8 |
  194. 3 f" v% E7 J4 X% `4 J$ _8 U
  195.         'Unloads the image when the NX session terminates
    ' q6 q) U) [* u* \
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination, z2 g" p$ y  ^$ H2 M8 c* ?
  197. 4 {+ @  [8 Y3 o; ~
  198.     End Function
    . ]0 R) s; p% L+ _

  199. 9 D2 [2 g  |) y' y& o$ s
  200. End Module
    " o8 C7 M9 [2 \9 O, \4 b6 ^! x
  201. </P>
复制代码

" ~! D4 e, u4 g$ J3 y, L0 P1 @
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了