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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了
. ?" f  B/ p' |3 @  E
) `! |6 S" G2 h$ M[mw_shl_code=csharp,true]using System;* M% X4 D; q3 a7 ?' O0 N
using NXOpen;
, _& U; \& {1 }2 m* Nusing NXOpen.UF;
! E  n2 [+ s5 J, K1 @5 y4 v7 F- n5 ^using NXOpen.Utilities;' t2 S7 C0 X; k% P  ]  s% P4 k; O' k4 \, C
using NXOpen.CAM;4 I: E2 b( N/ B9 W& F
using NXOpen.Assemblies;
' B. u  m# A, t: R
+ L/ j7 W: Y/ W2 nstatic class GetSubTypeToOperation9 y: }6 h9 g9 @1 U7 j
{6 g% I( d' i9 l* d! o7 i' |
    static Session theSession;. ?* V2 U$ w7 q; L; K; G
    static UFSession theUfSession;
) i+ T& I4 t! q    private static UI theUI;
/ {$ l# n( S  T/ ?4 ^4 n
$ z, U8 u" g5 t3 y    public static void Main()
6 g* l0 s  G+ b' j$ O# J. n    {2 H' [# I. @7 t* ~
        theSession = Session.GetSession();
, h9 D4 d/ e9 G- j) n        theUfSession = UFSession.GetUFSession();* l9 r6 |  q( f
        Part workPart = theSession.Parts.Work;+ A7 A4 `: d2 q! {* v
        theUI = UI.GetUI();
1 J& g- n' i: H/ L3 A' ~. m* q  x+ Y' X! D6 }3 ~6 d
        if (workPart == null)
  _' M) ~) x) o# x, W( @        {
- Q' s5 l" _6 O- X: k            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");
7 y/ q+ y- I. l9 R( s0 V            return;
1 n# R( r5 r. h5 H+ M  g* J        }6 f9 }) L0 P# x8 ]

9 F4 ?" y5 p; X  P. L  _        theSession.EnableRedo(false);" g" m; R6 y0 Z3 F& S- P& t
. {  [7 p" l4 u1 M" h
        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();  j" w- l5 O! ^1 _& A
- F9 l$ q5 T0 Z) a, R# `/ M; g
        SystemInfo(theUfSession.Part, workPart);9 R7 e' o& q: @' y! F* J; y/ G

1 g+ H$ E; b4 {* E* G1 c" v        try+ z# ]4 A2 @! O4 h; G7 ^, J. \
        {
$ o* N0 B2 k+ m. [% J            Tag[] operTag;) @3 q" @* j0 n2 l9 g
            Tag setupTag = Tag.Null;( e* h5 g! q9 [7 H. @& K  k1 @8 n
# r# s$ Y  M- U9 q
            int countObject = 0;$ s7 j  d0 m& l7 ?# S1 ~

" y  Z1 X8 ]1 H5 K/ u* n            theUfSession.Cam.InitSession();
, }+ V( C" C4 L2 V            theUfSession.Setup.AskSetup(out setupTag);
. `( Z; t5 G  w; t: d8 N
# ]& v8 k3 D2 Z' Y            if (setupTag == Tag.Null)
3 A7 z/ k  e" p. S! y! m8 r& z9 A# E            {
, H8 A7 \3 |. F; r' v% u                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");
7 p7 K5 H4 i: h' U                return;5 }0 ^% T: F7 V* y
            }& e; M' u& ^. l8 c, o* B2 r$ @
" s2 C$ ~( W; [' U. l
            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);
# e0 N8 S; G9 n* |8 ?$ m, n- Q3 ^  x8 r- w+ n. r2 ^
            if (countObject == 0)
& {" V% _4 l8 k- g) h$ f% n  |            {5 L, I' v" c. a$ o* W! d
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");1 V$ C; ~+ Y3 ]7 o
                return;8 [( T& ^# N3 l- p
            }
1 F: U7 ^1 {- B, k1 S: J( J4 w# o& E
            for (int i = 0; i < countObject; i++)
0 [) K4 U1 Y* K% O            {
' P: t! X! e6 d' Q                int type, subType;% e% t+ W& O/ }, d4 ^6 w2 Y" l) Y  h

: p, l- ?" C4 j# Z                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);
2 P# @) O+ g5 _! c7 U
! c9 ?: G2 v! `  y, S; s                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));
1 w2 K; R! Z! s) s) X6 V+ d' B8 D; @  A* f+ K. ]
                if (type == UFConstants.UF_machining_operation_type)
' Z& t3 D+ U# f                {; N. l2 L3 H8 N  a$ e
                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));3 @$ f# M6 d# t& q

. }+ ]  M" h+ M( O0 ^7 A                    GetOperationSubType(operation);3 D9 Q3 u& l% ^3 k* D0 [. x) A. V( ]

: S) E/ |$ |" f5 o                } /* if type */) s# k$ Y9 q; x- O" t! T, p
            } /* for int i = 0 */& y. }( _6 y2 d/ a3 L
        }
5 b( z, S3 X: `5 Y1 f9 S        caTCh (NXOpen.NXException ex)
. I8 y0 i6 U1 @' I) u3 y3 f        {2 a8 G1 p4 v" [! m# A0 u9 g
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
5 b2 z. T8 M7 k  i1 [" m. h9 _, h        }2 ~0 p2 c& F) v  a0 q1 E
    }* F: x- T( o4 c/ b) I: f* _0 J
$ G5 d& H, y" R! H& U( H- q
& y, Z1 _* V6 T1 K1 ?
    /// <summary>1 a! J$ D, `1 g8 o3 @3 f0 H
    /// Retrieve the Operation Subtype
( s( t4 F' v8 g5 m: X: v" a    /// </summary>$ q" V2 E- a1 h8 K' |
    /// <param name="operation">The Operation to Query</param>6 m$ j/ J7 O9 d2 u0 i# V
    /// <returns>Return the Subtype Operation</returns>8 u4 Q$ j9 u+ q7 s% y
    public static int GetOperationSubType(NXOpen.CAM.Operation operation)
. j: r! U" o$ {$ B! G) r) o3 r    {8 ^: M1 [+ S- S6 Q; s6 `7 J
        if (operation == null) return -1;: I# U, H- ]. }, A
7 ]" n1 ?: o9 X1 ^2 G# R
        int type = 0;+ t+ m. {' E: F2 T
        int subType = 0;* @: {! m+ }8 ~$ F( f
  ~  b5 C8 A9 c3 Z7 H* S7 o
        try
, v; G- D& ~& R$ M" k$ D0 W- Y        {
& b9 p' E$ O  ?0 y! _7 x            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);
1 c3 L6 ?1 z; S  D$ N* d- q            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());3 s0 q2 g/ G# ~, {1 s
9 q; R9 {8 U9 A* M0 d2 T5 w) O
            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
, Q4 K9 }+ D% X( C9 k2 H) q0 J            switch (subType)& \: U8 }0 }* L1 N0 d/ G- t
            {2 i: c1 d$ w0 e+ _" g; h6 O
                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */
8 C& z* f2 U6 J) F                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
6 l, ?' a/ K: K9 f- Y                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */+ [5 E5 e+ c1 M* e, {
                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */
6 y; X) Y' @* E5 |                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */* e' c4 T5 o/ b: @
                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */
7 S# S2 O, \" D( R                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */9 s5 U  b% E; X1 Z' r+ J
                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */0 }9 {5 J; @: h8 h0 O9 ^8 o, [
                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */7 o4 \- |1 {, U; F2 ~
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */# g4 G/ u& t9 L6 D2 {1 j9 A
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */
1 N  b8 F( Y- v1 f. u, z- B* |                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
1 K) O2 l) c7 @5 i3 w& E( V5 B0 b                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */
0 `( _) z1 F0 F, K                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */. V9 {' j6 e$ c* T, u* U4 q
                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */) Q5 w% m9 {. b
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */
+ ]' `4 r! ?2 `( N) J! V) J                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
8 K3 p( |! S9 p                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */
( F( }' A: w5 \6 G3 H                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */
1 G' ^% P: |% r2 d- `- y                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */
, v) q6 S; H; p+ i( d                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */; C: W. R. m+ R9 C4 Y

! ^, t  b" s! k1 M: `( _4 y                case UFConstants.UF_insp_tolerance_subtype:
! ~4 R' y0 a" d/ b" q/ w                case UFConstants.UF_insp_path_subtype:! p: E# r0 E8 W9 p
                case UFConstants.UF_insp_output_subtype:
* c( X0 o0 ?8 l, H3 s2 p                case UFConstants.UF_insp_misc_subtype:
: }% P0 H8 _% Q0 }6 _+ r3 j9 V2 g                case UFConstants.UF_insp_align_subtype:. L/ y# F' n/ ?
                case UFConstants.UF_insp_sensor_subtype:' h6 u7 B6 K: S! m( e- n' r: k- S
                case UFConstants.UF_insp_construct_subtype:
/ O/ C2 O1 O9 F% F) u/ Q* V* x7 y  y                case UFConstants.UF_insp_bounding_feature_subtype:, A0 }# j/ H+ r+ u4 |; F
                case UFConstants.UF_insp_feature_subtype:
$ @9 S! z) r' ^( p
/ j4 e5 a3 _) M- k8 Z' X% S# p! ?% ~                case UFConstants.UF_mach_canned_cycle_subtype:( ]0 I: |8 t9 Q0 a+ \9 K
9 F& w, j# m* h/ a# l
                case UFConstants.UF_mach_laser_teachmode_subtype:
/ b( B5 p  b! [6 u" C; M
, @8 K  [* E) ^7 N                case UFConstants.UF_mach_turn_roUGh_subtype:1 ~$ q. g& c$ ]: M1 I) A4 v
                case UFConstants.UF_mach_turn_finish_subtype:
: f/ j1 I( g; @1 T                case UFConstants.UF_mach_turn_teachmode_subtype:/ w  n3 j0 k' C7 q' k
                case UFConstants.UF_mach_turn_thread_subtype:
/ ^. U! z3 O& K) b6 Z                case UFConstants.UF_mach_turn_cdrill_subtype:
, {% T2 `) n9 k, R  M% h/ M. E) t                case UFConstants.UF_mach_turn_auxiliary_subtype:' d# a% G! T" x/ Z2 n' e
                case UFConstants.UF_mach_turn_probing_subtype:
$ [* s; \# r# H4 U                case UFConstants.UF_mach_turn_tool_probing_subtype:
" o+ r9 L# \3 n( s                case UFConstants.UF_mach_lathe_mc_subtype:
/ @  Q0 }" O/ d& t! ^                case UFConstants.UF_mach_lathe_ud_subtype:
- {' V9 Z1 {( j4 W9 K$ a/ |( s; Q! p
                case UFConstants.UF_mach_wedm_subtype:0 t% {1 l6 c; g; P8 I
                case UFConstants.UF_mach_wedm_mc_subtype:
9 D+ x& C! V$ e4 w+ s                case UFConstants.UF_mach_wedm_ud_subtype:
, b( @7 C; z. @3 J/ G" B+ \                case UFConstants.UF_mach_mass_edit_subtype:
6 J2 l. e2 o) J$ f+ N& Q                    break;* c  J. j: L% _- f

2 O. |5 W9 y! s: W8 x                default:
0 S" `3 y" t* q( t                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");/ e/ t" b$ ?6 _3 A) L1 {7 N' n
                    break;' }* z. b3 z" B  ]; j0 J
            }
* {3 P" w- ^2 Z( K- A0 J        }
" V3 K5 i: H0 Q7 n( \9 G: K0 F        catch (NXException nXException)
- J* t5 T3 }/ q# Y( m        {# ~# d$ U* A6 m+ @) z, s
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());
7 S/ `8 u: m# D            subType = -1;1 v! `' `8 F( K- i
        }
9 c6 M  @% C5 r6 V/ T8 G2 S9 }9 W
4 ~- e7 X$ N& j) ^7 l$ ]( _+ X        return subType;0 }# D( z7 V. J0 F6 N* O
    }( D1 E( i! s! I; V2 `: ], V
  O9 J/ Z- E# o2 K: `! ?
    /// <summary>
4 X& \3 p0 R% Y- Y9 |3 b8 C    /// Display System Information
# Z& A. f, |! o4 @. }( P    /// </summary>
1 |- `* }1 d8 b    /// <param name="uFPart">The UFPart to Query</param># n* _6 Q' Y' H1 h! b
    /// <param name="workPart">The Work Part to Query</param>
$ ]. n7 D5 G; A4 k7 c    static void SystemInfo(UFPart uFPart, Part workPart)6 [4 i5 o7 \4 V. u1 D
    {
# {0 C# r9 b7 u% q5 u        try  ?, s3 y$ Y7 x* R% k5 D/ _
        {& X% ?9 V8 K7 m/ `: m9 Q5 N2 y# m, m
            SystemInfo sysInfo = default(SystemInfo);
! T( ~( |) k& M9 A* ?( P, c! X            theUfSession.UF.AskSystemInfo(out sysInfo);
# a3 t' U$ v2 P; N7 F: J% ?9 L
: o5 R) X6 I* v$ j0 O0 M+ p            string partName = string.Empty;
2 T2 b! V7 j- B            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);9 T/ i$ ^/ S0 D3 I1 B- H; E  q

3 r4 ^: L3 O/ }- }; t6 C% K            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
: t1 ~1 o4 x; U            theSession.ListingWindow.WriteFullline("============================================================");0 K$ n- {+ E  o) p5 u
            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());
; f8 E# v5 u4 `+ G5 i5 B0 D! Y. V            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());7 `( A: x  W( W. s7 C2 x- h4 a3 i
            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);
  n1 A% K! V1 W0 u            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());
/ }! Z: Y; O9 x) B( k" \; K  |            theSession.ListingWindow.WriteFullline("============================================================\n\n");% t; o  j2 B# e, A# |
        }
) \& O% ]+ Z" m- S6 Y        catch (NXException nXException)
1 U: i- z  O9 e: V5 G9 ~        {! H& f0 L+ w2 `1 x& f
            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());% P6 n9 ~8 u, w% ?! Y2 x% L
        }% x: Y2 c+ M7 g, l. r
    }
) h  `$ C- ~7 x% R" S: g. x( {0 Q1 h% S) e
    /// <summary>
! u" z, U5 ^7 u* M( m2 {    /// Unload the Current Image: T! c' K* \% ^" g" k; O$ a
    /// </summary>/ t- u  o5 M0 P3 p+ f. G  \
    /// <param name="arg">String Send by NX</param>. s+ B# o9 y  B
    /// <returns>Unload Integer</returns>( ]& Y8 }* L$ s% d
    public static int GetUnloadOption(string arg): Y6 h/ x4 h# c, r
    {
2 P6 d7 N8 r* {: x3 V        /* Unloads the Image Immediately */& m9 h/ q$ ~  S# J* F; q
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
7 S: _; f' {3 g# L0 Z" z- P3 N0 T9 W  O# s6 _7 X
        /* Unloads the Image Explicitly, via an Unload Dialog */6 b$ h4 m; H/ a; C
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
' K2 I# v2 c8 U1 _7 @
% G8 U  @, ~% B( S1 S6 n        /* Unloads the Image when the NX Session Terminates */
  J- `5 e$ u8 }: d2 o1 w" x        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);- i$ Y( Z- `$ J7 O/ m1 l
    }1 d+ v9 L) `% ]$ i# l
}[/mw_shl_code]
! I9 a' e- m. I  X
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了