|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
. `2 E* i4 c8 w5 Y2 \* `: j0 M
NX二次开发源码: 装配中选点,并报告父对象2 p! _9 P7 [; k5 D
" ?: q0 v" R7 P. d9 n' y z- <P>Option Strict Off$ Z2 _4 D2 d7 q, X- V7 H
- Imports System
: x" m1 C& i6 Y; } - Imports NXOpen
+ b5 [+ i; j+ ]7 z! D - Imports NXOpen.UF! N0 w1 ]0 m( E! J8 `3 K
-
2 z) k+ ^7 X+ r, H8 K - Module Module18 M0 a" R) q& U. p8 T' [/ O
-
+ L2 D( ?/ V$ m - Dim theSession As Session = Session.GetSession()
6 ^3 x$ a$ i: o: Z- d - Dim theUI As UI = UI.GetUI()
: \# k8 E3 t- m3 l - Dim theUfSession As UFSession = UFSession.GetUFSession()! h2 X" F* C! [2 j* j% M9 r4 O. u
- Dim lw As ListingWindow = theSession.ListingWindow& Q1 M" A. b0 ]9 t$ j/ d& D" X
- Dim pickedPoint As Point# j) D& n; M2 d4 ?4 P
- Dim myModelingTolerance As Double
8 S% y' ?$ v, q9 j( ^3 H) a9 j -
6 m r, a$ @0 `, U/ M3 N+ ~ - Sub Main()6 k9 i8 ]- B; E0 |0 N6 M. P
-
& U0 f1 p4 k4 g' [ - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)' k; O/ P, s& g$ d7 w1 ?( a
-
. j0 L' a0 ]3 v! V - Dim workPart As Part = theSession.Parts.Work
$ P. E5 {+ ~+ N' w7 \4 [( D: z - lw.Open()# k/ \7 |( W" P& J% O/ u% L2 S
- * p4 j$ ?( k3 @3 N+ }( H
- Dim myPointTag As Tag9 g. d$ L8 L- U1 |
- $ e# d7 v" P2 z/ F4 j
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
1 S8 e; _" g# f; B - Exit Sub
" a. A3 Q x8 D - End If
6 H0 J6 e5 P! v- Y$ C - ) q5 z3 g( n. h& U. G
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
6 b7 z. C! `! r# c - ! k6 C" b9 z$ Q# f/ F* I
- AskParents(myPointTag)# Z" t! {# t& O. u
- 1 ]; @0 [" ?; u
- End Sub
5 h& b0 J: z% k+ V6 Q8 L4 }1 Z - 9 r @3 ?4 A& \. F8 E( F7 e' t
- Sub AskParents(ByVal objTag As Tag)& \6 {. Z. p7 x" ^% J; X! G5 a6 d
- ' c0 ^" y: F6 L8 B4 O& f
- Dim n_parents As Integer
: o" h& R3 s& o A' x( W - Dim parentTags As Tag()
+ v3 _; n5 ?% o" `; A, j" Y& N: t - Dim myPoint As Point! ~7 f5 N: q6 K% V' x% D- @0 y
- Dim myEdge As Edge4 V; [# n' X/ N" Y" r
- Dim myXform As Xform+ i$ E- k4 j3 w
- Dim myTaggedObject As TaggedObject4 a" }7 p7 g$ `5 ^
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)
+ a$ k% p; f( U% D7 J2 v2 p9 @ -
5 [0 S" Y0 j# u4 |+ G: W+ f - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then" ?3 R0 p$ d# N( v% q
- myPoint = myTaggedObject& Q; t, j" S+ n- I# r6 c. K z
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
$ i( J" e/ X$ g$ I - lw.WriteLine("")) M( c X% o0 s5 E' D
- ! {0 Q* \5 E8 n( d) \
- Try
$ D- B9 A1 A+ l, h* o$ u - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)0 C7 F v+ D5 W1 q
- lw.WriteLine("num parents: " & n_parents.ToString)
! \* n0 K" u) ]; J - For Each parentTag As Tag In parentTags9 ~" K& v: ~4 b$ I4 ?
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
0 L2 w7 t K) t8 H6 X - 6 L* m0 B- a# [" _3 x2 c0 ?
- AskParents(parent_object.Tag)8 ~( J' H, r; ?5 N" [: P
- ' m! L. W2 d# v
- If parent_object.ToString.ToLower.Contains("edge") Then
1 w. X2 [5 X% k/ Q* q- ?( e - myEdge = parent_object3 ]+ _) D9 \9 d; s2 `$ M
- lw.WriteLine("")# x" Z$ }4 M+ O2 X1 e
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)) h/ H# Q! M" \/ c
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)" u' J" t7 o& ]3 W5 j
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString) d3 C: O5 d, a1 m& } @; k
-
, M% _- @" \& O1 I! w7 L$ @ - Dim partTag As Tag = myEdge.OwningPart.Tag
! x, k" e+ }5 R( k. N- ] - Dim occTags() As Tag
; X5 Y! X$ k: R$ j3 S9 ~ - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)6 u4 M# E( g3 z2 [* w% o
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)) o, H! v, O u/ j3 J
- For Each temp As Tag In occTags
4 p+ U8 L$ q, _1 q0 ]' m: W - Dim myComp As Assemblies.Component
; S0 W2 r5 c5 {# Q - myComp = Utilities.NXObjectManager.Get(temp)
2 o. p$ v% m9 e' V+ o8 J - Dim myCompPos As Point3d0 [: w/ h6 I" ?: \, z" F2 ?0 y B
- Dim myCompOrientation As Matrix3x3
4 E" `, T9 Y2 f5 X+ |, v, H2 F - myComp.GetPosition(myCompPos, myCompOrientation)) P7 @. Q# Z1 l7 y
- 0 I8 m2 }2 T) Y0 y
- lw.WriteLine("")8 m: e) I2 B+ O) e) Q: ]( q
- lw.WriteLine("component name: " & myComp.Name)6 Y/ r$ V @ ?1 e& A% J
- lw.WriteLine("component display name: " & myComp.DisplayName)
. l; @) Z. n5 d$ N- H' r - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)# {8 o# r* A5 Q
- - j/ F3 E) x3 r
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
2 |! w1 D6 w) d4 X2 [1 l6 a- [ - lw.WriteLine("** This is the component that was picked **")5 j$ J5 u X# F( K' p" j$ e$ q
- End If/ I5 O4 t9 B+ G+ G7 i
-
' i& @3 D6 c$ x* w, Y. F0 _ - lw.WriteLine("")
: V. ]' E( s+ v- ]% j - Next
4 O( G; D8 u1 ^! v5 Z* V1 X -
" P! N, a, b8 _' ^0 [6 j - lw.WriteLine("")
6 o4 G! ]1 ~( u0 ~ - End If& L) T1 ?+ i, l/ A/ b5 J( Y7 s4 ^
- Next; ?8 s' [9 S* U! G! r( ?3 O% j' ^+ x
- . m( q2 [+ L, ]1 B& i* E, }
- CaTCh ex As Exception
# }6 V" Q& s. D) g0 a - ' NXOpen.NXException: Current object is not smart
( z# L& R7 B7 C ? f ]7 @ - lw.WriteLine(" Error: " + ex.Message)' x0 U+ z3 \/ g! Y, Y
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)* X5 L% M) x" A' t" w' W% Y( H
- End Try0 N) O- F; k# s/ ]# C, V3 a( X1 F
- 8 D9 _2 M! ~9 {, T, S' a
- 7 f; _# E0 ]- \) P8 L/ V0 t2 I1 |5 G
- End If- ?! ^& N4 U7 b) t+ O, }9 m
- - ~0 Z& k: \3 o8 o" \& f5 P
-
( b1 b- G% k4 Q, C. @# A& X - End Sub$ ~3 ]/ Q( z" c" [! @* d
- 9 }5 K) H+ G% m+ @0 a6 d' Z
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer6 ?7 O6 J# m9 d9 h( R# \$ s% o
- 6 j- u1 Q" h) ]$ S* m. B% [
- Dim base_pt As Double() = New Double(2) {}) X( s7 L$ H& M" M
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null6 _) @; J+ p" u% [0 `7 l8 @
- Dim response As Integer = 0( ^* x# \ N8 \
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt& L- Q7 E+ s2 ^6 D: S
-
7 ~9 y& a' a( V; i& [ - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
9 L/ V3 {+ d+ L7 h - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
7 \" V( {* E" B% E - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)9 H4 B# G$ d, K9 Q2 F
-
, d \* h7 O7 \5 E" b2 Q - Dim pointLocation As Point3d
* V1 T$ C) |# k' [% `; b' H; p+ ` - pointLocation.X = base_pt(0)) ]% b9 i9 l' d7 h; W9 W% F8 H
- pointLocation.Y = base_pt(1)3 z4 ]# D3 ^- l6 n3 Y8 ]
- pointLocation.Z = base_pt(2)7 X5 y" v4 R+ N3 e
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)) K# H+ ]& t9 N
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
- T* F8 W$ l2 \, H" J2 U" S -
( X2 i" V9 [ H0 C/ R, a. o - Return response
. ~3 S; \% w0 i -
1 o( B2 ~# k5 P2 x1 X' ~ - End Function( [5 P3 Z! v( Z; f7 L* K
- 5 P/ }3 g- J) l
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response4 Z/ L, P8 z5 {6 }6 _# s
-
: S& q. U$ i8 F - Dim selObj As TaggedObject
* s1 ~4 ?( G0 c, \4 s* ~0 N2 R9 S - Dim theUI As UI = UI.GetUI
2 z+ v: Q, {8 Z% i - Dim title As String = "Select a Point"6 P- @! V( A9 R! ~' z9 G' P% {
- Dim includeFeatures As Boolean = False
1 o! {; I6 [* U: U3 \ - Dim keepHighlighted As Boolean = False
# m6 J+ V, _1 M5 Y( W9 i4 G - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
% {% m7 [( o" ~% ?$ S' \ - Dim cursor As Point3d$ }* s$ v% y# k. M K1 [7 N9 T) C' a4 v! o
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly6 |. r6 g8 W2 b* v/ D- x. p5 {
- Dim selectionMask_array(0) As Selection.MaskTriple. \/ P6 g6 X2 w# {4 \. i
-
) C, z( j( w7 o- z# w0 T - With selectionMask_array(0)
* U0 G$ }6 K$ n' u$ V. S - .Type = UFConstants.UF_point_type
' B U. m& H9 s2 r3 v# ] - .Subtype = UFConstants.UF_all_subtype
- P! F6 m) q9 {" v; q3 s! M - End With- v% Z$ C: s1 ]$ E& B5 F6 k3 V
- 7 R1 g8 q" O- Q& e& l8 N
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
/ { ~8 F2 M- `7 m7 b - title, scope, selAction, _: e) V; f4 L( Y; B9 R/ y/ I! h
- includeFeatures, keepHighlighted, selectionMask_array, _8 f1 x* Z( R% w! ^! L: P) X
- selobj, cursor)
2 u2 @1 r- I5 v* J - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then: r4 [, q' d- O; Y* T5 h/ r
- selPoint = selObj F1 L+ F% Y8 ~+ R7 |, |$ O. N
- Return Selection.Response.Ok( Q3 k: Q( i+ o$ d) [
- Else
% S, w, A6 ^4 H3 W( o% c - Return Selection.Response.Cancel; C W2 H0 k5 u" W* x
- End If: U, h* F$ K. n4 A t+ l% {3 V
-
9 y2 ~! q, v% e' K; g9 A, M - End Function% @ ^# v1 Q0 f" b
- 5 D. ?$ T* Z- p8 d
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double' A$ t6 w8 j, ^, y
- ' E, J. g. O# s+ w
- Dim result As Double
" l' K4 V& s$ y - 2 @" h9 u. x' ? ]1 q7 T
- Try
7 N2 |0 p' r0 A! e - Dim nullNXObject As NXObject = Nothing
/ o u' Y: ]$ f/ o1 y) g% E3 o - 6 X* R7 O; e6 n0 d4 c9 q0 Q
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder" L7 L: W7 _) Y% B! Y# E6 \, P
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
4 Z( M( M, m% C: Z1 D - ( O# h: e/ ?8 p* f$ l& i
- measureDistanceBuilder1.InfoWindow = False1 ^' X( j& D3 h; l( C
- : h: y& G! d# Y" m3 H
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
$ S# z: ]3 D0 p' |* \5 I -
* K' ]& Q, L) ` - Dim nullUnit As Unit = Nothing
: c; c; \6 x0 J+ j6 r4 D* t - 7 Z. f# |$ T! {% k% h5 F% L
- Dim measureDistance1 As MeasureDistance
6 q" y5 Z- s9 r& I6 {* q4 w% y - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
2 Z/ k5 E; o0 d1 D2 f1 @3 ^ - 5 s; f/ I8 o% ?6 O+ F( K+ Y
- result = measureDistance1.Value
, C- {& x' R) U -
" O* F# Y' K, B/ d- ^ - 'measureDistance1.Information()
% [ e7 a( M+ Q& N, H# C - , n4 h, y8 C( J' Z. i( _# Y6 O% h
- measureDistance1.Dispose()
$ I) U( s( k9 r9 }% ]1 e2 Z -
/ \1 q: _1 G r( c% I, g - Catch ex As NXException
0 }1 U6 b1 Y% i' n1 C# x - MsgBox(ex.Message)
6 R3 k) e9 @& u( a6 T+ d1 O0 ~. r - Return Nothing5 n; U: }+ h# y
- 3 A5 P4 m3 s9 n6 h' O N I
- End Try
9 e6 e2 b5 D( O+ G; r - 8 P5 r9 q- [. |
-
5 g" f6 x) R/ X' f) ~+ N+ i8 H0 m - Return result
# q7 i4 f# S* D - ; T' x0 ?* ~9 \
- End Function1 [6 } o7 U, a+ X& T& l P ~* z
-
2 m. M, A* K0 a7 M% { -
( W. | W. `* B2 \4 } - Public Function GetUnloadOption(ByVal dummy As String) As Integer% N' ^) W0 o' P
- : @) p1 l6 T3 m, L" c4 y
- 'Unloads the image when the NX session terminates0 }+ P! \8 Z9 m' a. S7 I
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination8 l/ D: G7 n8 l# S' U- x4 i) q
- 7 b3 Q0 v3 J+ y# D6 z% z1 E/ e7 l
- End Function
! ^- i3 }7 e8 @( p7 h, p -
" \2 J+ N9 h% Z! i8 _ - End Module: {" n* w3 b( l0 v, i) h4 [3 Q
- </P>
复制代码 5 n9 R, t% Y% T( S3 U% s4 O1 ?
|
|