|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
0 ?+ B; v* D4 ?& ~! {
NX二次开发源码: 装配中选点,并报告父对象
3 N! U; n' d" I2 U- b" X8 u. u+ k
" C A: X: o% b* I6 O- <P>Option Strict Off
0 L/ T" O* {5 W& w - Imports System9 s5 L* {1 C2 z( O* z8 i
- Imports NXOpen+ E2 J8 h$ I0 Z; r) s& t# D9 _+ T
- Imports NXOpen.UF
7 ~0 W7 p2 t0 q- q& Z6 x* N -
6 ]2 H' V1 ~- G* A% l - Module Module1" K! w0 s" N5 b+ k& f+ I
-
# T3 ^& v! `# ]1 t! G4 h - Dim theSession As Session = Session.GetSession()
1 L' d; ~3 i1 d/ @ - Dim theUI As UI = UI.GetUI()
J( W+ u$ ?" k/ n1 d3 P. s5 t+ E9 \4 y - Dim theUfSession As UFSession = UFSession.GetUFSession()# O3 A9 G" V5 B5 J
- Dim lw As ListingWindow = theSession.ListingWindow
: X, L1 u2 e; I9 N; q# J! k - Dim pickedPoint As Point
( U1 u: T5 W) _% W - Dim myModelingTolerance As Double8 x. f' f) \+ K$ W
-
6 c- c+ h* y- T" M" O3 V - Sub Main()
% j4 V: y8 X' _. S' D( `; x9 i - 3 g$ h% M) n& \, S) f
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
6 D3 X x$ _: N4 }% y# F: ^- u7 q. Y - 1 a) P& k4 `6 Z, Z7 W. j
- Dim workPart As Part = theSession.Parts.Work, W( i8 u9 ~9 t, D$ P2 |+ }
- lw.Open()
9 x- O3 p+ E# p% a -
0 C! H0 _+ ~8 q - Dim myPointTag As Tag, T# o/ n' e4 i, y8 r
- * ^. Z: F3 I- _+ e2 ]* u& S
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
9 T+ @* n2 O$ G- s) t - Exit Sub( o m4 K( c: G! b9 G7 g2 ?0 Q
- End If% P' f/ F" L( W$ C
-
. {5 M# U2 O+ Q0 o' N3 j* O! X - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
+ o9 s: d/ M2 Q8 v7 ? -
/ E0 A: ]# m( f5 b - AskParents(myPointTag)3 |" S/ U( C9 \2 h0 ~# Z
-
' l9 S4 y% E, y: }! E - End Sub
- G% |, `6 h- u9 R1 U/ N3 m: \; Q -
) u+ d& D* K- U. V' N - Sub AskParents(ByVal objTag As Tag)
7 T" g$ {% M0 L- I; I; d - ) c! ^1 v# f+ n6 R: c
- Dim n_parents As Integer
3 ]1 m" i: K8 S: x3 q& @& J - Dim parentTags As Tag()
* Y$ U1 @# P% P - Dim myPoint As Point
S5 g8 k6 M- L+ e9 \+ n1 A* o- r - Dim myEdge As Edge0 K+ D/ L9 A+ w0 p0 }) e5 [4 z7 i. c
- Dim myXform As Xform
9 m7 D4 t4 Z% ]3 w7 ^& S: b - Dim myTaggedObject As TaggedObject
) a8 ^9 M5 ]6 a# F5 p8 z% n* x - myTaggedObject = Utilities.NXObjectManager.Get(objTag)$ G4 R8 k0 G# ^8 ]
- ! Z$ y" v' C3 T: i/ J7 }7 z- j
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then/ S7 A K& T1 B" `& U
- myPoint = myTaggedObject8 k# G1 g9 E. X F2 h9 Q! d% j" P: b
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)' u7 r8 ?/ r6 ], f
- lw.WriteLine("")
1 C" S' {, C0 Y0 w* S3 I2 {- j -
$ K/ }1 A- ~# J - Try6 o+ n/ j0 K! j) s8 _) {- W
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)* V7 P. j9 @+ M4 R
- lw.WriteLine("num parents: " & n_parents.ToString), x8 t0 g8 o q! {8 M3 |
- For Each parentTag As Tag In parentTags1 R; O4 N0 s/ i5 g, ]
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
$ i/ ?2 x: z9 a. p: y - 7 c8 F) V7 A9 S [- M
- AskParents(parent_object.Tag)* T8 ~, S0 h/ x. O2 C$ }4 ?
- 9 V" g2 k# N6 P8 G" B9 T( {) G
- If parent_object.ToString.ToLower.Contains("edge") Then
5 I5 ^3 S( m1 t# E: { - myEdge = parent_object
E+ u8 v, k3 w& Y7 I - lw.WriteLine("")
( Q0 q5 z, u9 _+ Z - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString): s/ A" B% ]# U
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)( |) f: w& [# v( x
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
. g( T+ x6 j4 A A -
" d* U% u$ y$ }/ m D# a - Dim partTag As Tag = myEdge.OwningPart.Tag
7 w2 U% V" I3 N7 K9 M8 P) R0 j$ _ - Dim occTags() As Tag
0 P x ]- Q( ]; s4 x" q% V - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
* B, Q5 i; P$ n) Q - lw.WriteLine("number of occurences: " & occTags.Length.ToString); V$ B- U- c& p, c& z
- For Each temp As Tag In occTags! ^# }) j6 P5 O
- Dim myComp As Assemblies.Component
9 s1 P; z9 t. C; r - myComp = Utilities.NXObjectManager.Get(temp)
# _+ V( y% Z- F7 _7 h: d - Dim myCompPos As Point3d0 W7 ~' F: n5 g; ]
- Dim myCompOrientation As Matrix3x3- }3 W, s4 Y w. L! f
- myComp.GetPosition(myCompPos, myCompOrientation): V R9 D( Q* j; }, w
- i0 y! W) g' G! f8 H0 d8 V3 A3 j
- lw.WriteLine("")
7 x" Z% Z* `, Q7 x2 v. B- @5 s - lw.WriteLine("component name: " & myComp.Name)
6 ^. T0 v9 {/ o0 f( K4 J& d - lw.WriteLine("component display name: " & myComp.DisplayName)
d! L, E) X" R% E/ c - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)& f7 k7 M/ w" S; o1 c) }- N, M
-
% I3 n0 \1 j1 X P b( k" m - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
: g3 Y( l( ]+ G" u4 L" h8 V) } - lw.WriteLine("** This is the component that was picked **")
- T0 ^& |5 u9 U9 w, `; _; m% I" S - End If" O( q; e7 I7 A+ L; m2 T
- 9 Y- p! V% N9 G/ k" N6 ?
- lw.WriteLine("")
9 ?6 d% f( I$ \8 p5 V - Next
& u) j p2 @1 n3 \. A; F - 9 @8 o. i8 v/ o9 G: k
- lw.WriteLine("")3 ~( b4 M6 z7 z' B! ~
- End If
+ B6 a0 J) B1 t - Next# ~8 A/ A/ d8 Z% j" d
-
/ U+ S6 k$ h" u) v( ^1 r; l - CaTCh ex As Exception, |! F0 V, _1 a/ L9 B$ f
- ' NXOpen.NXException: Current object is not smart
5 L* g9 @! w3 z( R" S8 s - lw.WriteLine(" Error: " + ex.Message)
: R1 }1 v$ y% K" L+ U. f - lw.WriteLine(" " & myTaggedObject.GetType.ToString)7 \) T: c `3 c1 ~1 r( w+ E
- End Try6 {+ h8 l2 u6 a1 D/ G1 p- }- z! h
-
1 K. s5 D q* s: {- N -
1 O2 Y0 [3 B! l# o+ Y - End If
( ~; I0 r1 L% x# V -
# O1 w, }0 D9 w. x) g -
0 v% P1 N& T7 C - End Sub: ^- P0 q! u: E- Y( E: ~: \9 i6 B
- * P [. E2 _2 g1 X
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
6 _' d. {. W( e: r7 E% l - * S- u* T7 j" C6 b) m" G, p8 `
- Dim base_pt As Double() = New Double(2) {}
: B- B+ H- d: j k: ] - 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
- H4 ?% x! F* r' I& J - Dim response As Integer = 03 i6 j# m& u9 f' } y7 S0 p5 c. O- e
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt" _+ D1 d4 C* [
- # n. Q" s( }0 H+ ^# ~- n; v
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
. w" J- d' p( ~4 w0 Q - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
1 @" Y2 e+ p% C6 X, T; }# B+ M - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
) y& M; R$ n3 U2 I -
1 x c; m, D1 D - Dim pointLocation As Point3d
* H* g" N" N5 o. }: n2 i - pointLocation.X = base_pt(0)
) L- w7 j4 o# L' I# |2 m6 m - pointLocation.Y = base_pt(1)! N6 z/ Q* \& I' ]( ^* y, n
- pointLocation.Z = base_pt(2)
$ i/ A0 F3 t) b; w$ M' K# E V - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)' M* `" i6 g2 s* A9 a
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)! s7 {- g: l, E( Z0 U- A
-
" ~. t, Y$ N# X, W3 v. e. c$ a" k - Return response5 m$ a x0 l: N! I
- 8 l- u- m5 Z% m' t
- End Function
$ |+ d9 n$ N% I& L -
7 K- H; G, I1 e - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
4 {* x% D% B4 e R3 n K( b" x - ( _% D6 ]! Z2 ^3 H
- Dim selObj As TaggedObject
. k- W: g0 \# Q9 i3 m - Dim theUI As UI = UI.GetUI
$ g' |$ B Y! j, K* M6 ~' t8 `# { - Dim title As String = "Select a Point"
0 M7 x' B8 J2 y8 E - Dim includeFeatures As Boolean = False7 N& {, t1 `) i
- Dim keepHighlighted As Boolean = False
3 z# C; r x% m" I' z6 Q# ^( l - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
7 ]- g; S& `5 S, [3 s - Dim cursor As Point3d: L; O/ \4 e& K! l
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
" V$ X1 r) `" z - Dim selectionMask_array(0) As Selection.MaskTriple9 E, c6 `, |( n C5 [! E# _
- 3 _/ \3 R/ m I2 z0 a
- With selectionMask_array(0)
# i) ~! z; ?4 _$ h7 e5 V' Y# I! X j) w - .Type = UFConstants.UF_point_type* C; z( s& ^6 ^0 P
- .Subtype = UFConstants.UF_all_subtype
) s+ Z# M7 m/ U7 ~. P1 h - End With$ h" E; ~+ Z: F+ [
-
$ ~, H, P: H) J r% W; r - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
+ [, v7 D2 `8 _, `1 L5 U i& } - title, scope, selAction, _
' J; I4 y/ a' |, h6 |, u - includeFeatures, keepHighlighted, selectionMask_array, _
7 w+ P& G1 n& V$ _- u' T" j' U - selobj, cursor)
4 r4 Y( t6 i1 Q/ Q1 q9 @! a - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
$ B: Q+ F$ X) @! F/ d+ q0 s) A1 A - selPoint = selObj7 V! {. X) K I1 |# t7 I$ X4 p
- Return Selection.Response.Ok% B4 ^7 \) q: s8 }. @
- Else$ u/ `& @" _. f" g# @/ x
- Return Selection.Response.Cancel
& _: O+ \) `! W f z4 t# D l - End If1 {) a& O9 ]8 l' s. v% j4 l
-
; H& t" O* d) c; i/ l: S8 I: W% J - End Function
6 k! \3 O `- D( o5 Z G: m -
" z% c/ N* e/ f Q - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
& q/ U! E& g2 |: Y -
2 x9 a" C q* F* V6 U) Y; H - Dim result As Double
" f% K/ K; ~& \+ X- @' _ - 8 C" f* B- v7 n* q
- Try' R9 }; t- M, V9 N. |- \2 z2 X. r5 b
- Dim nullNXObject As NXObject = Nothing
4 K( |! y) v8 @ _. Z L- ^ -
; s# S4 x0 X* j - Dim measureDistanceBuilder1 As MeasureDistanceBuilder* x2 O# u" C+ r, [0 ^0 D V
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject); Q: z* j1 H7 U5 s* x1 M% U; U7 U9 ~
- 3 ^- R8 ^$ P; m1 D R6 F
- measureDistanceBuilder1.InfoWindow = False
k" l* w% w- f( } - 0 C) ^# l1 |5 C, d3 u% A
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum# o8 P# N8 M: P- K4 Q; T+ v( }
-
; } g l- w3 X6 I - Dim nullUnit As Unit = Nothing6 f- y% @2 I0 s0 j- g
-
, K- h' ^6 v2 q8 h0 l - Dim measureDistance1 As MeasureDistance
$ w& Q8 J1 p' Q" }8 g: m - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
! g" `" S, Y$ J -
% {0 D$ s, [+ G! A+ P, F - result = measureDistance1.Value
' }* N) B+ g9 { - ' k* j7 n1 L( U2 W9 C+ j3 ]: ^
- 'measureDistance1.Information()% T( B2 s/ R. M/ z2 U
-
% s0 J! D/ j0 _ E* o0 F! O. S, S - measureDistance1.Dispose(). w3 R; Z0 q- d
- " B/ i; b, h+ W4 s% X, X( c) h6 M
- Catch ex As NXException
' b5 X! h' h( k5 o0 i - MsgBox(ex.Message)
1 y5 ?$ H$ V" y% ] - Return Nothing* @7 b" D+ r& A9 _3 s e/ C6 ]
- ( ~6 V/ Y$ K) [* H1 u* ~5 X4 X
- End Try
4 s0 Z/ O8 A& _" s; H; P0 S$ K - ) D& l& R5 }' y) \5 }! a
-
) q6 z" k1 y2 _9 M: T- j/ X. h - Return result& n- _- H& ]- Y: L( Z+ M7 E% }
-
* d: T4 P: h5 v - End Function8 ?, N& {6 p5 [* w
-
% K" G- l# g9 c* _ -
V, Y+ e2 s: l: e - Public Function GetUnloadOption(ByVal dummy As String) As Integer
' g) H1 x, N, i. |" w- a9 ]5 l -
8 U0 q7 \/ M- a& c/ S - 'Unloads the image when the NX session terminates' g% K A. `8 g- D
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
8 S% E8 L ^! [5 v. f ?1 v -
: Z: F) U' |& ?4 p( n& p - End Function8 r0 H1 m! D. P) B8 w& p9 Y
-
+ D$ d7 t/ j0 E# e5 h' x$ p0 n - End Module& C! }1 K8 M. ], j1 O2 |3 T
- </P>
复制代码
: G' Q# R/ v7 J |
|