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

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

[复制链接]

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

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

admin 楼主

2018-6-1 09:27:40

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

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

x
NX 二次开发源码分享: 直接获取CAM对象类型 C#直接播放Jornal 就可以使用了3 }1 C9 D! w% v# e0 M
9 P% G1 X# m* P, b0 e
[mw_shl_code=csharp,true]using System;
0 d3 e8 F, B- x1 Z" X4 K4 qusing NXOpen;
" n8 g, _. M* K3 Ausing NXOpen.UF;' z$ f" }: A& F! Y! T7 P& ~, Z) q# i% D
using NXOpen.Utilities;/ |8 _# C: {+ h- k& ]1 n" q
using NXOpen.CAM;8 _( x. ?0 ?1 ~- c( M+ D# l
using NXOpen.Assemblies;
+ D1 s4 ~5 V1 u9 E' f* b& N: h( o% C( ^. w1 J
static class GetSubTypeToOperation
9 e- W7 J8 L. @{
5 K1 }, Q9 N' L    static Session theSession;
( o8 ^  S- Z0 Q. N# k8 z    static UFSession theUfSession;
1 F$ ]. t# A" j6 E    private static UI theUI;
* J8 }' r+ f- V0 y& _5 ]2 D6 L
) b* o, b# ^5 a# W. K. B, V3 Q    public static void Main()
% }0 M6 O& Y3 v' J* b1 k    {% U8 m8 P* G2 `/ Q, e* v
        theSession = Session.GetSession();
' g3 A6 k7 Y$ U+ i$ C3 a+ F        theUfSession = UFSession.GetUFSession();% v4 G" \& R' c( l( W
        Part workPart = theSession.Parts.Work;
/ i6 x7 _8 `/ |% [4 s        theUI = UI.GetUI();
" D1 y, O5 R8 i: @' w$ A
4 t0 _/ Z4 P: p3 S* @+ x        if (workPart == null)
0 Y2 A6 p- j  G9 B! f7 h; T) U        {; y% I% k* q: G. C+ i8 F& k) l
            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "A Part must be Opened.");
) ~. Z2 J7 c0 I* B5 v  J+ k) \            return;
; }7 g! Z4 o0 u        }' O& Y+ m' Q8 p4 Q; w
) y" {+ I# ?3 `4 f  X% l2 w
        theSession.EnableRedo(false);8 w9 [1 R$ L% O1 K$ Y7 j" M& t

. g/ x) |6 S1 p# C( ]; y        if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
( |% f# r4 S" k( N
- N# Z$ p: |( e% ?$ ^3 C. Q        SystemInfo(theUfSession.Part, workPart);
8 J9 a1 ?, v3 k* i  [
2 c" |* g7 v  o+ s        try$ m3 j  Q$ U* I! b
        {
. L$ J9 f/ `, }! E9 R            Tag[] operTag;' U: i, q2 G2 r' t" ~6 R
            Tag setupTag = Tag.Null;& s! g# }% V* a, w* ]: d$ x
  g; O/ w, a! a, `2 U
            int countObject = 0;
9 ?  o. U3 j# ^2 U/ ?$ N) q) d3 n5 b3 ^9 Z  A
            theUfSession.Cam.InitSession();
4 g# Q3 K8 z0 a            theUfSession.Setup.AskSetup(out setupTag);; W! G4 b8 \' x; C* u
) P. l, c) O  Q+ G" j
            if (setupTag == Tag.Null)
: }* \. U& n5 d2 A  ~            {/ f' z5 o) I# a4 d
                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "No Cam Setup in this Part.");& i9 C1 [2 C9 N
                return;, ?2 b. V9 ^$ a* P/ K
            }
! V+ H+ r+ L- ~2 _8 X$ n- H' J( p% \0 ~7 V' p" m, J
            theUfSession.UiOnt.AskSelectedNodes(out countObject, out operTag);
- \: Y& R/ N5 F$ K7 \4 S4 e- i9 D: j1 I0 a4 }8 Q
            if (countObject == 0)% d7 ~4 R. k4 ?- c, J3 U: t- K6 ~
            {
1 o$ C: N2 D0 W6 }                UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, "An Operation must be Selected.");) g  c8 u- i9 j( l) ]7 y
                return;/ i# V3 {1 K: r6 K
            }
7 ^* |4 }+ ~. B7 {
8 E0 q0 `6 J4 f% F  h2 i% f            for (int i = 0; i < countObject; i++)
& E. d% a7 R4 P: l' U            {
# z6 X; w* ^+ g  j% J4 _% o1 U                int type, subType;
2 f  y8 F6 |* d0 k' _, U/ X! Y+ h! N1 b. T* K. V3 @8 `. G8 ^1 L
                theUfSession.Obj.AskTypeAndSubtype(operTag, out type, out subType);& m5 \, \: m4 Q8 B  B

6 v6 E# O9 A1 Q                NXObject camObject = (NXObject)(NXObjectManager.Get(operTag));
, D6 d9 w) [$ v! s, N' W6 d# T# Q" L! S) y" X( y3 j% i& l8 k/ M
                if (type == UFConstants.UF_machining_operation_type)
0 D2 h; [4 W3 f% h$ o2 k2 i                {
( R7 r, ]+ h* Z  J: c6 Y1 I  ^( G                    NXOpen.CAM.Operation operation = (NXOpen.CAM.Operation)(NXObjectManager.Get(operTag));
& K: C' G8 Y3 j+ f
0 O5 j0 W3 P* n; f  G4 S' B                    GetOperationSubType(operation);( W4 I/ @$ U, a5 \
4 {* }& w) W8 K; y% f' `
                } /* if type */
, i; {2 C0 W6 f  R            } /* for int i = 0 */
, x% w  Q! i2 D3 I& ~5 s# @" Q1 q3 _        }
: _5 L, b8 Q0 e/ b        caTCh (NXOpen.NXException ex)% B  P: b2 F+ X$ K# y
        {
! H) O8 b8 f0 K& e            UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
- D" k: p$ U' V4 Q  {# v7 u        }5 [1 g1 k) G* u  G7 o$ B
    }* o9 E+ v2 W" P9 x' m( }
  k6 M+ Y% [2 ^
' u+ N2 C8 ?* H7 i8 M6 k9 {
    /// <summary>
% l3 e- t% K' B$ ?    /// Retrieve the Operation Subtype
; r! W7 L0 E7 ], W4 F8 @8 O    /// </summary>
# m3 ~' f6 ?1 @5 }5 G5 `/ q    /// <param name="operation">The Operation to Query</param>
+ H; H2 ^' x9 w; e' B, n/ @" v    /// <returns>Return the Subtype Operation</returns>
  R4 e! Z# \( D6 J# P- K- _    public static int GetOperationSubType(NXOpen.CAM.Operation operation)/ s9 G- x- L5 y* R. J$ Q7 G8 c
    {2 \% {% `7 p, y' N& e
        if (operation == null) return -1;
7 `0 E% d; A: p' y& J: G/ K$ Q: m; J* |1 K" V- J7 ~
        int type = 0;
4 T, k2 n( k  H" W        int subType = 0;$ c8 y5 M* A6 v4 B, X

2 y8 j2 M7 C1 c" H$ B; M        try  o2 T5 o' y: f8 }  i
        {) F: s" E: ], O6 s
            theUfSession.Obj.AskTypeAndSubtype(operation.Tag, out type, out subType);
7 ?+ W; w3 l# K$ i& H            theSession.ListingWindow.WriteFullline("Operation : " + operation.Name.ToString() + " -> Type " + type + " SubType : " + subType + " -> Operation GetType : " + operation.GetType().Name.ToString());
- U2 n: \* l+ k. S: x9 B& S; z  L" h9 B9 D0 d$ F, |, R8 C7 C" L
            /* If needed to Switch to Operation Subtype (uf_object_types.h)*/
/ T) W6 q$ M$ O: t( z" d7 e* A' Z            switch (subType)
/ H) d( v1 T" Y+ s, L  F0 }& n            {- b: a2 F) X5 c- n
                case UFConstants.UF_mach_pocket_subtype:                /* Planar Milling Operation */
# M5 M/ Q+ B- `* n1 C3 E3 Q1 K                case UFConstants.UF_mach_cavity_milling_subtype:        /* Cavity Milling Operation */
2 ]" y; ~* j! {$ B                case UFConstants.UF_mach_face_milling_subtype:          /* Face Milling Operation */% |3 V! V. |* n- A# W
                case UFConstants.UF_mach_zlevel_milling_subtype:        /* Z-Level Milling Operation */% y. J  @. V6 O& l. N5 a5 ^
                case UFConstants.UF_mach_groove_milling_subtype:        /* Groove Milling Operation */
1 r7 Y9 _+ e0 D) Z& |. \                case UFConstants.UF_mach_cylinder_milling_subtype:      /* Hole Milling Operation */7 R1 X0 n+ s# d0 x" w$ V( c
                case UFConstants.UF_mach_chamfer_milling_subtype:       /* Chamfer Milling Operation */
# L1 n& u; F  r                case UFConstants.UF_mach_radial_groove_milling_subtype: /* Radial Groove Milling Operation */
3 Q( J: N: x( A1 s' n. _& y                case UFConstants.UF_mach_surface_contour_subtype:       /* Fixed Contour Operation */! B/ I+ q% Z0 ~  `2 }8 O0 i
                case UFConstants.UF_mach_plunge_milling_subtype:        /* Plunge Milling Operation */2 O$ y2 A  N5 ?& _
                case UFConstants.UF_mach_vasc_subtype:                  /* Variable Contour Operation */
2 O* y2 o5 n+ F0 H                case UFConstants.UF_mach_vazl_milling_subtype:          /* Z-Level 5 Axis */0 |8 n9 {: k( ~% ^  v: j! P
                case UFConstants.UF_mach_thread_milling_subtype:        /* Thread Milling Operation */
1 }8 b9 D! R, s$ a! I3 [                case UFConstants.UF_mach_fb_hole_milling_subtype:       /* Hole Making Operation (Legacy) */
' s1 M6 g+ A9 g# T) K; o                case UFConstants.UF_mach_hole_drilling_subtype:         /* Drilling Cycle */6 f# P( z% K' L
                case UFConstants.UF_mach_mill_ud_subtype:               /* User Mill Defined Operation */
6 |1 k. k2 g. Q! v* C                case UFConstants.UF_mach_gmc_subtype:                   /* Generic Motion Operation */
% L1 v0 {1 e, P" Z* M5 \                case UFConstants.UF_mach_mill_mc_subtype:               /* Mill Control */
; X! M( R& V/ `3 n0 N                case UFConstants.UF_mach_gssm_main_op_subtype:          /* Sequential Milling */
1 C2 Z+ w4 ]5 o& S                case UFConstants.UF_mach_hole_making_subtype:           /* Hole Making (Legacy) */
! L  a$ K3 A& a: l                case UFConstants.UF_mach_planar_additive_subtype:       /* Additive */
, c, q! X% A: T6 }6 M
3 ^! H# L+ H8 [/ k                case UFConstants.UF_insp_tolerance_subtype:
9 g3 Y, j4 G; B% W8 C; w. k+ {                case UFConstants.UF_insp_path_subtype:- n( D9 l8 z3 a/ w; O7 |/ ?4 P- j" W
                case UFConstants.UF_insp_output_subtype:
; m% |+ D3 z" B6 ?1 ^! G. P                case UFConstants.UF_insp_misc_subtype:+ v6 H/ F/ K, D& \* T4 c6 @& v
                case UFConstants.UF_insp_align_subtype:
$ d1 g5 }0 e) I4 w9 M5 [0 [                case UFConstants.UF_insp_sensor_subtype:1 J' K; {1 l* u2 P
                case UFConstants.UF_insp_construct_subtype:
5 U" m$ j: q1 h: A- J6 z                case UFConstants.UF_insp_bounding_feature_subtype:  x+ o: _8 L% q- p8 p
                case UFConstants.UF_insp_feature_subtype:
( N) @; z5 y, T0 C+ l9 w
3 r6 X( t8 ~1 u7 L# k* T+ ^3 Y2 m                case UFConstants.UF_mach_canned_cycle_subtype:
5 M$ R: |9 s" I6 Y* i5 |2 Y
1 p4 r* c" ?" A  @! N& [                case UFConstants.UF_mach_laser_teachmode_subtype:
+ N6 `$ }# E" l, g) ]3 y
5 R0 P* i/ A+ L3 H                case UFConstants.UF_mach_turn_roUGh_subtype:
1 V! e& e3 }3 g% a2 Z& t* W1 `# q                case UFConstants.UF_mach_turn_finish_subtype:" V% U4 Y+ D( Z  N
                case UFConstants.UF_mach_turn_teachmode_subtype:  A0 _6 C: z! [
                case UFConstants.UF_mach_turn_thread_subtype:1 u. E9 z  B2 S5 |8 g) F
                case UFConstants.UF_mach_turn_cdrill_subtype:
3 G: O, P9 _2 I6 M. U) U% I                case UFConstants.UF_mach_turn_auxiliary_subtype:
/ }7 k7 l( v7 a% B2 D+ t                case UFConstants.UF_mach_turn_probing_subtype:( `) o7 D1 l1 d; _' [; n0 I2 p
                case UFConstants.UF_mach_turn_tool_probing_subtype:
) l. p* D; O- e, |" H; y                case UFConstants.UF_mach_lathe_mc_subtype:
; P& }. C) }% e3 l# O1 z0 [                case UFConstants.UF_mach_lathe_ud_subtype:
# @& v; V* y$ H3 N% Y/ D" @. b, K9 X; d* W
                case UFConstants.UF_mach_wedm_subtype:9 q, g" X/ \1 u* I: u1 M: E' e: f6 C
                case UFConstants.UF_mach_wedm_mc_subtype:+ D) Q2 r7 g% c; _/ G
                case UFConstants.UF_mach_wedm_ud_subtype:
4 q2 O  j7 z" T+ ^" B1 [  f8 A) Q                case UFConstants.UF_mach_mass_edit_subtype:3 R* q" _+ G1 [9 ?6 k8 v# n
                    break;: k1 m8 p$ b8 n4 U9 W( c
/ _! O. i4 Z; p
                default:" ~( x/ H' p& E7 a1 Y+ x3 T: S! J
                    theSession.ListingWindow.WriteFullline("Unknown Operation SubType.");9 N- R7 `2 X  r' l( ^  Y
                    break;
0 ~7 p: a. P3 m3 a9 `7 p            }% ^, Z$ w7 C" Y5 j6 D
        }
5 K4 J  R/ w2 v+ j        catch (NXException nXException)
, R; S8 r) c+ [8 D: q5 i% T9 B6 J' d        {
. ^4 }4 ^; @4 V% u# m  B            theUI.NXMessageBox.Show("Operation", NXMessageBox.DialogType.Error, "Unable to Query Operation. Error : " + nXException.Message.ToString());
9 N8 K: Z3 c. o7 ~$ M- W( F            subType = -1;
4 y3 v+ E, L9 R! j' |% ~        }8 P) q+ @% M7 p

1 J2 `' H  d; S1 s: m1 J. t        return subType;
1 f* E' {: d. e+ v    }1 F; q- g  c* o) Z1 H

, k4 z( i5 X" c    /// <summary>
) p8 w2 N7 d7 r" k: P+ o    /// Display System Information
9 Z8 C1 q; Z% x8 V) u    /// </summary>, z; q. Y. u3 i' g1 j) j9 N! R% J
    /// <param name="uFPart">The UFPart to Query</param>1 C5 A3 P7 \  d% j
    /// <param name="workPart">The Work Part to Query</param>
( q! ^  Q/ d$ h: m4 t; w    static void SystemInfo(UFPart uFPart, Part workPart)
; N0 g- u- o1 K    {( p0 U6 W2 `6 }( @
        try3 o: T, s: s( f8 |
        {
8 @8 Y) k( y7 z3 G$ y! Q2 v            SystemInfo sysInfo = default(SystemInfo);, U- p7 Z5 ^! b6 U# g7 F
            theUfSession.UF.AskSystemInfo(out sysInfo);( p7 M* o  _( e5 y5 [
- a$ n9 z. O8 R! n; L, b# k
            string partName = string.Empty;
- ~' h* a4 j0 O            if (workPart != null) uFPart.AskPartName(workPart.Tag, out partName);+ t+ G2 D% a8 S! ?
+ i5 b' b4 @) {6 w! m
            if (!theSession.ListingWindow.IsOpen) theSession.ListingWindow.Open();
* w  `; @/ j( c( G3 a            theSession.ListingWindow.WriteFullline("============================================================");# e$ D5 F  Q1 n
            theSession.ListingWindow.WriteFullline("Information Listing Created by : " + sysInfo.user_name.ToString());
$ }3 c+ I% A% d+ X' ~3 D7 s: M            theSession.ListingWindow.WriteFullline("Date                           : " + sysInfo.date_buf.ToString());( f* z  |( E% S' P( g
            if (workPart != null) theSession.ListingWindow.WriteFullline("Current Work Part              : " + partName);" I3 b- t  e* d6 H
            theSession.ListingWindow.WriteFullline("Node Name                      : " + sysInfo.node_name.ToString());0 ?# p4 C4 {- ^+ K
            theSession.ListingWindow.WriteFullline("============================================================\n\n");; T) A* u2 w9 D" p1 s9 E
        }
4 H/ {- U2 c% g" P' k/ Y) x2 X8 k: Y9 k        catch (NXException nXException)
5 R1 e  [, i5 J: g5 b7 V        {
& N+ L9 F: J" X7 h- _: k/ D            theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.Error, "Unable to Display System Information. Error : " + nXException.Message.ToString());
  T+ I. ?; D; b* D1 Y  r        }1 |2 }' Q: p5 v, }
    }
' ?9 S. z; G5 u2 O8 P; J& j' c4 H; R+ C
- ?! e' v- @- M) q3 P$ ]2 t* ]    /// <summary>8 [1 y! z( k, [, |) l7 u4 s, X
    /// Unload the Current Image$ z3 `- Y. r& ~1 m" l, _
    /// </summary>
: D, e9 @: x! p+ N    /// <param name="arg">String Send by NX</param>
5 G5 i: A; a0 U) S/ m& ^) E    /// <returns>Unload Integer</returns>
* B: W1 E; `+ u: }# v7 [    public static int GetUnloadOption(string arg); Z  F  d! L. o- V# M* r$ W' B2 l
    {
# t0 g* O2 Y7 O) s' D( [  b        /* Unloads the Image Immediately */
8 }! c6 b6 V+ l        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
) w  s  _2 g2 J2 `- G# X) h* K* I  m. l. K2 Y$ [. U
        /* Unloads the Image Explicitly, via an Unload Dialog */
  y2 |+ T7 c. W7 b: X        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);7 |/ h& j' e: d  ?2 D
4 f* O, z+ n% {$ p0 v+ F! J& I
        /* Unloads the Image when the NX Session Terminates */9 M' m" y- H+ y; B. f% e$ V$ ^
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);# i( L  A) s. A* I
    }
/ ?2 E/ Z; J1 V2 n# [3 K}[/mw_shl_code]
+ E4 n6 J' r( M( _' P8 L3 Y* W
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了