|  | 
 
| 
x
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!您需要 登录 才可以下载或查看,没有账号?注册 
  ) X/ y% G7 g% V4 PNX二次开发源码: 装配中选点,并报告父对象
 4 w6 _+ _( H$ N, k( y
 复制代码  U2 u$ f2 x3 r. o3 r
<P>Option Strict Off+ G1 w6 f' C; W: m
Imports System$ M/ M( r: [) u
Imports NXOpen! a. Q3 d% E: E; x* r
Imports NXOpen.UF+ d# v* ^) ^$ y: v. ~
 3 T/ Z+ j0 h7 o/ Z: H( ~
Module Module1; Y5 t2 A. C6 ]% [( r, L
 s; J* ]7 W0 g8 ]
    Dim theSession As Session = Session.GetSession()4 G9 J) Z4 Y( f+ s0 `
    Dim theUI As UI = UI.GetUI()5 p6 f( z( X4 C% I2 X
    Dim theUfSession As UFSession = UFSession.GetUFSession()- \) m) [* H. o4 q1 D
    Dim lw As ListingWindow = theSession.ListingWindow' Z) F+ H" f( K; j: k
    Dim pickedPoint As Point2 f, O# d) a" m: k, c* \+ _
    Dim myModelingTolerance As Double0 \7 u# H4 q( c/ \9 o  t
 . g( I, \+ s9 U7 \' m2 l/ A! c
    Sub Main()' r! L, x7 W0 K: j$ n0 {% r
 0 N* D, @* |& Y/ A
        theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)3 i6 r) V" `, k/ v7 T! ?  I2 I
 8 ]% R5 z& c& {4 N7 p
        Dim workPart As Part = theSession.Parts.Work' Z4 x: v! |9 ^
        lw.Open()# S' P- G$ j; {0 {) x) D  a/ |+ T
 . D4 ~% l# z, g& k9 ?
        Dim myPointTag As Tag6 U2 t% ~% W0 b3 s: {2 O% L3 P+ }
 . G7 D" p- r* E2 I4 _
        If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then/ R+ L4 ~) ^% S8 X  n
            Exit Sub; z/ Z5 d9 S0 t) m
        End If8 A+ ]4 g2 e( f4 w
 2 B, G1 Z2 K' g8 N  x7 P/ w
        lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)0 \$ R# N6 b  o: c, Z$ s
 * w* n6 E/ ]& N( E3 w0 p& J
        AskParents(myPointTag). R3 `! }. G; T+ e
 3 c5 O# j$ N+ r$ W! h
    End Sub% O. W- y: |1 k$ S6 w8 R8 y4 m
 ; i" Y  Q7 m' \- g
    Sub AskParents(ByVal objTag As Tag)! T. k2 b6 z, i
 8 q. z# Y: q6 B0 G8 e
        Dim n_parents As Integer2 |8 O! v) t+ d# ?# I3 u; b( \: B9 ]
        Dim parentTags As Tag()  W! q5 Y& u6 t4 b& _5 E8 b
        Dim myPoint As Point; }2 H) B' N; T2 @5 m5 c
        Dim myEdge As Edge2 \) a; ~8 f; e4 Q  `2 }, v' t
        Dim myXform As Xform( p* A) z' T+ N. R( K" ]- ?
        Dim myTaggedObject As TaggedObject# F7 F& E9 A) m, C& c
        myTaggedObject = Utilities.NXObjectManager.Get(objTag)) O  c0 ~8 r7 ~- j/ M8 k% n+ v! q! }2 U
 8 U: y) G+ W5 {. S7 H1 [) L5 r
        If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then8 B4 U* p" \9 A# j5 K9 R9 V
            myPoint = myTaggedObject1 x! ?) @% ]% S: v2 \$ Q, x
            lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)* Z* v; H0 g6 u
            lw.WriteLine("")2 ~& ~" Q$ T/ f* R
 . e- o3 {& e0 }- ~1 ~
            Try8 L& M+ ]0 ^6 T% {% W( T
                theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags), f, b$ m1 P3 d9 N, u7 ~/ z
                lw.WriteLine("num parents: " & n_parents.ToString)- q+ g4 _( x0 T3 |2 E0 D$ \. O) }
                For Each parentTag As Tag In parentTags$ {; d. k+ M! }/ \
                    Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)/ T5 m" I! ~- R. y* |
 & O0 w# P' }: `9 o) a
                    AskParents(parent_object.Tag)3 W1 l8 z  G1 l1 U! m( l
 ( ?; u& F  y( E+ _
                    If parent_object.ToString.ToLower.Contains("edge") Then1 ^% k" H& s  n- p
                        myEdge = parent_object( k! `  D8 u1 u
                        lw.WriteLine(""). {2 Z, {; d3 ^4 ]! Z
                        lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)3 u$ E8 v6 |: P+ W8 i9 E) i
                        lw.WriteLine("edge length: " & myEdge.GetLength.ToString)3 m7 P+ }  M! j
                        lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)9 r+ @0 w3 J% f) i$ Z# R" r
 # w- U% n; J- \1 a6 f
                        Dim partTag As Tag = myEdge.OwningPart.Tag8 v4 @: K" n1 v2 d9 h
                        Dim occTags() As Tag; T/ S& H$ \- F( l8 e
                        theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags); @& k3 Q% E8 A
                        lw.WriteLine("number of occurences: " & occTags.Length.ToString)& i) R. ]) d  }" |: x4 _. H
                        For Each temp As Tag In occTags" u, z8 u! n# z! U
                            Dim myComp As Assemblies.Component1 t; U# j- o& R2 F5 D
                            myComp = Utilities.NXObjectManager.Get(temp)& K5 g$ ~9 ]* v3 l
                            Dim myCompPos As Point3d. }) ~+ L3 f% _
                            Dim myCompOrientation As Matrix3x3% x0 [  ~% z. V
                            myComp.GetPosition(myCompPos, myCompOrientation)- ]" f( R% ^1 k* d2 f
 / J# O4 }; \7 K9 W
                            lw.WriteLine(""). ^( o7 f4 {! N7 a+ F1 Y
                            lw.WriteLine("component name: " & myComp.Name)' e0 I4 z/ l  o( h
                            lw.WriteLine("component display name: " & myComp.DisplayName)# C  y2 ]# D- N9 i9 T/ a/ a% E" n
                            lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)/ {) i3 z$ J- A+ P' d. P
 $ ?( g% I( m  _
                            If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then% ^& L! c; a* L6 A! X
                                lw.WriteLine("** This is the component that was picked **")5 X7 v( g$ ]/ z! u+ Y8 o- K' k. J
                            End If+ h" @; j  R( s* T' M& t
 0 v% K0 f( t! [5 I4 a7 P
                            lw.WriteLine("")9 _5 P* J# R6 y& e7 B
                        Next; w2 H( |: o3 S: |( D
 8 K2 F5 s/ U0 ?9 X7 V) _
                        lw.WriteLine("")$ O6 t8 V6 m5 m' x) D, q) d7 O; X
                    End If" R& p% T( }0 y  b6 {: j/ B% N3 g
                Next" C, u* y9 ?1 Q% w
 - ?; h1 i; ?: l! h: L
            CaTCh ex As Exception* d" w; r5 Y* c8 ?7 [
                ' NXOpen.NXException: Current object is not smart4 W! q: B, x% x# I) x. B
                lw.WriteLine(" Error: " + ex.Message)# p2 K6 K' y3 P7 q
                lw.WriteLine("    " & myTaggedObject.GetType.ToString); ]( i/ o$ J. D
            End Try% c7 F: O' |' Q+ e7 x/ A# d
 ' q& d7 \- n4 o/ ?' F/ P' ]1 v- M+ x
 ' v7 Y$ b3 d6 x1 S& u" ]
        End If2 B+ p" G; l+ f( Y+ \0 ~+ J5 g' _
 3 ]6 u. A' p& M3 O2 h0 O$ {9 Z% K
 % t% I  |5 ]# v0 Q
    End Sub; _& q3 w1 w! ~+ X2 B
 ' D; V0 b3 S6 d/ Z
    Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer7 z$ K) ]& G: t6 g4 C! k6 @4 B. s
 , `; E' v! t* \' o
        Dim base_pt As Double() = New Double(2) {}6 c' O" h5 w/ y! `+ Y# M
        'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null* k; b0 C+ L1 H$ j; Q# O
        Dim response As Integer = 05 U) S4 U) b' v# \9 `% J/ s
        Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt; ^; z! P. E: p$ H3 G2 U% T
 2 V0 t. ^2 X' ?6 T4 c
        theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)1 c; [( ?' I# z9 K
        theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)/ I6 i  z+ C' q/ r; z
        theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)3 L9 @! O, R1 @+ o: ~
 - B( J; t+ E3 w" B9 H8 [/ w: O+ I/ E
        Dim pointLocation As Point3d5 b9 ^; j4 I1 \- R! @
        pointLocation.X = base_pt(0)& B! U+ H1 I: [5 Z8 I
        pointLocation.Y = base_pt(1)7 N9 C, S- K- G. A$ |" |
        pointLocation.Z = base_pt(2)/ |5 N& T% |, }, Z6 {5 \
        pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)$ K& O8 ]& Z' R$ k
        'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)6 f: B3 H  y9 x0 i; b
 . f8 b( z5 E8 C- B1 O  h
        Return response6 r% q5 y! P, S  t8 B# R% |
 0 U7 w/ z; z! @/ @+ e; B$ H6 x
    End Function3 t9 {5 Q! V/ r" C5 j
 ) Y, |0 s0 f, o
    Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response) j6 n/ L- k* ~! d' }
 ' b9 l/ p9 C$ o( Z) Y/ O
        Dim selObj As TaggedObject8 G0 }* R# X" [
        Dim theUI As UI = UI.GetUI& j* g. n) J/ X! R
        Dim title As String = "Select a Point"+ l1 |  S2 @: B3 Q
        Dim includeFeatures As Boolean = False" T% f: R# ^9 B2 P
        Dim keepHighlighted As Boolean = False7 G! y; m# |+ b- u
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific# r) E/ o5 u  W6 w
        Dim cursor As Point3d  z! Z, J! B" m: h/ ?
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly; S( |& T( I  N$ Z) x
        Dim selectionMask_array(0) As Selection.MaskTriple% N- c4 P/ K6 R/ P# H% j
 9 D+ m1 t/ }  N) e8 _5 x
        With selectionMask_array(0)! p. k' i$ C; r( W" n) z8 A. P
            .Type = UFConstants.UF_point_type7 _" {) g% _/ ]
            .Subtype = UFConstants.UF_all_subtype& _, K8 z+ O! }
        End With2 m) p( q' U0 W5 N/ Q
 - W: Q) x$ F7 K- b% F1 u4 a
        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _& k. A6 X( Q. s2 W+ ^) @! R* I5 `
         title, scope, selAction, _6 V' S" o+ \/ y
         includeFeatures, keepHighlighted, selectionMask_array, _5 w+ m4 ^$ c' j$ `4 M/ j1 {2 `
         selobj, cursor)% E% b# [1 N) f  [( @. A
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then/ l; m' l( p# `) `' b6 {1 G( {! t
            selPoint = selObj- ~$ p9 w$ U, r9 s
            Return Selection.Response.Ok* g0 t. w4 F& n' M" F. n
        Else/ c# Y* B$ U& \! i2 B" R
            Return Selection.Response.Cancel# E# N- {" S; Y4 n9 o0 k$ [
        End If) _1 c# I7 J+ A( Z: Z+ i
 ) m( ^* z8 h: T$ r% S3 Z9 B
    End Function7 J6 D! y5 |3 V- C4 h6 {7 D3 G+ q
 0 c0 }7 L; L' |) {3 Z
    Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double8 u! r& ]; E+ m, K# [/ _
 7 y+ k- P/ u% s) _1 @# w& u
        Dim result As Double; f; M0 z" e, f, i* |. X
 ) n9 X/ n: Y  U7 B
        Try: F: @" D3 V8 ^) g
            Dim nullNXObject As NXObject = Nothing' d. |# K5 f8 t* \3 u
 D: F% O/ d; H) n4 O3 d5 l% E
            Dim measureDistanceBuilder1 As MeasureDistanceBuilder/ ^' l1 K5 [4 A5 Q+ Z  \4 \+ ?& p
            measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)( y9 Q( z# d5 l: M
 0 R. p- N- M) t1 c2 Z) e
            measureDistanceBuilder1.InfoWindow = False5 Q9 j' Y, o9 v) S# r
 & r* |# z( |  ?9 L* K# o
            measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum/ b9 d4 \8 P0 ^
 ; q: o2 k- z8 O
            Dim nullUnit As Unit = Nothing6 ^0 R+ m3 h, }" K0 [9 |9 E
 % m: O' S# v2 _; X3 G& `$ {
            Dim measureDistance1 As MeasureDistance% l5 ^. @& S7 n* A
            measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)5 o2 J5 h' P2 G! |2 a
 $ ^5 [7 \. n4 Y# K) Q# X5 h
            result = measureDistance1.Value* n& F. S4 ^9 P) b: P5 Z* n4 I
 0 A" L& D0 ]4 Z6 Y- w8 _8 {( g
            'measureDistance1.Information()& G, R4 }  l6 ?. K3 I4 B
 % n+ U0 Z4 Y* o7 E2 M' F
            measureDistance1.Dispose()+ O4 S+ }7 O( v( @
 2 J: S1 r( R# b5 W& Z
        Catch ex As NXException& f9 b' @6 e1 o  k! r3 ]9 p6 b
            MsgBox(ex.Message)* b( j" T. `6 T5 q
            Return Nothing# i7 d5 |) v# Z: `% ^
 5 I  u- y0 K0 D: m: _$ r( b) Y$ x
        End Try) `3 Q& d  \8 U6 e  B) }
 " o+ o; u; Q2 M0 V+ N' A
 9 @: g% P% s  Q) K" A) i8 F
        Return result9 O4 p5 E. ?( k% {- ?" P1 R# f7 d
 6 e  N3 X  @, O" P
    End Function) c% w4 D8 h1 f
 / _) t+ g4 ?2 f  E# e4 ~4 X9 F
 7 x- x3 |* a: t0 W
    Public Function GetUnloadOption(ByVal dummy As String) As Integer" H; T$ R) W- a8 Z; @
 7 O% l* B; f  }* p4 ]5 n7 B
        'Unloads the image when the NX session terminates8 K( f2 n$ }; L) L
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination: f$ u2 Z6 N7 e- `
 9 X" q5 J2 C, i0 C
    End Function* z+ p$ s1 }/ \9 ~0 [0 u
 : F( l8 E/ w1 s! G* }& Y
End Module! o! Z; H5 N$ ^
</P>
% A3 k5 J" @0 ]. r* s2 g6 {/ b
 | 
 |