|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
7 @2 G- M& }8 k% Q8 ^- P. w* KNX二次开发源码: 装配中选点,并报告父对象
" [$ N% g" {& x8 r# d4 `
# o8 Q! H9 e" l) M5 m8 Z5 @- <P>Option Strict Off
4 h$ h" L6 z& M8 O8 @2 n; u# i - Imports System& x! c$ i4 z" C' P* f# b
- Imports NXOpen
: t' K. h5 q2 L' K& I4 O% m# F - Imports NXOpen.UF/ [: o- `' @ {
-
0 N, \. g' `( [. t! [$ U - Module Module1
. j5 p2 S! x' r# { - # o- w% r0 f: \0 p
- Dim theSession As Session = Session.GetSession()/ }$ ]" Q* o: }3 n9 q
- Dim theUI As UI = UI.GetUI(); C# x4 {3 @9 J* l- r/ [2 X
- Dim theUfSession As UFSession = UFSession.GetUFSession()% L0 a) ]8 F8 t S7 y4 D
- Dim lw As ListingWindow = theSession.ListingWindow
6 p2 x- V" e5 E; U0 [ ~! N3 G - Dim pickedPoint As Point
5 ]' M0 x" J- h. ? - Dim myModelingTolerance As Double
6 |- Q5 m" H7 N8 d: {7 @ -
M9 A+ I; r) Y6 R f - Sub Main()$ x/ N% R L- Y; x" i$ S1 R
- ( O5 |7 ~2 L+ q5 Z# T0 a
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)9 A( o b4 l' H1 R* _ G
- : D9 q0 }; P0 L! s) b3 h! K" B0 @
- Dim workPart As Part = theSession.Parts.Work, M i. k" N" {, U0 |' I ?
- lw.Open() i* x0 _. ?( f. A. P
- 8 A8 `( l9 N% i! q4 O$ D5 u* o
- Dim myPointTag As Tag
, L3 L# L* Y. M; u8 F -
2 B6 X2 H8 k4 Z* k" H; L1 |5 z! w - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then3 ?7 }8 F/ A9 H) r0 u* {' `; C
- Exit Sub! f5 y% H* I6 e. b' o
- End If$ @/ J+ C( L7 z
- y) J# M. z$ I& A5 }
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
$ Q* a% B! }0 S: L$ ~* q -
1 x. j; M2 _8 V# V - AskParents(myPointTag), ^8 [7 Q# t9 J* a2 q1 G) T
-
& x0 `* p- v K) h8 ~, g$ g - End Sub0 e H+ h. c, _) |( Q r
-
8 s7 ~; ]' K" h4 ?- I! e, e - Sub AskParents(ByVal objTag As Tag)
2 u. D: f5 F& w5 ~9 } -
% S; f7 \& _) h: C* W* r { - Dim n_parents As Integer5 C& l2 Y, E4 _
- Dim parentTags As Tag()& ?9 h: n2 U5 Q2 Z# L# H* V
- Dim myPoint As Point
. \) T0 s8 v: R# b1 K - Dim myEdge As Edge$ b( b/ c4 i* }, Z! a
- Dim myXform As Xform
; x- T5 b- q9 H$ Y7 \- w - Dim myTaggedObject As TaggedObject# C) C* l5 q4 S! ?/ j6 l) M1 U y
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)
C0 w3 R5 c# B6 A2 V - ) X+ P, x# ~+ ]
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then( H6 v) g& E& {7 C: \
- myPoint = myTaggedObject
5 f |7 D2 f6 \8 y, ~ - lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)% C/ W: e. D- S, r* C
- lw.WriteLine("")
% ^& @ A9 P$ J ^# b4 r: b1 z% H - , r2 F" Q* z. g: U5 x' x
- Try
# v9 G; b- \3 V. [6 o; ? - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)/ q0 L `' Z; W/ }
- lw.WriteLine("num parents: " & n_parents.ToString) F/ e( T; W8 \! \% D$ R
- For Each parentTag As Tag In parentTags
. W8 a- h9 u. ^# L) g: f; X - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)# k; `0 l7 |* F z q9 w4 O
- , \% }/ K" C, I4 X; X
- AskParents(parent_object.Tag)6 ^0 R+ Y3 h4 b3 n4 |& F! g
- $ q2 E( M. i+ \' ~$ R! U! _+ F0 Q2 _! z
- If parent_object.ToString.ToLower.Contains("edge") Then
2 [, i U! `/ p3 |0 n) ~" d - myEdge = parent_object8 g0 X7 i5 R( {+ T8 B: U# G
- lw.WriteLine("")
" v) x9 X( e5 G: y/ l/ v: p4 o - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)3 Y. Z: [3 x m; [
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)) |6 C6 K4 x- \5 X
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)" t4 M6 D8 U! ~* E1 g/ S: G7 L; U) `
- 8 v. @1 r+ y& l s- F
- Dim partTag As Tag = myEdge.OwningPart.Tag% u; R, `/ ^: a$ @5 ^: }/ M
- Dim occTags() As Tag
3 a: Q2 h; b/ \8 J- y( I$ G - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)( Y% w0 s! G7 J- y9 I; ]. b5 ^+ H
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)
9 d) {# m- G/ `! f - For Each temp As Tag In occTags. M1 J% N- q# h/ G9 E) q/ K f! H
- Dim myComp As Assemblies.Component, P, {% D) L% T/ G9 }
- myComp = Utilities.NXObjectManager.Get(temp)
8 |9 _* J9 V. S2 s: z - Dim myCompPos As Point3d
+ V: m9 l7 f& C) C6 G+ Z; V8 o6 D - Dim myCompOrientation As Matrix3x3
& q" O0 L# l! T, y3 x - myComp.GetPosition(myCompPos, myCompOrientation)5 K% G) z3 Q; d$ f V& N
-
% m8 d1 q K; O1 p; \3 h - lw.WriteLine("")/ x4 t5 i5 }( Y+ v; }% y1 U8 n
- lw.WriteLine("component name: " & myComp.Name)$ x2 `5 b& Q4 {7 s6 {0 b2 z
- lw.WriteLine("component display name: " & myComp.DisplayName)
, o. K5 y9 G3 q3 d6 T* Y0 B% o - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)* Z- x" M$ ?! m) ?" u( ~: f
-
! v I& H6 ^3 U* m. Y - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
! K: G3 g! _: p/ o - lw.WriteLine("** This is the component that was picked **")
7 T9 b& b' \) X/ i/ _, B - End If
0 ]: U9 E3 _) H) l2 r( l5 L- S -
8 @6 c; T. ?0 @) q7 |, X4 B& y0 ? - lw.WriteLine("")! T+ H4 M' M/ J& x+ L
- Next
4 A/ ?2 l0 j' V; z! K0 S -
/ D2 s. p7 x' P8 y - lw.WriteLine("")
0 u# {& X) Q- H% o. x: d0 h - End If" b# ^& F7 p& m E1 J0 o( Q: B
- Next/ ]7 K( a, o9 r! |. d" `
-
: e1 p8 s! z, v7 o - CaTCh ex As Exception
3 S) S. O# m# y2 V - ' NXOpen.NXException: Current object is not smart
0 B/ W% \2 T6 b1 J- P - lw.WriteLine(" Error: " + ex.Message)
# p, L0 F8 N: p& T2 t9 d - lw.WriteLine(" " & myTaggedObject.GetType.ToString)
+ ]/ p2 x5 W/ l0 \) _ - End Try& h+ z3 l1 S6 f* V- N% K4 I( \
- ! Q/ Q7 i" q5 V- d* |+ S
- ' Z1 E$ n4 a- H( b' i
- End If7 c {) b- M6 ?+ H5 j
- ( C- ?! X: N* p7 N5 g- G4 |
-
, U7 d- a0 `/ S - End Sub
3 ?' `$ i- w9 i0 l* ]( j" Z; | -
9 {. @5 s3 P0 _2 U) u* l - Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer9 E$ [, d7 b0 h% Q( U9 K
-
9 V9 c$ T, k B$ u% r - Dim base_pt As Double() = New Double(2) {}4 y ?6 ], ^ `( i: C; Z5 Z, c
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null: E" t7 z! Z3 x
- Dim response As Integer = 07 h/ h6 X8 R8 ^
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt3 y% U; s1 B( R( ^9 V6 g0 X) f
- * J2 y( Z/ o* {2 @5 p4 j
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM), E; h6 j2 `5 v; ~6 K
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
% g. S/ f: Q7 W' r" q - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
7 c. @' Z- O* c: S6 v$ ? -
- m5 e8 A$ V6 \; W2 S, }, o - Dim pointLocation As Point3d/ x$ a2 V! X8 s0 n1 E1 R
- pointLocation.X = base_pt(0)
. y" g$ T, U9 @# u' x - pointLocation.Y = base_pt(1)& D* R: T. ^, V4 C! L7 \5 {
- pointLocation.Z = base_pt(2)
4 \4 w' w+ u& d) t - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
. L1 E' _. Y7 V& |6 p6 h - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)$ t. t8 }% T- E+ {6 v' H* j
-
Y3 s# ]+ M, o7 y3 g C - Return response
+ t7 n/ k" e/ j' D( R - ' S5 o0 ^* h. o$ Z( m1 G v+ e
- End Function7 R/ A2 `: A& E _; N
-
- z7 x' f( F/ @& D* F - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
. Z: c: j- R8 }5 f' s, l -
* b4 Q0 O, f# n6 O r - Dim selObj As TaggedObject
! _, J0 k4 \- Q, X0 y6 e - Dim theUI As UI = UI.GetUI" b; R. _5 z5 \1 K
- Dim title As String = "Select a Point"
5 x" h. o# F5 r# u - Dim includeFeatures As Boolean = False5 [7 h8 r7 s4 s/ K( s+ J0 H: D% y
- Dim keepHighlighted As Boolean = False
, g3 z0 u$ b9 Z, J% l - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
% v$ x% l v' u; K' [6 V! G. M - Dim cursor As Point3d' ~. @8 S: C5 u
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly' E' h: n2 [4 D, ~/ ^4 t
- Dim selectionMask_array(0) As Selection.MaskTriple d3 \1 p/ S2 @1 \" @
-
2 t3 k$ B/ X0 U2 ^7 V4 ]0 N3 `1 h - With selectionMask_array(0)$ W2 w, \4 _3 j, b1 X) x% O
- .Type = UFConstants.UF_point_type
( k8 _ E3 J* g% z! B- P - .Subtype = UFConstants.UF_all_subtype7 y, Y/ u$ u4 p( e: F
- End With
* a* v& W) q& M7 ^$ T -
* m, q1 L H! c. m" N - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
* Y: f6 H& ~1 c) n - title, scope, selAction, _
0 z1 u1 c0 n3 p0 F3 { - includeFeatures, keepHighlighted, selectionMask_array, _
$ I0 ~) w; T) J. o% J7 g) E7 P - selobj, cursor)9 ~8 ~' H- e+ \" ~" @5 A# P
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
; m6 H- h9 q, c - selPoint = selObj
0 R6 a! o; k& t! G: k2 C/ O+ T$ E' \ - Return Selection.Response.Ok
. b0 t' z8 W* O! v' J7 T - Else
/ ^0 Y/ `/ G! D' [ - Return Selection.Response.Cancel" O% |( W$ q: m9 a) G0 C T
- End If
7 Q$ j: Y$ V" {4 U6 d - 6 |% N+ i+ m2 f0 U2 v7 f. V
- End Function
P9 M O( S7 q' R -
, M* V1 x) M9 K1 L% i& C5 t+ D, T" g - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
1 G2 a* y6 z) x* W7 Y( \ -
0 B" k/ @' w ]: }, |- K - Dim result As Double
3 G! F; t. P; {9 L% n -
% w" I$ H" D" w- \$ N - Try2 R% r) k( @, u) {# {6 V1 E5 z2 ~1 a
- Dim nullNXObject As NXObject = Nothing% p/ Q1 a' j) g7 M7 K" X; h1 I
-
. g6 T, p' Q T3 O0 p: ?: F; N - Dim measureDistanceBuilder1 As MeasureDistanceBuilder
; r% c& c8 [/ |) o, a0 E - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)# j' h; y, F; Q
- ( l4 o5 _2 b1 V$ c8 S
- measureDistanceBuilder1.InfoWindow = False- g* ~1 T% Y/ s0 F5 F4 k
- 2 m" U% ]2 S }
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum2 L& m6 f7 @1 D Z
- $ w$ H* _, f4 n/ a- U
- Dim nullUnit As Unit = Nothing
0 {; S9 q% ?. v' t -
2 `5 `6 ]9 l# e0 E* L- v - Dim measureDistance1 As MeasureDistance
/ S8 `) l6 d5 o, G: e9 b, L - measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2); R. S' Q ]8 S; o. X. l
- 5 T( Z$ U( {. p* o! [
- result = measureDistance1.Value
- t, z. \6 y3 V9 @" E -
2 x( g+ g8 Y8 } z. ~ W' ` - 'measureDistance1.Information()
! U" J& }. F r6 P -
& z, @4 @" [1 e/ B" }. w! a - measureDistance1.Dispose()$ j( e) \; {& n {) t1 A
- % D! H! j# Q) h$ k* z8 C% E
- Catch ex As NXException% Q& z# P' e0 [( Y7 _. H, K7 a
- MsgBox(ex.Message)
5 U( T; _( \! F - Return Nothing" B6 Q0 Z0 N X7 }
-
: w+ x; o/ Z) V0 Y' e' s3 X - End Try& J8 E9 v, T) v3 ], q" s% n. |
-
" `0 s# L N4 m -
* E, R; m9 |% u% q' T1 w - Return result1 O8 u* Q9 O( t
- ' k# x/ B6 u& i% M
- End Function
E3 [1 A; A% ?, \7 P3 L -
1 P! X- b' F+ F% R) r -
; f* t6 I3 _+ R& S' y( o - Public Function GetUnloadOption(ByVal dummy As String) As Integer! ^( n! i' j7 ~! r j
- H$ @6 f7 O: U4 U. ~$ P
- 'Unloads the image when the NX session terminates
- ~% i, V% B" b- w y$ F4 K - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
/ x- \! u1 V9 T- |* c0 p -
; D% e8 c: |: g9 g3 m - End Function
+ g4 Y/ r0 L& m5 ? V, O) ? - 0 I% Y3 x" B6 J( b2 ~6 N% n/ P, x j- v
- End Module( D+ A3 R" v1 f
- </P>
复制代码
5 x# @, D0 I' Y7 N4 y" O9 Z |
|