|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
; `5 Z% s9 ?' a
NX二次开发源码: 装配中选点,并报告父对象7 @ ~1 q+ l2 c, B; ^
3 M! a* @: a1 X( \& E0 V3 A ?; g% T- <P>Option Strict Off
% D. \! x; n- y- @8 Q @1 p, n - Imports System
# B* c* Y$ k) j8 N* G# ~0 W$ C* c - Imports NXOpen
# B* ~1 }. j1 c1 K, A+ M. K - Imports NXOpen.UF& z, \$ M% J, g; D
- % J* L( u7 v4 E
- Module Module1- L0 N2 X( t; V& J2 V
- ! S9 g2 k. z- y1 n- N( R
- Dim theSession As Session = Session.GetSession()
: S z0 x& ^7 Y% x; I3 _7 M) ~: A2 y - Dim theUI As UI = UI.GetUI()" X) F9 b" k1 M) i8 ]9 P- P
- Dim theUfSession As UFSession = UFSession.GetUFSession()+ m3 S$ B7 G' s" S W
- Dim lw As ListingWindow = theSession.ListingWindow
5 f. r" r+ `) i" L/ F8 G2 y - Dim pickedPoint As Point" _9 C( S) M G, {) k9 _) u
- Dim myModelingTolerance As Double
7 P0 w* n6 o! i3 C" T5 m -
0 a9 M3 U1 U6 L* i) ?: n2 b2 q& i( } - Sub Main()
) o0 ?' p/ b' O) ~$ i: `$ X - ! [( F$ g; d3 P c" u7 x, Z& S
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)( s" n' C' n6 t6 Q, \8 X+ g" a
- 8 a' B& n0 ~" y3 z4 G. L' X
- Dim workPart As Part = theSession.Parts.Work
$ e: z4 m8 Z. \) D v - lw.Open()1 i/ U, n6 @1 n7 g- j
- 4 @* l4 I2 g6 ~ a: N: u
- Dim myPointTag As Tag+ E: }" K. p* @: i& J: Q
-
8 ?8 U! l6 O9 A% H - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
; R B! y4 O5 }0 M$ { - Exit Sub) g9 J" Q" ^6 M& [) E
- End If. @ m) \- h3 d/ A/ _1 p
-
# l j8 o% ~& e0 q - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)( {2 n$ F# ]. v. j; I
-
' R3 \ R# ]$ B( e. O% } - AskParents(myPointTag)
K& U- O& ]/ A5 Q$ G! @ -
5 q2 r, e( d8 U) k8 R8 } - End Sub
. v0 ^- j+ r/ l" X6 L% j8 A* l9 b' a - 0 Z5 G/ W- A: U+ M: t. X+ o
- Sub AskParents(ByVal objTag As Tag)( w! E: [; a/ O
- , g. K1 t5 [3 Q# i
- Dim n_parents As Integer
# O! f, k/ m+ K8 @& U& l; y5 { - Dim parentTags As Tag()
4 S* A/ ^4 X8 N4 ] - Dim myPoint As Point8 M. H7 a( m$ A! w, x! z0 U4 ^
- Dim myEdge As Edge$ t: E) K; _+ q( e: ^" V8 M8 M( q
- Dim myXform As Xform
# H3 \. B5 H t, z+ R - Dim myTaggedObject As TaggedObject7 c# D4 R5 N. ~1 G+ H
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)
+ v: s- Z8 e! D5 R -
! l4 t" Q0 r# X! a - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then9 o4 Q- M1 B/ w, `- f% D
- myPoint = myTaggedObject; t+ }7 B' ?/ ~5 u' f
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)' ^. A( J- _ U8 e( u
- lw.WriteLine("") _4 A& N8 E, ]9 L8 A5 ~
-
' P% m1 P8 R, ~ - Try
/ O& a( v. l# K7 R3 g5 Q - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
2 p, ]; w5 G6 M3 }" _( e - lw.WriteLine("num parents: " & n_parents.ToString)& o1 O0 V! [6 ^! C; c; o; `
- For Each parentTag As Tag In parentTags1 G+ Z W6 j- Y+ C0 [
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
+ E) U+ W+ [ A: h# J7 K& K. k2 i - 6 L; {9 z* q$ X9 R/ c
- AskParents(parent_object.Tag)
2 x' O5 z& s( x6 l9 ?: G -
& Q N! r. I* [) t) V1 U - If parent_object.ToString.ToLower.Contains("edge") Then
2 w- S9 k, V) P; @ - myEdge = parent_object
- j, `8 @; ~7 \ - lw.WriteLine("")
2 s9 C$ u. s0 X# c - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)+ A+ u# A8 {! U1 X8 A; a4 J
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
{! z/ v/ x2 I+ X3 o+ S" ]/ B. A - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)' T: E" T& X- L1 H
- 3 t1 L- F# Z' x; E2 P' D3 ]
- Dim partTag As Tag = myEdge.OwningPart.Tag
9 @$ w; Z. Q1 f; r) K! X - Dim occTags() As Tag* e9 c8 F$ O$ E, `1 S- J
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
/ B# s8 F: j0 f - lw.WriteLine("number of occurences: " & occTags.Length.ToString)
& a' _& e7 ]; {/ n5 u - For Each temp As Tag In occTags
* X: w2 g: y t5 o- F" \ - Dim myComp As Assemblies.Component
' N2 n9 m# ^1 j2 j T0 V. V# v - myComp = Utilities.NXObjectManager.Get(temp)* v- z" y( C4 |, A6 c8 R0 C
- Dim myCompPos As Point3d% I6 K& f/ B' L
- Dim myCompOrientation As Matrix3x3- ~! r4 \5 R' g9 c8 J
- myComp.GetPosition(myCompPos, myCompOrientation)) m& n. t$ k* l; r9 O/ Q6 S
- 3 {8 @1 S$ Q6 n8 f9 g) U$ f
- lw.WriteLine("")
; X3 [, d. t: p) o9 k+ f1 k: @ - lw.WriteLine("component name: " & myComp.Name)
4 @1 [: ^' M/ W3 z6 _' k - lw.WriteLine("component display name: " & myComp.DisplayName)
$ b: f2 ~- ?1 y0 v2 N5 ~/ E - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
5 a4 Z- l7 [) i4 J# H, x/ o -
& l2 v& ?' \) V - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then; \# _7 F5 `) {! @+ H+ t
- lw.WriteLine("** This is the component that was picked **")
_7 ?2 o& I% F2 M - End If6 @- n8 |* i! v" y7 J- P! D
- $ V. o1 M8 s: V8 y# V, O
- lw.WriteLine("")4 J3 Y* d9 n& y2 k: u" r
- Next( G; U k. o T2 t! G9 B- M1 Y
-
9 e( q! w0 C2 K) }' i: l - lw.WriteLine("")
. O6 L+ j+ r j o! k! D( P - End If
/ E" |: ?! Q0 B" v) i - Next
& |9 A% i$ g1 _ m- X5 a% \ - : t& V7 C$ v! C2 E+ |
- CaTCh ex As Exception7 E9 p7 j; Z, P9 ~
- ' NXOpen.NXException: Current object is not smart6 c7 b- H/ v1 a; o1 @3 B
- lw.WriteLine(" Error: " + ex.Message)
0 {. B1 K, ?6 N1 G2 S; \% N& Y - lw.WriteLine(" " & myTaggedObject.GetType.ToString)9 f' ~ j& w5 @. a
- End Try* U3 }1 ]% U) v4 s
-
1 \2 @1 s9 t ]: K- b' C8 r* V - 7 T. z& W+ ?' ?
- End If
/ v0 d( i( p" i/ m0 Z- z -
% C3 I( z: h/ b& i -
, ]7 `" U7 t2 t5 D( G M - End Sub" l& f$ V8 D0 y9 }, O# K
- - `. T6 v! @; [
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
7 c; F9 L! {; I - : G. Y& V* A9 {" |
- Dim base_pt As Double() = New Double(2) {}% I) O7 g. r: t% R
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null' e( R9 l+ m3 w" c- y
- Dim response As Integer = 0; m: |1 W3 M1 u2 o8 m( [8 T9 a
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt4 M" Z$ B$ J# K) n
- 7 A' E8 F0 ?4 G* w9 K
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)9 C5 @0 {. d+ Y* h0 t
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
! M+ l1 y8 V2 m: M% A& o$ b o - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)$ e6 r+ e' X, g5 } w1 B
- 4 Y3 e7 y6 I, \# _
- Dim pointLocation As Point3d
' E) x; ^7 p$ u# d8 | - pointLocation.X = base_pt(0)3 c3 f, w5 g6 g
- pointLocation.Y = base_pt(1)2 V8 k# a6 q" h, D Z& p
- pointLocation.Z = base_pt(2)
% M7 T' M' G7 s o) O9 d6 i - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)/ V# ]1 m" T5 M. a# r! X
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
4 g7 K/ V# W* J |1 V -
- j! L1 B* g* P1 e3 _9 M' Z7 G% R - Return response Z& s% x# R; C/ z* l t
-
0 U( O0 ^- Y3 Q( S- I3 W! ?5 l. p - End Function! T- S# \/ f( N1 v1 A8 z; ^+ S
- ; ^1 x+ g$ N2 x
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response! J* @* |* `1 L
-
4 l% n; {4 ^: Z3 Y' E. q/ G1 ^ - Dim selObj As TaggedObject
7 J4 S; f. W9 R0 m. v1 G: R - Dim theUI As UI = UI.GetUI
( Y3 P5 l1 s8 m; c* ~ - Dim title As String = "Select a Point"
, c& N) r2 y D# ]- K# N - Dim includeFeatures As Boolean = False
' O' e1 p- O5 `0 j# } - Dim keepHighlighted As Boolean = False
* o" c' H. M) d; v - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
M. ]. q, ]3 x% C6 V - Dim cursor As Point3d3 {5 u+ ]* K" J/ ^
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly9 K6 M o# O. X7 H
- Dim selectionMask_array(0) As Selection.MaskTriple0 g* @/ e. L& e* C+ {: w1 p
- + y9 `$ }2 ?# o3 d
- With selectionMask_array(0)
$ b2 P6 \) l6 O - .Type = UFConstants.UF_point_type2 ?9 f9 e) P* P9 U% f" r
- .Subtype = UFConstants.UF_all_subtype+ e3 \4 g/ A# ?5 M
- End With; r& F' D: f5 M, ?, z0 j* d, g
- ) q$ w4 j, s: n, d
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
" H$ Z5 X/ o2 n! u- L K - title, scope, selAction, _& e6 w% M: ~8 e0 a
- includeFeatures, keepHighlighted, selectionMask_array, _5 P* x3 Q7 |5 v% q
- selobj, cursor)
8 Q& B( q, w* Z% v1 H - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then$ A) O: |) u# B/ x3 w6 N
- selPoint = selObj2 [% O# E5 A/ l
- Return Selection.Response.Ok8 }, k( u% K3 N
- Else+ j& N, v* b9 Y5 b. `
- Return Selection.Response.Cancel' a% }# P0 m/ l
- End If# L8 U2 Z3 T$ X
- & z# t: J4 R- o
- End Function
2 F: I+ \: @ B3 ^2 W" I' s( i0 B - ) t; ~4 T+ C4 @7 A
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
8 O$ E) z0 ^! R0 p) O' { -
4 g+ T9 u/ \9 d' f( P - Dim result As Double
. ]9 k H( C, s# w2 y% b1 x, | -
- C! \0 G h1 ^1 F; a$ e! a - Try6 ^ L9 J' Q( Y7 B" `
- Dim nullNXObject As NXObject = Nothing! v- ~6 z0 \/ Z/ B5 w0 X0 _, r
-
/ T2 x' v: c5 I2 }" r - Dim measureDistanceBuilder1 As MeasureDistanceBuilder
' @0 ]) [9 [' N8 V, Y4 X; Q - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
: [' Y- z. R. p5 O -
; U4 L! s5 _1 a* d5 B - measureDistanceBuilder1.InfoWindow = False( e3 {* G# S; I; l" X
-
$ A. }# R9 y) R; M0 _7 a- c - measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
0 K6 u5 T! i/ J7 e -
) y5 x# o3 _4 G' v - Dim nullUnit As Unit = Nothing
5 r& [3 \7 d5 s7 H- t) | - / k" I, v' C+ ?
- Dim measureDistance1 As MeasureDistance. m0 p$ @% B0 W
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
( A9 c' g, F/ Q( x+ E - 2 A- S& q6 v1 z6 v& O0 w: L
- result = measureDistance1.Value; Y5 G5 _+ T6 q' R
- * X% {, S4 B0 _( O! o$ O% t. [! ]) ~1 b
- 'measureDistance1.Information()
: ~" ^+ t3 J3 F9 F# z7 | - ) h5 J% Q3 i+ s! i5 R$ b b
- measureDistance1.Dispose()
0 @2 s5 }( |& W9 L7 c -
! g5 M; K( J1 R" u# W - Catch ex As NXException
2 v$ C8 @1 D4 c* U - MsgBox(ex.Message)
+ {$ Q- w2 \: w& J8 J - Return Nothing# Q+ k. o, b1 Y$ }, E# v1 U# B0 J& t
- " r5 s8 q+ i4 H" S+ s
- End Try: a, @+ u6 q' s3 ^
-
* x/ h# v+ }6 O$ P6 _0 h - + `) V Y' V5 }+ ^
- Return result1 {( m- z: l7 Z; n) B& F
-
5 c; m9 P- a$ H/ G - End Function! E! B$ x3 ?1 D1 S- P" _- g1 q$ B
-
5 a/ F& a1 @7 d7 r9 U5 { -
0 y3 Z8 ^) R( u. a - Public Function GetUnloadOption(ByVal dummy As String) As Integer) C0 W8 m- V, _3 {: E0 G! }
- 1 X3 F) u% Y6 j Y$ R5 O2 u
- 'Unloads the image when the NX session terminates+ v' V# U0 F8 V
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination }/ }1 [1 H: i% k( y" r
- ; G; f1 K. J( W, {; X1 y
- End Function
) M% E7 Y+ X1 }$ y - " Y; i: X# X( e* ]4 s3 @3 @. D9 Y. b
- End Module
# a2 T0 \! E. F) ^ - </P>
复制代码 1 y5 a8 ]$ m" \( J5 h: E
|
|