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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了; x& Q, ^. O  A' L% ^

9 ~4 i* `/ d- W0 |6 N[mw_shl_code=csharp,true]using System;
: T* I" v6 b6 v5 Eusing NXOpen;/ a% D# b7 b, U) Y
using NXOpen.UF;
0 Z' k3 s: v: S; E0 N. gusing NXOpen.Utilities;1 l& Z3 D7 W  Y
using NXOpen.CAM;# o* D" E) d* s  t: b) ^2 r
using NXOpen.Assemblies;
2 E* U$ I* U* @# f5 P! U1 h  K& r* r3 q! Z" e
static class GetSubTypeToOperation
0 [/ n- S; `9 C' K2 d4 b( h) n{
6 n+ \+ b2 k& ?! K$ X# u    static Session theSession;
) i- j% |! v0 H    static UFSession theUfSession;9 i% V6 l" f4 ?5 }
    private static UI theUI;
2 E1 U; }8 m# e: F* f3 s/ T& t. |) L# F( b% W- h
    public static void Main()
- z7 J3 P1 p5 z0 E: O# `# z    {
% c) z. b6 E" `. |4 f8 ~        theSession = Session.GetSession();# d- J$ x8 L  X( Y4 m
        theUfSession = UFSession.GetUFSession();
2 X8 P7 X- ^. m. w        Part workPart = theSession.Parts.Work;
5 Y' J  a. v1 y        theUI = UI.GetUI();$ s9 U5 M7 B; O6 i6 }& O9 p8 Y5 `

! V  p/ b7 k% W        if (workPart == null)
5 f- s- _! G( E( a. a        {
3 s) j' E: K0 D' G/ ~& G! {' @" `            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");$ }; a1 _5 o2 F4 Z
            return;2 I! h4 J& ?6 V; x! K. o) G
        }+ U8 p4 i2 J3 B: u" `$ q6 \

0 y( {- k+ n( o- F  D  W, n        theSession.EnableRedo(false);
0 v. G0 v  `+ H' @' n' R* l
" ^* Z- n( W0 J3 c9 |% R        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
& ]. m& e! K% ^  U5 F* Z2 Q& a3 u6 Y) Z
        SystemInfo(theUfSession.Part, workPart);
, p8 r! [1 o& o- D7 R6 V4 w9 U' `! x. y& J7 Y3 I  T
        try: x" K, s4 L) f. Y6 M& ~1 Z/ q
        {& l; f4 ]5 S( h5 ^2 S/ n. x' M( B
            Tag[] operTag;
5 D. b, ?3 `; I. i- X' ^9 ^            Tag setupTag = Tag.Null;
; A6 Y  B1 Z& R2 r; W. y$ ?+ j9 _) B6 b$ V+ \
            int countObject = 0;
" t. x3 \3 m+ z  V' a- W1 }' u+ {# _( k1 g- [6 W% X# n
            theUfSession.Cam.InitSession();$ Y+ K" a& D4 T( T! e
            theUfSession.Setup.AskSetup(out setupTag);
$ a7 x' v" h, v3 e/ u+ T- A" x" x; p. t% N- N
            if (setupTag == Tag.Null)
3 n. x6 f& e; n4 Y            {5 |; e. I  \2 v) t
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");
" N: z$ @- _1 \; k9 j                return;
2 E0 D; i! t% D) W            }
. y3 w0 U  w, E" Y
  T( W! A; O# f' ~  T            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);/ G: d3 m3 C6 U. `% V) d- \- z
* B9 @7 D1 g: g7 A( Y
            if (countObject == 0)
7 B+ T" }- p! q# g7 M- R0 I$ T" z5 H            {- l# t) ~0 `. g3 ?6 D+ R+ Y
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");! C6 W1 M% G/ K+ u- s
                return;. a: g2 U  W7 n$ T
            }
! ?" \# ?) l, o; x9 o) i$ x1 ^& l: W- u* y# l  P, j7 t
            for (int i = 0; i < countObject; i++)
" D+ K* e7 j- b! y: @9 p$ Y8 a1 T            {
& N8 R3 g2 v/ h+ _                int type, subType;& Q4 d" c: E) F% z

; v6 @; r+ }1 T) ]                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);, o4 O% C% t2 ~" |

* \7 O4 }* E% s1 S                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));1 ]: p& y% w7 t3 H; _; U, W

6 D" e, q5 b# V/ }- E( y( O                if (type == UFConstants.UF_machining_operation_type)
, U' A' G# x8 l0 t  U7 R                {$ o/ A7 s1 B. s4 n- c1 v
                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));
6 j( ?2 L$ C/ y9 n' d1 b, u1 r1 c5 H. }
                    GetOperationSubType(operation);
' I( O6 i6 F  E& ]9 O$ v
( b5 F; ^, S. G" c* w) s+ U% s                } /* if type */" |4 B" S+ d2 Y4 W7 x
            } /* for int i = 0 */
. T) [0 W& d& o! w1 I3 N        }  D8 t9 A1 `$ ?& q% F% U
        caTCh (NXOpen.NXException ex)3 S0 O0 o& N7 i. X
        {
3 U  D" i) _% G7 u            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
) w: k: u4 f4 o- w        }2 Q/ X! l. ?, B  E: H9 j
    }$ `9 S4 {% N$ X& g# M' |
5 M3 \# j9 l2 O* i+ e
( o6 V1 j& ~1 P+ t% z$ C
    /// <summary>
7 u7 H/ O5 e8 g2 i  n    /// Retrieve the Operation Subtype
$ |5 A+ f5 I+ n% S& \( L    /// </summary>' X- h! F5 G* [: b: U" f
    /// <param name="operation">The Operation to Query</param>
* w/ a* s( Q, T/ }' |; s  U; b4 i    /// <returns>Return the Subtype Operation</returns>2 C/ d" e1 A. o& C/ g9 b
    public static int GetOperationSubType(NXOpen.CAM.Operation operation)
$ D: l7 D1 ~$ a4 a    {
! ~% s; |. O; X/ e. y' t; Y; O        if (operation == null) return -1;
( C) v3 a* X8 N! ?2 i* v
- S2 u$ l/ Y" ~6 I        int type = 0;
2 @- T) F) p0 u1 ^3 @  c        int subType = 0;* q; P& v' j. p

9 m2 W2 _; @& a0 Q7 e- C        try
3 `! k1 o& `( k) I; P. d        {% f# @% Q9 \+ u# E: Y% G
            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);* B  |6 v! Q5 K- F4 X( F
            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
% h# _5 N6 J7 d- a5 A
6 Z4 e$ B2 ?3 \  I9 a* ]            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
1 K. ^, A: |/ F8 l            switch (subType)' i( S( s$ s# }$ [
            {
" f- j* }$ \- v: W  {) k5 P: N) \                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */5 r% P" T: x4 x: o, b' R. Y) O
                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */( D( M- S9 ^8 l' b5 F
                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */
4 W5 [' H% C9 p' u* s                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */
+ f9 q* G6 j! n9 L1 a                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */' |/ h* }& ]; g9 E: M. d! u, w
                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */) |6 k. |9 e6 q/ v6 w9 H, y4 G
                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */
6 M; [) k) W2 b% h# W                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
4 G: x3 z7 _) T; n0 l$ A8 E                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */1 S" \5 d% q1 H$ i2 L, g/ G
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */* o8 g* o( e9 ~" i9 {0 V0 W0 B
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */
/ w% q5 X, _8 \. @                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
1 @7 U* q/ [$ \; ~  G6 S* E                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */4 ~! {! s4 A8 I. W/ e  f  i
                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */9 E) r+ G5 [+ d6 U
                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */3 ?+ ^: \- X* S0 m: L
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */
  q: R( O. G% d5 u+ _: V, z0 C                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
+ m% w8 Q8 _2 w                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */* P% C- ^1 E  U) j, D6 G
                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */6 M4 P2 E% [$ ^
                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */9 ~8 `$ S1 h0 G0 v: Q/ |: B
                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */5 T: o" ^/ }2 D+ u; l
7 k+ ?  g2 i' u
                case UFConstants.UF_insp_tolerance_subtype:8 Q/ a, V2 [- ?6 [
                case UFConstants.UF_insp_path_subtype:
6 a4 ]& v, c* H                case UFConstants.UF_insp_output_subtype:7 T8 q! F" F& V; {( \7 Y* c
                case UFConstants.UF_insp_misc_subtype:
0 ]0 W, t9 x, ]* j% F# r& i3 \$ l                case UFConstants.UF_insp_align_subtype:
' P5 V" Z2 }! E( H) n- [                case UFConstants.UF_insp_sensor_subtype:
( b3 u* x  G' I6 i5 K                case UFConstants.UF_insp_construct_subtype:9 I' I) Z( F! Q6 V) @8 p: F
                case UFConstants.UF_insp_bounding_feature_subtype:
/ \$ f. u& V% s8 H; e  o5 w                case UFConstants.UF_insp_feature_subtype:
2 Z; x9 o5 o: y3 @8 ~, ~
& _+ s9 T! P3 K. I                case UFConstants.UF_mach_canned_cycle_subtype:
3 d, W4 p9 {0 v0 f  R+ \$ c
4 S3 H5 l; V. ~( H7 @                case UFConstants.UF_mach_laser_teachmode_subtype:. ~9 G# o! D+ _) l3 ^. a# C

2 B6 {; _" ]4 ~: p% y3 C6 P                case UFConstants.UF_mach_turn_roUGh_subtype:; a. w' M' G' J' }" l. r
                case UFConstants.UF_mach_turn_finish_subtype:! U. h0 d9 A" U8 D7 _
                case UFConstants.UF_mach_turn_teachmode_subtype:7 N; f8 L3 f- C0 k8 w
                case UFConstants.UF_mach_turn_thread_subtype:$ k4 c' a$ H# w& l/ }
                case UFConstants.UF_mach_turn_cdrill_subtype:
* R) o5 {. `' J# m                case UFConstants.UF_mach_turn_auxiliary_subtype:0 }, x; y# p4 ?$ G& a
                case UFConstants.UF_mach_turn_probing_subtype:% ~1 P( j' x& _
                case UFConstants.UF_mach_turn_tool_probing_subtype:
+ d7 m6 |& R* d$ O6 `8 e8 ~! a                case UFConstants.UF_mach_lathe_mc_subtype:
4 y' A3 G( z' z8 x* R. V+ w8 f9 w4 A                case UFConstants.UF_mach_lathe_ud_subtype:) B6 x# e' ]- M) K  J- X( b) S

/ P" O. z7 n8 R5 Y9 ^& R+ [                case UFConstants.UF_mach_wedm_subtype:- O# T2 Z/ i4 E. b# h: E* [
                case UFConstants.UF_mach_wedm_mc_subtype:9 e3 |! L, N+ z
                case UFConstants.UF_mach_wedm_ud_subtype:( D+ L' m/ w, H' b( F: w0 v3 V
                case UFConstants.UF_mach_mass_edit_subtype:$ E9 m2 Q# I% h$ l
                    break;
" v- X. _& S" ]) R; t0 U
4 q( e- _( F4 s, [2 }+ o5 f- \6 F                default:
$ d; I& f) F( j! c' M1 S0 k                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");& [) V$ c# X3 W
                    break;) X+ }# x$ k8 F" o4 ~, D
            }( Y$ z9 q: Y6 E* P. G/ t
        }
4 b+ a' ]* t4 x+ ?6 x4 \( X; m        catch (NXException nXException)
% w0 @  |+ w( F" U. s& B4 C. H0 G        {( ?0 Q: u. l/ \% w9 @! ^
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());
# H& K4 w+ e, U$ p, F            subType = -1;
+ ~$ L  ~, J  _1 K4 s        }
  s2 p) y7 W6 V  |9 i
6 u. ~; M) Q# U9 D, m        return subType;
  p- u0 Y$ ~8 S7 }3 \    }
: V/ n9 S* s6 _' ^) N1 C1 I- Y0 N7 v6 Z# O/ e
    /// <summary>
& @* V2 Y7 I: V& k+ r) P    /// Display System Information' P/ n7 X' m5 k! }2 Q7 [
    /// </summary>
! d1 m* X5 a3 J- p    /// <param name="uFPart">The UFPart to Query</param>8 x# e+ y3 c8 E9 ^
    /// <param name="workPart">The Work Part to Query</param>
9 `5 I" \8 j" x+ q& ?    static void SystemInfo(UFPart uFPart, Part workPart)) S* q& _1 j& `& [
    {  r- x0 i+ [5 G6 w
        try: |+ a) j  i/ O% y% ~+ _
        {
: |# _: ]/ C' G            SystemInfo sysInfo = default(SystemInfo);
* z! Y6 g8 N$ ?! @* O            theUfSession.UF.AskSystemInfo(out sysInfo);
' d7 ]7 @6 W7 R1 z0 t3 |' W( b5 B. W6 \% ?
            string partName = string.Empty;
' u  [$ _0 |8 n/ X            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);
' w0 f  K8 @" ]0 W% v1 f4 n& r8 T( M( I2 A8 L2 ]( b
            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
3 ^+ w  P) w) K3 @, ~# J            theSession.ListingWindow.WriteFullline("============================================================");. [: E% S8 C; k2 A4 O/ ~( a
            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());
, w  |; l( S& K/ c( \6 w0 w# y" s            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());* m! o6 c; `% J& r# b& {
            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);
9 D8 N2 A, e. a- J; e5 K            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());
& I5 M/ z  j7 l6 m7 e. z            theSession.ListingWindow.WriteFullline("============================================================\n\n");/ O6 D9 s8 z6 g8 a# V0 @
        }
% I- I' v7 M3 h# w& a$ D        catch (NXException nXException)  W" f2 _2 V0 x, G/ @. A
        {- V- W/ p3 m$ j% n
            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());( n  k9 U  D+ N  r4 X
        }
) X1 L5 p* ]  q' y: B    }
/ k  D0 D2 T! F9 V; X$ {" e$ Q' {- q; X( X' s9 q) [  V+ ?
    /// <summary>
( y  n- I0 h$ e* z2 _    /// Unload the Current Image- b  K8 E; B( K/ s. Q  @
    /// </summary>
& ], f- }% X. k# D) L    /// <param name="arg">String Send by NX</param>
! k) b6 c( l6 U% A( J    /// <returns>Unload Integer</returns>
" i2 a! t* n% l* }  ^    public static int GetUnloadOption(string arg)% ]. E, ~) f/ Q3 m4 x
    {$ {' b3 o& U) v% n% u" z) n
        /* Unloads the Image Immediately */
' M. r) A$ R+ a% H2 C& `* f        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);2 G' }6 Z, Q$ `2 ?2 f, A6 |
" u( _! Q" e. ?5 T& d# R- t- E, A
        /* Unloads the Image Explicitly, via an Unload Dialog */; G; M; M3 R+ Z( ^" I5 W
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
& S; w( u/ o) e1 H, t; x" p; S0 H7 d1 g# `
        /* Unloads the Image when the NX Session Terminates */3 u$ P- f8 `- ?* }$ Z
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);  _7 ^& U! q5 b) |: |
    }8 F( d* ?/ O1 O6 S: n$ K
}[/mw_shl_code]
' J. e3 c' X! Y2 k
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了