|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
9 g4 R) b/ R' i o; ?5 X6 o/ S
NX二次开发源码: 装配中选点,并报告父对象
- h w$ l* v. o8 z. v4 e
3 n% a8 R8 O& o, [" [% |* e- <P>Option Strict Off: Y% t0 k. N9 y9 Z3 U6 M' @# g. F
- Imports System/ {5 z, z D6 B% X$ K* h0 r- o; l
- Imports NXOpen2 R3 ?0 ]" j$ R- |
- Imports NXOpen.UF
0 b/ W- n! Z8 O, m! p) B. P -
6 q, ]8 l6 b# H/ k6 V x V* C - Module Module1
n/ e* q4 \/ I5 } -
0 W( ?& v, s* c8 z* W/ c, e3 a - Dim theSession As Session = Session.GetSession()
9 E, v3 J! J& d* Q# F - Dim theUI As UI = UI.GetUI()
" C+ ~! x) G2 n' o% K! Z - Dim theUfSession As UFSession = UFSession.GetUFSession()
1 s/ U2 |& L d; e! h" { - Dim lw As ListingWindow = theSession.ListingWindow
* l3 B4 c7 c, T: n: c8 a - Dim pickedPoint As Point
6 p, b6 c3 y4 Z, A, j - Dim myModelingTolerance As Double
4 m+ J' t0 k) a1 z' |) i - J& b8 `' m) h( X4 N
- Sub Main()* J8 [) f1 _: s
- / E0 e$ y: B* B* ?! i7 A& y
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)- w6 x" l; f9 \( m
-
' l/ W* S8 b7 o+ [ - Dim workPart As Part = theSession.Parts.Work
0 x9 M! V# ~4 \7 o. x& G - lw.Open()# s1 E/ u7 y0 x! ?5 M
-
3 a5 Q* w% J/ s# W4 l - Dim myPointTag As Tag
) v% j- H, h* B -
' E \3 H) t9 L- C( t - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then) e9 B0 n! L% r2 C8 _
- Exit Sub) p" G, G& W6 g
- End If
4 K$ W2 H; K+ j: M) @ -
0 [+ w( _5 C% X7 t. M! {2 I: k - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
% {+ y1 ], D! b O - / c) E: K' @" i/ S
- AskParents(myPointTag)7 N$ w/ B) C' ]) `+ S r4 l
-
) C3 I% s3 O6 c' `; ~; R - End Sub
, \/ n) p# a- c& ~: y) g5 u -
% Y+ E" _' T3 \6 O6 n7 y7 E - Sub AskParents(ByVal objTag As Tag)
1 }& C* S& O1 g! t# g -
1 E. L# c: G& [& n& ] - Dim n_parents As Integer
1 T/ F$ d4 [6 m3 u2 [! d3 D$ F% Q - Dim parentTags As Tag()+ T' h# w' w% J+ v8 S
- Dim myPoint As Point
w1 g: ]: P8 ~- C+ X/ e+ Y8 j% V7 P - Dim myEdge As Edge, Q+ F) [" x7 h5 Q1 A `
- Dim myXform As Xform
5 i8 k, K( U" H ]! Y - Dim myTaggedObject As TaggedObject. E, W3 I% d7 k+ j: w# K5 N
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)
1 |- {8 U$ C$ A9 X B. I# g - 0 ^% }: R' c6 e1 C: s
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then+ P6 v' U8 D8 [$ d1 g; G
- myPoint = myTaggedObject) Y5 y* Q4 Q' \/ p* }
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
1 h. i% Q4 c/ ?; L+ f' f; m - lw.WriteLine("")9 [" L: y4 m% C6 A
-
# r$ S( w3 ^( ~6 ]. u - Try, l5 N" A3 L# f- t" }
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
8 q9 \. f7 M( {1 S! O - lw.WriteLine("num parents: " & n_parents.ToString)
6 A$ h1 L) _8 s: K - For Each parentTag As Tag In parentTags5 r! |1 `- K \# k
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
( z( q, G4 }4 ?# e/ K3 S -
$ r+ y' F' ?* A, X- P! P - AskParents(parent_object.Tag)
4 {" p' g7 c& w" n -
0 H: m4 v( L5 q - If parent_object.ToString.ToLower.Contains("edge") Then# m2 y2 H7 ~* c4 i! k( E" ~$ v
- myEdge = parent_object
( l$ q; M6 D U - lw.WriteLine("")
; c( ?- X6 ^8 E2 o - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
( P$ a2 r) x. ^2 M x% [: X - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
& [; w9 ^# W# B# x - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)7 K& k8 g+ \9 N% \7 L2 p
- 6 S* V+ @! ?: S
- Dim partTag As Tag = myEdge.OwningPart.Tag/ Q# H; Y( r5 W' c2 D3 P
- Dim occTags() As Tag, r) K! h3 a% E9 k3 s7 B" @
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)' j! I- J) J3 }0 Y8 K; y
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)
( S0 [$ L Z) g( k( T8 \, u' F - For Each temp As Tag In occTags& u( F1 `, z! {6 E" G4 X6 ]! L5 |( m+ r
- Dim myComp As Assemblies.Component
4 m- m% H+ e$ K# ^* ?* C5 I - myComp = Utilities.NXObjectManager.Get(temp); P: T+ O( k* m5 B1 Y6 l3 C$ f0 Z$ Z
- Dim myCompPos As Point3d
) D" s- X. w; [8 t - Dim myCompOrientation As Matrix3x3
3 a# i7 r( |7 t k% R - myComp.GetPosition(myCompPos, myCompOrientation)
% E$ }9 N8 W7 b: J - 4 B) z5 F( o8 A' Z n* Z
- lw.WriteLine("")
8 ^6 s' ` Q" P* e" g4 C1 } - lw.WriteLine("component name: " & myComp.Name)
( r4 }8 @5 Q8 \: ^. @ - lw.WriteLine("component display name: " & myComp.DisplayName)
, @; L& w7 R$ t - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
3 n5 ?4 L. \0 T- `3 ` - 1 k) }0 ?7 c7 U x) g T) q; J, q8 C3 g
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
8 {' [3 T; C( f8 o! o - lw.WriteLine("** This is the component that was picked **")" b/ ]' _- i* d$ C8 u$ `
- End If A- V- u; t8 G3 L* C( w# b8 h
- 8 k) d3 _: y4 I" V9 ^8 D, W
- lw.WriteLine("")
3 j; q# F. E6 c: L7 S3 A7 P - Next
0 v. h. \! t( a& G - : u0 b7 U& X6 ~0 t' a: }# B
- lw.WriteLine("")1 ^. l9 y8 Z0 u0 p& e7 c+ _
- End If
# i# P3 }; {* u6 X/ u4 v' q6 b - Next
1 \2 I* _: q: X# t. M z. T -
* x1 t) a+ a1 q; ~ Y% O - CaTCh ex As Exception+ e$ O2 s; i9 v, M) Z
- ' NXOpen.NXException: Current object is not smart
6 y, I3 \: W4 _- f; J; E! y - lw.WriteLine(" Error: " + ex.Message)
+ X9 P p9 N4 q1 E - lw.WriteLine(" " & myTaggedObject.GetType.ToString)
$ O2 N! B3 p; U7 A9 q' F - End Try, g/ ]( Z( W+ z8 {, D' n9 L" K
- 9 v" K8 m0 z7 S9 X7 [( l
- 1 m# H' G9 U9 V% \- z
- End If
. e. J) u" _9 N3 P0 l -
& r. {- @/ h5 C% ^# C9 W -
+ B6 v( n* F2 B, h* S) ?5 R9 ? - End Sub
& q" j3 a( a+ P) J3 u' q& F( X - $ |4 W; t# R: c, t: V f
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer7 P& o2 t5 T% _: r" p
- " G$ X6 ^* ]0 Y% |
- Dim base_pt As Double() = New Double(2) {}+ A% b' M9 u7 C0 G
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
& |$ V. C8 ^6 B0 |% y# V7 b - Dim response As Integer = 0
& n) h( @$ X) Y% D/ ?3 F; Y - Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
* B! @. G& t2 P1 v5 e0 p - p1 V# y: a3 e6 B5 ~" W4 F
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
3 ?% _0 `* M, F' A - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)( B! w9 J% G. m
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
" T+ R5 b) X: M% Y$ _ -
: _: G! E- y0 D# B6 ? - Dim pointLocation As Point3d" `( O4 R! q6 Z$ |2 U+ h0 p0 B& |# T
- pointLocation.X = base_pt(0) {8 B' X9 W% t- ^" P
- pointLocation.Y = base_pt(1)2 |. W9 m) t8 w' b7 q1 n+ h0 O: e
- pointLocation.Z = base_pt(2)& i6 q& R8 E: ~
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
9 F. Q! c/ S1 b; F - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)6 v8 {% `/ q5 c6 l; L }
-
- B' ~4 ?2 x$ P( ]. y - Return response1 d8 Q! a# A$ x9 p+ o' V
- 0 F/ t1 h6 W+ n
- End Function9 c; u4 S( E, _
- ! ^# A, O2 @: f9 t5 p8 F$ Z2 M
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
, s2 Z/ l# G8 j1 @1 g q1 Z -
1 g6 S$ n+ s! _3 L6 ^: | - Dim selObj As TaggedObject
$ Y; [" K8 P9 g. c$ X2 J4 ^ - Dim theUI As UI = UI.GetUI
/ P* V0 x" i6 K - Dim title As String = "Select a Point"
' ^" ~- V0 j; C1 F0 h% ^ - Dim includeFeatures As Boolean = False
4 T# Z# y# I0 n2 Y# _0 q3 F$ n - Dim keepHighlighted As Boolean = False$ C0 ^6 c, @& F9 h- K7 @
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
( q7 C0 ?; b8 F5 q: h - Dim cursor As Point3d
8 \8 Q1 h! L5 W3 H' P - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
7 A! N. D5 Y: x: t* E5 M! E1 K - Dim selectionMask_array(0) As Selection.MaskTriple' I7 X( W$ F j4 [1 v
-
* l L( X0 W9 I( k7 @ - With selectionMask_array(0)$ Z! Z; {- @! O3 j; X4 ~# _
- .Type = UFConstants.UF_point_type
& y; _7 ]3 b. O+ N7 l- `) v - .Subtype = UFConstants.UF_all_subtype6 S8 V; P% v7 K' n3 y4 j7 g
- End With0 R; L1 p: N" W! K4 ^5 O% J7 t
-
& ^$ m( B$ n2 k! C - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
. x) G7 A d8 w7 j9 U9 d9 z - title, scope, selAction, _
5 o7 `. W& K2 n( [% z, I - includeFeatures, keepHighlighted, selectionMask_array, _4 l$ l4 q; F0 _) I
- selobj, cursor)
5 L- P. A( b3 d# l - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
% G4 O& Q0 t3 e! j - selPoint = selObj
. v9 b( R: I& f( ]/ c - Return Selection.Response.Ok
/ R: J9 j. `) r- g. | - Else: K! j! q; N! R1 n, w4 K: C0 V5 y
- Return Selection.Response.Cancel
7 S- U7 \( L" V2 E9 { - End If
) U% i0 y2 r( a1 @) v& w7 @7 ^5 M - 6 }6 @8 x) m2 W- A" i
- End Function" c( s3 }& f) ?# T z' N" t
- $ i4 g) o6 O& i( ]: J
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double' v2 P) g- W, Z5 [
-
) N6 ~! w+ c/ S: m7 k - Dim result As Double4 k, @8 X2 r; i( X
- 4 x( ~) W2 P. U; | k
- Try
- _- G8 R9 P. z7 m7 Q - Dim nullNXObject As NXObject = Nothing3 l# k8 M v! F7 G. f
- ) M" D2 f$ c% j4 `/ i6 ]
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder
6 {4 w! L/ Z) W, | ^) v- {" i - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)7 D* G% h1 Y( m. M8 Y, O5 D
- : |* h- N0 a7 |) i% d: m) a
- measureDistanceBuilder1.InfoWindow = False# \5 F% ~* _& p+ I+ U
-
& F9 P$ |* u* [/ ?5 \ - measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
! f& ? g$ t; e1 V' X- b# g - . ^0 ^" _: e( Q$ S
- Dim nullUnit As Unit = Nothing5 k6 V& S0 Y8 O9 O+ i+ {
-
! m2 K. }& J8 \7 e6 N - Dim measureDistance1 As MeasureDistance
* |7 K& l- O- e0 U X - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
& |4 g7 i% c. x% F' x -
d- W; ?) a& M) d& u - result = measureDistance1.Value
7 W# ^7 v b- {0 z1 @ -
5 B# l3 I% Z9 P. r6 N; O1 ] - 'measureDistance1.Information()
3 j* k( ^# \: [) W% b -
. i$ t- t, {3 I+ q+ L - measureDistance1.Dispose()8 {5 R' y! F2 t5 s
-
- B) F% c. j* `* i0 K8 S - Catch ex As NXException
7 F- |+ F5 W; i0 u - MsgBox(ex.Message)) i6 C! s0 w* Z7 E$ d
- Return Nothing
3 H( F8 n1 D5 }+ R) C: K8 p - % @ _8 y* M% V
- End Try1 z/ X0 N7 L y _" {" K: E A: A
-
4 \3 H# A( _8 a$ O! _ - 3 u. P) _ V- q5 Y+ N
- Return result: O5 l4 K+ [& _
- G1 X3 W3 [1 s/ Q9 ?! V0 P& H2 Y9 a
- End Function& B# _1 c: s* Q) E1 k) O0 @2 l
-
5 |; l4 l7 G8 j7 q5 \4 b6 n& H& o$ x -
, n+ z, C- A: F; \5 J - Public Function GetUnloadOption(ByVal dummy As String) As Integer& \; A- l8 V9 L+ r' q- C
-
9 ? K o; A, a' J6 U - 'Unloads the image when the NX session terminates
3 S. @" c- s# `4 e: o+ K- z - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination& J6 K% \7 a" m' q6 |
- 5 v% i% H9 B# o5 D
- End Function
8 I, c/ \- |' X2 i" d - 8 H! z. _2 f9 Y( z: m4 b4 ?
- End Module
9 K4 Q, b& w2 E& q - </P>
复制代码 $ R" ?, v$ ~9 \. W; l" ]% k, }( W
|
|