zoukankan      html  css  js  c++  java
  • 实例开发-NX二次开发选平面快速分割实体工具

    本实例非博主原创,是从大神的视频课程中学习的。

    • 版本:

    NX9+VS2012

    • 演示:

    • 源代码:
    //SplitBody
    
    // Mandatory UF Includes
    #include <uf.h>
    #include <uf_object_types.h>
    
    // Internal Includes
    #include <NXOpen/ListingWindow.hxx>
    #include <NXOpen/NXMessageBox.hxx>
    #include <NXOpen/UI.hxx>
    
    // Internal+External Includes
    #include <NXOpen/Annotations.hxx>
    #include <NXOpen/Assemblies_Component.hxx>
    #include <NXOpen/Assemblies_ComponentAssembly.hxx>
    #include <NXOpen/Body.hxx>
    #include <NXOpen/BodyCollection.hxx>
    #include <NXOpen/Face.hxx>
    #include <NXOpen/Line.hxx>
    #include <NXOpen/NXException.hxx>
    #include <NXOpen/NXObject.hxx>
    #include <NXOpen/Part.hxx>
    #include <NXOpen/PartCollection.hxx>
    #include <NXOpen/Session.hxx>
    
    //头文件
    #include <uf.h>
    #include <uf_ui.h>
    #include <uf_disp.h>
    #include <uf_modl.h>
    
    // Std C++ Includes
    #include <iostream>
    #include <sstream>
    
    using namespace NXOpen;
    using std::string;
    using std::exception;
    using std::stringstream;
    using std::endl;
    using std::cout;
    using std::cerr;
    
    
    //------------------------------------------------------------------------------
    // NXOpen c++ test class 
    //------------------------------------------------------------------------------
    class MyClass
    {
        // class members
    public:
        static Session *theSession;
        static UI *theUI;
    
        MyClass();
        ~MyClass();
    
        void do_it();
        void print(const NXString &);
        void print(const string &);
        void print(const char*);
    
    private:
        Part *workPart, *displayPart;
        NXMessageBox *mb;
        ListingWindow *lw;
        LogFile *lf;
    };
    
    //------------------------------------------------------------------------------
    // Initialize static variables
    //------------------------------------------------------------------------------
    Session *(MyClass::theSession) = NULL;
    UI *(MyClass::theUI) = NULL;
    
    //------------------------------------------------------------------------------
    // Constructor 
    //------------------------------------------------------------------------------
    MyClass::MyClass()
    {
    
        // Initialize the NX Open C++ API environment
        MyClass::theSession = NXOpen::Session::GetSession();
        MyClass::theUI = UI::GetUI();
        mb = theUI->NXMessageBox();
        lw = theSession->ListingWindow();
        lf = theSession->LogFile();
    
        workPart = theSession->Parts()->Work();
        displayPart = theSession->Parts()->Display();
        
    }
    
    //------------------------------------------------------------------------------
    // Destructor
    //------------------------------------------------------------------------------
    MyClass::~MyClass()
    {
    }
    
    //------------------------------------------------------------------------------
    // Print string to listing window or stdout
    //------------------------------------------------------------------------------
    void MyClass::print(const NXString &msg)
    {
        if(! lw->IsOpen() ) lw->Open();
        lw->WriteLine(msg);
    }
    void MyClass::print(const string &msg)
    {
        if(! lw->IsOpen() ) lw->Open();
        lw->WriteLine(msg);
    }
    void MyClass::print(const char * msg)
    {
        if(! lw->IsOpen() ) lw->Open();
        lw->WriteLine(msg);
    }
    
    
    static int init_proc(UF_UI_selection_p_t select, void* user_data)
    {
        int errorCode = 0;
        int num_triples = 1;
        UF_UI_mask_t mask_triples[1] = { {UF_face_type, 0, 0}//定义选择面类型
        };
        errorCode = UF_UI_set_sel_mask(select, 
            UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,
            num_triples,
            mask_triples);
        if (errorCode == 0)
        {
            return UF_UI_SEL_SUCCESS;
        }
        else
        {
            return UF_UI_SEL_FAILURE;
        }
     }
    
    
    //------------------------------------------------------------------------------
    // Do something
    //------------------------------------------------------------------------------
    void MyClass::do_it()
    {
    
        // TODO: add your code here
    
    L10:
    
        //打开单对象选择对话框
        char sCue[] = "请选择实体的平面";
        char sTitle[] = "选择平面";
        int iScope = UF_UI_SEL_SCOPE_WORK_PART;
        int iResponse = 0;
        tag_t tObject = NULL_TAG;
        double adCursor[3];
        tag_t tView = NULL_TAG;
        UF_UI_select_with_single_dialog(sCue, sTitle, iScope, init_proc, NULL, &iResponse, &tObject, adCursor, &tView);
        if (iResponse == UF_UI_OK || iResponse == UF_UI_OBJECT_SELECTED || iResponse == UF_UI_OBJECT_SELECTED_BY_NAME)
        {
            //取消高亮
            UF_DISP_set_highlight(tObject, 0);
    
            //获得面的类型
            int face_type = 0;
            UF_MODL_ask_face_type(tObject,&face_type);
            if (face_type != UF_MODL_PLANAR_FACE)
            {
                uc1601("请选择平面!", 1);
                goto L10;//跳转回去重新选
            }
    
            //创建基准平面
            tag_t object_tags[3] = {tObject, NULL_TAG, NULL_TAG};
            int point_select[3];
            char* offset_string = "0";
            int num_dplanes = 0;
            tag_t dplane_tag[2];
            UF_MODL_create_relative_dplane(1, object_tags, point_select, 1,NULL, NULL, offset_string, &num_dplanes, dplane_tag );
    
            //从基准平面特征里拿到对象tag
            int n_eids = 0;
            tag_t* eids;
            UF_MODL_ask_feat_object(dplane_tag[0], &n_eids, &eids);
            tag_t dp_plane_tag = eids[0];
    
            //面找体tag
            tag_t body_obj_id = NULL_TAG;
            UF_MODL_ask_face_body(tObject, &body_obj_id); 
    
            //创建分割实体
            tag_t bodies[1] = {body_obj_id};
            int num_split_bodies = 0;
            tag_t* split_bodies = NULL_TAG;
            UF_MODL_split_body(1, bodies, dp_plane_tag, &num_split_bodies, &split_bodies);
    
            goto L10;//跳转回去重新选
        }
    
    }
    
    //------------------------------------------------------------------------------
    // Entry point(s) for unmanaged internal NXOpen C/C++ programs
    //------------------------------------------------------------------------------
    //  Explicit Execution
    extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
    {
        UF_initialize();//初始化
    
        try
        {
            // Create NXOpen C++ class instance
            MyClass *theMyClass;
            theMyClass = new MyClass();
            theMyClass->do_it();
            delete theMyClass;
        }
        catch (const NXException& e1)
        {
            UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
        }
        catch (const exception& e2)
        {
            UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
        }
        catch (...)
        {
            UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
        }
    
        UF_terminate();//终止
    }
    
    
    //------------------------------------------------------------------------------
    // Unload Handler
    //------------------------------------------------------------------------------
    extern "C" DllExport int ufusr_ask_unload()
    {
        return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
    }

    Caesar卢尚宇

    2021年2月13日

    作者: 阿飞

    出处: https://www.cnblogs.com/nxopen2018/>

    关于作者:......

    如有问题, 可在底部(留言)咨询.

  • 相关阅读:
    第26月第26天 Domain=AVFoundationErrorDomain Code=-11850
    第26月第25天 ubuntu openjdk-8-jdk jretty
    第26月第23天 nsobject 单例 CFAbsoluteTimeGetCurrent
    第26月第22天 iOS瘦身之armv7 armv7s arm64选用 iOS crash
    第26月第20天 springboot
    第26月第18天 mybatis_spring_mvc pom
    python中的字符数字之间的转换函数
    python if else elif statement
    python 赋值魔法
    python print import使用
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/14401302.html
Copyright © 2011-2022 走看看