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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了" m) F4 N- F0 U' w0 i  A

) F# O% V  z+ T[mw_shl_code=csharp,true]using System;  g" r; U8 h3 V$ Q
using NXOpen;5 g' D  K4 u* D, t- K6 U
using NXOpen.UF;
6 D5 M8 Z1 T! I1 }; U( [2 Zusing NXOpen.Utilities;
- J) p) e4 q/ Y+ m& p$ susing NXOpen.CAM;
( l6 m# p( n/ a5 ~4 Z" M6 P: F; yusing NXOpen.Assemblies;  [. o' j' M: A' W  w0 U0 Y

0 B$ s; ]) T( v/ b/ s7 H8 Bstatic class GetSubTypeToOperation) E9 ~0 w. p3 s; R  U2 g5 Y1 K" [8 F
{
5 [0 a) \2 c( [; W( V    static Session theSession;
; |! L# L9 U5 W8 q& `    static UFSession theUfSession;9 ~2 _6 q; k* ^
    private static UI theUI;
  B; k5 M1 B  F0 d+ y/ d0 `& q& s: f/ a. h  f% I
    public static void Main()/ j; u5 k! s. ~+ H. {* M7 g2 [/ N7 V
    {
- L! e- m4 P- B3 }        theSession = Session.GetSession();
. a& A: o, g* [8 K* t, \# ?        theUfSession = UFSession.GetUFSession();$ D% X! N: Z- M; T! t, T9 Q
        Part workPart = theSession.Parts.Work;
' V/ s, E( ?8 m# W& b# `        theUI = UI.GetUI();
  B/ T* K1 L" T0 g3 Y3 _' N3 s
" ^; g& z! o( |2 h' @3 ~& c        if (workPart == null)) _: l0 D& g+ d; S
        {& w1 f5 I" y) Q% b( e3 Y
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");
) @4 O$ k$ [* G) G6 z( ^            return;
9 }1 ~; a" o* k        }1 |$ `% k- w- V5 u4 R" B7 K; r

) S8 ~  d, g5 @8 w( [# T        theSession.EnableRedo(false);
8 v1 C$ o1 @- s3 E# q' [2 u
* F0 g: ~; c6 {( d        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();. L6 y! e" m- Z2 r. _9 @

: Y$ D, [. A; Y/ E0 P: ?: K3 Z' V% q: f        SystemInfo(theUfSession.Part, workPart);
$ |" R! p2 f4 B$ w. l! J  i  U; ]2 |  c% m& \5 ]
        try( M' D8 M7 _. z1 C6 k$ ~! n
        {, q* \. X# k: q4 s- y8 y
            Tag[] operTag;
9 k% z, m/ K. t; b& I  M" n/ P            Tag setupTag = Tag.Null;
6 y( a1 x3 B+ Z! w0 w4 K0 e
* ^* n) X4 a  {( e" ~# c            int countObject = 0;' V; m% ~8 V4 w" }2 @
' P! ?; K4 I' S1 d# x
            theUfSession.Cam.InitSession();
+ X2 V& }. r/ K1 K  [            theUfSession.Setup.AskSetup(out setupTag);
3 f# Z4 ]( _% m* U8 E# t1 T/ h0 B& {' B4 q" }
            if (setupTag == Tag.Null)# z# O# q: g, L* w  u
            {
% s  J% C' R: z& c5 j4 |                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");
  ]% P' ?6 H' ]# W/ k: h                return;
& s6 E0 v$ V+ f, @            }
! Y: |7 e! E9 k8 F8 q
* Y# j3 c( r6 ]* E( l* R            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);- w( E' ~9 V, T) v

5 `) ]  O1 z+ a# v- Y5 ?3 u+ s% K& c9 U            if (countObject == 0)& P+ G/ U' F, F  w7 k6 o. q' K
            {
  Y6 d: L7 h' a1 C# s                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");5 ~" C8 |1 p2 J. `& P2 p
                return;
' W; U+ c6 n5 y7 Q9 J            }
/ |- C% y' Z1 m4 e9 G( G, \
% _5 Z! v/ ~, F3 L            for (int i = 0; i < countObject; i++)
$ o. p: N: S) {/ M; [5 ]# |            {, R1 a# W3 u+ z* g) _, N
                int type, subType;
3 D9 R( d' ^* e+ L" i2 Q
: D( q* H1 L9 L! f                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);7 p& f) C% t: Y) v
, v8 R1 N& ?& K& \4 J
                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));
3 M& Q9 i, y! z5 B0 ~( V9 t' V8 N1 p5 z5 u
                if (type == UFConstants.UF_machining_operation_type): C% Z* h! N& e+ S; b# T; M
                {. `" [- ~% [: i$ Y3 Q
                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));
# i* m8 K$ J( L
. y7 t6 k6 d. Z/ C1 f; H7 {3 E                    GetOperationSubType(operation);8 ]# e& {4 i0 l# k0 u
6 H, L2 [# W0 Q5 ], b' q" b
                } /* if type */* }# l7 y- d$ M& O$ j7 x
            } /* for int i = 0 */
% n$ n) q- ]! i& m4 q* Y9 w3 ~2 r        }* Y6 X' Q6 N( `6 r* {' z
        caTCh (NXOpen.NXException ex)
5 H$ o3 y0 _; e. Y- s        {
0 @' H) ]) O# v9 Y5 ^# N            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
0 P6 E+ H0 a1 a& l5 t        }
& }6 [, D! |. ~- b& C% K    }
2 \. Q; p0 b7 c* N- f
9 y2 z4 V7 ?* O6 P4 I5 o, Z7 x3 ~' I  D. q
    /// <summary>
3 O7 r2 A$ D( N. C4 _    /// Retrieve the Operation Subtype
0 }5 ~3 i( A5 _; @& R7 U    /// </summary>
7 F3 r$ f. L9 v+ ~/ y    /// <param name="operation">The Operation to Query</param>
# |0 Y( A0 R5 y    /// <returns>Return the Subtype Operation</returns>
. y4 J! v/ p8 p' c$ F5 C' S    public static int GetOperationSubType(NXOpen.CAM.Operation operation)
8 q, c$ f* c& U3 j2 t  ~5 H    {  P* O, l, _4 P' O" m  O+ ~% ?
        if (operation == null) return -1;
+ M  s) h* s  F8 w& F1 {! H, \# ^# j
% R2 ]! ~3 G+ s- x        int type = 0;3 C7 d: b' [$ L
        int subType = 0;
& n  d8 @& }. H5 \
9 v2 I7 Q# j4 w# b( g        try+ k  p" r# C6 {- H5 R  s% h! C
        {- ]6 j6 [! `+ F# `' A
            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);
/ E2 S; s; W  W3 E$ ]$ K# r6 y            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
/ i7 f9 [" J: n" A' C! f$ K+ o- Q; _5 _  j3 _) n
            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
; d2 ?. N9 ?6 {            switch (subType)
" P2 g9 U" K# N- S            {# a) e3 E% h$ Q) K* h$ h3 `2 G
                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */5 |. ~1 c/ V; t6 }/ Z) |
                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
3 P. ]! P' a( W9 i; t- P                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */
3 w$ F; p! x8 n+ v( T/ N                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */! N- G5 U( T8 H: s( E
                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */
) X3 D+ ~) h# S3 j! h- R: @) h                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */
, ]8 ~  q/ h0 y; Q- e                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */1 v  R. |% ]: d# Q
                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */  I) {" C# u6 _3 e6 ]+ u) N) I+ a
                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */
  v/ Y" o1 j. u1 ^" x                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */
( d( y" Q& h; z/ O  t! M                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */
: J/ m2 D. e/ ]8 _                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
2 R$ @7 _2 U! U8 T+ W* ^- E                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */
! b: E! B' }5 i                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */# f- Z, s* m3 V, Y- O2 D( r
                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */! R5 k! S* B7 [- g
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */: V. {# J  m7 u  @/ _3 ^9 F
                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */, C7 x, |; h+ r7 \& }8 J" K8 }, D
                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */
# S5 F; e" {! [; Z. E6 L* e                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */
' y7 l, _; c( l* ~% ?/ U% H                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */
/ x# y4 C' p% X* m) t& J                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */1 z- z8 a- u9 L0 W6 ~( {- a% h
# P- T, e, b2 s0 c8 P! y
                case UFConstants.UF_insp_tolerance_subtype:* ]. U! s1 i* k- x$ Z- A3 {
                case UFConstants.UF_insp_path_subtype:
$ _# b( s3 g7 h& v                case UFConstants.UF_insp_output_subtype:
" \; s! I8 @) P* X! x/ z1 `; s                case UFConstants.UF_insp_misc_subtype:
& w4 I. ?0 y! J; Y# D                case UFConstants.UF_insp_align_subtype:
$ c$ y4 t9 {! F& O4 E3 {# p                case UFConstants.UF_insp_sensor_subtype:
) L7 C6 I1 A4 K" C                case UFConstants.UF_insp_construct_subtype:
2 g: j2 T( U- R' S/ R( j! `( k                case UFConstants.UF_insp_bounding_feature_subtype:# W. B2 e3 W( C& b
                case UFConstants.UF_insp_feature_subtype:
$ r4 ^. S% m, O' i$ k2 p" Y% D: n$ O* F) o, n% }' @6 ~( t3 M
                case UFConstants.UF_mach_canned_cycle_subtype:/ V5 F- t( k; V9 d# _( s0 F
2 U) A% L) V* |  V* v; A, P. V
                case UFConstants.UF_mach_laser_teachmode_subtype:
. A& T" M1 _& Y/ g4 S$ }% X2 u9 V' K( a8 F
                case UFConstants.UF_mach_turn_roUGh_subtype:' Z  a. S  P' q
                case UFConstants.UF_mach_turn_finish_subtype:
1 \* |# [0 M/ \                case UFConstants.UF_mach_turn_teachmode_subtype:
% r* F! j% V; _5 S- X/ @& g4 F. m7 n                case UFConstants.UF_mach_turn_thread_subtype:
  W, V! Y2 E5 o$ t* a                case UFConstants.UF_mach_turn_cdrill_subtype:* ^6 a: j3 L$ p1 q! P- h- {
                case UFConstants.UF_mach_turn_auxiliary_subtype:- K* ]+ s) y/ N
                case UFConstants.UF_mach_turn_probing_subtype:: g' g5 x- y! F
                case UFConstants.UF_mach_turn_tool_probing_subtype:
- Q3 Q) L/ M. e4 U' W' T4 X: P. A& i                case UFConstants.UF_mach_lathe_mc_subtype:( ~- p  M0 z% U1 `: z
                case UFConstants.UF_mach_lathe_ud_subtype:
+ r, ^8 \$ ?6 E5 E) U1 N: u  l# f$ y8 ?7 r
                case UFConstants.UF_mach_wedm_subtype:
/ V0 e+ L1 v- a& Z6 |' N* c                case UFConstants.UF_mach_wedm_mc_subtype:- f7 H: w& }  C" o
                case UFConstants.UF_mach_wedm_ud_subtype:
3 x! q' J7 T) M0 M                case UFConstants.UF_mach_mass_edit_subtype:
' L2 U2 i3 m0 f7 @1 u; {                    break;
- Z9 N% V2 \8 X3 e; v' w, Z3 L. P6 d- ?9 u* m4 h
                default:4 |; C% w- R* r0 T) e
                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");2 A  N  K6 e; y% c
                    break;
2 N' q) n# I* y5 x            }9 P; o0 R/ f* L9 B$ ]0 \
        }. x- c5 G$ b. U
        catch (NXException nXException)+ T; q! `* K3 _
        {
/ f: p6 U$ \2 u1 W* c            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());. ^2 K" g5 W5 `+ L& L( ?/ W4 P- m
            subType = -1;' Y9 k, |/ K( g2 L, p" O' P3 W
        }" j4 t, |- c" b) g, K$ \" y

( v0 ^' a# R/ ~/ R0 b2 |6 G        return subType;
1 H0 k' D% g  U% {& I" A" d5 V2 Z    }: X6 o& M, w; y4 ]) R2 |

! j' w4 z& \  z/ T3 o7 {% b6 G    /// <summary># y& {* X* u9 [+ I" f. o. m
    /// Display System Information# _/ O# R1 E1 q. R+ A0 d. J, y* o
    /// </summary>
, b# |1 F% j. a' W    /// <param name="uFPart">The UFPart to Query</param>
% ?3 v! d# s2 s( _; M    /// <param name="workPart">The Work Part to Query</param>
8 O5 p  i/ r" P! O    static void SystemInfo(UFPart uFPart, Part workPart)
  ~' X- Y" `7 P' U" h" }5 z; P    {0 j! Q+ A0 h8 J
        try
0 k6 q" ?9 K  e, m2 Y+ c. }        {
# d; t4 O+ B" a            SystemInfo sysInfo = default(SystemInfo);7 f8 r0 m' J3 T/ Z/ h
            theUfSession.UF.AskSystemInfo(out sysInfo);2 C- c' K- W; q2 I% g

* `/ V- l: ^9 g  |. S            string partName = string.Empty;6 P6 J, O" p! A" `, N9 Q  O
            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);
2 {& f: a3 z3 T- v/ U/ f& i/ X0 W" S$ P; ^
            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
2 l; P6 I8 J0 C% c& a/ [            theSession.ListingWindow.WriteFullline("============================================================");
4 H$ @) `7 F/ k) a  h; D! Q4 P            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());$ |# n4 f7 f* q
            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());
  \% L9 o$ |  C            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);
3 g8 @3 {; l& }8 n2 X" y            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());5 t& U( A( i' {! ]1 M$ y8 E
            theSession.ListingWindow.WriteFullline("============================================================\n\n");
5 x* \9 ]& G8 A6 H5 \) c5 v( J        }
7 B! ?$ u9 y& Q/ W& D/ K; y& _. F        catch (NXException nXException): f5 v( s, J9 w( T3 R0 f* v- }
        {
7 w$ x1 u9 Z5 q* H, c6 h, i8 s            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());! |) V7 e% ~# Z
        }
7 X' n# v: X5 U9 ~! V; Q& s    }* h' |+ ]) N; m  n
  @+ y0 ?* `+ y$ ^
    /// <summary>* Q  D+ X& e: t- D
    /// Unload the Current Image
% `( K+ w5 V. V( J    /// </summary>
( [5 G  j0 m: P) [& q    /// <param name="arg">String Send by NX</param>
' `% W$ z% _# d    /// <returns>Unload Integer</returns>" m! [) s1 G/ `3 K) R
    public static int GetUnloadOption(string arg)
, q& }- d& C6 a: y    {, u! y( b- r+ i: S. M
        /* Unloads the Image Immediately */
) E. i* C2 c/ i; ^        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
5 j8 t6 D& D8 x8 d4 T8 o/ d- a. C! S/ H. U$ [% t' f) l$ f1 j% v
        /* Unloads the Image Explicitly, via an Unload Dialog */8 W1 h4 r3 J% r& l
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
( P4 S, I2 @+ g4 `4 m" f( F4 q5 R! u$ b. J3 J- U5 G+ ]
        /* Unloads the Image when the NX Session Terminates */% K1 q+ u5 ^0 c0 ~: z: Y
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);* H. P. s+ p+ {8 Q
    }
0 G+ S/ s- T) ~  Y+ ~, @" L% v}[/mw_shl_code]
1 }; ]1 l2 d$ q3 N
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了