|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
/ p9 W8 }9 u' b4 h' Q
NX二次开发源码: 装配中选点,并报告父对象7 I4 s( o; r1 C) o7 E3 ~) H- d$ O
- ) a" ?" X0 i+ y! g2 A6 ?
- <P>Option Strict Off/ o: `* M: A- Q+ P' d
- Imports System% o/ r& D7 `$ o# S4 ]& Y% F
- Imports NXOpen. q6 f% n* K& w: t& m$ w
- Imports NXOpen.UF9 D+ d: x6 {+ {
- " ^5 l% M1 `* K3 C+ x, l) C
- Module Module1
4 f m' n* c( y! ~ -
1 }6 L5 e) c i# h+ H+ W9 t - Dim theSession As Session = Session.GetSession(), i* P! Z6 V" |1 k' \
- Dim theUI As UI = UI.GetUI()0 k$ F' V B7 Z7 U3 q/ [
- Dim theUfSession As UFSession = UFSession.GetUFSession()
6 @6 w9 P6 [( h- @6 |0 x - Dim lw As ListingWindow = theSession.ListingWindow
7 X% O2 B5 l0 Z" e - Dim pickedPoint As Point; o7 c1 w' G: y- b+ n
- Dim myModelingTolerance As Double
5 R7 q' R* U/ p4 R/ [ -
9 z2 [3 u# w+ H - Sub Main()1 k8 ^% q- G" e' m
- ' Y9 o t4 ~2 B; s
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)4 p. X+ i5 p/ n# [+ l9 B
- 8 E! E4 ]. k' ]4 j, {% f$ o& F
- Dim workPart As Part = theSession.Parts.Work
, k; q0 S$ f w3 t - lw.Open()
! T3 B3 X# B' m- f0 u Z' d8 ~% G -
( e, l& Z0 \2 q: Q - Dim myPointTag As Tag
1 q! V- x$ W4 p/ z& e - ( |; q% @% D E
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then. ]3 Z( p* H+ i' q V8 l
- Exit Sub
+ I" v; c, s. R3 O$ U& c) z% N' G L - End If5 V, n. e0 R# |# o
- 3 J, ^, G' t2 \4 c6 z
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)
h' Y( X4 P* ?) l3 q/ B -
: h# `, {3 [! m" j1 s9 P - AskParents(myPointTag)( n i0 ~* l* U
- 5 T7 u$ {" y) r( ]2 o
- End Sub/ Y [- C- Q( s: J, X1 e& K
- # ]5 h; F7 U& F4 F
- Sub AskParents(ByVal objTag As Tag)
: p+ t* V4 [; Q - # a. ~+ y7 W5 ?3 M9 y) y% Z
- Dim n_parents As Integer/ s! \! {% I+ h0 f
- Dim parentTags As Tag()6 W; N4 N7 O7 [! h
- Dim myPoint As Point
! B+ z8 O; U. u4 f" G7 e - Dim myEdge As Edge
3 k2 ?# J" D0 M8 r& v3 V - Dim myXform As Xform
& T; `6 w0 I8 y/ C& z# l - Dim myTaggedObject As TaggedObject
6 |! p# [7 Y) Z P8 u. _ - myTaggedObject = Utilities.NXObjectManager.Get(objTag)- p" @ K4 F1 ]& k( B& i
- , J: M5 |* l$ B# k( R# k7 V E& u
- If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
# v/ a( q1 P. ^6 `# j5 D - myPoint = myTaggedObject
$ Y- M G& n0 f - lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)9 Y6 }2 X. K' k" U; u4 F2 [6 n
- lw.WriteLine("")
K3 @& W3 _% n: O9 ] - & ^/ m7 ]7 u( b3 V& A% f8 ?
- Try# G) ^3 K; r" J) w
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)7 w* @* J( U v9 [; y7 z/ W% i
- lw.WriteLine("num parents: " & n_parents.ToString)
7 N- b" |- I' \/ ^3 } - For Each parentTag As Tag In parentTags
9 z/ i/ D7 t3 Y/ J, \" l+ I - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag); ?* k q3 D/ y) f I
- : E/ d0 ]6 l6 F" q J
- AskParents(parent_object.Tag); G9 i0 L/ X1 ~4 J
-
: u; Z0 u' g' A - If parent_object.ToString.ToLower.Contains("edge") Then' V i8 G: {7 K9 Q: J, j; ]! {
- myEdge = parent_object% [3 @6 E- A; _7 i; W
- lw.WriteLine("")
$ H9 n7 s3 x" x! c2 b: l; z- V- O X9 C - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
1 V1 m! j& [% B$ [( S - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
1 y6 z8 e- a A+ E% d4 [' O - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)) f8 @/ ~! q) M+ y
- 5 Q9 S2 `- @1 I9 e/ W4 T2 a5 t& r) f, {
- Dim partTag As Tag = myEdge.OwningPart.Tag' _$ T9 m/ U l8 m7 B
- Dim occTags() As Tag
$ t: f+ m& G' J* n - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
8 \* u3 Y$ s% y" P+ r - lw.WriteLine("number of occurences: " & occTags.Length.ToString)0 T3 t: E1 m6 X+ K3 \; G+ U8 x
- For Each temp As Tag In occTags
( i' G& e( b1 R - Dim myComp As Assemblies.Component
% {: B- i" ~! L# O) b1 @9 z - myComp = Utilities.NXObjectManager.Get(temp)
" e' e3 [& \- S - Dim myCompPos As Point3d7 Y5 W% N6 l# ]- P I3 g
- Dim myCompOrientation As Matrix3x37 X9 ]( ^* r4 y6 s0 D
- myComp.GetPosition(myCompPos, myCompOrientation)+ ^' Z. { t! ]$ }7 z
- 3 G7 j; l6 K% H, n9 v/ X, E
- lw.WriteLine("")# T9 T0 Q, U" e. s+ O" t% x. A
- lw.WriteLine("component name: " & myComp.Name)
( a; i7 L3 W: m* ] - lw.WriteLine("component display name: " & myComp.DisplayName)- u3 s X3 g m2 o W. i
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)6 a! ^# P' X/ b9 D5 ~& ^, @
- + u1 B& @" A; j4 c; m
- If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then/ o, N; O. _/ ]4 x3 \
- lw.WriteLine("** This is the component that was picked **")+ b+ [6 \* Y$ ]3 F+ q8 k; t; g
- End If. C/ }( X: l6 a0 e( ~
- " x, w* `, h0 ]4 m7 j
- lw.WriteLine("")& F4 x' @* U, n
- Next$ R# _+ d* F }. j ]; @: F& H# ?
- / P; t' V* F, b `# V! g
- lw.WriteLine(""); i9 l4 }' C+ i! n
- End If$ X0 i$ m/ {, t, s
- Next
! G" e1 U O" h: H& f; W/ q5 A' H, Y -
. I) `4 W' _. { Y1 w# z - CaTCh ex As Exception8 q I1 Q% M" `- }
- ' NXOpen.NXException: Current object is not smart
8 w; H& z7 C; I" \/ t6 u9 v4 _ - lw.WriteLine(" Error: " + ex.Message)
+ L! r6 Z1 b& B - lw.WriteLine(" " & myTaggedObject.GetType.ToString)
/ y7 F' T/ a% l1 e4 D9 m - End Try. O- `. h% w$ d: u8 E& ~6 w
- ) G8 [; \, S6 F6 a, x2 ] Q( E p
- 1 U% a1 L v) f2 c) O9 f2 f2 P
- End If& ^/ H' _+ {! ?4 H! Z
-
7 h: I, j; ?0 P6 n' _% t - ( h% g/ X. ]4 ^% m6 H6 G9 |+ n
- End Sub7 r6 `% |5 w! C* `& u' B
-
$ g7 O7 n$ i5 J9 g - Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer2 c8 _0 g) R. _+ N; j+ K
- * K( O! i1 |% ~) a* J' o
- Dim base_pt As Double() = New Double(2) {}
$ a3 T5 N8 K7 H6 e$ \ - 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null8 B$ C" _7 u: h0 B, O+ g4 \
- Dim response As Integer = 01 u# u1 T- m6 W$ F
- Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
" y. Q1 q* ^# E6 L: e -
* {; S- {" p2 S% I/ p, ?# T - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)4 ?7 i& _$ z) j9 @
- theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)! I g6 a3 f# j( R7 ], f( r. v
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
+ E% I D5 X, ~, i/ Q -
% r/ R6 b% _; [# w9 U - Dim pointLocation As Point3d- e- x# D1 K1 `4 u* F
- pointLocation.X = base_pt(0)$ I2 T2 l5 ^( y. E
- pointLocation.Y = base_pt(1): t9 q3 f" d& J! `/ n' C
- pointLocation.Z = base_pt(2)
# x! m, z0 [, s - pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)" s v) P' J$ x; L' Y3 c
- 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)8 n/ d) E% h, R
- . u5 B4 K6 W7 A
- Return response' p1 }! y) Y8 O9 z
-
^" A- q3 G' x+ I5 b$ {* u - End Function+ ?! }, ?" |; E o
- & w3 \% W. _0 }5 K6 A
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response* D9 t! A3 y! r x- |0 p
- e$ J3 {3 ?! h0 L9 n. i) s
- Dim selObj As TaggedObject' q# |1 `% h- N. b2 d8 a0 ~
- Dim theUI As UI = UI.GetUI9 R$ G% k) E3 y
- Dim title As String = "Select a Point"
. O5 i8 A1 R) s# S1 b - Dim includeFeatures As Boolean = False
$ r9 Q' m8 o, P0 \7 Q# R( X - Dim keepHighlighted As Boolean = False
6 p# k3 P7 |; {2 u# d ]0 }9 A - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
9 W& V9 N7 J. ? - Dim cursor As Point3d
, B+ f- E2 ^: [ - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
& R8 d5 J5 e; ~; H, ]& A& I5 u4 X. k - Dim selectionMask_array(0) As Selection.MaskTriple! k) R1 n X0 [: i2 I$ E
- 5 O7 L3 E% F# d' ?8 Q+ e0 }
- With selectionMask_array(0)
/ c; A# G; x: f5 R x - .Type = UFConstants.UF_point_type
. ~% M8 o/ f3 O$ j( \# D - .Subtype = UFConstants.UF_all_subtype
- j) l) `& y @+ C, R/ {; w/ G - End With
, l: f' e8 V% H) l; b -
u! d1 C( B! ]8 w% h - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
% F4 r d- w3 s. l. m5 K- |) Y, { - title, scope, selAction, _7 v) g U' V$ Q: J
- includeFeatures, keepHighlighted, selectionMask_array, _$ ~9 k, b, \& T7 O
- selobj, cursor)
2 g- U! d7 L' A& R+ @ - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then* F! X9 Q% T ^" S; j% B2 R# T c9 U- P
- selPoint = selObj
C2 g$ z E! K! u3 K - Return Selection.Response.Ok
0 m d [* V9 a- |" a/ W! { - Else
% j! w! r0 k8 d- d - Return Selection.Response.Cancel- _( T0 c0 k$ G: H
- End If
5 e' H+ E+ Z1 V$ d) } - 8 f% e7 h* ^3 c8 w' b. X6 H. Y0 w
- End Function; Y# t! a3 K/ N
- 4 d' R' o" x" w, x- H
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double: J$ |6 s, h+ ]6 d9 ^$ ~* W% z
-
$ ~# H. M/ P2 _8 i$ ^ - Dim result As Double
$ f( O* P3 W) ^. |* ^ - - ?* t. p0 a1 S9 _8 [7 O$ p
- Try
" a; j" M- }$ x3 L- E - Dim nullNXObject As NXObject = Nothing
9 j: }4 `$ D6 c - ; k9 C B* M7 D: u
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder- g6 i! ~6 j: S+ n& {% _' u+ B/ U
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)0 B7 Y# N% @0 A% X8 Y* O. E9 n& S
-
! j6 N$ `! \, x0 r& s - measureDistanceBuilder1.InfoWindow = False: A7 i3 Y0 K: \9 B' ^. W
- # f& Q* w7 ~) D1 ~ i- ]
- measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
0 H" W) ^. q$ U) e -
& i+ N' ^' r5 W# z' n - Dim nullUnit As Unit = Nothing
2 n+ r1 ^) {9 K9 g% @3 Q$ D* o -
" P: n9 A7 L. L1 P& n5 I' y- h4 z: x8 Q - Dim measureDistance1 As MeasureDistance* [1 n9 P5 ]# n! w1 ?
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2) e) K6 A( J- s+ E" Y8 s9 \4 y
-
; ^. Y$ Y; [% \8 N V - result = measureDistance1.Value. l# z B/ V0 w4 H! C; A/ D
- 1 {/ @' z' e+ H0 ]" i! ?. r* K7 n
- 'measureDistance1.Information()2 e9 r* P; E% Q0 o5 m( O
-
0 `' c; S! p( b, F+ G% ~. L - measureDistance1.Dispose()8 d$ t, m; W7 [
-
" Z7 q" i$ N( f - Catch ex As NXException$ d% f6 f1 }: ?( L7 |, y( c
- MsgBox(ex.Message)% n' K# a# X9 s; q' ^* i2 d8 U6 x
- Return Nothing; C. R7 a% H) d8 K
- 8 [6 e( W5 G: u# y* P7 k: z
- End Try) y/ ^) G8 C. B% m% f1 D
-
! p$ c7 l# ]" `- {8 ~1 ~ - W( b& r: _& p( J
- Return result& D% k4 T5 r( X2 @2 E
-
9 E c" v8 o, z3 p4 p/ ]# @. g - End Function
' B" @" F9 v0 F* e% _) Z -
6 a5 g) D. X0 Q4 }/ ~: d+ N -
' ^: k7 b% P( S- r( u. N- l1 N - Public Function GetUnloadOption(ByVal dummy As String) As Integer
# W, x7 j6 Z+ E6 d - 9 w* U* T3 n7 C7 H1 f# I2 v
- 'Unloads the image when the NX session terminates4 f; M3 P: o1 V
- GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination+ u% j0 o) F8 b; |' P: ?0 Y
- 4 m& h! d4 \% V$ X
- End Function
* R/ n! V% q9 _ -
, v6 E8 |+ Y+ w - End Module- ]: _" ?! T; U% m6 l# @
- </P>
复制代码
. v9 J4 m; N3 D3 d1 m |
|