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

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

[复制链接]

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

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

admin 楼主

2013-11-5 17:34:33

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

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

x
" M2 J+ S1 m+ J9 ?
NX二次开发源码: 装配中选点,并报告父对象! }% `4 V5 }5 I$ ]4 A6 R/ e
  1. 6 U% Z4 {% v" u$ z2 o( q* A) L* G
  2. <P>Option Strict Off
    ( I! _: u& q: {8 h8 _( e+ T
  3. Imports System
    7 |- ~. J; s% ^$ _
  4. Imports NXOpen. {7 y: o1 z/ d) j
  5. Imports NXOpen.UF
    1 E. [/ Y2 c0 v6 x. W

  6. 3 X  v' u2 Y% G3 ]3 v
  7. Module Module1
    " S; h; B+ g* q7 d  r6 f
  8. 4 T9 k+ s. g" _
  9.     Dim theSession As Session = Session.GetSession()# Z& |7 j5 W; b0 }' W* I8 {/ q! Y# s
  10.     Dim theUI As UI = UI.GetUI()0 h8 J2 z! r4 E6 i4 H! t1 x0 Q5 o/ \
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()% V. Q- g* k! O2 i5 T4 _$ t! S
  12.     Dim lw As ListingWindow = theSession.ListingWindow  ~% N/ u4 X- `+ d  h8 A3 B; z; Y! u
  13.     Dim pickedPoint As Point
    + t# \- }, P" |4 A1 [% J
  14.     Dim myModelingTolerance As Double  S" b3 R7 o2 o( s3 J; M6 J

  15. $ K5 X1 T. e7 z; s
  16.     Sub Main()2 u! }& X) j7 x1 x+ j
  17. 1 f- R/ [, J6 r7 d) Z6 C
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)) }+ f, _. h, r4 A9 y6 S

  19. " \5 q6 {9 L; r, j
  20.         Dim workPart As Part = theSession.Parts.Work
    ; _1 W1 i' t% t& B
  21.         lw.Open()3 R) ~+ v- X; ]9 I( T* W* X  C6 y- d
  22. 4 o* C! Y" C/ J8 t) @/ w2 @9 k' O
  23.         Dim myPointTag As Tag
    & W" n$ W) g% J; |  s
  24. 9 {5 F3 x8 H3 m3 k3 z. [( [. ~( t
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then: v# Q" D4 D: \5 H
  26.             Exit Sub
    5 C! E6 ~7 O4 A) r: v1 {
  27.         End If
    7 u  e) r. z; ?
  28. : i1 T) Z/ e6 f2 u
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)! N9 Y( ~  v6 [' p

  30. 2 ^% p: X2 U. L- F$ }- ~8 ^
  31.         AskParents(myPointTag), W" O2 m  U- w; r

  32. $ |3 x% K1 S' {; q
  33.     End Sub! @5 d) t8 o( H  E

  34. + p6 S0 M' g! T
  35.     Sub AskParents(ByVal objTag As Tag)
    * D& T0 t2 ]  z$ p7 ^$ e

  36.   T  k& V* z5 K1 f
  37.         Dim n_parents As Integer
    ! S8 {: e( B4 O# u
  38.         Dim parentTags As Tag()
    / }6 G1 u5 r) l3 G
  39.         Dim myPoint As Point6 y, ^' U% {* s" H4 W
  40.         Dim myEdge As Edge3 N" C8 ~  c- p8 T1 W4 u7 K7 r9 K
  41.         Dim myXform As Xform/ O3 ~3 r4 x& q: D+ i
  42.         Dim myTaggedObject As TaggedObject
    3 l) S; Z4 u& H
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    + N& u8 ?$ X( ?" q

  44. 0 `) o: G& c# v  x4 T5 U
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
    6 y: \/ B1 L" e% t+ O* x; J% h2 i
  46.             myPoint = myTaggedObject9 _/ A' F. h7 v" M! ^7 i
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
    . m2 }6 W. [1 b' i  @+ l
  48.             lw.WriteLine("")2 m& ?6 u$ N. D/ F& @' O0 i

  49. ! `# k: Z6 E) C
  50.             Try% J% s/ g; L- \: p  d
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
    - v8 {" l: O+ f+ d0 q8 b
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    " j  a1 F, M* q7 a' S( {: E
  53.                 For Each parentTag As Tag In parentTags8 T# t' R4 H. w1 K; e) H
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    : W* Z2 {$ }6 {9 _9 E* M
  55. 2 s8 @! ~  @9 k2 b' w" K
  56.                     AskParents(parent_object.Tag)1 b" U! c& k9 s" A& r5 V# c7 O
  57. " y7 \2 R6 C# w1 ]* F. u
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then( Y# Q" t- t+ |" M- `
  59.                         myEdge = parent_object
    & r- `! x1 t, g9 p
  60.                         lw.WriteLine("")2 j# Q. y  q1 D; i0 i
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    9 \2 t# q9 q/ k# o. d9 B& B
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    * b/ Y' Z9 T6 o$ Z# s- t# H/ A
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)4 k: r4 z8 q+ Z
  64. 2 e( @% s" M2 x# u$ I
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag7 C; H" |7 w+ y' G  R0 Y* K" Q; K
  66.                         Dim occTags() As Tag
    , m1 m6 @& a1 r1 o4 q
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)5 v2 Y+ w2 p' n" s8 Q
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)& O5 I' V' o2 h8 g6 S* d
  69.                         For Each temp As Tag In occTags, I3 U$ ]: A& l# G. K4 [
  70.                             Dim myComp As Assemblies.Component! n# ~9 c, G# P' Y$ V3 t
  71.                             myComp = Utilities.NXObjectManager.Get(temp)
    1 O0 V1 p( K& k# `: i7 C
  72.                             Dim myCompPos As Point3d
    : p+ P8 J$ f5 o; M" r
  73.                             Dim myCompOrientation As Matrix3x33 Q: {) q9 e0 }! \$ J) B4 f/ R& G
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)& x1 m5 {& Q' o/ U0 P! |/ G- r# B' j
  75. 0 l* d9 j5 g0 `. L$ w: d
  76.                             lw.WriteLine("")
    3 h0 W: W) d+ @& |3 c7 f
  77.                             lw.WriteLine("component name: " & myComp.Name)2 \& Z: t1 q( b  \
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName)
    / a8 U1 ^; k  K! h
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)1 k0 d/ ^, _8 i3 |1 J4 s: a

  80.   |1 Z+ V! g; i+ K9 Y
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then! g+ s5 ^! p3 x& A' k4 Q% @
  82.                                 lw.WriteLine("** This is the component that was picked **")
    * j3 {5 H4 n+ N% I
  83.                             End If2 N. F4 s* C, U

  84.   E: n, j! R# {( y
  85.                             lw.WriteLine("")) m' J+ a8 X) {) E, r
  86.                         Next
    2 E) G2 Q- O( i) ]. H
  87. 3 ~) e. O1 h; H3 [
  88.                         lw.WriteLine("")
    * p0 E% u6 h) |6 p$ J, b2 ]
  89.                     End If
    - h- p" `  n. B  P# @; j
  90.                 Next, E& }. y7 p5 r7 G9 Z3 v+ [; i
  91. . ~  Y- f/ x; C
  92.             CaTCh ex As Exception
    ! X- g+ L' R. y9 l% J6 p0 L' d) G: y
  93.                 ' NXOpen.NXException: Current object is not smart8 G. h7 I( Q3 d- a3 m
  94.                 lw.WriteLine(" Error: " + ex.Message)+ |' J) h& h1 y) e
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)$ \3 }) b4 Q5 |' W4 h
  96.             End Try7 D3 n7 p0 @+ p3 [2 R" ?3 Q
  97. / K$ j& l( `/ i* ]; u

  98. 5 l) d+ @1 _& h) Y
  99.         End If
    8 v  g# o8 i" g& `; L& C  J
  100. 1 W8 K3 b& ^5 m' t8 {
  101. 9 ^: |( N3 b! ?6 Z) H2 p7 l  i
  102.     End Sub
    + }% }4 j4 s: |- Z- i
  103. 6 ]  ?+ q0 f" ?6 W6 {
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer0 T4 {( a) n, k& q1 Y0 Z
  105. & R+ J; G9 s4 R" l* O* M2 }
  106.         Dim base_pt As Double() = New Double(2) {}% ?! o# X  x, G/ u' N% o
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    . G5 U0 y( z4 a, Q2 _
  108.         Dim response As Integer = 0
    ' _- P1 O0 d+ w( \( f( B" Y& h
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt& J1 t- Q  v  P' N
  110. 8 p0 e* U0 p) |2 h8 @2 B
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    $ F: B  U% a: S2 f6 s
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response); T* `; f0 @" h
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
    % O: h: v0 g6 Y' N- ~
  114. ! E4 L5 S1 m- v0 Y! B" p9 u; P
  115.         Dim pointLocation As Point3d
    & m2 I) f$ t# C
  116.         pointLocation.X = base_pt(0)
    : n3 Q8 `$ d0 w+ _- L  S
  117.         pointLocation.Y = base_pt(1)# h2 d( Y2 {. ~" K& {' T3 M' b
  118.         pointLocation.Z = base_pt(2)1 C5 L8 ?0 A+ X8 m
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
    5 z& |3 G2 ]1 @
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible); S& @" b4 Z9 h, h
  121. ( w" m( u8 A  c% p0 i
  122.         Return response
    - v- X: {3 J0 I7 o# e) X" l

  123. ) c6 o* ?/ k' Q7 ~5 t
  124.     End Function5 i) s9 I2 ~$ u6 S/ C
  125. - r! @" g( _5 q. c$ ~% I& G8 W
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response5 K+ f7 v6 A; R1 K' U' G

  127. 2 v( V& L, @8 j) D6 k3 t& u2 V
  128.         Dim selObj As TaggedObject: o% z$ E; M2 q3 r, e* b
  129.         Dim theUI As UI = UI.GetUI
    + @0 J  S6 {" @( `- e/ ], \( A
  130.         Dim title As String = "Select a Point"
    1 q5 P' k$ z+ U; W" t' j: S- @# |- Y
  131.         Dim includeFeatures As Boolean = False5 `6 T  u* V2 @0 \6 i! {
  132.         Dim keepHighlighted As Boolean = False. r7 t1 G7 L, y8 H0 Z2 R! z
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific5 f. z1 q: o$ }
  134.         Dim cursor As Point3d7 e1 p' _0 \6 e' y# x+ f. A1 ^
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly6 u6 _8 s' S0 ~
  136.         Dim selectionMask_array(0) As Selection.MaskTriple& _* F/ [; k! j1 s" {; ~# m

  137. ! g% S8 C4 K2 M  S
  138.         With selectionMask_array(0)2 a  @" l& L  o2 I; n, b
  139.             .Type = UFConstants.UF_point_type
    9 u: x1 y9 u7 ]# U. n+ G! N6 [' g
  140.             .Subtype = UFConstants.UF_all_subtype
    + V8 \" `* ^& }6 o
  141.         End With. T6 Q9 ~. z7 U+ a/ |

  142. & _" y  T. M2 z, D
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
    1 q/ s4 Z& {( q0 f7 p: l( p
  144.          title, scope, selAction, _
    ! u4 o- ]1 R, l6 Q: @" i6 z% q
  145.          includeFeatures, keepHighlighted, selectionMask_array, _7 W  {5 C$ e' u! k
  146.          selobj, cursor)5 Q7 |1 l3 y9 z9 i+ j! q
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
    5 C/ z  h1 v& g
  148.             selPoint = selObj
    % J# O1 ~) Y7 T$ w6 y! Y; Y
  149.             Return Selection.Response.Ok. `+ X, S; M1 `  t2 X1 t/ X$ \' d8 L
  150.         Else, k8 b0 s/ m; h* ~
  151.             Return Selection.Response.Cancel
    . @! O; I' e+ A. Q/ e  K& s
  152.         End If
    ; |; Z  G: f( a$ L0 W1 ?2 {

  153. 0 a3 k- c' a. C# ]& i
  154.     End Function6 K  B' f- _- L( Q& P* t

  155. ) G  T/ R: X, h. Z" B! Q$ I
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double7 ^' d, L/ S1 |7 m
  157. . K# N2 R( t- k
  158.         Dim result As Double
    9 B$ a& S- ?. B4 }4 r. h$ g. {

  159. 4 V0 |+ X# H2 n+ {# s
  160.         Try, W/ ^) W; }0 X. G
  161.             Dim nullNXObject As NXObject = Nothing  u/ k5 p% s( D2 d' I
  162.   S# M6 O$ A2 J/ `  O
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder. Z5 H2 e# P6 j' k  r
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
    ; X2 H2 ^6 k: D" p" [" ?* h$ X
  165. 4 H$ h0 ?( S  I
  166.             measureDistanceBuilder1.InfoWindow = False
    ( O3 u% X4 ]* I* u' W: W

  167. ; Z* @* ]' k: [4 v$ B
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    * D, }! Q1 O* G
  169.   i2 m7 h( b; `1 Q+ H. o
  170.             Dim nullUnit As Unit = Nothing
    $ e2 S, Z1 r9 A1 m- L( p4 z) q

  171. - [9 R- V8 }# U0 i" S+ j. g+ K
  172.             Dim measureDistance1 As MeasureDistance* m* y* A5 Z, e
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)" x& j3 t5 A8 N

  174. , B7 I, X: R" D* |$ o7 L0 x$ G- C
  175.             result = measureDistance1.Value
    ( |% x  b% r$ t, R
  176. 9 }; V$ X1 `: |7 R( N
  177.             'measureDistance1.Information()8 Z/ D( A  u$ K9 h9 @
  178. , A/ R  {- {' v+ ?2 D
  179.             measureDistance1.Dispose()1 Z" w- F* g4 h
  180. 9 R- \! ?1 Q3 p  Y' Z* `3 m% ]
  181.         Catch ex As NXException  f% p! l# k" u+ b3 s- b
  182.             MsgBox(ex.Message)
    6 C6 Q6 Z2 ]1 G) T7 ]) l
  183.             Return Nothing2 H) R" Z3 @9 ~/ {* E

  184. : ^1 f4 h& F, v2 J
  185.         End Try
    # i' J1 G% P) ~  I6 @% p1 y6 x9 `
  186. " K; H# d+ W- o! k) s
  187. ( `' s' Y0 a% I8 d# E
  188.         Return result0 e6 P$ W3 B* U  q2 q" _5 N7 v% C

  189. 1 Y  v; t( @: f0 G
  190.     End Function# T6 k' g4 v; h7 K# N
  191.   @( V9 G6 m# k6 w- [
  192. # t" [! s9 d. K' l: I
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer$ @( R/ }4 X+ T/ r
  194. / L. Y' V4 g9 ?7 m
  195.         'Unloads the image when the NX session terminates
    + i/ }4 E" r2 E- N
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    5 w$ C/ r/ X0 k
  197. 4 x' T' v2 x" k& \6 ~
  198.     End Function
    * Q, U+ `8 t. {2 \. ?0 ^3 T! D8 }* @0 ^6 a5 w
  199. ; ]  g% [5 F. C3 m0 y/ I
  200. End Module
    $ u% g" ~4 |( i8 m
  201. </P>
复制代码

* \! ]4 f8 n" J# Z/ u1 z; V0 U  M2 _
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了