|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
; O7 H9 s8 i, c) l- X2 gNX二次开发源码: 装配中选点,并报告父对象
& t" L- G T- G9 l: P0 M( }
4 {. l( k/ @+ ~ f- <P>Option Strict Off
& I" n y; ~5 D) ?, x4 Z - Imports System
/ b+ u* R: P/ x' ~% e - Imports NXOpen& I7 q1 Z& v9 ^* }% V/ [0 D9 L
- Imports NXOpen.UF
; J; ]. v. Z- K% @, y - 4 U, n. V0 y1 K; `; w5 ^/ a' y9 P
- Module Module1# }3 P. ~+ J! w6 }! m* r& `
- / c+ R5 K) T' r9 Y: p. |& U& k
- Dim theSession As Session = Session.GetSession()+ ^/ n* r& [& w+ n
- Dim theUI As UI = UI.GetUI(), K+ J: ^: L4 k/ v
- Dim theUfSession As UFSession = UFSession.GetUFSession()
! b$ Q9 R9 C" }2 y - Dim lw As ListingWindow = theSession.ListingWindow" Q* i8 w( r4 H
- Dim pickedPoint As Point
@; f6 M, l* Q; N* X' z - Dim myModelingTolerance As Double
8 L, S8 W4 }1 V4 i$ h - ( }# k" F: h& N
- Sub Main()
8 a+ ^, W" `( [3 z% d5 |: b - 9 ?# X, r6 Q, _7 t
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)3 o% @8 s' b$ x: Y
-
- g5 h z# {8 ]5 }4 | - Dim workPart As Part = theSession.Parts.Work) k4 i+ A [4 k+ u3 J, v2 d' A( t8 L
- lw.Open()
) H& I/ U* S' D0 n0 ^ -
/ J' N! O: V8 D3 X - Dim myPointTag As Tag
$ [4 `( P. Q. P9 J: Z* t - 9 J4 @5 l8 a1 d, d3 S7 t
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then. b; s. x5 \' Z, @5 C% R* w
- Exit Sub, X, ?( d) [* T& Y
- End If
# g3 Q. [6 e( I6 a/ z - 9 r6 g: K# C0 U8 X- ^+ l7 L
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
% _2 \6 ~- S) }& A; q -
v, j7 s6 ^9 E, ] {3 l; } - AskParents(myPointTag)# i" e' r' C# O- o8 S/ _7 i( D
- + I( ^, z7 y! Y& B i/ G# I
- End Sub
( B- K% X; e4 e& a6 { -
1 p9 |3 H5 c& ~0 u - Sub AskParents(ByVal objTag As Tag)4 g% S% r' x7 w! R3 h% v
-
( R2 Y) U! |/ e) { - Dim n_parents As Integer# a" ^$ t! w+ A% P* G1 k2 A, b- U
- Dim parentTags As Tag()3 O! O' ?4 ?& `) v7 n
- Dim myPoint As Point- S; z# w; o5 [/ Q, y' Y9 X
- Dim myEdge As Edge
, o/ D4 V; V! p8 E - Dim myXform As Xform
. F7 y- I- p6 d- d- s - Dim myTaggedObject As TaggedObject; Q1 l. J4 R0 u& ?# j! B
- myTaggedObject = Utilities.NXObjectManager.Get(objTag): ?9 x P9 u% ]/ R. ]' S
- 8 _) I# b" C9 R( u5 q- U( G8 p
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
( F9 P, v/ H$ h( u - myPoint = myTaggedObject- ]- M5 v" ?* R3 @" t0 T" N' b
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
: c f7 _* Z# F5 l2 k: R - lw.WriteLine("")
7 @1 k+ [5 k$ A# b5 y9 Q -
# y/ q% D+ A' X; b3 _ - Try! p$ U& O6 u$ ^8 f# W! m
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)( Q! |, C* j* _+ n K& R
- lw.WriteLine("num parents: " & n_parents.ToString)
' G0 y: n. l/ ~% F - For Each parentTag As Tag In parentTags: E0 U- r" e; q. R/ c
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)* ?, B( U; O, ?/ p. {5 Y7 D
- 5 d# u6 m9 \2 ^# V5 M5 u3 q
- AskParents(parent_object.Tag)4 `8 f2 U, |+ {) L* R: \: y, r
- 8 d/ Z% H3 R2 ]. S6 C+ M
- If parent_object.ToString.ToLower.Contains("edge") Then1 [9 K8 e2 O2 f% L
- myEdge = parent_object
1 R5 ~. Y3 v: v& K3 h8 I- l7 |$ ? - lw.WriteLine("")
% B% c" [! E4 B: M - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString); Q1 l N4 _* m* x9 P+ T7 w
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)) K1 U1 M! p s* S( Y: y9 J
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)5 t. B: c& Y. j2 M' G# j
-
/ B( o2 Z* ]; s3 C4 t2 ^ - Dim partTag As Tag = myEdge.OwningPart.Tag }* f1 y3 ~* L
- Dim occTags() As Tag7 G D5 N% Z: L" Y7 J5 \9 P
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
; y( H' u0 k6 E8 _ - lw.WriteLine("number of occurences: " & occTags.Length.ToString)- {! ~7 N0 y# {
- For Each temp As Tag In occTags6 Z- Z R3 u8 W( F$ X( c* I9 M
- Dim myComp As Assemblies.Component
/ [9 \; [: B+ w3 g7 ` - myComp = Utilities.NXObjectManager.Get(temp)# V( I$ O* S# X) v S0 k
- Dim myCompPos As Point3d2 H% ?8 R/ t' V2 z% S) ]6 G( I
- Dim myCompOrientation As Matrix3x3& u) P; s9 Y8 h' b
- myComp.GetPosition(myCompPos, myCompOrientation)
( M: K, D$ y2 r - , H/ f9 S& k& d3 Q- t0 w$ Q5 I- ?
- lw.WriteLine(""). f m' y, |/ r0 y9 }& e0 j
- lw.WriteLine("component name: " & myComp.Name)3 C( H4 {0 w2 q
- lw.WriteLine("component display name: " & myComp.DisplayName)# Z* U0 \" c g c
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
. [# u; g" u( I5 p -
8 s. ^! W+ i, ~ - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
9 S; |3 ]! d3 H" F - lw.WriteLine("** This is the component that was picked **")
" i! [& W' g- B - End If9 _, c" o3 z1 o. U: t/ `
-
0 P. ~5 W: v4 N. \& l; a; l2 y - lw.WriteLine("")9 N A/ P; B- |6 k3 E- Q2 ]
- Next. F5 m6 |5 G- U6 f
-
! T% d8 E/ m: X& E' }8 V) h - lw.WriteLine("")
* i& ~2 u2 n) ]/ ^0 ~% \ - End If Y, X( k: j( V7 z, D. L) D
- Next; u+ }# X. m3 s$ b
- ; V- h+ Q8 D% n
- CaTCh ex As Exception
0 J! O) l4 v7 |, z - ' NXOpen.NXException: Current object is not smart
n+ X/ u+ C- h# I - lw.WriteLine(" Error: " + ex.Message)2 Q3 f# P# c2 m- `: B. I6 J
- lw.WriteLine(" " & myTaggedObject.GetType.ToString), l( E" I! G! n9 J9 G2 x* F
- End Try
0 X: h% [6 c+ U; v! w3 h" `% n -
: j5 T2 q3 f6 L5 d - 2 |- t2 e* u; O: ~" @, z/ j
- End If
. T8 K H/ X8 B' D -
: t% v( u- \, |# v V+ _. r7 c -
' r, t' K7 E4 m - End Sub e; s9 o- |9 Z8 m# W) P* w$ P
- ! z) z4 k: E# T6 X
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer5 G9 |$ v: t( a! l4 r( m
-
+ S+ o) _+ j5 i: W - Dim base_pt As Double() = New Double(2) {}1 F# u, r2 K. Z4 t9 e! T
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
! ?5 X; a. r. K' J! ^+ `! @ - Dim response As Integer = 0* Y5 v, y; O+ s
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
2 }3 l2 u/ A2 T% D' r& D0 Q3 l - 6 C$ D: I& L/ Z" B# }7 m+ [
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
4 N- M/ N6 t" F& g" G - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
9 L7 u; J0 K3 D) W - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
! |, T3 \: ^5 e: o! D -
0 K; S0 M/ w: { - Dim pointLocation As Point3d
9 j4 O+ q6 a& C( r- @& c7 ~ - pointLocation.X = base_pt(0)7 r7 g8 u* S2 `' P' K) {
- pointLocation.Y = base_pt(1)
) Y5 j( P8 u% h" M6 \; C - pointLocation.Z = base_pt(2)! ]3 }2 z# }; f6 J& X1 P& e$ s( `
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)3 n6 E2 ?% h# C& i. ^9 E. A: e! v
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
* J6 ~2 u [% G - 9 H- U2 m! s: D! Q% d4 S# @$ i
- Return response& e+ l- ]- a2 r; C
-
' Q. A$ ~/ I9 j - End Function' ?5 G: J! ~0 }$ X; Y$ q
-
; e. x8 {5 R9 q2 g6 [7 M1 W0 l3 l - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
5 }0 ^' W! B+ q& a% D - 4 i' c0 U0 H6 z8 @+ e
- Dim selObj As TaggedObject" j* I' R1 v9 l# E& v
- Dim theUI As UI = UI.GetUI ^5 x; ?; b- m
- Dim title As String = "Select a Point"
: z! ] h+ p/ ~# ?2 X7 c2 U; b9 X3 J - Dim includeFeatures As Boolean = False
% v3 B. g& O" r( T" l - Dim keepHighlighted As Boolean = False
# B! P: L$ g" Q. r5 B) G% C8 { a - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific" U* A m! @; g" Z O* `) k
- Dim cursor As Point3d6 q& T4 L; u) A
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
7 |/ @& d- Y8 k4 |/ ^ - Dim selectionMask_array(0) As Selection.MaskTriple" Z: \7 }( `1 u6 J( d6 d
-
! N+ |3 K6 G8 ~' @; v - With selectionMask_array(0)
9 \( Q. f2 e% I, P0 a- Q* j - .Type = UFConstants.UF_point_type
9 V' M3 R6 @: q4 c0 M - .Subtype = UFConstants.UF_all_subtype! G* R+ D$ J: z0 m
- End With, b8 `% z& M9 ?# h! A/ G Z% @
- 8 n- G A N# q' A" i
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _( R' [3 ?3 W% S; Q# ]
- title, scope, selAction, _' S$ P, H: u0 Z! K4 N1 I
- includeFeatures, keepHighlighted, selectionMask_array, _
, C( s! s; z* V" R - selobj, cursor)6 q& O; O7 t: |3 _: s
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then) p4 r9 l* z3 A& ?' S5 w5 c% ?
- selPoint = selObj$ w( T4 Z7 m% D. @/ f
- Return Selection.Response.Ok# V0 ~& Y4 S0 j4 P+ ^5 l3 J
- Else0 @& b3 b; @1 w. R' R5 w
- Return Selection.Response.Cancel
# `2 W7 A9 k% X$ K, X& X9 Y - End If
) p' e" T. e, i# c. p - ) {( S/ ^" s; j$ ^/ k
- End Function
, S: B, _% B; \- Y% A5 ` - 0 }, k n5 d) W
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
9 F9 b) J6 ?. ]& i& E9 ~: y3 {! h - 6 n# C H: ]9 V+ _
- Dim result As Double
0 r% q* u" i9 i' s$ K1 n( ~. z - [8 X9 {+ \: y# H, C G) ?- I
- Try
9 G1 c. C% O- p9 g% z" j - Dim nullNXObject As NXObject = Nothing& d6 v4 f, f3 H c0 p+ f; F
- 5 b& |4 w& }( g/ f8 t b: x1 m
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder- n$ x( r9 q( c) v x7 q
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)& [8 }1 ^1 o$ W2 M+ b" Q+ H
- 1 F6 r: w6 [3 M4 `3 [% X3 f
- measureDistanceBuilder1.InfoWindow = False
8 p8 o' K4 o# g* `7 Q - - O! `# M! Y& v$ C
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
- G0 S0 `% A5 r$ P9 J) }! B -
4 _* z! ]' v% v7 h; ^" o& r - Dim nullUnit As Unit = Nothing
6 b8 s, w i7 }& @% u- }' }2 O! ?+ ` - 7 r- i0 e% I7 T
- Dim measureDistance1 As MeasureDistance
( x- r8 b& X. O/ Q# e, y - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
5 T/ P5 S7 g' i' \) x0 |$ ` - & ?9 e+ }! Q+ I( P# s9 G( P" p
- result = measureDistance1.Value
: a. g# }4 l a6 l, B. Z* P' z -
( E" b8 K/ b3 I8 }& }3 W - 'measureDistance1.Information()) I9 ^/ |, s# |: Z+ \- R# c; H6 b
-
, e8 M6 i9 F7 G* R - measureDistance1.Dispose()
+ Y4 V: s% @/ e" {$ A -
u5 d1 d, r, N: w0 P9 A3 E1 i - Catch ex As NXException7 `2 S d; t& M: l* R
- MsgBox(ex.Message)2 ]* K$ u) `0 q2 v- P' o4 E
- Return Nothing
1 p' ~; p9 ~2 J( i9 H% S# I. Z" U( Q -
" Q( A" m1 p( P2 w- N' m) @ - End Try
e* v' c+ h C0 u5 g -
$ ~4 v1 E8 J' F3 i -
V: `, E3 S$ q) m - Return result) L+ s4 o3 T$ O: m! R6 v
-
! R1 |7 x0 Q) I, M) F! ^/ O - End Function2 o! {1 [! O: o: @. N7 f" c
-
% j. l3 s9 V1 u4 m8 m* i6 j -
: p7 r( d0 H3 k, ~- S! N - Public Function GetUnloadOption(ByVal dummy As String) As Integer
F+ t. I- S6 P: V+ l& g - ( y- Y" a A8 H8 C8 L
- 'Unloads the image when the NX session terminates
) {- R6 Q5 i; G. w- y! x l - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination+ h0 h) }! E W* D- ~; y. D
- $ W* K: Z6 ^8 k9 c
- End Function: d( w& d9 V% g3 e% m5 T; o
-
w1 I2 f/ o1 }% b7 e# P3 [8 a7 f - End Module
1 R% Y2 Y: f$ v% f3 h' k - </P>
复制代码 " H6 p4 g9 C1 i7 k8 K0 V
|
|