|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
7 G5 u5 n7 o$ Z( V+ M+ J
NX二次开发源码: 装配中选点,并报告父对象
z8 g$ B4 f; F/ \! i( ]- 0 s# \+ P8 }- M2 X
- <P>Option Strict Off3 ~& }& g$ ?9 y7 A9 _
- Imports System, ?% g0 M. ~! [, c# f/ y- @
- Imports NXOpen! c1 s" |& x3 o& ^9 x6 \
- Imports NXOpen.UF v' B8 U2 ]) w3 }
-
) {8 |) v5 k) x) j$ \; Q - Module Module1: e4 Y; n3 q: O1 | `3 G
- - M( z" x0 f/ V- x/ {! c3 `
- Dim theSession As Session = Session.GetSession()
, I ^: P6 B( ^- q - Dim theUI As UI = UI.GetUI()
, I" A' f6 B; X8 T- }( d - Dim theUfSession As UFSession = UFSession.GetUFSession()+ ? w4 E& ?0 |( z- G' G
- Dim lw As ListingWindow = theSession.ListingWindow4 ~; s. ]. j3 d- M; w( w
- Dim pickedPoint As Point
3 R3 C1 R8 B! f7 ~$ ~* l& g" | - Dim myModelingTolerance As Double! W3 B E7 d, G/ c" }
-
& W& V) @4 ^0 ]2 e/ d) M/ u6 _; G2 Z" y - Sub Main()! |9 \" d9 ?' U9 k2 U e
-
( H; |5 g7 Z. K) C- ` - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)' K9 r' D5 P8 h4 G* [# E
- 7 H2 \( |' |- w( c0 }# s( F9 }
- Dim workPart As Part = theSession.Parts.Work% G7 s( _% g* ~
- lw.Open()
7 z) W/ c7 s& b+ d6 z( T! ^ -
6 i; v8 p0 c, r7 @& G - Dim myPointTag As Tag
* {0 v. s, R. z% _! X2 q, Q -
3 M8 I* @7 @) v7 @# \8 @) R - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then- I! h4 u& C$ S, k5 R8 u* l3 a/ e
- Exit Sub4 b! T1 v( j/ S. [4 I% L( z9 [6 x
- End If
0 j- C" F1 V' E! S - 1 V* A( H: Y, ^: H0 X
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
+ j6 E% N+ {% X+ e: _+ ] - ( ~0 k) V% X, q0 F( g( h. K( ~
- AskParents(myPointTag)
$ j) X# S4 q+ j% ?3 ^ - 5 V9 b+ I( x9 e6 n# y: l
- End Sub
' z/ c0 s1 l" {! P9 G2 _ - ( m9 p h# ], X) {
- Sub AskParents(ByVal objTag As Tag)* S( ~# h+ J8 g3 B8 w
-
) j- }% P- r$ ]4 t% G - Dim n_parents As Integer
L3 ~: a$ G0 c* w1 P$ H6 l2 } - Dim parentTags As Tag()
- ]; [/ n9 C$ {! n1 ~ - Dim myPoint As Point" B5 B, |" @: N. f, Z% J
- Dim myEdge As Edge
+ [: U3 R6 R. i) b! X& @ - Dim myXform As Xform
7 `* ]: g6 m3 p( H1 w - Dim myTaggedObject As TaggedObject
2 K6 x# A. y' J2 m& v( h - myTaggedObject = Utilities.NXObjectManager.Get(objTag)# k# ~7 j6 K( |% c: x6 A
-
/ d( ~4 m% y0 m - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
1 R: }4 i- G6 _% q - myPoint = myTaggedObject
. L# f' U9 c$ H; G( i - lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)7 }8 g$ w& a' ` F2 j5 I
- lw.WriteLine(""): T' X# i! W7 S( f
-
/ u u5 y8 `5 z F* h" R - Try
1 H, U( [+ k; ?0 k! X& R' J' S - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
' w/ t" |% A* Y% v7 ?" S8 x0 K - lw.WriteLine("num parents: " & n_parents.ToString)
" M& W. V4 G p/ f9 Q - For Each parentTag As Tag In parentTags
i' Y1 P H! G# ]. n$ I; r - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
" O# F; N# y8 i# N+ H: a -
; l9 j! R3 B, s2 V. N - AskParents(parent_object.Tag)# _* m4 W, T( B5 J! `, k( J
-
9 t# M8 f6 K: r6 c" O( | - If parent_object.ToString.ToLower.Contains("edge") Then
+ I- ]+ E' D C% o1 w. Q - myEdge = parent_object
2 Q! y7 U$ l1 l8 [ - lw.WriteLine("")
: R: b; w5 {& `5 O/ s - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)1 O7 { L9 ]" v# E4 K
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
j9 c1 O& _) k- j# W9 I2 ^ - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)! b! v* _0 j U6 N. j" B
- 9 R. p- p9 Y6 p
- Dim partTag As Tag = myEdge.OwningPart.Tag
2 f, s# [( E: G/ } - Dim occTags() As Tag! t. y5 d( x: \5 ]0 ~
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
; e7 p1 E$ m) o. ?2 z2 \# b - lw.WriteLine("number of occurences: " & occTags.Length.ToString)
, r! B6 W2 Y$ S- v' [7 E/ B: V7 } - For Each temp As Tag In occTags" w/ e6 \' X' U7 C3 ?
- Dim myComp As Assemblies.Component9 v1 l0 u [) n* o- I) ]
- myComp = Utilities.NXObjectManager.Get(temp)
) {' [( y v( H$ ?) |* M; v+ D - Dim myCompPos As Point3d
3 t9 F( G1 G" T- A+ m3 ], | - Dim myCompOrientation As Matrix3x3
; O: P* V5 o+ W' p G* y - myComp.GetPosition(myCompPos, myCompOrientation); u% f9 h5 m, M* J
-
5 A$ r5 ?3 d( H# s* n - lw.WriteLine("")' d% o& c! l: [& B$ S9 P# Q5 J
- lw.WriteLine("component name: " & myComp.Name). n( d8 J6 }5 U. v0 w' m" R
- lw.WriteLine("component display name: " & myComp.DisplayName)
+ v& Y/ u; s# c0 g$ Q - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
* n/ ^ d3 Q$ Y2 {/ ~$ Q/ q0 U6 f - ! o7 T1 j0 C, r n( d( P
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
; D/ y+ W# Z8 Q3 |; F - lw.WriteLine("** This is the component that was picked **")
/ [4 U r8 x0 s - End If
* L% J; U) s- p0 d - 9 ^( y }1 h1 o; |& D& _
- lw.WriteLine("")
8 T6 x8 i5 x& N3 _ - Next
5 \& n" }& ?0 p -
2 R6 B" o {) P1 _4 s; S - lw.WriteLine("")% P& z4 W0 T. |+ f, D
- End If
2 u: S1 r- ~2 w4 X# e - Next
2 G$ {! _! y% W' }! L' ?! d- Q! l9 C -
$ ~0 U4 A3 l; @8 r' ?, z& m4 O! H - CaTCh ex As Exception' N8 K2 o: H, _
- ' NXOpen.NXException: Current object is not smart
, Q+ V5 L2 |/ L, _, c - lw.WriteLine(" Error: " + ex.Message)# _6 _* r( I$ N8 ?' d3 U
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)
& Y" ~. L% W6 ?( P% ^0 h1 _7 x2 o - End Try, `- a; C$ v4 z: ^9 }) @
- 4 w7 |0 c3 g$ d$ V$ j% l. O5 W
-
+ m# ? y; N a t$ d+ B - End If7 `9 L2 n( U% _& r% |+ \
-
. ^8 \- V' J9 ? c$ a+ i7 E - ) h9 x/ F+ F% X& t! {$ I
- End Sub
8 Y: a8 y( N1 d9 g8 R' L( U- Z P" e, T - 5 v$ M6 b( b# _4 a
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer: `4 X( A# }2 K, F5 I5 J0 g
-
/ A0 u+ `* `8 x; ?$ R/ `* v - Dim base_pt As Double() = New Double(2) {}% ?* E9 ?8 C9 [4 z1 n( W
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
6 n' M& g2 J& d% g- q- M - Dim response As Integer = 0
4 v! }3 R+ `5 H8 o- D4 ~; C - Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
% o% S! D! T; ~0 F, @ -
, l7 P) y% Y5 A6 T9 U - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
9 \" I' S) K9 K- Y" I# ~ - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
8 f2 @7 I4 v* n. \2 R7 O* Y1 L7 \$ { - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)8 K$ e% M2 P+ |! \$ O# a
- 2 w2 }$ I+ l" S3 j; t
- Dim pointLocation As Point3d# H$ M. s% y0 ]* ?- p) w: d
- pointLocation.X = base_pt(0): M7 b4 N$ u W G; M$ g! v
- pointLocation.Y = base_pt(1)
. D. H5 q3 B" B - pointLocation.Z = base_pt(2)- C }8 r0 ]# ?( z2 J, p; n1 m
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)( L% V' X' P6 z& p
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
7 r/ w4 ]6 ]$ l9 b* S7 T9 V - : H; g6 N) d) Q' w8 |. v% s
- Return response2 h* l# W, @( T* U* ?' @
-
8 f! U3 e( [+ _ - End Function
/ I3 A0 k( s9 U r0 T: H' _ - 4 n& V9 F; o4 Z! {6 b- V
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
2 {& p' }, N/ B& U* p - + }' _0 M H, J6 K6 H9 P/ K
- Dim selObj As TaggedObject- |/ ^9 w' N2 j" `+ A: j: v
- Dim theUI As UI = UI.GetUI- t7 n1 N* X9 U4 v9 U$ S- N
- Dim title As String = "Select a Point"9 W* \ |3 h- Z; K( E" R
- Dim includeFeatures As Boolean = False3 U+ G5 G4 j1 Q. y
- Dim keepHighlighted As Boolean = False& r' ?) ]. `6 R: u4 H
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
+ }! t, [* P1 g - Dim cursor As Point3d" H. m' k* g% a9 k, K, g* x7 ?3 d
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly$ E" a7 D- ]- [* i) ?, H) |* z
- Dim selectionMask_array(0) As Selection.MaskTriple" {/ a, ]0 H+ v3 v
-
# i8 @% Z# V2 r1 V7 F6 J, O$ o6 X - With selectionMask_array(0)+ t' b9 W5 _" G# i
- .Type = UFConstants.UF_point_type
6 B2 |0 T4 d, b4 o - .Subtype = UFConstants.UF_all_subtype. Z) K* s# q) u& V# r4 [, f0 c0 N/ {
- End With( m3 J8 `! ]# p- H7 Z$ |
-
4 C& H4 K' N) x- n4 E0 v - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
1 `. u" S" P; ^- E - title, scope, selAction, _; _/ b9 Y$ T* r4 a' U4 ?0 S1 F
- includeFeatures, keepHighlighted, selectionMask_array, _
' P' ^: K( [# _, h5 n5 a' i4 R - selobj, cursor)
% k$ i% ~: @: X' Z0 Q! N& G - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then; G0 [; o0 p1 c; k6 s
- selPoint = selObj1 l! p# \- \; t. ]# X! ]
- Return Selection.Response.Ok# N3 C7 x! d1 Y0 y; D4 U
- Else; A2 M' S" y% l2 _
- Return Selection.Response.Cancel
" |. I& v) f2 Q5 Z* O5 X) F( g- Y - End If
! Z$ s+ w3 q2 K$ {) | -
4 _$ P! L% B$ R - End Function8 u- Y2 h' D+ L( I3 E
- / o) t; C$ F' l7 u
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
5 _6 z. z/ @5 _# n -
8 |/ u. @9 c- b1 R- `9 I$ ? - Dim result As Double
- Z9 h5 Y+ r. r" A% Y! f9 I9 Z3 A - 8 ~1 F" p! U2 [- \3 A0 n
- Try
8 T0 b7 z6 l1 a9 ^ - Dim nullNXObject As NXObject = Nothing
1 w( r' T# t+ A: H$ v i4 @9 X7 H - - s) J8 {, E' |* N+ ]
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder
5 h% |$ a( R N - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)3 P" n3 Q# W! u, S3 v8 \$ D
- K T. s8 ^" a, h1 u1 V/ e
- measureDistanceBuilder1.InfoWindow = False
! R; O5 i2 X( P5 \/ v9 N R -
/ L7 S+ R: d+ k- t/ X - measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
5 t) e0 H2 Y8 Y4 Y0 h( l u# n - + L2 Q- P o2 V- s6 q
- Dim nullUnit As Unit = Nothing! f h+ K+ L: j! G9 n0 Q
- ' n3 h! S- x* J) j6 `
- Dim measureDistance1 As MeasureDistance
* C8 X$ [ j' A3 g { - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
' _, U0 R% T# g4 D& H- ] - 2 o- `; I `2 e6 V* S/ F' T
- result = measureDistance1.Value! y' K+ k! Q) _" Q
- 4 u/ l8 `2 q* b' I
- 'measureDistance1.Information()2 v# q! H; K+ f
- ( f+ X5 `, s- Y* N0 O
- measureDistance1.Dispose()% q6 a, V( _' T2 h
- 3 j8 j1 _6 V0 ^) p2 `8 T3 R- T
- Catch ex As NXException
4 Q" {- Q* |0 Y, d- H% T - MsgBox(ex.Message)
) W! j2 b- `& ]% M: l5 a - Return Nothing
' {; x! o* e1 w3 V0 U -
, G7 r$ U; B( @* J6 u0 G# W - End Try9 Y8 k; B, `" [- p; N
- * D, v, j( d; A+ _9 [% _
-
8 l: h# c& s: x: I0 P - Return result
5 f5 f: d5 m( Q/ X8 E -
5 O' @: A2 W6 Z' S# N3 n$ x - End Function
# y6 i% `+ q- q - 2 N) u$ z! I, r% ?
- 2 j, h% J {: n. V n8 m
- Public Function GetUnloadOption(ByVal dummy As String) As Integer; x$ p8 c3 d! |$ Y M+ B1 V3 g
-
* p' b$ O7 X" m/ C - 'Unloads the image when the NX session terminates6 C8 u/ K, t1 c: F9 C
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination# w% [+ f' ]# _( H4 J2 a, y. J
-
]; G& A8 O1 h+ D6 Q: b6 j+ b - End Function
2 |2 L. V8 |! x- I -
" s- b9 s2 o/ |1 R7 L. I+ a - End Module
2 c: U- a; v. {- T$ b- ]; p# F* j - </P>
复制代码 ) C$ A' ^2 a. o/ ~3 |1 I1 K0 c" C" X
|
|