|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
: k4 Z% w+ r; j7 \, i+ O2 V. _
NX二次开发源码: 装配中选点,并报告父对象 X8 R- T+ f: } g0 E6 A8 N+ V
- / J% d, W0 t1 Z! J, d3 W
- <P>Option Strict Off
1 }% w8 l$ ?+ t- _- ^6 s! `% e( v - Imports System
2 N! x' S' o, C' X$ r - Imports NXOpen5 W! j: g/ m7 Z5 H! I+ d
- Imports NXOpen.UF
( s4 l, y& I {+ L# x R2 i - 7 H; E& u9 ?+ l8 b! H2 d4 W- x. ^! ?
- Module Module1
- G6 t, {( U5 U$ a% h/ |! `0 d - : I7 ]2 |% a) ~4 l
- Dim theSession As Session = Session.GetSession()2 {0 P: C# N) d
- Dim theUI As UI = UI.GetUI()1 _$ ]* z8 J. \! Y* k5 x* T" ]
- Dim theUfSession As UFSession = UFSession.GetUFSession()
1 A) D4 X; B5 ~- K, j- M - Dim lw As ListingWindow = theSession.ListingWindow4 t& I) A& G, f
- Dim pickedPoint As Point
" C& y2 ]1 M& ?* S6 g* U - Dim myModelingTolerance As Double
% I( j: x6 ?/ B" _8 l+ a- s) d -
: Q" j' d! s1 M/ i/ k - Sub Main()
# d& {) I' B6 h: H" w5 @ -
" h, j1 D3 w) R: {& c+ A4 K0 k - theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)
1 _6 P2 p5 [0 c7 o" I! a0 J -
1 ]9 e6 c) {8 K0 S# d7 @& w - Dim workPart As Part = theSession.Parts.Work
% y- d, ]3 h: u# N, [ y - lw.Open()
: N9 H, A( W0 |! v$ W8 ~( L -
* \9 s3 D* S% ?9 [8 w - Dim myPointTag As Tag
5 _ g- C+ {! Z -
! w# w- ]- \ o - If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then( J6 H% E) v9 B8 u9 ]6 \$ f
- Exit Sub* v/ C, Q3 P: H0 Y$ `
- End If+ F6 y5 r. n+ a& k- h5 G) s; b
-
$ u7 a* _8 y$ d$ A- ^2 F( { - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)+ P a0 v& R* v. u' L0 y
-
/ t% F' Y/ D3 S6 A/ l' w - AskParents(myPointTag)6 L) ~. e& i8 {9 p
- B$ A( ^0 a5 b' d4 q5 \
- End Sub8 n9 Y! k5 v! h# }7 A F
- 1 t6 A) Z2 J. @
- Sub AskParents(ByVal objTag As Tag)
) k8 Q& X3 d9 L- z -
+ u5 i) w' V2 \) ~& ^6 b( g/ k+ i - Dim n_parents As Integer) e0 }" J2 t! l3 m* O0 G
- Dim parentTags As Tag()9 r+ E/ H. ^$ P2 x. ^, b0 C
- Dim myPoint As Point3 R5 ` J. a) z
- Dim myEdge As Edge' W8 H2 a' U( r: x$ X; i2 J4 n% I$ h2 P
- Dim myXform As Xform/ [. i; H, g/ n) e; _' y
- Dim myTaggedObject As TaggedObject6 Z, X$ I- r! v T/ S$ i
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)
t% f$ F) z5 Q4 r. z -
& n! H0 Q; c, F- D0 g - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then) }0 W4 v! L; `& N G* b1 I" ]9 h
- myPoint = myTaggedObject/ k: c1 ?" @$ e* V
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)3 x+ E7 e. x a- c- m# P
- lw.WriteLine("")0 Z. o7 U- n L* X' s
-
0 s" L8 H" s* ~ L z# `) c - Try
5 C. W% u' J5 r& Y. A9 \ - theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
4 C Y4 j) U* K Q# F - lw.WriteLine("num parents: " & n_parents.ToString)% d: r% S( M7 o7 l3 M$ N& [* ^
- For Each parentTag As Tag In parentTags
2 {1 u( Y# t- ~' Z7 ^2 v - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)5 y$ U6 ]& K. e" |1 r. V: ~
-
) x! d2 ~! c/ q7 e4 A - AskParents(parent_object.Tag)
# ~0 A) H# w9 G! K! a, T -
9 j* e# ~ U: D - If parent_object.ToString.ToLower.Contains("edge") Then
; ~5 m. U( `3 ^$ n2 x5 A& `/ h* r - myEdge = parent_object2 v' i' O8 X. \( `6 |6 k) h
- lw.WriteLine("")& `* o' M* t; P! X/ V$ c9 b# k
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)6 N0 B5 P# t9 n8 X% g5 l# ~
- lw.WriteLine("edge length: " & myEdge.GetLength.ToString)' M& L- ]5 [/ V5 |2 P( c
- lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)
* c' t T! R' s% y: @6 {# s -
* c9 N; H1 m- s+ ]1 K - Dim partTag As Tag = myEdge.OwningPart.Tag. a2 D8 \0 W' [$ |6 ~9 j
- Dim occTags() As Tag# v$ k. L: T# H; I$ T* [
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
! Y. ~( D- Y! u* Z3 a - lw.WriteLine("number of occurences: " & occTags.Length.ToString): S& F( K# x: d& I( Y3 C
- For Each temp As Tag In occTags3 _1 [ C& s2 g& L/ ^9 F
- Dim myComp As Assemblies.Component
2 [$ Q1 M* M# ^4 [: k" f - myComp = Utilities.NXObjectManager.Get(temp)
: S7 ~& G B( |. N6 o/ s - Dim myCompPos As Point3d) C1 I3 v5 K+ P& ~) q3 O) s% l- N
- Dim myCompOrientation As Matrix3x3" S" X4 e# l: d% ?( d8 g! Y) F
- myComp.GetPosition(myCompPos, myCompOrientation)
$ i/ K: T" A3 L* ] -
3 B+ U0 S" u) k) `; V+ \ - lw.WriteLine("")
E' r' _2 p+ r; {0 |1 [" V: c - lw.WriteLine("component name: " & myComp.Name)6 q: }, S% D3 W4 g6 s4 v! B. F
- lw.WriteLine("component display name: " & myComp.DisplayName)4 l" ~8 ], [6 I0 R9 S
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)7 ^3 J! ]) B+ B
- 8 {' Y- t/ u# J* N0 Z6 b8 x" S7 a
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
8 o2 z1 J, ^' a7 {" x6 J; Y - lw.WriteLine("** This is the component that was picked **")
% S3 e1 A" @; m+ u- M f% J - End If, t/ @1 f! `7 h. ?: x( L& ^
-
4 b7 z& W) ~+ I' S2 ~8 D - lw.WriteLine("")
- G, ]0 O7 H) E" }3 j - Next
, g1 n+ {; x$ | t - ) @2 S; A0 z. l
- lw.WriteLine("")
# ]2 h& S+ u* e/ ?( \ - End If
1 u) r# X @$ |. C7 W x" D - Next8 w0 i3 b# L4 s( ]1 Z
- ! L( V" N9 E5 Q. e6 F
- CaTCh ex As Exception
, m" M# _! B9 S9 W - ' NXOpen.NXException: Current object is not smart" D E9 V2 f4 h
- lw.WriteLine(" Error: " + ex.Message)
1 o+ b* s" R0 e5 l$ A) \9 S - lw.WriteLine(" " & myTaggedObject.GetType.ToString)
' E! i$ I1 D6 a+ T7 ~; S3 r C% d* \, R - End Try
/ b5 Y& }( g A9 p6 n! p. ^. F -
; m7 Y, t( a4 l9 J; j - " l+ [, @/ B6 T$ R. ~% L
- End If% _) d* G2 w5 [3 p
-
: S' J% l0 p+ w* c -
8 g$ Z0 D9 {7 T8 ^8 N Y5 C& Q. j0 V - End Sub
7 V; M4 C w3 t. \8 w -
% X0 R! y3 y/ K4 h - Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer& F( w6 @, ^' K# R! D% B- g8 F7 [4 I, A
- ( A! b3 F1 \( c1 E2 j$ M
- Dim base_pt As Double() = New Double(2) {}' h2 u' i4 P9 {. T1 q- a G3 g
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
, N- A6 [2 t8 Y/ L0 [ - Dim response As Integer = 00 B. | O' p( d9 G* J/ e/ \9 ]
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt5 A: ^: q; C- d- D& }5 K
- 8 h9 O1 H4 g) n
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
: @3 N4 C. ^7 L! Z& w - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)% Q: W" V3 k; ?% I1 r# g. V$ B
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)& Z0 a# b* \; E
- 0 u: Y: q2 H) Z7 v0 a8 U# d
- Dim pointLocation As Point3d' N2 i( V8 t) _% z3 r+ N7 t
- pointLocation.X = base_pt(0)
: i4 K+ M' j X - pointLocation.Y = base_pt(1)) F/ |% n# {& b) k
- pointLocation.Z = base_pt(2)$ `4 M( N8 z6 X: Z. i [' S4 P! C% r
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
5 U# z' @% g+ g - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
1 O: [ t7 u/ c9 `1 I/ m -
2 V6 c5 B3 s! g f3 V$ z* q# X - Return response
" E. Y1 L2 y: n5 _/ W' P! I4 N -
4 M9 V, n2 J, K. V3 i3 A0 o1 [ - End Function2 o& s; L5 ?9 ]6 v
-
3 Z) I h4 m8 |9 C( v Q - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response- v6 |9 V' \ l+ ^& |- i/ K) j
- 3 q; v, ?5 p/ b0 z$ p
- Dim selObj As TaggedObject8 m6 L" G# T2 w- o3 A/ ~
- Dim theUI As UI = UI.GetUI
8 U M$ _4 A' L/ u4 n, \3 U* Z8 S - Dim title As String = "Select a Point"
$ U/ N% P5 x; n - Dim includeFeatures As Boolean = False* b0 k5 E1 Q& P @5 s
- Dim keepHighlighted As Boolean = False
5 T+ l) S) d4 n0 w - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific: L! J' \, U. A' v; a) b' W4 f
- Dim cursor As Point3d5 p) S. ]3 ~; N8 o7 B. {) r
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly u. h. n* w" l" q+ {6 e( w
- Dim selectionMask_array(0) As Selection.MaskTriple
) H) Y9 F# B5 @3 Q7 F - : A9 `1 C. O: Q. ?- a( y) I4 ?) {; |" B
- With selectionMask_array(0)
) F& ~" u( v. F* T' U+ V, ?" | - .Type = UFConstants.UF_point_type
8 d+ q# y0 A+ v& b. v% Z3 Y3 ~: P - .Subtype = UFConstants.UF_all_subtype
6 K& w7 Y4 T6 \0 s - End With) v/ U9 _3 d2 k7 A1 U, r
-
- ]% a2 x- _" ^+ v - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _+ t, G, ]: D# \/ D: i6 A
- title, scope, selAction, _
5 u* _" d- l4 {/ w2 _* Y7 P2 C& R- I; y - includeFeatures, keepHighlighted, selectionMask_array, _9 A( @/ u8 w$ [3 x {9 d
- selobj, cursor)# q3 q: F0 V6 ~7 R" C3 N+ l0 }6 z
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then/ ~+ b0 O6 U+ C8 _; \3 e9 Y
- selPoint = selObj/ B7 F/ g+ i# V$ b
- Return Selection.Response.Ok
' z, S) m& |9 C+ p# L - Else
l ^7 ^: N9 _% Y" k3 \ - Return Selection.Response.Cancel9 w" H$ ~1 Q$ T# V4 C$ X
- End If' H$ e# @9 f( u9 {8 D/ b
- / a: T" Q6 Y) I8 M
- End Function6 \, g! Y1 D" F
-
8 C+ q( c& _# s+ C: c3 t - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double( [1 S* i" k( _+ D7 F
- : h/ ^9 f1 T5 _6 ^) P( N
- Dim result As Double
* {$ H; M* }( f1 S8 E -
; v3 p2 C! t& \4 X4 j - Try
3 k' _! R; t/ V8 J& n' H - Dim nullNXObject As NXObject = Nothing
* w# S- U4 T3 t% c -
/ p$ Z/ N/ [ ~+ I1 [% [ - Dim measureDistanceBuilder1 As MeasureDistanceBuilder4 Y' h1 G [ `* u
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject); s& D1 k, q* t1 g
-
8 w8 }2 r+ D4 z. k - measureDistanceBuilder1.InfoWindow = False
; I1 Z0 E; X8 f& w - # v6 |5 i% R0 V8 b" y, t! ~6 ]& A" f
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
& H5 W4 ~. e- N. A1 k5 r7 E - 2 ]! h* M4 Q) q
- Dim nullUnit As Unit = Nothing# y' C( r$ i. c) i0 @' b8 |
-
( I; L$ w1 B, M8 U - Dim measureDistance1 As MeasureDistance$ J: Y# v! _: o; B( @4 a0 x; r# `
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)# U) ]8 |5 b, S1 @4 y h2 q6 T, [
-
6 Y1 U( N) g0 M% A1 j* q - result = measureDistance1.Value* W/ ~# c6 M6 W) N p) G% A
-
4 ~6 E, x4 q, n) a8 n - 'measureDistance1.Information()5 {% R+ K3 e6 r
- 0 C. i! Q5 m2 f% J% |& a+ P
- measureDistance1.Dispose()( L% O: a' a* N! {( f# d
- ' r' g! \ t8 t% c3 K& ?1 Z
- Catch ex As NXException
" T9 x' o5 S# Q% t# R$ f) L - MsgBox(ex.Message)
4 M- D' e& c. |3 Q; ~ - Return Nothing$ E2 p' r# _: }1 b; |. J
-
8 N2 u6 g2 ~6 U" R1 \# }6 F - End Try" H9 w5 Z& v- p% N: W
- & T( T, {) m/ ]) g3 } B
- + ]* b( N$ u+ E
- Return result
+ C: q( ^4 p6 w' n, y/ y! b3 B -
: v: M; @5 E* w% G4 A - End Function
9 ?* [! o" z* u: M7 m - 5 R' d& c* F2 \7 ]3 T% Z( U
-
" K) `8 W, F- |9 w3 Q- L2 S - Public Function GetUnloadOption(ByVal dummy As String) As Integer) G$ V1 T* h# ?0 X+ I, F7 y
- : p) x2 m% i& J& V% t; z2 S% F
- 'Unloads the image when the NX session terminates
N3 e+ \" q |6 ]$ Q+ h( ] - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
# m7 U" P( ?8 f2 x: g- F - 3 J: n- _8 [7 C
- End Function
U* m! A* x1 g& b: v -
6 W$ _5 a" e5 \* k! { - End Module
: U f) q9 b, g0 E* H0 H: m0 ` - </P>
复制代码
( c6 o4 `0 b; [9 }! c |
|