|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
4 e! B( {- R1 O$ bNX二次开发源码: 装配中选点,并报告父对象
: d: ?7 I. r; F8 W7 |- 6 X8 `8 e( C, W, w C: b
- <P>Option Strict Off
* J/ h1 L. K9 I9 P* y - Imports System
6 u6 L$ p0 S4 e9 C! \4 F - Imports NXOpen
0 t9 c, g9 b: `& a - Imports NXOpen.UF
8 L1 p- \) f+ T ? -
4 J4 A; v1 `! H1 o3 D - Module Module1) n9 a0 U; U' B- q' B
-
. r$ D# O6 Z3 A A" c - Dim theSession As Session = Session.GetSession()$ g2 t$ u2 [# H8 w; G* w9 Z: M
- Dim theUI As UI = UI.GetUI()* v( K# Z" Y, M5 D' s8 T% H
- Dim theUfSession As UFSession = UFSession.GetUFSession()
: x4 Q) I6 N. G/ v( i$ Z5 R - Dim lw As ListingWindow = theSession.ListingWindow
) `0 ^3 }" g5 @ - Dim pickedPoint As Point! f0 k% w, l; i$ m9 a
- Dim myModelingTolerance As Double8 D9 x% |7 N% [- B( T; z
-
* v/ j5 J% w9 r& R1 v3 d0 W3 ~ - Sub Main()
9 ^" |+ o0 K' @9 G% [+ b$ b - % ?2 n! V- d j0 |& X1 P" M
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
' |" w; g! [ I0 B y e. ] -
# S; S# ~0 s5 }, S - Dim workPart As Part = theSession.Parts.Work" N( n6 \- H- v8 \
- lw.Open()
& ?& d+ i# u8 z; |: n1 ~4 ] -
2 u2 i* S# }- a L9 C+ u, V0 a - Dim myPointTag As Tag/ ^( |: d4 d/ n B+ F c' q
-
; Z7 |$ {) z( q0 ~2 }- s - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
- k* ?6 [# n4 P& y - Exit Sub- I1 H/ ~# P) b$ j/ S
- End If
# q# _; P0 |9 f- T - . h! q7 k0 r% d; a# Z# Z
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)6 e+ }+ T( U( R7 M
- ) f5 P7 j1 m p- r5 E
- AskParents(myPointTag)
. A( }0 N* o u* M9 q6 F( X -
$ ]. L* G2 K0 b - End Sub
/ g# f2 d: J5 J( T' I w -
- q0 @2 H- g" ?, P - Sub AskParents(ByVal objTag As Tag)/ L) p8 `9 [, Y/ N& U
-
6 _# G5 U+ m: `3 m" c4 l - Dim n_parents As Integer
) K S. ?; p- v- A5 d$ w! E) @ - Dim parentTags As Tag()
+ ~; k2 l: }2 W' p- Q+ l+ I5 z" t& r! U+ c - Dim myPoint As Point
* i2 _2 ~* u7 {2 U: i/ k; ? - Dim myEdge As Edge1 G8 E" h. M8 a3 J( U' q+ d
- Dim myXform As Xform
. Q4 |" k4 h& @: [, t - Dim myTaggedObject As TaggedObject& y6 y u$ F0 X9 V6 l
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)/ n R8 o( h" \/ D8 y
- + T: O1 C3 b: A9 q+ ^
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then/ ^! h/ a d. x5 e& o; |
- myPoint = myTaggedObject. n' e! \( \, I& z& d+ U* s
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)+ ]! y6 J) E) j' V* J
- lw.WriteLine("")
2 @/ N6 x( B0 U1 p/ \8 X - 2 S# z8 H' `' h {5 `# c, Q2 i
- Try
9 F4 a. i$ p5 f" { - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
' n2 |8 B6 K% I2 e2 q" p- b - lw.WriteLine("num parents: " & n_parents.ToString)( T# }- s6 |- y( r
- For Each parentTag As Tag In parentTags
' X* F: }! Z; Y, \; |% c - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
M9 ~) k. u& p4 P2 C9 ]) Z" D - ( m) a, k2 F- [; k1 Y8 I4 G
- AskParents(parent_object.Tag)/ A, V# u& A q/ q* U2 M+ i( ~
-
7 _5 R+ B6 N- K g( A8 e6 s; k - If parent_object.ToString.ToLower.Contains("edge") Then! p" [9 f; b% b+ l2 v
- myEdge = parent_object
6 A h3 R' |# k# k4 T - lw.WriteLine("")
& K1 |, r$ G2 F& X& u - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
9 m, _+ D5 g+ f+ U+ w* s9 L) q - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
2 {6 ^ i5 v/ |" s - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)6 T; l/ b5 K# E* p% k1 I
-
& y5 M: E4 i4 c" ~8 W# O - Dim partTag As Tag = myEdge.OwningPart.Tag1 _, `2 P2 [' B9 w
- Dim occTags() As Tag
0 \$ q* Z* Y% |# }) B - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)# T8 \7 o* F; e0 {- v$ }) J
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)
/ ]% E) B. y4 p) \4 J - For Each temp As Tag In occTags
' Q( o+ I. P& Z6 L; F- j% r - Dim myComp As Assemblies.Component
0 b8 m, |- `3 c0 ~! H: I( M - myComp = Utilities.NXObjectManager.Get(temp)! t) M1 o* l9 _& I& _# @
- Dim myCompPos As Point3d
2 d4 ^5 P2 w, y2 ~$ ^! @! i: Q - Dim myCompOrientation As Matrix3x3# i: n& H8 _, p) ]
- myComp.GetPosition(myCompPos, myCompOrientation)2 C' l- E& ^2 k$ A4 ~3 X: h1 ]/ H
-
% {* ~& }( w# c" w, @; e, J+ W - lw.WriteLine("")
8 T" h6 f- U% N' | - lw.WriteLine("component name: " & myComp.Name)' Z1 p& U: F- w0 e7 T9 J( w
- lw.WriteLine("component display name: " & myComp.DisplayName)
' o. q: C1 k$ A+ _6 X' u - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
" d- l2 h* L' ]; C4 e- p - / I- b) A2 J4 {
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then+ G9 `0 x' Y; P0 w/ |
- lw.WriteLine("** This is the component that was picked **")
+ u3 Q6 r; N, j' @0 H - End If7 F z+ U; f$ a# Y' P7 L
-
( h1 |4 M. \( U - lw.WriteLine("")
1 ~9 B( i/ [; \, A3 m4 K# A - Next. A4 V! t3 F4 ? v
- 6 G4 `% q9 z$ W$ L( N7 Y* J
- lw.WriteLine("")
8 }% a A, x& T0 s% }0 R) [/ L) } - End If
5 }0 C, |, ?9 d( s6 ] - Next# S5 f4 V3 s! u) `- r
-
! O+ Y7 t& p- }4 ^ - CaTCh ex As Exception
2 _8 ~/ N) Y( v7 c7 p) D8 y - ' NXOpen.NXException: Current object is not smart. w' X4 [2 ~" ?/ l0 [# v1 l
- lw.WriteLine(" Error: " + ex.Message)8 A' d$ i% c$ m1 z! D0 P) d& p5 a
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)
1 K# j5 \1 ~' b* `' ~3 Q, f7 X3 c7 j( t - End Try, l0 Y k/ c3 `% B/ E. Z4 P8 E
-
$ N. \7 ~, M& h7 V -
4 j+ T. Y9 }. g* r1 U* J - End If
1 N- A: V. w0 ~4 }# X3 Y - ( j# u8 T7 F2 u3 `5 W7 M- `
-
" C$ B! D% l' h: l3 J1 f. ~ - End Sub
! ]6 q# |9 c2 m. x* f' ? - , T' Z0 ~5 N# Q. h2 s
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
/ P1 ]8 g$ G$ d- u. H5 P* k -
" A& m, W' ~/ E0 w* S+ e+ ] - Dim base_pt As Double() = New Double(2) {}5 u T! V# W9 A" ^8 f& V7 w
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null! r$ b# k' L% X4 z* }# [
- Dim response As Integer = 0' }2 P) X+ S3 z" i8 t. J, S& Q7 y
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
- g3 g/ ]) h8 Q1 P - # ^( }* w; \& |% [5 ?0 C! ~* n
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
4 n" p" c4 c/ J, m* { - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)# T8 ?8 N$ ^) q/ `
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
% h9 `) p1 I$ j, f& e/ d -
. y v$ [) @" W0 ?; y) o$ j: j/ h - Dim pointLocation As Point3d
* s4 y" w5 K z0 I - pointLocation.X = base_pt(0)
7 B& {- G e! d9 o- U# s+ v - pointLocation.Y = base_pt(1)
4 O; l5 K; V* C# F) r( P* c - pointLocation.Z = base_pt(2), v4 D3 a i; f
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)+ s, Z/ T( s, M8 x: I& ^
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible): s4 @, F. V/ U' D& c8 l
- , L% K5 S. w' j3 w
- Return response8 m% r/ @) r" y6 W; o- Z4 }3 t
-
' Q* ]4 T2 b% ?( b - End Function
! J' _' ~! x7 t* y: T7 i -
7 f [/ v* l9 Y, G2 O- m! ~+ l - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response7 u/ p' X X0 M$ p8 e7 \6 Z) D. b% g
- 6 i4 V# k8 j L- G
- Dim selObj As TaggedObject
0 ]& p+ x, j% m - Dim theUI As UI = UI.GetUI
- r- j7 M/ {1 l+ G7 d0 p - Dim title As String = "Select a Point"* s4 {9 @0 w8 K4 V( _
- Dim includeFeatures As Boolean = False
- Y9 h. W" A% {7 k! { - Dim keepHighlighted As Boolean = False
/ ?+ V- }7 X" G* q/ w5 J - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific" @0 H: F( B& e! y$ D
- Dim cursor As Point3d+ J& R6 f7 _6 e) B i
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
0 J$ Q# }! r4 p4 ` - Dim selectionMask_array(0) As Selection.MaskTriple
7 _3 Z3 d+ m1 A4 | -
& V* \7 [0 b3 o- V; B6 V - With selectionMask_array(0)+ b! O& N: g& q
- .Type = UFConstants.UF_point_type" b: [) P) M9 E/ P4 j) k
- .Subtype = UFConstants.UF_all_subtype1 N6 k* M8 K \. D
- End With
+ a' R t% R7 u7 c9 Z: V* E -
3 _# h z" s p; w/ u1 r - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _% p% p- z) o" R; {6 v- [% K
- title, scope, selAction, _! S) j, P3 \* j7 U% r
- includeFeatures, keepHighlighted, selectionMask_array, _
) S- Q, Y; x$ A4 | o% l - selobj, cursor)! d6 U6 q Z* O- D. I
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then' G2 O) w/ C. Y# A8 ?/ z8 L
- selPoint = selObj
( s& Q( a+ C% v1 p9 Q" R - Return Selection.Response.Ok1 g8 y8 X9 n' F/ o+ v; T
- Else
/ g+ L( W7 @: W- m - Return Selection.Response.Cancel) @9 r0 l# B% ?
- End If
4 N3 n- X" P7 o -
) _" ^: R4 J! b6 W$ ^% _ o7 O- d( q - End Function
" I; ~; ~: w6 J. S+ Y6 ^% M -
" d5 G$ P2 D/ ?* _/ F8 f - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double% y4 h) ~# U, s9 u0 O5 B+ L$ n
- 3 @& ?1 o% x+ l8 d
- Dim result As Double. N' z- Y/ T/ H6 W! L* g' |' K4 p
- ' `1 n+ ~/ H; c
- Try
. b+ a4 z7 F% R6 }* A- Z5 _& ~ - Dim nullNXObject As NXObject = Nothing
+ ^5 A# A7 d c* e- k -
7 k) v- w3 Y3 F8 w* l" f/ W - Dim measureDistanceBuilder1 As MeasureDistanceBuilder
' Q5 [4 s' N3 `# |! ]$ } - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)# d! `& Q s6 C. ~
- 3 P; Q$ V( |4 \
- measureDistanceBuilder1.InfoWindow = False1 M# l. f4 d |1 h2 R7 W
- # _% l$ S, X' W0 r$ D
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
3 }* _, ?" B) g9 z+ v) A -
) f$ g0 W* k' Q) J5 h - Dim nullUnit As Unit = Nothing
* d: G( R" i4 N6 }4 O, F% x+ ? - X5 k4 X i: N3 R. |, F1 O
- Dim measureDistance1 As MeasureDistance
% x3 ?, N y; m; m - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2) c$ j: k. L0 l: k
-
4 p9 J" k7 m, x: w1 s1 C - result = measureDistance1.Value
- K& D2 r/ |. t$ F. }- d5 m k - 6 H$ W8 w: Z( D2 t& \% u' d ]
- 'measureDistance1.Information()
' U; r+ h$ S+ Z2 }+ _/ ~( a -
" @! L. X( @. i3 Z% H: G( i - measureDistance1.Dispose()
* N$ h }! ^% U ?+ \; @: C- q' V - ( {7 [$ ?* ~ l# [0 c( P R' R* u+ T2 j- R
- Catch ex As NXException1 G8 V$ u* _ _ v
- MsgBox(ex.Message)
, l; I4 x4 N T6 L" d& [: {$ s - Return Nothing
$ r6 G. L. T8 d9 c8 B. Y& w -
6 Z$ e- C }( M& Z% C L - End Try; l- J* K0 C/ \' |1 \
- 6 `' k- j' y% z5 p7 V; j
- 2 p( h( y/ Q3 e( m2 ?+ z
- Return result
( S; n7 I k& j5 o2 \' c - 6 n+ `# \$ s( ?2 b0 w" ?* F
- End Function! _3 f" J9 w* S, m& V$ V7 k
- ) A, y" j$ F- \4 w3 x& w
-
2 V! f3 a- S: J9 S, j - Public Function GetUnloadOption(ByVal dummy As String) As Integer5 |3 K. V$ s1 E
- $ M. Q! ?! |7 k4 D+ j
- 'Unloads the image when the NX session terminates' o7 ?, D; ~* D' U, D0 x
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
! i- \$ a/ v7 @3 H+ e -
; T% `& p! ~( a1 Z# Q - End Function
" [) v. h( G0 N& n- ^ - ; V$ N# _7 q% A( Q( a$ j
- End Module
* r2 ~' L3 E/ M0 [2 ^! N& d% } - </P>
复制代码
8 [. L* z2 l+ |1 b |
|