|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
4 ~$ x& ~1 z& a/ \% N1 c. M5 ~NX二次开发源码: 装配中选点,并报告父对象
- F3 H, F( W% ^3 P& r j
z e& p. X# u: I+ i- <P>Option Strict Off
8 G. j) K$ `* d9 K8 B - Imports System2 p) |( t. u( c( T6 V, E
- Imports NXOpen5 G8 Z+ I$ I7 C2 u
- Imports NXOpen.UF: l$ s! b: i( K; r1 T0 w3 v2 c
-
+ [/ F& _0 L* c' [- k7 M - Module Module1
2 U6 l6 q6 Y& }6 h4 i -
! q. U; e; T" \1 e - Dim theSession As Session = Session.GetSession()/ o" N; N0 \, `
- Dim theUI As UI = UI.GetUI()( z$ q8 Y9 ~' l9 M7 d4 i! t
- Dim theUfSession As UFSession = UFSession.GetUFSession()
3 l2 j' d n5 `) t# J- s( g* l% w - Dim lw As ListingWindow = theSession.ListingWindow
1 }5 S" Y( h5 M1 C+ E7 J8 i0 @ - Dim pickedPoint As Point* U7 ?9 g& c8 z. b. C
- Dim myModelingTolerance As Double
, \, }0 Z+ g1 t$ b& R# K: j - ; R2 z+ z" b$ u8 B7 V/ W
- Sub Main()+ @' A. B) Y' s9 E3 Q- m7 [
- 4 k/ j: I$ Q- o6 B/ s1 v4 k/ D
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)" a/ Y8 x8 @. I1 P
- $ P2 V e% z: p' i9 b
- Dim workPart As Part = theSession.Parts.Work
2 A- m6 E& o4 y( ^9 K( _6 R - lw.Open()* q/ n/ o% O) u' Y
- & f6 A3 l7 K. |# I+ y
- Dim myPointTag As Tag
6 y0 m- B. i1 } K/ |/ l$ h' v& P -
! ^- \ C7 U+ q3 V5 ~5 J - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then& g" ^* k# F2 J8 X* [
- Exit Sub
" y- f+ T7 f7 R4 I" e# d: K - End If! A8 v C5 @# {" [& ]; t+ J. e$ F' O
-
& L9 Y( T+ _2 c( Q2 T - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
* S; c5 t# E O" {: ]3 F4 K - 0 m& t& x0 o4 t% B8 r
- AskParents(myPointTag)0 c" ^7 u4 F* C, D; D( m
-
3 k1 n3 D; g, W- v5 z - End Sub
% y( u8 B0 H# H- D1 i -
; G# l* J! B( i* H) V( l, I4 { - Sub AskParents(ByVal objTag As Tag)
4 r% ~ C% U5 x v) t3 [2 c -
3 K1 x& |# D! W0 m- m: M - Dim n_parents As Integer! [/ C; ~* g2 o( H$ ?% u" U
- Dim parentTags As Tag()' E( v& F" o- p1 m$ j* N# B/ N
- Dim myPoint As Point+ e1 l5 b' \5 ?3 M+ i$ B
- Dim myEdge As Edge" g& H) z, j& a* w1 N
- Dim myXform As Xform
H+ }( }7 z4 f' h% B6 M" H - Dim myTaggedObject As TaggedObject
' o: b+ h$ T1 z% v( Y9 K5 | - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
9 }2 h5 R0 I$ n' ~5 h, g - ( U8 l) X( G4 |' l; W u
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
5 s/ V- Y7 H; Z) ~9 p - myPoint = myTaggedObject) L9 ~% U. d* d1 E( b
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
& X' K, n ?1 t - lw.WriteLine("")
, w+ o. f9 o4 W! f( _5 i3 X -
: k$ s4 F: @/ q- }" n% t/ @ - Try
) V" w3 }$ B9 \& f/ q - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
V# k8 l) I6 I% x6 x - lw.WriteLine("num parents: " & n_parents.ToString)
j0 K6 }: j( `. F% {3 m+ t) j - For Each parentTag As Tag In parentTags
0 {) Y4 h$ P# V8 Z$ i1 N - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)6 @, b# ?0 _: S; l/ m3 Z
- $ I) q' |0 @) n4 U+ s* T
- AskParents(parent_object.Tag)6 N( P2 t1 G6 V" p
- $ C5 @+ P5 y" G7 [& E
- If parent_object.ToString.ToLower.Contains("edge") Then6 E# b& f: N) j2 c( T! J
- myEdge = parent_object N% _, k& F# c+ t
- lw.WriteLine("")4 Z( u; f8 E7 ^2 l# T
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
" k( K) J# B- g9 U - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)7 |+ n% ], I4 a4 S$ F8 ^' Z1 c
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString), J# L2 C2 o; m9 J1 Z$ E* J) {; I
- ) g! f( @$ V' x$ m
- Dim partTag As Tag = myEdge.OwningPart.Tag1 ?9 U$ H, C$ \
- Dim occTags() As Tag, Z9 }; s- G* ~" }7 C! Q
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)- x9 z2 U3 t- l) s$ |
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)& P: o, d, u0 M/ {! d# u3 I6 I1 M
- For Each temp As Tag In occTags
7 b4 _9 R7 D' Y, i" k7 z( l- d - Dim myComp As Assemblies.Component' B) O5 {0 w9 i+ l* ]$ ]( k
- myComp = Utilities.NXObjectManager.Get(temp)
/ F, ] @+ r+ x - Dim myCompPos As Point3d
+ `" p7 ?" T. U$ G f - Dim myCompOrientation As Matrix3x3! a. t/ B/ A7 W' k P
- myComp.GetPosition(myCompPos, myCompOrientation)
6 R0 F& n1 L% l) b - 3 C. h2 h1 E1 `, ^4 f" ]
- lw.WriteLine("")7 i T* G$ E, Y9 }% A
- lw.WriteLine("component name: " & myComp.Name)3 j( E& H0 ` }
- lw.WriteLine("component display name: " & myComp.DisplayName)
- G! @9 B) b, |$ Y2 g* R - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)
1 i3 y6 }3 E$ Z; M9 m - # ]& H* X, c: ~/ d; w: u8 r& \, z
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
& |' t9 G4 d g5 x7 ^$ f - lw.WriteLine("** This is the component that was picked **")
! n" x# {. g* ~, j) _7 t2 x - End If
6 J; c. _+ p8 ^5 g - " e9 }$ y( c4 l9 h# x
- lw.WriteLine("")
; b: R% M- E2 j) ]6 L - Next2 _, { k) u% a, t! s! G& G
-
$ b) z8 e7 x. X V - lw.WriteLine("")- {4 B! @7 M$ Z' M$ F
- End If
% o- w" N( ?& e - Next
9 K" r$ N: N: V - : [$ K3 ~" I$ Q, B! J
- CaTCh ex As Exception$ s- R) P0 Q( u' S
- ' NXOpen.NXException: Current object is not smart& _1 p |$ z _7 p B
- lw.WriteLine(" Error: " + ex.Message)
' j1 D* T8 R' X6 V - lw.WriteLine(" " & myTaggedObject.GetType.ToString)- Q* ^# j2 d, y3 F8 j$ o
- End Try; m- k/ r1 I* R% R
-
8 F) f" i. |: Q" Y -
" N+ A$ I7 N" `$ p - End If
2 l$ Y, \9 h: e% }. b6 u -
# R( { n" a- W! P5 ^ -
' u5 b8 L$ @9 X: i - End Sub& l% [: X+ c; h/ A: T
-
m t/ k8 w- _& j, c- @ - Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
% B1 [* _* a3 p - & `) E( x" T, A* N) C3 Z! D! t
- Dim base_pt As Double() = New Double(2) {}
/ y. q% Z" x8 p" ]( } - 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
! v. I8 e5 W1 j* T; P$ d - Dim response As Integer = 03 g! h; K e7 j' }9 s
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt; ?2 j8 A; _ J) t
-
2 y7 X; K. E+ ]2 ]+ l( b. c$ w - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)2 d% q% ]" y# v! l6 E
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
# p6 s) g& A- }* ~$ T - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
* w* L, j8 @6 f7 [. }0 {' M -
% V$ I; x5 }' O, B% E - Dim pointLocation As Point3d
. p: v$ K; B2 r! V% C - pointLocation.X = base_pt(0)
, v. Q7 J. c/ D- J v) b& S; b - pointLocation.Y = base_pt(1)6 d* }; q, Z8 F: T- ~/ _
- pointLocation.Z = base_pt(2)
! j3 s" l' x6 w5 C - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
" S7 W7 d( S6 K7 [1 p - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)$ R' p; B' R$ @ S5 }. q
- $ P5 z$ I" j/ m* ^1 n7 E3 T3 J K U
- Return response
$ @% n" T. l- G* X0 z0 o - 2 [& r: d) O$ e ?( R4 g
- End Function q6 i# M& w& M, C
- : f! y/ D) V0 B# p! z
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response" t- d( A6 n& H' |3 M
- , ]7 P) R& o5 R. [
- Dim selObj As TaggedObject1 A- z! W1 ^5 D8 a7 f0 c
- Dim theUI As UI = UI.GetUI8 \( ]+ [$ ~6 t' c3 ~0 ]
- Dim title As String = "Select a Point"9 N6 A C7 U; R1 ?
- Dim includeFeatures As Boolean = False
8 \8 m5 G8 M% e5 S/ Y Q7 U - Dim keepHighlighted As Boolean = False9 }3 o9 v- h. i: |# O" b
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
( ?0 B4 H1 f |3 Q; v8 Z - Dim cursor As Point3d
/ D5 E+ k) M; w% w - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
8 `; V- D: o) B, | - Dim selectionMask_array(0) As Selection.MaskTriple
# b0 k3 ~ S# t1 E1 n - 3 j/ H! ~& q. C F
- With selectionMask_array(0). x. N: l6 Z2 {) N2 n
- .Type = UFConstants.UF_point_type
: Q4 S) v3 P$ X. d) W# U - .Subtype = UFConstants.UF_all_subtype
, Y. y9 a8 |6 }2 j - End With
. E# E0 F& f, c) U5 G -
2 X" c- f* g; A& w3 r7 } - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _# K2 V8 ]: E% C- e, u
- title, scope, selAction, _4 b: M- h" L2 l6 {+ u( V
- includeFeatures, keepHighlighted, selectionMask_array, _+ D& x/ |) B' p2 L7 }# |
- selobj, cursor)1 G* l1 D* U" d5 l
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then* c' s( s6 [/ v8 F+ W
- selPoint = selObj0 `; ] Q; h; W, j8 o+ V9 c$ m3 b
- Return Selection.Response.Ok7 q6 f1 N: H% a8 M
- Else T, e8 I( ~/ l0 @/ t
- Return Selection.Response.Cancel$ E; a+ b/ B2 R7 T' f2 }4 h8 g
- End If! A* Q; [" {0 [5 [: c+ b* X( X! P
-
* m/ P3 ]' c& I6 R - End Function9 v: c9 _ t, h/ j$ h% S
-
2 g8 t- ?6 o4 h2 c - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
1 B E) z8 C0 \: f# E# D - " t% n7 h# G/ x" U
- Dim result As Double- L$ s" r% C/ q' H+ H0 i
- 1 t: B$ B- _5 s
- Try9 D' K+ d$ |; d4 c+ H
- Dim nullNXObject As NXObject = Nothing
+ m: N9 E# x. P, S -
! A0 {( U9 I9 L: U/ \ - Dim measureDistanceBuilder1 As MeasureDistanceBuilder( C) _1 Z$ f2 m' ]/ p
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
3 g) o6 ^# W9 z! l -
6 A$ W$ }6 q" i- l - measureDistanceBuilder1.InfoWindow = False
/ w! o$ L$ {( J% I7 l, O5 x - : M! o9 N% D' D# R/ j; q) z9 O
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum D; z- E6 v% r: a; B8 J
-
! g8 X! h$ a3 H3 r/ ~ d8 W0 i - Dim nullUnit As Unit = Nothing z( l! m4 G: c. Z
-
: s, X" [1 k8 P5 g - Dim measureDistance1 As MeasureDistance
# m) ^2 K! y+ L4 C& {$ @ - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
# {" d" [ m* r* F( {3 o8 U# U - q2 v) r! `3 R! W9 s# c# L
- result = measureDistance1.Value
" ~% Y; o" q) }1 }. F5 P0 q# h -
' H* s8 F$ i4 C# G0 ] q$ O& A - 'measureDistance1.Information()$ \2 Z; d3 T( q1 g. X
-
7 F1 i9 _' @" R5 O, y$ g - measureDistance1.Dispose()
% S( {2 A/ ^5 P; F7 N2 a - $ i8 T0 C+ f9 R
- Catch ex As NXException/ ~* x9 k- [0 e( P: u' t0 @
- MsgBox(ex.Message)
5 l7 z8 |; J$ J1 U" N1 r& R - Return Nothing* ~% [5 Z" `- m V/ J5 J
-
* ~) B( y* W+ \# ]( b% M( x$ F - End Try
3 a1 D, H4 L" H - 3 ~8 }- s# J+ ^
-
4 G4 [% n& @ V7 t - Return result, @. ^$ |# G& g9 {' M, S' ~5 {+ y5 `
- ; Y2 c# [8 i" V
- End Function$ M7 \. T1 [' k+ l- c/ _
-
5 V, _( ]# h9 a& W' H -
7 a/ [' Y& z8 N$ k - Public Function GetUnloadOption(ByVal dummy As String) As Integer
: b' e- i [& }9 l. @. ~ - 9 t3 X M x. l* f# ]( G' I5 e3 u2 B
- 'Unloads the image when the NX session terminates j" T+ Z/ ^9 y) r" ~
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
6 g2 w$ S8 W$ i: o# M3 }) l - ' C( R ]& u3 W j" x/ V3 M
- End Function8 y7 e: d. t/ J8 c. o3 e
-
& T! H& [. i1 x$ T - End Module% j1 ~) @/ H# P I( t7 _
- </P>
复制代码
+ E; p# D+ j- w/ ~' M6 s' Q |
|