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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
2 _% b, K! S6 T* _
NX二次开发源码: 装配中选点,并报告父对象5 A3 g  E6 e1 T- x  O

  1. 8 P% [. C9 B& T" u; h. z0 \7 p" r
  2. <P>Option Strict Off5 M. k* h' D1 T, p6 a
  3. Imports System! N6 T, d" W, U" g
  4. Imports NXOpen# D! z& b8 z% B
  5. Imports NXOpen.UF9 w2 O7 t" E6 T2 ]8 ^, t
  6. 2 p: T6 z  ]5 I4 o
  7. Module Module12 E( S/ f/ z1 c6 M2 O! q
  8. / [- V: O* ?) C& s
  9.     Dim theSession As Session = Session.GetSession(), a; h; l$ L0 m# h1 \% I" r
  10.     Dim theUI As UI = UI.GetUI()0 |$ s" G7 i# _" e
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    ' B6 s  Y& w1 ~' w0 D' V6 {' b, ^
  12.     Dim lw As ListingWindow = theSession.ListingWindow* l; w5 B4 L6 s, C* e! U9 G
  13.     Dim pickedPoint As Point
    * D6 s2 ?' y' U' p, w8 f
  14.     Dim myModelingTolerance As Double# v2 c0 ?, H; f6 k! s
  15. : b% n, A! u3 n5 }& h9 v
  16.     Sub Main()# D8 T( s8 v4 o

  17. % R5 q/ @8 t* p$ y  {* B
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)8 ~  x0 N9 ]0 x! o6 V
  19. + n$ w- p$ ]; \, u. B9 D& ?
  20.         Dim workPart As Part = theSession.Parts.Work
    1 Y/ U) V/ t2 B; x8 e
  21.         lw.Open()
    1 o, Z* h4 V! V6 u- O
  22. $ c+ u, u6 K6 V+ D2 q4 N7 f
  23.         Dim myPointTag As Tag$ a' c8 r! T6 U+ F1 ~
  24. 5 {1 s" i" w1 q1 v/ v) X
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    ( [" p1 ~& q& i  ]* H" t9 i
  26.             Exit Sub* X1 W+ ~+ t, h; z' w3 H) ?
  27.         End If
    ! V; h: c" r: l& h

  28. " }$ \  N- e; C. u
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
    7 Z9 k6 w! Y9 P, C' K
  30. 3 ]& o: Y! J: i
  31.         AskParents(myPointTag)
    2 V7 n- D2 o4 R% G- l

  32. % G) a2 Z* W* K& }
  33.     End Sub
    ; O, t- Q( R# ]! c. G/ Z0 n9 {" k

  34. 2 c; S) |# l% ]+ o) K
  35.     Sub AskParents(ByVal objTag As Tag)9 T4 f; l5 H0 H: q
  36. : M# Q2 U+ n6 V. q8 b( k
  37.         Dim n_parents As Integer5 G6 W2 x1 s& v0 L7 }3 S6 r
  38.         Dim parentTags As Tag()* ]0 f; x& P5 L7 N& d: _5 P- m% w( V
  39.         Dim myPoint As Point
    4 P& P4 N+ T; l2 ^# ^
  40.         Dim myEdge As Edge
    2 P7 `* X! n) p4 y4 {
  41.         Dim myXform As Xform
    6 m5 t0 z0 |) a4 K$ r4 I
  42.         Dim myTaggedObject As TaggedObject5 w+ \. h6 {2 p1 D$ p' D6 ?
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)% |9 Y- b" E  I/ d0 m4 |! Z) d" p. |
  44. : Z  Z2 H2 J7 i: c+ I9 Z
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    ! S  G1 S; @/ S
  46.             myPoint = myTaggedObject( |* e2 ~. F* @* s% y7 Q6 C
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    % |  \# F; Q! [$ \  D  J
  48.             lw.WriteLine("")
    # B' u* V& y) A2 y& M
  49. 1 h6 D9 e& N0 V) B5 [1 |
  50.             Try
      A+ k$ g- c9 h7 B9 D
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)9 m" h/ r# Y7 S  b2 K4 P
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    5 k  m' k* h# w0 Y9 Z+ O
  53.                 For Each parentTag As Tag In parentTags
    ' ^& w8 a1 o  e3 E4 l& o6 X
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag). I  x# ?, b( N
  55. & t' G! q; O& @, d$ z* f: w
  56.                     AskParents(parent_object.Tag)8 E" j6 |& J3 d8 Q' ]* v! G
  57. $ z* @; f5 p" A$ f. K: Q, m. W1 A& l3 M
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    8 r5 w6 t2 L! V$ `$ E
  59.                         myEdge = parent_object% e3 ^4 ^& k  \, M& i/ x2 p! _
  60.                         lw.WriteLine("")
    # n$ y, `; Y! @
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString); \' E( r) q2 _" ?
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    + c/ `4 U) f( v
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
    " V2 s/ ~- g  X* Q5 E. A
  64. 1 v+ m2 M2 d! R  c
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    4 n3 ~" m# I2 G( a
  66.                         Dim occTags() As Tag3 v$ q. Q0 f" q' F% y) q
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)9 ?/ ~& a  z! T; T4 o2 ~* x
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)8 b8 r8 H  l, a" ?" N1 H5 F8 x, N
  69.                         For Each temp As Tag In occTags
    6 |: `: j& h5 G8 c; |
  70.                             Dim myComp As Assemblies.Component
      o( y3 M0 j9 W
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    $ d$ I& P' k6 o) R
  72.                             Dim myCompPos As Point3d
    9 k( ?& G$ z: t# u, }. ^/ J
  73.                             Dim myCompOrientation As Matrix3x3( K, H  L0 R0 S# O3 b: n
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)
    % a' V3 a3 ]1 ~1 I" C9 q
  75. - k* |9 t1 z3 Y9 W3 Y& [/ l6 r" H- n
  76.                             lw.WriteLine("")
    5 k; g: O5 y5 t* x
  77.                             lw.WriteLine("component name: " & myComp.Name)+ _0 E$ B7 \* E% f1 A+ G
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    ( r! Z( s5 C# P4 I
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    9 H7 Y+ d! y5 d$ ~
  80. % d0 }, _" v, \1 I  i3 t
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then/ F1 b2 q9 u8 l7 U" B5 ?7 s, f; f0 `
  82.                                 lw.WriteLine("** This is the component that was picked **")
    4 ?3 p; G1 p( \* F
  83.                             End If
    9 p- @9 v# ?* r: L/ y
  84. % m7 V5 X* ~; W# s" |9 q, H* x
  85.                             lw.WriteLine("")
    9 l' G7 U( W# A# w
  86.                         Next
    ( U# E3 Y" i4 ^! `8 ]4 ]$ s) f
  87. ; ]/ w: T  F5 N4 i# r
  88.                         lw.WriteLine("")
    : M( h" r  [8 H; O
  89.                     End If
    7 F+ P% y$ x% Q' N, B+ G. N
  90.                 Next+ s+ T0 Y% r! ?5 l- L

  91. ; X7 D- e  ?9 C6 b8 M
  92.             CaTCh ex As Exception
    % B5 k! v4 Q. b: s9 N
  93.                 ' NXOpen.NXException: Current object is not smart
    # N4 T9 z' {  F! b7 @' w
  94.                 lw.WriteLine(" Error: " + ex.Message)
    3 q3 F) `! v2 m0 C6 V/ r) T
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)  _' N! G# ~! ~  N  E( z
  96.             End Try
    # E! \/ ]4 p! M' {2 Z
  97. . e! P, |  L  v  S2 ^4 j* c. V

  98. 4 h" b( g. f* K  b4 e/ _, ?
  99.         End If
    ) J- |% e# d5 [- h' w# L; U- c( f4 Y

  100. # S" Y6 A+ Q/ ~# w5 }2 r

  101. 5 T# z" ?( Y( f, C3 b( c
  102.     End Sub
    . ], G4 l  z4 d: S4 b) w  B: }
  103. 4 m; [9 s6 C  C1 I. U
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
    2 r# |% S! G8 c0 {7 E

  105. $ T' G! a! X  ]
  106.         Dim base_pt As Double() = New Double(2) {}1 s' r! q) K  \, M3 l6 K& W
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    ! u4 V  {. T9 y' z
  108.         Dim response As Integer = 0- w8 O# [. T7 N1 Z1 k& o. L( s
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt6 }9 C3 F0 d3 i
  110. 0 e) H7 Z  M- i6 b: W' C! o& n/ j
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    2 l; x. I+ _  j
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    ; N" v" t; H: q
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    7 |/ H; W, v! r8 @8 V/ o4 ?% M
  114. 3 v8 a8 l/ O) k- B
  115.         Dim pointLocation As Point3d6 C; f3 Y# h) N' z
  116.         pointLocation.X = base_pt(0)
    0 I: k& F5 G$ R8 ~
  117.         pointLocation.Y = base_pt(1)) B+ K. b) C- N) k' s/ M/ A! R
  118.         pointLocation.Z = base_pt(2)
    9 h8 K1 i5 O, G7 U- v+ j7 c7 K
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    1 W" M$ k+ t; |0 p' l/ J7 Q
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)' Y0 P6 [+ `( ]9 S( Y9 ]8 D+ _

  121. % O7 S" ?7 x0 p8 b# Y
  122.         Return response
    * F, P$ z7 i3 T1 K; o, p& ^1 C
  123. * b$ k2 C2 @7 m) i% P# \% e3 X
  124.     End Function1 _. t( ^3 _( u: x: }: N- r* z0 g! [9 L

  125. 4 n9 r' Y) X5 H
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    / B% ]) V5 a6 g6 J# N

  127. 4 y# L0 i2 c. [6 {0 B* T3 g! c
  128.         Dim selObj As TaggedObject
    ( ]6 R/ R$ W+ \# h
  129.         Dim theUI As UI = UI.GetUI5 E$ x: O8 c/ L) d. T  }
  130.         Dim title As String = "Select a Point"
    - l" b0 }" Q% I' j) Z, `
  131.         Dim includeFeatures As Boolean = False
    2 R1 l! k% w3 Y' w% I$ O
  132.         Dim keepHighlighted As Boolean = False
    ! S# c- `% Z# v/ ~! {1 {
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    ' b4 e2 [. M6 O! \
  134.         Dim cursor As Point3d/ w' E) R9 P' w
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    1 w' ~6 c8 ^8 t* m+ Q
  136.         Dim selectionMask_array(0) As Selection.MaskTriple8 e7 z/ \, k& X! V; o# C( j2 ^

  137. % ~) {- y- Z' H% T8 r( F  `
  138.         With selectionMask_array(0)
    : ^- `3 X. u/ p6 Y& S3 S
  139.             .Type = UFConstants.UF_point_type
    ; |$ D3 d9 G3 N+ f
  140.             .Subtype = UFConstants.UF_all_subtype
    4 Z0 ]7 x% N& m6 O+ a7 o
  141.         End With% l1 d* m0 l4 g4 n5 Q, |

  142. 5 n: w$ a/ A/ X* {. B$ N0 l* M
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _9 B: w1 a" _+ b4 q0 S) \
  144.          title, scope, selAction, _9 a; ^  i1 `# u: B- Z0 ]
  145.          includeFeatures, keepHighlighted, selectionMask_array, _& M1 M$ ^" G1 Z6 G  q; x
  146.          selobj, cursor)8 F4 o" {2 M% k, |( \
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    : m" A  p$ g) B- H8 b9 ?
  148.             selPoint = selObj
    ! ~9 h" q! |6 H; y# A! Y" i: ]# H, x
  149.             Return Selection.Response.Ok
    2 E) [+ [! J- C! q$ x; ]
  150.         Else2 ~+ d/ q# U( S/ \* T
  151.             Return Selection.Response.Cancel6 E5 _/ G" k' S1 N: g9 m8 n3 H
  152.         End If
    " u* }+ U: U+ S+ \6 D: R- b
  153. ( g, a4 ^! u7 S
  154.     End Function
    8 s, R/ [# v1 G6 e* H9 E* |

  155. 9 k' T8 M4 }- h% F6 g2 e; H
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
    ' V- r2 ~; |* X; I

  157. $ H$ I$ }- G  i3 W
  158.         Dim result As Double, J: }( Y1 u) j1 s, w
  159. : {5 O8 ~! C3 @7 D/ S; }4 ^
  160.         Try) h- T/ Y9 G, c2 F3 |
  161.             Dim nullNXObject As NXObject = Nothing" t2 l4 B$ r1 Y" e
  162.   S$ z) G/ F+ u
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder" r5 W) D, l) {8 ]- R! g( f' q
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)4 n$ I: g+ |3 F; j# l. e; m4 j
  165. 0 N, v" C' L4 W$ z- P
  166.             measureDistanceBuilder1.InfoWindow = False5 {8 _+ D2 [1 N  w' A
  167. * o* U8 s3 ^& ?, c
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    / N; s2 N( ]/ W2 f: d
  169. 1 q) ?$ g' p" |6 ]1 ~. |- V
  170.             Dim nullUnit As Unit = Nothing2 J3 A0 {8 p  f9 r8 `

  171. * D& ~: ~' [* \5 _
  172.             Dim measureDistance1 As MeasureDistance
    7 w$ x$ o$ F& T# }6 x
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    ' Y% I+ N) k* q3 X

  174. : {, l% [; o8 y$ b7 L
  175.             result = measureDistance1.Value
    ) y! O, g0 J$ X3 o6 n, c

  176. % V+ ~& \# \0 c  E# @0 A/ S: n
  177.             'measureDistance1.Information()
    2 r1 P% R- [  r9 M
  178. 4 s! z4 m) m  H& w. V- g
  179.             measureDistance1.Dispose()) n' K1 G! [2 ~0 R% v6 W/ Q

  180. + x6 ~5 K, o, y, x
  181.         Catch ex As NXException; {  z  X4 b/ M8 i1 X+ y2 ]% F6 ~6 `* \
  182.             MsgBox(ex.Message)
    4 h3 V& F+ n. F. k5 N; z" w3 H& O
  183.             Return Nothing
    * _8 ?3 h& A5 a/ [3 r

  184. 0 r' R4 N, R7 U; H2 h
  185.         End Try3 b5 j4 ~, m4 m  `6 e
  186. 5 A3 e$ R% C$ v2 t! ^' b1 \; d
  187. 3 ]7 r* d! T2 v1 K: f/ z
  188.         Return result
    3 A5 N$ i+ i- q1 ^' M; m* Q

  189. , G# E5 P: [% \# g: ?  U# S9 W6 j2 c
  190.     End Function9 V$ E% \$ W0 `0 b( P3 Q2 o

  191. 7 o1 ?; L( ]; L! p0 `' U2 Z

  192. ) c- S- f9 m+ r- z; w" e
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer2 n$ t1 V) j0 c* u

  194. , @2 D" {8 w: x. A
  195.         'Unloads the image when the NX session terminates
    , D5 e6 M3 P2 c. S  r
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination5 H$ k# b- ^# ]
  197. . p5 |. n) Q: q+ B1 R# T
  198.     End Function: f. I$ \( p0 h  `( ^; R4 }

  199. ) v  G: W) B5 q/ f% I4 U4 u9 y
  200. End Module
    ; }1 u! G/ }4 ~/ G/ p+ q
  201. </P>
复制代码
0 A* L! S* p, _+ ], y
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了