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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了6 n# W2 ]0 V% H! O
- _3 Y* w, J* r4 M* p5 p  L
[mw_shl_code=csharp,true]using System;
& f% O+ v& \8 y8 t. ^* W; rusing NXOpen;
9 c9 `$ g" P5 Iusing NXOpen.UF;' B+ b& @  {( y8 i+ }/ `) @
using NXOpen.Utilities;
3 L9 a& I0 S0 U" x/ `0 \using NXOpen.CAM;8 }4 K2 f9 B8 w3 A8 @
using NXOpen.Assemblies;" Q( I! Y# e3 I/ L8 J0 H
) o/ E: L! Y$ T1 t
static class GetSubTypeToOperation
% P+ |! F, h, H; p{: n* {" n8 ]8 c. z# g+ k# Q
    static Session theSession;
6 Z6 h. x! Y6 m    static UFSession theUfSession;* F0 Z7 O# n2 h* T0 i; f) n8 f2 p
    private static UI theUI;
4 n$ G& d( ]- C' u$ i" {: C& a! f, P. U$ B$ y9 J" D# J& |, n
    public static void Main()
$ ^! I" m/ k9 d( U5 p- q3 b    {3 Q1 ]; ]; N! j
        theSession = Session.GetSession();5 U( k- ]; s9 ]6 Y% h; }$ H8 k
        theUfSession = UFSession.GetUFSession();
- X, [" \6 g! C0 b1 i) d6 ~        Part workPart = theSession.Parts.Work;& r' V3 S! r$ v( i5 m6 ]
        theUI = UI.GetUI();
) \$ p. ]! ~5 t/ L, B$ [: b) d% g: v
        if (workPart == null)  g- @2 w- P/ g$ D
        {. D; L, ~* T; S
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");
9 }  M* C4 r6 G+ I! C            return;2 B5 R8 \. ^+ N/ z2 d! s" q
        }  s! I9 w9 |" a  L7 M" L+ [* D
  P* @: c3 M  n! G/ E3 z1 n+ j6 C. @
        theSession.EnableRedo(false);
7 t4 S3 F/ u1 r! G) X2 o( ~8 M' ^: C. x  n
        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();8 x; q# Y& K' |1 [
& r# F& w# S1 O4 U
        SystemInfo(theUfSession.Part, workPart);2 f3 l* ]3 H  }/ z/ y3 |
, P$ d$ X4 [- g# y
        try
- m0 H: [* H: J) J) u        {
8 h$ U3 R3 r3 j# c            Tag[] operTag;7 q) \9 k* A( b* D; _( X- Z
            Tag setupTag = Tag.Null;- L% Y6 X" a* j+ e% ?0 L
+ e: l% [. F1 r4 u
            int countObject = 0;# f, r5 ?$ y( s. t. F! y6 q  C" s

2 _# |8 Y+ k& b: p0 R1 R0 Y0 s8 {# J            theUfSession.Cam.InitSession();
; a4 V2 z' T, V" C8 T2 L! h- d            theUfSession.Setup.AskSetup(out setupTag);$ f$ E: B6 v6 m+ \, Y$ v

6 ~3 K: s5 A7 z' Q  ]* W            if (setupTag == Tag.Null)" ^! A; C! ^7 S3 @- S/ ^
            {8 o8 y" T* g4 ~! H6 h) y$ z
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");. X7 W8 K' \, a8 H" F
                return;" e: n1 K5 T% s0 I% _' U
            }0 N6 Y1 l0 p4 m+ P- S+ w8 F/ z

6 Q7 S! [, {" x- a9 W            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);
' y* d' g2 H2 [# {3 o4 N* v) S. o4 O8 h; ~2 T% M
            if (countObject == 0)( d$ P8 N* \. X) Z, H  t
            {
4 A+ {* N- u7 W                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");" b7 d% M* i/ m+ ]
                return;
  S7 _7 ?) _3 L" s            }
6 o5 D! q- X9 S8 f! a# h& B) y5 e5 i- T2 p) b
            for (int i = 0; i < countObject; i++)
5 u; W" z( b" n' J$ ^            {
+ p: m- p: b5 p% H( D; ^4 N: w/ n                int type, subType;
8 Q( m1 ~$ c  K" G% [; }- M0 s2 o0 [/ J: U3 o, g+ f
                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);: R; T$ P! ^% e5 B4 }2 W) [1 E3 N

4 B1 I4 Z/ V" I( n$ S5 J4 c" R                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));
$ k- t; n- k0 O0 F' |2 T/ T  s9 p( r; s0 b5 l7 q
                if (type == UFConstants.UF_machining_operation_type)
6 z# c9 r' H. H0 P; L                {
' K% K" W: F5 h2 N/ c2 A: F                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));
  Q9 W1 R9 y9 l+ w. p7 e) s! C' |8 r& B) \. ]( M8 ]: E8 @/ N9 A# }% E
                    GetOperationSubType(operation);
, j- ~: ~& [2 w; T5 c8 J+ {5 Q- d3 h# B6 B: f0 _' d8 C7 e
                } /* if type */
7 q4 G7 T) J2 x8 d, G* Z0 j            } /* for int i = 0 */
& G% ~; ^' \: _; P9 M        }
" j9 o; M6 w/ B$ r        caTCh (NXOpen.NXException ex)3 M% Q6 ^, z( M  q
        {
( Z; f4 `4 h/ H3 K1 i9 d            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);/ Z! l  G% i. S
        }
1 C! q+ d' B& F# D$ y2 }. t0 ~    }
9 @5 p8 ]" N! y3 K7 z6 [0 Q6 X0 O' ~* {4 w' V$ M5 a

* B3 x7 O1 V: `/ n1 X, m- ?    /// <summary>3 I& u3 [" [3 o( K' t" P
    /// Retrieve the Operation Subtype
' a/ {% }* X9 y3 J    /// </summary>/ h/ o- m* _& |2 Q7 D& G. C
    /// <param name="operation">The Operation to Query</param>' k& G& O' q; m! c8 r
    /// <returns>Return the Subtype Operation</returns>
; Q/ V, w( o9 O2 ~    public static int GetOperationSubType(NXOpen.CAM.Operation operation)4 ?& x* r7 [1 \0 U
    {$ k; Y* p) Z+ d, ^) x6 J3 `- b
        if (operation == null) return -1;5 j* R+ y; {' i. s2 ?$ Z8 ~+ N+ q' O
3 e) E' F+ r5 Y9 R' B) d
        int type = 0;
' r3 i* Y) x  r4 u- W        int subType = 0;+ @/ f7 V9 w1 k( P7 J- P% ~% m3 ?3 v( n
6 [% k0 J3 p) A$ F4 I( X
        try0 m- I& y, s$ O* i7 U* M/ ?4 k$ O5 \; T
        {4 s, J. a, X6 P4 @
            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);/ n% G2 s& N# `( ~' H1 t
            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
! q( l7 o, }; n7 a6 G% m
/ v$ h/ N# Z7 X: V            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/# d9 L3 d# F6 `+ p& k/ P
            switch (subType)
5 M+ B" J- @" U! d+ T8 U            {
7 C) d# U* S) c0 R6 [5 x                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */
. ]0 I! k# R/ l: G' I                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
4 `7 L$ }  \! m  E                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */% H0 o( P- e* \# y; w  e
                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */
1 j4 u+ s3 a: g                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */
$ O+ m4 l! v6 x9 h. m/ {1 C                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */
8 L6 q% k9 \4 e9 b0 ^% |* h                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */6 \) N$ [- _* Y6 `
                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
- C8 _! o* f' A8 i5 n0 h                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */' _0 W( G# C/ l! b+ K* G# f
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */2 }1 _$ R& C5 z
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */! ~/ ^; @- A- I- @, {' Y2 S: j: J
                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
4 b; K$ _: X2 k) j* f" p7 b2 l                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */& i) B4 A8 V: a4 c) w1 D. E1 |/ {+ R
                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */
6 V: c) @: t$ f                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */  W0 K. y' |/ s4 \2 I
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */
5 L7 M1 ?1 z" J, {" p) ~5 `                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
- U' y  o! i9 L                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */5 D1 L2 F1 `' I9 i0 T4 Z3 p* ~
                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */
5 D+ p9 u+ l+ ?& w$ ^. H7 s                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */
/ l8 N# Y1 q; s; I                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */
4 a9 _: f& r2 l% [$ Z
8 G, t9 M3 e* {. p( @                case UFConstants.UF_insp_tolerance_subtype:
/ h* |! y1 D( Q& l& s0 Z                case UFConstants.UF_insp_path_subtype:
( n! X7 l; a: h                case UFConstants.UF_insp_output_subtype:& F1 M0 P0 P$ Q7 e
                case UFConstants.UF_insp_misc_subtype:
7 u+ n8 h" k$ c' k% g  ?" m! U                case UFConstants.UF_insp_align_subtype:( S& l# j2 a, N- w8 M" D1 v# b* w% _2 T* G
                case UFConstants.UF_insp_sensor_subtype:- U! g( x$ w4 s8 L! o: {
                case UFConstants.UF_insp_construct_subtype:) ?$ R3 f. H6 ]+ U4 q6 U& M
                case UFConstants.UF_insp_bounding_feature_subtype:( r4 b% v2 o) O' U' w# r7 u8 T& c
                case UFConstants.UF_insp_feature_subtype:
" @! n) Z. a( B' w0 e2 U" X1 y, t( P, ]2 c
                case UFConstants.UF_mach_canned_cycle_subtype:
6 o, [, T4 H0 d; X4 f; s
7 M5 |- v. `: ]" n; ~9 v  ]                case UFConstants.UF_mach_laser_teachmode_subtype:
$ [; `% @9 s  Y) s& E! k8 \* x1 \4 z$ P
                case UFConstants.UF_mach_turn_roUGh_subtype:
0 a5 c! z2 R* {9 D1 v! N                case UFConstants.UF_mach_turn_finish_subtype:& |7 U3 V+ X, Z" @( Q
                case UFConstants.UF_mach_turn_teachmode_subtype:
( F2 K) f# b4 F6 |( D                case UFConstants.UF_mach_turn_thread_subtype:
7 o6 S+ \, G1 U                case UFConstants.UF_mach_turn_cdrill_subtype:. E2 c4 c/ I0 r/ v7 |7 j
                case UFConstants.UF_mach_turn_auxiliary_subtype:
' P# d. t* {! c                case UFConstants.UF_mach_turn_probing_subtype:9 {. W2 }  v4 D  B7 r: {
                case UFConstants.UF_mach_turn_tool_probing_subtype:5 \, m  Z2 ~! |9 Z; K# D
                case UFConstants.UF_mach_lathe_mc_subtype:7 I* {8 \1 `5 L; Q2 j
                case UFConstants.UF_mach_lathe_ud_subtype:
2 S0 @* d& z4 }- G0 n  K7 ^  y" U$ r+ ]# F+ ?
                case UFConstants.UF_mach_wedm_subtype:
% Y* M' K! m/ T, z9 I                case UFConstants.UF_mach_wedm_mc_subtype:
& k( N8 ~4 M3 D) V& o                case UFConstants.UF_mach_wedm_ud_subtype:
& y. ?7 R/ _' o1 w                case UFConstants.UF_mach_mass_edit_subtype:! ]& Z, N% H6 s/ \
                    break;
! |7 }( m8 ^9 {6 \+ t! d1 L7 h' u+ O; c% C) R8 v7 b
                default:
% N8 B, s! s2 K  S, B                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");2 K  d4 A3 f% {, }
                    break;0 \4 o3 A. a) q) J, z
            }% g+ K3 F4 o7 i3 S0 U8 ]
        }3 Z1 p$ }4 Y$ E
        catch (NXException nXException)' A! d( s2 K& _
        {' r6 t6 y4 t! L8 ]% n# P" h- E
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());
* W; G6 o! i! \            subType = -1;1 w- V  v( ]' g1 `! Z5 c2 u
        }
  X) {7 w# O) o: x+ P/ ]& [0 ?
4 |9 `! ]4 }/ f# A        return subType;# d3 G( t1 H6 B4 J& ]! G1 c) e" P) f% e
    }
$ S4 |. C" `$ I0 o
& v( j& E, x( A4 @    /// <summary>
3 l9 G: Z% Y8 ^! B2 \# v    /// Display System Information% T! q/ A) z/ F6 \$ U
    /// </summary>; V3 c. P9 E  ~4 N! v8 `  H$ p
    /// <param name="uFPart">The UFPart to Query</param>  T5 W+ U# v0 V" n$ ]5 s
    /// <param name="workPart">The Work Part to Query</param>8 ^& k6 L3 Z, Z! E6 I9 y
    static void SystemInfo(UFPart uFPart, Part workPart)0 ?' \7 p( Z! m9 r% k9 i
    {
  U; y+ B& z9 ]  g! D4 @/ m& y        try5 S! f" z+ I1 z+ `5 W6 G
        {
0 \1 Y. O( B. M) z) z8 P2 @            SystemInfo sysInfo = default(SystemInfo);/ @$ W% C6 u9 k/ ]" a5 e
            theUfSession.UF.AskSystemInfo(out sysInfo);- K' b# X5 j7 o; Y
2 ^: h8 j4 \; ?! B' R7 }
            string partName = string.Empty;
9 T8 i1 @1 }/ L' d5 i            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);8 J# E3 H+ g7 I) Z
& h. v( S  E! e: x5 |1 E
            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();1 m/ e. s7 U& B: j- M7 M% P9 C
            theSession.ListingWindow.WriteFullline("============================================================");, ?# w. {' o+ v5 D! v
            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());
# {: s( p1 {. p' V$ \- K) ?            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());
" U9 Y8 ?( R* W0 C            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);4 p% h: L2 |  \1 I$ ~$ j& q  k
            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());% I) B, t; z1 b3 A% @
            theSession.ListingWindow.WriteFullline("============================================================\n\n");
0 B9 j4 O, f% ?        }
' D) Q3 T, r7 `! E9 M+ R5 W0 k        catch (NXException nXException)' u& H2 W2 \7 Y% S
        {+ @( Q, {: j3 Q+ O. C7 H3 H3 S
            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());7 ?7 E/ v& s! ?& I4 B. V3 h
        }5 W5 `3 x) P8 I5 ~- G
    }
6 U' Q% W6 p7 a7 q
* ~* t+ a6 T5 B, F0 v    /// <summary>
/ P. O) ~3 L9 |& k6 h    /// Unload the Current Image
2 k% t+ e: o5 {9 o5 n    /// </summary>4 x- u2 H; [% z) w1 R4 N+ [
    /// <param name="arg">String Send by NX</param>
' z+ K) S' ?; K3 \5 r    /// <returns>Unload Integer</returns>; ^; a' h7 [6 u# z2 J# x+ D
    public static int GetUnloadOption(string arg)
4 [% f+ a' {' W! Q* X8 @    {
# k9 o% X' y0 |5 i        /* Unloads the Image Immediately */
; g# m! W; [5 M8 }        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);5 O1 S1 c7 r  q! P  b+ t$ \- T- }  e$ B
( M8 y# g8 R) Y( l3 O
        /* Unloads the Image Explicitly, via an Unload Dialog */
* t4 t$ Y$ v$ n* q4 v6 }: g" P        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);; C( N, ^. Z: |/ L) W0 V, s- ~

! S* A1 K3 t% b. V        /* Unloads the Image when the NX Session Terminates */+ ~7 M9 W& j: \* l
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);* Q. N* ?6 S" f3 u" F3 i
    }' G5 w6 l$ d, V2 v8 `
}[/mw_shl_code]
+ L) A- m9 o+ N% x# a
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了