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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了: l9 E# y8 Q: r0 ^
+ v2 S; @; E7 f2 \! D) b
[mw_shl_code=csharp,true]using System;
7 h* F; X6 Q% `using NXOpen;( V  m. n/ ^6 y( j+ ^! S9 J$ R' X
using NXOpen.UF;
9 k/ p9 B3 J5 [6 m& husing NXOpen.Utilities;
7 @" q$ c9 b- O' O+ l9 Rusing NXOpen.CAM;
" Y7 b. e! C% T, ?using NXOpen.Assemblies;
# q% C* Q* P/ e* p
6 B+ N/ T0 {0 q, n2 \! dstatic class GetSubTypeToOperation
$ L2 z" S7 D+ C- b# r+ f5 s! R{' n/ V% |" q; W& y; y
    static Session theSession;  @: X( Z! ]" ]. @' j# u7 A: D# |
    static UFSession theUfSession;
; S0 `  Y$ {/ I( d; H8 |    private static UI theUI;) }% `9 i7 E" X6 g  W" K
- y7 s9 Z9 y& x# u( D' @: J
    public static void Main()
2 i" K5 O" F' L/ N8 A2 v    {- E. w, j. Y9 c4 }  v/ j# J" ?9 ?( E
        theSession = Session.GetSession();
5 g/ R/ s# \! E$ t1 M        theUfSession = UFSession.GetUFSession();1 s3 P0 @3 c* i# a0 d3 p
        Part workPart = theSession.Parts.Work;
! @( R5 Y* S. x$ `+ o6 i        theUI = UI.GetUI();
" |& {! [: T* v8 r9 i6 S' D) p# g
  a' ]3 _4 k* V! K, e        if (workPart == null): ^- X6 i. y4 d# `
        {
1 W& n* d$ [8 v, [; ~* i; ]            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");7 f0 O+ S- n# R* {8 j  E# t
            return;
8 `) f& [8 a/ H: n        }. R, s7 \9 ^$ N6 d% ]$ b  S1 f+ y

$ J3 p' F8 A1 _& e, g8 M        theSession.EnableRedo(false);
4 _0 o8 F1 p/ _0 P1 {, l3 R9 O. X
        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
( X& h& X+ `' _4 {* Z6 J
7 Z  B5 T) F# m0 {) U: I        SystemInfo(theUfSession.Part, workPart);
( c# R4 K& c' U( W& x5 G3 Y5 G) m/ Q+ |/ q
        try
/ ~1 q3 ~& h9 S+ v$ w; ~( L* R        {
7 Q0 C8 `* \) b$ ?6 `            Tag[] operTag;& q! A) d* X. y2 e/ J6 h
            Tag setupTag = Tag.Null;) H% ]& K8 h, e% A7 V* h! _

$ F0 M3 b( y5 Q            int countObject = 0;
7 S5 I& Z' l0 P1 J% O  O, U; q% F$ Z, a; p0 J: I
            theUfSession.Cam.InitSession();
" }( r6 l2 ?, c: q) d9 h            theUfSession.Setup.AskSetup(out setupTag);
' M$ z+ w' D/ D8 V* F( j2 H5 }- A# {* h% G
            if (setupTag == Tag.Null)( k1 q6 }7 o) d. v( Z) {5 D* a
            {
* ?% `4 w4 v. y' z" t                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");
0 ~: _& E; M2 |- @                return;
  @3 k& m- y( u$ }/ }" T            }( o* N  M" c1 p5 H( D$ X
  @5 V: o0 P* ]8 G1 K7 w6 E( u$ U: u
            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);/ v, N/ ^: O# p4 w

* R; m8 k' I1 i1 e6 J            if (countObject == 0)
. F) W2 j7 j4 g; N+ r" v            {+ Z, x: K: p- f
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");# k9 V/ X; d* F: D1 A. z- i2 [
                return;5 g* B* {8 @3 ]) T5 ]. }
            }
& d2 }; C. K* L3 Z- N% t; @3 b: p. O' }9 M( a
            for (int i = 0; i < countObject; i++)) r; ^# m+ i$ j2 Y0 ^  m# B- G; J
            {
6 i6 A: a# g0 ~. [7 y* ~: p' T. C                int type, subType;
7 W8 C! B* R# h1 u5 I$ E
) N( u5 T( Q  n! s                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);
' v& x/ s! n; G' ?# N% G- I  P
                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));4 U% ~* R' U% c: ~

/ p" @  ~  A# M! N" c                if (type == UFConstants.UF_machining_operation_type)3 T: F+ k. g2 ~0 D  x/ ?* ]& o; ]
                {: n1 F+ o9 S: u1 |0 Q! z
                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));
- K7 F( f3 g8 r
! b9 j2 L% J0 B1 [                    GetOperationSubType(operation);! d3 o% U0 N7 N) j* k& `) i
, ]& z# _6 @1 |9 L8 }/ F
                } /* if type */7 @3 [7 O2 o5 Z/ J6 \
            } /* for int i = 0 */; v6 T: ]4 D6 \; d6 m
        }
  @5 F- y! E, Q        caTCh (NXOpen.NXException ex)5 p) M) R' \  ?1 f7 x" G
        {/ \; d& N# }; x
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
6 o8 _) _0 }/ B        }. R  p, X$ f- Z% I. ~
    }' b$ O; j5 x2 E' u7 u
3 ~$ I" u/ H! J) d. `7 c! V

) i" S! n0 J+ {4 w* Y" w    /// <summary>
9 y4 j  `' H$ F! o; i8 N3 j3 Z    /// Retrieve the Operation Subtype2 }8 J4 |! G1 v$ B! e1 A3 A/ @
    /// </summary>4 j/ i/ C2 B: j# C/ Z6 g
    /// <param name="operation">The Operation to Query</param>
( `1 g4 @7 R( l; K, ?+ u" Y, }    /// <returns>Return the Subtype Operation</returns>
; V8 W0 I$ I/ @& ^1 y0 I    public static int GetOperationSubType(NXOpen.CAM.Operation operation)) l" ~7 I( d% z. U" C& V
    {
6 q) Z/ H4 ^  `9 U7 C" T        if (operation == null) return -1;, r$ w% r4 C0 b1 [

0 Y( B- s# L- `( A        int type = 0;- e1 W; `% X) E) Z8 M% d
        int subType = 0;+ l- v4 w  Z% x) H  p! `
3 c' P" b5 j# M3 H% g3 J* S& ~
        try
. T' p" Z% I$ {  }6 Y( L        {
& ?6 F$ B& i7 Q. u" v            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);: V7 u4 J# I, M
            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());' t0 |- u3 J' i

, Q+ ?7 |: p5 M  ~' p$ Q9 H1 H            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/7 x6 `/ _  M) f
            switch (subType)
. {( j+ t" x7 h( ]+ _            {
& w6 _5 `* W* G. R# a/ b6 d                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */
7 W& \+ Z4 `" B0 v) M' A$ ], F                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
5 P$ }, ]$ X* ~3 u7 B9 s: K                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */7 C5 M. i4 F" m5 i- X
                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */
9 S; B( s0 X2 o2 e                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */
/ T/ @+ z7 l( p  e                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */; i' G" K: ?1 a2 B
                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */
. r7 l% T, u$ N$ ^                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
" M6 m. W, v- [: F. O                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation *// Z( ^# Z% U. S7 \# F
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */. B: Q- u- |  g4 U9 G
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */- c! [: C9 e3 m! [: g3 T
                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */4 ~$ K% l  I, y/ A
                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */. V7 x1 n1 b6 L! S
                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */
' I) U* g! r* z; F1 ]  B; l                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */
/ R6 l+ O! g/ j, u8 p                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */- W) D- Y  k* ~% ]8 H& @# z
                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
7 ]/ V1 |; ~! G. N+ z- y; o                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */
- K( N3 I9 D+ r                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */
( s, k; y, ~% k# k! o7 ?                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */: J  T* k& l* J8 r- x. H, V5 ?
                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */
; J% O3 ~! |9 l5 z1 |" r1 Q
4 ~0 O6 T( ?; G) n) |$ D                case UFConstants.UF_insp_tolerance_subtype:# x2 E2 s: }; N
                case UFConstants.UF_insp_path_subtype:
' {5 D' t/ T$ |: E. W2 }  a                case UFConstants.UF_insp_output_subtype:+ M! G. B  l4 \, z8 g5 i$ G- D
                case UFConstants.UF_insp_misc_subtype:! \6 w8 r5 h, I6 |$ |8 d  W% v
                case UFConstants.UF_insp_align_subtype:: J  K! ~# T' D& C3 [4 v3 J
                case UFConstants.UF_insp_sensor_subtype:2 X; W: G" ?( r% h4 O: l
                case UFConstants.UF_insp_construct_subtype:
  `5 H$ Q- ~5 P! d                case UFConstants.UF_insp_bounding_feature_subtype:  `: d3 a9 o! x: S2 C: V' E
                case UFConstants.UF_insp_feature_subtype:
$ y0 `4 j. V$ ^
! s- T  ?$ |$ @5 I- j( A7 Q                case UFConstants.UF_mach_canned_cycle_subtype:
* Q$ y% ^1 g( d3 I( C
6 D2 |# S# F3 Q. u. z/ U. {' X                case UFConstants.UF_mach_laser_teachmode_subtype:
) }3 [% s/ j& v' S, n3 p4 n9 V" E/ }+ M
                case UFConstants.UF_mach_turn_roUGh_subtype:
8 \4 {, @* m$ B/ q3 k! A2 h9 I  n                case UFConstants.UF_mach_turn_finish_subtype:
3 m% r6 d! ?. q( u% K! u                case UFConstants.UF_mach_turn_teachmode_subtype:
8 g' J9 z2 ^) L                case UFConstants.UF_mach_turn_thread_subtype:
( l) c& O5 p. Y% k2 I                case UFConstants.UF_mach_turn_cdrill_subtype:
! |0 ?% b8 u0 v0 N8 G                case UFConstants.UF_mach_turn_auxiliary_subtype:# h5 S5 x# l/ C* S( _: f
                case UFConstants.UF_mach_turn_probing_subtype:/ R( x) s2 j9 A1 B/ _
                case UFConstants.UF_mach_turn_tool_probing_subtype:
9 O. L5 h4 M2 {                case UFConstants.UF_mach_lathe_mc_subtype:
7 W& A& j/ b# t& x6 n% j7 B                case UFConstants.UF_mach_lathe_ud_subtype:
5 q0 ]$ [/ `  I$ |/ _! w# m2 J  w5 i7 b
                case UFConstants.UF_mach_wedm_subtype:9 q3 ?) T2 J' W) u% W1 G
                case UFConstants.UF_mach_wedm_mc_subtype:2 B, k* m; ^: H$ z3 X9 A6 V
                case UFConstants.UF_mach_wedm_ud_subtype:
  A. Z1 {" g- w5 [" W; n                case UFConstants.UF_mach_mass_edit_subtype:& r" c1 x7 H* a
                    break;8 f0 ?% v" _6 y: S- X2 N) g8 S

4 D; N' b/ o6 n% Q9 i" T: L6 @                default:
0 O- X( k  F+ f9 v                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");/ T' }2 J$ r3 i, T/ i
                    break;
8 p: q$ s0 K" x, r1 [( r            }
5 w3 \8 N' r3 Q( _4 O/ x        }+ s1 W; B& {$ I) h; b7 u
        catch (NXException nXException)6 a4 C7 C9 |4 G
        {# ~" N3 V5 _+ ]7 U2 o
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());' e3 N; {! G' s$ z' f% V! J" ]
            subType = -1;
& _; ~1 G$ a3 O- c        }& x- c" x# [( _8 _' ?% i! T3 K

" U) T0 o% M" M0 Z* {        return subType;) n$ E6 d& P3 X2 V7 l% F
    }! q1 S% D+ c0 T# V& `, w, I% v: y9 W# M' R
  K# w4 M) c  H) C% S  B8 N
    /// <summary>
; `; k: I$ F0 v% u' r$ |0 W1 T    /// Display System Information! q) G$ k3 c, }+ s2 ]0 V' z& f
    /// </summary>5 s6 C( v1 D4 Z  x
    /// <param name="uFPart">The UFPart to Query</param>3 ?# @4 v3 g, n" ^+ R' v
    /// <param name="workPart">The Work Part to Query</param>
* i/ K, n/ P1 G. k  i* s, c    static void SystemInfo(UFPart uFPart, Part workPart)
/ X& S0 j* V1 D) n# L    {
8 u0 S, r5 \. A7 C  F        try
% `0 c$ F' Y1 u9 L7 c        {( P  t2 H# x. W2 Y- ]9 X
            SystemInfo sysInfo = default(SystemInfo);  u' R1 ]7 m, F
            theUfSession.UF.AskSystemInfo(out sysInfo);
- a2 u/ C* r/ h& F- T
* h1 m- m; O" [3 u" M- O            string partName = string.Empty;
7 {& F# a/ V5 u            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);
! x1 ~. p4 U) i3 O+ Q' U( W9 B
7 \( u& d& U8 T' A; _( C2 X            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();) g. P& f% \) r# ^# V% k
            theSession.ListingWindow.WriteFullline("============================================================");
) D1 d$ q. b- e7 M2 W$ S5 h            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());
. T/ d2 k. W- b6 A! B  j: ], x            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());" h. J1 R5 X2 C: _
            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);
! q) q" C/ B7 x            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());
4 ~* N' i) i& O7 D( u7 z  u! T            theSession.ListingWindow.WriteFullline("============================================================\n\n");; o; ^. l5 }& f7 n
        }
/ `; N" ~% g6 y5 m        catch (NXException nXException)
+ C& J! \  l0 G" X2 O" M) B0 e        {
# |8 `- D; h4 t  [( V            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());8 }9 _; R! ~5 G- G% m/ k
        }& [: i, j" h' m! b, M
    }
# O  ]$ ~; K1 U/ m$ j  c6 K
9 q1 y* s0 E& `- {8 U" T    /// <summary>- R# B  j( V4 G( A8 W
    /// Unload the Current Image
/ P8 y: q+ m, l9 O    /// </summary>) m) M( p6 ^( L: a( j
    /// <param name="arg">String Send by NX</param>
5 B" u: D0 v- i. D/ X    /// <returns>Unload Integer</returns>
# ?1 Q8 v, g5 J8 Q; t" l4 z2 {    public static int GetUnloadOption(string arg)( I; ~: J: C% b+ b& v$ p
    {; k: q- S4 k9 k8 c/ O; ^% |& h' P% @
        /* Unloads the Image Immediately *// Q# X2 z" P# |. ]
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
3 ~& j0 V2 V' M& q+ ~. ]" W4 _' g' {# k/ i
        /* Unloads the Image Explicitly, via an Unload Dialog */
/ W- Z, f: K; M4 y" o; O        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);6 x) `5 |4 i6 [: {0 s
$ A" M9 o; F- Y6 x: V
        /* Unloads the Image when the NX Session Terminates */8 k& V- E1 n& w' X8 ?2 M6 v
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);. U7 N% @. v. |4 ~% V3 w" q" Q
    }
- ?; j7 o' G' Z. a/ C  P}[/mw_shl_code]
! k0 q& L+ ?' W% V) 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二次开发专题模块培训报名开始啦

    我知道了