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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了0 i9 y1 g' Q3 `) w" z
3 Q; Q6 J8 X! }1 U
[mw_shl_code=csharp,true]using System;% K# ]7 N. Y. \* ?
using NXOpen;
6 d3 h* U2 I: K9 W& Busing NXOpen.UF;
( A$ }  x) J% u0 R0 y9 h( gusing NXOpen.Utilities;
$ Z8 Q% E& P! \) f: Busing NXOpen.CAM;8 {9 N& A; l! C" i) T% D
using NXOpen.Assemblies;
) n# P+ x$ {7 J, a
: K. ^$ N# i; Z# zstatic class GetSubTypeToOperation4 r2 [& M7 N- [# ~3 c
{. d( a9 _$ ?& A# Q
    static Session theSession;0 f5 Y& h" i3 w4 t% B+ f3 ?+ _; [. g
    static UFSession theUfSession;
! _7 ^* g$ D- I! p( }: y/ Y) Q    private static UI theUI;
6 h  b+ d2 n7 ^4 ?9 j& k
/ b" i7 v5 ]. q5 E# X8 I2 k0 _; s0 W& P    public static void Main(), p, w; Q0 l( t3 l
    {% a3 [5 a3 t: C2 R" X$ A+ Y5 O  o
        theSession = Session.GetSession();
5 e# }8 q$ v! j6 V1 ^) r8 }0 x        theUfSession = UFSession.GetUFSession();
+ Z  U7 b: g# ], V# `        Part workPart = theSession.Parts.Work;3 p# ~+ c8 A0 _, b: N3 z2 `/ c
        theUI = UI.GetUI();) z$ R+ P& n" F

% i7 F( C# k& `2 m9 `: e# z        if (workPart == null)) v, ^8 c6 `' z  s
        {% ~- W! e" k; e; _& c* k- e2 Y; l4 l- P
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");
. v% ^% F* i* j+ T" C# v0 h6 V; y            return;
: d& [/ P, `& b; [! Z6 a- \        }
$ d0 }0 ^# R; n: x, f+ d! R& |/ O) I. p1 `; U0 }
        theSession.EnableRedo(false);* K0 M+ V& P' n; l3 r
) F, w9 e7 T( `) N9 p
        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
$ N4 ]) L4 I, k  f" n* |
6 G5 x4 E9 X; X' b5 g        SystemInfo(theUfSession.Part, workPart);3 @3 A1 W; T( g! k. V
. u6 |9 q( X8 Z7 D. w) q
        try
% {5 V1 A# p$ Z! k) x; \! i        {
. ~6 O0 r& d' p            Tag[] operTag;
% `0 f; }3 I% q8 t            Tag setupTag = Tag.Null;; [9 l: b3 C+ r8 a( H* @5 m

# B6 j. \8 S! E: V# M            int countObject = 0;9 m  s$ Z$ C, o$ O$ T

. x5 V' m: i- j( Y            theUfSession.Cam.InitSession();
( K0 l2 x3 q) d" c/ x4 E            theUfSession.Setup.AskSetup(out setupTag);
7 R! k& F8 I# k9 F; k& h( m& o/ |0 I1 f" w6 y: ~" z% F
            if (setupTag == Tag.Null)
" @6 Q# c! y+ C; {$ {: n            {0 M, H  i3 Z4 G: j8 C8 M
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");
0 ?" `5 M! x% `9 p; d( b/ C( o                return;  L) G" w$ }3 c  ~5 _' F3 N% v" S
            }0 _0 V9 H* x! P" o# j5 ?7 A
3 |/ E6 [& ?- x4 e$ A
            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);! W. j6 H4 y# H0 x% R5 m

- W( P8 w1 U' ^! U            if (countObject == 0)" r: n4 n5 Z8 U: o" k
            {: s' ^8 Y( p) ]( a1 N  @
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");
3 |# C6 m+ Z# P$ i/ P; ]                return;* {3 O5 b' m; t- ^" ^- m+ b  H
            }$ T: n- N3 u; p7 I6 R- ]$ C

( N  ]3 w& i/ @  G$ V/ B1 G6 N0 a            for (int i = 0; i < countObject; i++)
1 W% h- P' t6 _6 A            {  o2 ^3 D. m/ I6 K+ a
                int type, subType;, @* ?7 x$ t0 r: a% y

/ h9 k* s# _0 W! A3 b, H, f4 Z                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);0 s$ l7 _8 M( U" D, i) p

: E. y& q7 L1 G( N& }% D- K1 p                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));  ~$ S$ {' r! S
5 U/ s5 B; m" _' n. D
                if (type == UFConstants.UF_machining_operation_type)5 A3 J0 z; s( p0 ?9 e3 G3 E
                {2 s( Y) Y4 b( \; O; D1 R( ~
                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));
3 v! `5 I% U( C. g4 l3 k" r: ?' V0 z7 z
                    GetOperationSubType(operation);2 l$ Q/ t' S7 T" g3 E

8 K) O, Y/ y  k' P5 }- w" a% j  Q                } /* if type */
4 n4 E& n1 x* ?: A7 g4 o3 L            } /* for int i = 0 */( K8 V0 D  u$ e. W
        }( [( [9 U# x) f- G9 y4 V
        caTCh (NXOpen.NXException ex)3 m- ^9 `  [0 N3 `/ a/ }
        {
6 U5 j. l) a$ o7 h            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);' d( m. h8 _0 U
        }- ~' a2 I% ?2 D% O7 z* b
    }3 t% Y; s& c# l  @4 _  Y, T  Q$ j0 M
% r( Q! ~5 {/ y' e* a$ P

& w: u' H. e* e    /// <summary>) N' x$ `5 w7 F  ~. N
    /// Retrieve the Operation Subtype
2 f* m1 N1 `1 w+ q0 q: h    /// </summary>
4 h# D2 H, J! R& m2 F+ ?    /// <param name="operation">The Operation to Query</param>! \8 a5 V; s% l  r  y  P
    /// <returns>Return the Subtype Operation</returns>: ]: E' ~  p3 e6 `  |+ M1 ]& p' P
    public static int GetOperationSubType(NXOpen.CAM.Operation operation)
( z' U3 z1 v& Y# `1 x    {8 Y2 i2 l9 a  Y3 A- x- I' O
        if (operation == null) return -1;: B3 t# ~% c& {6 F# M+ @

0 d+ L! @7 l4 }        int type = 0;
4 C  L% ?; w. ^) ?; z3 Z        int subType = 0;
# N1 ?( S5 f6 R) f' l. G: E
! d1 }- m' K; K% Q% d1 N        try* p2 W8 P- D# Q" t% Z5 E3 s
        {
3 W; p( p9 O1 T: g# {            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);  h0 Z& S# O7 S3 v, Q8 l" d
            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());& s8 j; M1 Y7 \/ A9 x; Y
; h" m" J; D) a1 Z
            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/$ O5 i% W+ s) {3 s5 {
            switch (subType)- }! B" X7 z) D
            {
7 t! G9 B1 {7 V. s) H                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */- W& m* s3 f. E. h; y* T/ ~
                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */' M1 j/ G! `% Q1 Q* m! f, z
                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */- ^8 ?+ o. O" I" j2 e6 @
                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */
1 [2 ?& b2 \$ Q, O* K7 D& }                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */& G0 h7 _( _9 I
                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */
  _0 i$ g  W3 Q: B! Q/ |3 u) ]                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */& R6 F: Z# j/ k; C
                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
7 g5 B% a& y, _! r                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */" r# c0 _* p) O( y
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */+ y( L$ ?! S6 O" U1 N/ i7 d
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */
/ @7 c4 o* I0 q! n0 Z1 b5 I# u                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
; ?; V% u3 |& k4 C- b. h7 ~                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */
0 k1 d4 j0 s  ?, t) v- N7 ]+ `                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */
0 H. d% a. o& ?; _                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */& `! `2 G: ]" M7 A& ~
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */
7 P: `9 P1 z* G& o( K                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
- P* A, y' F+ _7 |5 ^, j                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */1 h% i  F/ a' {" ~
                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */1 v. {- I0 ?7 T# I- L+ ~
                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */. h; \. O3 j; {2 W" D; O4 a  |/ r
                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */
( a. r; `( _- c4 x: R
, J) |; i% A. D& P  v- d                case UFConstants.UF_insp_tolerance_subtype:' E$ D+ j* Z4 ?# b" r9 Q: z
                case UFConstants.UF_insp_path_subtype:+ q1 G1 z, c3 ?- J4 W
                case UFConstants.UF_insp_output_subtype:
* f  r+ L, R% e  h. t                case UFConstants.UF_insp_misc_subtype:
+ H; `) G$ d8 A; o% m                case UFConstants.UF_insp_align_subtype:
7 W+ ?5 L3 O! b- ?8 H                case UFConstants.UF_insp_sensor_subtype:
3 n) b# `9 A* a9 j# m; Q! u                case UFConstants.UF_insp_construct_subtype:! a! v5 d8 {4 |  @
                case UFConstants.UF_insp_bounding_feature_subtype:  s8 G, T1 w( b; G
                case UFConstants.UF_insp_feature_subtype:" v5 Q5 q+ \; l! J- ~# ]) s3 j$ x

; S- b9 ?8 h. O# m: q0 R                case UFConstants.UF_mach_canned_cycle_subtype:
7 N' n( Y# U1 s
9 r+ k. w/ g2 y                case UFConstants.UF_mach_laser_teachmode_subtype:
2 h3 W  A! R: }, t' h" ^: F  N7 u8 q/ n1 W* E
                case UFConstants.UF_mach_turn_roUGh_subtype:
( j' C& U, `+ w/ c, L" F9 v                case UFConstants.UF_mach_turn_finish_subtype:1 ~% G6 k/ G/ u7 Y% ]
                case UFConstants.UF_mach_turn_teachmode_subtype:
9 F5 g; Q: [, W& o                case UFConstants.UF_mach_turn_thread_subtype:
- K+ q0 p; y- I% y: c                case UFConstants.UF_mach_turn_cdrill_subtype:
0 n% c5 ~1 o) j& X% L5 |                case UFConstants.UF_mach_turn_auxiliary_subtype:. H- ~0 o# E/ j% w9 g
                case UFConstants.UF_mach_turn_probing_subtype:& R4 \) G: ~( ]/ \9 M; k. ]# M+ u
                case UFConstants.UF_mach_turn_tool_probing_subtype:
. X- K' v7 ?+ b                case UFConstants.UF_mach_lathe_mc_subtype:. e# s+ L: ^/ z4 ^  J  x3 Z
                case UFConstants.UF_mach_lathe_ud_subtype:
6 v' j2 W) j. L1 _3 c
4 Q( d) t2 I: b' N6 b/ f' I                case UFConstants.UF_mach_wedm_subtype:
8 r0 `" @  N) C2 }                case UFConstants.UF_mach_wedm_mc_subtype:2 {) t6 u" S: I6 e
                case UFConstants.UF_mach_wedm_ud_subtype:
5 x% ?5 E, ?+ k/ m0 E                case UFConstants.UF_mach_mass_edit_subtype:3 n" ]" S6 N% {8 ^9 s' \* L
                    break;: i) I+ T! i; v2 N

7 s0 @) ~+ t. \3 @9 Y                default:
# @8 U8 c' F1 F                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");
) |8 z6 ^9 J# t: d2 S/ ^* [! a                    break;) \5 K) d) ~, [5 Y  ]2 {0 R0 L4 G  w
            }% A: }( a3 x/ B3 I; L  Z
        }* e# M' [3 z" _; T" ^, u! @/ B
        catch (NXException nXException): b. R7 y3 N, [1 Y$ l2 W
        {# E# e1 k7 K  L7 Y+ o0 n- W  X
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());
  F0 R* `" E- `+ l- ^" q  F4 R            subType = -1;) e1 V9 B. @" s5 A. I  O" _: H
        }2 [3 y, p, L3 t# U- n# K. A
5 G( K; Y* F3 V$ u# W" f- }
        return subType;
3 o, {- E2 o  F, p    }: H3 k. ]5 e0 `: i

$ R/ A7 }% r" m! o7 P+ T    /// <summary>
6 d5 ?: F! k: T. C# j, m6 D    /// Display System Information* q* U; v3 z8 X* S- Z2 U& I" e9 f' X! G6 m
    /// </summary>
; j8 f9 U6 C+ T5 H( w' K/ a    /// <param name="uFPart">The UFPart to Query</param>
1 i, m4 Y/ n0 h    /// <param name="workPart">The Work Part to Query</param>! u  E# N% j) D# L: ?$ ?. t
    static void SystemInfo(UFPart uFPart, Part workPart)+ z% N3 ~2 G0 Q0 N$ {; S  I0 q; i
    {7 y) l( O: u; u2 {, X0 z) ]1 S
        try  S0 K& y$ M" w$ g' e) f4 i* L
        {* x0 n( R; x/ t1 i6 Z* [. r; l% i, e
            SystemInfo sysInfo = default(SystemInfo);
2 s6 Y4 ^$ v. L$ y* p0 f            theUfSession.UF.AskSystemInfo(out sysInfo);
9 {, k" o+ D& `9 h8 w# J% p7 G1 ?! a% O$ p3 e' ~
            string partName = string.Empty;
9 P) R  n! Z- x1 G4 @$ B            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);0 f8 Q9 U# _, X9 a

; K$ o8 R+ q: r' [( J4 j' E- L            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();; J0 d: g) W. U  \7 V5 ?
            theSession.ListingWindow.WriteFullline("============================================================");
2 n+ c7 X6 I' L* o# D+ x            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());0 j5 a: f* U4 C& {
            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());
* Y8 `: C/ x+ Q: w/ N2 x- _: R            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);
$ F9 S7 s3 P9 n" @/ c) ~% P3 e            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());
, [+ @/ ?: ^% g1 s4 q' X2 ]8 B            theSession.ListingWindow.WriteFullline("============================================================\n\n");
# A" p$ W& A7 e4 i9 v        }
& F5 x' g( J% B( i        catch (NXException nXException)1 m) Y: d: d  T( L( l5 F
        {
5 R- b. f7 B- Z( l            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());
( R, A3 S, k; ]        }
4 W9 U. k2 e; j4 u: B. f+ o    }
: t  W/ j' K" `( l* f# z4 T% c! y0 E$ a/ w" y4 z/ Q+ i! l  c
    /// <summary>
, {0 j9 `. x; C  l    /// Unload the Current Image
5 C' g' p8 b1 x# k, c0 _% N6 W    /// </summary>  D: Y  R8 H# K6 _8 b
    /// <param name="arg">String Send by NX</param>1 a0 w' u7 X; G# ]% F6 O2 p* x8 `! v
    /// <returns>Unload Integer</returns>
2 Z& U2 l! w! k; J    public static int GetUnloadOption(string arg)" r6 e; N( r8 e) U  \6 _! P
    {" V4 h3 h! G# O8 _" `  s; I
        /* Unloads the Image Immediately */  I  A) T, Q0 q% W8 z: ?
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
! X7 d5 i8 H( U3 r& }! N7 o* c% |$ {; s& J
        /* Unloads the Image Explicitly, via an Unload Dialog */3 z% p3 l" \0 T# V5 i
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);; \" d; b, }' [; O( W; t) B- t
& U" Z8 x) K1 \$ I
        /* Unloads the Image when the NX Session Terminates */
+ f* j. _5 I+ H( h        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
5 a9 M- q2 w: ^4 V# j    }
1 A) H; v& _3 d+ d4 M# l}[/mw_shl_code]
. z) J/ m. `% D
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了