|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
9 N# X" n, i9 c4 C( y6 ~NX二次开发源码: 装配中选点,并报告父对象
) L% }0 {1 O5 P6 ]& q( q5 |; r5 I
2 |5 e- r% O# K% T/ }- <P>Option Strict Off2 c; J" r: U- q7 U5 `
- Imports System
; q4 [" e8 Q1 ]. y0 t - Imports NXOpen
, g+ ?/ M6 |9 S/ s& ~" w - Imports NXOpen.UF
2 v% y8 ]7 H% b& t' V" T. o+ X0 n -
, ?9 T! ]. D( B7 k9 n) X: }5 ] - Module Module13 G8 L% ~) n9 V2 b: e* V0 j- T
- 6 e6 D, O- X- a" S) y. ~1 X/ ^
- Dim theSession As Session = Session.GetSession()
% l; |) ]6 b; p& d) f0 c - Dim theUI As UI = UI.GetUI()
, X- h! z9 d( u - Dim theUfSession As UFSession = UFSession.GetUFSession()' J) E& `: z1 \' q4 T
- Dim lw As ListingWindow = theSession.ListingWindow
( I. n/ l( Q9 `% q& p0 D: v8 V5 f) u - Dim pickedPoint As Point2 i% I5 u$ F( A) k: x" |! W
- Dim myModelingTolerance As Double! V: m6 x0 e" x z Q. X1 c
-
* M6 r- L9 L. ]! u- X - Sub Main()
+ E+ y6 W; p# A3 P ^) G - 4 C9 P# N4 z2 F% n
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)4 F+ a( ]9 H; ]( J
- $ E/ _# s5 d: r. t# K
- Dim workPart As Part = theSession.Parts.Work* E* Q9 O5 ^5 p. O
- lw.Open(); O1 d$ _ o- R! Z
-
M+ h& Z9 d6 y. F - Dim myPointTag As Tag
: n. M9 r& K9 k/ E1 F, k* P. w -
; l+ W- c4 g F3 q" D: P$ ~ - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
! q: c3 P+ `4 l% M - Exit Sub# d# d" |) P0 \( `- _2 \3 k
- End If4 j7 b1 q. q6 T
- / i7 y: F. K2 X( k& @* f- N9 o0 ]5 ]: s
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)% ]5 c" r) k* Y) o0 ^" C% ?! k
-
5 E" ?0 V+ _8 l& p' D. n - AskParents(myPointTag)5 \# F0 t& x% ~6 A6 [' K
-
! L' k* Z; Q# c6 \, ^9 P% J - End Sub. s& ]$ U+ t4 l# r
- 4 A; h6 _4 K; [
- Sub AskParents(ByVal objTag As Tag)8 a. E2 \, S' I& W7 Y3 {
-
/ g9 a* r e0 i4 E+ @2 F. f' h+ l - Dim n_parents As Integer0 E2 h! y8 E0 B# b7 r
- Dim parentTags As Tag()2 l& n& y6 n% P5 D
- Dim myPoint As Point( G0 Y: T3 p ?% C% x+ L9 z: x1 x
- Dim myEdge As Edge6 n! ?8 L6 i* e1 x! ^
- Dim myXform As Xform' Q' e$ @" b/ a2 r/ A0 Q/ I" B3 a
- Dim myTaggedObject As TaggedObject
9 Q" _) |/ _5 o3 V - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
! _7 B, I0 E6 `, P2 H/ C9 @* z - & b7 u+ [+ T# H
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
% L+ x# u9 z5 ?+ A- b, m' r; @ - myPoint = myTaggedObject' K$ a% _4 J& g( n) R
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)- f4 i8 {. B( _5 u( }; ~
- lw.WriteLine("")2 Q- }6 ~2 n2 q
-
( O" G" z9 b( t( a) b - Try5 f3 Y0 ] v# ~" C6 p! K4 }+ B1 a
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)* `& a1 L) w8 w2 m) K5 D+ [
- lw.WriteLine("num parents: " & n_parents.ToString)1 m& z' x* q3 V, Q( S* k
- For Each parentTag As Tag In parentTags
6 C0 d4 |+ O) b+ i, B* d - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
3 B* V% O, {2 n% k5 Y) ? - + t3 }0 e+ H, Y+ Z8 y9 s
- AskParents(parent_object.Tag)
& }8 w c1 Q4 J- n - ' k. N4 S% \( Y U; f
- If parent_object.ToString.ToLower.Contains("edge") Then3 q# } y4 i; u# S
- myEdge = parent_object
2 V3 v3 w6 A7 G - lw.WriteLine("")4 y3 J: ]; G$ t% ^8 I2 v
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
: ]5 Q3 h m+ Y" b( j4 ^ - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
. N& q3 E% t8 q - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)) x" ~! l/ Z6 }$ v
- 2 Y( O8 i# ]/ q& ^% J
- Dim partTag As Tag = myEdge.OwningPart.Tag4 @- e7 `1 ^. z
- Dim occTags() As Tag
, v7 V7 t1 `0 Y" m, J9 L - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
; r y+ L; I9 w# d1 x# B! H - lw.WriteLine("number of occurences: " & occTags.Length.ToString)
/ n- s5 _' C: v1 ^ - For Each temp As Tag In occTags
0 F1 R/ p8 b7 v4 } - Dim myComp As Assemblies.Component/ I* ~) y3 E2 T( ~% A- u
- myComp = Utilities.NXObjectManager.Get(temp)
^' _; h3 X q S - Dim myCompPos As Point3d8 C% _2 a5 Z7 \$ V M# D9 h& R
- Dim myCompOrientation As Matrix3x3
3 | a) l: X( {9 q6 q - myComp.GetPosition(myCompPos, myCompOrientation)/ m( A+ p3 N" L' P0 ]* c7 s- m
- 8 Q, I5 l2 R3 t' o1 `* F
- lw.WriteLine(""): n8 ~' }$ q, B# \$ N
- lw.WriteLine("component name: " & myComp.Name)
: P! L- ^1 z5 C& F5 s; e, T. F: X+ U - lw.WriteLine("component display name: " & myComp.DisplayName)
4 r3 t- |, K7 K) n& c8 B+ Y) v& N - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)5 r; u3 S/ O! l8 n3 z# g! V' V; u$ w
- ' {0 R% R' e8 ^* u5 Q
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then6 S7 W0 l" b5 P
- lw.WriteLine("** This is the component that was picked **")+ I: ^8 q- ~! X# n
- End If
0 i# _ C" n& Q) }( K1 F0 ?: I! Q - ! ?8 y: z! q: B2 B3 F
- lw.WriteLine("")7 Y1 R5 ]% ^) K7 u. S
- Next: g \& ~' n$ {+ \6 H5 n
- 7 m4 P; ~$ c, y
- lw.WriteLine("")) H! a$ ]& T; Z( a
- End If" @ n9 x. T4 \
- Next
6 ?$ y4 y! |' s -
6 f" s6 K) s5 U7 A - CaTCh ex As Exception& E9 U! W1 {" s* F
- ' NXOpen.NXException: Current object is not smart) S7 K! ?* a- H
- lw.WriteLine(" Error: " + ex.Message)
/ ^% x! e y# q$ Z: h; b q - lw.WriteLine(" " & myTaggedObject.GetType.ToString)
; c$ \+ A/ O* {: S, L% M - End Try4 K- U& ~5 N( @8 h! o
- " V" p# M# `: l- C$ x: t
- . U! y5 L$ O0 H1 o1 ?
- End If8 Y' Q, D8 G2 X
-
! a: x/ o, R' w. ?' a1 R -
$ g$ ?- o4 d& ^3 ?% n - End Sub
5 I# @, p& Z% u/ B8 P ^9 d# @ - * u# T1 ~1 O9 l, \# g& L
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer3 q' u5 H5 ~( C u2 p% H- i
-
( J9 V& f& T" K1 V - Dim base_pt As Double() = New Double(2) {}: s2 l+ y! f8 J& d- b
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
# |, o7 O. P1 W R - Dim response As Integer = 0- U. g$ r, s! I2 o9 o' |4 L. ?) I
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt; Y2 j5 ?6 H: R
-
) v- w- T; m: _& b! T - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
( E0 `1 b' O8 b, } - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
5 I# C* F% w; G) ]& l+ d* K - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)! l; j9 S" A$ I& V
- . s, v% t2 e0 d3 u+ K/ B& o
- Dim pointLocation As Point3d
4 T/ l/ @2 I) v - pointLocation.X = base_pt(0)
* O# Q9 q# F1 V& L - pointLocation.Y = base_pt(1)4 r# ~) h( Z) I8 q; A; \* K
- pointLocation.Z = base_pt(2)7 C, r9 i) |6 l( w
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
3 K( f4 t, }$ y' |) y6 F - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)3 }7 k& g* a. H: v
- 7 D+ J( {% C& Y" `" I7 D( w0 z
- Return response+ j# ] ` J3 h; U/ D% z
- : q3 s: l7 n& C! D
- End Function
+ J! e- @) V& f& H0 f% F - g% l6 E3 W6 y( p3 z- i ~+ D" Q
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response$ V) _; |- J# R. N7 r( M
- & T' L6 r7 f! y3 f# L( l! G& }
- Dim selObj As TaggedObject
' _5 q8 [6 i3 q2 w - Dim theUI As UI = UI.GetUI9 ]) Y4 `/ u; ^6 x
- Dim title As String = "Select a Point"
% S! _' @1 A) |" z - Dim includeFeatures As Boolean = False1 d" b) S) Q2 F4 F
- Dim keepHighlighted As Boolean = False
8 H* y6 g7 T6 W; s/ ~ - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
3 e' ~7 I& _; D& e+ ^9 {" { - Dim cursor As Point3d
, @" `+ X" A: `) h/ N6 w - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly# t- q( U4 `% y/ |; d
- Dim selectionMask_array(0) As Selection.MaskTriple+ l9 N0 G5 k$ S& T9 v1 j' T
- * h( t5 n# a }+ `
- With selectionMask_array(0)
; a. m' J: C# v0 e# \ - .Type = UFConstants.UF_point_type/ J6 z% X; M; N% U
- .Subtype = UFConstants.UF_all_subtype7 @" j- s! f+ ^# X0 O" s
- End With, J* E% O5 Z( g1 w& e3 W; J
-
& B% k9 Z) B9 T - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _6 q6 j3 s W- z" k
- title, scope, selAction, _$ S" j+ l4 M- Z2 Q& I$ F
- includeFeatures, keepHighlighted, selectionMask_array, _
" ~& `/ |7 o4 O - selobj, cursor)
9 Z* i+ T2 e5 b. z. D3 I - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
% n4 B. X8 \1 ~ - selPoint = selObj
3 o( b( Z( J- Z' b - Return Selection.Response.Ok4 t- q0 ^5 {4 F; G
- Else; F9 X, \4 N8 K6 E
- Return Selection.Response.Cancel1 G4 F' b& g& S
- End If
' |% }, S2 b2 e" z& |+ ]* D) f( Z - " Q$ V* ~# t* K& L' K# ]
- End Function
m3 C6 y. d& j1 c9 D7 H: z+ B - " N- [ r1 A8 u, E; k
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
, c" l' Z* |* q - - t+ m5 s+ x/ N, m- d6 m( l0 h* _
- Dim result As Double
, W, n( @- `6 l. `* W) M1 @' L s -
: Y, A6 H: [9 }1 q - Try) Y* U4 W2 o" X% k: b; r3 l
- Dim nullNXObject As NXObject = Nothing
' W$ R% D. a4 N3 o, [ -
; R9 b1 i, y1 H - Dim measureDistanceBuilder1 As MeasureDistanceBuilder; U' g1 [( j! H" O* | r/ }8 O. u
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
& ? w3 J- R; ~ [1 n _8 b -
7 S! C( M B" K - measureDistanceBuilder1.InfoWindow = False
: k) K! x8 P2 Y! |- [ - / ]8 K: V- i5 G, [5 w
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum) d7 d) m! P; {3 Q' O2 S F% A
- 5 f. h9 A1 u- P) g4 b. t1 W
- Dim nullUnit As Unit = Nothing. M0 ]4 S& j6 G; a o: k
- ) _0 Z% @ Y3 k; G3 O
- Dim measureDistance1 As MeasureDistance
+ [) F' c ?. s* F) X/ [ - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)+ d# [& U2 V6 r) A: i+ Z
- 4 ]( E9 E; M/ a
- result = measureDistance1.Value
9 z% f4 r1 f4 k8 {$ t$ V7 Z - & K# C; }0 `' ~7 F3 h7 n
- 'measureDistance1.Information()
; ^" }9 b2 F9 T6 _ -
% Y: R! \! J8 h2 C - measureDistance1.Dispose()
4 M u. |" v8 B9 N( q -
; U7 t# w2 W0 q$ c0 {; w$ @ - Catch ex As NXException6 D# Z4 P( G# O/ H# C3 `
- MsgBox(ex.Message); V* A! M# k! h
- Return Nothing" e1 J1 Q) k7 t" r4 o1 N
-
* x. p/ t0 s3 V9 L4 n6 d$ {2 T - End Try: D: s; ~" X5 B+ |1 \
-
. c& x" i0 T' B$ e; R* S& ^ -
$ [ k: Q3 a6 K0 m; f9 J! H - Return result
4 I4 J. E: Q) M3 n4 h3 H. ] -
& D9 W6 R7 k7 ?- Q - End Function
# C9 ~- t' d+ L i7 S; D& s -
% v& _; l$ C; W0 g, R -
/ p0 Q; q5 T8 J K8 J9 b4 H - Public Function GetUnloadOption(ByVal dummy As String) As Integer
. ?' A; _* M+ K, W. L- s- Y - : R8 K/ j6 E' L- o8 D- ^" k. h/ G
- 'Unloads the image when the NX session terminates
/ ]% s' Y( J% E& J1 }! f; q. _ - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
- u' ?+ ?/ }5 k2 X) t - 4 } `3 X0 ~6 H
- End Function- ^- s' \4 E# e: Q! l
- & ~& c2 A: H0 `& U+ _; _% K
- End Module
" N7 q+ W. R5 Y+ l) r$ y& P - </P>
复制代码 " M ]+ L, t% `5 a7 \
|
|