PLM之家精品课程培训

PLM之家精品课程培训

联系电话:18301858168   |   QQ咨询:939801026
NX二次开发培训

NX二次开发培训

UFUN/NXOpen C++和实战案例

适合初级入门或想深入了解二次开发的工程师,本培训结合ufun,NXOpen C++,大量的实例及官方内部的开发技术。
公众号二维码

关注公众号

点击扫描二维码免费在线高清教程

课程详情
Catia二次开发培训

Catia二次开发培训

市场需求大,掌握核心技术前景广阔

Catia二次开发的市场需求大,人才稀缺。掌握开发技能潜力巨大,随着经验积累将在汽车、航空等领域有所作为。
B站二维码

在线原创B站视频

点击关注工业软件传道士主页

课程详情
Teamcenter培训

Teamcenter培训

全方位培训,从基础应用到高级开发全覆盖

涵盖用户应用基础培训、管理员基础培训、管理员高级培训及二次开发培训等全方位内容,由多年经验讲师打造。
QQ群二维码

加入同行交流

点击扫描二维码加入QQ群

课程详情
×

PLM之家plmhome公众号

课程涵盖: PLM之家所有原创视频

×

关注B站视频

所有高清视频一览无余,全部在线播放学习

×

加入PLM之家QQ群

同行交流,疑问解答,更多互助

PLM之家PLMHome-国产软件践行者

[二次开发源码] NX二次开发源码: 装配中选点,并报告父对象

[复制链接]

2013-11-5 17:35:29 5465 1

admin 发表于 2013-11-5 17:34:33 |阅读模式

admin 楼主

2013-11-5 17:34:33

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

x
. ?, _/ m+ I! E4 V. @
NX二次开发源码: 装配中选点,并报告父对象4 j: P5 |- ^* ^- p) {& i# w. k

  1. 0 o# u% z# w  k9 n% u; n
  2. <P>Option Strict Off
    % q3 f( H4 r0 }" X$ ]! D- j
  3. Imports System
    , ^6 l" R# }9 H) A
  4. Imports NXOpen
    2 g" W! `* |0 a: @6 m. ]
  5. Imports NXOpen.UF
    ; b# S( B- L3 j- Y; h* {+ e- {
  6. . O2 m. q( ]' n9 [& d
  7. Module Module1
    3 r$ n# D6 x. c5 Q2 }3 f5 a5 b- S
  8. - j" |1 J' _& _0 i8 Q
  9.     Dim theSession As Session = Session.GetSession()
    . N6 A/ Z1 Y1 T- @% G) [
  10.     Dim theUI As UI = UI.GetUI(): p/ C5 \* X& w5 t7 F" }3 W
  11.     Dim theUfSession As UFSession = UFSession.GetUFSession()" y+ ^! y7 z* h! R4 n4 E
  12.     Dim lw As ListingWindow = theSession.ListingWindow; v# G4 P" Q# y. r8 ?# D3 z
  13.     Dim pickedPoint As Point
    2 n) F# Q- K+ G# ?; _8 T
  14.     Dim myModelingTolerance As Double1 L, d# N0 U- C7 O8 y- S; Q1 G
  15. , _+ C# C- J0 w8 g+ h* H+ P
  16.     Sub Main()
    5 G; p; z4 b& X) W( f
  17. & H; L/ n- a3 x. c/ d. A8 p
  18.         theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)! ]! q/ G" m* ^& P# q" k- H

  19.   J% O$ P7 o( j; D% t1 T
  20.         Dim workPart As Part = theSession.Parts.Work+ W& n5 r$ l3 ]/ T' [$ R
  21.         lw.Open()! O2 a' {5 P) R5 E3 O( L5 s$ Q
  22. 9 u6 Q3 b, W+ U$ A
  23.         Dim myPointTag As Tag- D* @9 Y, l0 R- W( y5 {4 a

  24. " d$ y4 h$ j( i: d
  25.         If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then" J# U6 B' }- W3 X
  26.             Exit Sub, K$ g7 w+ M9 Y
  27.         End If" R: h& A# i9 i& `0 a7 y6 g
  28. ) f: o# l+ c) b3 H9 Z( {( x. p
  29.         lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString); Q. a9 D# e; j& ]
  30. ( s: r0 i% r& M2 B5 M' z) o0 g
  31.         AskParents(myPointTag)/ i7 H8 F  |" L

  32. / @7 n: e: Z' \) ]* e
  33.     End Sub1 g- c& W# T6 U+ H

  34. / G% m0 R. J6 U. T# P3 z
  35.     Sub AskParents(ByVal objTag As Tag); y3 _0 Z% e6 S7 E/ g' ]0 p

  36. , Q- D5 ^; L0 Y; d9 G4 i
  37.         Dim n_parents As Integer7 L. J4 t% k1 T$ Q8 U
  38.         Dim parentTags As Tag()
    - z9 M- W3 N1 d6 i! d
  39.         Dim myPoint As Point: z9 v. |2 s' o  \( O
  40.         Dim myEdge As Edge
    # t0 ]* U$ _( \$ s) \3 j
  41.         Dim myXform As Xform
    3 w% M: ?0 t( B+ \9 L9 m& ^
  42.         Dim myTaggedObject As TaggedObject
    ! @6 D2 M- w- K2 `
  43.         myTaggedObject = Utilities.NXObjectManager.Get(objTag)
    ' S- v; L8 r3 _. X! G, b) T+ D+ t. `/ m
  44. + |/ W' {$ V- n3 o
  45.         If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
      n* @5 [8 o! n0 ^
  46.             myPoint = myTaggedObject: V! t0 U; D. K+ e9 {* n* a; w3 @/ H. ^
  47.             lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)1 F3 E3 ~2 n( ~8 l" |$ P( @! L
  48.             lw.WriteLine("")
    6 m) _" n, ~/ k1 c, |

  49. 2 E" t  o' ~  M7 I/ X' }
  50.             Try
    0 L8 v7 C* u4 Q  c
  51.                 theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)) Z' t2 E+ m, B
  52.                 lw.WriteLine("num parents: " & n_parents.ToString)
    ' _& u. F7 A$ Q6 R, I
  53.                 For Each parentTag As Tag In parentTags
    5 A+ A. M* f  G5 E& N5 Q; C) M
  54.                     Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)
    4 y: k% x2 Z' t6 a: W& o

  55. 0 f3 O1 ]4 m4 U2 R. g/ e
  56.                     AskParents(parent_object.Tag)
    + x; l, W" J% N0 J

  57. , I' e" D/ y& _
  58.                     If parent_object.ToString.ToLower.Contains("edge") Then
    , F& F: E4 c5 t5 p: U( A4 f* d
  59.                         myEdge = parent_object
    1 K3 ]9 O# l- C6 P  ?( p
  60.                         lw.WriteLine("")8 J1 H* t+ h! t6 y6 G0 o1 W
  61.                         lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
    6 x: m4 p5 Z+ ]3 ?0 Z3 L
  62.                         lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
    0 R" x, \' H' c2 l# v
  63.                         lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)) k( i1 _  y9 E9 `6 n
  64. : H0 g  _9 _7 f$ o$ M5 W1 g6 `
  65.                         Dim partTag As Tag = myEdge.OwningPart.Tag
    & K6 o2 T# c8 x
  66.                         Dim occTags() As Tag5 H7 [, T4 @$ {8 y
  67.                         theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)" M1 D; p' W  G+ e1 H, e
  68.                         lw.WriteLine("number of occurences: " & occTags.Length.ToString)0 N1 u3 e8 m/ ]
  69.                         For Each temp As Tag In occTags) O! i  X1 l; y4 C" u/ z
  70.                             Dim myComp As Assemblies.Component$ E& x  l0 A; `0 i
  71.                             myComp = Utilities.NXObjectManager.Get(temp)& G( h. l) ~! d1 |7 T5 z
  72.                             Dim myCompPos As Point3d
    ! W& @0 ]; f' G( L" r) T+ |) Z# ?
  73.                             Dim myCompOrientation As Matrix3x3
    8 H# D# D# F- i8 L8 F- b, `
  74.                             myComp.GetPosition(myCompPos, myCompOrientation)  y; X$ d5 e* a$ D* D

  75. : e$ t5 q) z$ X. U) j" [
  76.                             lw.WriteLine("")5 k! q5 p3 b0 p- F+ C% e
  77.                             lw.WriteLine("component name: " & myComp.Name)
    6 Q6 H8 J) d. p, c7 l
  78.                             lw.WriteLine("component display name: " & myComp.DisplayName), k9 l* z! e- I% |6 ]0 J1 |+ x- B0 c' I
  79.                             lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)1 q  j# i; r! E" C; M! j, r
  80.   S1 u% b/ a1 M  J
  81.                             If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then/ y/ e; }9 l7 W
  82.                                 lw.WriteLine("** This is the component that was picked **")
    $ z, r0 a) U/ e/ M5 Q0 J( r
  83.                             End If
    6 p: S) o7 `! u- J8 I

  84. * @4 n3 ^7 L! \. b
  85.                             lw.WriteLine("")
    5 s# O" p1 P2 C* m4 n" P
  86.                         Next4 P4 Y+ z4 m, ]. e2 T( t! `. E, O, w
  87. # M. K5 I+ s  m. U5 b7 k
  88.                         lw.WriteLine("")" e9 l! V4 p) K; [1 {5 c7 v
  89.                     End If
    - q' O) \; F4 x" A
  90.                 Next
    6 t" B% E/ v# f3 `1 G; L$ p" q
  91. ) H8 z5 M8 X5 `9 U8 c
  92.             CaTCh ex As Exception  F* d  {9 c! _  M
  93.                 ' NXOpen.NXException: Current object is not smart  h7 G) X& p% }; X) k' \1 B/ x
  94.                 lw.WriteLine(" Error: " + ex.Message)  o( G  E* i' g8 H8 Q1 g( j
  95.                 lw.WriteLine("    " & myTaggedObject.GetType.ToString)
    4 w+ U5 V% L% b$ s9 J
  96.             End Try2 w; Q) A: K0 G# s
  97. , {" R6 d- \% g5 F' Y0 D% S" t# _' P
  98. 6 V0 b. r/ e- Y0 w) t
  99.         End If
    / \6 r- x" J, D" F- a& m0 B8 g4 E
  100. , y) w+ N3 }. V6 p" l/ m7 \

  101.   P& z4 v; `6 N* k8 p
  102.     End Sub9 U$ G1 p2 x9 O2 M' r2 X, Z4 l4 i6 ^
  103. 8 G/ n( z! J  ^! j% x+ H+ Z6 b
  104.     Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer6 o  x8 X& ]. x/ d8 }. d
  105. ! @. I& ]7 P# V6 V0 s
  106.         Dim base_pt As Double() = New Double(2) {}! _; |+ y( v0 K9 ~) J9 T3 y
  107.         'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
    / X" q5 i3 l% _* Z# `: ]$ u0 k
  108.         Dim response As Integer = 0
    7 ~- q+ P1 R  a
  109.         Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt, O* N7 K/ l" Z) I4 |; a5 A

  110. . b- K7 B) l; O, L7 ?; S6 l
  111.         theUfSession.Ui.LockUGAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)4 k1 S5 `0 a1 {# Y' j1 F# O; Z
  112.         theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
    ) e' c% e1 @/ n  n1 F7 ?; C" x2 G2 s
  113.         theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)% w/ @; D8 }. r, G8 l  o3 l
  114. 6 `/ w! q: H% w* o$ [- ]  [7 G  c
  115.         Dim pointLocation As Point3d# {$ K5 y/ g: w2 R5 U
  116.         pointLocation.X = base_pt(0)
    + J% J4 I$ l. J
  117.         pointLocation.Y = base_pt(1)0 Z$ n/ ~" ~7 G
  118.         pointLocation.Z = base_pt(2)" x: d' H% ]! y- v5 j( W
  119.         pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)5 i2 ]' \3 V3 R; u' D
  120.         'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)
    # r. f( [/ p2 X( ~9 n3 h8 J; S8 a
  121. ; V" C- p' \7 v3 w5 q1 |: k
  122.         Return response
    7 U3 U$ [; `' {3 d& i/ Y
  123. 4 w: M( y- Q. h$ A# }
  124.     End Function
    $ N0 t1 j3 T0 a0 e

  125. * ~: e- y' W- r! J# W! }, U7 b6 y
  126.     Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response
    / }9 V$ |; L1 M

  127. ) Q1 s! _1 x6 R+ K' Q% S
  128.         Dim selObj As TaggedObject# N. j  j# K6 Q
  129.         Dim theUI As UI = UI.GetUI
    0 q' Y8 l* I$ s& e' c
  130.         Dim title As String = "Select a Point"! U4 F# P/ W& [
  131.         Dim includeFeatures As Boolean = False
    0 n" q" W" p% _8 {
  132.         Dim keepHighlighted As Boolean = False
    / x! b9 a" q$ J; ?- F
  133.         Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific. O) q0 G2 e' C: E* n
  134.         Dim cursor As Point3d4 S: n5 _! H1 d& O! w1 j; e
  135.         Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    / u: \5 f: h( g( Z* ^1 @8 h
  136.         Dim selectionMask_array(0) As Selection.MaskTriple7 d8 Y- L) ^. R; J0 m7 s7 |
  137. / A; o4 d8 G; s$ |
  138.         With selectionMask_array(0)
    ) Z( I6 K( K; B0 l, t
  139.             .Type = UFConstants.UF_point_type
    8 r7 t2 }0 b3 T! h. H/ ?2 ]7 I9 d5 a, ]
  140.             .Subtype = UFConstants.UF_all_subtype. u* @+ K- X7 z) G
  141.         End With% [- r& _3 }4 r% V. }& k3 ?

  142. ) g9 P7 F* s; n9 F6 D' @1 h
  143.         Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _5 m0 A) m& J  i2 b- I9 P9 u
  144.          title, scope, selAction, _# J5 J& d3 Z) g
  145.          includeFeatures, keepHighlighted, selectionMask_array, _5 J7 G4 `/ P5 S* z( {
  146.          selobj, cursor)
    ; F2 ^$ ^: d$ d9 J6 \9 b' Y
  147.         If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
      O& M6 Q$ ~6 Q
  148.             selPoint = selObj  A7 m* C+ t% u7 n/ W& E  d# H
  149.             Return Selection.Response.Ok& g' F) p7 B+ y7 C
  150.         Else6 \5 l2 k7 B$ A% B+ t
  151.             Return Selection.Response.Cancel
    , e5 r- t9 L6 q5 q( r$ N4 a, N
  152.         End If9 g) t4 J1 y2 N' y

  153. ; V3 h% T3 L3 M6 Y
  154.     End Function+ H- X1 {* z' B3 A2 T

  155. : b) Y2 ?" S  f. ~& E$ T% o9 s
  156.     Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double0 l6 g' n8 f- v, |
  157. , }( n' i9 b4 w4 L- }" e  O& J) K$ j
  158.         Dim result As Double
    5 L- d& g2 t3 p

  159. ! g# W  R1 q! ]# v) q& {% n. Y
  160.         Try5 g, S$ p) e+ K# q
  161.             Dim nullNXObject As NXObject = Nothing
    ! u8 y8 H" ~3 I% L& U6 I- C

  162. ! v# E2 @, C6 O" ~5 u' g
  163.             Dim measureDistanceBuilder1 As MeasureDistanceBuilder
    ( g0 @, i) I+ x4 Y* ~$ g% e5 _
  164.             measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)! _( v4 s0 @7 a  Q5 W, L" x6 j; ~
  165. 2 Y, J  [- {5 e5 \1 L! L
  166.             measureDistanceBuilder1.InfoWindow = False* o9 b3 Q3 z" t
  167. - t% s+ _$ X- i+ w- D
  168.             measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum
    : A1 Q5 N' A4 H: P* u3 S
  169. . r* i8 i; E" b  j0 i
  170.             Dim nullUnit As Unit = Nothing, m- d; Q$ a5 V- ]. Y8 H0 B( Y

  171. * i% r2 `, V! }+ C( `
  172.             Dim measureDistance1 As MeasureDistance
    % Q# i( U4 o* W' b
  173.             measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)
    ' z% {/ z1 F. j2 i. Q

  174. + `/ d3 |3 x  ~5 N
  175.             result = measureDistance1.Value
    # E6 R9 J' D( [( Q% a; Y

  176. , J5 k/ |1 x0 ^) }6 c# L, O  B
  177.             'measureDistance1.Information()$ V# }1 W  z; d$ D$ ?2 i' D
  178. $ U8 L3 \/ Q  n' Z% L7 `& |+ M
  179.             measureDistance1.Dispose()) F4 N: M3 }5 z% L5 p2 d
  180.   P1 d5 G) s4 ~3 O
  181.         Catch ex As NXException% b- {# y1 w1 ~7 H2 ~
  182.             MsgBox(ex.Message)
      E' w$ f) Z6 B3 G: J
  183.             Return Nothing: O6 v% V. x/ M. ~8 a( k

  184. , \/ z0 k2 w' D$ c. z: ^4 O" W
  185.         End Try$ ^7 P9 e* v1 L7 e- \
  186. - g+ z( U( j3 |8 g' B

  187. 1 F$ e! `4 j; I7 y: ]8 e/ q
  188.         Return result2 I" T/ B- N: m: R) x4 T
  189. ! m  s3 @+ x$ Q% w  f
  190.     End Function/ z$ ?2 F% @5 s- j8 P6 L

  191.   ]+ f+ \& E. j4 ~: e; N
  192. 3 |' G1 _6 k( q$ G1 B1 {: z
  193.     Public Function GetUnloadOption(ByVal dummy As String) As Integer' Z5 j3 Z' L* A
  194. / A# t! B2 x& A# S& [
  195.         'Unloads the image when the NX session terminates
    * C; F; [8 {  s3 X7 `9 P0 L* g
  196.         GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination) ^$ Y! W! e- N3 Y7 B9 A

  197. 7 z" z3 g% w( z
  198.     End Function
    ) b; _8 y0 B& J* p1 Y0 Q9 _

  199. ! c$ U0 J- e% B! n; K; D1 p
  200. End Module
    1 V. m+ `- C4 o: |/ L7 L( b2 k4 v
  201. </P>
复制代码

4 T* ~- Q5 ^/ ^5 e" m; b
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com/ doTeam.tech
回复

使用道具 举报

全部回复1

admin 发表于 2013-11-5 17:35:29

admin 沙发

2013-11-5 17:35:29

直接 跑下 Journal 就可以看结果了
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com/ doTeam.tech
回复 支持 反对

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了