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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了
2 {( {' E" q2 p! c% F$ y# t' b; ~8 F
[mw_shl_code=csharp,true]using System;, k- F: ]; e- S( \% R
using NXOpen;  j& K/ Q* J1 H. [  c4 n
using NXOpen.UF;% ^" i7 R, Q4 U& e: o0 Q
using NXOpen.Utilities;+ p" [, k. {' A
using NXOpen.CAM;) W& y) i: n' l" B
using NXOpen.Assemblies;
5 A5 g5 ~. o/ w7 N& ]. ?
6 r1 b) C/ L: {; Q5 T0 }) f* Ostatic class GetSubTypeToOperation
' o& N) z: J3 `% O# n5 o. n( V9 j{- r" }' A! N' Q$ S' Q8 }' i) R
    static Session theSession;
& Y" D& T% y- C3 w) c  E& A    static UFSession theUfSession;
+ Z& Y5 ?. v9 d+ i3 d% J    private static UI theUI;% X* j. d& }& ?% [9 C* m
9 p  E, n: i: ^+ ^( V$ P- {
    public static void Main()  n5 @1 G3 C7 j  }9 b
    {! A, ?0 l; Y% @7 `0 f1 T9 B
        theSession = Session.GetSession();
! T( u8 ~  `# G0 T$ ]        theUfSession = UFSession.GetUFSession();
5 k9 W  K, r9 D7 ^$ c; L5 J        Part workPart = theSession.Parts.Work;6 Y6 l; {+ w/ h% n9 h
        theUI = UI.GetUI();) W& j9 }* d6 \; T7 t
9 x) L# m! c; u
        if (workPart == null)
% i2 I& I4 F* f1 s, c/ ^7 y        {/ i+ T  a. _8 |- [+ r' o
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");$ Z9 T7 D3 x/ f
            return;/ ^. ]; C" k& o1 H! k% L
        }
3 w) n/ V* q! x7 g/ `  [
7 f2 Z  ]5 W* X+ p4 Z) q) _1 K* e        theSession.EnableRedo(false);
# c- i* b8 l" o# K% v2 \/ h+ k! }
        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();. t6 K5 g" G7 Z0 u- U: n( f
0 h1 |0 Z0 j% W3 b# P) _& w3 r  P
        SystemInfo(theUfSession.Part, workPart);
- z$ B) a6 F- Z! V, h, J4 Y. ]
* {$ L5 U4 H5 i! f7 @        try
5 i' n5 P7 h7 N" G/ Q        {; U; t% g( ~1 T- O8 k# t
            Tag[] operTag;
% P3 _) L$ S5 ^4 w, g            Tag setupTag = Tag.Null;' m5 }; g% ]* L! j

& `* Q* C. }: w$ O            int countObject = 0;" K/ K6 @$ c; f: {) D1 Q1 ?6 H4 Q* ?9 r- D

( ~8 n2 L! ~+ V5 T: z* c            theUfSession.Cam.InitSession();0 g5 e' z- |9 a3 w1 X" b& O
            theUfSession.Setup.AskSetup(out setupTag);0 e3 K9 I4 Q: s
" W. i8 I$ `8 }( V
            if (setupTag == Tag.Null)$ B) v& u0 B% q
            {
6 T  w! E* e6 B+ D" h                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");
# y. D" G5 g$ X: x% y: J                return;! j* W% x* `7 _, P# F2 A, K$ n1 Q
            }
# t+ @6 }% n0 R2 h% e* K) ?4 Z/ [7 F: p8 F7 g' X8 p1 S
            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);
" a" Q8 x5 t1 z$ v+ {, U; Y/ i  q! D0 Q: A1 @% Q
            if (countObject == 0)" X) G% U) s: ^2 j3 l/ A9 ~
            {
- s3 R  F( F' v1 e* p# |4 t                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");7 N% ~, }1 M4 y4 t
                return;2 m; A) W2 P, W- G
            }& {, p. i$ o* q

$ Z% X3 i) p; R9 |* ?, Z5 l/ r! H% d# s            for (int i = 0; i < countObject; i++)
% o% q6 ^) T( O% L: ]6 K, R            {
* a2 z6 U* b% a2 a+ W                int type, subType;  n6 Z2 Y" _# z$ d
; p3 U' q3 Z* Y! a: b3 H5 [: v
                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);0 N- t% L0 p! p
2 h2 W# O+ D+ i1 I- ?8 T) Q
                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));
9 a' d9 M( z& {& ?" V5 {8 D- d' f/ T& T
                if (type == UFConstants.UF_machining_operation_type)# s& p# w1 B; U3 x" ^( E0 }" U- U) J
                {* T9 h, W8 C" E5 n9 ~! [- ?" D4 k0 I
                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));2 U+ |1 a5 u* X  m

8 x/ T0 u5 c+ V' d; A                    GetOperationSubType(operation);
$ C% ]% Q2 R, r5 r9 F& B+ F5 |6 s" g% x1 }) p2 `7 @  c) f9 u
                } /* if type */
9 [4 J2 g2 q  F: k            } /* for int i = 0 */% i% ^% O, u3 D" I( D" |
        }  O$ P/ x% {* g
        caTCh (NXOpen.NXException ex)
) I" b( K1 I9 C" o# i  \) S* t        {
7 a1 j  F1 Y- U! e            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
/ d0 D8 s: c5 H7 i8 z* N: _        }
+ j! v* B: {. Q" `  U3 X4 d  z    }# o1 i+ U! _6 v  l

* E8 U1 D/ _/ v) E  \5 E' S% F6 u( \+ {# |
    /// <summary>
1 i& _' G: m! {) W    /// Retrieve the Operation Subtype
" t6 e- k2 }$ z; R/ G6 p1 m$ u    /// </summary>
9 h0 }7 n0 K4 E5 @5 C7 ?% t6 E    /// <param name="operation">The Operation to Query</param>: s" z8 d" ]: {3 ?
    /// <returns>Return the Subtype Operation</returns>3 f' K$ I+ x2 \3 B; T
    public static int GetOperationSubType(NXOpen.CAM.Operation operation): ?3 m, ]# J, V  n1 J* j
    {
: `- g* l7 _& s  f        if (operation == null) return -1;
! x- [  a  W6 m& y- P8 G
1 |! `8 O$ D4 Z# Q1 \* O        int type = 0;3 c6 Y  t) E3 L( b# @& t+ g% ]
        int subType = 0;
- g3 [' g8 X  u; P$ @
* d; `- x( s; V8 u- m        try- [" Z! v' i0 L6 u' {
        {
' q! R" N( U# X1 `5 W1 V3 i            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);9 i4 M. H0 U' B6 B
            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());/ f' n5 B1 e: }- m5 E. X

4 N; x; M2 W, R$ u, ]5 d/ y            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
' B7 z+ ?. q; ?            switch (subType)
5 M4 m( z. o- u/ b            {
/ J$ N6 K1 z- L) E8 w                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */. f4 S! e6 C* S# b4 g. d- d" _! H1 B4 }
                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
8 }: k0 ~( P: i( I                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */
: E" Y6 p: y, f$ P$ v) U6 A                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */+ r; m( n- {5 W% g0 M
                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */: g0 T! g; v/ Y
                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */
; p7 a+ u( o- U: k' [                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */
! N  g; A/ `4 ^: s                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */3 ]: I, r& w5 C( O4 @. K
                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */$ A( C. D( ~6 _' N% Q7 T% h
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */
# X4 ]3 [/ R2 Y                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */
0 _% X6 W. u1 b# l+ y; N/ ?4 ~                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
4 H* R, W- T6 k4 x) z! b                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */$ o7 k3 C: _$ U7 p  v
                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */6 P7 W) F5 @8 P
                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */) B- u& x- k: w: ~. y% O
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */8 ?7 K) W4 i7 t! z. ]; P7 [
                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */5 U# c1 O* S, f9 x8 Y0 j5 U
                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */
9 F9 T9 B5 ?1 Q% }  @* D                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */% n7 D2 Y3 i4 Y$ ?9 r& _
                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */
7 j' d6 ]8 \) ~% B+ F                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */3 |, c9 ^  `/ ]+ ^

6 d! H% k7 N7 B; |5 r. v                case UFConstants.UF_insp_tolerance_subtype:% _1 v; ^- C7 q" u' W( F
                case UFConstants.UF_insp_path_subtype:
/ _7 p5 {8 x6 u                case UFConstants.UF_insp_output_subtype:1 D) C- }+ ]' \9 Y9 J0 ^! q
                case UFConstants.UF_insp_misc_subtype:
: n6 ]6 R( S1 ]* e                case UFConstants.UF_insp_align_subtype:  e5 t# v7 w" S* a" {  L
                case UFConstants.UF_insp_sensor_subtype:4 w# S/ i2 ]# y& F3 n+ U5 Q
                case UFConstants.UF_insp_construct_subtype:. ^+ T7 `/ z4 t+ @6 m$ Q9 j
                case UFConstants.UF_insp_bounding_feature_subtype:
0 a% D- T8 e; M) ^! G1 p# d                case UFConstants.UF_insp_feature_subtype:
" j$ A! |* y" c2 Y) v# a
3 k3 H7 K  ]: W4 z* ^! `- A9 @+ b                case UFConstants.UF_mach_canned_cycle_subtype:
; p! [8 B! I$ Z( C  Y  n8 b, ?5 {: k( f. w# N2 G/ n
                case UFConstants.UF_mach_laser_teachmode_subtype:
% k8 ^0 c$ C7 X0 v+ I. Z! s! h3 o3 t& Z; _" F
                case UFConstants.UF_mach_turn_roUGh_subtype:
& j6 X' ?! [& I  K5 T0 I9 w# @                case UFConstants.UF_mach_turn_finish_subtype:
* a/ {* o! E4 g$ G6 r* c2 {                case UFConstants.UF_mach_turn_teachmode_subtype:. e4 q% ?1 f( [3 J4 t; a* w
                case UFConstants.UF_mach_turn_thread_subtype:
- q& U! G) V; t2 ~                case UFConstants.UF_mach_turn_cdrill_subtype:/ U8 C8 i7 s# N# J
                case UFConstants.UF_mach_turn_auxiliary_subtype:; u# i0 m" o7 G! F+ z3 H3 C' S
                case UFConstants.UF_mach_turn_probing_subtype:, s6 C2 C; S4 Y3 e8 [
                case UFConstants.UF_mach_turn_tool_probing_subtype:
' q6 s) ^2 T3 b5 ?3 n5 L                case UFConstants.UF_mach_lathe_mc_subtype:
9 |9 I: Z' h1 G5 |1 m4 Q# N$ d                case UFConstants.UF_mach_lathe_ud_subtype:
/ S/ B% f: r7 x+ C& a4 I7 j
5 [7 ^! ?' E2 _6 i1 U                case UFConstants.UF_mach_wedm_subtype:
' k. i$ Y6 n; K4 G: W! B                case UFConstants.UF_mach_wedm_mc_subtype:( Z0 p5 ?# M, o& |
                case UFConstants.UF_mach_wedm_ud_subtype:5 E, X( s6 P+ v
                case UFConstants.UF_mach_mass_edit_subtype:
, y8 f! W( a0 G8 Z- N/ z/ }; P                    break;' L. g' J/ i, F7 i% L

! Z! K! p( J: c8 r+ S% R4 l8 B                default:
# ]! e3 r. c% f2 g, A7 u                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");6 X7 D  q# m2 {8 [" X: y/ m
                    break;
% M8 n) k; Z3 ^            }
# ]7 j& K: a0 [3 M& D2 k8 L4 W        }' U# v% C% {* j
        catch (NXException nXException)
) ^$ a7 g7 {4 j+ L$ f' V        {
9 f" ^4 T  [# x- A4 \! o( w5 c            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());0 T, t* @$ ?; u- a' C/ h
            subType = -1;" j; D/ i* ?* Y& z, H
        }
. d$ F0 e+ d$ C. E2 N* a
9 e; L" v5 Z2 ^5 L/ q5 S        return subType;
; }2 v, B; Z) L* J* }) [" s    }& x% Q; E5 h9 E9 S- q

2 @+ d& J1 ?0 q7 @    /// <summary>
1 g8 u; c: K- T. K    /// Display System Information; J! a2 M) V/ `+ f' ~
    /// </summary>
* w% R' i( {! V- B# u    /// <param name="uFPart">The UFPart to Query</param>
# q. s- q+ M/ Q% C    /// <param name="workPart">The Work Part to Query</param>/ X# E0 Q1 ~( Q" D
    static void SystemInfo(UFPart uFPart, Part workPart)! o; @1 n$ x" }8 R
    {
! s. {# b( E& R( T# [9 q% F  ~        try) y) S4 Y% q$ [5 U7 ^
        {( m2 @( c2 X( _& E1 G* T/ k
            SystemInfo sysInfo = default(SystemInfo);
( E9 k7 S( P0 F" X6 b2 @            theUfSession.UF.AskSystemInfo(out sysInfo);
6 }) p6 |- r: K1 z; _. X' V9 m) [/ O/ F
            string partName = string.Empty;
% P. Q0 [. a6 u1 ^- J            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);6 s7 ?5 H7 e/ P2 o( B0 r6 D

7 A8 `3 Z8 [+ ~! W            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();7 I. v' T% }3 O9 f* J" b
            theSession.ListingWindow.WriteFullline("============================================================");
( p; ~; F" Q9 m' x3 t4 @8 t+ F            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());
' W' M6 o# L3 K5 }' n' t& I            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());4 p1 T2 g& S3 e0 u0 A# b
            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);
* c* }+ c1 M, S  L& w, P            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());' d1 L8 G' \9 D4 B
            theSession.ListingWindow.WriteFullline("============================================================\n\n");
& c5 h& f% x+ N* a. }& i4 V        }8 d, k& C) G$ s- M; v) S  Z
        catch (NXException nXException)
9 p( m& H2 s6 r% n: x7 I        {+ C: n# s0 u- W( H* |6 O
            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());2 Z4 o8 Q4 j9 \6 M
        }/ O$ W7 B+ O, v2 D
    }8 D* v7 z4 y$ l
  G7 [5 G* @/ }4 C' K
    /// <summary>
. q& y" e9 @8 _: X. A+ |    /// Unload the Current Image
. v# l  l+ n" r3 m' i    /// </summary>0 M# ?& _* \0 M- y
    /// <param name="arg">String Send by NX</param>
( \) ]0 C( K6 l$ D    /// <returns>Unload Integer</returns>: E& b2 u! `- h2 M0 j- q
    public static int GetUnloadOption(string arg)
8 {: {/ L2 n. a- A! W9 h# k* S    {
5 n, i  T: j" f# ]+ v        /* Unloads the Image Immediately */3 B! _2 p8 G6 o
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
% E0 Q4 U5 X) v- Z2 e0 L( y/ w
( H& @& {" b9 v        /* Unloads the Image Explicitly, via an Unload Dialog */
3 X, M! ?, c  n" E% F: y        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
6 U% |0 m" @) G( {. i7 M4 {' O
$ n  R4 u  P5 A0 H/ U( {        /* Unloads the Image when the NX Session Terminates */( i# C/ G  W* P7 ]/ S& a9 j
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);, j! X5 n& t& Q( ^; B9 Z
    }! V; P9 }0 d( ~# `
}[/mw_shl_code]
6 v, x, |2 {( S( Z% d  c8 g1 \
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了