|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
: `3 \6 H) V4 ~! D) b
NX二次开发源码: 装配中选点,并报告父对象7 [9 |1 n# }8 c2 H1 f& e" X
- ) j7 N% N3 X7 D' P$ S6 T. s
- <P>Option Strict Off4 Z' N2 L0 m- k; h6 j* _
- Imports System7 R& E0 A9 w7 l O- P# s
- Imports NXOpen6 W: f6 y7 B# P* Y' E! A, m; g
- Imports NXOpen.UF; H1 E* e) F# g- ]( ~
-
% r$ |1 ^6 W+ \3 ^/ V - Module Module1
: ?" w7 [- M# p$ m8 C -
$ y' r1 k& [2 x. w: L4 f2 {8 ^ - Dim theSession As Session = Session.GetSession()
, W2 {4 R' @# n4 Y2 f l+ [/ ?7 Y - Dim theUI As UI = UI.GetUI()$ D7 P( Y& \: L, I8 _1 f" g
- Dim theUfSession As UFSession = UFSession.GetUFSession()4 g, y1 B- I5 k0 D7 F3 {" n1 a# z: F% N
- Dim lw As ListingWindow = theSession.ListingWindow
6 m& m! a/ l) |* s - Dim pickedPoint As Point% a0 N# ?4 R5 V% D
- Dim myModelingTolerance As Double
4 Y! O* R; }* w/ w' Q/ r - ' a+ Y2 J1 V" |. q% M
- Sub Main()( |2 C3 u, }; k. O; B1 [1 X3 t
-
( U& E- v5 J% x: ~ - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)! Q# }/ k8 N, \: ^: i
-
; p7 g1 H3 ?& b) W% b1 v - Dim workPart As Part = theSession.Parts.Work0 B5 i) H9 e0 i* ~% h
- lw.Open()5 a" [' e3 D# b9 ]; ~
- ~' i9 V: C- q6 ^8 N# @0 u
- Dim myPointTag As Tag
5 u! Y# ~# j! m -
( E: B" @! h, |/ m) P7 Q E1 E - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then. a" j8 B) g; p$ ^
- Exit Sub
8 ~! D7 Z( z0 c5 _, ~1 ] - End If
% D( F Y; Z$ O' C2 @3 b* @3 r - ( b* ]2 Q% a9 [* C. @; r
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
/ I& E! F% X4 {7 L6 V" X. | - ( ?$ J- {9 p8 i' o1 j
- AskParents(myPointTag)
6 X" I9 ~, ^; @! g* P/ d/ k) n -
& P! U6 Q/ X( p6 S$ g, W7 B - End Sub e: w$ S- f) `
-
+ Y: [; X/ b7 C0 b2 H/ F5 s - Sub AskParents(ByVal objTag As Tag)
6 Y" G5 b" {8 V7 T+ V - / q& w' H# s u( V( f
- Dim n_parents As Integer3 ] Q! [' a/ Z4 s) w* v
- Dim parentTags As Tag()
- O$ T8 H" f( c/ C1 s' b - Dim myPoint As Point6 N; P: n: L3 g, `
- Dim myEdge As Edge, G5 h' D) J% m0 D1 h* V4 q& R
- Dim myXform As Xform+ D3 R) a3 j0 R" f8 I
- Dim myTaggedObject As TaggedObject
7 G6 [9 q- h# | - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
% @$ A* w1 t( n' T; w9 `" M1 t - 5 Q8 d+ i$ l! [7 t( ~1 _/ |
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then C4 U( Q8 G; }+ R' P" s8 C) U+ A( y
- myPoint = myTaggedObject5 e% s9 O2 y" ^7 m+ U/ U
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
: G6 [3 O; p5 _ - lw.WriteLine("")
$ m0 ^# C {4 n8 N1 ~7 B8 H7 z - $ l) j$ Y. S/ b' G, @9 k4 N, i% f8 A
- Try* V4 L* _) F7 u
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
2 c# {1 y- D5 B+ g - lw.WriteLine("num parents: " & n_parents.ToString)5 i' ~: u/ D. @7 \
- For Each parentTag As Tag In parentTags4 J0 [' d5 } f" W
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
8 z) ]' B3 k- a$ f/ d! z1 Z -
* d% l9 m! f) y5 c" T: W - AskParents(parent_object.Tag)
9 ]# }5 q- ?/ F/ B$ {) J -
" g; {# K# d- c - If parent_object.ToString.ToLower.Contains("edge") Then
) ]! J! I+ X3 U2 Y. V$ F - myEdge = parent_object* u- @- B1 V/ a( j* I2 R" U8 f
- lw.WriteLine("")
, @" U9 {' r$ H* ^! U/ Z& A - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
0 `# D, }; F1 K' x - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
9 k, `+ u% ]1 S' Y- }5 c( V% V - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
) S* }% X* H5 a: X! C -
9 n3 L" J c) z! ^ - Dim partTag As Tag = myEdge.OwningPart.Tag
( V. f9 a) K% h# @4 p - Dim occTags() As Tag
, J; Y! @! A& U - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
0 z7 P& Y5 X! l4 h) p2 | - lw.WriteLine("number of occurences: " & occTags.Length.ToString), b& k/ Z% K/ T3 a2 _ ?
- For Each temp As Tag In occTags
! u5 Q5 n6 `& L* [ }2 e0 { - Dim myComp As Assemblies.Component9 p0 D! [% B; T9 |& e
- myComp = Utilities.NXObjectManager.Get(temp)& _4 ^! t1 ?+ p m% o
- Dim myCompPos As Point3d; B% A( [" y3 D
- Dim myCompOrientation As Matrix3x3, W4 K7 R+ |. [6 ?( P5 c6 a
- myComp.GetPosition(myCompPos, myCompOrientation)2 C/ h( v0 T2 h- p5 Z* d9 q8 O" d7 C
- . T6 `9 B O6 r6 _8 a: Y: _! H7 d& f" M
- lw.WriteLine("")
7 B* L& S1 T4 N9 r) k1 J) ^ - lw.WriteLine("component name: " & myComp.Name)
1 _4 y, H& s7 V: J - lw.WriteLine("component display name: " & myComp.DisplayName)
* N* c- ` R4 Y4 l1 L - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
; P3 Z: H0 |4 x% B -
0 I5 P* R6 Z& x* F - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
3 I q6 M: r: |1 M! f4 y! ] - lw.WriteLine("** This is the component that was picked **")/ C: l t& c! {5 d- p
- End If
6 Q( W7 V' ?% L& G% j- f -
6 G9 W" r5 q& G/ u - lw.WriteLine("")$ r9 N( ^5 L2 I. s" J( U
- Next
c5 q% W. v0 ^4 b - 7 n2 Q" [2 j$ G+ s( _
- lw.WriteLine("")
( n& E- P9 a! D - End If0 g& }, Q4 k A
- Next- K# O* F) B8 p! s7 K+ s
-
M0 F' R' ~' |; g - CaTCh ex As Exception4 Y9 l9 ? Q( ?3 b" ]9 B, A ]# u: ?; S
- ' NXOpen.NXException: Current object is not smart
( T8 `( [- P, e - lw.WriteLine(" Error: " + ex.Message)
* ~, E" i/ P1 U: C - lw.WriteLine(" " & myTaggedObject.GetType.ToString)
/ p9 e$ ?4 Q( J! I - End Try1 U" i' ?5 D( O! _
- , K# @7 A% z1 e6 U, M" x
-
?8 n5 g$ L% X& x - End If
4 P. Y" d8 ?% c0 f' s3 R - 2 \2 l1 o. k" |- J
- - a T d6 i7 d5 B9 L# ?% p5 [
- End Sub
" v$ @) o; f3 p0 i1 g# Y - ( E4 g, c2 ?. w
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer' u2 T8 s5 O8 Q/ B
-
/ Z6 d2 Q7 ?1 J/ v1 j3 X- m0 f p - Dim base_pt As Double() = New Double(2) {}' c( j2 V0 r2 v4 Y
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
$ M a* m8 ^" j. r* W3 `$ W" {- l) N - Dim response As Integer = 0+ }6 i) M& m2 i3 m% H
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
: B: A6 _4 l, ~ -
4 |& Q2 R/ K% t3 R0 ~ - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
# z6 E6 B% g8 M/ J - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)) p7 [- c2 F- A3 ^
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)/ O6 ?! ^; C# k
- ( A3 ], I8 I, O: g2 M% e
- Dim pointLocation As Point3d
; Y0 _( `1 K' Q7 S ]" T" g2 r# I - pointLocation.X = base_pt(0)* F7 t1 A5 N# h( x/ B `
- pointLocation.Y = base_pt(1)8 `0 r5 O, E: `& J- j
- pointLocation.Z = base_pt(2)' W0 }3 a! i, h v/ G
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
+ w6 f& y5 @2 ~/ @3 J, u - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible), x$ q( i) P" s" L# a5 T
-
6 r9 o2 z" P2 a( d$ ^& U, Q - Return response
3 X) \7 i9 J/ M" |, E) \& ~/ C -
9 ^* D; O; \% k- C- \: ?( l6 D - End Function( P0 m% R9 C; y5 j* T% c0 c
- % O5 `! d& C$ l6 U) Z6 T% J
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response2 ~; O+ W8 l/ ]- q4 Q" j% T5 d
- ' l$ [4 N" U. j9 C
- Dim selObj As TaggedObject* @9 O: y( K$ G) l O D
- Dim theUI As UI = UI.GetUI- H$ j+ ^( k$ I- c1 i0 ^
- Dim title As String = "Select a Point"9 A8 ]; Z2 s; {. R
- Dim includeFeatures As Boolean = False
7 D6 ^" _; H* r* I5 a - Dim keepHighlighted As Boolean = False4 Z3 k/ z: v' a/ a
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
; V1 O0 J3 T9 L) ] - Dim cursor As Point3d
& Z; n- U& p# R3 F" x/ i6 X - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
; d( f. Y# H" } p3 l - Dim selectionMask_array(0) As Selection.MaskTriple
+ L- u5 j% k% U0 R/ |# L - * t" n' @& s2 k+ n3 b" d L4 w
- With selectionMask_array(0)
+ n6 z7 G) L1 }) `, f - .Type = UFConstants.UF_point_type
0 b: P @' H! O4 }7 H) K. u4 o - .Subtype = UFConstants.UF_all_subtype
$ A I& E. @6 E! M! m - End With( @5 _% d+ Z( _
- * a3 N- }# E% e# U
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _, _, a# O8 p0 _; u n2 |
- title, scope, selAction, _7 q" d' m- S! {9 J9 J
- includeFeatures, keepHighlighted, selectionMask_array, _! R4 w- t& D4 H7 E
- selobj, cursor)( E/ S; \) N7 y7 T* C
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then1 O/ \' N0 D, f5 v Q, _ f) C. ]
- selPoint = selObj5 I B+ Q* K0 j; e
- Return Selection.Response.Ok- G: g4 j" U+ D! l6 }- [& V
- Else! H& V4 X7 S1 @1 A9 b; R
- Return Selection.Response.Cancel
0 `+ y- _" k8 D, W7 K7 i - End If
3 ]) |8 @$ e, G) c# O - - w. E5 {3 ~" e0 f- m. i
- End Function
: z3 e( m: b! m" l& K# i1 P - ! L5 }. U5 W4 |+ w7 C
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double( w+ [ W0 B' }! K
- ; P' [, Y( @6 ^+ G2 J
- Dim result As Double+ \% A) G M3 ?- w7 A
-
3 C. s2 `4 T8 e# t8 h9 Z - Try5 I, e: n" q, }; m
- Dim nullNXObject As NXObject = Nothing: }8 K4 q' h3 j+ k
- + Y: l: r/ p6 o4 _0 P3 e3 U
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder; E7 w! L" G3 M
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
& k! }* |6 r4 N- I) S7 V3 S) K - 0 I {. z! r' ]# e5 i2 M$ s
- measureDistanceBuilder1.InfoWindow = False
0 V7 k) y/ g' @) B. k7 I -
2 s. u' s. _2 J( A" w! N$ @ - measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum) k0 Z1 h! N4 G* V4 M/ \$ a5 Y
- 6 F0 A5 \( M$ P. @: i9 K
- Dim nullUnit As Unit = Nothing8 D' Y7 a% s) m. q2 W5 [
- : e: r" u: L4 J% C1 W, P$ x9 x t
- Dim measureDistance1 As MeasureDistance
2 a# V- G; V$ k6 @- J& N: I - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
3 p, _' G k- b -
. s5 T+ B+ x$ f - result = measureDistance1.Value7 ~' r" `- O. Q2 q8 S" H* Y
- 0 p. o/ L5 ~! n$ ^2 {+ f7 A
- 'measureDistance1.Information()
; v, `) |& c3 W# ] - 5 @2 G( l5 D: L$ |* n( H
- measureDistance1.Dispose(), l) B( [+ _7 D) Z4 e. f
-
2 \" F) s3 g- S1 I$ l# D - Catch ex As NXException
/ P, v6 I+ v+ F6 t: e - MsgBox(ex.Message)4 l! _' @/ j; W
- Return Nothing5 U" E# T$ u8 H( Y8 v
- , t! S9 r) |0 D1 h! D p* G
- End Try
( Q" H" r4 i* }3 V, U; ]4 ? - , W5 ?0 |" X: H1 \* Q
-
) z, g9 h# }9 s$ _) b - Return result
0 N6 s3 R6 a$ ^0 f* m2 C$ | - 3 n g' ~& H5 @3 P9 M' r$ Z/ P
- End Function
& ^8 e5 a6 [" ]1 s% c7 T- h - ( J/ B" y: H: [, A8 k! O8 T5 Z
- % d4 n* I! c/ B% E6 o# X4 N: y
- Public Function GetUnloadOption(ByVal dummy As String) As Integer
1 l, b- E5 e/ `; y% d5 F -
* P# h0 \! `/ e: z6 U% z - 'Unloads the image when the NX session terminates
7 v; D* d/ Z+ _# \( U9 M - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination% }' W2 B! J% k. R. u& { E
- : d" @* }4 d( o- Q( Q( C( M( x
- End Function
' E; v8 O8 c+ \3 P -
0 |* V/ C) \7 q# \ - End Module
7 l! i" w4 y" q - </P>
复制代码 9 z+ I- V! a) Q8 U! Z& S
|
|