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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了
- P, k( z/ |& [7 \/ k' M
$ R0 m# F4 f, s9 k% m' X  \5 S[mw_shl_code=csharp,true]using System;
+ B; A) j0 r* \' r9 susing NXOpen;+ G; S3 a- h6 E2 e, L) ^% O
using NXOpen.UF;. _+ |6 x0 e4 i4 ?) s$ q, T
using NXOpen.Utilities;
# r& t! V! f( u3 jusing NXOpen.CAM;# ~) Y( c/ t% n8 X/ I
using NXOpen.Assemblies;
& q0 Q5 B+ K& @9 a6 e5 Y( E& F0 e) y$ c. e' T
static class GetSubTypeToOperation
: ]; g: r) w& p+ P4 S6 _2 U{' H6 T0 p$ C: I2 \& X
    static Session theSession;4 o/ n* e3 \! l3 Q' j
    static UFSession theUfSession;
5 @9 }: z" `3 `0 @( T    private static UI theUI;
$ `: G6 L5 t. i$ g6 g/ A% w6 x. }; h' I" n; B% M3 \
    public static void Main()
+ r" n( G# D0 s    {
( `3 b  i0 r0 d5 p        theSession = Session.GetSession();
+ |& O* p, ~& _/ A* u$ o! L' u        theUfSession = UFSession.GetUFSession();# v) b* ~1 r" N5 c/ H+ @; U( `
        Part workPart = theSession.Parts.Work;6 P9 E/ v; @0 D+ r. [+ i* R' o
        theUI = UI.GetUI();
: G- {. J( _+ \& ]7 D
# l, x  g! i- a9 v+ ?' e' A! n1 `" x        if (workPart == null); P% `! c5 C, q& c& y
        {
7 {% R2 E- u: `2 x            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");
/ q& Y: @0 L8 g  i. s, W1 t- f% {            return;
( X3 a) [3 g9 X- `' U3 B        }7 K. C- R" F" X$ I8 I# W
: a! M0 x+ i. Z6 v; p3 U1 y
        theSession.EnableRedo(false);/ I& e6 l  O/ I0 ^, L
0 |, H( n- [, w
        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
, m7 K5 w7 }( v) l* I/ E9 |! K/ ?' b" |
        SystemInfo(theUfSession.Part, workPart);8 x  M  |) O4 x8 Q# m: V+ p# m

- p0 b6 E" k$ u* F4 u6 o; J- R        try9 n2 s. L5 e& t0 g& h/ a0 [( Y, ]6 |7 j
        {  @: [8 @0 H) M4 n* ]6 [$ G7 o9 w
            Tag[] operTag;' Y. b% ^# f$ X) w+ l
            Tag setupTag = Tag.Null;
* ]- Z( ^/ u+ P# m6 r7 I
, }8 Q7 M$ l; D( u/ `            int countObject = 0;
& h- ?3 a8 i8 k$ @- p1 q* D$ |6 C
4 n* `8 m% y) B( T, L4 ?3 m            theUfSession.Cam.InitSession();! X& R! [2 ~8 H1 n! l
            theUfSession.Setup.AskSetup(out setupTag);3 K7 X: H; ]' E6 E7 q3 O  U5 |1 O# k
. l( P7 A$ r  ]3 Q* e4 \5 b; d
            if (setupTag == Tag.Null)  T5 U  Z& b9 V3 h% H: x  P
            {
. B! i- N) K( a* z6 n$ S3 _1 ?                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");
! O, x. I/ g  \. B/ K2 W                return;
+ f1 K+ Y! M0 s# n4 D            }
2 O0 k& f/ l5 K8 |. S0 {' z9 ^  r; Y! L3 M2 f
            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);* d/ i! L6 V4 F' C9 O; |
" b& g9 K7 S2 v! s1 L
            if (countObject == 0)! [! N# T0 M! g  O3 j- E3 K3 t2 y
            {0 Q6 Z  i) l& V+ w
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");
  x; F7 J! c! h/ F8 I                return;3 I, |% `4 t- s' E5 y+ s" N
            }
* s  S% b5 B" }: k3 U: [9 x; |) X- O: Q4 G0 q0 h, c# }# F; N
            for (int i = 0; i < countObject; i++)
0 k! A: c  @1 \8 ]. \( B0 C            {" j* j6 t% A: D# V, i
                int type, subType;
& c6 J- B4 c9 m# R/ x7 J
. N3 _' K" M' A0 K$ j. U' f0 }                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);
) p7 I" c5 O7 ~2 v( X4 l
1 `, o, _% k, Y* E! o2 ]* R                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));# k/ H/ }" x8 t3 q3 X

) D2 z" f4 v0 [( F( R                if (type == UFConstants.UF_machining_operation_type)
$ X( R" b, g1 [3 c% V                {
# p2 [* g3 i) g- N                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));
* e8 c" _) o8 e  N3 K) _/ S$ w% o- O1 d
                    GetOperationSubType(operation);2 ~" p6 {! g: I/ O. t/ @
/ t% f  R( X5 A' d( i
                } /* if type */
+ P% g- y( O: d$ q, e! [0 e' C5 ~* ]            } /* for int i = 0 */
9 I0 l+ p/ M! N, O$ C        }
( D- R0 A0 _) i* j( S$ n2 s; h        caTCh (NXOpen.NXException ex)( ]7 Y0 ^" q8 o( `
        {- d2 M$ `! Z8 l) j5 t
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
7 {& H7 b0 \+ ]2 A6 m8 X$ I        }
( ~5 K5 a" U3 R/ L) B    }
# v5 N' w$ z/ L: L5 P0 t
* D" M4 L: p& y6 w/ D
0 V1 ]. ~( I6 B& w) ~    /// <summary>
/ K- \9 \+ R1 T1 m    /// Retrieve the Operation Subtype
0 l* j! ~. p# x& t1 X    /// </summary>
5 B- C5 S, T9 O# w1 D: e. [& G& Z3 H    /// <param name="operation">The Operation to Query</param>
: K7 U) ^: a% z* }  H& ?    /// <returns>Return the Subtype Operation</returns>- B, E: j$ C$ b6 H* _
    public static int GetOperationSubType(NXOpen.CAM.Operation operation)
( G; [6 x) ?3 H' L' d( m4 C    {
! }0 i- k, D6 N6 h: X        if (operation == null) return -1;4 N1 S$ K& F; `/ y# m! F
0 b% t0 N0 g) @% e9 }- R8 S
        int type = 0;
2 n/ D- k- R2 K! B+ A8 p9 J        int subType = 0;2 l# [8 D/ j6 K; E! s7 }

' E9 h' u! n; J  A* a- `        try# ?9 N* \* w3 t2 _1 X7 V
        {8 M% w/ t% P) f* [; l+ Q$ u7 _3 a: I
            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);, W, N! d7 B3 p' ?
            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());9 B6 @' ^! a3 C+ [

8 n) ^/ H2 \% m' J9 P  h            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
9 o7 M9 Q2 U5 I7 @. v            switch (subType)6 B* j0 T8 H7 y5 c9 E' ^
            {
6 Q* [, w; w4 ^: w* Z                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */" }0 y# y# R, t* Y6 ^9 R
                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
' Z+ h5 W3 w/ Z' y                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */
  V/ {5 C4 N; F6 p! q' K                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */9 g5 d7 A# k/ {1 T* O5 @9 [
                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */+ n; u# e+ @% b0 h3 m0 F' z' l
                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */6 F" {4 E$ s3 S% P/ n' H
                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */4 g+ `. O- ?" b' E6 |% O2 ?4 _% G
                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */2 Z9 ?: R* y( }# Y/ T; u
                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */
& `3 }" W! U" G( X0 u                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */
* k* X: L/ d" P4 Q. Z                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */, I' q  m1 o' H  W) q8 V
                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
) @6 ]& t3 H7 v# t3 O) T7 N                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */
0 ?4 f& ^8 W% i3 Z9 W5 S                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */
: I; R% W6 U( K& Q9 e: v                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */3 a) o8 a% I" _! p
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */
$ `  }* W/ `4 ]1 M3 H8 I3 \/ d# p                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
- p& i( v0 \. w* b0 t7 N                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */( k2 M& B" B: h4 r5 N* T
                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */3 D6 ]) f2 p) X' F/ A
                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */9 ?- L4 b, M/ M8 p
                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */; L, s) d' d8 W7 O, Z* r* K

2 ]% v' x" ]4 I$ t                case UFConstants.UF_insp_tolerance_subtype:8 J+ C3 Z0 ]4 r+ }: \
                case UFConstants.UF_insp_path_subtype:
9 i8 G" U! R- z) C                case UFConstants.UF_insp_output_subtype:1 V  ?. H: U3 m. z; \; ]
                case UFConstants.UF_insp_misc_subtype:$ w6 o7 K* B; p! N4 J$ m0 |0 e
                case UFConstants.UF_insp_align_subtype:, N7 @, J* R9 j
                case UFConstants.UF_insp_sensor_subtype:
& Y  X% B* g2 L2 I( h4 [  O4 y8 h. K                case UFConstants.UF_insp_construct_subtype:5 @# r7 Y/ @; }. m6 E* H
                case UFConstants.UF_insp_bounding_feature_subtype:, A) `& y! R( l3 @  G
                case UFConstants.UF_insp_feature_subtype:: ]% F( u) v, V, P0 t* H. C, s
3 q1 W9 I% ]( {8 j, F0 B0 t
                case UFConstants.UF_mach_canned_cycle_subtype:  @2 z3 W5 Z6 d7 U4 q

6 q+ {! L% \+ U                case UFConstants.UF_mach_laser_teachmode_subtype:- R/ _5 @) |2 d6 h$ M3 x
) ~6 C1 G% K1 q! ~; p
                case UFConstants.UF_mach_turn_roUGh_subtype:( m  k3 H* ~: o5 ?. k( Z' @
                case UFConstants.UF_mach_turn_finish_subtype:
' t3 s- X$ M; x/ u" b1 S( b* r                case UFConstants.UF_mach_turn_teachmode_subtype:0 }5 }7 W* m$ W! z
                case UFConstants.UF_mach_turn_thread_subtype:2 D& b( j) n) T( s
                case UFConstants.UF_mach_turn_cdrill_subtype:6 [3 C- v1 I( b/ \/ E' k( R
                case UFConstants.UF_mach_turn_auxiliary_subtype:$ ?+ Z0 U# {  b: d; [+ j9 v
                case UFConstants.UF_mach_turn_probing_subtype:
4 y0 f0 D9 W/ W$ l                case UFConstants.UF_mach_turn_tool_probing_subtype:
! s* j9 a* e: v0 F3 m# h9 O                case UFConstants.UF_mach_lathe_mc_subtype:
, {6 w5 ~5 X+ O* R  L1 c$ |6 H                case UFConstants.UF_mach_lathe_ud_subtype:  G: }- C/ O; \$ B8 ?6 [- B  z  w9 ~
& w: p& ]: e2 u5 n/ n' K0 u
                case UFConstants.UF_mach_wedm_subtype:5 C7 X8 h3 k; F
                case UFConstants.UF_mach_wedm_mc_subtype:1 W/ F2 B& f5 o% m, K5 @& c
                case UFConstants.UF_mach_wedm_ud_subtype:
2 @5 T1 `. I6 P5 a8 u+ D/ W                case UFConstants.UF_mach_mass_edit_subtype:/ q; H( L3 Z7 s# b6 q
                    break;
) P3 _$ c" X8 Q8 [. }* u3 J) {) U4 A0 ~+ [
                default:2 ]) y6 H8 W3 H" C# ~9 `; z& m3 b3 E
                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");# M+ M3 D2 \; X- a# j- C+ i
                    break;6 P8 i% y" i" o& D
            }
2 U( [  R) _8 H( I& S1 C        }1 n3 W2 @8 Z2 d" N1 r
        catch (NXException nXException)
9 o2 u/ ^: \  a. e: A( ]' j        {
: j. j. ?. f* Y+ e0 Y            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());$ Z. e* g2 d( c+ a# z
            subType = -1;
# J, I; n/ x3 R+ p        }
3 z+ {( k, U( ^$ X% j$ n6 a! f; H) f. o
        return subType;# |' f7 {. f8 K$ w; n) G1 @
    }
  `' O0 A4 \+ Z" i
/ B: k9 H) K. l8 |- [0 o) A    /// <summary>  f- N" V5 \: Z
    /// Display System Information! g- _: T4 A9 n0 P7 `
    /// </summary>5 H7 r2 x/ `0 a$ [) T! N( C2 [
    /// <param name="uFPart">The UFPart to Query</param>. V5 M7 [% Q2 O# y5 x
    /// <param name="workPart">The Work Part to Query</param>% r9 ^- b+ X( S& ]# J$ L! v
    static void SystemInfo(UFPart uFPart, Part workPart)) Y2 K. i. b! t9 E- ]1 j
    {
: D' @: @# }% r& X5 t. n        try
8 A2 E% M3 z( J3 @        {
& Z+ i9 Z0 U( ^            SystemInfo sysInfo = default(SystemInfo);
. X# P5 q, ^" ~! A: S            theUfSession.UF.AskSystemInfo(out sysInfo);6 X. D& V# F. G- B
& f: F' C% S$ e+ d
            string partName = string.Empty;
/ x  i& z3 A- o) z/ g! Y            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);8 ~  p2 |- C2 u3 ^+ d
+ g3 C& |4 h! n
            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();8 ~1 e! b0 H- C" _# s! X, s
            theSession.ListingWindow.WriteFullline("============================================================");1 ^$ U6 w3 \+ @' L
            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());% ^. ~4 t/ \8 B, i+ F+ I
            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());
4 }+ J/ \/ h  K7 o            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);! ^5 ^( _* K! ?! Y
            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());
  j5 c# K% b. b) S- J1 Z: o7 V6 q            theSession.ListingWindow.WriteFullline("============================================================\n\n");& V" G+ T5 V6 f
        }
, J6 c9 G' k% |1 _        catch (NXException nXException)+ ^4 e& r* Z  S$ I; o- d
        {
7 p5 Q; X" K8 H# ?: {            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());
1 @7 i( v9 {0 m7 [$ C) T3 ~        }9 t1 Q2 y& Q' S+ z& _& a( U( f' @
    }& j/ m  T! O  w9 b

8 t8 x7 H0 {: N4 ?- q    /// <summary>1 f; X( I) [2 ~+ I
    /// Unload the Current Image+ |1 z: ~0 K( ~/ Q
    /// </summary>
2 y# n  s+ t7 H" s% Q4 `    /// <param name="arg">String Send by NX</param>
6 K! v# b, [7 z0 O    /// <returns>Unload Integer</returns>* U, O* b3 N, X3 V
    public static int GetUnloadOption(string arg). q) ^! h( G, C& P( b: i
    {
1 ?4 E9 ~3 k, u$ }        /* Unloads the Image Immediately */
5 p' i8 M5 c8 _7 e1 c1 P        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);! A8 N  u4 F+ ]) I- m

: b% t( R5 N. W7 H        /* Unloads the Image Explicitly, via an Unload Dialog */9 p4 Y' |" y! W  u+ K8 a/ O- Q
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
; ^# R2 J* B/ j$ e, q2 J
( t6 p- p+ G& }  l2 Y        /* Unloads the Image when the NX Session Terminates */
/ _/ x7 C* x' o4 i6 z, E0 \$ c! l        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);$ |+ M0 Z# }4 S8 X/ I% a
    }4 @  G0 y3 K" c* C/ `0 r
}[/mw_shl_code]
( F# O! T1 n, w: g) V8 Z* _! g, ~. \+ a
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了