|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
5 k, c, h5 O$ o
NX二次开发源码: 装配中选点,并报告父对象
$ I9 ^4 U7 C( v2 B' o- 1 g6 X2 J7 p& \* J9 J
- <P>Option Strict Off
0 L. C# ?- q; H4 A - Imports System1 S9 C3 K9 B( K# ?$ R9 q( O
- Imports NXOpen
- S$ n0 x& S1 u# _4 L2 o* [' E/ x+ [! ] - Imports NXOpen.UF; ]% A# d9 l& b
- 0 }3 V$ J) d0 _6 m/ r6 z3 x Y# Q
- Module Module1+ {6 L& ^" [/ S6 L2 q2 D2 v
- 6 P$ J) I/ {: @/ \5 } d9 R j
- Dim theSession As Session = Session.GetSession()7 R W( @4 k; c4 ]* w4 y( f
- Dim theUI As UI = UI.GetUI() C' I. k' H3 r, K# C5 [
- Dim theUfSession As UFSession = UFSession.GetUFSession()
) S2 q B* k! R' R. z9 K8 N' v) [0 o( u* l - Dim lw As ListingWindow = theSession.ListingWindow; z6 o' @7 c. L/ g T1 e4 j$ X+ g
- Dim pickedPoint As Point
, z9 ^% y3 w6 [& h9 ` - Dim myModelingTolerance As Double
' | a' m0 Y% f7 ?) Q - & [+ [+ E8 k/ U2 u) M
- Sub Main()
6 V) E8 G( `& f -
; Z1 X0 r9 m$ q- j: K ?: z3 a( S - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
; V$ ?/ k8 S7 \- D2 f -
) j# z- S1 [4 x) T3 C* q - Dim workPart As Part = theSession.Parts.Work1 P$ P* p3 g- i* Z
- lw.Open()
% s* Z* @( E, K$ E, `9 E -
* d8 }2 M. T. s+ g3 e' ] - Dim myPointTag As Tag5 M" Y! n" {! Z
-
' x" z) Z3 T$ h, y: G - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
! S( ~9 ]$ p+ d, d, ~( F - Exit Sub9 W6 b5 p, i+ c$ B
- End If: T* h, E: s7 \
-
, }+ j; n- R; E: Y$ p2 ^ - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)- W0 D, H* x, o7 D
- * l6 \8 a+ a" X3 E( p' {
- AskParents(myPointTag)
9 E. h" h: q5 b% [5 r# E) A -
# E* I# V$ X8 b( ? - End Sub& k. y2 D6 _& z: `& T. J
- 5 R: p) e2 y7 x4 Y ]
- Sub AskParents(ByVal objTag As Tag)
0 H8 U7 \1 Y0 X - + w9 @: b$ v9 e* ]% V3 d
- Dim n_parents As Integer' r% i! c4 D: R- w" l
- Dim parentTags As Tag()
3 G W7 Y4 w) `, j - Dim myPoint As Point8 j+ k3 S, [) k
- Dim myEdge As Edge
& l9 h& R, Z8 o. w, b9 u$ ` - Dim myXform As Xform. c- {7 ^' i7 K5 i6 d+ a5 J
- Dim myTaggedObject As TaggedObject
0 @3 }3 } Z% j: K - myTaggedObject = Utilities.NXObjectManager.Get(objTag)5 e; _, N7 u. F) K* @4 N9 K
-
: }2 K& X# a( ?6 w# n - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
2 y+ d+ n2 H- Q- T7 H% y" z - myPoint = myTaggedObject: _* x( F2 n0 x9 J# V
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
1 M S" z, j3 u1 R0 ?! m: ? - lw.WriteLine(""): v1 W' T) U ?
- 1 M1 H# S" z- [0 H
- Try, K0 n2 D: c. e3 {
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
4 f- W- R. _, w( q% _5 L u; S5 n' F% A - lw.WriteLine("num parents: " & n_parents.ToString)
- [4 R% M0 c5 ?( O5 {& f3 } - For Each parentTag As Tag In parentTags& q5 W8 [: ?# J; ^) F2 }
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)4 Y Q. ?3 {3 ]/ {, e
-
6 H$ I% R! y9 }- b* [! y - AskParents(parent_object.Tag)+ r$ u, v) [7 ?7 H u i
- : V" W x, m+ X2 v3 R
- If parent_object.ToString.ToLower.Contains("edge") Then! X/ e9 u, b2 b* d
- myEdge = parent_object
9 n7 D; O2 {% z4 _ - lw.WriteLine("")* [* V1 V2 y X9 `. i& j0 w8 {
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)7 V1 S O7 }5 B1 g" T* B, M
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
( z/ s3 w8 G2 m) g5 \4 y - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)3 s* r* Q' n5 g5 L; T% I
-
! N' q' e4 k, X" R9 b - Dim partTag As Tag = myEdge.OwningPart.Tag0 K/ g4 T- v2 b2 _+ a
- Dim occTags() As Tag) a4 L+ r( _ V7 M( V
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)$ l+ W" G3 P. W) U; q# n, b
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)
+ }2 t/ x' J4 o, }; @4 T6 r! ~ - For Each temp As Tag In occTags
# C+ w/ D: Z3 `9 ~) {' E - Dim myComp As Assemblies.Component
! w) m0 a8 @* D" c1 j - myComp = Utilities.NXObjectManager.Get(temp)
3 c2 k* ~$ V3 L$ q$ y - Dim myCompPos As Point3d0 ~# q9 @4 V# T- S# E
- Dim myCompOrientation As Matrix3x3. |) B, ?6 `/ L0 \7 D' S# Q9 b4 c
- myComp.GetPosition(myCompPos, myCompOrientation)
% }( F5 ]' H# F3 S' h- f -
8 \% `; G- U) W - lw.WriteLine("")/ H& [: b. y1 M0 L
- lw.WriteLine("component name: " & myComp.Name)4 j/ g6 X5 Q1 \! I. Q D9 P- l8 E; d b
- lw.WriteLine("component display name: " & myComp.DisplayName)/ p+ o4 Q6 y0 V! f
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
# t7 J2 ]4 D: P! { -
; Z& w" w# B* ]1 W1 d. H/ q( M - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
/ B2 W' y; j7 a" F - lw.WriteLine("** This is the component that was picked **")* `7 ~7 ]; o" k9 _# O* R
- End If6 ~3 }4 m) N8 j* Z
- & s# W; a! A( N& F- s
- lw.WriteLine("")
Y9 Z" @/ P* s' T: ? - Next
' D- S: |% ^3 g3 Y* w4 d -
d9 a, n3 H2 X# s - lw.WriteLine("")
2 s9 O+ _/ ^' }4 u - End If
8 ^% g4 A- i# j6 u: J6 K9 C1 N - Next
9 G5 T' l! y7 K9 L8 t -
- c* h" j& V0 `9 g - CaTCh ex As Exception
8 m6 \, g: T) J2 k% g' v+ y - ' NXOpen.NXException: Current object is not smart
5 w( r8 ~& W3 X/ I - lw.WriteLine(" Error: " + ex.Message)) `$ A7 N- w$ e+ e- ]* ` a8 ]; N
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)
3 c& ?2 Q- \" Q) ~7 c9 V+ } - End Try2 e/ @6 c& v, f0 P l7 M2 `3 C7 f# L
- R$ w# J) e" q; A
- # \4 Q% x7 V$ q3 [/ P1 p
- End If. B9 ^5 J6 A3 P' G4 y
-
4 M o( G, t# T' [ - ; h) ~1 o( J' a, t+ G
- End Sub
3 S( I% \, x& N$ F, q, q! a1 A/ J - " b) j$ r* i/ t8 m0 P! h
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer( {% u& ]4 b! @2 ~' ]7 t0 l
- ~* V f3 \6 U) ^, r4 c( P/ }& R
- Dim base_pt As Double() = New Double(2) {} N$ z- [- ^9 Y p& F
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
( L) m& d. ?7 b' P - Dim response As Integer = 07 W8 N. F& g/ }4 D# R( j
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt. g* N# @8 j/ y7 j& H' v, i! B3 e
-
4 i# V8 K& U! e0 }# B - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
. k- B, m0 k' a: I4 s - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
4 N5 q9 e& }, }' M' G: X - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)+ W) n7 K" s3 y9 V2 M, b8 i* J
- 1 H8 W; ^2 A6 H& g8 q! t
- Dim pointLocation As Point3d5 G2 E7 {3 f! s) V3 a, N4 T; ^! K1 o" M
- pointLocation.X = base_pt(0)' G' O1 R) W9 C2 G
- pointLocation.Y = base_pt(1)$ M5 X; ?; i1 @+ f4 g
- pointLocation.Z = base_pt(2)8 o8 e3 r9 Q3 O# a$ q
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
( _+ V$ J4 w- w& J9 f - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
8 h9 {0 s- A& { -
' E8 d6 ~- D1 B9 L6 C4 C1 K( ]' T! O - Return response! K+ n+ z+ g7 U# V
- 2 r. _6 R; j4 X! P$ G, M; q! d$ I
- End Function- E4 Z9 |, m) ~- z6 e
-
8 `- {3 B' }! L3 t5 p - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response9 X. d* ^& |/ k' [
- : T S7 j9 \4 Y- L! y0 o
- Dim selObj As TaggedObject
0 ]+ W Y' {$ r* a7 X - Dim theUI As UI = UI.GetUI5 Y/ `% D Y& i: Q# |
- Dim title As String = "Select a Point"
. Y3 @. C: v6 U1 c+ Y/ q - Dim includeFeatures As Boolean = False
; l" \0 k( |' R% j% @ - Dim keepHighlighted As Boolean = False
( W0 ]& P1 Z/ T/ `. Q8 X! d! J& \' w - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific6 R* e5 D- K* _: F! J3 G) |7 i" z& B
- Dim cursor As Point3d
3 s5 D2 v% s0 r \3 Y3 _# N - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
! R* Q. C3 G$ r1 j5 v - Dim selectionMask_array(0) As Selection.MaskTriple
* R, I& h$ ^7 ^' V5 a" m4 d- A - - b% J6 ]: ^8 j' a* O
- With selectionMask_array(0). ?0 S* p! o, J6 m( L- ]* o9 v
- .Type = UFConstants.UF_point_type3 o. s H% [. [4 K8 f( S6 \
- .Subtype = UFConstants.UF_all_subtype# _& ^9 P" e! y# u5 l
- End With+ p& [- y! z& W+ x2 l* A7 _
- ]7 l% A2 N# R2 @/ n6 s
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
7 w, S4 l- V' C; s8 t - title, scope, selAction, _7 f+ @: Q+ z1 t2 m& _
- includeFeatures, keepHighlighted, selectionMask_array, _) V, I3 K* o! g1 S$ M% }* D% j
- selobj, cursor)
) A5 X F* p1 B- Y% \ - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then" K; B0 B) j" k a Q# D
- selPoint = selObj
+ Y! r/ Q& d+ { - Return Selection.Response.Ok# W& K: K* d( _2 I7 ?
- Else6 ?4 `$ w, g/ y- b
- Return Selection.Response.Cancel' A: q6 @0 G8 F; M
- End If
- m! n/ o: V0 [) U$ e9 d; n6 J -
8 m$ {7 n- g) ]5 a - End Function
6 `7 i8 i: \1 b1 K5 _6 E - ) }' Q+ V5 v" D# Q, x
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double" [7 i( Z t- X& s& y7 c# x6 r
-
0 b( B0 o6 R* g0 G* u9 M - Dim result As Double7 D( o. \5 B" I5 `) ~
-
% d3 F/ x% i* I A. M& t' o - Try) G" U" G) m% l/ A
- Dim nullNXObject As NXObject = Nothing
! D6 q1 j- X4 N! j' Z - ( B9 C% X8 z; ~2 X
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder
4 K& }/ k; A- c - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject); d8 b# v4 d L: V
-
9 a: s6 W* }9 p& D* p - measureDistanceBuilder1.InfoWindow = False/ C8 E, M/ m5 G
- ; q+ s9 }4 M8 r
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum7 Y& Z( \; ?: P$ z6 ~
-
' L, Z4 S* m1 ~. g8 V( d - Dim nullUnit As Unit = Nothing" N9 }1 y y% S# U3 }1 ?( S
-
( G7 G( D& G, _) r: W6 I - Dim measureDistance1 As MeasureDistance5 O6 \0 [% f3 J: P2 J
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)" F! i( I2 t$ `) O4 c1 m
-
% l/ p: h: A7 q( A* K S - result = measureDistance1.Value. U p6 ~5 G; @# P4 L
-
+ b) e7 R4 Q9 W6 T - 'measureDistance1.Information(), C- x* c; w0 y9 I/ ]+ m( i! R; r7 c
- 0 f5 L5 s+ E/ H+ ^% ~; ]; H
- measureDistance1.Dispose()! f: v! D) h+ _
-
. W" D$ x- m; X q - Catch ex As NXException
& @' y+ o3 D3 F$ x - MsgBox(ex.Message)
; Z9 n8 U/ `* T* `9 s - Return Nothing
}1 a. d- Q1 J# h -
. y ?$ ~2 s v - End Try* @8 d7 y* e1 W3 o
-
- E) q" X3 n! k -
+ h) U- L) d; N - Return result
0 G7 r6 r7 N+ g/ v* Q' X6 f) w - 9 F) O0 k R% U4 r {4 M
- End Function
R4 k, A0 B$ Z. h -
- L$ J, D0 d" R6 ?7 z; h# V - / X5 z8 I* q( A1 W" `0 i7 I& K
- Public Function GetUnloadOption(ByVal dummy As String) As Integer
$ c$ ]+ {& _9 B9 j. m+ Y, J -
5 Y& q. a! C; p7 E0 W. y6 f2 c - 'Unloads the image when the NX session terminates- Y# U4 ?* \( L4 k+ x8 G- {
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination1 U9 d" m& z) Q) v+ |) y" T ]4 K6 ^9 y
- : F9 v$ v$ t8 l2 S
- End Function
& D6 s1 p" m5 U2 p9 w - % V: W+ c! n/ e+ ~: ]2 }
- End Module
7 |3 w1 w: m- i! P3 R; A - </P>
复制代码
6 Z, M6 n' \5 s& V$ Y |
|