|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
2 _% b, K! S6 T* _
NX二次开发源码: 装配中选点,并报告父对象5 A3 g E6 e1 T- x O
8 P% [. C9 B& T" u; h. z0 \7 p" r- <P>Option Strict Off5 M. k* h' D1 T, p6 a
- Imports System! N6 T, d" W, U" g
- Imports NXOpen# D! z& b8 z% B
- Imports NXOpen.UF9 w2 O7 t" E6 T2 ]8 ^, t
- 2 p: T6 z ]5 I4 o
- Module Module12 E( S/ f/ z1 c6 M2 O! q
- / [- V: O* ?) C& s
- Dim theSession As Session = Session.GetSession(), a; h; l$ L0 m# h1 \% I" r
- Dim theUI As UI = UI.GetUI()0 |$ s" G7 i# _" e
- Dim theUfSession As UFSession = UFSession.GetUFSession()
' B6 s Y& w1 ~' w0 D' V6 {' b, ^ - Dim lw As ListingWindow = theSession.ListingWindow* l; w5 B4 L6 s, C* e! U9 G
- Dim pickedPoint As Point
* D6 s2 ?' y' U' p, w8 f - Dim myModelingTolerance As Double# v2 c0 ?, H; f6 k! s
- : b% n, A! u3 n5 }& h9 v
- Sub Main()# D8 T( s8 v4 o
-
% R5 q/ @8 t* p$ y {* B - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)8 ~ x0 N9 ]0 x! o6 V
- + n$ w- p$ ]; \, u. B9 D& ?
- Dim workPart As Part = theSession.Parts.Work
1 Y/ U) V/ t2 B; x8 e - lw.Open()
1 o, Z* h4 V! V6 u- O - $ c+ u, u6 K6 V+ D2 q4 N7 f
- Dim myPointTag As Tag$ a' c8 r! T6 U+ F1 ~
- 5 {1 s" i" w1 q1 v/ v) X
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
( [" p1 ~& q& i ]* H" t9 i - Exit Sub* X1 W+ ~+ t, h; z' w3 H) ?
- End If
! V; h: c" r: l& h -
" }$ \ N- e; C. u - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
7 Z9 k6 w! Y9 P, C' K - 3 ]& o: Y! J: i
- AskParents(myPointTag)
2 V7 n- D2 o4 R% G- l -
% G) a2 Z* W* K& } - End Sub
; O, t- Q( R# ]! c. G/ Z0 n9 {" k -
2 c; S) |# l% ]+ o) K - Sub AskParents(ByVal objTag As Tag)9 T4 f; l5 H0 H: q
- : M# Q2 U+ n6 V. q8 b( k
- Dim n_parents As Integer5 G6 W2 x1 s& v0 L7 }3 S6 r
- Dim parentTags As Tag()* ]0 f; x& P5 L7 N& d: _5 P- m% w( V
- Dim myPoint As Point
4 P& P4 N+ T; l2 ^# ^ - Dim myEdge As Edge
2 P7 `* X! n) p4 y4 { - Dim myXform As Xform
6 m5 t0 z0 |) a4 K$ r4 I - Dim myTaggedObject As TaggedObject5 w+ \. h6 {2 p1 D$ p' D6 ?
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)% |9 Y- b" E I/ d0 m4 |! Z) d" p. |
- : Z Z2 H2 J7 i: c+ I9 Z
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
! S G1 S; @/ S - myPoint = myTaggedObject( |* e2 ~. F* @* s% y7 Q6 C
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
% | \# F; Q! [$ \ D J - lw.WriteLine("")
# B' u* V& y) A2 y& M - 1 h6 D9 e& N0 V) B5 [1 |
- Try
A+ k$ g- c9 h7 B9 D - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)9 m" h/ r# Y7 S b2 K4 P
- lw.WriteLine("num parents: " & n_parents.ToString)
5 k m' k* h# w0 Y9 Z+ O - For Each parentTag As Tag In parentTags
' ^& w8 a1 o e3 E4 l& o6 X - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag). I x# ?, b( N
- & t' G! q; O& @, d$ z* f: w
- AskParents(parent_object.Tag)8 E" j6 |& J3 d8 Q' ]* v! G
- $ z* @; f5 p" A$ f. K: Q, m. W1 A& l3 M
- If parent_object.ToString.ToLower.Contains("edge") Then
8 r5 w6 t2 L! V$ `$ E - myEdge = parent_object% e3 ^4 ^& k \, M& i/ x2 p! _
- lw.WriteLine("")
# n$ y, `; Y! @ - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString); \' E( r) q2 _" ?
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
+ c/ `4 U) f( v - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
" V2 s/ ~- g X* Q5 E. A - 1 v+ m2 M2 d! R c
- Dim partTag As Tag = myEdge.OwningPart.Tag
4 n3 ~" m# I2 G( a - Dim occTags() As Tag3 v$ q. Q0 f" q' F% y) q
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)9 ?/ ~& a z! T; T4 o2 ~* x
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)8 b8 r8 H l, a" ?" N1 H5 F8 x, N
- For Each temp As Tag In occTags
6 |: `: j& h5 G8 c; | - Dim myComp As Assemblies.Component
o( y3 M0 j9 W - myComp = Utilities.NXObjectManager.Get(temp)
$ d$ I& P' k6 o) R - Dim myCompPos As Point3d
9 k( ?& G$ z: t# u, }. ^/ J - Dim myCompOrientation As Matrix3x3( K, H L0 R0 S# O3 b: n
- myComp.GetPosition(myCompPos, myCompOrientation)
% a' V3 a3 ]1 ~1 I" C9 q - - k* |9 t1 z3 Y9 W3 Y& [/ l6 r" H- n
- lw.WriteLine("")
5 k; g: O5 y5 t* x - lw.WriteLine("component name: " & myComp.Name)+ _0 E$ B7 \* E% f1 A+ G
- lw.WriteLine("component display name: " & myComp.DisplayName)
( r! Z( s5 C# P4 I - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
9 H7 Y+ d! y5 d$ ~ - % d0 }, _" v, \1 I i3 t
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then/ F1 b2 q9 u8 l7 U" B5 ?7 s, f; f0 `
- lw.WriteLine("** This is the component that was picked **")
4 ?3 p; G1 p( \* F - End If
9 p- @9 v# ?* r: L/ y - % m7 V5 X* ~; W# s" |9 q, H* x
- lw.WriteLine("")
9 l' G7 U( W# A# w - Next
( U# E3 Y" i4 ^! `8 ]4 ]$ s) f - ; ]/ w: T F5 N4 i# r
- lw.WriteLine("")
: M( h" r [8 H; O - End If
7 F+ P% y$ x% Q' N, B+ G. N - Next+ s+ T0 Y% r! ?5 l- L
-
; X7 D- e ?9 C6 b8 M - CaTCh ex As Exception
% B5 k! v4 Q. b: s9 N - ' NXOpen.NXException: Current object is not smart
# N4 T9 z' { F! b7 @' w - lw.WriteLine(" Error: " + ex.Message)
3 q3 F) `! v2 m0 C6 V/ r) T - lw.WriteLine(" " & myTaggedObject.GetType.ToString) _' N! G# ~! ~ N E( z
- End Try
# E! \/ ]4 p! M' {2 Z - . e! P, | L v S2 ^4 j* c. V
-
4 h" b( g. f* K b4 e/ _, ? - End If
) J- |% e# d5 [- h' w# L; U- c( f4 Y -
# S" Y6 A+ Q/ ~# w5 }2 r -
5 T# z" ?( Y( f, C3 b( c - End Sub
. ], G4 l z4 d: S4 b) w B: } - 4 m; [9 s6 C C1 I. U
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
2 r# |% S! G8 c0 {7 E -
$ T' G! a! X ] - Dim base_pt As Double() = New Double(2) {}1 s' r! q) K \, M3 l6 K& W
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
! u4 V {. T9 y' z - Dim response As Integer = 0- w8 O# [. T7 N1 Z1 k& o. L( s
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt6 }9 C3 F0 d3 i
- 0 e) H7 Z M- i6 b: W' C! o& n/ j
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
2 l; x. I+ _ j - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
; N" v" t; H: q - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
7 |/ H; W, v! r8 @8 V/ o4 ?% M - 3 v8 a8 l/ O) k- B
- Dim pointLocation As Point3d6 C; f3 Y# h) N' z
- pointLocation.X = base_pt(0)
0 I: k& F5 G$ R8 ~ - pointLocation.Y = base_pt(1)) B+ K. b) C- N) k' s/ M/ A! R
- pointLocation.Z = base_pt(2)
9 h8 K1 i5 O, G7 U- v+ j7 c7 K - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
1 W" M$ k+ t; |0 p' l/ J7 Q - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)' Y0 P6 [+ `( ]9 S( Y9 ]8 D+ _
-
% O7 S" ?7 x0 p8 b# Y - Return response
* F, P$ z7 i3 T1 K; o, p& ^1 C - * b$ k2 C2 @7 m) i% P# \% e3 X
- End Function1 _. t( ^3 _( u: x: }: N- r* z0 g! [9 L
-
4 n9 r' Y) X5 H - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
/ B% ]) V5 a6 g6 J# N -
4 y# L0 i2 c. [6 {0 B* T3 g! c - Dim selObj As TaggedObject
( ]6 R/ R$ W+ \# h - Dim theUI As UI = UI.GetUI5 E$ x: O8 c/ L) d. T }
- Dim title As String = "Select a Point"
- l" b0 }" Q% I' j) Z, ` - Dim includeFeatures As Boolean = False
2 R1 l! k% w3 Y' w% I$ O - Dim keepHighlighted As Boolean = False
! S# c- `% Z# v/ ~! {1 { - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
' b4 e2 [. M6 O! \ - Dim cursor As Point3d/ w' E) R9 P' w
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
1 w' ~6 c8 ^8 t* m+ Q - Dim selectionMask_array(0) As Selection.MaskTriple8 e7 z/ \, k& X! V; o# C( j2 ^
-
% ~) {- y- Z' H% T8 r( F ` - With selectionMask_array(0)
: ^- `3 X. u/ p6 Y& S3 S - .Type = UFConstants.UF_point_type
; |$ D3 d9 G3 N+ f - .Subtype = UFConstants.UF_all_subtype
4 Z0 ]7 x% N& m6 O+ a7 o - End With% l1 d* m0 l4 g4 n5 Q, |
-
5 n: w$ a/ A/ X* {. B$ N0 l* M - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _9 B: w1 a" _+ b4 q0 S) \
- title, scope, selAction, _9 a; ^ i1 `# u: B- Z0 ]
- includeFeatures, keepHighlighted, selectionMask_array, _& M1 M$ ^" G1 Z6 G q; x
- selobj, cursor)8 F4 o" {2 M% k, |( \
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
: m" A p$ g) B- H8 b9 ? - selPoint = selObj
! ~9 h" q! |6 H; y# A! Y" i: ]# H, x - Return Selection.Response.Ok
2 E) [+ [! J- C! q$ x; ] - Else2 ~+ d/ q# U( S/ \* T
- Return Selection.Response.Cancel6 E5 _/ G" k' S1 N: g9 m8 n3 H
- End If
" u* }+ U: U+ S+ \6 D: R- b - ( g, a4 ^! u7 S
- End Function
8 s, R/ [# v1 G6 e* H9 E* | -
9 k' T8 M4 }- h% F6 g2 e; H - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
' V- r2 ~; |* X; I -
$ H$ I$ }- G i3 W - Dim result As Double, J: }( Y1 u) j1 s, w
- : {5 O8 ~! C3 @7 D/ S; }4 ^
- Try) h- T/ Y9 G, c2 F3 |
- Dim nullNXObject As NXObject = Nothing" t2 l4 B$ r1 Y" e
- S$ z) G/ F+ u
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder" r5 W) D, l) {8 ]- R! g( f' q
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)4 n$ I: g+ |3 F; j# l. e; m4 j
- 0 N, v" C' L4 W$ z- P
- measureDistanceBuilder1.InfoWindow = False5 {8 _+ D2 [1 N w' A
- * o* U8 s3 ^& ?, c
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
/ N; s2 N( ]/ W2 f: d - 1 q) ?$ g' p" |6 ]1 ~. |- V
- Dim nullUnit As Unit = Nothing2 J3 A0 {8 p f9 r8 `
-
* D& ~: ~' [* \5 _ - Dim measureDistance1 As MeasureDistance
7 w$ x$ o$ F& T# }6 x - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
' Y% I+ N) k* q3 X -
: {, l% [; o8 y$ b7 L - result = measureDistance1.Value
) y! O, g0 J$ X3 o6 n, c -
% V+ ~& \# \0 c E# @0 A/ S: n - 'measureDistance1.Information()
2 r1 P% R- [ r9 M - 4 s! z4 m) m H& w. V- g
- measureDistance1.Dispose()) n' K1 G! [2 ~0 R% v6 W/ Q
-
+ x6 ~5 K, o, y, x - Catch ex As NXException; { z X4 b/ M8 i1 X+ y2 ]% F6 ~6 `* \
- MsgBox(ex.Message)
4 h3 V& F+ n. F. k5 N; z" w3 H& O - Return Nothing
* _8 ?3 h& A5 a/ [3 r -
0 r' R4 N, R7 U; H2 h - End Try3 b5 j4 ~, m4 m `6 e
- 5 A3 e$ R% C$ v2 t! ^' b1 \; d
- 3 ]7 r* d! T2 v1 K: f/ z
- Return result
3 A5 N$ i+ i- q1 ^' M; m* Q -
, G# E5 P: [% \# g: ? U# S9 W6 j2 c - End Function9 V$ E% \$ W0 `0 b( P3 Q2 o
-
7 o1 ?; L( ]; L! p0 `' U2 Z -
) c- S- f9 m+ r- z; w" e - Public Function GetUnloadOption(ByVal dummy As String) As Integer2 n$ t1 V) j0 c* u
-
, @2 D" {8 w: x. A - 'Unloads the image when the NX session terminates
, D5 e6 M3 P2 c. S r - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination5 H$ k# b- ^# ]
- . p5 |. n) Q: q+ B1 R# T
- End Function: f. I$ \( p0 h `( ^; R4 }
-
) v G: W) B5 q/ f% I4 U4 u9 y - End Module
; }1 u! G/ }4 ~/ G/ p+ q - </P>
复制代码 0 A* L! S* p, _+ ], y
|
|