|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ _- P8 J; E& P3 C9 l$ T2 ]2 Y
NX二次开发源码: 装配中选点,并报告父对象
5 E& S9 x) g* P- N. N3 ^3 W' \- 9 b0 T% ]# j9 J& d$ M2 p
- <P>Option Strict Off6 g" r9 ~: d7 W$ D7 E$ k0 F S
- Imports System" T$ _4 M$ {2 i3 q
- Imports NXOpen
4 ~1 W! t2 g, N- T, \ V5 ` - Imports NXOpen.UF
. @" Q! {/ _9 e @8 Y - 2 q4 R% {3 h8 c4 s+ o* j$ ?
- Module Module1 |' X- A7 i) W8 Y5 u
-
q1 e, V! x' V' B9 q - Dim theSession As Session = Session.GetSession()6 x! H ]5 j; ]# c6 ]
- Dim theUI As UI = UI.GetUI()
8 N. x1 s7 E& w: b, a - Dim theUfSession As UFSession = UFSession.GetUFSession()
6 [' B8 F; ~, V) Q3 I' ~) h - Dim lw As ListingWindow = theSession.ListingWindow
& e' F U& H/ d2 r6 t* r4 [ - Dim pickedPoint As Point5 g' p/ p+ i, K" h- m$ V# N
- Dim myModelingTolerance As Double
+ x) E2 \0 `" `# T% H+ Q9 _ i2 X - ( I$ i) g9 L! A% n- \( K8 a
- Sub Main()0 s7 ~. o. a/ {4 K4 Y7 x$ L
- $ t1 o$ q0 ~9 [* e
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
+ e2 F5 Q; f2 y) I) } - ' H" b) t% ~9 ^
- Dim workPart As Part = theSession.Parts.Work2 c6 D( @' ^- ?, k W7 M# T: M; W
- lw.Open()& D3 @$ L+ r$ f+ |' ^8 L! M
- ' t& O% _4 v! p: m
- Dim myPointTag As Tag
1 f4 k) W: c5 q2 T: H - / w2 v8 P X' q; @# i
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
- m+ V) F8 \& X3 y: K! l7 u - Exit Sub( L! w( s0 L5 j
- End If
/ t2 E2 s3 e" x; F( l! I% ] - 0 U9 B! L7 n6 n* m$ Y
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)4 C3 L* V2 s, v" ?
-
2 c7 v; a7 C3 b n: G4 F e* `, X% _ - AskParents(myPointTag)* ?+ C: Y- e; x$ B* c, ^
- " V& V1 P: \. E' F# d7 e
- End Sub- A0 C5 T+ T# O- R* c& l7 } p
- : J0 B1 r. e6 [$ U
- Sub AskParents(ByVal objTag As Tag)" F( W3 V7 X9 g
-
o7 I$ g4 t+ b& q4 Z - Dim n_parents As Integer
+ H0 N) y3 U+ H& U5 L - Dim parentTags As Tag()
7 l' w% g4 m, \6 ^2 f - Dim myPoint As Point* P* h& @! n6 m! p9 ^8 y& l
- Dim myEdge As Edge! p. w% R. m6 J, C6 \ f
- Dim myXform As Xform
5 p8 W. U) q& U/ N) ?8 ` P Y+ H - Dim myTaggedObject As TaggedObject# t" P4 T$ S1 e
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)+ Y! E" T' P: C U; Z
-
( }: ~; S, e5 j6 J2 U+ z - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then, K+ {, _4 R# C z# Q
- myPoint = myTaggedObject
% N7 G& x5 ]# [' b8 B x/ u" u - lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
) J0 S$ @/ s$ C9 n6 t& J: ^. }! M - lw.WriteLine("")& G6 i& ]0 P% m6 z
- 7 s* j: A* f8 X) y4 ?
- Try
* D# L& D% o: Y% b# [" h c - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
1 A1 w6 b5 e: t0 R) H- n - lw.WriteLine("num parents: " & n_parents.ToString)9 S5 e4 V* P1 c$ o* p% X+ r( H! v
- For Each parentTag As Tag In parentTags
/ p9 ]0 M" R. q# Z) L, h2 x" r - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)1 s% D3 ^3 m( c+ i+ Z: p) B
- ; b1 N( A# o, w' _# k
- AskParents(parent_object.Tag)
( s2 b4 \8 q+ x( a- ] -
- F. f, u% Q2 ^/ w- r - If parent_object.ToString.ToLower.Contains("edge") Then
: d- Q: O; I9 n' R) c - myEdge = parent_object
/ `# V \+ {' v - lw.WriteLine("")
" i6 `5 d. W" T" m) Q8 i" m! \. y* a - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
2 r2 f8 v$ n* u6 G/ \2 G+ [ - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
& U* l% D. X& c - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)( D8 r% c6 G( U( W- I0 S
-
9 f. l* H8 @* g4 I# M$ T6 r - Dim partTag As Tag = myEdge.OwningPart.Tag
7 ?# u! B! @0 F( _* s1 i - Dim occTags() As Tag6 q* X* N7 s% X( F
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
- o$ f5 ?" M5 y }$ ? L - lw.WriteLine("number of occurences: " & occTags.Length.ToString)
( t$ q/ }9 u/ H9 @% B/ D! H - For Each temp As Tag In occTags4 ~0 D3 n p n" T. t
- Dim myComp As Assemblies.Component* |) _6 d+ m8 ]( f# o# D
- myComp = Utilities.NXObjectManager.Get(temp)2 l, M9 z8 v. n
- Dim myCompPos As Point3d, X8 h6 e: D# W
- Dim myCompOrientation As Matrix3x3
" g: S% i0 {4 `6 [$ Y! k6 j - myComp.GetPosition(myCompPos, myCompOrientation): {" w" Y" s, `; i$ V
- 6 W5 i1 j9 a' ]4 Q# L
- lw.WriteLine("")
1 M, k5 q; E/ F: w8 N - lw.WriteLine("component name: " & myComp.Name)0 o& H# C9 y! j8 ~* \( C- t$ B: g
- lw.WriteLine("component display name: " & myComp.DisplayName)
# I T/ N, w' `# c# Q$ C9 G3 V - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)* U1 ~+ M% s0 i/ w
- 7 o$ _9 B& S& d4 ^- K
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then# y/ Z( x) B# Z- R' k% O: K
- lw.WriteLine("** This is the component that was picked **")
! u: Z+ f" V0 ^1 X2 u8 p( ^; e - End If
) Z' M7 ^" K' `/ |9 ^5 _ -
4 r" H: i$ E. v' `0 o6 h. G( J( v0 o - lw.WriteLine("")+ N( G0 C- P1 _- Q Q D5 h/ i. J
- Next3 _; {# C5 U: N ]
-
8 _* l; n, C; m - lw.WriteLine("")
' V4 ? b' M1 B& L8 P: d% Y8 F - End If
8 i1 y2 ?+ v& h; V - Next
# l9 B- K5 K! X/ F4 l0 y0 e - $ g- ^* ]7 A7 M# f
- CaTCh ex As Exception0 k( D! f% S5 u# y0 I! [# t1 A" |8 X
- ' NXOpen.NXException: Current object is not smart8 P+ g; K1 u% z `: G$ M0 o
- lw.WriteLine(" Error: " + ex.Message)
+ h3 \$ A# G) c# A - lw.WriteLine(" " & myTaggedObject.GetType.ToString)3 g& k+ t/ y. Q4 {) @
- End Try
7 ?6 y# n6 n; }* X; H# y! g - 3 k9 j2 |: R9 A4 p6 a, A/ }/ O1 o
- 4 y, {7 I, V g) }& I; C
- End If
; q$ O: f0 _, B2 \# a8 K -
8 h( E# Q# ?% v6 L; w0 N- o - ' l4 i) r/ w( n, c4 k3 V
- End Sub
; e$ `! v. |3 c' ]; j* K* I - 5 F3 X' a& }8 t& Z
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer) o7 ?4 ?" [. T
-
! V4 f' H* _/ ]9 X - Dim base_pt As Double() = New Double(2) {}
) t" V; N; }4 X2 D: o. P3 q - 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null! a& K0 N; `* E5 K! g6 [( ?! R
- Dim response As Integer = 0$ c. c p6 q3 b- ]: Q$ q
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt- h6 n# `+ c a1 q0 d
- ; N. K& h- Z I4 @; R q9 ?8 P) T
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
5 E2 q. V' V* J - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
_2 |0 `# U5 g1 o, E - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
" F: D B. N! X# d1 J, S2 _' R1 U - 7 D: P0 {) S$ K
- Dim pointLocation As Point3d7 w& G: [8 v$ M# |
- pointLocation.X = base_pt(0)2 k! y$ I6 ], i9 N+ ~
- pointLocation.Y = base_pt(1)
5 X8 w- C8 s4 ?! G% D - pointLocation.Z = base_pt(2)
8 W* K: \3 _" [6 w( [( v - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
$ {; ]7 d8 s2 m' p3 h - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)! @# ~& j- ^) U5 ?7 |3 \
- 8 q1 ]/ p) n* K! `6 V- K1 P. P
- Return response$ _/ A3 n6 V0 r
- & X" m) t# d9 C/ W
- End Function
s+ c" g' S8 j) `2 Z - + O: @+ ]+ M w
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
; a: ^1 W4 M% z, g: [ - ! E6 j7 a6 \8 S; a! Z0 g
- Dim selObj As TaggedObject
9 Z. j* m/ ?1 p: Q% [1 v - Dim theUI As UI = UI.GetUI
- ]4 U& x" Q5 o- S - Dim title As String = "Select a Point"
8 i2 s' s( u( {7 g1 F - Dim includeFeatures As Boolean = False, c8 ^6 M" Q0 j+ @$ I
- Dim keepHighlighted As Boolean = False8 E/ r# i7 e" u. u# H
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific( f& A, K" Y1 y( n
- Dim cursor As Point3d0 L2 b4 z+ e1 \1 r- a. p/ T
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
) d4 _) Q$ [, @( b8 o# ] - Dim selectionMask_array(0) As Selection.MaskTriple
# Z: h1 D6 S* c. @. n4 h -
! R+ u) b/ s3 ]# _6 D" I4 o! [, q - With selectionMask_array(0)
; @; D- f- l2 B6 h, n - .Type = UFConstants.UF_point_type0 _; r( p9 v5 s/ H1 |
- .Subtype = UFConstants.UF_all_subtype
0 k( t4 _8 v- \- E; A+ X7 ^6 T' K - End With6 \$ ?# y1 U6 U! M3 j$ A& C
-
5 A( p/ D0 C: \# N - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
2 ^) s8 R. t5 y# n, ]" \ - title, scope, selAction, _
6 C4 J8 |( S" s8 ~9 C0 I - includeFeatures, keepHighlighted, selectionMask_array, _5 p! \' b& T, f" N h6 h- i
- selobj, cursor)
. U% U3 F/ i( W, P: w - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then% {' o" ~, n) v5 W. f$ u" ]
- selPoint = selObj
' E: Q1 G' ?6 C+ Q- I - Return Selection.Response.Ok
/ }% P7 m7 `5 P; K; \6 \ - Else
E7 ?( {# e( z - Return Selection.Response.Cancel6 G3 W; y# l* f' b8 D
- End If, z8 j: R4 Q1 c8 M
- # C1 ], v: s' a# y' c8 W: Y5 q
- End Function
% I2 ^* T e* H7 W0 D -
5 E0 r0 z$ j' D% _( b0 G8 |* Y& S - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
: S' X2 l( z- N1 A7 R" A - 8 ~4 O6 P: {. K2 L6 V7 Z; N% [
- Dim result As Double2 g3 X. `/ j. B' ?5 u. B
- / ^( S% f1 P: k3 h: [' I6 A& r
- Try, { K3 I1 {6 k1 ?9 _6 {
- Dim nullNXObject As NXObject = Nothing
9 P/ A& a4 v" L- W' }: O -
G; a- s3 M; ~* O - Dim measureDistanceBuilder1 As MeasureDistanceBuilder
0 y- s X7 C% u' u - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)* a7 g' z _* S: L1 t% [5 Z3 e% \
-
3 O8 O" k" [5 y. W/ H - measureDistanceBuilder1.InfoWindow = False1 R$ L \1 k; D5 Y
-
$ e& X7 c5 G! x' D. [. V- B0 o8 n - measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
! s+ @6 ]: D$ ?6 A, a' J; D - ; U2 j; l* }# `" V) ^& p( B
- Dim nullUnit As Unit = Nothing5 o& i& [* l! N( X
- # `0 m. D! _. n/ @
- Dim measureDistance1 As MeasureDistance
; F7 U9 @: |, \. b' n9 ~4 t6 ^ - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
9 \( T- [! h( X4 e+ j/ r. s+ R - 0 x" ?2 C/ U5 X' h. I. g( A
- result = measureDistance1.Value) b0 W8 B( R# `6 {7 a
- 0 z* M) c& y7 M- r+ A g1 P9 B
- 'measureDistance1.Information() s1 a* l- R6 a
- ' n: f0 x( N2 e+ N- o m( a
- measureDistance1.Dispose()
. H2 m ^1 Y/ K -
4 b% i. L0 Q S8 O - Catch ex As NXException0 ~3 Y: k0 N7 X3 w7 j0 q& e8 \
- MsgBox(ex.Message)
3 T2 L/ |9 G: Y w- _7 ?7 {) A - Return Nothing: e( L t; [8 g* {. ^
-
7 [& ~# p" S8 U( e# H& f8 q7 | - End Try
j8 H: t; a& `1 g - 9 j; y+ n: g9 d, \
- 7 V9 v0 J; `- _( ]& F
- Return result5 `) Q9 P; b3 I R; j7 ~
-
9 U [, ]" [) r6 a8 c; e - End Function1 G: M; w4 f" p1 x3 B
- % \& T' i2 x% N
-
$ |) ]! d" z c e6 S* l( j - Public Function GetUnloadOption(ByVal dummy As String) As Integer
! ~# R! ^' G& `' u; O - 6 f8 n! V% [! _
- 'Unloads the image when the NX session terminates' x9 c; w- ?6 L# Z3 g
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
. _0 I9 y" I9 M5 d/ h" {8 ? -
2 R. F) P" g, B! L6 T0 c k! _ - End Function
1 }7 N) l: `& F" b+ B -
2 y* r: I( x- k5 m C0 g - End Module
) j( f6 D& Q* q+ i- A2 L; W4 I - </P>
复制代码 4 b' b" [6 y$ C& W
|
|