|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
, d1 J: k) R& v) w. {. W) G5 L
NX二次开发源码: 装配中选点,并报告父对象+ b) @- q- L2 l$ z
- ) D" S( k3 A+ Z, K. R1 c: x
- <P>Option Strict Off
2 P2 l) } b9 |1 F) h - Imports System
: Q+ H1 p7 F1 M9 L8 W7 y. W7 u - Imports NXOpen. Q2 Z$ M4 G8 @0 P' H2 V' F
- Imports NXOpen.UF
' g% ?) L( K8 J1 J) ] - 6 `6 `- ?) b' M" Q( W
- Module Module1
: `9 V4 g9 W* k9 d) {6 r - E2 d3 O- l& p- c5 Y
- Dim theSession As Session = Session.GetSession()
5 P( s; T) k+ l5 f: i3 a' o# d( D7 I - Dim theUI As UI = UI.GetUI()/ w L% T. |& |: l" x s
- Dim theUfSession As UFSession = UFSession.GetUFSession()
4 {9 y( y+ x' G. b0 j- T - Dim lw As ListingWindow = theSession.ListingWindow7 k+ ] a) I- v
- Dim pickedPoint As Point
0 q7 W+ ~. m( o& {! K - Dim myModelingTolerance As Double
* m( m, x) ^& g- H - " O. i) F$ k$ O9 Z3 u( |+ @
- Sub Main()" d1 E I$ m8 n1 r+ M" h' Q& T1 g
-
/ ^- c) K9 Y& Z+ n* D3 _5 F - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
- P5 N5 I: f2 l4 Y -
! y) ?) u- b @! { - Dim workPart As Part = theSession.Parts.Work
5 c- a, ?9 v! K9 s/ ?; O* y- [. \! S - lw.Open()' P3 \- {/ w) {3 W5 v9 K L
- 5 w3 E3 W- v4 V" m
- Dim myPointTag As Tag
: i1 P x; r! }. F - # p1 @! @$ J3 W2 `$ R! c$ [
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then* ^/ a1 j( c' T
- Exit Sub
O9 I/ j6 s* f - End If1 I4 N0 r8 G4 q3 ^' d- b* y
- / A! }0 S; m! @2 O
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
$ l; [& m" b& x" B0 p) a -
; B9 i6 X' w' d5 }0 ` i! w. G3 W0 v - AskParents(myPointTag), A: w$ C' @: m, ^" x1 x H
- ) x$ U' \! ? @+ z! O/ B" b
- End Sub% K$ ^6 G: g8 S' n
-
O9 Q' P- f! y6 k k; L& ?& R - Sub AskParents(ByVal objTag As Tag)
* Y- [/ I! Y! }. n4 Z' X$ w$ o -
- ?# }3 A0 F& C# }0 e" R4 m+ W - Dim n_parents As Integer
* `- e/ d9 N; i8 d - Dim parentTags As Tag()
6 X5 g! T5 O2 ~. c \/ g - Dim myPoint As Point
, R1 r' G }7 Q/ Q( h - Dim myEdge As Edge% r% @: g" Y6 q3 K6 L
- Dim myXform As Xform
/ [+ ]6 x, y t( a2 i" k - Dim myTaggedObject As TaggedObject) p: C0 \- m& _8 g& e+ r o
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)
# B0 d$ j. Z+ Q( C, n4 D$ ` -
9 B! @- P: @! z - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then0 H& B& m+ ^7 l) _) U2 H
- myPoint = myTaggedObject6 m, F$ M# y* G# }& @' f
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
+ L M8 W, d8 P4 U* e - lw.WriteLine("")
+ A) |. X: k' U2 I. _ - * A9 o8 O- D+ @
- Try
5 B2 g/ q' |) ^9 D- [" {3 M/ h - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
2 {7 ~2 \# v8 X/ B; J; I9 F$ @ - lw.WriteLine("num parents: " & n_parents.ToString)
2 b* O# S, \* ?6 v0 @; a% G9 y - For Each parentTag As Tag In parentTags
5 K/ L# q; w3 k8 m& t9 b - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag) q2 |& z% }& ]
- 1 f ~9 V5 ^7 _" G$ Y
- AskParents(parent_object.Tag) Z3 n2 ^' c% Z' s* P. t
- |8 u8 b+ K( {2 a2 j
- If parent_object.ToString.ToLower.Contains("edge") Then: O- |8 }$ e8 g
- myEdge = parent_object1 U+ N! R+ k9 A H0 y8 O: m
- lw.WriteLine("")
9 [( J9 X% P# |/ _* m* p( H - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
5 H8 Q" \$ E6 P1 z2 C - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
, l; x) |- a5 J( o - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
& k) |; e5 k+ z f- c) j -
3 w3 _2 J: G* e6 {$ n: B% ?6 y6 W - Dim partTag As Tag = myEdge.OwningPart.Tag
; P& J! N: n7 [9 a- `$ m - Dim occTags() As Tag/ r" A% }/ I* E \) h5 f
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags); L) _. g. d, [
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)( H/ `4 C q/ c2 ~4 S
- For Each temp As Tag In occTags
% q' e: C9 ^% d6 J - Dim myComp As Assemblies.Component
/ D( i6 A% A: h& l* B1 a - myComp = Utilities.NXObjectManager.Get(temp)
/ p, b( c0 t* J$ v$ v - Dim myCompPos As Point3d' c" F4 ?- p6 _$ H1 q
- Dim myCompOrientation As Matrix3x3/ g0 [5 \/ e6 H7 M( j3 y
- myComp.GetPosition(myCompPos, myCompOrientation)( E4 B/ G2 ~, V+ X8 X1 D
-
! g1 E5 _- V k: ]+ p - lw.WriteLine("")$ o' p7 v. P4 g/ P! Q
- lw.WriteLine("component name: " & myComp.Name)
$ X1 n3 u, [: { - lw.WriteLine("component display name: " & myComp.DisplayName)
; k# h: k: B2 I1 a" K - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
& v9 f; f }* Q; a" _ - - c# @" b |$ h3 e( l% A
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
; T3 [5 R. m$ w4 H3 h9 h6 o - lw.WriteLine("** This is the component that was picked **")1 w& J9 L. q# f4 |( I
- End If
1 {- ?1 V2 J. t/ v9 M' L4 n: E! G -
4 r6 V+ \) K$ e: |6 g3 `6 I7 v - lw.WriteLine("")& x: I1 s/ m2 l4 ?8 `; S& A) T
- Next6 @' ?& Z* d0 a' O
- 1 A+ q9 W9 i8 Q2 h$ m, C0 F6 N
- lw.WriteLine("")
, ?* Y. F( D V - End If0 f' S+ [* y* ^1 X
- Next
: d3 B9 Q, K2 G) [ -
2 W" f2 J1 d0 v, S( v F; v - CaTCh ex As Exception
H8 X r0 Y# o# {1 j - ' NXOpen.NXException: Current object is not smart' r2 }* [* R$ x4 k* `/ f5 P; X
- lw.WriteLine(" Error: " + ex.Message)
8 S. u- y8 O8 j9 p; u/ Y$ @& h - lw.WriteLine(" " & myTaggedObject.GetType.ToString)9 m0 o9 n' I; W8 G( w' D/ d! H2 g+ Q
- End Try2 {# ~( D; b! q; U( `& T
-
% B. h } \) D/ E* D2 M; u: G- N9 C" f - , G$ T' X/ R9 g
- End If7 M! H/ u8 F0 p
- 0 ~0 [1 N& U# w0 Z
-
) i: t) v1 T: z/ `) \$ _4 P7 { - End Sub: l3 g5 w# w& v% s1 Y
- 4 u, v+ i/ w. t. C0 g
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
5 G6 @' o/ Z7 p# s1 e- B( F9 O - % N6 t: d+ [) ? d5 E
- Dim base_pt As Double() = New Double(2) {}# Q6 d: M! O1 {& \) u( v" a
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null0 G9 _' P0 V0 a9 a: ?% S
- Dim response As Integer = 0/ V1 d% l8 a( W4 Z
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt0 N( ?7 w, w! v9 p6 _6 x) o" |2 D
-
, F' B, |7 ~! i+ i- }1 j! X$ H - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)/ ]# J2 f' ?2 s! @' J
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)4 c2 h& v3 ]& V- Y, o* j
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)2 M+ d4 z- ^# e& P) f6 _
-
( ~" S. F# r2 W: x% U - Dim pointLocation As Point3d) P0 ]% D4 h3 W, R& b
- pointLocation.X = base_pt(0)+ |: `& a9 y' h) b w
- pointLocation.Y = base_pt(1). g- L7 a* d$ z9 x! R
- pointLocation.Z = base_pt(2)
( q# Y- B5 l" c$ N7 e; u& t. t - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation). R" v3 D! V2 p
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)/ x& H$ Y0 G1 w s j' b
-
% D/ X& k/ t& @0 u1 w - Return response
5 a5 L+ E3 O( t7 B- {6 R - . ?* d. m; _0 i: z
- End Function
$ o o5 t8 F5 ]4 D1 o3 S - 8 d- s8 R0 P, q |5 \3 M- _
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
! u# ]1 |' ~/ K5 Z v* {& r -
2 q% m* a1 k% x9 c7 v - Dim selObj As TaggedObject
# h2 r4 c7 }4 ~$ E - Dim theUI As UI = UI.GetUI% r. b9 b; c# e2 b% T' |
- Dim title As String = "Select a Point"' h- M0 B/ A1 z+ M, ^: e2 h" k, x
- Dim includeFeatures As Boolean = False$ @) w9 s0 W$ m; f }
- Dim keepHighlighted As Boolean = False# H _6 N# }' N, N
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
9 e0 x M+ D* z# O0 i - Dim cursor As Point3d% l$ {( s6 w& I
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly! u5 d$ j& f' c6 [4 {' I) k- ~
- Dim selectionMask_array(0) As Selection.MaskTriple) f0 d( P. [. \0 x, y
-
" i ^/ k' W. @, L# s( l0 `7 t4 t - With selectionMask_array(0)6 Z" S% g1 c9 A& `+ `
- .Type = UFConstants.UF_point_type/ _+ u& ~9 ]/ J9 _. H2 g* F
- .Subtype = UFConstants.UF_all_subtype* i: `$ ~3 W! m8 D; Q" A+ y
- End With5 [/ N! v# e) \# |* Q
-
" @6 M- A* f6 L. j7 y - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
; C: `8 F8 a0 z3 } b; O u# e' n - title, scope, selAction, _$ @' M9 p1 {: O6 z" b! h
- includeFeatures, keepHighlighted, selectionMask_array, _, P/ X! A" q$ F( s! g8 m/ A0 L+ o2 f
- selobj, cursor)
1 X' Y S4 j& ?( b( x% [; U1 ` - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
' c' f3 M8 E" x$ p O. z - selPoint = selObj
. o: ]# _& j. p - Return Selection.Response.Ok
4 S4 u. f; N) B! J - Else
0 d; x* N& d9 [1 E# k# k - Return Selection.Response.Cancel. B! x! |% ?) u4 Z: l J) h
- End If
4 e1 q1 }- d; Z) y - $ F0 F- |& [1 g4 x/ w% L1 x
- End Function" @. r, j3 Q. Y1 k8 s
-
+ p/ m% E8 U# ?- D# x - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
% C$ l0 ?6 n0 f - , r* w: O' b+ a. }! s
- Dim result As Double
# a8 X2 A3 {& N. P0 i) H - - t! s% j. U4 h8 k1 U* S
- Try
$ e) _: m5 R. I+ z1 M; u - Dim nullNXObject As NXObject = Nothing
8 E% |+ i$ E! R, Q, x! e# h -
5 j' B! e/ G# f7 z( ~& d - Dim measureDistanceBuilder1 As MeasureDistanceBuilder8 \) ]' p+ a3 @! r6 C/ C
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)* b& D2 x4 d+ k1 V1 a
-
, g9 y6 ^3 ~5 I6 d$ j, ` - measureDistanceBuilder1.InfoWindow = False# }0 I; ?9 `1 U! G A
- 5 b/ j" y6 n; \& S Q! N
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
j9 V" Q ^8 M+ j - ' {5 L7 [' G" l8 d8 I3 l: m
- Dim nullUnit As Unit = Nothing9 y# d* j0 e( B/ ?8 ~ t, r0 W
- * \- ~$ Z( B) |0 A
- Dim measureDistance1 As MeasureDistance4 u- I! T/ F _
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)" ?: C1 c8 O. @' ~1 X" I f
-
6 I+ U" T+ \7 Z% m- l C; x - result = measureDistance1.Value8 e- \) v0 P" D& M; V9 @
-
$ Z/ J# w% [# c7 J4 h) S' { a: x - 'measureDistance1.Information()
2 w; Z u+ f& ~2 y% G$ @: m) C - q% f5 o$ Z' D; _; ~& U8 s# {
- measureDistance1.Dispose()
1 c" {0 L6 E3 J, H5 T - " @% W, W3 i6 z9 g, [
- Catch ex As NXException
1 J( {% w* j; W' i3 v; f - MsgBox(ex.Message)
b8 R& t5 C1 ]* h. w3 Q2 v0 s2 u - Return Nothing
5 `' r8 G, e. v -
. ? p, C9 T: L4 D7 F - End Try7 u% m$ g7 [2 k9 W9 X h
-
7 J0 |, q0 ^8 ]7 r1 E b3 A - 3 b2 h/ m$ P6 F5 x3 q: S2 V6 | N
- Return result
# N- G1 d! T0 U" I5 e: g, V5 F4 c* p -
& p9 R$ j. J( f, H# M$ @ - End Function& g, j* Z/ P% z- ]/ u; ~/ \% Z
-
' R7 G9 l& ^9 N' W1 @7 ] - # e3 u2 n! ~" ?! p( v
- Public Function GetUnloadOption(ByVal dummy As String) As Integer1 _# U; @0 q2 K$ E
- 6 j5 b4 P) @0 _( Y7 K/ }
- 'Unloads the image when the NX session terminates
: `4 ~: k) ?4 l/ o5 ?4 @) B; g - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination! j4 Q1 K. e3 o: H+ O7 t# b/ D
-
w9 s% a) {2 ?! P# h4 { - End Function
# C/ i8 J4 U% R/ {9 c7 F$ o -
M* r% V6 T9 b( a - End Module
9 W8 d( k8 T# e! Y7 y( J- j5 c H - </P>
复制代码
9 D- k9 O% E l- S! T- w( M |
|