|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 I6 x, @" T3 o/ W/ m# h8 }1 YNX二次开发源码: 装配中选点,并报告父对象
# t; i; X7 z5 s$ _
3 u/ N/ W; N0 J( v$ ]- <P>Option Strict Off& V4 ?2 k. u7 c2 Q
- Imports System5 \# P5 T& S5 i% ^' n! N p' g
- Imports NXOpen
! ` ^1 Y# w4 W% W+ C - Imports NXOpen.UF/ H, x H6 d1 }- C Q* `/ q; M% ?
-
5 ^6 o& r9 B$ A% u, Y3 B - Module Module1- ?% h4 E6 z4 Z, a2 S2 B
-
8 @$ [ C# B& X* q' ] - Dim theSession As Session = Session.GetSession()1 a8 k' D) L2 C( G' t7 i9 h+ W
- Dim theUI As UI = UI.GetUI()5 u7 L% @& P" {
- Dim theUfSession As UFSession = UFSession.GetUFSession()
/ r1 k6 r L6 g - Dim lw As ListingWindow = theSession.ListingWindow. h" y5 x6 X+ p. s7 ]4 D5 G
- Dim pickedPoint As Point
3 Y/ K4 u) p) }( v8 u7 y3 I: S - Dim myModelingTolerance As Double) a1 c% ^7 w% ] p$ h
-
Z% {* }" M( v4 | - Sub Main()
+ q( d8 ?" G0 [3 [' c/ C -
/ A9 p$ N! B! C6 z5 w1 f - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)9 S0 K. {& D- K F" B
-
/ G# B( j' S6 j1 L t( t - Dim workPart As Part = theSession.Parts.Work* g a% P6 @: C
- lw.Open()1 S/ s I* e, @7 d+ Z' W
- 1 _" ]3 ]: p. j7 F1 y
- Dim myPointTag As Tag3 @/ Q2 P( w8 C8 ?& A
-
, q% Z* Q$ ]- w5 u8 ^ - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
1 p) a" D) z7 V& t( c8 D+ D/ f - Exit Sub, G+ I/ ?4 i8 {; B
- End If4 z. x* K% @" F. e+ e% V
-
4 _) y) O8 S: Z# ^) f, I v - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
. E9 ?! I r% m. A+ S7 U -
/ J" `2 z# o2 w8 b$ ]) K1 ? - AskParents(myPointTag)
* }2 u6 ~, e8 Q$ Q5 W -
" X# j9 }* I2 ?2 w: L7 q. @ - End Sub
; ?- \# D* i" B; u) Z -
P2 Q2 `6 k7 m' E0 W - Sub AskParents(ByVal objTag As Tag)
, `, S* F, u+ @- F* W) R - & Q5 h k8 D7 x1 L( ^/ v
- Dim n_parents As Integer5 `5 }, F6 F/ A, P# R
- Dim parentTags As Tag()7 t' b2 ~' e6 y* h) y0 r
- Dim myPoint As Point
8 W* G8 e: }6 e+ R( J( h) a - Dim myEdge As Edge
" |- G* h- A. ? l N, r - Dim myXform As Xform
" t6 d& e) L$ L' S- z - Dim myTaggedObject As TaggedObject
+ U3 l- H7 \ @ - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
. [( K5 z0 a- [8 v -
1 a. q2 R2 p. G' V7 {, M Y - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
0 h; T0 t7 b2 h/ m( ?" L - myPoint = myTaggedObject
8 _$ Z( `1 f" q - lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)* `8 |6 |, _0 R: F. E
- lw.WriteLine("")! l3 P; m7 ]2 r5 Y' O/ Y2 o$ E
-
! S. U' n, F; b0 c - Try
5 a% f5 y- Z, } y - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)6 i4 P# {4 v* v) t( D' p
- lw.WriteLine("num parents: " & n_parents.ToString)- s$ G% p* o+ J( u$ i
- For Each parentTag As Tag In parentTags
. Y% d, E+ J) g- E - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)8 F a. }6 D8 x$ r7 |, W4 E* _
-
0 m1 ?) l7 [: R' c1 c - AskParents(parent_object.Tag)
9 X G L8 \( @) l) l5 Q. | -
2 G* F* @% e! C# ]7 S5 C4 L - If parent_object.ToString.ToLower.Contains("edge") Then
3 C8 A, p! @7 I% X - myEdge = parent_object6 j, I% b/ D1 o& t: d U. b" P
- lw.WriteLine("")
% H; Y. A! d b6 s* _# ] - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
" e7 A4 Y0 c) w, h; V8 _ - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)* U1 T5 |- z U) ^2 q3 @. k
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)# x- {7 B* ~/ u- X
- " k5 n [! ?- x5 T5 |# [$ S9 e
- Dim partTag As Tag = myEdge.OwningPart.Tag6 e3 z1 S& v' b. c* Q5 w2 b3 m
- Dim occTags() As Tag. A# R. p3 g b. \
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
$ L! d) d& |( e - lw.WriteLine("number of occurences: " & occTags.Length.ToString)
5 o& k R Q5 N0 |6 k4 N/ I% O - For Each temp As Tag In occTags0 j) @' C8 i9 {
- Dim myComp As Assemblies.Component, O1 [" r3 c4 h2 t: t/ X) r
- myComp = Utilities.NXObjectManager.Get(temp)
. V9 b$ d) t6 d1 F( y4 x - Dim myCompPos As Point3d
8 X) H: G$ c( k3 M5 x$ o - Dim myCompOrientation As Matrix3x3+ u* H. x- n }$ `# Z( Z# _6 Y
- myComp.GetPosition(myCompPos, myCompOrientation): }* p/ j& D. y
-
# ?: b- e* f h+ \6 w/ ?. [) O - lw.WriteLine("")- G3 b; a1 J8 J* W: M! R
- lw.WriteLine("component name: " & myComp.Name)
% q2 v) ^( y1 O- B' z - lw.WriteLine("component display name: " & myComp.DisplayName); L7 L3 b6 a. Z; j6 [3 Y$ k/ K( E' ?
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
0 i$ c/ d: y5 ^# f. U* r -
4 g1 r% ?! x% t7 L6 C. | - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then" R$ I7 x0 B, i2 B T' X8 D
- lw.WriteLine("** This is the component that was picked **")
/ B* d8 U; E! z6 |5 d - End If5 l3 V6 m) Y8 E& y
- : g$ u7 s# P" G5 k' n. I' K/ O
- lw.WriteLine("")
0 V3 T4 {4 x( p+ I. L R - Next
$ {1 g9 H+ ]0 [0 O7 Q& N - $ _* q, [' J. ]( b
- lw.WriteLine("")
+ j) T3 ~! j# F7 l8 d2 _( U - End If0 D6 I9 w& @$ y1 k3 F0 G; S. j
- Next0 }0 g" V& C7 |4 |1 c
-
* h: l% C: U' c. p - CaTCh ex As Exception( l0 W7 B8 |: k
- ' NXOpen.NXException: Current object is not smart
8 F, r# p% f# \! ^5 t9 G6 ` - lw.WriteLine(" Error: " + ex.Message)& X }( s6 |+ \, T# I+ C. A
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)
) u) B% L- @6 o! K* x1 z, x - End Try
& e$ p* g; {4 |& ~/ ? - ! T. |$ D& p4 R, _! s1 \$ B. B
- / Z; c( f# S y2 H0 J p- S1 a$ k
- End If# b" N- O( [4 B3 h$ N& i3 s0 y
-
2 w* X; R7 t3 C; ^ -
8 m+ c/ A: ]3 N- h# q; l - End Sub
7 Y( c# x* a, W4 } -
/ }: Z* T7 `. J% c+ `1 w$ F8 j+ ] - Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
: O2 c. g, ]/ m( |. M# U - + b$ x; }0 M" ~( h5 L, B
- Dim base_pt As Double() = New Double(2) {}
# s b9 V+ Q F' Q5 x( z" | - 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
9 l( ?" k4 o- H2 u3 p - Dim response As Integer = 0$ p+ B5 c- p' B+ b2 [# r) C
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt/ \, V! H% `0 G1 R5 K- A
- , X1 E4 |+ }9 s( h% I3 \6 d x
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)9 a0 @1 p0 }* _% Z
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)6 _; A& g" G: }- o% A3 c6 K
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM); R+ v4 G* W" r# P% y
-
1 N/ T# P) I' f7 h' z - Dim pointLocation As Point3d% D2 Q6 |' m' e) N
- pointLocation.X = base_pt(0). `/ u. ?9 H% E6 i5 A5 W# e
- pointLocation.Y = base_pt(1)
0 x% e) S& o2 @. ]/ l y2 H' p" A - pointLocation.Z = base_pt(2)3 b, ` \4 I, ?' J" ]3 {1 z. J
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
1 q0 j% y4 c: d6 h - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible); n9 U. R% w: e$ u9 c
- 4 |8 P% [8 D" w1 C# ?0 V9 p
- Return response
z# x: y4 @6 c) r9 Y& ~# v% k) l -
4 T4 k$ A9 f, k+ l4 i4 V* Z p - End Function' U2 f: ]& Q& O
-
) ^) k1 c3 _% m# o6 ^) y - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response" w3 E( ?0 o0 T& U+ Q6 _
-
! U" \- j3 ^- B5 G. Y& S' [ - Dim selObj As TaggedObject# u( Y! |3 K5 T, J7 Y
- Dim theUI As UI = UI.GetUI
) y: |) G. \, X- F' a - Dim title As String = "Select a Point"
& L; t+ ^6 e6 P, j5 J& f; q0 d& s - Dim includeFeatures As Boolean = False
: e1 e$ u' f3 e) H# g' |$ C - Dim keepHighlighted As Boolean = False5 m" p# ~* i$ E3 W* k5 M h
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific/ g9 Z0 ] Z, z3 w
- Dim cursor As Point3d
5 S: d4 _/ x9 h0 F4 }$ V - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
7 C4 f6 x% K9 d9 I - Dim selectionMask_array(0) As Selection.MaskTriple# s' \% _* G8 L' p! O: Z1 A
- 3 ^8 |9 S$ x) \# }! J, o5 E
- With selectionMask_array(0)6 ?( a4 ~) X* S. x7 w9 a
- .Type = UFConstants.UF_point_type1 J! {* J& s! a4 }* U- D
- .Subtype = UFConstants.UF_all_subtype9 T* a* a# t# F; E
- End With E& k8 @- n/ R; D
- . @: N/ z. d" c% j* }
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _5 V/ R& {7 N2 O$ T4 R
- title, scope, selAction, _
( B4 \7 K$ D- z$ W# c [ - includeFeatures, keepHighlighted, selectionMask_array, _
. n) ?& s L& }. x0 {8 [ - selobj, cursor)- f5 A8 M" m9 _' ]
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
- m @4 W9 d- F i2 M4 d6 D5 k - selPoint = selObj/ k7 F, |5 k3 [ U
- Return Selection.Response.Ok
* O! q6 v* W+ Z7 K r" y7 U - Else( a7 p; o k: f$ B8 G0 z
- Return Selection.Response.Cancel
& z" F: t" i# c - End If
3 _9 {+ k) [( Z8 i: e) @ - ( L. U. z8 ?& _/ A% }& B
- End Function
" h+ b* T, C, D" r: Y -
# _$ B, i0 F4 V - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double3 L/ `) `+ a. [0 W
- : A9 B" X" x* X/ ^0 Z$ n% d; I
- Dim result As Double
) s8 |& C9 P9 c; z - * \# \/ x$ p3 t3 Z$ ?
- Try( a6 q3 g* S1 A& ?. v
- Dim nullNXObject As NXObject = Nothing6 `+ L2 H: e& I% N# f5 E
-
/ L) U# R. d8 l* C - Dim measureDistanceBuilder1 As MeasureDistanceBuilder
. W# w s9 V+ j& P! \( ? - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
! Q7 V+ N9 y, \7 n; w! Z) O Y% o - ! N+ S x9 N& Z
- measureDistanceBuilder1.InfoWindow = False
) b1 F x$ ^. E% { y% d -
# H5 v/ O l9 N7 G' B3 k- M - measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum' _ L% s7 N- G* O @
-
8 f: ^, D# ]' h8 \- {$ A, ^1 e - Dim nullUnit As Unit = Nothing% k& R8 X6 \ i4 A* `; r- G) u7 h
-
" G. K# y8 r0 O+ d5 K9 z/ [ - Dim measureDistance1 As MeasureDistance
7 D6 x* M1 L8 Q7 c - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
' @. q% f7 u* Q& X7 ?# P9 W$ J - $ e" x+ _- l) K& N' _# h' R6 e1 ?
- result = measureDistance1.Value
4 Y2 D& F2 j' O# s - : C2 E* A& ~# b, N) \+ V% Z1 y
- 'measureDistance1.Information()
( i1 N& V6 V9 W0 \ - # q6 V: R6 d) k
- measureDistance1.Dispose()5 N1 X) Q# o' A' w) ?* a
-
4 _( k. ^ C/ n9 E - Catch ex As NXException. l* N- l( T! I( i# J3 P: y0 Q" l
- MsgBox(ex.Message)0 m6 F% B9 M {% a% K- f
- Return Nothing/ c' o. m* t4 S2 a6 E
-
! E* E8 t1 a) o6 z! F7 j6 |9 E - End Try
1 S4 n& J" Y- \2 j5 { -
6 d( c0 G8 @+ n5 z, x2 E6 Y - & L/ j5 d1 n* g. ~& i
- Return result
8 {3 @, e7 L: P3 r' b: c - + t; O, D# |5 K
- End Function; X2 j: v. T9 a' f" a/ q) k
-
) `3 t. w' d, y" `# B/ r - ' ]9 Q! Z' [3 Q, ^9 h
- Public Function GetUnloadOption(ByVal dummy As String) As Integer
0 e3 e4 v+ L4 A - 5 o* u5 P4 f) I! u, ^' d( y
- 'Unloads the image when the NX session terminates
& D; n4 K/ N) ^6 Q) n - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
: m" @9 H/ n1 n6 a: Y - * X$ T4 i, w S+ i
- End Function
/ z& h/ N8 A, b1 Q+ l - 9 N1 b' q& |7 o+ ~. }. z
- End Module
v; f# _+ z2 ~: c - </P>
复制代码
. C6 K( f0 ]8 S/ L3 b) R9 Z+ w |
|