|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
" t3 N5 o' v$ Z# M, |8 a& l
NX二次开发源码: 装配中选点,并报告父对象1 C; g. M1 P5 Q, x, J
" K/ z- L2 Z \% E7 U7 v! z- <P>Option Strict Off' Z: D j/ F8 a, J9 r6 K1 u+ Y
- Imports System
5 r: q3 {. D! y% R - Imports NXOpen
( [1 E" Y1 Y9 M# w b. p* M - Imports NXOpen.UF
1 ~! B! w: F0 k3 u - 8 C) r8 m# y9 K/ H
- Module Module1
( {6 e# Y; x3 W: l4 {3 Y0 u- d - 1 n" v4 @5 k( U' o# x6 v. [! ^1 b
- Dim theSession As Session = Session.GetSession()
$ A( @0 z4 G! @ - Dim theUI As UI = UI.GetUI()" A9 H3 A5 q% h
- Dim theUfSession As UFSession = UFSession.GetUFSession()3 E. W' `) g9 F' `' ]
- Dim lw As ListingWindow = theSession.ListingWindow
$ J0 M) L4 ~7 B9 l - Dim pickedPoint As Point
$ k2 j0 D/ ^! V" [" g$ D+ s9 i6 l - Dim myModelingTolerance As Double. O2 H% m g Y! `6 j# y
-
! ^3 J$ E# g! L - Sub Main(). K* j' ?! L5 Q" h
-
' ?2 |% t2 ]3 p5 }& f - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
; G( e6 u6 E3 ` -
. b: l. f8 F" h4 ~. r" d1 r$ T - Dim workPart As Part = theSession.Parts.Work
. p' m) ^9 F) _- K+ c2 A& a - lw.Open()9 K6 a! X! F, u f1 m
- * L! ~$ M) y0 j7 M6 z# E2 Z* w/ C
- Dim myPointTag As Tag2 \/ B4 i7 O" w* j
- " |9 ]3 A' Z* b( T; M8 p9 b. J
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
) w6 l2 j$ k% [! w4 s- s - Exit Sub9 a4 c) h% _6 }9 i/ [% u' K2 a
- End If' T" h: O. u7 |& \7 N! m1 q) n% U, d
-
: `9 ^0 J# K! Z& U - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)& _9 g! _7 m0 n' @+ B* L
- 0 s+ m, l7 Y# N v. K0 L
- AskParents(myPointTag)( b; g \& I: g/ B' E9 J
- ' c/ \2 B; k, a0 @! w" _
- End Sub/ \7 v* Q) d3 j
- + _6 S( M1 N4 D# o
- Sub AskParents(ByVal objTag As Tag), [# [* |3 I7 K; O( @! ]
- / T3 Y2 B6 t1 o/ d6 u' K
- Dim n_parents As Integer
: b/ _; |% H2 l8 y" F, N f5 z+ Z - Dim parentTags As Tag()
9 L- I0 z4 _" ] s - Dim myPoint As Point
0 X0 f/ ^- W, C% d- R - Dim myEdge As Edge
1 s6 E! ~) u4 s4 o, R0 o - Dim myXform As Xform; s4 T* k: D/ n4 {5 L. t' n4 H1 H. U
- Dim myTaggedObject As TaggedObject
# ^7 V4 B, u: _ - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
: G0 a) u+ o* k3 ~ -
& r7 J! ]& v9 J( k9 {& [# X - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then0 R p2 p! Z+ U( K& ]5 ^0 Y
- myPoint = myTaggedObject
: o* v1 A, y& K9 [ - lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)* K2 R7 { ^. p6 ]# L
- lw.WriteLine("")- K( G' @8 a* c. ?; P
-
6 Z* b' l! ^& a2 ?' J - Try- B$ y" d3 @6 |8 K. U( t
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)" ]+ \' n. r+ `2 N1 c0 p; |) K W
- lw.WriteLine("num parents: " & n_parents.ToString)) C w# M5 W, V0 X+ J1 w9 u
- For Each parentTag As Tag In parentTags3 h" X2 A3 q+ t+ y' F" v
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
. w, m d+ K' ~ K6 d0 ^; l - 2 v* H5 s( F: i1 g D* y2 j
- AskParents(parent_object.Tag)% {1 @$ U+ L/ L* }6 U
- 1 {. M- `. c: i; B* t
- If parent_object.ToString.ToLower.Contains("edge") Then. Z" L+ J, s0 a1 D
- myEdge = parent_object4 |/ E$ A8 [! H& d0 c6 c8 D2 c
- lw.WriteLine("")
5 L; x4 g+ j, s7 Q; G! h - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)6 T; N2 w3 {7 b3 ?( c4 x
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)" ~' I: y( b; q7 V* Z7 y
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)% g2 E k" k, Y; L- n5 G
-
4 p; L5 T2 j: c$ H& g3 ` - Dim partTag As Tag = myEdge.OwningPart.Tag
+ p: O+ \3 ]$ u6 `4 O8 R, K - Dim occTags() As Tag4 ?( s M' H# k+ L/ p: a
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)4 X1 s5 ]/ i1 l
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)& v" ^& y, O: Z! r3 ?
- For Each temp As Tag In occTags8 i3 @6 E4 ~' K. h! @
- Dim myComp As Assemblies.Component& h- G0 z, _% D3 U2 u) d$ u+ Y
- myComp = Utilities.NXObjectManager.Get(temp)
( U3 f4 B. S; S - Dim myCompPos As Point3d m9 Y$ }5 O& e+ N1 q {" b2 ^
- Dim myCompOrientation As Matrix3x3
2 N3 K& b8 E$ H5 Y - myComp.GetPosition(myCompPos, myCompOrientation)1 R& C! [- D7 y
-
0 i, ?) g. c3 W" ^/ ? - lw.WriteLine("")* }2 v2 ~* i$ H6 {7 V: E
- lw.WriteLine("component name: " & myComp.Name)& U1 O1 e: I. A0 `2 `
- lw.WriteLine("component display name: " & myComp.DisplayName)1 C: U, ^" ]; C( y% z
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)' W0 ~! y6 H9 b% P3 G
-
9 E% R! y1 F9 b: D, |4 M1 R - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
0 S$ G1 D ~: }0 k - lw.WriteLine("** This is the component that was picked **")
" P* ]' D5 [6 J, `: l - End If. y- J/ s8 |* u1 q5 c2 }
- r. n0 _) z, X N1 P7 g# ?+ M
- lw.WriteLine("")4 \9 }; V @8 E" ^$ k5 g
- Next6 a' Y( ]/ x. ]! r
- - e4 ~# V1 S, t9 _+ S$ M
- lw.WriteLine("")
8 r$ b1 m, @2 z/ P0 k. T/ G - End If% z! ~; @; K3 Y# s
- Next H+ I# t, q8 l
- , W8 G8 d: O9 h
- CaTCh ex As Exception' {+ _. M% J, D" I3 X V; ?
- ' NXOpen.NXException: Current object is not smart
9 ~; Y9 |0 h& s1 ?* A; i$ o - lw.WriteLine(" Error: " + ex.Message)( b8 b: G X( C* s4 _
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)
* B t; a2 n1 q% B - End Try
' g5 X9 r' C q: E6 F2 {* [7 S( D. { - ( u6 i9 Y7 A2 |( r' Y' ]
-
1 D' Q; B0 s, b - End If& K$ ?( G6 U, V ?" U
-
# p- s& A3 |, v; v/ { -
- B, z6 o1 ^- T! b$ `* V) W& @ - End Sub
: a: B5 Y4 h2 Y% b- `, i" j' g -
! g% ~6 x0 n2 o1 | - Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer x! ?1 B- @5 f, x$ k
-
. Q7 y# q6 k+ A - Dim base_pt As Double() = New Double(2) {}3 Y0 R# Y+ h7 \: m/ e( u
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null+ j/ w: g, p& k5 Y1 G4 I% h
- Dim response As Integer = 0, ?7 g0 N: m4 f2 b% w3 \- ^) S- P+ E
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt3 n: {1 n8 q) M
-
; p- T" G7 ^9 O7 \* n - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
5 P9 V5 u& i! b - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)* n, \- u3 l+ s8 K$ c
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
~2 I1 | |8 Q. R! X: | -
# y( o2 t; e4 K( { - Dim pointLocation As Point3d
) [4 m1 \" H" G+ D - pointLocation.X = base_pt(0)
/ o G5 R$ B7 |. h) b. P8 ` - pointLocation.Y = base_pt(1), F( M! M: ?; q, R# \$ w3 ~0 r7 |
- pointLocation.Z = base_pt(2)
$ L# U8 f. `9 }8 k4 J$ g3 m - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
! \, N# W3 @, v3 a, Q/ U* F - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
' v4 A2 q( r* j4 ?5 Y5 k7 c - , f0 V9 K' g& {1 b% D
- Return response- u/ S# A3 N) S4 u* q2 H
- 4 J3 d- V( t, Q( V. H
- End Function
) h* y; @& _( |& P - 1 m0 N1 E7 [1 v3 [4 H+ ^2 [ M3 Q' v# s
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response) a7 @$ p; v' t* p2 P! _
- : u" c+ K- r5 @* Y' M; z
- Dim selObj As TaggedObject
4 g- ~3 `6 f" y4 z - Dim theUI As UI = UI.GetUI" ?) T2 G2 k) B+ H
- Dim title As String = "Select a Point"
5 |/ q2 q' l# c1 R0 B( D/ s - Dim includeFeatures As Boolean = False
8 m: f+ A- I, M3 W. `9 v: n - Dim keepHighlighted As Boolean = False
/ ~* J1 O- N8 P3 A$ f - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific- I) s! {9 R2 Y- Z. l4 z
- Dim cursor As Point3d
; A8 i; Y9 U1 g, y% h - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
( s/ s! M- w$ W# z5 w V - Dim selectionMask_array(0) As Selection.MaskTriple Y o' d8 L* G$ E3 n
- ) o6 m& G/ W& }- C3 Z8 J" e9 [; p5 h
- With selectionMask_array(0)
6 F& @0 c' _% m) e - .Type = UFConstants.UF_point_type
% d K: x, M: S& O# g - .Subtype = UFConstants.UF_all_subtype6 v6 }1 {" |/ v* ?/ Y; U: d
- End With9 N' q4 U3 D$ B* Q( h% c
- ) {4 _: B8 p+ `2 V
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
# u( M* G7 B* d7 _ - title, scope, selAction, _
6 n, w/ l' u B+ i. p$ } - includeFeatures, keepHighlighted, selectionMask_array, _
^) o& D! g. T - selobj, cursor): `9 ?; d" T6 {" r
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then; l3 B2 o! j/ _8 b+ ~2 _
- selPoint = selObj
7 Y) t. x/ S' }8 E" P& z - Return Selection.Response.Ok
; A) @; H) ^- m& p# g4 O | - Else
; W& m7 M% A! i. w( i9 @ - Return Selection.Response.Cancel4 q9 I4 X; }1 a& \, v
- End If. _; {0 V& i' U& p6 h& E9 `
- / K( P9 O3 q* g8 F' J( `3 Z
- End Function
5 j: v9 v- m1 J% |' @8 F4 \ -
* t2 { v6 N2 P4 | - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
4 p9 a/ G1 J3 F, D - - x: n$ Y+ H$ a! e2 T
- Dim result As Double/ I5 S$ e* J& Z0 i0 q& R
-
5 k) R/ g* z* J6 b - Try) E P9 l) N8 R( Y# B6 _' N1 b
- Dim nullNXObject As NXObject = Nothing" N; v" i' \' B# g+ q
-
7 Z; h% b$ N. o - Dim measureDistanceBuilder1 As MeasureDistanceBuilder
6 Y/ g6 F* u# B( |- A( o _ - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject): F. ?% @) h/ G }
-
) D Q' v9 O$ z4 c' L; } - measureDistanceBuilder1.InfoWindow = False
( |/ p. @* {7 y Q6 K7 f6 j - 1 I! M3 o2 x2 o' C c
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum6 a% c2 L V9 n% G+ O
- " x' s2 C7 d+ _5 I# T
- Dim nullUnit As Unit = Nothing/ t) ~) J1 e5 z9 _
-
4 k: z2 S) `% G9 L - Dim measureDistance1 As MeasureDistance
8 a+ A0 v# k% a: G' L; }% y3 a% S - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)5 h7 Q1 H. X% r0 o: r
- $ i' ?& p* p" G, ]
- result = measureDistance1.Value
- {. r ~- R I& D1 K @2 p -
, H9 D1 U7 f- O- y" K - 'measureDistance1.Information()
4 F* c7 b; R: @ - ) v& ]1 e6 M7 Y; P. |3 p; ~
- measureDistance1.Dispose()3 i# k. {' U4 l8 d! }
-
0 ^& N) ~ U' T S8 b0 l8 s5 } - Catch ex As NXException
" g" L) K; x x1 I9 L - MsgBox(ex.Message)! h4 Q% Y( g8 D8 F# X
- Return Nothing) N0 w G7 Y/ z }# w0 w: u
- 3 r8 j! r; k/ Z/ H! `7 G, f* N
- End Try: [4 n6 W" `7 x$ Z7 _
- 2 `0 t7 R; W6 v; O
- 4 J" x, h1 E* @1 a4 C' Z$ `0 T
- Return result
, w9 A7 p& f1 E" z b8 v - 1 f9 b6 g% i6 z# \. h
- End Function5 a2 q8 K, h: V! ~: M* y5 H
- 9 D8 G! h* m3 g5 \- B
-
& I0 R. H" V% U% C8 Q - Public Function GetUnloadOption(ByVal dummy As String) As Integer& I, W% K# W5 Y/ L* E Y
- + s/ i9 p+ M1 Q8 s4 Z' ]
- 'Unloads the image when the NX session terminates9 P" m8 Z+ L" G
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
+ x7 X' p; c3 ?$ t3 x0 D' z - * \$ @4 u8 a7 B, Z( K
- End Function& g$ q7 F6 q( R' @
-
# S( K+ o* O: ?. s. D. G - End Module$ W/ {3 a$ C, [* G: r( ?
- </P>
复制代码
. G. u7 v, {! ^( Y' D: U6 w |
|