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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了
2 t7 v3 u& q0 @, H& V) S5 o8 \9 V2 L$ Z* z/ U
[mw_shl_code=csharp,true]using System;
! z4 J. A# l! v7 n: Iusing NXOpen;* }* H+ K. u. c% \  R0 Y
using NXOpen.UF;
  c) G$ K5 |. F- I% lusing NXOpen.Utilities;
* m0 }4 Y7 P8 lusing NXOpen.CAM;
0 m  X1 Y0 f) K5 Y7 D5 B4 Tusing NXOpen.Assemblies;1 [. U. C( F+ X

' h* _4 A+ M" _4 n1 q7 Dstatic class GetSubTypeToOperation
% U; X5 w' H+ i3 t; F% j6 V{
+ S; u! B" y% h( e    static Session theSession;
7 z( F! F7 i* Q9 @    static UFSession theUfSession;( l2 ^: M$ ~. x6 n, V) v
    private static UI theUI;
' R6 N1 b, w7 x3 [" W5 g. ?0 ^5 V* ?. ?: a  D2 L- s7 A# k
    public static void Main()
0 P4 J& U6 F( k; C4 B7 H% W    {
& d! P0 a8 Z+ m' Z. f0 h        theSession = Session.GetSession();9 I  F3 X. Y7 H0 O
        theUfSession = UFSession.GetUFSession();* x; f2 L- \7 \* R+ M5 f: ~7 W- y& w
        Part workPart = theSession.Parts.Work;2 N6 T& p+ u8 ~& ~4 i
        theUI = UI.GetUI();
$ M& T% Y0 |3 n" t. Y1 y. x/ W0 {
$ h* K4 y* F5 A5 g        if (workPart == null)
# N. n( l' F5 L# N: `+ `        {
3 r4 m' t, U3 E6 Y            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");- P" B8 U* T$ A. ~8 n1 m
            return;
2 {" X% T3 z: R        }9 T, A  Z/ {. Y6 W$ ?" f1 ]( U

5 z& O! j' R6 P& H* D, P- e        theSession.EnableRedo(false);
3 g& Y7 G. D. R% f) }
+ ]* @! L8 `* p  `! [/ `: F        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
3 Q: {/ k/ ^& L" h5 d
5 n& }1 h& J2 _2 o/ {        SystemInfo(theUfSession.Part, workPart);
) D, M: ]& C, q/ c2 C
* T2 g4 S' ?6 }2 i* I' w$ D        try( ^" k' K3 ^4 I5 W7 E; y
        {. t' z# Q  v" u4 q7 L
            Tag[] operTag;' g2 w. I/ H; A% q) o
            Tag setupTag = Tag.Null;
$ X* q7 S0 h( o
# y) g; F2 X9 ?0 G/ P& w            int countObject = 0;
. k5 Z+ M. q1 i, g5 a8 x
  E2 f5 Y/ j& Y7 b0 p7 Q            theUfSession.Cam.InitSession();4 `- A* m0 q- u4 ~3 m9 I
            theUfSession.Setup.AskSetup(out setupTag);& h. I: P: C. p9 M8 |
! X) q! n! W8 j
            if (setupTag == Tag.Null)
% ]4 }+ \0 O6 R7 F4 B            {* _" W# I3 c* B' \( y# d$ F
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");* f' D3 y$ H3 i$ J5 _& ^
                return;
1 y5 _* p  y6 j; s1 o  X            }
6 [7 h5 Y2 R  J: p: s' t" f6 {- T/ z+ p  e7 m
            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);
0 Q# s, }( Y* ^) \" T2 G& e! ?4 a( u
            if (countObject == 0)0 H; g0 @! S8 g7 B6 V  ]) H
            {
8 R" ^6 m( ~/ t' X, ]) O' N  |                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");* K! y  m0 t$ R! y% v. c( g
                return;) \6 u" h3 f1 I" S+ n) s' q
            }
" V( N2 ?/ o* T3 Y( `  a4 n0 H' p+ N) q) Z$ F" q* D  k
            for (int i = 0; i < countObject; i++)% [7 h4 J+ R+ ^: |3 f, a1 t
            {$ P$ |" Z; y' f. j' b7 E
                int type, subType;
7 ]2 A) i! q& R' ?: h7 J0 c% K8 u, v
                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);8 {+ Y7 l( m( C; ~; [& r6 ?6 a
; U6 f% L" f/ Z. w4 R. d
                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));3 j( z8 v* f  y+ O! s

4 g- p; a& z: A                if (type == UFConstants.UF_machining_operation_type)
8 v2 m+ c" R& O3 V                {
& Q7 g. b! F2 c& ?                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));
6 d1 J: S. L6 g
0 _$ u' v. ?# T. m0 u                    GetOperationSubType(operation);/ x1 d! I% {+ L, x
- y! }1 Y2 S7 H( Y/ d+ @
                } /* if type */2 T5 i+ [2 _8 m" `" o
            } /* for int i = 0 */
  b; w1 T& A( k9 o. ^        }7 P: d7 @9 s8 i; Q6 K& C' R
        caTCh (NXOpen.NXException ex). ^- ^4 D1 Z# ^
        {
0 |! e* Y0 B% S6 z1 @6 @4 X            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);9 I7 Z! O1 y  k7 Y6 Z! K7 Q
        }7 H& b+ O2 ?6 B# r8 f
    }5 ~  N" W! F; q% Q& W; Y

. ]3 i0 V( E3 s! j* D$ Q; f& H
7 Q0 Y, [# v* ]2 ^9 v9 m    /// <summary>; F( ~* Q4 U. U3 f) E* N6 k2 S6 e; R
    /// Retrieve the Operation Subtype
5 K8 e; T$ Q+ S5 F4 ~    /// </summary>* k4 P) I4 }- K/ P: V7 `
    /// <param name="operation">The Operation to Query</param>" y1 o- ~; Q1 ^% o' e
    /// <returns>Return the Subtype Operation</returns>( ~2 X  q) A/ ?8 R( n+ U
    public static int GetOperationSubType(NXOpen.CAM.Operation operation)1 n! v& E& Q2 d1 p; C
    {3 L2 Y/ g* ^) J" R: z- }/ X
        if (operation == null) return -1;0 v1 d2 z% E3 z- ~3 m

2 @0 ?; k  F$ y" Z4 j        int type = 0;
% _$ {: G! t! `: Q        int subType = 0;/ B% a4 x% h8 S4 d/ O
. \/ T: }4 t0 ]* H, |6 ~' I: B
        try
6 E, w! p9 R' m! T+ ~        {2 m& J% h: A2 d0 T
            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);1 Z; E1 Q+ Q# ], ]8 w2 V+ N7 j
            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
3 X! _; z2 ]9 V; _* O6 [% a5 ~1 t' ~1 o9 v
            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
9 F! f& `, ^. x& a            switch (subType)+ k. h: y3 f+ b- j* E! P
            {+ i! N3 c6 E* c# W$ |
                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */; ?5 |4 p: Q: G- h4 P, l
                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
2 i( R. b2 p$ h# {5 i' w                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */
- B" Q0 y. X* H                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */
$ l! j; D0 b- J0 o                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */1 r" |5 C6 H7 v1 c* }2 g
                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */, \6 A$ \% ~3 y( V) o) a7 Q5 n
                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */# b2 M, M$ Y( P2 q. T) t
                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
" m$ D" k, x( |0 |' f                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */
# |- n& w! M! G4 \9 w                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */& K+ q" i( P6 `+ E
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */
) l* R( I1 m+ e' b                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */1 A, O4 G' f7 o, I' r8 E* C
                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */
9 Q* ?7 s+ ~' U9 N' i                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */
9 J* M% i( l- U' v# g: N3 @                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */% @- r; `/ t% r) y
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */
! |2 k5 E: g2 n) L                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */  M8 L* m" t1 Q6 l# v( y) C% t
                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */' F  @3 v. C6 E2 P0 M7 Y" y7 e& u! {
                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */
! U1 \$ }7 R+ y2 }- y% Y' w; v  n                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */4 e5 ?& ]) U7 M
                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */' p' Z* Q5 b; K

' Y) K4 y, b# q( b1 |                case UFConstants.UF_insp_tolerance_subtype:
+ g$ s$ r3 u4 a" j, P9 s! m                case UFConstants.UF_insp_path_subtype:
0 z( t/ D% ^4 t" P( F9 K$ ~                case UFConstants.UF_insp_output_subtype:
- l# K& h( p( i: A- t& i                case UFConstants.UF_insp_misc_subtype:
5 F: r) P+ Q( F0 @4 z' y) w                case UFConstants.UF_insp_align_subtype:4 A  p, V' u: l3 w1 k. x5 F
                case UFConstants.UF_insp_sensor_subtype:
1 ?4 ]( j/ R! z% G- M                case UFConstants.UF_insp_construct_subtype:
; Q0 ~4 B: D/ K+ N8 o                case UFConstants.UF_insp_bounding_feature_subtype:
, U+ B* h, o" f1 }! E                case UFConstants.UF_insp_feature_subtype:1 l" t6 S6 r0 R9 O: S

7 p% F+ K2 L' {: M0 q8 X                case UFConstants.UF_mach_canned_cycle_subtype:9 f0 B3 i% j! T% @. C# ^
! c* \4 i, Q3 k, W; u2 |1 p
                case UFConstants.UF_mach_laser_teachmode_subtype:
4 L" F/ p# b8 e  _7 B4 D! w+ z. Y* o1 J! f/ o3 Q6 [$ g5 q
                case UFConstants.UF_mach_turn_roUGh_subtype:
3 a" R) O9 w" z" M" O# @& c                case UFConstants.UF_mach_turn_finish_subtype:
" f. r6 n& I- a: c* Q, X% t                case UFConstants.UF_mach_turn_teachmode_subtype:- w& J! r3 l' X
                case UFConstants.UF_mach_turn_thread_subtype:5 J4 J% y. y% M5 ^; w$ ?
                case UFConstants.UF_mach_turn_cdrill_subtype:
6 ?* V4 D) H/ W                case UFConstants.UF_mach_turn_auxiliary_subtype:4 ^6 k; U; h& W4 ^! R& S
                case UFConstants.UF_mach_turn_probing_subtype:
3 g2 L) g* O. d                case UFConstants.UF_mach_turn_tool_probing_subtype:
* z* t5 J5 T6 o" h+ J7 o7 g8 j                case UFConstants.UF_mach_lathe_mc_subtype:
" X9 C# }/ P- L( b, [" J( N% M                case UFConstants.UF_mach_lathe_ud_subtype:* |$ Z2 Y9 f3 E: P
. ~5 x7 l+ K$ f* Y) N8 p
                case UFConstants.UF_mach_wedm_subtype:
. R) o) `9 C" q# \  X                case UFConstants.UF_mach_wedm_mc_subtype:
- d* D) m0 P) X: u2 _6 v/ n                case UFConstants.UF_mach_wedm_ud_subtype:3 U6 T. |- R/ S- U. B+ y. L9 n" K
                case UFConstants.UF_mach_mass_edit_subtype:- {9 [2 x+ ~7 X$ |  `6 d1 m
                    break;
1 e8 m0 D0 p1 B$ I4 v
0 X' U! p" ^% v, G) U                default:% p! J' W) }; b4 M+ m2 f
                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");
& R4 {7 e( M  }* {                    break;/ x+ t; J0 T/ i  |3 H; [
            }0 [9 m! c* ]* Q9 {
        }
5 s) U2 n8 Z  M5 h' d4 |& N        catch (NXException nXException)7 Q2 _; Q" d4 I! c/ u
        {/ f( i* {  S- b- t/ t, Y
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());1 s& t, E4 y! J
            subType = -1;
) Q1 v( R; s; ?        }
9 u$ A: a9 x/ G9 }9 D5 v
& z2 l3 }" z& X0 i3 I) v/ T        return subType;6 u& e( D1 ~( y2 j" j8 H
    }5 }) T7 E( O9 q
1 _1 ~: Z& P$ L
    /// <summary>
! h( z, l, ^! V# C1 q    /// Display System Information
* q6 y/ O; F* k    /// </summary>7 N: k  l& T, u' _7 c' R3 L
    /// <param name="uFPart">The UFPart to Query</param>
5 O3 p0 e3 Y/ J0 \3 D    /// <param name="workPart">The Work Part to Query</param>
! F$ F5 V/ l3 \- L! d- K5 m    static void SystemInfo(UFPart uFPart, Part workPart)
$ K- q; ^  }; v, Q/ p3 I* P2 Q" X    {' m' ?  U/ |0 O, M9 x0 @% L& o
        try
3 n. H) \( k5 t* p! o: {3 u: I        {
* d( }2 l* q/ h* \* S            SystemInfo sysInfo = default(SystemInfo);8 n1 |  f# S' v: e  f& t) Z' \
            theUfSession.UF.AskSystemInfo(out sysInfo);) h5 J0 R: y2 m& p* \' b
3 p$ C$ Z5 h) m3 O
            string partName = string.Empty;
/ x/ w" `0 r2 v0 ]2 g7 Y; ^6 J7 P8 v            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);
  m2 a" k9 o, r
4 s' \6 L  M2 x' S1 T            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();, `( M% \* ~' c8 Z. {
            theSession.ListingWindow.WriteFullline("============================================================");+ V0 x: I: b2 G2 i  g
            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());
& A, G  ^9 ?: }# B1 k, ?' |5 s; N4 A            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());: m1 ?& ~' A5 ?6 _+ b
            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);. L  E3 H6 C% d2 q+ O8 }. T
            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());( P! D5 w* [& d+ L
            theSession.ListingWindow.WriteFullline("============================================================\n\n");
/ ^( R( B& f. _( {        }
8 @( r. }6 }: T        catch (NXException nXException)
) E8 F0 b$ e  ?4 H! |        {
/ ^7 K9 Z5 }# |0 L            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());
( _5 s5 M9 M0 H$ P. t  I; h, O        }
; s' M* l! |. S9 g+ o9 |    }
' t8 D# R) i$ m2 P+ Z
/ u& D1 u7 X+ r% ]" E$ a    /// <summary>4 k; S* I8 Y( n2 S! e# P& g
    /// Unload the Current Image& f/ \7 ~8 s7 a
    /// </summary># V+ |7 _6 d9 `- [, A7 V
    /// <param name="arg">String Send by NX</param>
7 q$ `; ^& \- m; d9 e    /// <returns>Unload Integer</returns>
. {4 a" M  [% z1 f7 U- e    public static int GetUnloadOption(string arg): O0 i6 ?- Z2 ~- J  w
    {5 {  y/ V" C8 ]& t: i$ D( H  j6 }
        /* Unloads the Image Immediately */
% Z, z1 D$ P; h: d, Y3 m5 b) J        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);# ]* N0 a  m1 K! j" V

% c+ R; J( t' E9 ?  R9 H+ ?2 ^5 h        /* Unloads the Image Explicitly, via an Unload Dialog */
% p, O3 n# U7 {        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
: u' @% P/ j& m$ ~/ o" z2 K6 m' V0 t2 q
        /* Unloads the Image when the NX Session Terminates */
) d* a# U7 A! f: B        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);7 k$ Y( b$ V  a
    }+ m' h3 m# [; l
}[/mw_shl_code]
8 x. c9 G  g. D1 T% 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二次开发专题模块培训报名开始啦

    我知道了