|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
+ e+ I. M: P* ]- R3 E( W" V: r
NX二次开发源码: 装配中选点,并报告父对象
+ f/ I3 c! V8 a9 p) \0 I- " |& t6 \- h+ M* s
- <P>Option Strict Off/ h. j9 _* q% a$ t9 z8 Y
- Imports System H& B9 ^$ t; O7 v+ u1 z* m
- Imports NXOpen% m6 w2 A4 k8 V1 l( U4 n
- Imports NXOpen.UF) z: F$ F" T) m+ `
-
4 M a+ N$ c* G' L/ A1 L - Module Module1: r9 g4 m& ^; b# W: P
- $ n; |2 N( Y) m9 T0 H
- Dim theSession As Session = Session.GetSession()8 s+ I2 V, Z$ T5 y' i" D
- Dim theUI As UI = UI.GetUI()5 M" w( F- o6 T D
- Dim theUfSession As UFSession = UFSession.GetUFSession()' X# c, t, |0 P8 M
- Dim lw As ListingWindow = theSession.ListingWindow: y. U& h4 ]: H3 ?! j5 g
- Dim pickedPoint As Point( T X4 _8 `; D9 W0 M
- Dim myModelingTolerance As Double8 |: g5 y4 G0 }6 |7 H
- + I- F& n8 v; l1 t
- Sub Main()
- u5 h9 ?' f: b2 B -
" |* r$ E, _- i- y M# P - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
8 I" z p7 ^6 P) l3 E: C$ {; M -
$ g' c. g) q$ t$ j- T, Y* O; H - Dim workPart As Part = theSession.Parts.Work
7 J/ D/ d6 x+ S& C; T: |' a$ h7 M - lw.Open()4 r+ h# v" o! N- E
- 4 t0 V. R' ^! B; c4 p- d/ [: Q# ?# F8 k
- Dim myPointTag As Tag
' s/ @, V- v& P% ~+ P1 v. c -
+ W% j3 H4 O' N - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then+ i" |7 G; n1 w' I4 N; V
- Exit Sub5 F& [: s7 D3 X& _7 s( q
- End If
/ w; x8 J; Z3 l# j - 3 ~' e- @# W" @" x% q) j
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
S9 }8 }5 T M1 G5 I' y -
6 p7 o' W- u+ H' c2 v: D: L+ C - AskParents(myPointTag)
' Y6 q6 V0 ^9 I( l - $ _" v5 L9 y# x% z" w
- End Sub
( q8 I6 B& h0 D8 p - . P T" u( e9 Z" M8 I5 s/ F. A; k
- Sub AskParents(ByVal objTag As Tag)
9 B6 d* ^+ S5 I$ P# m5 N: p6 T -
/ @8 T+ g2 w, e/ Y* { - Dim n_parents As Integer0 N2 W2 E# |3 z. V; u
- Dim parentTags As Tag()7 }+ O0 M9 U+ @7 D* C/ G+ j7 Z
- Dim myPoint As Point
% d. P3 s+ \& p4 F- ~# \ - Dim myEdge As Edge
* x1 q L2 R3 h0 L7 D - Dim myXform As Xform, x& W1 e; a) K( V; r1 ~; _3 M
- Dim myTaggedObject As TaggedObject
3 p* a8 Q" _$ h L - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
+ q+ [) S' S6 I) d( W& \5 h -
" b+ d$ H& I+ n, d - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then& N* L. N5 D" g
- myPoint = myTaggedObject' y- J' y2 Q n% C# O( L6 S
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
/ j& ]' F. A+ u% m8 O+ J9 _" U! F6 q - lw.WriteLine("")
. m$ P! V, Q# l, E% L, J: a -
/ S. k4 ]8 S' W. j, L) d - Try
: D) [% t' }8 I* P - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)# c) T' N2 T0 ]! A7 b$ M. R4 N
- lw.WriteLine("num parents: " & n_parents.ToString), U$ C9 u1 h/ a
- For Each parentTag As Tag In parentTags6 G0 e" l& h1 l: h
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)9 \2 \) X: z7 ]
- ) `9 }0 X% C# k. x. E
- AskParents(parent_object.Tag)
$ v+ P( S3 }9 m- @& s -
/ J+ B' ~6 U6 N( a( p0 C - If parent_object.ToString.ToLower.Contains("edge") Then, c% ~# Y3 [2 S0 u4 ]4 @& ?
- myEdge = parent_object
- \0 S- v' M& Z% m- p! |- x - lw.WriteLine("")' }& _' P# T2 J4 H8 m; Y% {% Y
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString), f& ^3 M" v# d$ P+ Y
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString); _% Z1 ~" q) J
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
$ D& S! V7 a( b& }7 t; y( X. n, Z6 T - & b6 ~3 q2 m5 l9 h$ c/ r, ^8 b
- Dim partTag As Tag = myEdge.OwningPart.Tag
& V# C0 D8 Z6 P& ?9 i$ `6 p# ^0 e# d - Dim occTags() As Tag
, E# u* d A# ^' o - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)6 T4 E a$ ?/ _# D& N0 _
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)
% C* e' m O. g0 c% ^ - For Each temp As Tag In occTags
) ^; n+ ]+ D" |' j: Z: M y0 J - Dim myComp As Assemblies.Component, z, u" z' n% |. }
- myComp = Utilities.NXObjectManager.Get(temp)6 ~2 s7 [. a; p- s
- Dim myCompPos As Point3d
# e5 }6 f% @* F - Dim myCompOrientation As Matrix3x3* {& `) B S8 \- |' S( ]- V
- myComp.GetPosition(myCompPos, myCompOrientation)
9 }% ~4 r6 U9 J% k1 s - 7 ^' r$ U& }, U% r3 p+ T1 j7 X
- lw.WriteLine("")
$ k3 O e8 @' h; k. \ - lw.WriteLine("component name: " & myComp.Name)3 F P) \ k# o# t6 x* A
- lw.WriteLine("component display name: " & myComp.DisplayName)6 y2 C; d0 y* r8 J
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
1 _4 G: Y7 }4 b9 `/ k- z -
6 W2 i( W4 D/ W( O, D3 S+ J - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then# [/ D8 l% Y. y: u0 n; n E
- lw.WriteLine("** This is the component that was picked **")
. Y: T4 m2 r( o( W# j - End If
+ h3 C5 {0 B8 \ - ' Q" |" \ w: N
- lw.WriteLine("")
1 r7 B5 s- U5 v3 c- h% Q) w - Next
# W' @6 M! m1 _2 ^; U8 t% d -
" u: t5 u% G6 c - lw.WriteLine("")% B) j) f6 m# `8 p) }
- End If
/ B2 \! A. ^ K2 a+ O3 ~) F7 { - Next) j- b3 B2 M9 C% m8 l X2 e
- * L3 K- a2 `1 [" f" P. s" {0 U4 }
- CaTCh ex As Exception
, _3 I% G; b. Q; Y$ Y. i - ' NXOpen.NXException: Current object is not smart
& A; b, I# v6 ]! y6 e - lw.WriteLine(" Error: " + ex.Message)0 `) [ L, R6 T6 S% }
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)9 b- l8 b* o$ t" }9 `2 Q
- End Try
6 ^2 u- S. l; q -
+ x! `$ T0 N" {( f! q5 \! n - " D3 I4 W- B( g6 I- h6 J. h
- End If
, [6 a0 v' N# h# N3 _2 R) w1 | - 8 t, g4 {2 ^/ k& [+ ~; h L
- ( I( O+ f0 X5 D# J. d
- End Sub
& i" V/ x) T8 X/ P6 Y -
$ G, q( W9 i- }: m3 [0 E - Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
; m+ T7 L4 |! k; V& l1 R - 8 m; d4 x7 G5 K
- Dim base_pt As Double() = New Double(2) {}
9 w( N, g e3 T" y @; @ - 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null& s8 j Y/ ], h. n1 }% ~
- Dim response As Integer = 01 K( ? ?! T' z5 m+ p2 o) N3 C
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt) M7 r$ W! D$ S2 _
-
+ @& x. s5 W( `" I - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM) }1 q; y; L0 F7 H8 W
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)+ J0 y* l, R- K
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM) V- I) ?7 S; _: y9 G
- 0 s' J. t; x. ^& m
- Dim pointLocation As Point3d4 f" k& m" W2 a8 j+ @* d" l. D
- pointLocation.X = base_pt(0)6 V4 ^# e, H, D) }; C& n
- pointLocation.Y = base_pt(1)
4 f9 `: _. \6 ? - pointLocation.Z = base_pt(2)0 |: a3 j/ c7 F/ B; C
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)! `9 I. w3 p% L! g: r5 a) U
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
+ H0 c) T( n1 z+ R i -
" }9 i# q j) ^: C* r - Return response
* \/ @9 }5 a9 a5 J - 5 C- }- d! j0 [( k* m# p
- End Function
# p- {) ^# n! ]0 s+ H -
9 E/ N5 D0 F3 p. c- {) S - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response+ m7 f' D5 z: V1 j. q. v
-
9 t/ l- x8 M: _+ w4 D7 S: e - Dim selObj As TaggedObject, Y8 J/ P8 R# i5 P9 K% T
- Dim theUI As UI = UI.GetUI, Z: ?/ f1 b# c2 u+ p- x- f* u3 t
- Dim title As String = "Select a Point"7 h. w# N0 [' J8 U% {$ u: b
- Dim includeFeatures As Boolean = False/ y" e. |$ w$ E0 r: t
- Dim keepHighlighted As Boolean = False- a$ e- G% y; I3 d/ t" P2 w
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
0 x; n. ]8 [4 H* x% W7 x6 o2 k - Dim cursor As Point3d1 ^- w2 h& C. f
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
4 `9 G1 i" t/ b7 a; ^; q5 k: s - Dim selectionMask_array(0) As Selection.MaskTriple
' a, @' Y1 \& U, u4 z$ s - , b/ J- a# g% w+ G$ L
- With selectionMask_array(0). L: R+ V% f3 g
- .Type = UFConstants.UF_point_type
( `3 h) u' X8 G' T: P - .Subtype = UFConstants.UF_all_subtype
$ Q9 X, _7 e, P" o1 R; P4 h8 C/ d, B( w+ E - End With
" g) X2 E& C4 e7 R0 y -
/ v: ~# G+ p/ ?& H* K1 l - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
. l( J+ z6 j- w- p, e4 i7 i - title, scope, selAction, _
9 p* f5 g7 y: [: x' A4 g2 } - includeFeatures, keepHighlighted, selectionMask_array, _/ M) z; }+ o8 b9 j B+ F
- selobj, cursor)
4 E& e/ W: y s/ Y4 ^- | - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
" u& Y" Q/ b" H4 `9 B; S8 g - selPoint = selObj; x, q* {$ c& m: f8 Q8 x# t
- Return Selection.Response.Ok8 O! {1 @' T4 c2 h! l3 I
- Else
& k9 J) z( O+ z1 h+ {+ X - Return Selection.Response.Cancel
* R4 w8 h7 [5 \# {* P - End If4 t2 T( G$ u8 U- j( W. T" p
- % r; h$ E5 `; Q% N% ^
- End Function2 ^) W6 w( \1 n4 Z' K0 H: _
- % c, B" y% q' I4 L4 B
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double7 i3 e0 F4 g% n) L
-
; H6 a; N* G9 f6 H - Dim result As Double4 c& @. f9 l. h
- + I: a. r {# S
- Try2 k+ q" d/ S! o' n2 Q$ h9 _
- Dim nullNXObject As NXObject = Nothing
R, f4 r- I/ Q* I0 | -
2 q f3 u; N v4 n/ W# L, e - Dim measureDistanceBuilder1 As MeasureDistanceBuilder/ ?: x. {0 H! ~! t* _" x% |7 S( e1 _$ i
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject). C5 q7 k5 r* _! u0 ?' ^5 S
-
* e$ w( S( l# r3 e; C- e' V4 i - measureDistanceBuilder1.InfoWindow = False
2 w2 I h, h' ?# ] - ' n$ x0 m1 P5 d& G# ~
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
3 H& i; r& p% t8 o! [3 z( } -
$ h$ ]& ~, D9 y - Dim nullUnit As Unit = Nothing
! q. N3 r" J$ ?! P# S2 N" g -
! l) G9 [0 ?5 A - Dim measureDistance1 As MeasureDistance
# f! H7 N7 J0 M& X - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)- q) a+ e8 Z3 Z- e% B6 l
- , A3 G, e% X. P/ r: {4 b6 S
- result = measureDistance1.Value y. G9 }8 ~3 E; G* O4 P: [
-
$ f3 R% Q# H! f9 C6 }; }3 K' Z - 'measureDistance1.Information()
, z5 Q( a3 o; W$ }3 G& Q - 9 x% W! P4 j+ k; @ p
- measureDistance1.Dispose()( Z2 W1 o1 v2 n; C- l, y
- ' h# C% h3 Q) X: M! [- Q! Y9 }
- Catch ex As NXException7 P2 |7 c+ ]; a0 d
- MsgBox(ex.Message)9 O: K4 H9 u2 |2 [3 g9 Z }1 e
- Return Nothing( u" {! K6 \: A# H. L: s! x
- 0 I8 h' v& _ f8 s
- End Try
) A9 Z# |0 a; x3 `" S. r6 @: U - ' D2 ^. ]; {( w# R9 ?' C0 [$ B6 C8 o
- # ]; ]/ H7 ` C( w2 L
- Return result
- \ f% c2 a' [: q0 {- ?- O, B -
1 C+ [2 ~+ J( a$ ? - End Function+ m3 T1 \, |2 x. l, F: ^5 u
- , W8 R/ O& a+ p' f9 f8 P; _
-
5 \4 P9 U: @" x6 @1 h/ Y( \ U - Public Function GetUnloadOption(ByVal dummy As String) As Integer$ H7 B: K- M( _
-
2 e* w* G0 B& t* S9 H& { - 'Unloads the image when the NX session terminates0 G, B2 Y; v+ A C1 ?
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
' U$ u5 p+ e) S5 s. ?: Q- K0 i0 H8 A -
$ [ y/ s4 d" ~& } - End Function4 z1 s7 k2 e8 b) Q3 C
- ' O4 x H; k. J" \5 I
- End Module
9 Q4 H4 J) M9 E" }+ _$ f - </P>
复制代码
( g8 u# }+ k* l+ V5 D6 p |
|