|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
. ?, _/ m+ I! E4 V. @
NX二次开发源码: 装配中选点,并报告父对象4 j: P5 |- ^* ^- p) {& i# w. k
0 o# u% z# w k9 n% u; n- <P>Option Strict Off
% q3 f( H4 r0 }" X$ ]! D- j - Imports System
, ^6 l" R# }9 H) A - Imports NXOpen
2 g" W! `* |0 a: @6 m. ] - Imports NXOpen.UF
; b# S( B- L3 j- Y; h* {+ e- { - . O2 m. q( ]' n9 [& d
- Module Module1
3 r$ n# D6 x. c5 Q2 }3 f5 a5 b- S - - j" |1 J' _& _0 i8 Q
- Dim theSession As Session = Session.GetSession()
. N6 A/ Z1 Y1 T- @% G) [ - Dim theUI As UI = UI.GetUI(): p/ C5 \* X& w5 t7 F" }3 W
- Dim theUfSession As UFSession = UFSession.GetUFSession()" y+ ^! y7 z* h! R4 n4 E
- Dim lw As ListingWindow = theSession.ListingWindow; v# G4 P" Q# y. r8 ?# D3 z
- Dim pickedPoint As Point
2 n) F# Q- K+ G# ?; _8 T - Dim myModelingTolerance As Double1 L, d# N0 U- C7 O8 y- S; Q1 G
- , _+ C# C- J0 w8 g+ h* H+ P
- Sub Main()
5 G; p; z4 b& X) W( f - & H; L/ n- a3 x. c/ d. A8 p
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)! ]! q/ G" m* ^& P# q" k- H
-
J% O$ P7 o( j; D% t1 T - Dim workPart As Part = theSession.Parts.Work+ W& n5 r$ l3 ]/ T' [$ R
- lw.Open()! O2 a' {5 P) R5 E3 O( L5 s$ Q
- 9 u6 Q3 b, W+ U$ A
- Dim myPointTag As Tag- D* @9 Y, l0 R- W( y5 {4 a
-
" d$ y4 h$ j( i: d - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then" J# U6 B' }- W3 X
- Exit Sub, K$ g7 w+ M9 Y
- End If" R: h& A# i9 i& `0 a7 y6 g
- ) f: o# l+ c) b3 H9 Z( {( x. p
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString); Q. a9 D# e; j& ]
- ( s: r0 i% r& M2 B5 M' z) o0 g
- AskParents(myPointTag)/ i7 H8 F |" L
-
/ @7 n: e: Z' \) ]* e - End Sub1 g- c& W# T6 U+ H
-
/ G% m0 R. J6 U. T# P3 z - Sub AskParents(ByVal objTag As Tag); y3 _0 Z% e6 S7 E/ g' ]0 p
-
, Q- D5 ^; L0 Y; d9 G4 i - Dim n_parents As Integer7 L. J4 t% k1 T$ Q8 U
- Dim parentTags As Tag()
- z9 M- W3 N1 d6 i! d - Dim myPoint As Point: z9 v. |2 s' o \( O
- Dim myEdge As Edge
# t0 ]* U$ _( \$ s) \3 j - Dim myXform As Xform
3 w% M: ?0 t( B+ \9 L9 m& ^ - Dim myTaggedObject As TaggedObject
! @6 D2 M- w- K2 ` - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
' S- v; L8 r3 _. X! G, b) T+ D+ t. `/ m - + |/ W' {$ V- n3 o
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
n* @5 [8 o! n0 ^ - myPoint = myTaggedObject: V! t0 U; D. K+ e9 {* n* a; w3 @/ H. ^
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)1 F3 E3 ~2 n( ~8 l" |$ P( @! L
- lw.WriteLine("")
6 m) _" n, ~/ k1 c, | -
2 E" t o' ~ M7 I/ X' } - Try
0 L8 v7 C* u4 Q c - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)) Z' t2 E+ m, B
- lw.WriteLine("num parents: " & n_parents.ToString)
' _& u. F7 A$ Q6 R, I - For Each parentTag As Tag In parentTags
5 A+ A. M* f G5 E& N5 Q; C) M - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
4 y: k% x2 Z' t6 a: W& o -
0 f3 O1 ]4 m4 U2 R. g/ e - AskParents(parent_object.Tag)
+ x; l, W" J% N0 J -
, I' e" D/ y& _ - If parent_object.ToString.ToLower.Contains("edge") Then
, F& F: E4 c5 t5 p: U( A4 f* d - myEdge = parent_object
1 K3 ]9 O# l- C6 P ?( p - lw.WriteLine("")8 J1 H* t+ h! t6 y6 G0 o1 W
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
6 x: m4 p5 Z+ ]3 ?0 Z3 L - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
0 R" x, \' H' c2 l# v - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)) k( i1 _ y9 E9 `6 n
- : H0 g _9 _7 f$ o$ M5 W1 g6 `
- Dim partTag As Tag = myEdge.OwningPart.Tag
& K6 o2 T# c8 x - Dim occTags() As Tag5 H7 [, T4 @$ {8 y
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)" M1 D; p' W G+ e1 H, e
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)0 N1 u3 e8 m/ ]
- For Each temp As Tag In occTags) O! i X1 l; y4 C" u/ z
- Dim myComp As Assemblies.Component$ E& x l0 A; `0 i
- myComp = Utilities.NXObjectManager.Get(temp)& G( h. l) ~! d1 |7 T5 z
- Dim myCompPos As Point3d
! W& @0 ]; f' G( L" r) T+ |) Z# ? - Dim myCompOrientation As Matrix3x3
8 H# D# D# F- i8 L8 F- b, ` - myComp.GetPosition(myCompPos, myCompOrientation) y; X$ d5 e* a$ D* D
-
: e$ t5 q) z$ X. U) j" [ - lw.WriteLine("")5 k! q5 p3 b0 p- F+ C% e
- lw.WriteLine("component name: " & myComp.Name)
6 Q6 H8 J) d. p, c7 l - lw.WriteLine("component display name: " & myComp.DisplayName), k9 l* z! e- I% |6 ]0 J1 |+ x- B0 c' I
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)1 q j# i; r! E" C; M! j, r
- S1 u% b/ a1 M J
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then/ y/ e; }9 l7 W
- lw.WriteLine("** This is the component that was picked **")
$ z, r0 a) U/ e/ M5 Q0 J( r - End If
6 p: S) o7 `! u- J8 I -
* @4 n3 ^7 L! \. b - lw.WriteLine("")
5 s# O" p1 P2 C* m4 n" P - Next4 P4 Y+ z4 m, ]. e2 T( t! `. E, O, w
- # M. K5 I+ s m. U5 b7 k
- lw.WriteLine("")" e9 l! V4 p) K; [1 {5 c7 v
- End If
- q' O) \; F4 x" A - Next
6 t" B% E/ v# f3 `1 G; L$ p" q - ) H8 z5 M8 X5 `9 U8 c
- CaTCh ex As Exception F* d {9 c! _ M
- ' NXOpen.NXException: Current object is not smart h7 G) X& p% }; X) k' \1 B/ x
- lw.WriteLine(" Error: " + ex.Message) o( G E* i' g8 H8 Q1 g( j
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)
4 w+ U5 V% L% b$ s9 J - End Try2 w; Q) A: K0 G# s
- , {" R6 d- \% g5 F' Y0 D% S" t# _' P
- 6 V0 b. r/ e- Y0 w) t
- End If
/ \6 r- x" J, D" F- a& m0 B8 g4 E - , y) w+ N3 }. V6 p" l/ m7 \
-
P& z4 v; `6 N* k8 p - End Sub9 U$ G1 p2 x9 O2 M' r2 X, Z4 l4 i6 ^
- 8 G/ n( z! J ^! j% x+ H+ Z6 b
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer6 o x8 X& ]. x/ d8 }. d
- ! @. I& ]7 P# V6 V0 s
- Dim base_pt As Double() = New Double(2) {}! _; |+ y( v0 K9 ~) J9 T3 y
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
/ X" q5 i3 l% _* Z# `: ]$ u0 k - Dim response As Integer = 0
7 ~- q+ P1 R a - Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt, O* N7 K/ l" Z) I4 |; a5 A
-
. b- K7 B) l; O, L7 ?; S6 l - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)4 k1 S5 `0 a1 {# Y' j1 F# O; Z
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
) e' c% e1 @/ n n1 F7 ?; C" x2 G2 s - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)% w/ @; D8 }. r, G8 l o3 l
- 6 `/ w! q: H% w* o$ [- ] [7 G c
- Dim pointLocation As Point3d# {$ K5 y/ g: w2 R5 U
- pointLocation.X = base_pt(0)
+ J% J4 I$ l. J - pointLocation.Y = base_pt(1)0 Z$ n/ ~" ~7 G
- pointLocation.Z = base_pt(2)" x: d' H% ]! y- v5 j( W
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)5 i2 ]' \3 V3 R; u' D
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
# r. f( [/ p2 X( ~9 n3 h8 J; S8 a - ; V" C- p' \7 v3 w5 q1 |: k
- Return response
7 U3 U$ [; `' {3 d& i/ Y - 4 w: M( y- Q. h$ A# }
- End Function
$ N0 t1 j3 T0 a0 e -
* ~: e- y' W- r! J# W! }, U7 b6 y - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
/ }9 V$ |; L1 M -
) Q1 s! _1 x6 R+ K' Q% S - Dim selObj As TaggedObject# N. j j# K6 Q
- Dim theUI As UI = UI.GetUI
0 q' Y8 l* I$ s& e' c - Dim title As String = "Select a Point"! U4 F# P/ W& [
- Dim includeFeatures As Boolean = False
0 n" q" W" p% _8 { - Dim keepHighlighted As Boolean = False
/ x! b9 a" q$ J; ?- F - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific. O) q0 G2 e' C: E* n
- Dim cursor As Point3d4 S: n5 _! H1 d& O! w1 j; e
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
/ u: \5 f: h( g( Z* ^1 @8 h - Dim selectionMask_array(0) As Selection.MaskTriple7 d8 Y- L) ^. R; J0 m7 s7 |
- / A; o4 d8 G; s$ |
- With selectionMask_array(0)
) Z( I6 K( K; B0 l, t - .Type = UFConstants.UF_point_type
8 r7 t2 }0 b3 T! h. H/ ?2 ]7 I9 d5 a, ] - .Subtype = UFConstants.UF_all_subtype. u* @+ K- X7 z) G
- End With% [- r& _3 }4 r% V. }& k3 ?
-
) g9 P7 F* s; n9 F6 D' @1 h - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _5 m0 A) m& J i2 b- I9 P9 u
- title, scope, selAction, _# J5 J& d3 Z) g
- includeFeatures, keepHighlighted, selectionMask_array, _5 J7 G4 `/ P5 S* z( {
- selobj, cursor)
; F2 ^$ ^: d$ d9 J6 \9 b' Y - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
O& M6 Q$ ~6 Q - selPoint = selObj A7 m* C+ t% u7 n/ W& E d# H
- Return Selection.Response.Ok& g' F) p7 B+ y7 C
- Else6 \5 l2 k7 B$ A% B+ t
- Return Selection.Response.Cancel
, e5 r- t9 L6 q5 q( r$ N4 a, N - End If9 g) t4 J1 y2 N' y
-
; V3 h% T3 L3 M6 Y - End Function+ H- X1 {* z' B3 A2 T
-
: b) Y2 ?" S f. ~& E$ T% o9 s - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double0 l6 g' n8 f- v, |
- , }( n' i9 b4 w4 L- }" e O& J) K$ j
- Dim result As Double
5 L- d& g2 t3 p -
! g# W R1 q! ]# v) q& {% n. Y - Try5 g, S$ p) e+ K# q
- Dim nullNXObject As NXObject = Nothing
! u8 y8 H" ~3 I% L& U6 I- C -
! v# E2 @, C6 O" ~5 u' g - Dim measureDistanceBuilder1 As MeasureDistanceBuilder
( g0 @, i) I+ x4 Y* ~$ g% e5 _ - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)! _( v4 s0 @7 a Q5 W, L" x6 j; ~
- 2 Y, J [- {5 e5 \1 L! L
- measureDistanceBuilder1.InfoWindow = False* o9 b3 Q3 z" t
- - t% s+ _$ X- i+ w- D
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
: A1 Q5 N' A4 H: P* u3 S - . r* i8 i; E" b j0 i
- Dim nullUnit As Unit = Nothing, m- d; Q$ a5 V- ]. Y8 H0 B( Y
-
* i% r2 `, V! }+ C( ` - Dim measureDistance1 As MeasureDistance
% Q# i( U4 o* W' b - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
' z% {/ z1 F. j2 i. Q -
+ `/ d3 |3 x ~5 N - result = measureDistance1.Value
# E6 R9 J' D( [( Q% a; Y -
, J5 k/ |1 x0 ^) }6 c# L, O B - 'measureDistance1.Information()$ V# }1 W z; d$ D$ ?2 i' D
- $ U8 L3 \/ Q n' Z% L7 `& |+ M
- measureDistance1.Dispose()) F4 N: M3 }5 z% L5 p2 d
- P1 d5 G) s4 ~3 O
- Catch ex As NXException% b- {# y1 w1 ~7 H2 ~
- MsgBox(ex.Message)
E' w$ f) Z6 B3 G: J - Return Nothing: O6 v% V. x/ M. ~8 a( k
-
, \/ z0 k2 w' D$ c. z: ^4 O" W - End Try$ ^7 P9 e* v1 L7 e- \
- - g+ z( U( j3 |8 g' B
-
1 F$ e! `4 j; I7 y: ]8 e/ q - Return result2 I" T/ B- N: m: R) x4 T
- ! m s3 @+ x$ Q% w f
- End Function/ z$ ?2 F% @5 s- j8 P6 L
-
]+ f+ \& E. j4 ~: e; N - 3 |' G1 _6 k( q$ G1 B1 {: z
- Public Function GetUnloadOption(ByVal dummy As String) As Integer' Z5 j3 Z' L* A
- / A# t! B2 x& A# S& [
- 'Unloads the image when the NX session terminates
* C; F; [8 { s3 X7 `9 P0 L* g - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination) ^$ Y! W! e- N3 Y7 B9 A
-
7 z" z3 g% w( z - End Function
) b; _8 y0 B& J* p1 Y0 Q9 _ -
! c$ U0 J- e% B! n; K; D1 p - End Module
1 V. m+ `- C4 o: |/ L7 L( b2 k4 v - </P>
复制代码
4 T* ~- Q5 ^/ ^5 e" m; b |
|