PLM之家精品课程培训,联系电话:18301858168 QQ: 939801026

  • NX二次开培训

    NX二次开培训

    适合初级入门或想深入了解二次开发的工程师,本培训结合ufun,NXOpen C++,大量的实例及官方内部的开发技术对于老鸟也值得借鉴!.

    NX CAM二次开发培训报名 NX二次开发基础培训报名
  • PLM之家Catia CAA二次开发培训

    Catia二次开发培训

    Catia二次开发的市场大,这方面开发人才少,难度大。所以只要你掌握了开发,那么潜力巨大,随着时间的积累,你必将有所用武之地!

  • PLM之Teamcenter最佳学习方案

    Teamcenter培训

    用户应用基础培训,管理员基础培训,管理员高级培训,二次开发培训应有尽有,只要你感兴趣肯学习,专业多年经验大师级打造!

  • PLM之Tecnomatix制造领域培训

    Tecnomatix培训

    想了解制造领域数字化吗?想了解工厂,生产线设计吗?数字化双胞胎,工业4.0吗?我们的课程虚位以待!

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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x

- b6 n9 ?. `* ?* A) j8 JNX二次开发源码: 装配中选点,并报告父对象; T, ^7 B% X  H: `
  1. ; {0 R- g3 w, v
  2. <P>Option Strict Off* T0 A/ f4 y( r6 r' P- Q2 Z4 ?8 I
  3. Imports System# S; P2 @  g8 r/ l2 @" ~' f) q) q# F0 h
  4. Imports NXOpen+ H" _+ s: g- ]. M4 y% q. q
  5. Imports NXOpen.UF
    $ b: l+ I- E3 y+ Y2 F

  6. * j+ v( y) Y. \: R* A4 E7 E
  7. Module Module1
    , D2 L. X! e( d6 I; O
  8. ! X& i. D( p3 s7 d# k% h
  9.     Dim theSession As Session = Session.GetSession()
    % [1 Q! [) C' c: F4 L  E5 o5 e
  10.     Dim theUI As UI = UI.GetUI()
      n( i$ O0 P' k- t1 C9 @
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()1 ]& X8 [' U4 O4 A
  12.     Dim lw As ListingWindow = theSession.ListingWindow! ]  i/ d$ P% F& H' [
  13.     Dim pickedPoint As Point6 `8 W  I3 `) J9 t& A
  14.     Dim myModelingTolerance As Double1 r: R% r) ?* r8 ^5 T% ~/ P( m
  15. , _4 |/ p8 X% y: p: F% W
  16.     Sub Main()
    $ J4 G5 U+ Q) J" c: ]$ l
  17. 6 G. C- I# x4 H0 j- n8 h, K, B
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)( K8 I; ~. e: ?, X1 c
  19. 4 |  \$ a; H) O8 ]. h+ p
  20.         Dim workPart As Part = theSession.Parts.Work  p* ]8 ]; m0 Q5 D1 J
  21.         lw.Open()
    $ R9 w" T  B! H2 D0 W/ l

  22. $ c! v& s& L  {
  23.         Dim myPointTag As Tag  G" `* o* k" \4 R
  24. 4 `# }! U+ j& v  p( f& |
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    , D2 o# u8 i% y  |0 y6 x
  26.             Exit Sub; O7 E: Y5 B! T1 t+ Y- x: m
  27.         End If1 g5 i8 M! [1 H& J8 q5 n+ d8 ?; n! u

  28.   }4 e4 p- e- n2 v
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    ) Q: o; A6 B! J$ l% {
  30. : c% y$ U, H' o; |
  31.         AskParents(myPointTag)8 J$ c! k* ^1 G* c% m6 G2 k

  32. 9 M$ C" d6 g9 ?
  33.     End Sub
    6 X/ e( x0 S' v, r$ [1 E
  34. . x2 v& v6 w% c! i4 W4 R+ H: A8 s" Q* h
  35.     Sub AskParents(ByVal objTag As Tag)% ?7 u7 b  \% G# B
  36. % o8 X) t+ J( f5 q  \  l1 t
  37.         Dim n_parents As Integer
    9 k" N# N1 I( p( W$ Y( e6 F
  38.         Dim parentTags As Tag()
    & e' t/ e4 N7 V! r1 ?
  39.         Dim myPoint As Point0 m" N' m, o+ c, w# D( B, w. N  d8 R! V
  40.         Dim myEdge As Edge0 a; d, H% G1 k0 G3 q3 l' [( m
  41.         Dim myXform As Xform
    6 W) E6 P5 @# n& F0 ~* m* z
  42.         Dim myTaggedObject As TaggedObject
    : J+ L) r; D) s1 P4 G+ Z3 _
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)/ P* r. b6 J. p3 \  z, `- u

  44. 5 q+ P8 a- M" M
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then2 X: L  y* o: E. `: f6 b/ ?
  46.             myPoint = myTaggedObject
    8 G" ?  w7 E' Y( E) n/ |( |
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)0 C' y* r5 t' w: i7 k9 w
  48.             lw.WriteLine(""); y/ ]8 j" a4 r4 a% \, x
  49. $ H$ R' S" s3 }- m4 L
  50.             Try
    , I, L) L& x+ p, i, j. k
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    ! [% D% V1 Q2 o
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    5 G6 M0 U/ X% p1 q1 ]! c1 W! J
  53.                 For Each parentTag As Tag In parentTags* U5 y6 I8 ~2 f0 I3 ?- Q9 P2 [
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag). k4 {6 s0 g- @) n( u5 l
  55. 6 d- G- C9 Y$ F4 e+ L; O! N$ a
  56.                     AskParents(parent_object.Tag); Q9 w/ [' D2 h+ P8 i- j/ L8 E2 y

  57. , m6 i; Z  b3 A
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then0 t$ v3 i: }; _" R
  59.                         myEdge = parent_object8 n! p" X5 n+ W2 J7 l) T( O! R9 ^
  60.                         lw.WriteLine(""): B, |3 D/ ?; e+ Z$ w- R
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    , @" [( t8 x" Q9 S* ^
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)* p( P" c: j% a
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)1 d; V" o" l4 Q; l- k1 n/ F9 b- ^

  64. & L6 }2 x( V0 H: a, O+ V- y8 {
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    7 V' y! _* T9 V  v" f  l3 e) K! H
  66.                         Dim occTags() As Tag
    - O! y- \0 D# o; E
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    ' n9 L  Z" b! L
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    " L/ h( ]  W' f/ {$ b, O+ w1 b
  69.                         For Each temp As Tag In occTags/ U! {7 f0 v; i$ [
  70.                             Dim myComp As Assemblies.Component' T7 h3 ]. @( Q) w6 x
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    ; f, t3 z& \4 J$ p# D
  72.                             Dim myCompPos As Point3d: n  p( Q  D; R3 v* ?% d$ O, l
  73.                             Dim myCompOrientation As Matrix3x34 x8 [7 a" u, x
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)) \. x, D) V# O' j) {! B  K

  75. / p; `, \/ Q0 D" _5 v
  76.                             lw.WriteLine("")) [- q! V0 S; F- |3 V1 a0 j
  77.                             lw.WriteLine("component name: " & myComp.Name)/ V" N0 b' r8 Z3 v4 t# {3 h8 u' g
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)5 s: x/ R3 g- w3 H. N8 Y1 ?* m1 m
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)3 o3 _9 @6 c, H7 w. X# Z  x5 z

  80. & k) ~) ]- ^: w4 F
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    + l: `/ x7 I/ S- z& Z, |& D
  82.                                 lw.WriteLine("** This is the component that was picked **")6 i; e  t' E) i/ R. I$ ^9 H
  83.                             End If
    4 W/ @2 u4 U" `5 Z8 c. X* ]

  84. # g3 s: ~6 h/ v3 Z
  85.                             lw.WriteLine("")
    / g) W3 B! @- S7 Z; ^! _1 v
  86.                         Next1 v& A4 H3 w( N, V2 ~3 Y' N" Y
  87. 0 W; c8 E4 L2 \
  88.                         lw.WriteLine("")# q' o6 {" r- R
  89.                     End If
    ! V  T+ q4 A8 P) @2 I4 [$ q
  90.                 Next
    6 c- R9 i" R2 N4 E

  91. : f* J' v6 Y" U, a+ v% ^
  92.             CaTCh ex As Exception" T' g9 C; t' _! P5 O9 q
  93.                 ' NXOpen.NXException: Current object is not smart+ @9 s" s7 ~! H  U
  94.                 lw.WriteLine(" Error: " + ex.Message)
    6 ]7 B  o% l9 k/ d( u% [4 J7 C- i
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString). ]; g. y& m$ g% m
  96.             End Try
    , k0 Z, h0 h8 x% e$ d1 Y
  97. 4 [* }: a' I- C) G* B0 i3 }4 k+ n& {
  98. 2 _0 E. I1 @4 s
  99.         End If+ X) M0 g5 O! B+ B) O: S# Q

  100. ; p8 v  ?3 [, @7 t, I: `* D

  101. 8 U: ?* \4 v, }' }" ]. x
  102.     End Sub
    ! J. B9 `9 Q+ J" q2 x# I, k- J+ |
  103. 5 z' d  M' D3 e4 W1 E7 c2 f
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer( \# u" o" Y8 U
  105. 2 ^' B6 T9 \* A/ ^0 s
  106.         Dim base_pt As Double() = New Double(2) {}
    " C+ T. c- S/ p
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    ' u2 E' V+ |$ e* i# G, t/ c0 r
  108.         Dim response As Integer = 0! E- w/ A, Q' v) A
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt5 y6 \! w. R3 l
  110. 3 w" `5 d, A- j& V- [+ R' @
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    . }$ _. T. F# c$ j; c/ U5 Q1 y
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)& ^' W, O0 x7 m+ a& |' b4 R
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    $ I2 @+ @% [' R: {  V# @2 j

  114. * n0 A* ~  D; U
  115.         Dim pointLocation As Point3d# C$ P/ M4 w4 C- h. e
  116.         pointLocation.X = base_pt(0)
    $ b- g4 h4 X2 ~) ?! e8 i
  117.         pointLocation.Y = base_pt(1)
    " y$ X# g" Q0 |$ G' t: X3 f
  118.         pointLocation.Z = base_pt(2)
    7 K1 Y# l! D. N% C
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)4 J, K: A4 P' b. Q! v9 k
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    1 y6 l& q7 k3 u& _; {
  121. ( v, U; A5 X7 B) g6 f
  122.         Return response
    & P" i" [: ~5 j. q0 |

  123. + z: p3 x; q8 D. t, a/ p# O; f
  124.     End Function( B) u4 z- ?+ S8 p# M
  125. ! N. e3 V6 X, g
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    6 s- u9 v0 E# U  H
  127. . {5 P. G5 F5 B% E
  128.         Dim selObj As TaggedObject# X- y. x+ n/ w7 M6 J1 A. k- c. j
  129.         Dim theUI As UI = UI.GetUI1 w! U0 u6 @4 }
  130.         Dim title As String = "Select a Point"
    ) t, e, O8 x6 M3 o# M6 [6 m
  131.         Dim includeFeatures As Boolean = False
    5 O- l& b0 ~  ~* Q- M7 O6 n
  132.         Dim keepHighlighted As Boolean = False, H' a' c& h( H9 D% D
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    ! R3 m, [" x; E
  134.         Dim cursor As Point3d
    & I$ x4 w; ^, v/ H& [$ h
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly' j# y- u5 f" K6 u5 x. l  \
  136.         Dim selectionMask_array(0) As Selection.MaskTriple1 v4 O6 e# ^  M6 v* J. n

  137. $ @3 I3 w, O! N3 @
  138.         With selectionMask_array(0)
    ) J5 U1 y* m9 y# X) q* [
  139.             .Type = UFConstants.UF_point_type
    % d- u( `( u8 j- |7 n
  140.             .Subtype = UFConstants.UF_all_subtype
    3 z4 X5 ?+ M: z0 m
  141.         End With
    0 M1 b  R2 E" E, h  F6 E. w  B2 E

  142. 4 ?. v) z- s! a- s5 O
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _& {0 i' j" D4 G: O
  144.          title, scope, selAction, _+ {% c2 M) G- Q0 l7 M
  145.          includeFeatures, keepHighlighted, selectionMask_array, _
    ' _& j: }9 U! V8 M$ P
  146.          selobj, cursor)' M( X4 B+ E: |6 K/ M/ ]5 T
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    9 ]* }4 T+ w7 a. L& @2 u; C
  148.             selPoint = selObj( E2 l0 f0 |7 U5 c
  149.             Return Selection.Response.Ok8 z% F" u; F$ G9 S+ M/ N
  150.         Else: X, n/ q* R% |4 S0 f
  151.             Return Selection.Response.Cancel, h& ^9 [4 b, _+ x% w7 q
  152.         End If% `) r' C0 Y( J( x# e  I( X! @
  153. $ a% z- y) m. v- S) }0 T- q% @7 f6 a
  154.     End Function
    ' n& ~8 o& F3 ^, ~2 ], {$ `  }( e

  155. 1 P# \1 r; i  y3 L8 Q# m' y2 N1 b
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    $ W; [" _. R" a& `/ X% Z
  157. ' e' A& m* c' o1 C
  158.         Dim result As Double: N0 R8 N8 X5 S( Y8 X
  159. ( [: S  v8 [( j  Q4 E
  160.         Try- _3 t8 _; v; }" v9 B* t
  161.             Dim nullNXObject As NXObject = Nothing
    " z: x, q/ F8 ~; \* g7 n
  162. # d; a: M& J" e% K- p3 n; ~1 ?
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder! ^* h1 R9 ~% G- o
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    3 q. O* M5 Q0 V+ M& P  F4 i
  165. & K  l( Z7 r& E- v  p! s. S% g7 M
  166.             measureDistanceBuilder1.InfoWindow = False9 z3 z& W$ b) L( N& ~
  167. ' x7 Y) d# X: l
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    9 V( ]+ ?8 a" e! ?, h1 [: C9 J! \

  169. + K; X; d7 h) t" l! Q: v; s' @( o
  170.             Dim nullUnit As Unit = Nothing) T) i% u+ M- `2 P0 K/ X

  171. # P8 c9 N4 _' n9 Y
  172.             Dim measureDistance1 As MeasureDistance4 L4 S8 K, X& |& Q- Z  [; z, N
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    * c" T4 J; }2 H7 u1 M

  174. , |( x+ T; Q1 m- n0 a
  175.             result = measureDistance1.Value& c, S; J2 U9 }1 y

  176.   c7 I, ~9 p5 y5 N( }/ J( e
  177.             'measureDistance1.Information()
    : ~1 }$ G% n: Y
  178. ( [8 e6 f* c$ B
  179.             measureDistance1.Dispose()
    " m# M5 T( D1 n9 ]1 _7 _

  180.   H" [1 v  H; ^+ g/ i& e. V
  181.         Catch ex As NXException5 U/ ]; n. C& ]9 e7 r
  182.             MsgBox(ex.Message)
    9 s. e7 q1 }/ _" n/ M6 z+ J& B
  183.             Return Nothing
    ( p# g' R# D' N6 Y$ {

  184. ) W2 ~$ e, @: q7 O- X7 v
  185.         End Try
    ! t! y1 j6 N# p: b& {9 `: `5 c$ M
  186. ( c5 X" @' h9 s% x

  187. # C1 D. d& I/ U# b
  188.         Return result
    2 M" p8 o8 Q" _7 M/ `
  189. 5 d& e2 ?7 m2 {% y, f+ S. h
  190.     End Function
    $ _( b9 r: p# D! b4 ?0 |# P
  191. ' N1 @7 `8 v) w0 P+ J2 b# N
  192. ' W  N+ E8 c+ r% c
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    7 C' f' w& {4 F/ @9 B( K: M
  194. 3 e; j6 E7 l& P
  195.         'Unloads the image when the NX session terminates5 E/ P/ e9 O, ]; ]& R/ Z
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    7 r  j4 O7 N% b0 r5 h
  197. * i, T6 k: P$ w. g! q0 H) H
  198.     End Function
    ! n! Q7 X5 k8 M
  199. * w) w4 k# J; r) F4 \: M
  200. End Module
    / t$ q2 v' W5 }+ ]
  201. </P>
复制代码
4 l7 `4 g. Q% S, |) X+ b: w& O  l
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了