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 5352 1

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

admin 楼主

2013-11-5 17:34:33

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

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

x
$ G$ I; C1 }7 B2 `3 p7 @
NX二次开发源码: 装配中选点,并报告父对象
- `) @' R; j2 l& u# Z1 F
  1. 7 v' w; |* A4 b+ J
  2. <P>Option Strict Off: X6 Q9 Z# G6 ^
  3. Imports System( \- b$ l. G; r4 N! W
  4. Imports NXOpen9 Z0 P/ v6 m+ H# g( l7 y
  5. Imports NXOpen.UF
    ' O1 e3 m1 e9 ^7 Q& e

  6. ) W% X% Y/ B, j! `" R- I& s
  7. Module Module19 l7 b# r2 B4 x6 S
  8. - _% J- o( }5 a# ~- D6 X  n
  9.     Dim theSession As Session = Session.GetSession()% C% h+ m+ d" t# ]; e
  10.     Dim theUI As UI = UI.GetUI()
    8 f: p6 ]4 ]0 f
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    & ^; `3 n& K( g1 R# H; {& Y+ Q
  12.     Dim lw As ListingWindow = theSession.ListingWindow
    4 w# b' S0 ~, E6 o: W
  13.     Dim pickedPoint As Point
    $ ]! P5 T& T& e/ u( q& i) @
  14.     Dim myModelingTolerance As Double' i1 {1 q8 F0 }) Z

  15. & e$ R+ \) G% x0 v5 v
  16.     Sub Main()
    5 g* C- U! p6 B  O, Z$ n% O
  17. 7 U& T0 I! D$ {  Q; y/ K  {! S
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)& G$ V0 W5 n2 n0 P& T

  19. / v: _9 v$ C, T: X1 P
  20.         Dim workPart As Part = theSession.Parts.Work6 r2 M" c7 V9 D7 M7 \3 |8 e
  21.         lw.Open()) q' j3 M8 l, Q( p" B; M

  22. 4 t8 K' J, B! g
  23.         Dim myPointTag As Tag. i  u8 O2 o2 h& i: b  `1 C0 I* F
  24. + Q/ E' D8 l* E  t& M, k" ^
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then9 ]; ~$ y* v( _9 G0 ~7 }5 V. y
  26.             Exit Sub
    , u$ y" U2 ^( f# A, u: V/ r
  27.         End If2 ?4 ?! n3 R0 u) h" t, m

  28.   k% }5 Z9 |) e5 ?* J1 R: q
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)0 ?  }5 }2 m. n# @% k5 ~
  30. ; R0 B( |) Q  V( s! }
  31.         AskParents(myPointTag)2 `2 n7 X$ c% a- ]* \& ?* \. H3 I
  32. 9 l9 Z" Z& g. g  W" j! t$ f! V8 E  w
  33.     End Sub
    3 u# ?3 @7 ~, O5 j% M# J" z

  34. 5 u* Z5 ^# y4 T7 K4 N1 M8 P0 I
  35.     Sub AskParents(ByVal objTag As Tag)1 s, E) L% U2 j+ ~; X5 O
  36. $ c4 ^4 i: i- `9 E. o& ?1 u3 L
  37.         Dim n_parents As Integer. |5 X; h* R& }) v! w
  38.         Dim parentTags As Tag()
    6 p- x1 Q7 t7 S. D& A
  39.         Dim myPoint As Point
    3 B) X! n. ^3 S$ x
  40.         Dim myEdge As Edge+ l3 L3 @: K' j
  41.         Dim myXform As Xform6 Y# P; F) q$ P9 m) P$ s
  42.         Dim myTaggedObject As TaggedObject% w% L' R* _2 \7 f7 P! `
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    ' v+ L/ ^3 Q, |3 i( C

  44. 1 x6 j, W* U$ J0 \
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then, D8 c$ X7 x" }+ z8 g
  46.             myPoint = myTaggedObject
    ) ]8 L0 k( C6 S% k/ G. F& R
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    / x% b- t( S0 u( q3 m
  48.             lw.WriteLine("")
    6 z8 ^% j7 A* i4 k. _6 F" b9 I

  49. 4 b% j+ k( Q1 D% z- x
  50.             Try" P" b0 y7 P0 i) }/ _
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)0 n: H4 q, P  l
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)4 h& }2 _) {6 D7 @6 ?
  53.                 For Each parentTag As Tag In parentTags
    ! h$ `1 j. }' `; R" H$ e; a
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    2 p& K4 A7 p: S# n) q  ]% _
  55. ! ~# m' L4 [8 u$ ~) R/ |5 y
  56.                     AskParents(parent_object.Tag)8 c2 }8 ]7 \9 S8 {* @7 }  q" Z) Q
  57. ) W. c4 j( {& [4 w: J$ N
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    / ?. A6 |+ O$ p8 p8 b2 w1 n
  59.                         myEdge = parent_object
    4 _1 @0 \/ `0 ^5 \- Q; ]7 Y
  60.                         lw.WriteLine("")
    ' J) p3 n, @( z# k
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    7 V7 O1 }( j$ ^% D% c4 d
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    4 q6 W4 c' n6 z  X) Z* v+ L4 X
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)' P/ r- P" I( [. O4 S/ G

  64. ( X3 ?$ }, X  @8 \+ N) w. q
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag# H! J# X! D+ F) g% I! l
  66.                         Dim occTags() As Tag" n& I' D/ z* F8 [
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
    & o+ q: F% Z8 ~& O0 n. r3 i
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)/ W% B7 G3 i% E+ D6 m  a8 M
  69.                         For Each temp As Tag In occTags6 v* M" W  ?' ^
  70.                             Dim myComp As Assemblies.Component" U7 o% i1 ^! K+ O1 c
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    6 r) `7 Y/ M5 Q
  72.                             Dim myCompPos As Point3d" Y, Q" t* h. z: f  ?2 h
  73.                             Dim myCompOrientation As Matrix3x3
    3 M3 I# t1 y2 q
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)
    & P% b2 L/ H1 K5 Q# Y
  75. + {2 O" V& J  F' d) b! _+ N) l
  76.                             lw.WriteLine("")8 j0 a4 C6 f) R3 U1 a7 v, `
  77.                             lw.WriteLine("component name: " & myComp.Name)+ [" N$ G* x. f2 j* p. v
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName), ]* Y" u( F* v7 `$ q5 r
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)# m' n5 m* q) ~+ E' I

  80. 3 i( q0 m0 F% [
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then1 t: K: ~! w! `' Q
  82.                                 lw.WriteLine("** This is the component that was picked **")0 }4 P. |+ M. ?! F; P% y9 j
  83.                             End If
    $ j/ ^  f5 N4 \& i& n; W
  84. 0 y( O- U) L. r: `
  85.                             lw.WriteLine("")
    ) F1 x: G( Z2 B" {
  86.                         Next0 a9 ~0 G6 {1 j

  87. + q2 H0 @% N; W3 [. E8 j# M/ S
  88.                         lw.WriteLine("")- t3 P) R  j' b! `, z; N/ D$ z
  89.                     End If
    8 {( E  d- Z, K4 a
  90.                 Next' `2 v, I0 g  @5 D# T! [

  91. * s3 R5 u' _6 T+ H, X
  92.             CaTCh ex As Exception, H- g; [2 L; S* |: O! b
  93.                 ' NXOpen.NXException: Current object is not smart1 y  e; N% v6 f
  94.                 lw.WriteLine(" Error: " + ex.Message)! `# e6 F3 V$ I$ N
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)  f5 Q+ i: v2 C/ m
  96.             End Try
    9 |, h/ s3 p9 i$ @  X6 F
  97. 1 l2 T0 s7 L! O9 A9 W
  98. ' u: D2 n1 R: o0 w& G8 F9 o$ l& L
  99.         End If
    - I/ q- g+ Q) l" A- I  W% O- |* Y7 H

  100. * n) W1 e+ f% D4 n+ Q2 s

  101. 0 E1 p0 X0 I/ Z9 \3 T8 l4 B- e
  102.     End Sub9 |6 u1 P4 L, ]0 t
  103. 4 ~3 b( K. L5 D/ s
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    . k, }% d: U0 {$ S

  105. - k/ h9 }! W& U& z/ E
  106.         Dim base_pt As Double() = New Double(2) {}. t2 H6 f1 Z" @9 x0 h+ N! X4 h. f
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    + V. r6 h# B) e5 W
  108.         Dim response As Integer = 0
    ; ?! X# y/ ~. Q
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
    " L, o% n$ c9 l7 a. K) L7 S

  110.   A& {  W7 Z' m+ W2 v& j  ]
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    9 g' Y2 d$ |4 P' g& D: X
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    ' k1 N! R: k0 O
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    5 |5 k1 S  e' \+ o8 [1 N
  114. : l1 l1 O1 h- ]) I$ O/ Z& \
  115.         Dim pointLocation As Point3d
    & |" \' f. V  M6 l" b8 ?
  116.         pointLocation.X = base_pt(0)7 e# X" C6 V# H9 ~
  117.         pointLocation.Y = base_pt(1)
    8 c  _/ \# r* M7 z# D
  118.         pointLocation.Z = base_pt(2)0 P+ W7 |2 A- {: v. X
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    ( B4 l+ p) {1 }2 k
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)0 _% I' B* s+ }! j: z

  121. 8 O% p$ s% J) h) C% r$ ^
  122.         Return response& n$ h+ {2 x2 |* n: E- z  Z

  123. 2 R$ p  E& F" ?' O/ m2 f
  124.     End Function
    & p+ J1 u  F* _2 U! H) e

  125. 4 f" q1 n3 X- _: I
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    , N& k; k7 g# A
  127. 8 r; M& E1 y$ N: S& ~: _" }
  128.         Dim selObj As TaggedObject
    # [9 }) b5 y  Y6 c) z4 P
  129.         Dim theUI As UI = UI.GetUI% @- e+ j& W7 e
  130.         Dim title As String = "Select a Point"# {3 i; }  @  D3 X7 N7 i
  131.         Dim includeFeatures As Boolean = False
    9 u3 V& ~" b: n+ m$ o4 o
  132.         Dim keepHighlighted As Boolean = False
    2 X" a" l0 W+ F" |5 S9 z* f
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    7 E- \8 l5 w" s% ^  T
  134.         Dim cursor As Point3d
    3 `4 k, i7 `& ~9 ]% u7 ^7 _
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly0 ?; K! k5 k* i" s; s3 }: G/ J
  136.         Dim selectionMask_array(0) As Selection.MaskTriple: A9 U& I6 w: A# P+ }

  137. ! w6 F+ J& {  c1 n
  138.         With selectionMask_array(0)
    # K* _! `2 O# H# w7 B
  139.             .Type = UFConstants.UF_point_type
    6 Q# P% S, b: f# N2 Q3 d6 ?
  140.             .Subtype = UFConstants.UF_all_subtype
    8 j: ]) {8 ~2 _3 r0 ]$ ]
  141.         End With
    ! Q& ]; f) ~3 n  @" n$ s  C
  142. 8 W+ Y0 O% j8 O" L4 H
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _& P4 B& x* P# v# C
  144.          title, scope, selAction, _
    $ X& i9 F0 l9 W+ V9 Y% ^1 ]" ?$ |
  145.          includeFeatures, keepHighlighted, selectionMask_array, _3 q( {. D) B& o  F% c
  146.          selobj, cursor)
    % O) X0 Y7 E% u+ E
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    6 ^9 p8 J# A+ @; {8 q7 m% l+ e/ O
  148.             selPoint = selObj7 c( v0 A. F! `+ k# x
  149.             Return Selection.Response.Ok
    % c0 T! G) H- k& z, ^5 D* B. V) v
  150.         Else
    0 q( L! z3 b8 a9 _
  151.             Return Selection.Response.Cancel8 l6 s$ r9 T6 y6 q
  152.         End If& x7 J$ F7 z- Y% T( y5 p  T
  153. ; y7 s7 R) _1 z1 j# I
  154.     End Function0 x6 i" z: a% E9 [3 R( y9 B' u% N
  155. 1 K5 y; {$ O" K4 X  [" R5 k/ }
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    8 ?4 n5 x4 b" H7 a9 F3 m; d
  157. / \1 ~- n% U7 K, V% [" y
  158.         Dim result As Double
    + J9 B$ }0 u  E5 i0 j. D

  159. 4 C6 C, |. R& k- p7 Z" y
  160.         Try
    % g% L, _& |7 s* G1 T% Y( U
  161.             Dim nullNXObject As NXObject = Nothing3 k8 a, z$ l! H

  162. : f5 r6 i& V. j
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder4 a& ?3 M5 B# x2 e/ b  v
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)6 S( k8 }( K3 M  C

  165. ; A3 W1 B  Z* S3 X, ?
  166.             measureDistanceBuilder1.InfoWindow = False
    - ~3 g/ O: R6 H* p5 |) Y. B6 T0 `- ]( F! j

  167. 2 o/ S0 L5 g; f; V
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum- P- r7 K8 p0 x$ d' w

  169. / A- R9 e* b- q( R
  170.             Dim nullUnit As Unit = Nothing
    & D# q# L# p& M* `" [6 k
  171. 2 g- X9 l5 P" Q
  172.             Dim measureDistance1 As MeasureDistance7 j+ \  `, \: u! g& z8 k
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    % n1 m' E, E% X2 M
  174. 1 _3 n0 `6 h" j9 ]0 N8 ?6 f
  175.             result = measureDistance1.Value- v2 j' e- g9 q: r( k3 P  Q

  176. ( J! Q& z, l/ W0 p3 o
  177.             'measureDistance1.Information()2 r8 d1 U3 P$ D4 ^8 z1 k" a
  178. ! I' O$ {- |, {0 f8 {; ?+ @
  179.             measureDistance1.Dispose()  Q& d; i$ W: e2 x5 z
  180. ) ?4 v7 }8 [9 C
  181.         Catch ex As NXException
    : ]8 j' n/ T; Y8 B" T
  182.             MsgBox(ex.Message)# u5 D7 ?7 W) Y+ F2 A
  183.             Return Nothing
    * q% k* b1 }. K3 g! _

  184. 7 U6 W4 D, A! l3 U3 o1 V  Z
  185.         End Try
    : p2 _. N. G1 ~' g* D

  186. 0 q+ n+ h: n' {3 ^  B/ h2 V+ F
  187. 8 |" F5 _8 r. p! c1 X+ R+ B
  188.         Return result
    3 _& i" t; [/ ]1 O
  189. 4 h+ s. g+ R2 {9 H$ f
  190.     End Function5 r  l& ^* v$ P* H

  191. , x7 t  C4 g* g

  192. ! Y; h$ p5 k9 ]1 O4 c) Z. x
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer& U9 y; i: H- _, N/ T( ^
  194. 9 R* `: D2 b7 d( O/ v& G5 v: I' P% X
  195.         'Unloads the image when the NX session terminates
    8 U& U# P6 i: `+ W
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    ! ~7 o: H$ f5 k4 _4 Q$ K: G+ [

  197. " ~8 C  u/ B6 ?. k# e
  198.     End Function0 c4 C, y% O4 e7 [; u9 N! Y
  199. % k5 G7 \! `- F8 K1 y1 S  @
  200. End Module2 |9 ^. A1 S1 E% j% n
  201. </P>
复制代码

: F/ h8 L1 ?7 b/ k' |
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了