|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
" M2 J+ S1 m+ J9 ?
NX二次开发源码: 装配中选点,并报告父对象! }% `4 V5 }5 I$ ]4 A6 R/ e
- 6 U% Z4 {% v" u$ z2 o( q* A) L* G
- <P>Option Strict Off
( I! _: u& q: {8 h8 _( e+ T - Imports System
7 |- ~. J; s% ^$ _ - Imports NXOpen. {7 y: o1 z/ d) j
- Imports NXOpen.UF
1 E. [/ Y2 c0 v6 x. W -
3 X v' u2 Y% G3 ]3 v - Module Module1
" S; h; B+ g* q7 d r6 f - 4 T9 k+ s. g" _
- Dim theSession As Session = Session.GetSession()# Z& |7 j5 W; b0 }' W* I8 {/ q! Y# s
- Dim theUI As UI = UI.GetUI()0 h8 J2 z! r4 E6 i4 H! t1 x0 Q5 o/ \
- Dim theUfSession As UFSession = UFSession.GetUFSession()% V. Q- g* k! O2 i5 T4 _$ t! S
- Dim lw As ListingWindow = theSession.ListingWindow ~% N/ u4 X- `+ d h8 A3 B; z; Y! u
- Dim pickedPoint As Point
+ t# \- }, P" |4 A1 [% J - Dim myModelingTolerance As Double S" b3 R7 o2 o( s3 J; M6 J
-
$ K5 X1 T. e7 z; s - Sub Main()2 u! }& X) j7 x1 x+ j
- 1 f- R/ [, J6 r7 d) Z6 C
- theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)) }+ f, _. h, r4 A9 y6 S
-
" \5 q6 {9 L; r, j - Dim workPart As Part = theSession.Parts.Work
; _1 W1 i' t% t& B - lw.Open()3 R) ~+ v- X; ]9 I( T* W* X C6 y- d
- 4 o* C! Y" C/ J8 t) @/ w2 @9 k' O
- Dim myPointTag As Tag
& W" n$ W) g% J; | s - 9 {5 F3 x8 H3 m3 k3 z. [( [. ~( t
- If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then: v# Q" D4 D: \5 H
- Exit Sub
5 C! E6 ~7 O4 A) r: v1 { - End If
7 u e) r. z; ? - : i1 T) Z/ e6 f2 u
- lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)! N9 Y( ~ v6 [' p
-
2 ^% p: X2 U. L- F$ }- ~8 ^ - AskParents(myPointTag), W" O2 m U- w; r
-
$ |3 x% K1 S' {; q - End Sub! @5 d) t8 o( H E
-
+ p6 S0 M' g! T - Sub AskParents(ByVal objTag As Tag)
* D& T0 t2 ] z$ p7 ^$ e -
T k& V* z5 K1 f - Dim n_parents As Integer
! S8 {: e( B4 O# u - Dim parentTags As Tag()
/ }6 G1 u5 r) l3 G - Dim myPoint As Point6 y, ^' U% {* s" H4 W
- Dim myEdge As Edge3 N" C8 ~ c- p8 T1 W4 u7 K7 r9 K
- Dim myXform As Xform/ O3 ~3 r4 x& q: D+ i
- Dim myTaggedObject As TaggedObject
3 l) S; Z4 u& H - myTaggedObject = Utilities.NXObjectManager.Get(objTag)
+ N& u8 ?$ X( ?" q -
0 `) o: G& c# v x4 T5 U - If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
6 y: \/ B1 L" e% t+ O* x; J% h2 i - myPoint = myTaggedObject9 _/ A' F. h7 v" M! ^7 i
- lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
. m2 }6 W. [1 b' i @+ l - lw.WriteLine("")2 m& ?6 u$ N. D/ F& @' O0 i
-
! `# k: Z6 E) C - Try% J% s/ g; L- \: p d
- theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
- v8 {" l: O+ f+ d0 q8 b - lw.WriteLine("num parents: " & n_parents.ToString)
" j a1 F, M* q7 a' S( {: E - For Each parentTag As Tag In parentTags8 T# t' R4 H. w1 K; e) H
- Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
: W* Z2 {$ }6 {9 _9 E* M - 2 s8 @! ~ @9 k2 b' w" K
- AskParents(parent_object.Tag)1 b" U! c& k9 s" A& r5 V# c7 O
- " y7 \2 R6 C# w1 ]* F. u
- If parent_object.ToString.ToLower.Contains("edge") Then( Y# Q" t- t+ |" M- `
- myEdge = parent_object
& r- `! x1 t, g9 p - lw.WriteLine("")2 j# Q. y q1 D; i0 i
- lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
9 \2 t# q9 q/ k# o. d9 B& B - lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
* b/ Y' Z9 T6 o$ Z# s- t# H/ A - lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)4 k: r4 z8 q+ Z
- 2 e( @% s" M2 x# u$ I
- Dim partTag As Tag = myEdge.OwningPart.Tag7 C; H" |7 w+ y' G R0 Y* K" Q; K
- Dim occTags() As Tag
, m1 m6 @& a1 r1 o4 q - theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)5 v2 Y+ w2 p' n" s8 Q
- lw.WriteLine("number of occurences: " & occTags.Length.ToString)& O5 I' V' o2 h8 g6 S* d
- For Each temp As Tag In occTags, I3 U$ ]: A& l# G. K4 [
- Dim myComp As Assemblies.Component! n# ~9 c, G# P' Y$ V3 t
- myComp = Utilities.NXObjectManager.Get(temp)
1 O0 V1 p( K& k# `: i7 C - Dim myCompPos As Point3d
: p+ P8 J$ f5 o; M" r - Dim myCompOrientation As Matrix3x33 Q: {) q9 e0 }! \$ J) B4 f/ R& G
- myComp.GetPosition(myCompPos, myCompOrientation)& x1 m5 {& Q' o/ U0 P! |/ G- r# B' j
- 0 l* d9 j5 g0 `. L$ w: d
- lw.WriteLine("")
3 h0 W: W) d+ @& |3 c7 f - lw.WriteLine("component name: " & myComp.Name)2 \& Z: t1 q( b \
- lw.WriteLine("component display name: " & myComp.DisplayName)
/ a8 U1 ^; k K! h - lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)1 k0 d/ ^, _8 i3 |1 J4 s: a
-
|1 Z+ V! g; i+ K9 Y - If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then! g+ s5 ^! p3 x& A' k4 Q% @
- lw.WriteLine("** This is the component that was picked **")
* j3 {5 H4 n+ N% I - End If2 N. F4 s* C, U
-
E: n, j! R# {( y - lw.WriteLine("")) m' J+ a8 X) {) E, r
- Next
2 E) G2 Q- O( i) ]. H - 3 ~) e. O1 h; H3 [
- lw.WriteLine("")
* p0 E% u6 h) |6 p$ J, b2 ] - End If
- h- p" ` n. B P# @; j - Next, E& }. y7 p5 r7 G9 Z3 v+ [; i
- . ~ Y- f/ x; C
- CaTCh ex As Exception
! X- g+ L' R. y9 l% J6 p0 L' d) G: y - ' NXOpen.NXException: Current object is not smart8 G. h7 I( Q3 d- a3 m
- lw.WriteLine(" Error: " + ex.Message)+ |' J) h& h1 y) e
- lw.WriteLine(" " & myTaggedObject.GetType.ToString)$ \3 }) b4 Q5 |' W4 h
- End Try7 D3 n7 p0 @+ p3 [2 R" ?3 Q
- / K$ j& l( `/ i* ]; u
-
5 l) d+ @1 _& h) Y - End If
8 v g# o8 i" g& `; L& C J - 1 W8 K3 b& ^5 m' t8 {
- 9 ^: |( N3 b! ?6 Z) H2 p7 l i
- End Sub
+ }% }4 j4 s: |- Z- i - 6 ] ?+ q0 f" ?6 W6 {
- Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer0 T4 {( a) n, k& q1 Y0 Z
- & R+ J; G9 s4 R" l* O* M2 }
- Dim base_pt As Double() = New Double(2) {}% ?! o# X x, G/ u' N% o
- 'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
. G5 U0 y( z4 a, Q2 _ - Dim response As Integer = 0
' _- P1 O0 d+ w( \( f( B" Y& h - Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt& J1 t- Q v P' N
- 8 p0 e* U0 p) |2 h8 @2 B
- theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
$ F: B U% a: S2 f6 s - theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response); T* `; f0 @" h
- theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
% O: h: v0 g6 Y' N- ~ - ! E4 L5 S1 m- v0 Y! B" p9 u; P
- Dim pointLocation As Point3d
& m2 I) f$ t# C - pointLocation.X = base_pt(0)
: n3 Q8 `$ d0 w+ _- L S - pointLocation.Y = base_pt(1)# h2 d( Y2 {. ~" K& {' T3 M' b
- pointLocation.Z = base_pt(2)1 C5 L8 ?0 A+ X8 m
- pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
5 z& |3 G2 ]1 @ - 'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible); S& @" b4 Z9 h, h
- ( w" m( u8 A c% p0 i
- Return response
- v- X: {3 J0 I7 o# e) X" l -
) c6 o* ?/ k' Q7 ~5 t - End Function5 i) s9 I2 ~$ u6 S/ C
- - r! @" g( _5 q. c$ ~% I& G8 W
- Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response5 K+ f7 v6 A; R1 K' U' G
-
2 v( V& L, @8 j) D6 k3 t& u2 V - Dim selObj As TaggedObject: o% z$ E; M2 q3 r, e* b
- Dim theUI As UI = UI.GetUI
+ @0 J S6 {" @( `- e/ ], \( A - Dim title As String = "Select a Point"
1 q5 P' k$ z+ U; W" t' j: S- @# |- Y - Dim includeFeatures As Boolean = False5 `6 T u* V2 @0 \6 i! {
- Dim keepHighlighted As Boolean = False. r7 t1 G7 L, y8 H0 Z2 R! z
- Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific5 f. z1 q: o$ }
- Dim cursor As Point3d7 e1 p' _0 \6 e' y# x+ f. A1 ^
- Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly6 u6 _8 s' S0 ~
- Dim selectionMask_array(0) As Selection.MaskTriple& _* F/ [; k! j1 s" {; ~# m
-
! g% S8 C4 K2 M S - With selectionMask_array(0)2 a @" l& L o2 I; n, b
- .Type = UFConstants.UF_point_type
9 u: x1 y9 u7 ]# U. n+ G! N6 [' g - .Subtype = UFConstants.UF_all_subtype
+ V8 \" `* ^& }6 o - End With. T6 Q9 ~. z7 U+ a/ |
-
& _" y T. M2 z, D - Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
1 q/ s4 Z& {( q0 f7 p: l( p - title, scope, selAction, _
! u4 o- ]1 R, l6 Q: @" i6 z% q - includeFeatures, keepHighlighted, selectionMask_array, _7 W {5 C$ e' u! k
- selobj, cursor)5 Q7 |1 l3 y9 z9 i+ j! q
- If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
5 C/ z h1 v& g - selPoint = selObj
% J# O1 ~) Y7 T$ w6 y! Y; Y - Return Selection.Response.Ok. `+ X, S; M1 ` t2 X1 t/ X$ \' d8 L
- Else, k8 b0 s/ m; h* ~
- Return Selection.Response.Cancel
. @! O; I' e+ A. Q/ e K& s - End If
; |; Z G: f( a$ L0 W1 ?2 { -
0 a3 k- c' a. C# ]& i - End Function6 K B' f- _- L( Q& P* t
-
) G T/ R: X, h. Z" B! Q$ I - Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double7 ^' d, L/ S1 |7 m
- . K# N2 R( t- k
- Dim result As Double
9 B$ a& S- ?. B4 }4 r. h$ g. { -
4 V0 |+ X# H2 n+ {# s - Try, W/ ^) W; }0 X. G
- Dim nullNXObject As NXObject = Nothing u/ k5 p% s( D2 d' I
- S# M6 O$ A2 J/ ` O
- Dim measureDistanceBuilder1 As MeasureDistanceBuilder. Z5 H2 e# P6 j' k r
- measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)
; X2 H2 ^6 k: D" p" [" ?* h$ X - 4 H$ h0 ?( S I
- measureDistanceBuilder1.InfoWindow = False
( O3 u% X4 ]* I* u' W: W -
; Z* @* ]' k: [4 v$ B - measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
* D, }! Q1 O* G - i2 m7 h( b; `1 Q+ H. o
- Dim nullUnit As Unit = Nothing
$ e2 S, Z1 r9 A1 m- L( p4 z) q -
- [9 R- V8 }# U0 i" S+ j. g+ K - Dim measureDistance1 As MeasureDistance* m* y* A5 Z, e
- measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)" x& j3 t5 A8 N
-
, B7 I, X: R" D* |$ o7 L0 x$ G- C - result = measureDistance1.Value
( |% x b% r$ t, R - 9 }; V$ X1 `: |7 R( N
- 'measureDistance1.Information()8 Z/ D( A u$ K9 h9 @
- , A/ R {- {' v+ ?2 D
- measureDistance1.Dispose()1 Z" w- F* g4 h
- 9 R- \! ?1 Q3 p Y' Z* `3 m% ]
- Catch ex As NXException f% p! l# k" u+ b3 s- b
- MsgBox(ex.Message)
6 C6 Q6 Z2 ]1 G) T7 ]) l - Return Nothing2 H) R" Z3 @9 ~/ {* E
-
: ^1 f4 h& F, v2 J - End Try
# i' J1 G% P) ~ I6 @% p1 y6 x9 ` - " K; H# d+ W- o! k) s
- ( `' s' Y0 a% I8 d# E
- Return result0 e6 P$ W3 B* U q2 q" _5 N7 v% C
-
1 Y v; t( @: f0 G - End Function# T6 k' g4 v; h7 K# N
- @( V9 G6 m# k6 w- [
- # t" [! s9 d. K' l: I
- Public Function GetUnloadOption(ByVal dummy As String) As Integer$ @( R/ }4 X+ T/ r
- / L. Y' V4 g9 ?7 m
- 'Unloads the image when the NX session terminates
+ i/ }4 E" r2 E- N - GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
5 w$ C/ r/ X0 k - 4 x' T' v2 x" k& \6 ~
- End Function
* Q, U+ `8 t. {2 \. ?0 ^3 T! D8 }* @0 ^6 a5 w - ; ] g% [5 F. C3 m0 y/ I
- End Module
$ u% g" ~4 |( i8 m - </P>
复制代码
* \! ]4 f8 n" J# Z/ u1 z; V0 U M2 _ |
|