|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
( F6 a1 D8 ~' l5 ~* t
NX二次开发源码: 装配中选点,并报告父对象3 g+ |* A6 _9 ^- ]7 F- N
- 6 p. s7 r6 I# b) o5 j
- <P>Option Strict Off! G5 G% S( q: _; A0 s+ B3 i
- Imports System( p3 ?; c9 I6 v& r; p
- Imports NXOpen' I/ i0 P% o: z1 e$ [& {1 n
- Imports NXOpen.UF) j: H2 i/ I: h+ Y
- ! B( G6 }3 K8 @ f
- Module Module16 ~9 ~. C) v* V2 l) e
-
( t1 @# {/ z0 T" p1 S - Dim theSession As Session = Session.GetSession()/ j! e+ @9 m2 w8 W/ |
- Dim theUI As UI = UI.GetUI()/ O$ H6 w8 F. m8 F
- Dim theUfSession As UFSession = UFSession.GetUFSession()& G( q" x) O! w1 |' E
- Dim lw As ListingWindow = theSession.ListingWindow3 P6 Y3 G% f! Y: y; m' R1 r0 s
- Dim pickedPoint As Point
9 D9 H) U7 L f% v' [! T" R8 T! l - Dim myModelingTolerance As Double; D' }& p) z0 E7 D
- . P. f5 ^ P$ h" W
- Sub Main()
3 s! x) ^. v. d5 M: D -
% \% h4 B6 F2 R% N/ { { - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance): c& e1 Z6 N0 h9 d4 g
-
0 M$ {- t) ]4 ?* `" K) x - Dim workPart As Part = theSession.Parts.Work
& I4 n& E' W w& m, r - lw.Open() j/ ?1 l5 y6 o+ G7 Z1 c2 Q
- ' f6 { x1 T) \4 W% h
- Dim myPointTag As Tag
( k. u# T. R6 P8 u6 \. c - 6 U8 ]& Q$ o4 \: M r" l
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then O7 k, P2 q) C* Y
- Exit Sub
6 i; u- L0 w! A4 A- `; T1 s; S - End If
8 R1 V! b# y5 V" Q! s! k$ G - 6 T$ Q& O' @8 L( j
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
& e8 a+ D. Z; X" a -
& C4 P8 \8 R/ A# {( @: e9 w - AskParents(myPointTag)
& c! ^) z! c5 t0 O# {4 H+ [ -
9 z7 P T) c) k6 T- _- `9 N, e - End Sub6 G# L( C2 h& ~: Y# g$ J
-
4 h6 b1 v0 T" o2 T0 {) g/ q - Sub AskParents(ByVal objTag As Tag)% E8 o5 H c4 d% |6 h
- 4 M' ]4 R+ D* ]8 n! @( ]+ Q2 I9 o$ W# V
- Dim n_parents As Integer( G6 z5 ^6 w/ H, N2 b' F5 z1 l
- Dim parentTags As Tag()
# w& X. K4 ~7 M8 i) c: Q - Dim myPoint As Point
" c; x2 K& R0 s4 }+ O - Dim myEdge As Edge
0 Z6 X ?- V; s) I - Dim myXform As Xform5 Z- s1 S" W! Z; A- Q7 t& O6 _
- Dim myTaggedObject As TaggedObject
5 _' p2 p3 i( \2 w - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
; X7 ~5 t9 H* d: s% l+ { - / d2 O9 O6 d0 W2 W* T" x
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
9 h! X) s/ X4 C) Z7 |8 o$ ~# ? - myPoint = myTaggedObject
; ~' \' l! s8 \. [ - lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
' H f; h0 O @# v; U - lw.WriteLine(""), ^/ ~9 y3 h1 e
- 3 K3 e0 n+ u3 r
- Try
1 }: e8 q- q' m" K - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)- t C, Y7 O9 s* B u) M
- lw.WriteLine("num parents: " & n_parents.ToString)1 S- Q O- @6 y7 R: V) o
- For Each parentTag As Tag In parentTags& s% K! F9 l3 u) k; V
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
0 ^& U- n9 ^, C3 B% t2 R -
2 s7 `7 I" e8 D - AskParents(parent_object.Tag)# w8 c- q6 c3 f& t
-
$ O; {, l9 A# e% ` - If parent_object.ToString.ToLower.Contains("edge") Then
' Q& P' D) ?: e$ h - myEdge = parent_object
0 D9 q1 [4 M$ x# G/ `. ~" q - lw.WriteLine("")# d7 K r. D0 |5 v+ t, F
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
! u( j& U1 P. i - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)2 u0 _% ~+ f) n, V
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)) f* g+ r3 b6 P) R& c2 o
- 3 a$ T; x6 p: K7 V9 p; Y8 U R
- Dim partTag As Tag = myEdge.OwningPart.Tag
) B9 [6 g3 ^3 ~ - Dim occTags() As Tag, s. b9 _$ P5 c# H7 E4 H7 v2 {9 u
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
, A9 z0 Z1 f8 e6 ] - lw.WriteLine("number of occurences: " & occTags.Length.ToString)0 S- R$ Y8 i" ?0 h* u
- For Each temp As Tag In occTags
6 g2 p+ _1 }1 z9 K - Dim myComp As Assemblies.Component3 L* v8 q! l- V
- myComp = Utilities.NXObjectManager.Get(temp); t$ l G4 f- F1 N: f: W
- Dim myCompPos As Point3d$ N7 l. r+ k0 ?7 `* B% ]0 D
- Dim myCompOrientation As Matrix3x3: ?! e& t- [. W; J
- myComp.GetPosition(myCompPos, myCompOrientation)3 _- Z! p3 |* |' v/ z$ U% b
- " Y( `1 S8 Q( X) A
- lw.WriteLine("")
- q5 b. H: L3 O, y- _ - lw.WriteLine("component name: " & myComp.Name)4 j/ ]6 o% X# K( r: J) E
- lw.WriteLine("component display name: " & myComp.DisplayName). b ?$ ~$ z: W+ J2 r* w7 @1 V
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
2 u' y$ F/ i$ m9 M V -
4 z8 I9 I& r+ G# _* C! e' I( t, Y - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then1 i2 a. {( u, j3 J7 N8 t
- lw.WriteLine("** This is the component that was picked **")
- i0 K( A. w* l t" K: o+ s - End If
, F+ G- A5 U) M" f L6 Q- y6 o -
- v8 W# ]8 c# k }0 r" O+ x - lw.WriteLine("")
2 S6 V9 ]0 a- {% e: f0 Z% v* w, F4 W - Next
5 ~( O R+ ? o" E" Q -
6 r! |, x1 r( I- j, a& D: Z2 O - lw.WriteLine("")
4 Z9 A. M/ d0 l2 n - End If5 B8 K$ X' o, I b5 K: G
- Next0 p. E& G5 m+ D3 r3 K+ w9 e' v
-
8 s4 H* Z& l/ w1 U - CaTCh ex As Exception- Y+ ^7 V& y# b+ x6 d! X, z6 l
- ' NXOpen.NXException: Current object is not smart
5 W* Y4 o2 S6 K: \6 Y: v3 B- ?# ? - lw.WriteLine(" Error: " + ex.Message)8 _9 u3 m* g( [2 Y
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)' J& u$ L i( I1 `/ Y' A! B6 P
- End Try3 [1 B& [- K J: y; {/ f1 I- r; N* z7 q+ N
- 9 D' _4 ~/ C' }, w( N
-
7 @) b+ i6 P) T( B8 q: W - End If0 r0 q) A! M7 L' j5 h
-
O, ^# q( U' C E6 V$ A" V6 @3 { - + P" G) C1 S, }- h( X2 u6 h
- End Sub1 b$ [* L0 V9 O
- - o/ j$ o: O; w/ g) Y' V f( M
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
( y6 S. m, |7 C* b% t; J6 H - # o+ r A0 i4 a( {0 }" N; G) I
- Dim base_pt As Double() = New Double(2) {}( g! t D" m+ @# v$ e. I3 n
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
( A3 d9 W; @2 A( b3 l4 B7 F - Dim response As Integer = 0
/ q( g. }; t A" w" T - Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt T; N4 n$ Q9 M! t$ a( O; ` d
-
' b% A0 P5 E0 P# g; ]0 V - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
" t; P& v" l2 s - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response); ]( `. F. H; s4 |
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)0 D! q: ?( O: y) i1 _
-
( I# b$ t' c0 |/ Z1 @9 K - Dim pointLocation As Point3d1 b, e( m8 f7 v5 `& L
- pointLocation.X = base_pt(0)
" Q0 |! b: C9 j - pointLocation.Y = base_pt(1)
5 J" k. B' M# N0 p* v0 A - pointLocation.Z = base_pt(2)# Q" k& a, S+ V- r+ H( P/ _ n) Z
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)+ I$ J9 e( L- c
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)1 e! A% o% ^# x8 w9 ~
-
. b* |- z& i) \! C( I* B4 i* b - Return response
$ _9 N" ~6 F* Q! x) ?, y1 n) E& q - * H3 z; l, ?/ G; Y4 g6 i) Y+ u
- End Function
- L1 J# y+ o4 K - ! @" ~& r! v, N& [* ?
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
- ]) g, }- K3 X - % L& w4 G" z: q$ |+ \
- Dim selObj As TaggedObject
; Y- _( d- E4 }0 p! g - Dim theUI As UI = UI.GetUI
! G) _4 B; g2 n, `8 a6 ^! ?6 d+ [2 r - Dim title As String = "Select a Point"/ x5 Y, v1 L0 |2 f7 F* D
- Dim includeFeatures As Boolean = False. M6 U. R o: J
- Dim keepHighlighted As Boolean = False! b$ o. Q/ d& ]0 `& a
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
$ F8 j. _/ W% \ - Dim cursor As Point3d H( K1 H" I: O1 \7 p
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
, P. m9 j8 O' d9 Z2 L - Dim selectionMask_array(0) As Selection.MaskTriple- x8 e2 H' y; u5 l. m5 V) F
- : ^8 W, O& O7 }& W9 g
- With selectionMask_array(0)% C1 n3 G( p: B* N9 u. W `7 t e
- .Type = UFConstants.UF_point_type
+ O$ v$ U! W% W, \* k% u' K* |4 O - .Subtype = UFConstants.UF_all_subtype
5 Q/ a- T) @% I- t - End With5 Z2 P9 u0 z# u- ]; E
- , y$ L# W7 G( ]3 a. C
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
) Y+ X8 y0 k, W$ w0 {$ a4 l - title, scope, selAction, _; m- U5 T0 ]+ u& A; J$ ~5 G
- includeFeatures, keepHighlighted, selectionMask_array, _
8 c* ^" H( b! `, Y - selobj, cursor)
6 b2 n2 m' y$ l( @- m7 N# U - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then r" p( \7 l* {. W" t( [
- selPoint = selObj
% G. I" S$ V0 o r+ E2 y$ x. S/ o4 k - Return Selection.Response.Ok
: }8 f5 h5 I. D* g8 j$ w r - Else
# p, i7 c* a3 K# R. D2 a8 c5 ~ - Return Selection.Response.Cancel
6 T+ z Y( z7 [2 r# b - End If7 o) e5 B( G1 p4 \
- 5 z, {$ s9 Y: [9 i3 g4 ^- O2 I5 G
- End Function
" `$ T+ D5 Q$ G) T: K# ?+ j' V - + r2 ]& H/ c {
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double! [6 r: D$ v' }/ D! o" k$ c
- : z: G+ x2 |3 ~; F" d: P
- Dim result As Double
5 T/ ~; W5 {* X- E - / b9 G7 b7 P6 `' H0 o2 a
- Try
. }' p C4 V- } - Dim nullNXObject As NXObject = Nothing$ Z+ }1 f8 b2 Z5 F2 {2 s( f. |" Z. {3 W
-
/ ^$ ]/ b: O0 ]& v$ {2 F8 J - Dim measureDistanceBuilder1 As MeasureDistanceBuilder
8 n6 z! I- u; l* B+ i - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject), r+ ~, I# x# e- g! K' j8 @. C
- + q6 `7 M1 R: C$ n1 I. {8 y- k9 S
- measureDistanceBuilder1.InfoWindow = False
" D$ N' O; S0 h3 ` - $ y6 t& Q6 t; B" X) z( r
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum$ U3 |" i5 n0 Q5 j% @ U
- 2 R) ~: Z5 P# o$ l+ B/ `7 V
- Dim nullUnit As Unit = Nothing; i* c, G* ^, y. T. {! Y, e
- # D! s2 P5 e- n0 L* [# T, Z
- Dim measureDistance1 As MeasureDistance. W% A5 w; R$ n3 P& n( R% P
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
6 H! g+ R* V0 K - 7 s$ f3 s( e& y* P8 Q6 D
- result = measureDistance1.Value9 C+ {' l& u# r
- 2 f$ I# Z. J, g+ A
- 'measureDistance1.Information()! G' p) W& E- n; h
-
% y3 a- C( i$ Z: D# ` - measureDistance1.Dispose(). {7 V/ i$ d8 b* @
- + F7 F0 x* w' u9 |. e* L
- Catch ex As NXException6 C% f1 Z8 u3 f* G, C5 C& W. s
- MsgBox(ex.Message)0 I" F" r2 ?+ [' y
- Return Nothing8 |1 W; R% A, X; v" a
-
# l. s) j- N, x; y$ e - End Try
( K& r! q( |2 ?# i" l -
) A/ V; l J$ ^: X6 U -
4 {. {) }- M# ]8 _& X. U - Return result1 ], P/ [" A3 C0 l+ d
- % n) K! ~. ?, a$ Y4 \
- End Function
6 s; h& x1 a2 P8 j C -
% k. v6 a6 {; \' m. l. p, b2 e+ [ -
3 K/ i' S0 n; g - Public Function GetUnloadOption(ByVal dummy As String) As Integer! ]0 P$ }, b$ S+ c+ c% [( {! E; K
-
$ b( E3 Y2 z. d+ @" k- E' n - 'Unloads the image when the NX session terminates
; o3 g$ G$ T; N9 q* B% C: V - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
( t5 ?8 {6 j% ]9 p- D2 t x+ L - 8 l& u/ J3 t. {! @
- End Function
* S( }) ]: I% D5 O - }& U, O1 o" J
- End Module
: A# Y% e7 e" K3 G& N: }% k - </P>
复制代码 $ D. d& p/ h9 \8 |
|
|