|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
8 j# t2 i3 u# ~& s7 WNX二次开发源码: 装配中选点,并报告父对象
; {- `# o$ J/ ~/ h; t- m- 7 L/ G, l5 `+ L# O+ H3 p6 |1 ]4 }
- <P>Option Strict Off8 c" ~7 O9 u# F" g+ a& I
- Imports System
- B* M5 i; F; }2 H1 `2 ~5 l# q0 g - Imports NXOpen
7 s1 T) T7 ]5 `. V& w* ~3 k) m - Imports NXOpen.UF& j. W4 G( V/ O
-
0 [# S* R' K' i" b# A0 i1 y - Module Module1
0 X7 E; V+ I$ e- \/ R8 m -
0 p7 g; Z2 z7 i7 m+ i2 r { - Dim theSession As Session = Session.GetSession()
8 {5 c; t- X, @ - Dim theUI As UI = UI.GetUI()9 L5 | J; f$ y4 c3 b$ L( z
- Dim theUfSession As UFSession = UFSession.GetUFSession()
) S* V+ k) G6 ?' x; p - Dim lw As ListingWindow = theSession.ListingWindow
$ u0 ] T9 t5 ?7 \ - Dim pickedPoint As Point+ y4 e2 P: `& v( E5 D1 e9 z, f$ q
- Dim myModelingTolerance As Double
2 E6 P& J' n8 u3 Z5 {$ m0 N - : g( L) }; x1 E) | U- x1 |
- Sub Main()
9 a% g# t# A7 o' z/ H -
% S) t4 _& v. M - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
( c* m+ }1 R0 f, M( O - ; b8 M3 N! j1 I2 h; i% W7 G
- Dim workPart As Part = theSession.Parts.Work: d6 O/ U- \) c1 ]
- lw.Open()
0 V* P7 z1 m1 f" o+ `$ k9 {4 Q( e - 5 h: o+ L2 a1 a
- Dim myPointTag As Tag, }% B% _! W7 d% ~4 D' V' o* h# b
- 0 N6 t" x3 R1 H3 \. k7 s% a
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
3 V7 g3 v$ |# B5 K; `. Q/ e1 W - Exit Sub& n- ?( {. J3 e, z' M/ S* w
- End If
7 Z9 D* s( k- v - ! I- Y9 a& r( S; e* H! e3 S
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
/ A7 B! _- f1 N. \( q! I3 Y. \- `, K -
% G" [2 ~7 a' E n5 Q - AskParents(myPointTag)
. @8 k: w- m: f& Y f - 2 ?# X) M2 E U" y
- End Sub! |/ y& g4 H# w) A. [
-
( A9 p0 v# x; @+ Q: B9 a! {3 g% C/ D5 W0 [ - Sub AskParents(ByVal objTag As Tag)
+ o; O g9 y) ^/ k2 d, U& A. C - # ^$ v8 f, d. B" u7 O2 a
- Dim n_parents As Integer
7 B6 f8 L3 p; O: `3 N - Dim parentTags As Tag()
2 R2 T4 M; f3 k0 B1 [ - Dim myPoint As Point% O8 _2 A% f& Y3 H" z1 y8 B! m
- Dim myEdge As Edge
5 S( x. K$ z# k( u2 u3 n - Dim myXform As Xform
P- u* X* C9 c* Y: x/ t+ ]% I - Dim myTaggedObject As TaggedObject
. q: q' Z; ]% e2 Y - myTaggedObject = Utilities.NXObjectManager.Get(objTag)$ F, d" Y2 e/ X2 g: |; H
-
7 e1 U; s# @, u& q0 r - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
( S2 C2 e% Z3 F) [! I - myPoint = myTaggedObject. ?# z0 V$ N) l! a* k0 }& j
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)* ?& B4 K+ S1 D0 H* ?7 b
- lw.WriteLine("")* J' m4 C K o9 T. Y3 e( P
- 1 H3 b! o4 \% }% W& Q8 T
- Try) ?: ]# V3 M2 A: a
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags); n7 s- Y- d* ]+ P; j
- lw.WriteLine("num parents: " & n_parents.ToString)( E' e1 u+ @! J/ @
- For Each parentTag As Tag In parentTags
" g$ Y- D: m4 Y - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
6 H/ s( s% x# N& C; c -
. _8 A. n- P1 ?% f+ c" t |& s - AskParents(parent_object.Tag)
2 v% _/ P# n8 ^2 `* A4 o. _ -
% E3 j9 a% {7 U/ V9 ^4 j% l# q - If parent_object.ToString.ToLower.Contains("edge") Then0 g5 R& P8 i1 H3 K! I7 ]+ T
- myEdge = parent_object; b1 h$ E, S% v) T: g6 H4 T, [+ z
- lw.WriteLine("")$ z: b3 y$ ?% B' g1 h$ x( F
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
, q6 D0 B. Y* O. w* U - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)* J3 L% T) ~5 G# s) s8 w
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
2 k9 I' Q8 v" x( X4 } - / G1 F. }) k `. f) z# t; c
- Dim partTag As Tag = myEdge.OwningPart.Tag" @. G& ^+ v- E8 Q$ r2 f
- Dim occTags() As Tag
7 K* a9 a/ ^0 Y% L, Y3 e - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)6 I* ~8 L; S* z5 k
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)
' i. D1 W; S, P7 ~ g: m/ R - For Each temp As Tag In occTags# D( a" a2 U$ k) X
- Dim myComp As Assemblies.Component. [/ w2 j& V# B6 L
- myComp = Utilities.NXObjectManager.Get(temp)
5 t7 r( L, S! y2 l- a; L' C3 W - Dim myCompPos As Point3d# }- Z3 C3 C1 C0 `: z5 ]4 |
- Dim myCompOrientation As Matrix3x3/ d9 w! z" M3 Y: d: n4 D1 x
- myComp.GetPosition(myCompPos, myCompOrientation)
+ _8 [5 T3 u$ J2 P -
6 e2 ~4 ^. ~! O3 ` - lw.WriteLine(""): v8 a- W2 m6 V2 ]; x
- lw.WriteLine("component name: " & myComp.Name)( @, A1 z- W% R9 b+ Y
- lw.WriteLine("component display name: " & myComp.DisplayName)7 s# W! P7 J( K( {* ]) D" {
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
. T) \! e3 ]+ k8 r# |1 E) N -
& |& M8 _, N1 m! c+ i. V - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then: l5 m7 D, \5 l8 w+ F( Q6 I9 X
- lw.WriteLine("** This is the component that was picked **")
& {+ T8 f" v3 _ - End If c2 T) L w$ g% Q% L( s. g
- # w% z) d; s+ s, \7 h( `% |7 ^
- lw.WriteLine("")$ ?+ h7 ]% T- e* u9 z) f0 h: f
- Next
% y& k! H* O0 M/ ^+ ? - 2 _" W- ?* X7 N' x& V
- lw.WriteLine("")+ o6 R0 I. ]" i% q) u5 w; _& }
- End If
9 k( [1 B& p, L - Next* A( f9 x/ A; B2 T/ s( v% s
-
. z# R7 S/ w; G- T - CaTCh ex As Exception; d! V" H: M) x2 B( `
- ' NXOpen.NXException: Current object is not smart) P' V: j+ i/ U" ]/ v {- ?; o
- lw.WriteLine(" Error: " + ex.Message)2 c r5 w e% d) _! x' j" }9 i
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)- D* ?) V* \% J# E1 ?# z4 z' T
- End Try% U* Q. `& A% |7 @4 W7 k) A
-
6 \! O3 S, a; O) _2 u% L4 K - ' x" G4 `- j' x0 T' K0 y2 N
- End If
3 Z/ M, E5 r3 a) d) o - ; }9 U7 ?: b* \8 q0 w
- 8 d+ t5 J6 S( P" W7 r3 ~
- End Sub
2 e, Y! s8 s& U# Y' R - ; n1 P. A( e3 K" S, w7 b9 D! F
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer* G+ ]+ A M/ e2 ]+ J
-
0 u/ \; p8 W6 }& M - Dim base_pt As Double() = New Double(2) {}
; P3 k2 @- h# x, D% I - 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null4 S6 h5 f" q/ {3 J* q+ f0 e# A4 \
- Dim response As Integer = 0 Q& m+ X, C) q, x. x, n
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
3 y4 p5 u' J8 p0 Q t -
6 j( ~6 a5 Y; U - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)' z' P4 Z$ }; j n/ h
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
_7 o4 C- K$ l - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM): t3 _9 r( R" x* k+ ?
- 3 Y/ q$ s# \) E9 E
- Dim pointLocation As Point3d# R+ R, n: k$ ^6 w, R! x) H1 U
- pointLocation.X = base_pt(0)
0 ^. ~" B* @. V. @. S5 n - pointLocation.Y = base_pt(1)6 t# {5 Z1 L; I/ M+ d
- pointLocation.Z = base_pt(2)% h2 M4 U7 E2 F9 {+ B- E
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
7 D% g( R9 z' x+ d% r) P - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
& ?5 |- W, ^+ M5 O% r+ z -
0 f, k8 o5 z5 r( ~, ^" p( `: B - Return response+ g4 K8 q- s0 W
-
' F) E6 r* [ P# j6 ?0 H - End Function( _2 c8 C: T8 v! r+ k" k' \
-
: ~$ ]8 h9 ]' }5 [/ a - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response8 f9 x* m n8 l! {3 C2 a* u
-
4 ], s& w( v) _7 M c& y - Dim selObj As TaggedObject
1 s) x! e& B; a3 Z' M5 V% E7 I - Dim theUI As UI = UI.GetUI
: t! l$ F" U# o/ m - Dim title As String = "Select a Point"; r7 G6 G' O$ A0 b q6 q
- Dim includeFeatures As Boolean = False
6 C( B- t' l3 n' t, }6 f5 T - Dim keepHighlighted As Boolean = False
0 d( }1 a% e ]2 K - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
3 U3 c1 s9 J& N9 T3 C. o/ \& ~ - Dim cursor As Point3d
6 p# a6 E3 f' |9 d7 o6 |' F - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
4 P A% e6 P0 F6 k - Dim selectionMask_array(0) As Selection.MaskTriple
, ?8 h' r2 R9 b - ! G2 v. D" \# E
- With selectionMask_array(0)
; u% f4 }7 M, A" E - .Type = UFConstants.UF_point_type
' Y- c4 K7 T* g( a5 F- h `8 i - .Subtype = UFConstants.UF_all_subtype
) P$ ^* V" d3 j) o8 g# H+ W- l' b - End With
9 }% A$ [! ?1 Q0 u; M7 c - 9 y! |" j m4 q) N6 S* L
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
( a7 i# t9 O1 t) t - title, scope, selAction, _, |: C! G! R2 }# z
- includeFeatures, keepHighlighted, selectionMask_array, _
0 l8 s1 p1 x4 p+ S! M5 u' v: x - selobj, cursor)
3 H$ t3 E' \! {8 W - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then! I7 m6 A: V$ E& j4 v- P1 H+ o3 p" x
- selPoint = selObj/ F0 O# ?+ K8 |* Y7 U) l6 x9 C
- Return Selection.Response.Ok* y; Z8 Y! m9 Y# s; V4 [
- Else% U2 J: n7 `4 T \2 s+ `0 y$ ]1 U
- Return Selection.Response.Cancel
8 Q5 m% `4 G5 t, ~/ B9 ~0 d6 V# m - End If
! s' k- @8 j: t$ m A - ( E3 A$ Z6 ]) I; E' u' K
- End Function; }/ r+ n* Z8 v- T* j
- & P. |- C6 h; e
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
7 n4 {9 r# X4 C -
8 b- h8 \2 \# }- P- R9 {- o - Dim result As Double8 e" y$ g% K0 ^% g, ^
- 9 y3 L e) c/ Q# E
- Try
2 Q6 p- z- o# U" M. E+ _6 q - Dim nullNXObject As NXObject = Nothing
! z7 _6 z+ J; `1 H0 ?$ [& Y - " R' C/ I5 k) a3 M6 D3 p1 V
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder
$ F6 O4 V1 i' x1 h+ |7 O8 `* K - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
- m! y! _7 i& M: h/ Z* H+ [! ]- R -
* D% r6 ~# Q0 H Z$ J - measureDistanceBuilder1.InfoWindow = False
' Z0 B& \+ Z$ w# N - 7 k) {& L3 S: A* K7 F/ Z
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
; ^# O. C6 B" x - - L, K: ?( _6 u. t0 g2 y5 u. u
- Dim nullUnit As Unit = Nothing
. e$ S' z$ g3 _) ?% _! A -
' i8 \% m# j3 p, s: i" K5 h - Dim measureDistance1 As MeasureDistance
7 E" c+ F- D$ X! J; @ - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)& N; ~4 Y, J: k4 v
- # }/ T \3 v0 c9 J6 {" R) O
- result = measureDistance1.Value
2 |# Z/ z0 l* S* d -
( ~5 Z7 w: [6 @3 j% M - 'measureDistance1.Information()7 a- b; M" z2 t
-
7 G! n8 L$ j, O0 U - measureDistance1.Dispose()8 f, R6 q; e _5 h( [. [8 ~9 u
-
% s$ g* A k: E) {- }) F - Catch ex As NXException/ G7 J. y5 h& J6 ~, Q# ^( n% F
- MsgBox(ex.Message)" [! A. t8 h- u+ f# {2 N3 y; r( F
- Return Nothing
* ^+ Z% p' e. l! F - 7 |# l$ z7 y0 c, {+ T
- End Try" n4 n9 Q! X) P0 b- b" [; K2 |
- $ V$ i1 q" S! a& G; d x
-
$ u* x5 V3 x0 S0 ] - Return result% r1 \* A7 [. I6 A. h0 A$ V
-
4 `; N/ y: U5 w' ?8 B& E* r - End Function1 x1 V- K0 L- Y$ F% o" h
- $ T c& [* t8 z- f b9 \" I* C, L
- ( n0 L# t5 J: s2 ~" z! n
- Public Function GetUnloadOption(ByVal dummy As String) As Integer
- ~/ k1 S; [' v' F1 e8 W" Q- } -
# V x4 ^: A! @- M - 'Unloads the image when the NX session terminates+ n. e7 K" C' ~) i# u$ l0 ^
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination4 }- m q7 F' f" x1 p
-
' A" E: Z5 E; \& H - End Function" ?+ K& ~. ]: X7 j
- 8 ?; U1 r+ @2 }" P! T" @2 R8 S1 U
- End Module v5 J" n" B1 z, m& V; h( u$ d: `
- </P>
复制代码
( R9 O/ @ K E) I; I- d) }" `, x |
|