|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ G$ I; C1 }7 B2 `3 p7 @
NX二次开发源码: 装配中选点,并报告父对象
- `) @' R; j2 l& u# Z1 F- 7 v' w; |* A4 b+ J
- <P>Option Strict Off: X6 Q9 Z# G6 ^
- Imports System( \- b$ l. G; r4 N! W
- Imports NXOpen9 Z0 P/ v6 m+ H# g( l7 y
- Imports NXOpen.UF
' O1 e3 m1 e9 ^7 Q& e -
) W% X% Y/ B, j! `" R- I& s - Module Module19 l7 b# r2 B4 x6 S
- - _% J- o( }5 a# ~- D6 X n
- Dim theSession As Session = Session.GetSession()% C% h+ m+ d" t# ]; e
- Dim theUI As UI = UI.GetUI()
8 f: p6 ]4 ]0 f - Dim theUfSession As UFSession = UFSession.GetUFSession()
& ^; `3 n& K( g1 R# H; {& Y+ Q - Dim lw As ListingWindow = theSession.ListingWindow
4 w# b' S0 ~, E6 o: W - Dim pickedPoint As Point
$ ]! P5 T& T& e/ u( q& i) @ - Dim myModelingTolerance As Double' i1 {1 q8 F0 }) Z
-
& e$ R+ \) G% x0 v5 v - Sub Main()
5 g* C- U! p6 B O, Z$ n% O - 7 U& T0 I! D$ { Q; y/ K {! S
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)& G$ V0 W5 n2 n0 P& T
-
/ v: _9 v$ C, T: X1 P - Dim workPart As Part = theSession.Parts.Work6 r2 M" c7 V9 D7 M7 \3 |8 e
- lw.Open()) q' j3 M8 l, Q( p" B; M
-
4 t8 K' J, B! g - Dim myPointTag As Tag. i u8 O2 o2 h& i: b `1 C0 I* F
- + Q/ E' D8 l* E t& M, k" ^
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then9 ]; ~$ y* v( _9 G0 ~7 }5 V. y
- Exit Sub
, u$ y" U2 ^( f# A, u: V/ r - End If2 ?4 ?! n3 R0 u) h" t, m
-
k% }5 Z9 |) e5 ?* J1 R: q - lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)0 ? }5 }2 m. n# @% k5 ~
- ; R0 B( |) Q V( s! }
- AskParents(myPointTag)2 `2 n7 X$ c% a- ]* \& ?* \. H3 I
- 9 l9 Z" Z& g. g W" j! t$ f! V8 E w
- End Sub
3 u# ?3 @7 ~, O5 j% M# J" z -
5 u* Z5 ^# y4 T7 K4 N1 M8 P0 I - Sub AskParents(ByVal objTag As Tag)1 s, E) L% U2 j+ ~; X5 O
- $ c4 ^4 i: i- `9 E. o& ?1 u3 L
- Dim n_parents As Integer. |5 X; h* R& }) v! w
- Dim parentTags As Tag()
6 p- x1 Q7 t7 S. D& A - Dim myPoint As Point
3 B) X! n. ^3 S$ x - Dim myEdge As Edge+ l3 L3 @: K' j
- Dim myXform As Xform6 Y# P; F) q$ P9 m) P$ s
- Dim myTaggedObject As TaggedObject% w% L' R* _2 \7 f7 P! `
- myTaggedObject = Utilities.NXObjectManager.Get(objTag)
' v+ L/ ^3 Q, |3 i( C -
1 x6 j, W* U$ J0 \ - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then, D8 c$ X7 x" }+ z8 g
- myPoint = myTaggedObject
) ]8 L0 k( C6 S% k/ G. F& R - lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
/ x% b- t( S0 u( q3 m - lw.WriteLine("")
6 z8 ^% j7 A* i4 k. _6 F" b9 I -
4 b% j+ k( Q1 D% z- x - Try" P" b0 y7 P0 i) }/ _
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)0 n: H4 q, P l
- lw.WriteLine("num parents: " & n_parents.ToString)4 h& }2 _) {6 D7 @6 ?
- For Each parentTag As Tag In parentTags
! h$ `1 j. }' `; R" H$ e; a - Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
2 p& K4 A7 p: S# n) q ]% _ - ! ~# m' L4 [8 u$ ~) R/ |5 y
- AskParents(parent_object.Tag)8 c2 }8 ]7 \9 S8 {* @7 } q" Z) Q
- ) W. c4 j( {& [4 w: J$ N
- If parent_object.ToString.ToLower.Contains("edge") Then
/ ?. A6 |+ O$ p8 p8 b2 w1 n - myEdge = parent_object
4 _1 @0 \/ `0 ^5 \- Q; ]7 Y - lw.WriteLine("")
' J) p3 n, @( z# k - lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
7 V7 O1 }( j$ ^% D% c4 d - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
4 q6 W4 c' n6 z X) Z* v+ L4 X - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)' P/ r- P" I( [. O4 S/ G
-
( X3 ?$ }, X @8 \+ N) w. q - Dim partTag As Tag = myEdge.OwningPart.Tag# H! J# X! D+ F) g% I! l
- Dim occTags() As Tag" n& I' D/ z* F8 [
- theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
& o+ q: F% Z8 ~& O0 n. r3 i - lw.WriteLine("number of occurences: " & occTags.Length.ToString)/ W% B7 G3 i% E+ D6 m a8 M
- For Each temp As Tag In occTags6 v* M" W ?' ^
- Dim myComp As Assemblies.Component" U7 o% i1 ^! K+ O1 c
- myComp = Utilities.NXObjectManager.Get(temp)
6 r) `7 Y/ M5 Q - Dim myCompPos As Point3d" Y, Q" t* h. z: f ?2 h
- Dim myCompOrientation As Matrix3x3
3 M3 I# t1 y2 q - myComp.GetPosition(myCompPos, myCompOrientation)
& P% b2 L/ H1 K5 Q# Y - + {2 O" V& J F' d) b! _+ N) l
- lw.WriteLine("")8 j0 a4 C6 f) R3 U1 a7 v, `
- lw.WriteLine("component name: " & myComp.Name)+ [" N$ G* x. f2 j* p. v
- lw.WriteLine("component display name: " & myComp.DisplayName), ]* Y" u( F* v7 `$ q5 r
- lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)# m' n5 m* q) ~+ E' I
-
3 i( q0 m0 F% [ - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then1 t: K: ~! w! `' Q
- lw.WriteLine("** This is the component that was picked **")0 }4 P. |+ M. ?! F; P% y9 j
- End If
$ j/ ^ f5 N4 \& i& n; W - 0 y( O- U) L. r: `
- lw.WriteLine("")
) F1 x: G( Z2 B" { - Next0 a9 ~0 G6 {1 j
-
+ q2 H0 @% N; W3 [. E8 j# M/ S - lw.WriteLine("")- t3 P) R j' b! `, z; N/ D$ z
- End If
8 {( E d- Z, K4 a - Next' `2 v, I0 g @5 D# T! [
-
* s3 R5 u' _6 T+ H, X - CaTCh ex As Exception, H- g; [2 L; S* |: O! b
- ' NXOpen.NXException: Current object is not smart1 y e; N% v6 f
- lw.WriteLine(" Error: " + ex.Message)! `# e6 F3 V$ I$ N
- lw.WriteLine(" " & myTaggedObject.GetType.ToString) f5 Q+ i: v2 C/ m
- End Try
9 |, h/ s3 p9 i$ @ X6 F - 1 l2 T0 s7 L! O9 A9 W
- ' u: D2 n1 R: o0 w& G8 F9 o$ l& L
- End If
- I/ q- g+ Q) l" A- I W% O- |* Y7 H -
* n) W1 e+ f% D4 n+ Q2 s -
0 E1 p0 X0 I/ Z9 \3 T8 l4 B- e - End Sub9 |6 u1 P4 L, ]0 t
- 4 ~3 b( K. L5 D/ s
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer
. k, }% d: U0 {$ S -
- k/ h9 }! W& U& z/ E - Dim base_pt As Double() = New Double(2) {}. t2 H6 f1 Z" @9 x0 h+ N! X4 h. f
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
+ V. r6 h# B) e5 W - Dim response As Integer = 0
; ?! X# y/ ~. Q - Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt
" L, o% n$ c9 l7 a. K) L7 S -
A& { W7 Z' m+ W2 v& j ] - theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
9 g' Y2 d$ |4 P' g& D: X - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
' k1 N! R: k0 O - theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
5 |5 k1 S e' \+ o8 [1 N - : l1 l1 O1 h- ]) I$ O/ Z& \
- Dim pointLocation As Point3d
& |" \' f. V M6 l" b8 ? - pointLocation.X = base_pt(0)7 e# X" C6 V# H9 ~
- pointLocation.Y = base_pt(1)
8 c _/ \# r* M7 z# D - pointLocation.Z = base_pt(2)0 P+ W7 |2 A- {: v. X
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
( B4 l+ p) {1 }2 k - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)0 _% I' B* s+ }! j: z
-
8 O% p$ s% J) h) C% r$ ^ - Return response& n$ h+ {2 x2 |* n: E- z Z
-
2 R$ p E& F" ?' O/ m2 f - End Function
& p+ J1 u F* _2 U! H) e -
4 f" q1 n3 X- _: I - Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
, N& k; k7 g# A - 8 r; M& E1 y$ N: S& ~: _" }
- Dim selObj As TaggedObject
# [9 }) b5 y Y6 c) z4 P - Dim theUI As UI = UI.GetUI% @- e+ j& W7 e
- Dim title As String = "Select a Point"# {3 i; } @ D3 X7 N7 i
- Dim includeFeatures As Boolean = False
9 u3 V& ~" b: n+ m$ o4 o - Dim keepHighlighted As Boolean = False
2 X" a" l0 W+ F" |5 S9 z* f - Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
7 E- \8 l5 w" s% ^ T - Dim cursor As Point3d
3 `4 k, i7 `& ~9 ]% u7 ^7 _ - Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly0 ?; K! k5 k* i" s; s3 }: G/ J
- Dim selectionMask_array(0) As Selection.MaskTriple: A9 U& I6 w: A# P+ }
-
! w6 F+ J& { c1 n - With selectionMask_array(0)
# K* _! `2 O# H# w7 B - .Type = UFConstants.UF_point_type
6 Q# P% S, b: f# N2 Q3 d6 ? - .Subtype = UFConstants.UF_all_subtype
8 j: ]) {8 ~2 _3 r0 ]$ ] - End With
! Q& ]; f) ~3 n @" n$ s C - 8 W+ Y0 O% j8 O" L4 H
- Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _& P4 B& x* P# v# C
- title, scope, selAction, _
$ X& i9 F0 l9 W+ V9 Y% ^1 ]" ?$ | - includeFeatures, keepHighlighted, selectionMask_array, _3 q( {. D) B& o F% c
- selobj, cursor)
% O) X0 Y7 E% u+ E - If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
6 ^9 p8 J# A+ @; {8 q7 m% l+ e/ O - selPoint = selObj7 c( v0 A. F! `+ k# x
- Return Selection.Response.Ok
% c0 T! G) H- k& z, ^5 D* B. V) v - Else
0 q( L! z3 b8 a9 _ - Return Selection.Response.Cancel8 l6 s$ r9 T6 y6 q
- End If& x7 J$ F7 z- Y% T( y5 p T
- ; y7 s7 R) _1 z1 j# I
- End Function0 x6 i" z: a% E9 [3 R( y9 B' u% N
- 1 K5 y; {$ O" K4 X [" R5 k/ }
- Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double
8 ?4 n5 x4 b" H7 a9 F3 m; d - / \1 ~- n% U7 K, V% [" y
- Dim result As Double
+ J9 B$ }0 u E5 i0 j. D -
4 C6 C, |. R& k- p7 Z" y - Try
% g% L, _& |7 s* G1 T% Y( U - Dim nullNXObject As NXObject = Nothing3 k8 a, z$ l! H
-
: f5 r6 i& V. j - Dim measureDistanceBuilder1 As MeasureDistanceBuilder4 a& ?3 M5 B# x2 e/ b v
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)6 S( k8 }( K3 M C
-
; A3 W1 B Z* S3 X, ? - measureDistanceBuilder1.InfoWindow = False
- ~3 g/ O: R6 H* p5 |) Y. B6 T0 `- ]( F! j -
2 o/ S0 L5 g; f; V - measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum- P- r7 K8 p0 x$ d' w
-
/ A- R9 e* b- q( R - Dim nullUnit As Unit = Nothing
& D# q# L# p& M* `" [6 k - 2 g- X9 l5 P" Q
- Dim measureDistance1 As MeasureDistance7 j+ \ `, \: u! g& z8 k
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
% n1 m' E, E% X2 M - 1 _3 n0 `6 h" j9 ]0 N8 ?6 f
- result = measureDistance1.Value- v2 j' e- g9 q: r( k3 P Q
-
( J! Q& z, l/ W0 p3 o - 'measureDistance1.Information()2 r8 d1 U3 P$ D4 ^8 z1 k" a
- ! I' O$ {- |, {0 f8 {; ?+ @
- measureDistance1.Dispose() Q& d; i$ W: e2 x5 z
- ) ?4 v7 }8 [9 C
- Catch ex As NXException
: ]8 j' n/ T; Y8 B" T - MsgBox(ex.Message)# u5 D7 ?7 W) Y+ F2 A
- Return Nothing
* q% k* b1 }. K3 g! _ -
7 U6 W4 D, A! l3 U3 o1 V Z - End Try
: p2 _. N. G1 ~' g* D -
0 q+ n+ h: n' {3 ^ B/ h2 V+ F - 8 |" F5 _8 r. p! c1 X+ R+ B
- Return result
3 _& i" t; [/ ]1 O - 4 h+ s. g+ R2 {9 H$ f
- End Function5 r l& ^* v$ P* H
-
, x7 t C4 g* g -
! Y; h$ p5 k9 ]1 O4 c) Z. x - Public Function GetUnloadOption(ByVal dummy As String) As Integer& U9 y; i: H- _, N/ T( ^
- 9 R* `: D2 b7 d( O/ v& G5 v: I' P% X
- 'Unloads the image when the NX session terminates
8 U& U# P6 i: `+ W - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
! ~7 o: H$ f5 k4 _4 Q$ K: G+ [ -
" ~8 C u/ B6 ?. k# e - End Function0 c4 C, y% O4 e7 [; u9 N! Y
- % k5 G7 \! `- F8 K1 y1 S @
- End Module2 |9 ^. A1 S1 E% j% n
- </P>
复制代码
: F/ h8 L1 ?7 b/ k' | |
|