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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了
4 b0 w% S4 d: X/ ~6 y" U) \  |) x" ]) ^6 q
[mw_shl_code=csharp,true]using System;
7 V9 _' h! l0 k, Z  jusing NXOpen;
( n: ]2 e; c/ e6 ?- Cusing NXOpen.UF;
& H* ~" K0 @- X/ X# Gusing NXOpen.Utilities;# y. r, S" V" P. F/ O/ B
using NXOpen.CAM;5 n' P9 m; P  u, w1 @' m6 g; Y
using NXOpen.Assemblies;
/ P9 R% s2 u! D5 S
$ P8 z! l: v" F+ y" ostatic class GetSubTypeToOperation
+ {$ n' X/ [: W9 `{9 l5 u7 }* B; {9 [8 F
    static Session theSession;
6 N& P% C. P0 J5 b+ Q# |2 f) n. H    static UFSession theUfSession;
! r% ]' o, V8 s2 O' c    private static UI theUI;  ?; W! }. K3 m3 _8 K  \/ y/ B7 k

; `* m/ P7 T7 Z6 t: z$ L- s& {  _    public static void Main()3 @  }+ m/ T3 }& s
    {
; x- O! C8 k3 A' t, C! I        theSession = Session.GetSession();9 e5 X5 H$ Z2 [" F& p+ _; N
        theUfSession = UFSession.GetUFSession();
7 h. o8 @8 x7 _7 N" |( U        Part workPart = theSession.Parts.Work;" w, S) B) V/ s! F9 l$ s
        theUI = UI.GetUI();
1 a" T; d/ ?. n5 R8 m1 h+ L
+ t" K2 S8 N& m$ b; U        if (workPart == null)
' ~3 c* g& P2 u4 m- p        {
- }2 B& f: ~: J) g' c' ]; y            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");
5 M! n. y# p% h4 @            return;% U; z- @$ j6 }: V
        }
' Q& e) ?, z$ _1 W( C' X0 X" D2 K& `6 o6 `+ G0 c4 F1 z
        theSession.EnableRedo(false);: t7 b$ w( y1 X' T

4 |7 f3 ~& z( a- E! ?# l! }        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();6 N9 Z( x4 h$ L3 v7 m4 |  g: u

6 n2 c+ x1 H( |0 y8 U4 p        SystemInfo(theUfSession.Part, workPart);3 h1 V4 G' v+ c. W3 K# \
. u6 y& h4 u& C3 b: E6 R
        try* i& i% E' F% r7 i. U
        {1 T( Q7 r: [+ J: V# ]4 h
            Tag[] operTag;
3 Z0 ^( y- ?3 i6 R6 a% P            Tag setupTag = Tag.Null;6 y/ C7 @5 M* H- [

! u5 Z4 z/ ]5 R) v9 N, Z! a            int countObject = 0;0 a5 \9 w/ |# V" x' l  U

5 J! H2 P6 k) B6 L            theUfSession.Cam.InitSession();
/ |" j3 s& k4 w; \. X; o* M            theUfSession.Setup.AskSetup(out setupTag);/ S3 I7 e% d% [
9 K$ _! G" g- a  L+ Q' W+ J
            if (setupTag == Tag.Null)* w, p' R2 r& d" V, O
            {# R" y! D" K1 M- g
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");& R4 W; [4 K1 A' l* k
                return;
6 P) y- O) [; U( D            }
+ W6 K1 J1 `9 i4 ]; E
& h; g/ Z: [4 J) s) S            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);
' ]0 }1 ^* [- G, r( p# E4 W9 l; k
5 o! M5 r: @& [0 U. K5 a            if (countObject == 0)
5 `8 j- H+ Z4 T6 T            {
7 Z: B% D  i. g8 y4 g                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");
! R# ]9 s6 Z( D$ s0 J# B: ]                return;- P; a) a( f& p7 o
            }
; r+ R1 A; Z& Q" W  C8 D; O
, l. b/ P' `0 O- f0 l! l& }            for (int i = 0; i < countObject; i++)
9 Q+ [& K; s2 Q9 S& X1 H- n- |5 z            {" j& \6 |7 J* p5 i: R* ^" {
                int type, subType;& q( ?4 {) Y6 N& d  q( s
# z1 v& ^/ T6 n* |9 R0 g0 o( i
                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);
3 J% H+ Y+ ]$ S
, e& M3 j2 d. e( o3 K+ j/ L; W1 A                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));9 |+ O( q! [  V" n( ?+ E
. p8 e# x1 \$ A# l. |
                if (type == UFConstants.UF_machining_operation_type)* w. o; N7 }# h, W2 M: |
                {" P+ k: r: j1 y4 O7 x7 |! c  R: j
                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));/ y+ c- ?# [) z% j- Z

, Y+ W9 K5 U! H" n9 ^7 X4 t                    GetOperationSubType(operation);
. S7 R0 E9 w9 \' X, `( _9 H8 z. z, `& x# [6 Q( k) `& Z, w
                } /* if type */% T. f# K5 C0 @
            } /* for int i = 0 */* a1 u; g9 G9 K# \
        }  j3 Q9 |& O4 g  }5 J" K
        caTCh (NXOpen.NXException ex)' a0 s& L$ K6 W! k+ W
        {
9 C5 h& F6 B& M" r            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);: b! |3 n9 `" a4 x  v& s9 ~
        }7 p0 T  @# f7 w+ G0 ]- l! {
    }
( O% G$ D. Y3 L
2 `9 e" }/ ~& k% z% ?* w3 {- M
# ~1 f0 h0 g4 {! z5 q    /// <summary>+ t- m, a; l5 V) O
    /// Retrieve the Operation Subtype
+ j% C- O* i$ k$ R8 U2 v    /// </summary>
8 T4 n/ r( K9 m- j7 F    /// <param name="operation">The Operation to Query</param>( D; P/ @* L4 v
    /// <returns>Return the Subtype Operation</returns>
2 Q" `8 D7 R. A% n" M8 L" I% g    public static int GetOperationSubType(NXOpen.CAM.Operation operation)+ Y: z  y: W0 q4 h, H& \
    {
% `, {% j; b7 L! t        if (operation == null) return -1;9 P) ~6 i2 b# d9 D6 _/ N
8 @+ ~* J2 F) }& Y! ^3 H9 j
        int type = 0;
: {/ @5 I. D5 P        int subType = 0;7 b4 k7 L$ F; N" Z& e( D
1 n( w& }$ B/ k7 c+ W& Z
        try
, N  }. \. I0 }" Z5 P8 ?# E        {2 ]2 w* H& K5 B' H# V. U
            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);
5 D9 W7 O% e; F2 \( H/ G: x            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
  C6 D  J8 S8 V: T, _. @7 d* k* |) x. ]/ u6 \
            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
: X# L% R. `5 {7 {8 \4 ?) d            switch (subType)
% O: g* X8 t/ f! l8 @- W            {6 p" ~1 N! [( Q4 k: V
                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */
1 d0 S4 h$ J( f7 c# p; L                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */! G6 H& i3 |$ w; Y  t5 j  F* J5 u8 h7 W
                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */
- h# [& Z/ p5 c                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */
1 [) f, C0 I$ j, v6 w                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */
: Y4 }3 }) F6 R+ p" R                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */& ~+ x2 ^$ m+ h1 R; n
                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */" N0 c* S1 s( X
                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
& n" L* ~! Y2 |: e' K) q/ z+ B" o                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */
5 u* s4 f# t: i3 j1 i/ w3 p* W6 K                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */
: F1 a) W* I0 P' j1 D                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */
6 w  ?; g( T& S6 @, ?8 T                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
5 r) \" u7 z( ^                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */
5 R# A/ H* G" N  {7 S                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */6 L4 M# I( v0 @( Z$ M" L0 |
                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */
; {, k, i4 \- u: D5 H2 M                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */+ q/ o& M# o8 W4 g0 x- A
                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
  B% N# H  j* N1 E4 u, W                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */
" ]9 O7 f+ r9 M% V' n                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */8 h3 N+ w* E+ ?& B( m$ z
                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */
+ V* A- b$ |8 V; k' d1 k0 R2 ?                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */9 j1 V6 s. }, b0 H" A

2 s+ e) k2 u+ O2 d' z                case UFConstants.UF_insp_tolerance_subtype:
7 S$ S( k% P- D! S7 m                case UFConstants.UF_insp_path_subtype:1 i8 X/ X( g7 H$ }. x2 @
                case UFConstants.UF_insp_output_subtype:
6 H/ {" _: R: O' {: s                case UFConstants.UF_insp_misc_subtype:
( X# O0 @/ ]. Q2 o8 W2 U                case UFConstants.UF_insp_align_subtype:9 j8 E( Y3 X% Z1 u9 s$ s6 ]' `5 T) S3 e
                case UFConstants.UF_insp_sensor_subtype:2 z6 g, R- q8 O8 P: P, E
                case UFConstants.UF_insp_construct_subtype:
$ _' M8 H% B5 j' j0 J- Z                case UFConstants.UF_insp_bounding_feature_subtype:! @, L7 q1 ^9 ?+ m1 Y' V
                case UFConstants.UF_insp_feature_subtype:
& B6 {- P1 S. t) Q3 l3 @* Q1 S. o: z% C
                case UFConstants.UF_mach_canned_cycle_subtype:2 _* u% H4 Z+ _/ X
; A: p5 ]! x7 @
                case UFConstants.UF_mach_laser_teachmode_subtype:
  q$ }. h1 j: |" j4 F& v# E- \" J$ G( W2 }) w" ~. q
                case UFConstants.UF_mach_turn_roUGh_subtype:
, T5 h( r& |7 F2 {0 `0 I                case UFConstants.UF_mach_turn_finish_subtype:" H# ?% x% S6 Z! W. r
                case UFConstants.UF_mach_turn_teachmode_subtype:9 h) c1 D, G/ l
                case UFConstants.UF_mach_turn_thread_subtype:, D8 t8 A  t: Y
                case UFConstants.UF_mach_turn_cdrill_subtype:
2 t$ J/ O  J' a0 W9 L                case UFConstants.UF_mach_turn_auxiliary_subtype:; |7 @, y3 {5 P9 {: `( g' ?
                case UFConstants.UF_mach_turn_probing_subtype:) {0 U9 ]- [8 B1 D+ D1 d1 U! }
                case UFConstants.UF_mach_turn_tool_probing_subtype:
3 ^$ i0 p0 y  i! s% g% [                case UFConstants.UF_mach_lathe_mc_subtype:& e$ X& p0 {) R' J
                case UFConstants.UF_mach_lathe_ud_subtype:) Z6 H3 g, w1 z$ C/ `& g9 W
6 o  x7 d6 \# e: [2 x9 A
                case UFConstants.UF_mach_wedm_subtype:
  E; D" p% R- a+ i$ I' q" f' J: Z                case UFConstants.UF_mach_wedm_mc_subtype:
  r! I2 O9 E, ?3 K/ y3 ~                case UFConstants.UF_mach_wedm_ud_subtype:' S0 q) S8 m; [& ^7 d- C! y8 T  f% n
                case UFConstants.UF_mach_mass_edit_subtype:
! S0 R" \  j+ s; p                    break;
: g, J* l% w% y2 ^
* f5 ?8 i! G: `/ G/ U: U                default:+ G( J- n- E! _# N
                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");
4 R, N9 K7 ^7 V                    break;
' ~& L9 Y4 F, L8 L8 p: X: n            }+ U8 Z& q, Y+ @
        }
& w2 O: V3 N  T        catch (NXException nXException)% x6 `5 B7 l4 G/ D' x1 p  Q" N  {
        {+ S, j# @  Q% F5 e7 j
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());
+ v, N) z/ i4 G0 z! G. ~) Y            subType = -1;
% j3 H+ Z0 c; |. x+ B' \        }
4 r) S# D% R8 Y# @8 }
7 @! K, [9 f/ ?  X# p% x+ R$ b8 m        return subType;& L9 W+ N( x1 q' r
    }) m5 }9 v8 K% g( D4 x' L5 v' t
  b9 H3 }6 m- t& u; F
    /// <summary>
0 F; q  t9 Y4 g- q- F$ {    /// Display System Information
: a) f3 V5 x3 M' G* `    /// </summary>" X. s6 s" |/ K
    /// <param name="uFPart">The UFPart to Query</param>8 \& Z. e7 I% {/ p9 F
    /// <param name="workPart">The Work Part to Query</param>
8 M+ z4 {* p+ b    static void SystemInfo(UFPart uFPart, Part workPart)6 x' t2 j8 }5 h: l5 u  z9 S' b5 {
    {
0 g/ u% O' K; e" X6 c. d' s6 a6 `        try
/ D$ e; A2 {: u. S( M" x        {6 r9 K* M# t) Z; f* m5 o
            SystemInfo sysInfo = default(SystemInfo);
9 }; c) r  u( R. S; i$ |            theUfSession.UF.AskSystemInfo(out sysInfo);
5 u6 ^* c2 h2 e/ R* m: ?' r( j& w+ a4 W; ~
            string partName = string.Empty;
, b  G- j' N+ K5 L. x            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);+ H# B: x; Q, V* P7 {

% {; b2 O+ I+ i& D& {            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();5 G' T) o3 ~/ a: P  X- G* Y' r
            theSession.ListingWindow.WriteFullline("============================================================");" D) m! B! h' E9 j; O. V
            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());
( M- S) b: x) z            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());
9 u$ ]* V6 R$ k$ D* S            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);
9 s- C1 q) C. Q4 c            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());& [1 y( b( W' _& c2 A2 G5 E# r. M
            theSession.ListingWindow.WriteFullline("============================================================\n\n");2 R& `' ^% f" ?$ X! X  X' m4 E5 C
        }
/ y: j8 g" A# X7 m; j        catch (NXException nXException)
6 s1 X6 ~2 F& R/ D2 f        {
8 e8 k% `7 s( L4 v4 H" o; h8 q            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());
* U3 m: m1 h0 `- v& C5 x# O        }
3 ]$ b' |& W6 R5 A5 `( D. e1 |5 c    }
! C+ i2 g, m' w! w
2 F+ a5 b" b) Y; T  R    /// <summary>
0 O- n9 v. a( f6 D1 l/ U    /// Unload the Current Image
3 F6 z1 E& }  W    /// </summary>* n! F% ]. [, w8 M& D
    /// <param name="arg">String Send by NX</param>+ E  O& H; {2 j/ o" h/ d
    /// <returns>Unload Integer</returns>
6 F- l4 B+ i% f8 l    public static int GetUnloadOption(string arg)
; d. W2 B9 z- |1 t; K8 x    {1 w# z- `; G5 a" s3 W  T
        /* Unloads the Image Immediately */2 G1 c; ~- O( p! [+ i* K# h
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
5 l& D1 [' y1 O3 S2 b, R0 f8 p2 |9 p* N  b: T/ G9 R
        /* Unloads the Image Explicitly, via an Unload Dialog */- I% K/ `. R2 N0 ^. x$ C
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
! c+ r/ y$ v: ?1 G3 a# M  {7 X
2 a( l+ {* O; _/ ?$ n        /* Unloads the Image when the NX Session Terminates */
1 w; M6 s/ Q5 e8 t3 S        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);+ P8 S# e2 n  t' `, [1 s: d4 R
    }
8 P) s  N3 N) c}[/mw_shl_code]
0 [: x- h( l- l6 U  |  ~' l3 W
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了