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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了
; @( B. ~  n+ |, X3 o! A1 y  X- r7 N# ~( d* o& W% W) m
[mw_shl_code=csharp,true]using System;* w/ k" [& c' O+ ~  c
using NXOpen;8 |/ l. ^& D2 N% B1 `5 e
using NXOpen.UF;; {) N& u: r+ N. w7 y, y( l& C
using NXOpen.Utilities;
# j: j& R  f- J0 Eusing NXOpen.CAM;
2 k9 s& z6 Y# yusing NXOpen.Assemblies;2 d- Z4 V* O9 b& d/ L4 X

. `" D7 r. h; x. z: c  wstatic class GetSubTypeToOperation
  i2 v- c' V, r* P+ b4 t& R& E6 K: ~{
( S$ ^; ]# e& u/ k    static Session theSession;
  e5 X! U/ f9 |! f    static UFSession theUfSession;0 t3 k; u  @2 y! t# n
    private static UI theUI;
/ T# b& w( B7 d3 t) S! f+ G# K2 y7 t" e& B( ^* r
    public static void Main()
/ N& V9 s+ R, M3 B( {* c9 t    {$ W. w+ |2 ?' Q$ Q$ p
        theSession = Session.GetSession();
; O9 y5 O/ A9 i+ O        theUfSession = UFSession.GetUFSession();0 U/ P) ~; K1 E$ K6 C' J
        Part workPart = theSession.Parts.Work;
& Z* Z$ @( b, \% \0 N        theUI = UI.GetUI();; m" j1 m0 K- O9 T. Y# W

# Y' Z1 P5 ~3 T+ y        if (workPart == null)
+ S/ U1 p$ q  O7 T8 t        {; f: M- u5 o- j9 N2 W% m  P# l$ E
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");, r2 H9 g6 T& R
            return;
% T" K  }/ P& z3 w. z        }  E0 F1 y3 C. s

: d3 W- j* i: j( U        theSession.EnableRedo(false);
8 D- l* I' `/ `3 @. p- x/ W, |7 K- Z4 q0 I. `% C# t
        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
! m/ F5 U* L3 V0 Z7 x( {0 T3 K
* Q; h: v4 C# r4 j        SystemInfo(theUfSession.Part, workPart);
2 w1 ^0 E- b% m5 G4 o
( n1 ~5 H, k7 |+ T* C2 J+ f6 i- [0 J        try3 e) |. r' O( j) N/ J( U
        {& }6 [6 D8 V- G# B
            Tag[] operTag;: x: l( B1 C$ C, N0 ]
            Tag setupTag = Tag.Null;
  h2 @3 ?6 R. e2 ?5 d
9 S/ s& X# S1 _+ l# Z; i5 i            int countObject = 0;
, G& `8 X: z' K0 n4 L0 `  X3 _7 _% U
            theUfSession.Cam.InitSession();- Y* V$ w9 c, p
            theUfSession.Setup.AskSetup(out setupTag);4 y& |: x6 G5 F. C# |. J. t9 G" J
7 W+ c; d- T6 f) o
            if (setupTag == Tag.Null)
8 G0 ?2 j+ b0 l9 e: ^4 Q  \            {
! b/ V, |3 i5 U                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");; D+ Q" [' ^) e. D; ^2 X
                return;4 u* `% X7 g; E8 t$ I% _
            }% b1 K) c- M/ m0 M

+ y6 d0 A% }# F: }4 A* P            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);
7 a7 g3 p9 K0 W9 v6 d0 ^
& F# [0 e+ ~) J" [3 e            if (countObject == 0)9 `3 F2 K8 U; @6 o5 `$ L/ z4 {
            {
' I. x$ C' r9 X  \  F, r                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");& [4 c; E; O4 @# N  p- B7 `/ o3 ^
                return;
8 ^# [2 |+ Q8 a0 X            }
( K  o0 |: e! ^( p% B- h) a* I6 }6 |1 b2 M: Z6 I& n! S
            for (int i = 0; i < countObject; i++)
. c# i2 w1 {' L* U            {
* {5 h: V: v& N9 @/ Q                int type, subType;
; a! v2 W! ?9 Z3 v) L7 P/ m2 `9 z: L/ w
                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);
5 j0 @- p/ g; r- I3 b7 q' [
0 w( `1 }/ {6 H# w                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));- O, ]) _7 N( L- o" n

/ E3 W8 L+ d  {7 \3 f4 F                if (type == UFConstants.UF_machining_operation_type)
6 t. E6 S" q) [& _8 F2 D8 P                {+ c$ ^8 }3 o4 ]4 z+ [
                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));; p4 Q  {( U* `; c" }; l! X

0 P; D0 f" b7 Y$ p9 M1 q                    GetOperationSubType(operation);4 k4 Z9 a1 H" w
8 l) b3 f' F- c; r
                } /* if type */
# J" \8 D. o$ b            } /* for int i = 0 */
) D1 \2 |; |6 z6 f- D# a/ P5 Z# G+ j        }& t' M3 _% T- `$ X8 R8 @
        caTCh (NXOpen.NXException ex)/ c7 @  `( Q! V# Y$ E& p. C1 Y
        {
' f8 Q$ _; g; a  h1 t: D5 k            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
( }  g+ a* v$ E) z. a" P) E        }
9 S" Y6 b" K2 k( J+ |    }
/ P" N, u1 ~" g% p) L' p. r+ g" @: U0 X. Q8 P1 B
! ~5 X1 K2 m+ M; R, g( C8 P
    /// <summary>5 {  ?0 V9 P/ M" ]/ P. G
    /// Retrieve the Operation Subtype6 }- E" T0 Q3 \3 Z* Y$ Y2 ^. }. ]3 Q
    /// </summary>0 O! s( @6 F: F# X1 V
    /// <param name="operation">The Operation to Query</param>
0 H  I! }2 Y- E; O  O, c2 ~    /// <returns>Return the Subtype Operation</returns>
; K7 A2 Q* V$ W8 R$ w8 s    public static int GetOperationSubType(NXOpen.CAM.Operation operation)- `- o- c9 L8 C0 K+ w/ s/ U
    {
/ C  v( z: r* _% s7 M5 D        if (operation == null) return -1;
7 B$ g# K- N$ Z  q; k( J0 ^3 f+ z& a. B; Q
        int type = 0;2 o1 w" j, ?0 ^5 v; L$ t# d0 {
        int subType = 0;7 A/ h$ X) B. `. K  ~" J4 a
$ ^3 {+ _/ A$ y# D; f' F+ |" e  n. R
        try
' R- ~* b# W* \) Y. ]' U8 d; r3 X        {
, R" j8 q; g) _( ]2 \/ k- ~: A            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);
  N6 g4 Z5 d! \" N4 q- m            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
" E4 C+ ~. }4 w) @! e6 l' @& d3 A) U+ {
            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
( e2 Y; _1 B, d7 `5 h4 G            switch (subType)
$ ]; x2 p  A1 c% p, b            {# [4 {1 D7 L: _- @, {! O
                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */3 _3 A% O* `$ M" r1 S# k; g
                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */% g* q- h, I1 O1 F& T, p1 c( s9 W
                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */. R; d* Z1 c  ^4 n
                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */
4 y# a# h0 W$ ]/ h1 d* C) b& o                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */; l6 [! t  X4 u# p
                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */- x6 p: t+ u, E. H$ v1 o
                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */
& Y+ [8 K0 Y3 _5 }2 j1 F5 f                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
6 ?# h7 E, A$ K4 E9 S) g" V                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */  ]  z+ @% L7 j) J
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */" C8 ]- _4 n7 O1 A& ~, l1 G0 J# w
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */! Q9 U( k8 V: Z0 t/ Q
                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */
) H- `. h$ P) v2 s                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */
4 x; Z9 ]; U% D: w  z& f                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */2 Y* v1 G' Y4 r/ |3 S& J
                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */# v1 Q9 A# O+ s, y* k# f
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */' ?9 v. r: u4 p7 `6 b
                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */) i0 l) y8 x# B5 _1 `) n  [
                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */. x% q/ Q5 ?1 p, b
                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */  }: G$ k1 ?# }+ _- e3 C3 X
                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */# D" E+ t: w# U
                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */
/ M9 }+ u% A0 G& Y  ^
% h4 M/ l$ |; F' s" M9 a& N                case UFConstants.UF_insp_tolerance_subtype:" g' e; }% Q" ~0 W
                case UFConstants.UF_insp_path_subtype:( q2 X: x: K  t* z9 `3 D2 m5 w
                case UFConstants.UF_insp_output_subtype:
) j0 n9 p6 V. @& v+ T  ^2 Z# d8 u* s8 n                case UFConstants.UF_insp_misc_subtype:
8 Y, {  R8 k; i/ m2 ]: l                case UFConstants.UF_insp_align_subtype:
6 r$ s) v4 b# I) Q8 R1 T                case UFConstants.UF_insp_sensor_subtype:/ h2 t7 ^: {9 ], n0 X9 X
                case UFConstants.UF_insp_construct_subtype:/ `+ C4 m" ~# J9 ]; o5 ?' \$ T
                case UFConstants.UF_insp_bounding_feature_subtype:
7 E0 h' o; u7 i4 R9 F# n9 u                case UFConstants.UF_insp_feature_subtype:
3 x# T' Z3 w' u) n* s* M( w
, ]8 V) D9 v$ W# d1 g6 W                case UFConstants.UF_mach_canned_cycle_subtype:
7 @0 [4 l. u2 c  Z) K; |3 F
! U! ?6 \% l2 j6 ?                case UFConstants.UF_mach_laser_teachmode_subtype:
. z( K/ {5 }  {% C2 f1 H* P8 O
4 Q! F5 ]" q$ ~1 z- w4 E# }- W                case UFConstants.UF_mach_turn_roUGh_subtype:
# v$ T" i3 |9 J6 v# K2 j                case UFConstants.UF_mach_turn_finish_subtype:
- O6 B! r/ N4 I9 `; ~                case UFConstants.UF_mach_turn_teachmode_subtype:
- z0 d1 @+ |# M- c* |8 L% }                case UFConstants.UF_mach_turn_thread_subtype:  o9 c  w9 R1 m3 L4 c5 P
                case UFConstants.UF_mach_turn_cdrill_subtype:
) ]3 \+ M  |$ ?6 g                case UFConstants.UF_mach_turn_auxiliary_subtype:
4 K/ g3 X* `; s# V0 O* f                case UFConstants.UF_mach_turn_probing_subtype:' T2 A# ~* _* d$ g! E5 |
                case UFConstants.UF_mach_turn_tool_probing_subtype:
7 Q2 T( g# V. Q  v! e- a; R  Y                case UFConstants.UF_mach_lathe_mc_subtype:
4 H* {2 B( r% {0 Z8 f. X0 u                case UFConstants.UF_mach_lathe_ud_subtype:6 b& H5 V( B/ N( R$ J: v

0 \0 h. O; s  E7 l9 l2 @! x                case UFConstants.UF_mach_wedm_subtype:1 Q4 {: Q! k  x5 X+ Q
                case UFConstants.UF_mach_wedm_mc_subtype:
" b/ b  Q6 r8 `; r                case UFConstants.UF_mach_wedm_ud_subtype:
2 H( G; D9 `4 {                case UFConstants.UF_mach_mass_edit_subtype:
9 |  e2 ?. Y$ O. q                    break;
: r* E% T2 F3 }8 K
* ]  Z; m1 z$ i' a3 F% [3 [                default:
; h3 K4 M( _% A" k                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");
  K% ?7 v2 c9 t9 k7 z+ q- ]7 ^                    break;6 m8 |5 i' F9 r% y5 b7 Y- }% V
            }
3 w4 n8 s+ T: q2 f6 @        }
, c2 t3 E. u5 n) @* l        catch (NXException nXException)
4 A+ {/ t+ {4 p$ E8 ~        {4 }' F5 |. m& Y9 L
            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());
8 E. v2 k/ W8 p3 C5 R' B4 ~0 w            subType = -1;! l2 }3 G. X4 Y( S5 f* I/ J! J5 u
        }
% |. @- @& \: e' l! x% N5 [& i( ]6 x6 M" G4 f4 L# z
        return subType;! v% S( Y  M2 e0 L# a2 ^( ?
    }3 m: O/ `$ {' ?+ k/ t
% O* J1 E1 [7 s2 `$ g, I
    /// <summary>/ i, N( ^; s6 L
    /// Display System Information
& L8 M- {2 k7 K: V8 }    /// </summary>( E# J/ [: `5 Z: _, `1 |2 R" s4 W
    /// <param name="uFPart">The UFPart to Query</param>2 o$ A1 Z! i/ L8 y
    /// <param name="workPart">The Work Part to Query</param>
% _( n; D* k: B: R: i& k    static void SystemInfo(UFPart uFPart, Part workPart). l! V" X/ `" ^" I
    {3 U/ ?+ ]# {1 ~' W0 E
        try
; j7 l' t* Q! f& u9 `        {
( p6 M1 r4 L3 q5 `4 j            SystemInfo sysInfo = default(SystemInfo);
: W- c, {) o  R* F) c            theUfSession.UF.AskSystemInfo(out sysInfo);
! k: T3 a+ |, j# {$ v4 F; g
& f7 h* n  B& e  C+ T* r            string partName = string.Empty;7 W2 Y6 M6 F1 N5 i2 t/ m  C8 _
            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);: T3 y2 b# F; I; [
' C( o% ^2 A4 i: x
            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();/ l0 `5 W% O5 H
            theSession.ListingWindow.WriteFullline("============================================================");, y/ a. ]. d! |8 Z6 L
            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());
9 G; G0 H: Q! \9 p/ b2 a3 U" l            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());5 l$ M: F$ s: z
            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);
& ~5 p5 v  }+ @! j" D9 [            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());, x( a9 V6 a* B0 ^
            theSession.ListingWindow.WriteFullline("============================================================\n\n");
( F( A/ }* v" ^        }
2 K0 a( B' f& f/ f, l- z) \# D        catch (NXException nXException)
$ B$ |8 G' w  |        {
5 P9 o, X+ K% }/ t2 \# `            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());
4 R, M0 R# w* s- c! j        }
5 N9 y$ C0 m) X+ ~7 o$ C; P& n3 R# j    }
( k" D  z7 h, m- L6 q. P6 u' B7 Z* u; h3 f4 n/ L
    /// <summary>
6 _6 D7 `7 R/ x; |" ]! Q. ~7 f    /// Unload the Current Image1 J/ X) q! }* i# H9 ~" p# g
    /// </summary>8 C3 ^# g- P1 f
    /// <param name="arg">String Send by NX</param>
$ B  k# a5 `+ c4 ?% |) R+ S    /// <returns>Unload Integer</returns>& P+ j) E# h- }7 A/ f3 [/ w
    public static int GetUnloadOption(string arg)
  Q- Y; U/ c; k% p/ W1 h. f    {
. p0 P! p0 W9 |& K- U$ a4 N0 z! \  m& }        /* Unloads the Image Immediately */( W0 g/ |& U6 `& b
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
% @# F% n# r5 u( \( F- ~  O
( _7 a" Y- v7 b' I/ _        /* Unloads the Image Explicitly, via an Unload Dialog */; q% I( j1 r7 q: C& v0 ~. z
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
, i6 t9 `( U3 [7 y; r
! Y: y3 ]! s: r/ `3 k- S. u; ]4 J$ z3 z        /* Unloads the Image when the NX Session Terminates */
- n1 k! k1 ~# n* l0 M" |% v6 x. d6 i. i        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);4 H  ~9 Q& p* E+ g8 r& }
    }
6 F  _8 N3 N& c+ O7 S}[/mw_shl_code]+ }2 c6 T3 b% M- X9 {% l
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了