|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 t& p" D- f; G; V. K6 p! G/ x2 PNX二次开发源码: 装配中选点,并报告父对象
c; g8 j/ [) A S- - d* W1 Z" c1 U A
- <P>Option Strict Off: V) h0 {) X* j* S5 B; y9 O6 O; `
- Imports System1 z5 w) ]7 H+ ^+ h
- Imports NXOpen V' @, {4 d. y
- Imports NXOpen.UF
! |% f4 ]0 g: g, B -
1 E' f! Z, U M' f5 N - Module Module1
9 T. G/ x2 o3 o1 C# A -
+ }' r* e& c1 t5 T4 r5 x - Dim theSession As Session = Session.GetSession()* d8 v6 c- N% C/ b/ m
- Dim theUI As UI = UI.GetUI()
' x" S; ?8 a, I f- s- g - Dim theUfSession As UFSession = UFSession.GetUFSession()
' _3 f) E: ]: }! ]! \8 k& c - Dim lw As ListingWindow = theSession.ListingWindow2 U$ ~6 V3 p) ], C
- Dim pickedPoint As Point" e; X1 i" W5 S9 r
- Dim myModelingTolerance As Double6 L. |& d7 l2 F/ \
-
: r* M. H1 a7 q5 H - Sub Main()
* \8 A4 M7 @1 K -
& g8 h% ]% Z2 G' j: M8 W - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
/ _7 X0 g: I$ T0 l5 |# D - 1 }- f: W3 {% I% Z
- Dim workPart As Part = theSession.Parts.Work
+ p# P2 d7 X% f8 ] - lw.Open()& Q4 ~3 |, u. }6 V
- - |0 i1 @+ u4 P2 r$ y
- Dim myPointTag As Tag! I# d0 _$ C: T9 L% c2 \" J
-
/ D7 W0 W/ u) E2 z+ r0 q& w# M - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
( E4 s% p0 B( ~$ M! C# _ h - Exit Sub+ o, @' a: b! t2 L# m/ Y
- End If$ f! c! I1 x! _# S
-
! g E* ]3 r! U$ Y8 h7 h - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
7 [' i7 h5 w# `; P3 A. X -
% _( v6 ?& @) m4 r4 N* E, J0 h3 d - AskParents(myPointTag)
* I& J3 j, y/ `% O - ! `1 i6 J n" C, s
- End Sub
) |' I- s7 P2 v3 p7 y; l -
! H* Y% Y9 o+ z' _' ^$ k9 M - Sub AskParents(ByVal objTag As Tag)
( ~; b2 ]' U9 H! ~7 o$ ^ -
. B* R) d' [9 { l/ y7 o - Dim n_parents As Integer$ i' B& g) V' R+ U
- Dim parentTags As Tag()
# F8 M% L& N1 p( J) J - Dim myPoint As Point
( n6 {; h8 D0 w6 R; F - Dim myEdge As Edge( d, t2 D% w' ^; ^ X( r' {: W/ H
- Dim myXform As Xform
1 [3 x* K, g6 a& f - Dim myTaggedObject As TaggedObject# u9 y' u( P! \/ }' ?/ v
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)
/ J% S% I, Z% b9 p* B+ v; M9 u - 6 P3 O- V& K& I! R5 u
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then6 B0 i1 X, E. \8 P: }
- myPoint = myTaggedObject `; }3 `* U1 {1 [) h. t
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)8 F6 G( ~: \% E: m P0 I% r
- lw.WriteLine("")+ s* I5 g, [. f; p9 f
- # \ `, T2 B' t1 e( A. a
- Try
3 r# K4 }- W7 R: ?. K% m% b0 A/ c - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
0 U/ i( Z5 z/ N& ]. o) g( p - lw.WriteLine("num parents: " & n_parents.ToString)
( J- D! @; V7 E - For Each parentTag As Tag In parentTags" s E; }3 v) e! C1 Q2 ?7 ~4 o
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag), y8 B. ~% b; [, ], ~
- * o3 i& Y! T( G7 j
- AskParents(parent_object.Tag)3 F4 j; _! M/ H* a* J4 J
-
2 _5 n P$ Y( `2 L f9 a) u& G - If parent_object.ToString.ToLower.Contains("edge") Then
% }' M1 C/ V6 [4 {" M - myEdge = parent_object2 x% ?7 I" v$ X4 L7 t- q9 A
- lw.WriteLine("")
% S5 w* N( `# a j e - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)! a& k) {9 r3 Q5 h
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
; P" r6 K K: @3 s( |2 i - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
/ E2 \$ {" a6 a; [; _8 `- M5 O - 8 e: s/ X& |* n' }, U! g# j' [
- Dim partTag As Tag = myEdge.OwningPart.Tag7 P- v: v) _# i
- Dim occTags() As Tag
% I% t; ~. D$ c4 D9 A" V% x9 p: w - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
4 b0 L' Y% \ _( ^. M6 p - lw.WriteLine("number of occurences: " & occTags.Length.ToString)8 |3 h9 T( ]7 A7 Z7 [
- For Each temp As Tag In occTags1 q0 C+ S: `' J' e' n( d Z: ?
- Dim myComp As Assemblies.Component2 F0 q( C. M ~
- myComp = Utilities.NXObjectManager.Get(temp)
' G( S( m: ?) n# _2 `# f - Dim myCompPos As Point3d
+ ~9 d5 T- M% ^2 l! D& ~; R - Dim myCompOrientation As Matrix3x3- A6 E; o# L4 G& @
- myComp.GetPosition(myCompPos, myCompOrientation)
2 k/ w. K4 P7 a; I( ` -
# ?: U' S3 H! B% K8 j, o: U - lw.WriteLine("")
; ^" h! k0 k2 v: r% _ - lw.WriteLine("component name: " & myComp.Name)( Q$ v$ E% U* A9 M: W/ D
- lw.WriteLine("component display name: " & myComp.DisplayName)
$ H1 }2 J, a: u3 { - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)6 [( O3 u4 v) D, @$ h
- & C* @- N& O; M6 G- G
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then9 {# H- D; \ l [5 K
- lw.WriteLine("** This is the component that was picked **")
2 j+ r. }+ M5 N. @; V7 U$ Q - End If) V; t# _! N% _
-
1 {) A* ]% Q$ R - lw.WriteLine("")
. U' S8 u- @$ U' y/ D( a) @ - Next$ g; {6 v% f8 J
- ) x( }& y9 [/ y: F
- lw.WriteLine("")- F H, @) R) K4 g" T
- End If
$ b- e6 _6 |7 A. H+ ] - Next' ]: o9 R9 R7 E4 g& e" s! e5 X
- 1 L# w% M. k% m1 x
- CaTCh ex As Exception
3 t- ^ F; E# n9 u [, u - ' NXOpen.NXException: Current object is not smart' a- I& i& X: G& A% G0 Z& d
- lw.WriteLine(" Error: " + ex.Message)" Q3 p4 S$ ?$ P+ _) u& Q5 v5 _! v
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)
" b0 b5 B) X! y- _! @ - End Try
6 c- {1 d: E- Z$ c, B - - N2 G/ p" [% Y2 e, v
- " s! ~) s$ j p0 \2 E3 q4 p
- End If0 t( {# I8 L. B+ W! |& t4 I) C
-
& V0 m2 Y' b) Q# Z$ q -
6 K. R2 U) W4 p# Y4 `1 l/ F - End Sub5 m' O+ y3 h. J# r
-
/ j/ [' C6 _3 v - Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer8 J7 I1 P1 e5 B/ g; e, [* Y
- # {; P8 h* w/ Y+ y) I
- Dim base_pt As Double() = New Double(2) {}
; I' V0 O9 T7 A* @. w& Z - 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null; I; r8 k- ^$ T5 C Y
- Dim response As Integer = 0
' L0 V0 j- O" \# z, E! q% P- u - Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt, t8 q& ^( S, d( j" I% B
- * n! Q, h7 X4 e1 |+ x
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)" A2 o S+ G4 k; ]8 ^
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
& p5 \# i; I0 `( s+ I2 ~ - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
! `6 Q/ p; P- O" l- m - * l$ u( ~, ~+ z: F9 D0 f, Y# o
- Dim pointLocation As Point3d; n; N; ^) N- y
- pointLocation.X = base_pt(0)
% ]" S. C. L s1 ?6 J/ F/ J3 M - pointLocation.Y = base_pt(1)
3 N2 ?- i, f5 N- ` - pointLocation.Z = base_pt(2)
; [5 I6 N R6 }" { - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
1 k2 ^; x( ]* M - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
$ ~0 U' G: @, u' R9 r4 A' J -
9 ~ [7 ~) j% u$ u - Return response2 s2 y' C* T/ f9 T& H
-
: _1 N3 e( |0 ] - End Function! E% ?7 G& C. T
-
! e* K) c' i* k% @; F/ J4 J( p - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
p% j$ z9 I2 k: C# M0 A/ s9 Q0 J -
% x2 {5 b% j7 V& f1 {0 y( I - Dim selObj As TaggedObject; X5 W0 c8 E) F% t( _
- Dim theUI As UI = UI.GetUI: N7 N* h' g0 q1 Q/ [$ t3 x8 j
- Dim title As String = "Select a Point"
) M, V! p- r* U3 F! ? - Dim includeFeatures As Boolean = False! x: E: k. P1 h" O% J8 s; l
- Dim keepHighlighted As Boolean = False% i6 w4 V+ f( y9 Y y
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific2 y) b1 N1 J6 Q3 ]" l% I
- Dim cursor As Point3d
0 e4 Z% L$ Y. J8 ~2 I5 k - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly( v/ [- p7 O3 K) b& O$ k" l4 E' C/ Q
- Dim selectionMask_array(0) As Selection.MaskTriple
3 p3 w- \6 D @0 o3 G6 a& Z& r- Q _ -
4 k) F+ [0 s, `/ I( T - With selectionMask_array(0)5 k: ?5 V/ @* N9 J" L
- .Type = UFConstants.UF_point_type6 P) R9 k$ v' |4 P
- .Subtype = UFConstants.UF_all_subtype$ B- z4 g) z7 c
- End With
6 M0 m) X, L0 v/ H: O2 ^ -
9 U( ]- E. B$ Q4 ] - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _) e) Z& b2 Q# U
- title, scope, selAction, _) s: y- ?3 p; s& l; {- @ c
- includeFeatures, keepHighlighted, selectionMask_array, _
9 L- M9 L1 Z" H( [9 |3 f/ D) u - selobj, cursor)
9 Y3 ?4 o7 I7 n- C/ } - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
) V6 w6 v5 }+ E - selPoint = selObj( ]: Q4 p2 p7 [ L& Q# I
- Return Selection.Response.Ok
2 \0 \1 [0 m {9 n - Else
2 |7 z1 ?0 m( |, t, S( k - Return Selection.Response.Cancel; x# J) ]/ f. u/ [4 ^2 H
- End If% Y# E' M; d9 I9 Y7 v z7 P3 H, ~
- , k4 t" ?# }% p7 q! r, b1 b( B; s
- End Function7 B/ A; F0 G8 n" `% L
-
- h: N' e8 k# D) I- y/ c2 p - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
9 Q* h8 |: k! U* { - Q. B/ `/ ]% W$ }/ y K
- Dim result As Double- R, d' {- p+ \* h
- ' W; K- [$ G, L7 o2 s1 j1 k
- Try# }7 ~/ @" K c% q. b4 H5 r) |. q
- Dim nullNXObject As NXObject = Nothing
: L$ f; [0 W! y2 W0 { - , J% Z' L1 e; D; u
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder, O* H$ ~+ Z$ s* w
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)* {. b7 X5 U7 U) ? P
- # e; B' z1 r: h3 }
- measureDistanceBuilder1.InfoWindow = False
# }9 J; h/ R/ K+ H* @9 |( b - 0 p3 Y) G: u4 B7 n+ s4 z# @
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
' N- N1 L |7 d' v2 P - : f* [- d) R" a7 Z3 C5 z2 S
- Dim nullUnit As Unit = Nothing( {- Y1 X, T2 K4 Z/ B# h% ?3 r
- # Y' W( t4 D- x7 ]2 q) ^8 g: Q
- Dim measureDistance1 As MeasureDistance
3 b/ L$ a1 w+ b7 _! y - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)9 M' Q! r* l! N- T7 `
- - e! W" L" i* T; a! s3 Q' L: g- j, h
- result = measureDistance1.Value1 b3 q6 @6 _9 ?( I" m1 g% Q
- , j- {6 l# O/ y: e% y: `7 Q$ l& H8 N6 ]
- 'measureDistance1.Information()
1 B( k* Q0 ^/ P9 D! S -
: N5 H9 G P# ? - measureDistance1.Dispose()
7 k0 N. X$ o/ s0 k* u; o. M -
" h0 b! N3 z. o) k9 u3 @0 M5 s - Catch ex As NXException
+ l1 G$ i* I% S - MsgBox(ex.Message)
/ c4 X/ q" @8 R2 z) g% B - Return Nothing
) E+ J: }+ {" i6 J - 6 W( [9 v! C( @) r L1 z% n4 T z/ j
- End Try' k) I4 ^1 l! v% ^2 e2 y
- ( b7 k: F+ |9 h( g9 v# C
- 8 Y. x6 }2 q4 L# M
- Return result
) @& A" [" n5 V2 |+ [# n& Y7 O - % \4 i2 X+ R( j; n, \" B/ m# e
- End Function
+ I7 O5 m+ C$ A0 F3 E9 s -
+ p) i0 E1 A$ J O; s8 t - ; v# j: Z l2 J( l) G
- Public Function GetUnloadOption(ByVal dummy As String) As Integer
* h2 z1 ?3 k" H4 o -
6 t* G- J' ^/ e0 H& {0 z3 I - 'Unloads the image when the NX session terminates
8 q- }# f; |2 M - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
7 K' }: w7 H; t2 F) D. f* Z -
# y2 X6 ?! c7 r% E - End Function
' f5 C, L' J7 D# @ -
+ f* H: I- U1 B$ H- l5 [$ T - End Module
$ X1 T; P% L1 ?- `7 c. e" s" U$ [ - </P>
复制代码
1 W- d5 G9 U" R+ C2 i! D |
|