|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
; i0 w ~4 }. K9 ^NX二次开发源码: 装配中选点,并报告父对象
' ^1 Y! A, p0 X" n. w+ ]2 o
. D- N ]8 q0 `+ H+ {7 A+ E! P2 |- <P>Option Strict Off! q+ ?" H" I& ?* B; e2 t+ B
- Imports System
! N( Y" X0 ~! W) G: e1 ~+ T - Imports NXOpen
, S7 \1 i7 u& y. h S - Imports NXOpen.UF* _$ y. M5 Y# A- h* c* t
-
+ E$ }# W: Z. r/ L9 Q1 ]; \ - Module Module1
4 Q: i4 e/ q4 ?) j -
$ U, o5 V! ?. |; Y, V - Dim theSession As Session = Session.GetSession()( Q- q1 d4 L9 L- ?1 Y) m
- Dim theUI As UI = UI.GetUI()
6 h2 P. Y% r6 z+ H& d6 t, L) R5 y* S - Dim theUfSession As UFSession = UFSession.GetUFSession(): p2 G; ]8 ?- C* J" ]2 _. U
- Dim lw As ListingWindow = theSession.ListingWindow
; p7 w U: ?/ u: S - Dim pickedPoint As Point0 c' x$ ^7 f: f0 `( i) t N
- Dim myModelingTolerance As Double, |* k1 W0 |/ a) n' X
-
. Y2 [1 e. {. j* ~ - Sub Main()
: q# o. w9 V, }- \: Q9 p -
; b# l. X f) A- Q4 ^8 a1 f. X - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
2 y. f; l1 s: P5 b% l0 k* P1 { - * `+ q1 j7 }' e2 h0 h: E! i5 G! w
- Dim workPart As Part = theSession.Parts.Work$ }8 b. x# `# q& }! ]! ^
- lw.Open()( s; L t5 o: N+ v6 y
- 1 ?4 ^" _2 e; p. U9 l
- Dim myPointTag As Tag( B% \0 S$ z1 v! C# I
- 8 R) a6 T7 T" n0 R4 \$ A; D0 {
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
- ?8 p% I1 S( P' n% a/ @ - Exit Sub
D- ~& ^. J$ q! C ^$ ]9 X2 v8 g - End If0 X& B- x( q, ~9 F' T m
-
- e2 [% V" ^5 V - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
$ U4 T: {6 t: n! z -
. e) @) D& ` |& t5 {$ x3 f - AskParents(myPointTag)
" g7 I' w' F4 G" ~! e -
9 r6 o& j& }0 ]1 q - End Sub
5 b; ?. n+ A" `4 ~/ j- Y( X/ r3 X% A - $ u! [: p. T! U2 A$ [" o
- Sub AskParents(ByVal objTag As Tag). H4 Z g7 q* `/ u
- 5 o- s7 z% ^6 j; r$ g7 q
- Dim n_parents As Integer
* ?( a3 |8 I: g [% I - Dim parentTags As Tag()
8 b' L9 a4 P9 G8 u5 S8 b, u+ j - Dim myPoint As Point
% E' b c7 |% y/ Q3 j) C1 u) h8 V - Dim myEdge As Edge, L( o5 h% ?: p9 F' |
- Dim myXform As Xform
$ W2 a- ?6 T3 R6 B# B, n - Dim myTaggedObject As TaggedObject
) t, C/ n7 I- d V - myTaggedObject = Utilities.NXObjectManager.Get(objTag)$ [6 ]$ R6 f' d$ X8 F# \
- , Y+ g$ T# P( u+ p7 J w
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then; T4 h: c5 I+ X/ `5 M4 e4 \
- myPoint = myTaggedObject% `$ U% T+ P. C- w; ~, I. w
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)( ?7 a2 C/ V- D: Z% t. ~" i4 ` w' M
- lw.WriteLine("")0 A3 w& r4 @1 _" S( c5 p
-
* f5 Z! a6 _# }, Y* j h4 v$ G - Try
# G7 u) Z2 M! W j/ m8 S - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
5 e# i* e* j& }4 R: ^0 v - lw.WriteLine("num parents: " & n_parents.ToString); N m/ x- P4 h
- For Each parentTag As Tag In parentTags
i7 z" s4 L3 L& s% E - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
' g) m+ H) M0 u. v -
( u5 |0 C9 R ~1 ? - AskParents(parent_object.Tag)
3 A1 A, c! L2 j/ i - - \4 A) c& V% A& [" d- e8 f
- If parent_object.ToString.ToLower.Contains("edge") Then8 `$ o0 h' Q2 d" h) N. o
- myEdge = parent_object
% y1 t4 ^1 {% u - lw.WriteLine("")
9 `9 j B* D2 ^% K: ?; P. a - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString). `& A* I t# N4 Z) R, b8 q
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)5 i7 y$ [& |& B5 a3 D. O A( y
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
$ d% D) I' K" n) L -
$ z* ?$ ?" @$ a - Dim partTag As Tag = myEdge.OwningPart.Tag6 j& c; q! F6 D( B$ s- z
- Dim occTags() As Tag+ _' r) z$ L+ q0 [* W' h
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)6 M4 M, Q8 b: D; v8 n; R; Y
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)
$ H3 Z% z' z' a9 Z- I - For Each temp As Tag In occTags8 S( w Z6 C7 q' L$ R# e
- Dim myComp As Assemblies.Component
. [) {* ?5 C7 I1 Q - myComp = Utilities.NXObjectManager.Get(temp)9 O# D9 E/ S0 E _1 X( p" _
- Dim myCompPos As Point3d) F- X; l5 V2 `
- Dim myCompOrientation As Matrix3x3
+ W$ Y7 D/ t1 @- s; d T! _ - myComp.GetPosition(myCompPos, myCompOrientation)" I" l, u/ o6 s+ @/ Z* M
-
! E- ^: ~) m2 i7 d& V9 ` X - lw.WriteLine("")
) Y [1 \# V. n& N' ~ d - lw.WriteLine("component name: " & myComp.Name)4 J( t6 w9 H7 W) i" O
- lw.WriteLine("component display name: " & myComp.DisplayName)
4 X- s3 g* C- U: q6 j4 f - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString) f" S0 x) I) ^
- ) E8 a' A8 x: h# I# I8 U( [' Q2 f
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then, Z; ~* M: @% ~6 z) j1 ]( V( g4 o
- lw.WriteLine("** This is the component that was picked **")0 |& F+ |* S3 Y- G
- End If1 d* w9 r' o7 m9 v) X% q% X
- ' s. N0 h: |. M$ @# m2 \
- lw.WriteLine("")) O% p$ w' a6 K7 A5 G" {+ Y
- Next
& \8 Y4 E+ M; K -
# n: R7 [# j2 H2 @. ?' G - lw.WriteLine("")1 {, \4 k$ E4 s, ~" T! G+ P; S
- End If
- ?" ~1 P6 l! K$ u6 r0 Q - Next
1 e F8 g0 M# z2 j - % u- R/ [% Q4 o" ~, M; w. i. N- y
- CaTCh ex As Exception
1 x% `" W- H0 J# y+ z0 @* S& q - ' NXOpen.NXException: Current object is not smart |9 [- x/ o1 w6 O% U
- lw.WriteLine(" Error: " + ex.Message)
5 Q( {( t0 L- x9 Y; u( j% O - lw.WriteLine(" " & myTaggedObject.GetType.ToString)
9 l% m }& s: B" \) m v8 I - End Try
) j4 Z8 N+ U- J. P - U) }" ^* X k6 h0 d
- / B. F' D$ H0 E _+ }7 z
- End If- C; w% H/ l' y: D9 I4 A
- ' B5 c! R% j) ^- A: P3 \" f
-
, X% m$ C; X. }5 i$ @/ N( R - End Sub, ?* D7 X* p0 M5 E% P; W6 a
- ( D$ n9 M5 p2 r
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
/ Q! z8 L1 F% ] - . E' b% ?% R* U" P: l' g# j/ Z
- Dim base_pt As Double() = New Double(2) {}& h# ~ Q5 E/ I! p& f0 Z# R# S
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null4 F% b4 @4 b2 m" L2 i) u
- Dim response As Integer = 0' V! Q; M8 E! ~+ W* c
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
- K) B* _1 Q. P5 e+ q -
8 g4 S- s' {, s3 ~/ `0 d3 s/ p0 @1 j - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)3 d# b) T+ S6 Y. q3 l# Y7 D
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
; h1 O* z5 p$ }( K/ E, D/ V, \; e, p - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
9 d; p; ^1 M* l -
* Z2 x3 }' e+ b/ ?# \5 p; W) { - Dim pointLocation As Point3d/ o; l# B6 p7 Z: Q3 Z5 H
- pointLocation.X = base_pt(0)
. f( T7 q# r# X& }- u/ a - pointLocation.Y = base_pt(1)
) U: d- G( U5 Y% x - pointLocation.Z = base_pt(2) K: p" i) O1 f0 y; d5 T8 ^" P
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)2 P- j; M1 p+ U0 Z, C- i
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
& s3 W' j F" _; X; \ -
' S' Z2 m6 h7 w7 E - Return response
: R: T7 |6 ~, {. x+ V4 I -
9 i4 G$ t- H: e) ], ~: I2 Z; Z0 _3 o - End Function
& ^9 k0 V: E- i; T; u - 8 V, p) h# |! r& Y
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response+ n$ c: ~: F: c" I8 x! g
- $ L/ w+ e" x0 i' l2 ]
- Dim selObj As TaggedObject
( l- ?! @8 d6 M; q' W4 i% g - Dim theUI As UI = UI.GetUI
; J2 [8 ~4 y2 x+ o7 `5 \ - Dim title As String = "Select a Point"
. }1 [# V) b* X# L) o - Dim includeFeatures As Boolean = False
2 A2 G2 L. x# [' \0 {1 X8 |3 {0 q - Dim keepHighlighted As Boolean = False
6 P; t/ H* H" O" P( o$ j, k - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
, G/ H6 J& }% @" F8 L! w2 T9 K7 W - Dim cursor As Point3d* p( P) Z3 z% u' _ f
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly) q% X! N" G9 `& f
- Dim selectionMask_array(0) As Selection.MaskTriple
: d9 t3 f! o) c6 p5 b5 g -
* T2 v; M) {# Y - With selectionMask_array(0)
6 p5 m) R$ L, x3 b, U - .Type = UFConstants.UF_point_type
% x% J* a/ V9 |7 p# O0 ] - .Subtype = UFConstants.UF_all_subtype
3 l6 }9 f6 A6 F( j - End With# b6 A) w r5 ]5 @+ H' O' a+ K1 {' P
- : C( a _4 Q6 d3 q8 ? X
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _% _( T& ]5 j4 c5 l) k" s1 e7 L
- title, scope, selAction, _
; c9 C. Q; u" {; @: y - includeFeatures, keepHighlighted, selectionMask_array, _
- j- ?% _, b; Q! B - selobj, cursor)2 H, G; k: ]( q/ [( t2 h# |9 k: f
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
- ?' o! J' w" p: h3 h. q - selPoint = selObj j# v7 [5 r8 E4 o# I
- Return Selection.Response.Ok$ p% U" @! z0 n7 m
- Else! g, ^, E4 i( ?
- Return Selection.Response.Cancel
9 `9 S) V* F0 [" L1 E5 `5 t4 e - End If
. `8 Q: t% E3 ?8 I -
" y8 y' o: k+ O5 ~9 U - End Function+ z/ C4 }4 x/ M
-
4 Z. ]9 |6 k8 W+ o1 i2 F( ]' O - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double# H1 D5 P9 S1 t. h+ D% h
-
$ U/ j* b1 h& D9 H5 E, i - Dim result As Double
5 E. Z+ M8 ^1 r8 t - + E1 R' r0 X ]$ n" M# j
- Try
& b- g; V9 ]3 x' t; \ - Dim nullNXObject As NXObject = Nothing
. R: H, x9 x5 t- Z -
* v. b2 R6 H8 i7 e* @ - Dim measureDistanceBuilder1 As MeasureDistanceBuilder
' z' e- f2 t. q! x8 L( x3 S - measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)) ]$ Y' E% n: B- r% l
- b0 K* I, Q2 H3 S- l4 G: m, `# A
- measureDistanceBuilder1.InfoWindow = False
* o4 Y& t6 H+ {' Z6 D; n F. L - " V2 n3 o; y0 \! b" G3 @' ?& b
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
2 L/ ]6 n+ E4 ]' i -
# A7 E1 a8 \2 l6 T) b; B( {. { - Dim nullUnit As Unit = Nothing) q4 u& \) i- V6 q
- & N' p' X& y5 j) W7 V
- Dim measureDistance1 As MeasureDistance& W$ \! N$ Y+ q7 Y' _
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)0 V, R- j8 b/ o: F7 J
- ) M' K0 I' }9 B% u- I# `! ~
- result = measureDistance1.Value. y8 V* t G3 o
-
9 }5 A+ `4 C' G/ L. Q4 H - 'measureDistance1.Information()
z% X1 `9 L; _2 B8 y/ `# d - ! j* j; D( t, s: ?
- measureDistance1.Dispose()
/ V S- T0 y( v6 X - / f5 [& d/ V" s2 M
- Catch ex As NXException* w& e6 \/ Q% ^/ _
- MsgBox(ex.Message)+ c/ p6 i9 ?7 K9 t! Q5 d2 { c
- Return Nothing
: {( B: \8 [& P( k' z0 D$ I# l -
! n: y! o6 ]5 b4 q - End Try$ G* `' P w; Q
-
3 d( p" P( F% X+ Z a" R# s& } - 2 o B6 ]8 B, X2 K4 O
- Return result
0 h% H& l" R; C5 S5 O1 h -
+ C2 p) F! |" h r8 }) E - End Function, M+ F) D* [/ S% h% |% p: y+ X
-
! }* `% Q4 Y g1 C9 Z- _! e -
$ J; w) t3 b/ V" D* }2 G4 A+ o - Public Function GetUnloadOption(ByVal dummy As String) As Integer
) Q1 D' F& ]: L -
/ ^( p0 n5 |; k1 U2 h, D& D. | - 'Unloads the image when the NX session terminates7 [5 r% q) w2 R: Q% Z. t
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
" b' @0 {( g% \7 j -
( h" q/ H% V9 P - End Function
7 Y: o9 E& F0 T4 Q. i& A1 f. J - % w9 C+ C' t# e6 `2 G( }
- End Module
2 d% Y2 ?- {# D6 s% r! | - </P>
复制代码 , p! i5 H' t; V* u7 F# w5 y* V
|
|