|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1 M# U3 o; J+ m5 q
NX二次开发源码: 装配中选点,并报告父对象% B( R2 k3 E* W) C, m
- ) _( r: B* x. S( c
- <P>Option Strict Off; c, c$ {! l+ f4 q! j% V! B
- Imports System
( U: s" Z7 W/ C1 S2 J7 k+ \1 b; P - Imports NXOpen
" R5 L* f Z! t' F _. D6 ^; f, C. u - Imports NXOpen.UF# ~1 A. V$ S3 ^1 r# ^. R1 y4 C
-
, q/ h9 N. ~3 [# v4 K8 \ - Module Module18 w% H: z1 }6 _/ f1 Q3 L0 v: U
-
: M: V4 u7 q% ?( ` - Dim theSession As Session = Session.GetSession()
5 j; P! q5 Y" g# w3 D - Dim theUI As UI = UI.GetUI(), l4 \2 o7 F& A3 s
- Dim theUfSession As UFSession = UFSession.GetUFSession()- c# y5 h# p/ a/ R4 Q$ Q
- Dim lw As ListingWindow = theSession.ListingWindow
2 Q5 _' ^" S" K: P8 p - Dim pickedPoint As Point6 V, s# j# |) Y/ c3 ?
- Dim myModelingTolerance As Double6 @: o! y9 D: D) x+ L( q
- / J( H/ S; ?& S9 p) U5 ~
- Sub Main()
: j9 X3 o0 k: [( u) } -
5 @. m- B% o- |1 U. \1 ?, ^& L - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)8 @5 A3 p4 U% i1 H- y
- + c! O7 i: r2 v* Q- ]7 A1 \
- Dim workPart As Part = theSession.Parts.Work% N( l% B2 I" N
- lw.Open()
( C1 Z0 |) t+ O0 c* F* W* O, ] -
/ u! v7 u& _) s2 L* o8 M - Dim myPointTag As Tag
! f1 p+ |1 Z; d& y - 6 X5 F5 T7 E) g* R5 s- n
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
4 U# P5 u5 B: B! ~6 M2 }8 j - Exit Sub. f% _# ~. p/ M" G1 d
- End If; {# y3 e# C/ v
-
* [, T; ?9 R2 b. J- Q: Y, s9 F3 | - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)* ~$ B% R# f! h( P: L& t! f- |
- " | Q% }$ _4 O
- AskParents(myPointTag)
! U. ]9 G; U; U% i' B- a" C l -
# a. Y% d- o' ^% j5 q. w - End Sub
. _( C/ T$ x( G" X+ h1 k - 7 T R# y& M# J+ s+ f
- Sub AskParents(ByVal objTag As Tag)$ B( z% D# z5 L& C- b: s
- 8 N6 _! ~' j4 ` a/ m& ~
- Dim n_parents As Integer0 |" V6 I" C% K0 B
- Dim parentTags As Tag()2 r% d+ w+ C2 g, _1 `; _
- Dim myPoint As Point# d" U' I% F8 J6 K, A$ q3 v- I* Z
- Dim myEdge As Edge3 s; X3 a$ x& u, k F& F$ {8 F+ }
- Dim myXform As Xform
2 }, g' n: w: _- K& X - Dim myTaggedObject As TaggedObject
) f5 K0 I. r5 L& q - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
" i( s8 B; T8 | - + B" ~$ [* c( u
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then8 \! X4 d; b8 B. j4 b" V
- myPoint = myTaggedObject
4 B. ]" m% X) ~+ P% C8 a3 w) G - lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)+ ]+ z$ D1 l& w' m
- lw.WriteLine("")
* m7 i0 T$ G, m1 j: l - - ~3 a- p# _6 ]+ F1 J) K1 m
- Try* T1 f- a% Z8 ]% O, B* h
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
1 D- Y0 L6 V& a2 M. r - lw.WriteLine("num parents: " & n_parents.ToString)
' e3 {* Q+ f& Z$ s# C1 [. b - For Each parentTag As Tag In parentTags
. X/ V( V8 l1 X7 p - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)+ q5 B; i6 ~1 \
- ( x5 Y8 D2 Q, ~5 \& \2 O# W& I) L+ B
- AskParents(parent_object.Tag)# s! A4 Z; k" D" D3 R5 v5 w
- " p2 d& o. u9 A) s
- If parent_object.ToString.ToLower.Contains("edge") Then
. R9 R1 K! I- t4 C$ O3 j; a - myEdge = parent_object
6 j5 x$ b( D2 W- j* d+ d - lw.WriteLine("")
% b @' }) k) E3 i" ~6 T, a - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)/ S* ?8 a, b; j7 F8 o
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
9 J9 O1 _0 ]1 s! p+ B; ^4 K" L1 y - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString): y% B2 p. q0 q9 K0 R) F) l3 e" C
-
& d* i% T5 Q9 W - Dim partTag As Tag = myEdge.OwningPart.Tag
6 B) Z$ P- w0 z- k T! t% g* U" ^ - Dim occTags() As Tag. g3 M0 a; F& I6 \. E
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
9 k& D- V/ Z! X* ] - lw.WriteLine("number of occurences: " & occTags.Length.ToString)5 G+ w# j% V# T! X& W5 x/ d' V& H: D& `
- For Each temp As Tag In occTags4 M8 v# v d7 j- L( V2 b/ z
- Dim myComp As Assemblies.Component, A( N& o# h; \6 w
- myComp = Utilities.NXObjectManager.Get(temp)0 D/ p! t; `, G7 b, Q& H
- Dim myCompPos As Point3d
* C( q* A- @9 |! y9 k, ^5 [ - Dim myCompOrientation As Matrix3x3. Y# l8 K+ s" x. R
- myComp.GetPosition(myCompPos, myCompOrientation)7 V+ g5 M, ?# f& Q7 Z' X8 G
- ) M# @) W3 S8 x* o! G
- lw.WriteLine("")3 a& D+ r9 R; N
- lw.WriteLine("component name: " & myComp.Name)
. [5 W" ~, C Q - lw.WriteLine("component display name: " & myComp.DisplayName)
9 D7 ^' f: x- i* C+ I' r( h) ` - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
7 R7 X: k' J4 g- \- H7 c0 N3 e$ c8 J - 8 V# O* A" i$ m, `) u
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then d, b& j3 T" f6 I3 E
- lw.WriteLine("** This is the component that was picked **")
# a2 d, ]# O' C' x& \, z2 o6 }- P1 h - End If0 N, d- ~0 _" X# P9 y! Z# p9 |) e5 n
- ( N; e. m2 j+ Z8 C& j+ m+ L
- lw.WriteLine("")6 l7 m7 f- D6 h
- Next3 @1 A+ x- ]' W1 B7 w
-
6 }; _: K- ~& y6 r1 K0 P i - lw.WriteLine("")( t) ^& D2 j/ C5 _ t; P3 F
- End If
( Z3 ]/ _2 n% F, W3 N - Next# J4 c! e5 k( W5 u) } j# m
-
, V! q. O5 P- ]9 W& x: o+ Y - CaTCh ex As Exception" `7 [0 O& }- U. f
- ' NXOpen.NXException: Current object is not smart
3 h% x u9 m! o& k; R& y+ p' L - lw.WriteLine(" Error: " + ex.Message)* P' a4 z+ y( n" j" K' K! p; s1 S& d
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)$ A8 y2 Y& a) B
- End Try
5 g4 [8 G; L' r( K' ] -
" N! U6 l7 a; n4 p% L1 T: | v -
4 ` t: Y! W/ [3 }( c7 E8 \5 k - End If* Q( m0 X2 S+ P/ K) B; p& u# Z
- / [% F7 a1 K2 z/ ^* X
- 4 U3 m0 v" {* h4 T
- End Sub Q6 t* ~1 t2 \. ~8 |
- 0 m. f; r3 C1 v" R1 N! e
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer- f9 u; P4 @' b* [- `
-
0 ?4 S2 D) R; v. Q5 I$ M/ d - Dim base_pt As Double() = New Double(2) {}3 u, l K; w; n) V, G* a4 ?
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null0 Y) h( ]$ p) W- J% E
- Dim response As Integer = 0
! K, P# [. C7 H - Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
4 p( q. G6 A3 `2 C3 K. K- @ -
+ i5 e' I8 b5 K6 ] - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)5 L; f6 L- ^$ B+ Q6 G0 Q, r
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
0 w+ |# e# H' Q d6 Y4 q - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)2 C: m' f3 ~4 q( A2 z6 V
- - D. v' f. e- v. t& M
- Dim pointLocation As Point3d
9 Y/ H: P9 c* U) |$ Q6 y( G5 v - pointLocation.X = base_pt(0)
& [( R1 h% }! ^ - pointLocation.Y = base_pt(1)$ G. W9 w& t7 x" n5 d
- pointLocation.Z = base_pt(2)
: M$ r4 P+ W9 K4 T5 g) w - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
! }8 K4 s! n: T# A# ^ - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
' ?4 k: n! g5 b# p2 j1 ?. J - 8 |* d& D# ~% E' y) U
- Return response0 }/ D& u0 Y. s/ s1 B5 d3 Q: m
- & s& k* e4 k1 T7 b/ M- y
- End Function
$ s2 U4 P4 D7 ~0 _6 k' o y" z1 Y -
, h2 i' X" a' z$ z1 N9 g - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
6 Z% ~( N" [* N0 Y, x% y - % f& n% K2 t, G
- Dim selObj As TaggedObject
' x) W0 e3 g- `8 U, | - Dim theUI As UI = UI.GetUI) ~9 R; _* U! S! B o7 e
- Dim title As String = "Select a Point"; h1 h3 a. q+ E( H
- Dim includeFeatures As Boolean = False
$ @3 x0 H: b$ v/ ~# P7 k; f - Dim keepHighlighted As Boolean = False
0 F2 O6 U" _. V8 n- R - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific: }& d4 a$ E/ k9 ]; ]! a
- Dim cursor As Point3d
2 U& x+ h, l4 `2 G! g$ M - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly `( k9 I+ ]4 ?3 S1 m6 S
- Dim selectionMask_array(0) As Selection.MaskTriple% r$ Z# a) T+ M; r
-
4 N, o/ c; }. Y& F4 T - With selectionMask_array(0)
* ^; z0 F% z2 A o/ ?- { - .Type = UFConstants.UF_point_type
% v% m' W* Q2 X& [' ~ - .Subtype = UFConstants.UF_all_subtype
7 f1 _ f* V$ O7 } - End With
; m# Q a" W" b+ E' w. l6 v* | C1 P - q& X6 p% ^1 Q; x- R2 i# E6 z0 y
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
% w1 \, k/ T6 c# H - title, scope, selAction, _
" K7 P/ r* a' B5 c& }" H" Q0 z - includeFeatures, keepHighlighted, selectionMask_array, _. T& {* p. {7 U3 [ s
- selobj, cursor)0 r7 O2 B2 `% w' V
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then6 U9 h0 b1 [. ~, w8 a5 {% ~0 |. C. f
- selPoint = selObj
$ X7 Q/ C! [: @# j - Return Selection.Response.Ok
% t4 I8 Z& z2 q/ F# D ] - Else
. T: U5 C' I9 n% r - Return Selection.Response.Cancel( d8 A$ B" M/ h; N! s! Y
- End If H$ A1 s/ b' _" I/ N+ g5 `: E
-
3 L/ K% x: u( T$ C. p - End Function/ f# V Z/ O: Y7 b1 r
- ' j! H8 @; J2 F8 r4 h" w# |& a
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double! X. v9 ?1 ^, i. {
- ' r0 U' v$ d5 S# L8 h% W
- Dim result As Double7 T, s. H2 y6 z8 Y
-
( N. l( }* C( J7 U: |" Z - Try
( d! N/ U; s0 Y8 n* j: e1 w' d - Dim nullNXObject As NXObject = Nothing$ |2 f0 p7 b: [9 v+ s
- % G0 Q3 H$ C. ^3 R2 _) G! L
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder: e% t; ^- H" d$ M+ Y0 P L* x
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
( V; R2 d1 m7 U+ e& L0 w5 _ - 9 E/ m+ O+ G p1 `
- measureDistanceBuilder1.InfoWindow = False+ B4 K& j" V: c$ A& m# y
-
1 c& I- g- R3 j6 L& e9 y - measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
, r3 ^) i7 F7 A& ` -
5 i" M6 _8 \' f" w - Dim nullUnit As Unit = Nothing* r7 |# | w+ ~7 i
-
$ ]0 F( l; s6 I2 A5 h - Dim measureDistance1 As MeasureDistance
, n7 k/ r: }# u* s/ Q& u - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
" O* d; `' _* { - 7 p7 ^' {1 _- Z
- result = measureDistance1.Value
3 j3 p1 A# T8 x' ^& |3 B! l - # k( v9 a) j l
- 'measureDistance1.Information()
. j9 q9 a, P' g8 u- c2 X" Y& p4 @5 j - W* O4 @0 m3 G; F7 O0 u1 h
- measureDistance1.Dispose()
4 O# A/ ^' O6 z/ o7 {$ E, |2 X4 K2 Z -
, J G8 t, M( `' H - Catch ex As NXException5 s4 g: ?- w3 [ ^9 C& U5 ?
- MsgBox(ex.Message)3 w& a8 ^2 |6 x
- Return Nothing
" d/ w ~8 ~; e& q& @2 i. ^ -
+ ~$ ]/ `1 h( d7 u1 h, d; K# G - End Try
& ~# ?9 e X; e l+ D! w -
# j: P+ A4 v" r& r -
" F4 `* a4 J' J2 z) z - Return result: f0 M# t" k( t7 O( F
-
4 m( |- P" z ]6 }1 O7 W - End Function! p" l( \* S' a0 [3 l7 g
- 7 \" S6 a3 E: Z" ~( t% Q4 [: s! T' e
- 4 V+ o; B# s6 }
- Public Function GetUnloadOption(ByVal dummy As String) As Integer- e1 h6 j4 Q/ B; l1 K7 J5 E
-
) ?6 X% H. ^/ e; j# ^ - 'Unloads the image when the NX session terminates
. X2 W4 ^5 C( Q% V( g6 z( Q - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination0 {8 n* l2 b7 }% X, w2 j
-
/ v. t! s5 s' `, ~ - End Function! Z+ z4 _4 i' S: g
-
6 e6 O% P" n& S" q1 J - End Module
' h* t! ?7 b9 ], ~2 x( P - </P>
复制代码
; E1 [ z9 J' p; n/ m, O% {! C- I |
|