PLM之家PLMHome-工业软件与AI结合践行者

[二次开发源码] NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal

[复制链接]

2018-6-1 09:27:40 2488 0

admin 发表于 2018-6-1 09:27:40 |阅读模式

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了
+ ]2 l$ }9 w9 @( f1 U( d" j0 ^, j" {4 `. M7 A; I
[mw_shl_code=csharp,true]using System;
5 q3 {) W" \, [0 f/ y7 `6 n, Yusing NXOpen;
# [$ k. W! q/ b. x9 g' n; o4 yusing NXOpen.UF;
8 w( N! H5 t0 a- zusing NXOpen.Utilities;5 o, f8 s% J: Q+ p  B  S9 x
using NXOpen.CAM;6 v6 R) s# n$ ^$ C/ `/ b
using NXOpen.Assemblies;
1 W3 C) O$ p' \5 f
. l" T/ h8 P0 u* t, [static class GetSubTypeToOperation2 ?" y# Z& S: M1 f" a6 h1 ~7 x: i
{
( ]$ B2 G( Y( m* `$ k& @    static Session theSession;
6 Z* e" K# M2 V! t    static UFSession theUfSession;7 c$ H" y: q+ Y9 F: M
    private static UI theUI;( v8 U% {! K; G5 l1 Y7 T' [+ D
: g& A7 U2 K! V: u
    public static void Main()
3 b& N2 l& z8 D0 \$ w4 x    {2 c5 H9 v7 i; Y- \% J: ]
        theSession = Session.GetSession();
* i- Q& }& T+ R. h' J        theUfSession = UFSession.GetUFSession();
1 a2 x+ C+ W$ I, N        Part workPart = theSession.Parts.Work;/ d! _/ ^5 L, p" g
        theUI = UI.GetUI();
" L1 e# z3 H! H) d
$ q1 H1 W; ]5 N        if (workPart == null)
% ]( y& j1 h4 v/ K3 h( T$ y        {
( T& K  l+ N/ X- E1 ]1 F            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");9 a: k3 s" ~; s5 x! f0 W& V
            return;4 e$ U- l7 t( O- Z1 J9 K
        }1 `8 b& k; Y1 G2 e8 d$ B. s. C' S

  g/ a# W- u" X2 g3 O3 \/ G        theSession.EnableRedo(false);( G# i9 C6 [/ f3 n  T9 Z" |. y. Q

; i: v0 a9 l6 f! H% k/ |9 g        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();9 |# z' _" L9 {( A( N. L

6 t; L0 W0 }* I; P/ Q        SystemInfo(theUfSession.Part, workPart);
- O: d( _# M) a1 k/ \: k# k
/ J1 U1 W1 O$ Y) {1 G        try
" ~7 ]8 V# x4 g        {
9 z$ q! F0 Z# c            Tag[] operTag;  Z# b! R* U+ D4 z$ n6 U; j% Z
            Tag setupTag = Tag.Null;# K) X# a6 B+ x

3 ?% q/ ?6 v' _0 t% i. g            int countObject = 0;
; p' n1 e2 Y3 P3 V6 v5 X- H( K$ |, n5 e9 Z* y" p7 v
            theUfSession.Cam.InitSession();& f& e& P( g+ Q
            theUfSession.Setup.AskSetup(out setupTag);
; e: E1 i5 k" g1 y4 I+ H7 h0 O7 {# {" T. R9 T6 t6 m' z
            if (setupTag == Tag.Null). D0 l; J: T8 o2 a' P! _. ~
            {+ k+ K! f6 k7 Q5 i  c
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");+ i4 H' H5 j: S! j( d+ U
                return;
& f; F2 _9 [0 U6 n& H* U            }/ F/ \+ C5 Q4 n" q- H0 B
+ E! ^% ?* l1 |& Y. E- j2 Y" g
            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);
+ \# |1 h) ^/ T; u! ]- t9 [
* G% }" O+ U  f1 ~" L5 w            if (countObject == 0)8 D- M$ j; h+ Z6 P
            {
) i( V+ z) e, b/ a, k                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");
7 V' Q3 Y( Z4 F& N) i' u  k                return;9 K7 I0 l+ i4 \) W' [! N( B
            }
8 k" a! G9 g# d
1 j7 A; w; k8 M) C% P5 U            for (int i = 0; i < countObject; i++)
% W, w: l$ P- ]! j1 v- `            {
/ c) F" M  n; q" ^4 F, y" c! g                int type, subType;) U# ^8 F2 r; d% u( U, {

7 N4 ]3 k* D. I5 t) a! n                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);$ {* |6 d% H9 f, d6 P3 D
8 i, i6 o: D& h; ], Q5 \
                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));/ ]. _+ y3 X: b% y8 W/ K; ~
1 p8 X; w2 [# F7 q  l9 Z; g
                if (type == UFConstants.UF_machining_operation_type)& p5 a' V6 X1 y/ q' `/ j
                {7 g; l( o. J! \2 W4 o3 A* j
                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));7 m! T0 U, @9 E- [6 `) O4 _+ v

* n2 T7 k2 ]9 ?4 \& O, U5 f                    GetOperationSubType(operation);
7 ~& p" {: A) f/ }8 T6 ~
- D; T3 D# h1 y( E                } /* if type */
4 ~2 W/ r! q0 V            } /* for int i = 0 */6 K% Y, Y4 \7 p+ d  u& S5 V
        }9 t4 X/ ?' y- S" Y, S  K1 ~; ?
        caTCh (NXOpen.NXException ex)
4 R. ]6 v) F" D- K7 ]        {4 W$ R1 b, {# f0 c
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);4 z5 ]8 R8 ]( f4 {7 b  h
        }2 U4 z( t$ Q8 e+ ]# |, w
    }
/ g4 H; A+ e6 Y; a1 E/ Z5 T+ q) a' H" j7 f

& @3 g. T/ e/ P# S2 [' S    /// <summary>
$ G/ M" L$ b5 R$ v/ o    /// Retrieve the Operation Subtype
4 G( D3 X8 @8 x* z. z; |    /// </summary>$ E3 z9 V) V3 k( g( Y' C4 v! P  f7 M
    /// <param name="operation">The Operation to Query</param>
- `! w7 v7 w& T! `$ R  i    /// <returns>Return the Subtype Operation</returns>
+ v+ C0 [$ s1 g3 L" W- Q+ f4 T- q    public static int GetOperationSubType(NXOpen.CAM.Operation operation)/ O( b' L. F; r4 k# c
    {" c4 |8 y' B6 z! E3 J
        if (operation == null) return -1;3 V) z) D8 |2 W( T- K

# Z% b' {0 G- K( \        int type = 0;
! }+ \' o1 i$ \1 y7 i" O        int subType = 0;
3 R$ j5 j, T, `8 r  `
7 V  H0 u) O* \2 E        try
" P+ {; x+ h) u3 r# h$ j# W+ a( Y, g4 f        {4 [2 A6 Q% C, F# R8 ?5 V- e7 S
            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);
$ N# l* P; E  G. ]  K            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
8 ?- F4 h% N) l* L- h, R9 C9 B
% F- e' Y! l: h4 t* s! j& j            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/6 Q- e! O! j, |) C; l. }8 J
            switch (subType)5 w* v) D0 O- h/ C$ m
            {
( t5 R$ U) a1 _/ d                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */
* z1 ]' _- X' z, f2 m                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
: y* f" ]) a6 z4 K                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */; B' C( _! r9 p
                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */
7 A% D3 v* c* r8 C5 o) s+ L                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */
  J6 t2 q& ]! @" w6 [4 F" g# m" i                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */
3 ^0 [- i1 K( y. f- o: [* y: A                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */
+ `, {' T: r5 d7 A                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
; {/ e+ u& Z) I) ]/ Y% t, v  v                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */* L: k4 {( ?4 h$ G  B  _
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */2 q! G! P& w2 A
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */. V/ v! l6 ]/ t# m8 K6 f! O
                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
5 W- a/ i4 Z9 ]4 E. m% p! h" O' S  g. }                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */( y' |  H( Z' k' O
                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */
3 e. V( q2 w8 B6 e0 q& f4 F) d                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */
* N3 ^! p  W; d                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */
& h& _9 K+ Y6 u( }% s                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
: B4 U, M  z8 e2 Q                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */
2 Q9 o0 V. s( I$ z                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */1 q, y6 A- G, ?5 ?5 ~' J: n
                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */2 z: P% C! n& b% S( @" p8 I5 g
                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */, G6 y& a! n. k/ D7 Q* U0 H
$ L  N) V$ ~- m" @: J6 n# F
                case UFConstants.UF_insp_tolerance_subtype:
" \% _! s, }1 h0 I, m- K                case UFConstants.UF_insp_path_subtype:
; z5 u% W* Z+ B: A& |8 t) p                case UFConstants.UF_insp_output_subtype:
# n' F. z# J- V9 n* i                case UFConstants.UF_insp_misc_subtype:
$ u" s7 R6 Z: X& }                case UFConstants.UF_insp_align_subtype:9 j! Q: f9 i  L" v& J/ y( }8 q
                case UFConstants.UF_insp_sensor_subtype:
" i* o( {: M. x1 l, W$ ]2 p9 k, D                case UFConstants.UF_insp_construct_subtype:
; D0 m% s5 E  _                case UFConstants.UF_insp_bounding_feature_subtype:1 o0 m3 C# `& f( n
                case UFConstants.UF_insp_feature_subtype:- O: y+ o5 x, F4 n* m% k+ @) `+ }

6 _8 S- g7 j( _6 a6 [2 S6 U                case UFConstants.UF_mach_canned_cycle_subtype:1 Z+ h( r8 k* \  r
* `9 f% ~& J- d+ E: O( u! y" V
                case UFConstants.UF_mach_laser_teachmode_subtype:8 U9 M$ w. O4 T/ I/ G( [

# V" D  Q5 }5 X! l. I2 U                case UFConstants.UF_mach_turn_roUGh_subtype:, |, ~# J( H0 R0 Y) b, W% o
                case UFConstants.UF_mach_turn_finish_subtype:
1 I+ |* m, `5 b; q                case UFConstants.UF_mach_turn_teachmode_subtype:
, B- K" f: m+ ~: y8 u, D3 a- U4 S5 r0 u                case UFConstants.UF_mach_turn_thread_subtype:5 U  @+ {& z. J0 t% y- g% e. y
                case UFConstants.UF_mach_turn_cdrill_subtype:
! v6 }, z* {7 D# U$ \+ u8 o                case UFConstants.UF_mach_turn_auxiliary_subtype:
2 b/ {- n3 Z5 ~' V                case UFConstants.UF_mach_turn_probing_subtype:
( X$ [# K1 b; `* F7 F0 g                case UFConstants.UF_mach_turn_tool_probing_subtype:
" U. x, q7 ?' V' P! o                case UFConstants.UF_mach_lathe_mc_subtype:' C) ~/ _, w+ J, u
                case UFConstants.UF_mach_lathe_ud_subtype:; d1 s2 |6 A( S6 o& v) u% k% a" @

( V) R. |" Q$ d& @. U                case UFConstants.UF_mach_wedm_subtype:% a- c- F' q% t" d
                case UFConstants.UF_mach_wedm_mc_subtype:
- W; z9 [2 a0 N3 X                case UFConstants.UF_mach_wedm_ud_subtype:
- C7 p$ Q4 r' q. i, f                case UFConstants.UF_mach_mass_edit_subtype:
5 W' q: t8 @& O8 d2 e$ K                    break;
, T7 n2 _" V# s/ U2 `3 H& @; e2 _$ r% ^) A: [+ K( ~6 ~# Q( f, I% [! C
                default:( p% x' k1 N; z# J
                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");9 T1 @3 X  m- A' P5 n
                    break;
5 _$ {  c0 a# C7 w; z7 `  C            }
) J1 m2 v1 E3 F8 E( h! a( b        }
3 G& B% B$ i0 t6 \6 h! p+ f- b        catch (NXException nXException): l5 Y/ M9 S* P8 O/ U  i. I
        {9 [4 B7 f$ L  [- f' ]$ b& I
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());
' h6 V% Y& ]3 r( A% g% v            subType = -1;
; p' K8 N9 t$ O6 |; Q. X( b; J/ m        }
9 Q' o0 K2 u! [6 ], n3 [1 H  ]
, }( t+ W3 p$ k+ Z: J9 j6 a$ B        return subType;
% [% {6 u3 o6 H8 e    }
  g) F4 X8 ^7 Q5 F. Q* s, ~* @/ Z
! }, }( O% A% @: a: N2 ]    /// <summary>" ^, f3 Q# b6 h& ?
    /// Display System Information
* `  M. C9 y, s: j" d    /// </summary>
& Y6 I( h- p6 S1 A- I0 j    /// <param name="uFPart">The UFPart to Query</param>% Z2 _5 t( \- R, T$ G
    /// <param name="workPart">The Work Part to Query</param>* S& \# v+ V1 v8 n/ h9 z0 F* N
    static void SystemInfo(UFPart uFPart, Part workPart)$ I) C+ N( ?0 L( S- r
    {. s  p4 E& z$ I2 H& }+ T' H8 t
        try
# B8 ]3 n2 {/ Y) H  W! a        {
' T: S, U6 J& K5 u3 {9 P            SystemInfo sysInfo = default(SystemInfo);  ~# H# t" u0 w% W0 ~, ~
            theUfSession.UF.AskSystemInfo(out sysInfo);9 g4 C1 ~' |0 E8 ]4 b+ E
# l  i9 J8 p4 a/ p) h: e
            string partName = string.Empty;0 f4 j& q- l% u  D; A7 S9 R; s  C
            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);4 ?' Q/ b4 J4 P; s% o- s$ u
% p; A, I. ^) I1 ~2 p
            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
7 m1 w/ A! |/ H) ~* {            theSession.ListingWindow.WriteFullline("============================================================");
  h& W! h7 d8 [" o/ ~1 H5 j            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());- j0 p- u+ B5 I* ?. u5 u" X  M9 V% B
            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());. M/ o  q# c5 b6 d! E
            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);
" {; G. t+ i1 P! }$ E. o( [- w6 E2 O            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());, d* @/ R9 ?* f. u) k  ]
            theSession.ListingWindow.WriteFullline("============================================================\n\n");/ |* M) ~$ @' k" L
        }: C$ ?, k) y/ g' `9 g$ w, m- }# k
        catch (NXException nXException). {7 n2 p+ w& x6 J0 c
        {9 j2 {+ ]6 b$ \4 f* j$ ~8 Z/ D
            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());3 F% M; r/ e/ L# i% B. I
        }
/ E3 m/ {$ z- n3 @/ N    }
1 y* Z2 ~! z. d2 I  x4 ]' t- F& u8 D& O( v$ R
    /// <summary>$ k) y2 W) p) |/ _+ O5 i
    /// Unload the Current Image
6 F/ ~  X) m, _' t' j    /// </summary>
8 K( {/ ]$ p- n    /// <param name="arg">String Send by NX</param>
; L* Q$ i0 L/ C! i' j. A5 f2 i$ W8 Y    /// <returns>Unload Integer</returns>/ N* V" M9 n' @) M! S
    public static int GetUnloadOption(string arg)
: N- e( C  `  x+ G0 B# R8 S" T    {" @4 r2 W2 ]4 k$ f4 q( Z- Q6 {
        /* Unloads the Image Immediately */
; [6 l  k! }# i9 L5 P1 m: d1 G! @) j        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);- t. p2 {& U( R+ V5 S, }
$ A, @5 r. W$ x2 h% K) [+ o  i
        /* Unloads the Image Explicitly, via an Unload Dialog *// N8 r/ f+ z2 E' ^2 Y( w
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);( y4 ~- l! u3 o% M8 l
) L- h2 E" T9 D
        /* Unloads the Image when the NX Session Terminates */
5 |  w4 I9 Q  I% F1 k        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);8 U! {  O: v# n
    }2 \" |# k) t6 d% ]" @
}[/mw_shl_code]
( s5 c. ?+ W  X, G5 g) U
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 doTeam.tech
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了