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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
5 k, c, h5 O$ o
NX二次开发源码: 装配中选点,并报告父对象
$ I9 ^4 U7 C( v2 B' o
  1. 1 g6 X2 J7 p& \* J9 J
  2. <P>Option Strict Off
    0 L. C# ?- q; H4 A
  3. Imports System1 S9 C3 K9 B( K# ?$ R9 q( O
  4. Imports NXOpen
    - S$ n0 x& S1 u# _4 L2 o* [' E/ x+ [! ]
  5. Imports NXOpen.UF; ]% A# d9 l& b
  6. 0 }3 V$ J) d0 _6 m/ r6 z3 x  Y# Q
  7. Module Module1+ {6 L& ^" [/ S6 L2 q2 D2 v
  8. 6 P$ J) I/ {: @/ \5 }  d9 R  j
  9.     Dim theSession As Session = Session.GetSession()7 R  W( @4 k; c4 ]* w4 y( f
  10.     Dim theUI As UI = UI.GetUI()  C' I. k' H3 r, K# C5 [
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()
    ) S2 q  B* k! R' R. z9 K8 N' v) [0 o( u* l
  12.     Dim lw As ListingWindow = theSession.ListingWindow; z6 o' @7 c. L/ g  T1 e4 j$ X+ g
  13.     Dim pickedPoint As Point
    , z9 ^% y3 w6 [& h9 `
  14.     Dim myModelingTolerance As Double
    ' |  a' m0 Y% f7 ?) Q
  15. & [+ [+ E8 k/ U2 u) M
  16.     Sub Main()
    6 V) E8 G( `& f

  17. ; Z1 X0 r9 m$ q- j: K  ?: z3 a( S
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
    ; V$ ?/ k8 S7 \- D2 f

  19. ) j# z- S1 [4 x) T3 C* q
  20.         Dim workPart As Part = theSession.Parts.Work1 P$ P* p3 g- i* Z
  21.         lw.Open()
    % s* Z* @( E, K$ E, `9 E

  22. * d8 }2 M. T. s+ g3 e' ]
  23.         Dim myPointTag As Tag5 M" Y! n" {! Z

  24. ' x" z) Z3 T$ h, y: G
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
    ! S( ~9 ]$ p+ d, d, ~( F
  26.             Exit Sub9 W6 b5 p, i+ c$ B
  27.         End If: T* h, E: s7 \

  28. , }+ j; n- R; E: Y$ p2 ^
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)- W0 D, H* x, o7 D
  30. * l6 \8 a+ a" X3 E( p' {
  31.         AskParents(myPointTag)
    9 E. h" h: q5 b% [5 r# E) A

  32. # E* I# V$ X8 b( ?
  33.     End Sub& k. y2 D6 _& z: `& T. J
  34. 5 R: p) e2 y7 x4 Y  ]
  35.     Sub AskParents(ByVal objTag As Tag)
    0 H8 U7 \1 Y0 X
  36. + w9 @: b$ v9 e* ]% V3 d
  37.         Dim n_parents As Integer' r% i! c4 D: R- w" l
  38.         Dim parentTags As Tag()
    3 G  W7 Y4 w) `, j
  39.         Dim myPoint As Point8 j+ k3 S, [) k
  40.         Dim myEdge As Edge
    & l9 h& R, Z8 o. w, b9 u$ `
  41.         Dim myXform As Xform. c- {7 ^' i7 K5 i6 d+ a5 J
  42.         Dim myTaggedObject As TaggedObject
    0 @3 }3 }  Z% j: K
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)5 e; _, N7 u. F) K* @4 N9 K

  44. : }2 K& X# a( ?6 w# n
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    2 y+ d+ n2 H- Q- T7 H% y" z
  46.             myPoint = myTaggedObject: _* x( F2 n0 x9 J# V
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    1 M  S" z, j3 u1 R0 ?! m: ?
  48.             lw.WriteLine(""): v1 W' T) U  ?
  49. 1 M1 H# S" z- [0 H
  50.             Try, K0 n2 D: c. e3 {
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    4 f- W- R. _, w( q% _5 L  u; S5 n' F% A
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    - [4 R% M0 c5 ?( O5 {& f3 }
  53.                 For Each parentTag As Tag In parentTags& q5 W8 [: ?# J; ^) F2 }
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)4 Y  Q. ?3 {3 ]/ {, e

  55. 6 H$ I% R! y9 }- b* [! y
  56.                     AskParents(parent_object.Tag)+ r$ u, v) [7 ?7 H  u  i
  57. : V" W  x, m+ X2 v3 R
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then! X/ e9 u, b2 b* d
  59.                         myEdge = parent_object
    9 n7 D; O2 {% z4 _
  60.                         lw.WriteLine("")* [* V1 V2 y  X9 `. i& j0 w8 {
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)7 V1 S  O7 }5 B1 g" T* B, M
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    ( z/ s3 w8 G2 m) g5 \4 y
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)3 s* r* Q' n5 g5 L; T% I

  64. ! N' q' e4 k, X" R9 b
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag0 K/ g4 T- v2 b2 _+ a
  66.                         Dim occTags() As Tag) a4 L+ r( _  V7 M( V
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)$ l+ W" G3 P. W) U; q# n, b
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)
    + }2 t/ x' J4 o, }; @4 T6 r! ~
  69.                         For Each temp As Tag In occTags
    # C+ w/ D: Z3 `9 ~) {' E
  70.                             Dim myComp As Assemblies.Component
    ! w) m0 a8 @* D" c1 j
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    3 c2 k* ~$ V3 L$ q$ y
  72.                             Dim myCompPos As Point3d0 ~# q9 @4 V# T- S# E
  73.                             Dim myCompOrientation As Matrix3x3. |) B, ?6 `/ L0 \7 D' S# Q9 b4 c
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)
    % }( F5 ]' H# F3 S' h- f

  75. 8 \% `; G- U) W
  76.                             lw.WriteLine("")/ H& [: b. y1 M0 L
  77.                             lw.WriteLine("component name: " & myComp.Name)4 j/ g6 X5 Q1 \! I. Q  D9 P- l8 E; d  b
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)/ p+ o4 Q6 y0 V! f
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
    # t7 J2 ]4 D: P! {

  80. ; Z& w" w# B* ]1 W1 d. H/ q( M
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
    / B2 W' y; j7 a" F
  82.                                 lw.WriteLine("** This is the component that was picked **")* `7 ~7 ]; o" k9 _# O* R
  83.                             End If6 ~3 }4 m) N8 j* Z
  84. & s# W; a! A( N& F- s
  85.                             lw.WriteLine("")
      Y9 Z" @/ P* s' T: ?
  86.                         Next
    ' D- S: |% ^3 g3 Y* w4 d

  87.   d9 a, n3 H2 X# s
  88.                         lw.WriteLine("")
    2 s9 O+ _/ ^' }4 u
  89.                     End If
    8 ^% g4 A- i# j6 u: J6 K9 C1 N
  90.                 Next
    9 G5 T' l! y7 K9 L8 t

  91. - c* h" j& V0 `9 g
  92.             CaTCh ex As Exception
    8 m6 \, g: T) J2 k% g' v+ y
  93.                 ' NXOpen.NXException: Current object is not smart
    5 w( r8 ~& W3 X/ I
  94.                 lw.WriteLine(" Error: " + ex.Message)) `$ A7 N- w$ e+ e- ]* `  a8 ]; N
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    3 c& ?2 Q- \" Q) ~7 c9 V+ }
  96.             End Try2 e/ @6 c& v, f0 P  l7 M2 `3 C7 f# L
  97.   R$ w# J) e" q; A
  98. # \4 Q% x7 V$ q3 [/ P1 p
  99.         End If. B9 ^5 J6 A3 P' G4 y

  100. 4 M  o( G, t# T' [
  101. ; h) ~1 o( J' a, t+ G
  102.     End Sub
    3 S( I% \, x& N$ F, q, q! a1 A/ J
  103. " b) j$ r* i/ t8 m0 P! h
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer( {% u& ]4 b! @2 ~' ]7 t0 l
  105.   ~* V  f3 \6 U) ^, r4 c( P/ }& R
  106.         Dim base_pt As Double() = New Double(2) {}  N$ z- [- ^9 Y  p& F
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    ( L) m& d. ?7 b' P
  108.         Dim response As Integer = 07 W8 N. F& g/ }4 D# R( j
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt. g* N# @8 j/ y7 j& H' v, i! B3 e

  110. 4 i# V8 K& U! e0 }# B
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    . k- B, m0 k' a: I4 s
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    4 N5 q9 e& }, }' M' G: X
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)+ W) n7 K" s3 y9 V2 M, b8 i* J
  114. 1 H8 W; ^2 A6 H& g8 q! t
  115.         Dim pointLocation As Point3d5 G2 E7 {3 f! s) V3 a, N4 T; ^! K1 o" M
  116.         pointLocation.X = base_pt(0)' G' O1 R) W9 C2 G
  117.         pointLocation.Y = base_pt(1)$ M5 X; ?; i1 @+ f4 g
  118.         pointLocation.Z = base_pt(2)8 o8 e3 r9 Q3 O# a$ q
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    ( _+ V$ J4 w- w& J9 f
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    8 h9 {0 s- A& {

  121. ' E8 d6 ~- D1 B9 L6 C4 C1 K( ]' T! O
  122.         Return response! K+ n+ z+ g7 U# V
  123. 2 r. _6 R; j4 X! P$ G, M; q! d$ I
  124.     End Function- E4 Z9 |, m) ~- z6 e

  125. 8 `- {3 B' }! L3 t5 p
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response9 X. d* ^& |/ k' [
  127. : T  S7 j9 \4 Y- L! y0 o
  128.         Dim selObj As TaggedObject
    0 ]+ W  Y' {$ r* a7 X
  129.         Dim theUI As UI = UI.GetUI5 Y/ `% D  Y& i: Q# |
  130.         Dim title As String = "Select a Point"
    . Y3 @. C: v6 U1 c+ Y/ q
  131.         Dim includeFeatures As Boolean = False
    ; l" \0 k( |' R% j% @
  132.         Dim keepHighlighted As Boolean = False
    ( W0 ]& P1 Z/ T/ `. Q8 X! d! J& \' w
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific6 R* e5 D- K* _: F! J3 G) |7 i" z& B
  134.         Dim cursor As Point3d
    3 s5 D2 v% s0 r  \3 Y3 _# N
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    ! R* Q. C3 G$ r1 j5 v
  136.         Dim selectionMask_array(0) As Selection.MaskTriple
    * R, I& h$ ^7 ^' V5 a" m4 d- A
  137. - b% J6 ]: ^8 j' a* O
  138.         With selectionMask_array(0). ?0 S* p! o, J6 m( L- ]* o9 v
  139.             .Type = UFConstants.UF_point_type3 o. s  H% [. [4 K8 f( S6 \
  140.             .Subtype = UFConstants.UF_all_subtype# _& ^9 P" e! y# u5 l
  141.         End With+ p& [- y! z& W+ x2 l* A7 _
  142.   ]7 l% A2 N# R2 @/ n6 s
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    7 w, S4 l- V' C; s8 t
  144.          title, scope, selAction, _7 f+ @: Q+ z1 t2 m& _
  145.          includeFeatures, keepHighlighted, selectionMask_array, _) V, I3 K* o! g1 S$ M% }* D% j
  146.          selobj, cursor)
    ) A5 X  F* p1 B- Y% \
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then" K; B0 B) j" k  a  Q# D
  148.             selPoint = selObj
    + Y! r/ Q& d+ {
  149.             Return Selection.Response.Ok# W& K: K* d( _2 I7 ?
  150.         Else6 ?4 `$ w, g/ y- b
  151.             Return Selection.Response.Cancel' A: q6 @0 G8 F; M
  152.         End If
    - m! n/ o: V0 [) U$ e9 d; n6 J

  153. 8 m$ {7 n- g) ]5 a
  154.     End Function
    6 `7 i8 i: \1 b1 K5 _6 E
  155. ) }' Q+ V5 v" D# Q, x
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double" [7 i( Z  t- X& s& y7 c# x6 r

  157. 0 b( B0 o6 R* g0 G* u9 M
  158.         Dim result As Double7 D( o. \5 B" I5 `) ~

  159. % d3 F/ x% i* I  A. M& t' o
  160.         Try) G" U" G) m% l/ A
  161.             Dim nullNXObject As NXObject = Nothing
    ! D6 q1 j- X4 N! j' Z
  162. ( B9 C% X8 z; ~2 X
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    4 K& }/ k; A- c
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject); d8 b# v4 d  L: V

  165. 9 a: s6 W* }9 p& D* p
  166.             measureDistanceBuilder1.InfoWindow = False/ C8 E, M/ m5 G
  167. ; q+ s9 }4 M8 r
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum7 Y& Z( \; ?: P$ z6 ~

  169. ' L, Z4 S* m1 ~. g8 V( d
  170.             Dim nullUnit As Unit = Nothing" N9 }1 y  y% S# U3 }1 ?( S

  171. ( G7 G( D& G, _) r: W6 I
  172.             Dim measureDistance1 As MeasureDistance5 O6 \0 [% f3 J: P2 J
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)" F! i( I2 t$ `) O4 c1 m

  174. % l/ p: h: A7 q( A* K  S
  175.             result = measureDistance1.Value. U  p6 ~5 G; @# P4 L

  176. + b) e7 R4 Q9 W6 T
  177.             'measureDistance1.Information(), C- x* c; w0 y9 I/ ]+ m( i! R; r7 c
  178. 0 f5 L5 s+ E/ H+ ^% ~; ]; H
  179.             measureDistance1.Dispose()! f: v! D) h+ _

  180. . W" D$ x- m; X  q
  181.         Catch ex As NXException
    & @' y+ o3 D3 F$ x
  182.             MsgBox(ex.Message)
    ; Z9 n8 U/ `* T* `9 s
  183.             Return Nothing
      }1 a. d- Q1 J# h

  184. . y  ?$ ~2 s  v
  185.         End Try* @8 d7 y* e1 W3 o

  186. - E) q" X3 n! k

  187. + h) U- L) d; N
  188.         Return result
    0 G7 r6 r7 N+ g/ v* Q' X6 f) w
  189. 9 F) O0 k  R% U4 r  {4 M
  190.     End Function
      R4 k, A0 B$ Z. h

  191. - L$ J, D0 d" R6 ?7 z; h# V
  192. / X5 z8 I* q( A1 W" `0 i7 I& K
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
    $ c$ ]+ {& _9 B9 j. m+ Y, J

  194. 5 Y& q. a! C; p7 E0 W. y6 f2 c
  195.         'Unloads the image when the NX session terminates- Y# U4 ?* \( L4 k+ x8 G- {
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination1 U9 d" m& z) Q) v+ |) y" T  ]4 K6 ^9 y
  197. : F9 v$ v$ t8 l2 S
  198.     End Function
    & D6 s1 p" m5 U2 p9 w
  199. % V: W+ c! n/ e+ ~: ]2 }
  200. End Module
    7 |3 w1 w: m- i! P3 R; A
  201. </P>
复制代码

6 Z, M6 n' \5 s& V$ 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二次开发专题模块培训报名开始啦

    我知道了