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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了# H; z% {; s/ s0 K, T. @

; J- x$ ?, b& x[mw_shl_code=csharp,true]using System;
8 q; m8 s6 V; }0 \* `; husing NXOpen;: h2 |1 M3 |7 B5 o# K
using NXOpen.UF;' q- [# p/ M- ~" H& Z* f6 }) H- \9 O
using NXOpen.Utilities;5 e2 _7 ~8 n+ N! P1 {
using NXOpen.CAM;
9 e& x: W$ L# F8 P5 P; qusing NXOpen.Assemblies;
% @0 ?; S& Z% W+ Q) y3 y2 @6 V
static class GetSubTypeToOperation+ v/ |$ ]. ^, f: l
{
6 q" A1 h/ n$ ?# p    static Session theSession;
( R3 m/ M$ o- e( R, D  f    static UFSession theUfSession;$ s7 z) x/ |; A7 i9 H5 I
    private static UI theUI;
: H- M' R4 d. W+ e; L2 @
9 o1 t2 w2 I& t( z, g; f    public static void Main()
! ]% I5 j& W# d8 h    {
  h  Y. {6 t( b3 b/ s- Y; a9 b) s8 I4 y        theSession = Session.GetSession();8 V1 ?( _. i+ O5 Q- [! x& P
        theUfSession = UFSession.GetUFSession();
2 S. J( ]$ g" Q: T6 u# K% f: h/ I7 Z8 u        Part workPart = theSession.Parts.Work;; b7 T. A% [1 F% c- _& C
        theUI = UI.GetUI();
: A. Z* Q! U( W
) x: m: d4 u% y6 \% b$ d        if (workPart == null)9 a) p. O( {) Y7 [0 I
        {
  Y5 Z0 h4 V0 ]% P7 ~8 r2 c            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");( |: ^. ^( {+ ~8 B; T
            return;
1 }. H6 P! u( L0 x; h        }) Y4 K! B* W2 w& p

, }! `" L# o4 [0 f2 Z        theSession.EnableRedo(false);# b/ L- n# O! Z9 y" o' w& |

2 t% L& s! v' q        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
7 B; v* v2 [5 d) m( i% U" c" {/ d9 S/ J$ E) X( A- ~
        SystemInfo(theUfSession.Part, workPart);
: f/ ?7 _6 @9 I! E( o% {( i% I$ [% f; U* R8 E+ u0 k0 F4 i7 P# P6 e3 j
        try! {9 V" ]# v5 |6 F5 P3 d4 f1 h
        {
9 t+ x5 Z: _& e            Tag[] operTag;
4 R4 h, f( l8 B* z3 v            Tag setupTag = Tag.Null;5 |! ?0 T5 Y" o; g, O/ F

& t; \( Z0 N  |            int countObject = 0;
- M8 i% X7 K5 X& p# ]( A1 {( h
  H8 Y: d) j7 k            theUfSession.Cam.InitSession();8 J5 z% ?( K+ Y
            theUfSession.Setup.AskSetup(out setupTag);
& x6 x: ~1 Z7 x. d( p$ l3 _* i, E9 t) u: Y3 {  a+ L, W
            if (setupTag == Tag.Null)
4 G$ N2 Q; o6 o            {; v, S' ~8 h8 K9 V
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");
+ ?8 K$ T: A( Q6 K0 E8 b+ B                return;
+ k. S  E0 D4 d+ r' }& b            }3 l, d) B: b, Y' v% _9 J

6 J* F! i3 b) U/ A            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);7 u- X7 {5 X# c4 U
# U3 {* u: d% Q* Z5 U3 w' y) @2 V7 j
            if (countObject == 0)2 C, [) }1 E6 m' e8 w) |
            {
6 {6 ~+ {+ F6 T8 j$ q: ^2 g                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");& q' J/ W. m7 b2 Z3 k" I2 |" C
                return;7 H( T* ^! L4 l, E$ o4 Z$ U3 |
            }
' S' P& E! ]* n
: m' V0 `. }( S8 p) a$ B# |" }            for (int i = 0; i < countObject; i++)
9 D1 M/ {: U# `, W            {7 B$ y: r; L% l  [4 ~
                int type, subType;/ y& H. ?* p0 F; c* b. V
* b) O, }# s0 D2 @( `) f% o  N
                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);
+ ]% w% p# X4 d: k
/ O$ O4 g! N6 W! E                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));
! K: g9 v; ]  I
; s% C+ a' ?% {- |  L% K                if (type == UFConstants.UF_machining_operation_type)
% U% h' L  u. \1 }9 [  z% Q                {
% n; l: H) m# u/ C) K9 \                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));
' T' n$ B% ]: k! E" F
7 j9 u* }3 r" g8 Y% d                    GetOperationSubType(operation);0 ]4 Y2 Z' y9 t0 L( s" g/ ^
0 N7 }9 A2 R1 R" x
                } /* if type */$ s5 X! s. |& H9 R7 _
            } /* for int i = 0 */3 t& H. c) B. f5 T# p" m9 v  v
        }
$ p: m& C: }' {7 Q6 ]7 `: z- z5 u        caTCh (NXOpen.NXException ex)
9 s9 k3 u* G3 B        {
: _0 L9 {( y  r4 S: h  |            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);8 t5 K/ w+ w7 G# P3 U* u' W! n
        }5 x7 \" B5 Z. d7 m6 i' Q. B8 x
    }  I% a. w: S2 n2 c# p5 L

$ L! [  J! J9 o9 f0 Z0 J) _% u
5 Y& S' O% I/ t$ y+ S6 s" t    /// <summary>
) a0 O9 J* I' ~% ]& a5 m    /// Retrieve the Operation Subtype; |5 ~3 X( \% C
    /// </summary>& l% X/ v, x( r, k7 j" ^
    /// <param name="operation">The Operation to Query</param>) Q- @# u  n  a2 f! c4 d
    /// <returns>Return the Subtype Operation</returns>* X7 l1 j) v7 d' s  _* f
    public static int GetOperationSubType(NXOpen.CAM.Operation operation)
) x% B/ d$ m; ~* r' O* v! u    {
4 e  x* ~2 X. f0 j$ U8 r' `        if (operation == null) return -1;
) H9 \; h+ j& V/ u$ F
& S) ^$ t$ F1 j        int type = 0;7 M6 G% a* p% E5 F
        int subType = 0;
1 Q5 ?4 _& D6 A# n
% t, [3 _+ r3 P& s, `        try
" ?1 f0 V9 y, B# T        {
- o1 f( K5 X; J) i: f  U% H3 F2 K. e            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);
/ d6 X" z  g; O& v* u, D" q3 m. M            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
  ~, r/ N0 o% W9 _& b) l- j& ]' Q" P; d) j  C
            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
7 j( k  t2 U' d+ ^            switch (subType): Z( m3 t# D- Z
            {; {7 _6 }: z5 ]6 O2 e. d" W
                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */5 o' {: A) ^# h/ F1 h. X
                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
+ Q" G' p& ~' w) l                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */; a9 Z$ k5 N. b: I; d
                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */1 k6 W% r# `* q0 G+ k* ]1 e1 C
                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */
  r/ Y/ [3 F. X+ M; s9 V2 A  [                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */
9 T8 X* o0 U" O! _) A! p: ^                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */" z& O- C' F) k9 L: m. S; A
                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
/ r- A) ~4 H* Y( b2 z0 l# u                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */6 p) x8 W1 N% T1 H5 q( F$ p" |8 n
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */% u: @& R  j9 L$ x) W
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */+ C6 s4 u& J' E; [3 `: e4 Q3 \5 s
                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */$ f; X: k+ D5 g' b) U$ Y+ U
                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */, j; g$ J' a9 |& e- y
                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */. O3 O, ]5 q8 r/ O
                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */
1 J4 T: O) t! g0 q5 E3 g& ^                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */" x  N1 o# I4 ~  x
                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
& \" Z  y! u' G5 I                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */
) g6 A, L# C1 D0 p' X                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */! I$ U8 s9 u. K4 B
                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */
! z  y, e2 `) M. F1 O# q                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */  u/ X/ Q( a' @  i/ @/ p

/ B# w8 j- _7 t8 p' H, p                case UFConstants.UF_insp_tolerance_subtype:
& U/ z, q% u" f) w  t- o                case UFConstants.UF_insp_path_subtype:4 ]9 ]- S  T0 m: I) p3 e
                case UFConstants.UF_insp_output_subtype:
% T3 h( a2 n" p                case UFConstants.UF_insp_misc_subtype:
5 r3 [( r% ]! j2 q8 ^5 k% v: f                case UFConstants.UF_insp_align_subtype:
$ d) o. U* G1 c9 h  l" w9 r+ e; u8 T                case UFConstants.UF_insp_sensor_subtype:
4 ]8 V% j1 ?: n- L) E, J+ d0 l/ D                case UFConstants.UF_insp_construct_subtype:" Y9 S* v# [, n  {( ~6 [
                case UFConstants.UF_insp_bounding_feature_subtype:  m2 E; d; n6 I' l, g4 i
                case UFConstants.UF_insp_feature_subtype:
9 \) Y6 `2 @+ w- P
  Z0 {; d7 }( z. Z+ p                case UFConstants.UF_mach_canned_cycle_subtype:
( R( V* Y6 {$ L! F$ ]! v9 n# b" T9 T7 C6 d  M% Z
                case UFConstants.UF_mach_laser_teachmode_subtype:
+ W$ R6 z2 c* C0 B4 Q% j6 Q0 ~" G7 N: m/ u+ t* D5 _; J7 \8 M
                case UFConstants.UF_mach_turn_roUGh_subtype:
$ H1 c8 @* j+ W. X4 \                case UFConstants.UF_mach_turn_finish_subtype:5 E. [" a1 }& ?  e
                case UFConstants.UF_mach_turn_teachmode_subtype:8 W2 m8 X  [( H- }  v
                case UFConstants.UF_mach_turn_thread_subtype:6 x( k) A9 [. X+ |1 n6 U, k
                case UFConstants.UF_mach_turn_cdrill_subtype:
9 A. }3 ?; }" n0 H, a                case UFConstants.UF_mach_turn_auxiliary_subtype:
7 u6 V1 Q+ t' r" B1 h5 H                case UFConstants.UF_mach_turn_probing_subtype:- q/ E5 m4 F- z# h
                case UFConstants.UF_mach_turn_tool_probing_subtype:+ r+ T, R; Q9 }8 l3 o
                case UFConstants.UF_mach_lathe_mc_subtype:
6 Z" }$ L% m" |# O7 O                case UFConstants.UF_mach_lathe_ud_subtype:
: w9 v+ b  L; c: ]& v: @
, f% m0 l! {( q# r: J                case UFConstants.UF_mach_wedm_subtype:" H( B, G% b2 q1 @" ~
                case UFConstants.UF_mach_wedm_mc_subtype:3 g6 k$ x  f! }+ U4 x2 y0 f0 M
                case UFConstants.UF_mach_wedm_ud_subtype:+ L( d1 C9 |3 b( k2 O
                case UFConstants.UF_mach_mass_edit_subtype:# F- u6 X) r1 g
                    break;
6 O$ A1 d# ?# ~- f2 v& s0 a8 \; z" `1 x# ?8 n0 s3 }; v5 S! H
                default:4 j* Y8 t9 h7 u8 F" C
                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");
6 K0 `! h4 {) B- N) ~                    break;
' M1 H9 D: x3 Z: A- \& \- O" _            }3 b1 r8 `- w! b
        }9 `, S5 J8 M& A5 R6 v* _
        catch (NXException nXException)
) ]2 d, O. _& }; c" I. v        {2 H9 }* D1 E+ h4 i( |1 U
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());
$ }9 I% J8 t( c6 l( o4 w            subType = -1;
7 G6 l, n0 ?' L; j2 ^        }
# c9 ~: L- j# t
' l$ Q+ D' u- p2 B# i8 s0 l        return subType;, W2 l4 U# O- x$ M- [
    }
: Z" \' g1 c- |: N! _
+ A3 c& g6 b3 Y$ t& k! B0 V) b, v( p    /// <summary>
* \) r, b' Q! n- m3 E% p2 _! y/ I    /// Display System Information! r5 K0 B2 R. t/ }  U( {3 n4 i
    /// </summary>5 }# L% ?( ], v0 ?) U# u# @
    /// <param name="uFPart">The UFPart to Query</param>
6 `, `4 w, R* D0 U/ p# x    /// <param name="workPart">The Work Part to Query</param>
1 c2 r; s$ S9 A  ?* {; A    static void SystemInfo(UFPart uFPart, Part workPart)
7 I- y& V; s6 K* x2 K, U    {! h8 i6 m4 ?# y1 S
        try: Q* g' Z& E( r: I+ M  `
        {
3 N; h$ \$ r& Z6 p            SystemInfo sysInfo = default(SystemInfo);- C  r8 s* V) b  g( U+ c- a
            theUfSession.UF.AskSystemInfo(out sysInfo);
2 d. X4 r  H$ U8 N# ~" J- ?2 y5 \8 ~8 O  c8 c! C' s
            string partName = string.Empty;
: r8 Y2 o, t2 V+ }            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);9 i# R7 X4 S) ?- W2 F  T3 ]8 E. X- b
8 B+ q- r9 N5 A! X- r& P
            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
; s6 [8 L( Q. \) d            theSession.ListingWindow.WriteFullline("============================================================");
% J' p1 L+ ~5 {! e4 G& H            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());" I3 y- M: e* l$ O9 M# ~
            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());
+ w# d! R8 }+ ^6 A/ M            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);5 K- {. x# D3 _) ?5 I4 y" n
            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());4 K2 i; r! i: X9 D4 a/ o7 N9 Z4 B
            theSession.ListingWindow.WriteFullline("============================================================\n\n");3 e# u0 z) S9 l1 n. W
        }' a$ x4 `$ s% Z* n! J# }. a
        catch (NXException nXException)5 [% E/ h- y1 f/ {0 k4 g3 B. X
        {
& x. J4 ~% [7 q+ X/ g            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());! ^4 m+ a: L) Z6 G$ ]' U
        }
' e( A, G3 _8 R( `' Q+ X1 h/ X5 u    }1 O7 G7 _; J" T5 D4 {  j) `

* w# p9 D- q' y    /// <summary>: P+ t& d4 U7 b+ ^
    /// Unload the Current Image
4 K9 K& R, F5 l; b; [  i    /// </summary>& q( Q, U* d+ G4 W8 {
    /// <param name="arg">String Send by NX</param>; q. M5 J$ z& W
    /// <returns>Unload Integer</returns>  x  Y5 s5 d' N
    public static int GetUnloadOption(string arg)
) D& M7 U2 I- s1 p/ X2 k( @    {
9 H2 e$ X  ~3 {/ D; f- L) z  E        /* Unloads the Image Immediately */+ U3 A2 ^: W4 Z- i9 Z6 n
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);1 Z! Z/ _3 ?; n+ z  ]0 Z0 J' O
& r5 w/ r0 ?" L! T9 b
        /* Unloads the Image Explicitly, via an Unload Dialog */$ _4 ?- ]0 b; r% w& H7 d
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);4 l9 }1 F& ?% h  X" e3 d
6 n3 F: D9 M4 s# s& A8 G, h
        /* Unloads the Image when the NX Session Terminates */
* \: `; j+ w/ o4 Y4 {' \$ W5 ?        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
4 I0 ?4 V- v( j    }3 B( s, ~% G, B1 I
}[/mw_shl_code]
0 F, C, B+ G* v6 p
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了