|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
u" S) C- A3 a9 t& Z
NX二次开发源码: 装配中选点,并报告父对象5 D7 I" g4 x# C# A
- 5 u' b2 r% G& E' K1 b* f
- <P>Option Strict Off
; ^6 E$ x+ V" E2 d- L" ?1 z - Imports System3 ~( h& \4 S1 H, |- }
- Imports NXOpen
* e5 y" h8 \+ w - Imports NXOpen.UF
- T( o5 d* B. I, g J J - " c- u' n8 ^' h G4 p0 e; C( I
- Module Module1" K( c' K5 H5 _& U2 s8 B
-
S4 d- w5 T7 s - Dim theSession As Session = Session.GetSession()
& X! j+ R4 O- ^8 U. m - Dim theUI As UI = UI.GetUI()# N' ~% f7 {' w) z m: j
- Dim theUfSession As UFSession = UFSession.GetUFSession() @8 S( X' W7 f4 [7 A# H2 I
- Dim lw As ListingWindow = theSession.ListingWindow& |6 N+ j! O5 g+ ?/ u
- Dim pickedPoint As Point/ M. Z' J7 z$ X
- Dim myModelingTolerance As Double7 \- E% ~0 @2 | O
- ( z. u8 m) X- \! q2 ?2 k
- Sub Main() Y) d8 K$ n. J. a; d9 {$ [) O
-
' c4 |- j& {( V5 W - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
) V. ^- ?4 e! s. F -
1 M2 W5 q; T1 b8 K - Dim workPart As Part = theSession.Parts.Work" J; A. _! S1 K
- lw.Open()
! _7 Q! a# b' U& v# x& I1 v - 6 K; S1 P( f1 Z; Y4 G
- Dim myPointTag As Tag, G1 v# S. d. o
-
! u5 G3 H" ^: V. n, e - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
- x) n0 [4 j' T1 j& G7 O# L' {8 | - Exit Sub
, d# n8 ?. R& q7 G1 R1 T - End If4 q+ z* e' S! R
-
8 R$ Z* j) f. [6 F2 F3 \3 v - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
6 c6 n* P/ T' I5 T. b5 k - ( Q U `; b5 o
- AskParents(myPointTag)+ s% W+ _3 I( T0 E' ` W m, u# I3 g
-
?$ i: |7 b. v- y - End Sub* T0 o2 ?: [9 b4 U P# `! r k
-
) U6 X* a b2 C% J3 E4 u6 } - Sub AskParents(ByVal objTag As Tag)
9 K6 i& k* ? s3 | - 8 G: _- J: Q0 Z* h$ ^
- Dim n_parents As Integer
/ S9 d; o3 ~5 b! t* \/ U% I - Dim parentTags As Tag()4 f" T5 G; b+ E0 b+ a, D* Q; p, o2 ~0 d
- Dim myPoint As Point
9 i$ d( ]% D I' u2 ]& s - Dim myEdge As Edge
* Z& j4 _! C! G4 [) K! A - Dim myXform As Xform9 R9 h: I9 ?; E9 f; R5 x
- Dim myTaggedObject As TaggedObject
7 W9 J1 A3 f+ G& r. o! Z - myTaggedObject = Utilities.NXObjectManager.Get(objTag)9 s F9 ` W* @4 e! v
-
( V- o# w$ o2 [! j, X - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
& C9 n! }9 Y. t B/ o, s7 ]; p - myPoint = myTaggedObject1 v; B+ i0 f7 \+ p: c5 h N
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
" z. x' k+ N- y- i$ b5 [ - lw.WriteLine("")' t$ D) m- i$ D1 [, h9 J
-
- U# Y# `# X. L* L - Try" V9 j) G j3 e3 W. f$ \1 C
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)$ R; d- [% H% Y+ a% G' j
- lw.WriteLine("num parents: " & n_parents.ToString)
% h) V9 ]/ C8 I! K' g - For Each parentTag As Tag In parentTags
9 g! k, v% y, |/ I% M- S - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)* R# C7 p" W5 M' X" P& p8 n0 h8 y# H
-
1 f0 a1 F1 B( t5 [6 l) f8 i1 \ - AskParents(parent_object.Tag)1 t" }/ j1 d9 }) z; \
-
/ f/ \; j5 _% D3 J% t - If parent_object.ToString.ToLower.Contains("edge") Then
3 _% }+ ~& b0 u: M) o0 @3 R - myEdge = parent_object) N3 [" r. p5 L4 m: A. E
- lw.WriteLine("")3 s9 @. ^! M; r
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
P. Z" S; q( P- O% ?6 B/ \0 K- z8 y7 [ - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)0 s; ~6 K6 Y) R7 n S0 Y* V
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)5 H( k. h8 Y: S, P9 e
- ( D( ]1 `& L) F) _" M9 @8 u7 f
- Dim partTag As Tag = myEdge.OwningPart.Tag
( K2 o8 I: w- f& k b) V - Dim occTags() As Tag
& b; q" S2 ?, F% Z0 | - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
% T8 V7 k1 j+ S. x0 J - lw.WriteLine("number of occurences: " & occTags.Length.ToString)5 X' K6 T3 n% ^5 Y6 v- d+ J
- For Each temp As Tag In occTags8 v* l$ C9 G k' n0 V2 ]# G
- Dim myComp As Assemblies.Component
" X$ I' f+ i# |5 h9 G - myComp = Utilities.NXObjectManager.Get(temp)* A0 M, U- w p }, ^
- Dim myCompPos As Point3d
- ]4 X; A" j0 C# I: D; t1 o; W2 e6 | - Dim myCompOrientation As Matrix3x3& _* s7 I% S$ G; g0 c p
- myComp.GetPosition(myCompPos, myCompOrientation)
2 h' i2 _# ?1 t -
$ A; O9 [. a9 R2 g - lw.WriteLine("")+ \2 K; M8 N0 s9 s/ k& g4 L
- lw.WriteLine("component name: " & myComp.Name)6 b8 A$ Y$ C0 V* U
- lw.WriteLine("component display name: " & myComp.DisplayName)
) i1 I4 S7 b% c: s3 O; l - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
: Y+ K5 |( V z% U+ J) ^ -
! L- E1 u; W- v) } - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then2 R. |" D& E! ?$ k, `
- lw.WriteLine("** This is the component that was picked **")
- ~, K, `, ^$ ]% p - End If# Y: |- ~+ Z7 C% A
- ! s5 `; f1 x0 s( h! w# M/ X, ]. {
- lw.WriteLine("")) E. A/ U; i' O. V: s0 w* R2 b$ q8 Y
- Next
, g$ q" J( h, F& ? - % {8 C" N2 d! i( a7 I, O8 B
- lw.WriteLine("")
: W; e' v+ W8 D- } - End If
& w6 j' w D# d8 O - Next
$ y2 x# L0 e" z( J1 @ -
1 d+ L+ _" j2 m9 C$ X$ P( j0 T& ~ - CaTCh ex As Exception
" r# n: Z* ^/ n" q - ' NXOpen.NXException: Current object is not smart
4 h( X8 Y2 j: | - lw.WriteLine(" Error: " + ex.Message)8 l+ h; m P" y4 Z6 r
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)
0 C2 x( g# ^* z* J% Y - End Try. k. O% x V; L; `
-
* f; z# O* ^; f1 U+ R5 b, k S$ c - ) d1 |; d- Z/ }) D0 k
- End If
c2 S# p+ J- @9 H/ z8 G -
. L# k4 j0 E# I" U+ s* Q+ q - . H+ `% _% t) u S S0 t& L
- End Sub; J& U( t% Z/ l/ v* w* ~/ k. D
-
4 F# M) j3 J# R7 u f+ v# l1 w - Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
2 A# T! d( e9 T9 h -
/ ^# O }1 c7 }' e( N- _ - Dim base_pt As Double() = New Double(2) {}* S- y& V- j+ Q" s8 J
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
5 Z# h2 V- k8 ~5 u6 V - Dim response As Integer = 0
) |$ g4 N8 J2 F! h5 h - Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
# y8 { U$ v/ G: K( n - # e5 s# p3 T6 r, i9 X
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)( e) [! b( O8 k3 z+ a: M
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
) Z- f2 q2 p( n - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)/ `9 p" ^; H! P
- $ r7 A6 B8 o) y7 Y2 Z: Z! F4 a, V
- Dim pointLocation As Point3d
+ N! e" F" \: D/ V - pointLocation.X = base_pt(0), \& v) A1 x2 e9 u4 h2 Z
- pointLocation.Y = base_pt(1)0 d, r5 R1 i8 N+ ?) q: h
- pointLocation.Z = base_pt(2)
1 L' q4 ?1 ~' x/ ~4 S% G - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)% e* n& p: T" B X
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)+ G( h3 e: h, u6 q9 {. ?
-
2 U4 e& g% x' K1 `/ M5 v; g+ P, Q - Return response
+ }! s3 I! j+ Y8 H( L% G -
5 f- V( z. P4 L0 ~/ D* S7 G - End Function( B4 l, L9 L9 J1 _+ `) E
-
! v0 h9 W* Y) ]6 ?6 s7 [' \ - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
0 ~3 H8 b; X7 z* Q - 7 m- G) _" d8 n
- Dim selObj As TaggedObject3 E* ]" n6 I6 f% `+ T' h
- Dim theUI As UI = UI.GetUI0 i7 Y, t9 }, i) \7 f
- Dim title As String = "Select a Point"$ p$ j2 @! A% f p0 U0 S
- Dim includeFeatures As Boolean = False9 w# V7 l E4 Q
- Dim keepHighlighted As Boolean = False0 f6 N* O5 e) [- t, | P/ H
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific' O6 G H$ M0 E; d( j
- Dim cursor As Point3d' |/ D9 ^3 b& p2 f& ^5 ]
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
3 n; A1 {( e- S( x, [ - Dim selectionMask_array(0) As Selection.MaskTriple
! B8 t D1 ` n' T$ q -
& |# d( e$ P3 }. |5 E! d - With selectionMask_array(0)5 `6 ~- @, Z, z2 W7 T7 {5 \' ^) @6 i
- .Type = UFConstants.UF_point_type* X; Z, }7 {. w, [) e
- .Subtype = UFConstants.UF_all_subtype/ s' i; t* D; F' T# q5 \) [
- End With
9 s8 @: m# L8 l' R) u ?1 D0 @ -
0 L# v- f; w+ D2 } - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
1 i4 e3 Z0 H# ]3 m7 C - title, scope, selAction, _; g% Q- }, @' {, d+ H
- includeFeatures, keepHighlighted, selectionMask_array, _
( y& v7 j) k& T# L$ R2 }$ ^ - selobj, cursor)
1 K( H$ s7 g# e7 Y - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
- |# S5 V k* A2 d2 e/ C - selPoint = selObj
$ ?% j6 n& ~# j# g - Return Selection.Response.Ok/ r0 G5 S* E4 ] y
- Else1 N& Z) v# W2 l4 O! X
- Return Selection.Response.Cancel
3 g. a5 h: w* d( x& Z, Z. V0 a - End If. b# @$ }( g1 n# c8 S) a0 A# G
-
2 A- s N, {# B" J$ G3 ?2 N - End Function
2 |' c9 o$ T" D g9 g. P, d -
1 h8 @8 V& [$ w - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
* f# V S( r8 v7 r -
# Y! G. ]9 A; U8 m( g$ k/ l - Dim result As Double
7 z @! z1 X5 q -
* @: M% C, v8 h; g$ H' Z - Try7 }- I3 E* l6 C
- Dim nullNXObject As NXObject = Nothing' j/ |( \! }+ e1 j" s4 \, D
- ! M# b+ U Q# F- O; p+ @; j
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder
4 l, \ p; [) y- |% L/ Q0 T - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)6 c. x' Z" O! H
- 6 U0 z# A9 U! M4 p# `" i0 a
- measureDistanceBuilder1.InfoWindow = False' q/ k; d/ `6 w# `6 o8 ]
- - B; u/ Y! c! p' q U
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
" ]# Q- W" w) n8 z! Y -
: {7 b4 K2 |& _% W - Dim nullUnit As Unit = Nothing! x! o) n e3 b* f3 a
-
( W$ `% C6 ?: Y - Dim measureDistance1 As MeasureDistance0 G5 I0 Q0 [+ `2 S
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
: ^6 l( j7 n |, Q2 R) X/ f7 Q -
! E6 p: V0 b0 w, j - result = measureDistance1.Value
4 p# z O p( ?* K3 u8 S D -
4 g) v: n7 O0 [) n - 'measureDistance1.Information()
$ E* X; H$ ~5 o2 d: K -
4 }3 b$ C8 a e0 Y# f j7 `# ~) }) u! l - measureDistance1.Dispose(). ^7 N2 w( o5 |3 g; Q4 j, E
-
2 N) t! g: r, `& ?3 t - Catch ex As NXException% X; z1 ^3 I3 [' h1 |
- MsgBox(ex.Message)" n4 t6 N5 U4 l% r G. m
- Return Nothing
+ j/ P4 K& ]3 J$ t# N, L, V7 [ - / \: r% k- e4 V! }( h7 X
- End Try3 i/ W: E% ^5 ], m. J9 |
- ' X) z9 ?7 `& r& Y. ~
- 3 h7 d9 m4 v% P) ]) _" z
- Return result! l2 s6 I1 x; n# n" l
- 1 V5 s% T8 M7 I
- End Function
0 C1 l- L0 Q; T8 v - & j0 N) H1 L7 [; }
- ; z) Q) j7 x8 s0 t
- Public Function GetUnloadOption(ByVal dummy As String) As Integer
: j- P3 [ D) O r7 k -
# }5 L6 X8 U6 [7 e( c - 'Unloads the image when the NX session terminates
" k9 _' n/ r# I8 I1 S2 u$ S+ P - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
6 u+ w9 u* Z/ u. ^4 F* n7 M -
( i, P4 ?8 J9 K) v - End Function
. `/ `7 p3 h5 Z8 E -
! M g2 _) b, o) k- j) l- b0 g! N( X - End Module
0 t. @# I2 o' S* ^9 @+ a- z5 D; b - </P>
复制代码 3 ~* L- B2 H. N5 J$ I: y& f% G
|
|