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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了
- X+ n# ~7 c$ A# t, d( m
& s4 F9 }+ A3 X  D% S; X[mw_shl_code=csharp,true]using System;* s1 K% t5 }& \
using NXOpen;3 o0 I5 Y' U( t  t) t& a! K; R
using NXOpen.UF;
4 L5 A/ Y# N3 x; b/ n3 ~using NXOpen.Utilities;4 O6 v9 Q& i* A* \
using NXOpen.CAM;
* e7 p" D9 `$ o2 eusing NXOpen.Assemblies;
3 \" H  X5 U5 v6 A% J4 c
' m0 E% Z* [# w+ [! t' Z% z/ k( s, astatic class GetSubTypeToOperation9 C! u4 \: i. t' q7 d
{
& e' f" s  _& l, M    static Session theSession;7 v0 n$ F. B, O; R0 G
    static UFSession theUfSession;
0 V3 s. r( L7 ~' A" [' x    private static UI theUI;0 C: O! t. L; I# T
7 r5 h( ~  c7 \, h% U
    public static void Main(): ?( q  J! i& Q- J3 a
    {9 G7 A9 H: A: H- A8 j
        theSession = Session.GetSession();
  p( o! z/ c& x        theUfSession = UFSession.GetUFSession();
1 Z6 v, i, w# G8 _5 ^        Part workPart = theSession.Parts.Work;
) b1 p0 o+ ]( K, p        theUI = UI.GetUI();  z, p4 Z6 |5 ]: N- P( R8 H
/ a1 s# x6 C7 k4 m1 z
        if (workPart == null)1 p3 m) q% u1 u) ~. F( H
        {
. X- `# U2 O( \# A4 O            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");9 u4 H- e/ e. H# X
            return;3 D: X+ j# J: P
        }* m) _6 }  P$ e7 i  c$ z1 }( j

1 r7 j: ~! d8 r$ V1 s        theSession.EnableRedo(false);6 d9 Y* L4 h  C: t- w# H) h% x
7 X8 r$ {& v& v  W$ @# T: A
        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();+ a7 Z# z2 L8 U+ K' w6 O$ h& g
  \& l, V6 G5 D, J
        SystemInfo(theUfSession.Part, workPart);
: i1 v; j& ^2 u* Q" \: ]9 l5 `9 L4 P8 A
        try
9 }& ~1 z& \' j( ~  J, {9 n6 Q& E        {
/ @8 E5 c( i4 ~; c! f            Tag[] operTag;' i$ Y' `9 |% q- K/ C
            Tag setupTag = Tag.Null;
0 _1 F! o7 |" N0 [7 T" d
' p) g: H$ P! F+ B7 e; f) V            int countObject = 0;
# s5 K* I% |6 a8 K& q
4 ~& K, ?8 R5 z! N4 D" ?7 [            theUfSession.Cam.InitSession();: E6 L( u4 k# j. g
            theUfSession.Setup.AskSetup(out setupTag);
1 F% d/ Y0 q/ X0 j# @
+ g" D) G& l4 E5 Y0 G" h, ~            if (setupTag == Tag.Null)! x: c! d6 h* O8 ]: W
            {
+ B; d1 ]1 a2 L. e# e8 c4 n6 m                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");
9 h( d/ L, ~/ f$ D0 ^) S                return;
+ ~8 e6 ]' ^/ D  D            }
8 D3 r2 g7 s7 ?% ]1 Z  B) V
1 E' _  s( w+ P9 d9 m            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);
4 `$ q& x3 L4 ~6 i
0 o" \/ }/ o3 V8 m0 [/ ~4 h            if (countObject == 0)' ?% {1 O; U9 i, |1 D4 A* R
            {0 b$ I5 g# {& {1 L. Y
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");* J% A" ?8 ~+ L/ f$ A
                return;8 k* u, Q0 |( F" k+ M
            }
, \% x* S# `5 ?" \. Z0 w6 k7 y
6 f3 G9 k/ Z% [. n5 W            for (int i = 0; i < countObject; i++)& M* i* A% |; p9 {5 e- _% B* J0 |* D
            {
' ~2 a5 j9 k: d' b) B& c                int type, subType;
3 s0 L; f, X+ o: q' n' m0 u) U6 v8 j6 [7 M; P7 y  N( f$ O
                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);
9 I8 z* f& P4 h/ v: }0 u5 g6 Z8 L, F, Q- h( @& a
                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));
  W  N: ?% S. R2 S" E* x& j+ h9 s& l& a7 d8 L% p
                if (type == UFConstants.UF_machining_operation_type)- S7 I- ]8 z- S& y% @+ X$ f( K2 C
                {
: s7 G( c8 h9 L1 L% K3 _                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));* H$ q  _+ w" z
  Y) O. l' D3 `
                    GetOperationSubType(operation);
; X2 j# b9 h' f) B8 J9 _. H# K
                } /* if type */0 p7 U: e* F! F+ a
            } /* for int i = 0 */
7 B6 x: o1 O  G8 K2 v$ P        }4 o# `6 r5 h, {8 `" x' R
        caTCh (NXOpen.NXException ex)
9 S- y, M0 M( j2 O- J        {
2 t7 l3 q9 r& U            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);! f  \0 z" ?4 f3 V: {
        }
& P9 \( O: `7 g  b& x    }- u& J& r  z$ P, I+ c. l

6 P' S" p% O9 {" V$ {1 f  R
. U( ^" f/ U* G$ d- f# K. A$ _4 E3 |- |    /// <summary>! M) ^" i9 }4 U
    /// Retrieve the Operation Subtype
2 v3 Z7 w, Q( L' L& W  i4 ]    /// </summary>
' x) M  g# x; V; P    /// <param name="operation">The Operation to Query</param>
+ n" l! Q, O4 k1 R% X2 @. h    /// <returns>Return the Subtype Operation</returns>
2 y( h; v' Q) E: z4 u    public static int GetOperationSubType(NXOpen.CAM.Operation operation)
7 S  q. u$ `: x3 _! E    {
3 W5 y- i, i0 Y- `$ ]        if (operation == null) return -1;/ U' Z5 H, y! u! R1 O4 w3 R
9 t! W$ d3 e# m9 X  C0 e
        int type = 0;& H7 j9 X0 B! }& G
        int subType = 0;8 z  v+ ?0 g: o# X4 b( ]: C

2 Y% ]4 A9 w  Y8 G  \, z9 v        try* `% u1 B9 q) Q7 S% N
        {
8 X# Y: |7 ]6 k% @( H5 d. Y# K3 B            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);
3 m! \4 I0 B; U            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
4 A# _% @, L1 y
8 ]) D6 i0 }! c0 }8 r            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/# r* S0 _+ f+ Q' T2 I
            switch (subType)9 N3 q" U, e. \5 X& p
            {
3 b* s% q7 s& F; G4 N# [6 G, o                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */. j2 {( {% g, ]9 B* y- q7 c2 z
                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */8 q/ s2 ?& u7 h4 N3 v+ o  C  F
                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */
& D9 x* {* t! a, Z                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */5 l! ^5 V5 J! P$ N9 E# w
                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */6 f$ i0 J: |- Z+ w
                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */
- m/ J2 _4 F, ^; U                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */
5 P; G9 i# e- |+ y2 ^! ^5 V                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */! e0 F& ^( e; s4 L, {
                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */4 o7 _$ Z/ f) T% ?/ G" ~. W* W- A
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */
; l+ n7 t6 `! R" S: Q                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */
3 W2 j, i$ m' y& u$ X                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */) \+ q. r$ Q/ b
                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */
" l+ L# H1 |5 L                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */
/ x! a1 T9 l0 g8 o. }. z                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */# n* t  j) [+ O% G
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */
" ^! @1 I: ?1 `! Y! l                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */' o% W7 B0 m' j/ a# Q4 o
                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */6 \9 o" P. P  y7 O. E0 A: Y  [1 B
                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */) `3 y; W% e2 T' i, {8 d3 {
                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */, U/ v  |0 p& A0 p! d
                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */
# V& u& L/ `- Q- d  p
6 j/ q- e' `% {, X8 O. y2 f9 i                case UFConstants.UF_insp_tolerance_subtype:
  b, r2 y  _4 X  K. [$ e$ [                case UFConstants.UF_insp_path_subtype:
& o* A, {7 v9 M0 F                case UFConstants.UF_insp_output_subtype:
+ k; C* e% j- e; d8 `  U- c% b" ^: S                case UFConstants.UF_insp_misc_subtype:
2 n2 m8 Y. K; d- o! b4 A6 I                case UFConstants.UF_insp_align_subtype:
8 b4 a" B7 u3 \; p) u7 @$ i                case UFConstants.UF_insp_sensor_subtype:
0 P9 v- Y' X( F* Y+ L+ B' U                case UFConstants.UF_insp_construct_subtype:
7 c* g/ o, Y' C2 N- k                case UFConstants.UF_insp_bounding_feature_subtype:
- P' `7 J/ L5 ]# u3 M$ J7 M                case UFConstants.UF_insp_feature_subtype:0 i! s( \! _! B$ v5 \7 I
8 e$ }" x2 \1 Y, }; f
                case UFConstants.UF_mach_canned_cycle_subtype:
/ {2 A+ i0 q2 K. c  F. _) H/ g
1 B* C, \( ?2 d5 B: ^                case UFConstants.UF_mach_laser_teachmode_subtype:6 d2 N6 e; y% [; [( I* q9 Z

( b4 c6 V, M4 h4 S                case UFConstants.UF_mach_turn_roUGh_subtype:" q0 S- h. z- b$ w- F- E" X
                case UFConstants.UF_mach_turn_finish_subtype:9 H  p7 P& X9 c2 y
                case UFConstants.UF_mach_turn_teachmode_subtype:  N5 g6 T& I; H) b
                case UFConstants.UF_mach_turn_thread_subtype:6 h/ L! W0 y2 A4 e7 d, u
                case UFConstants.UF_mach_turn_cdrill_subtype:
2 B. A5 K5 u" I, e5 q' W: i                case UFConstants.UF_mach_turn_auxiliary_subtype:( X, U: w  R. W- D) ]' F3 E
                case UFConstants.UF_mach_turn_probing_subtype:
2 t1 D0 o! w5 k  _/ {7 o3 t                case UFConstants.UF_mach_turn_tool_probing_subtype:
1 ^  d/ {8 k6 W& W; T                case UFConstants.UF_mach_lathe_mc_subtype:
/ B: t5 x- f' I( K6 n5 ~1 b                case UFConstants.UF_mach_lathe_ud_subtype:9 a3 a6 c. H+ W' Q8 g2 k6 |
8 t6 M  {+ o/ l' D6 M+ X8 X! p: e
                case UFConstants.UF_mach_wedm_subtype:3 B4 c8 k+ T7 c2 d5 s
                case UFConstants.UF_mach_wedm_mc_subtype:: W  P, Q+ q+ ~9 U
                case UFConstants.UF_mach_wedm_ud_subtype:
7 f. U' @  _6 V( x( p: J1 e                case UFConstants.UF_mach_mass_edit_subtype:  d2 ^  H4 ^2 N+ I+ E( n1 \2 H
                    break;
' c4 v5 |" m, g& F1 ^) c
+ z1 W1 p4 j2 i( X& y                default:
7 J+ {* ]! N# Q- j8 F                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");
( e; }6 F7 E& I: G+ l                    break;
! M. f3 E+ K* \- V& d, Y4 V6 j. o4 J9 ~            }/ T1 v& g6 E, l: u! E/ s
        }
/ ?$ d2 |8 F' b8 ^        catch (NXException nXException)) H( F2 W  q' c# D  g! F6 ]
        {9 W3 L0 p& v% m- q
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());7 k  {, t* [6 x! C# t. F
            subType = -1;
7 c  [0 g6 x8 d$ n' z        }" V" b6 o0 G2 c, W( b, [
3 j5 U, y1 S4 Z* J& h
        return subType;! d. ^( q' {: l& }" R
    }2 s3 K# i! ^* x- ~" S! t

4 X+ r* Y. b5 h    /// <summary>% f4 n: u3 O2 ?& S$ r5 c, x
    /// Display System Information
+ ?/ [' \! ~) G% ~! K    /// </summary>, Z& S+ ?  r. {' r8 x# Y2 h. s
    /// <param name="uFPart">The UFPart to Query</param>
7 e5 c1 O0 n9 Z' _& M    /// <param name="workPart">The Work Part to Query</param>/ S/ S* E7 H6 @5 M, v+ V
    static void SystemInfo(UFPart uFPart, Part workPart)
. T0 p# x5 H% t# D    {
! e1 o! q! O  P: z, E$ H+ S+ n        try& u! A; d9 D' u% n  d" X3 Z
        {, e5 j' }& v7 I! z4 Y, S
            SystemInfo sysInfo = default(SystemInfo);- u3 t" v8 g( `/ g5 P
            theUfSession.UF.AskSystemInfo(out sysInfo);5 x0 h' X! m9 c3 _1 Q. Q8 C
  p0 W8 X1 j8 `, o2 M
            string partName = string.Empty;* M& Y% R% t! j+ S$ c
            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);
  H' u' ^" B' X8 g  z
) P) d, t7 }& s$ O" ]7 F. V; A3 M            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();( Q) Y* z$ H3 U* X! Y/ L
            theSession.ListingWindow.WriteFullline("============================================================");
8 e/ |: U/ D+ s  g+ y, D+ I            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());6 `; C* e8 }( h3 i6 N5 A$ j( G
            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());% m7 ]+ @/ B* p+ z7 t
            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);  d0 K4 @- @+ u5 c! P
            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());# D7 J% S9 |0 [. L8 T' b; X
            theSession.ListingWindow.WriteFullline("============================================================\n\n");
6 S) a' J( o, U" r# v5 @8 U9 {0 I        }
3 Y; Y) n/ r9 c- F! q- C        catch (NXException nXException)8 V& d/ c7 x' V( V7 Z! Y
        {
" |" p5 B/ D8 K/ v$ V9 C4 Q            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());
+ A! M$ G9 x% r( E        }" b9 u5 z3 D8 f4 J
    }
+ K- ~* U- Y/ K- U& ?/ J
7 f1 [* p' u7 V8 u: W: x9 ~    /// <summary>+ ^- ~5 G. W3 n8 f. d8 k- q/ r
    /// Unload the Current Image  o* V+ Q0 q( R0 C. E
    /// </summary>
% i; z6 ^( S; o' X# @8 m7 P2 H* H    /// <param name="arg">String Send by NX</param>
$ t8 j/ N1 z. c( R- C    /// <returns>Unload Integer</returns>
# U) @# X& i) G- ~4 k    public static int GetUnloadOption(string arg)3 j; {7 w/ ?  q) }0 I
    {
2 l$ [5 z5 O& x  W& n1 Y# ]! G; {        /* Unloads the Image Immediately */+ ]5 Z1 F- h9 M! u
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
+ Q! q5 I- \! _& F( o* Y! ~
! i5 _$ l, ]( B2 ~4 Q( K( U; v* h        /* Unloads the Image Explicitly, via an Unload Dialog */- l: ?$ H; V7 e. {1 G6 `
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);' N7 R# F# F' g- T2 c5 X; ]7 H5 I% s

/ I3 B6 Q3 A7 T        /* Unloads the Image when the NX Session Terminates */$ k7 U* z/ R% V- @
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);6 F6 Z, G5 N7 U' K( L3 m8 w
    }' A8 F  z. ^+ w& S' Q7 b
}[/mw_shl_code]
0 t, ~* l3 r( j0 T% V. E: ~% y6 U
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了