zoukankan      html  css  js  c++  java
  • NX二次开发-NXOpenC++部件操作

    版本NX12+VS2015

      部件(Part)文件在某种程度上是一个数据库,包含了所有的零件几何信息,同时也包含了大量的非几何信息。

    NXOpen API提供了PartCollection集合对象和Part类对象来实现对Session中部件文件的遍历和操作,

    这些类对象的属性和成员函数可以实现NX图形界面下的所有对部件文件的访问和操作。

     ------------------------------------------------------------------------------------------------------

    • 部件文件遍历

      NX允许用户打开多个部件文件,同时可以通过NX主界面中Windows菜单项下的子菜单选择实现已打开部件文件的切换。

    NXOpen API通过PartCollection来实现对所有部件文件的遍历。

      PartCollection是Session里面所有Part的集合,通过PartCollection的迭代器(iterator)遍历所有的Part。典型的遍历代码格式如下:

    NX12+VS2015

    #include <uf_defs.h> #include <NXOpen/Session.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/ListingWindow.hxx> using namespace NXOpen; extern "C" DllExport int ufusr_ask_unload() { return (int)NXOpen::Session::LibraryUnloadOptionImmediately; } extern "C" DllExport void ufusr(char *param, int *retCode, int paramLen) { //通过Session的静态函数得到NX Session实例对象 Session *theSession = NXOpen::Session::GetSession(); //通过Parts属性得到PartCollection集合对象 PartCollection *partList = theSession->Parts(); //通过iterator迭代器遍历所有Part for (PartCollection::iterator itr = partList->begin(); itr != partList->end(); ++itr) { //转换 BasePart *processPart = *itr; //打印Part的名字 theSession->ListingWindow()->Open(); theSession->ListingWindow()->WriteLine(processPart->Name()); } } 阿飞 2021年6月7日

    API说明

    1.通过Session的静态函数得到NX Session实例对象

    2.通过Parts属性得到PartCollection集合对象

     3.通过iterator迭代器遍历所有Part

     

    演示

     ------------------------------------------------------------------------------------------------------

    • 显示部件和工作部件操作

      NX部件有两种不同的工作方式,工作部件(Work Part)和显示部件(Display Part)。

    NX图形界面上显示的部件定义为显示部件,可以通过PartCollection对象的Display属性

    得到当前NX的显示部件。工作部件只有一个,可以通过PartCollection的Work属性得到

    当前NX的工作部件。当一个部件被定义为工作部件时,其他部件将被显示为灰色。在NX

    交互模式下,只有工作部件才能进行修改编辑操作。

      通过PartCollection可以获取当前的工作部件和显示部件

    NX12+VS2015
    
    
    #include <uf_defs.h>
    
    #include <NXOpen/Session.hxx>
    #include <NXOpen/Part.hxx>
    #include <NXOpen/PartCollection.hxx>
    #include <NXOpen/ListingWindow.hxx>
    
    using namespace NXOpen;
    
    extern "C" DllExport int ufusr_ask_unload()
    {
        return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
    }
    
    extern "C" DllExport void ufusr(char *param, int *retCode, int paramLen)
    {
        //通过Session的静态函数得到NX Session实例对象
        Session *theSession = NXOpen::Session::GetSession();
    
        //获得NX显示部件
        Part *displayPart(theSession->Parts()->Display());
        
        //获得NX工作部件
        Part *workPart(theSession->Parts()->Work());
    
        //打印
        char msg[256];
        sprintf(msg, "当前显示部件为:%s
    当前工作部件为:%s", displayPart->Name().GetLocaleText(), workPart->Name().GetLocaleText());
        theSession->ListingWindow()->Open();
        theSession->ListingWindow()->WriteLine(msg);
    }
    
    阿飞
    2021年6月7日

    API说明

    1.获得NX显示部件

     2.获得NX工作部件

     3.NXString转char*

    演示

       设置新工作部件

      设置新显示部件

     ------------------------------------------------------------------------------------------------------

    • 创建新部件

      PartCollection集合类对象为开发人员提供了两种创建新Part的方式。最简单的方式

    是调用函数NewDisplay直接创建一个".prt"部件,也可以创建一个FileNew类对象,设置创建新Part对象的所有参数,

    创建一个新的Part对象。

    创建一个新的".prt部件",并设置为显示部件

    NX12+VS2015
    
    
    #include <uf_defs.h>
    #include <NXOpen/NXException.hxx>
    #include <NXOpen/Session.hxx>
    #include <NXOpen/Builder.hxx>
    #include <NXOpen/FileNew.hxx>
    #include <NXOpen/NXObject.hxx>
    #include <NXOpen/Part.hxx>
    #include <NXOpen/PartCollection.hxx>
    #include <NXOpen/Session.hxx>
    #include <NXOpen/ListingWindow.hxx>
    
    using namespace NXOpen;
    
    extern "C" DllExport int ufusr_ask_unload()
    {
        return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
    }
    
    extern "C" DllExport void ufusr(char *param, int *retCode, int paramLen)
    {
        //通过Session的静态函数得到NX Session实例对象
        Session *theSession = NXOpen::Session::GetSession();
    
      //调用函数NewDisplay直接创建一个".prt"部件,并设置为显示部件 Part
    *newPart = theSession->Parts()->NewDisplay("D:\1\2\456.prt", Part::UnitsMillimeters); //打印当前prt名字 theSession->ListingWindow()->Open(); theSession->ListingWindow()->WriteLine(newPart->Name()); } 阿飞 2021年6月7日

    API说明

    1.调用函数NewDisplay直接创建一个".prt"部件,并设置为显示部件

    演示

    FileNew类继承自Builder类,用于辅助创建Part对象。开发人员可以通过

    PartCollection对象的FileNew方法创建一个FileNew对象实例,设置创建新文件的模块、

    模板文件名称、新部件文件名称等,最后调用Commit方法生成一个新的Part文件。

    NX12+VS2015
    
    
    #include <uf_defs.h>
    #include <NXOpen/NXException.hxx>
    #include <NXOpen/Session.hxx>
    #include <NXOpen/Builder.hxx>
    #include <NXOpen/FileNew.hxx>
    #include <NXOpen/NXObject.hxx>
    #include <NXOpen/Part.hxx>
    #include <NXOpen/PartCollection.hxx>
    #include <NXOpen/Session.hxx>
    #include <NXOpen/ListingWindow.hxx>
    
    using namespace NXOpen;
    
    extern "C" DllExport int ufusr_ask_unload()
    {
        return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
    }
    
    extern "C" DllExport void ufusr(char *param, int *retCode, int paramLen)
    {
        //通过Session的静态函数得到NX Session实例对象
        Session *theSession = NXOpen::Session::GetSession();
    
        //通过PartCollection对象创建FileNew实例
        FileNew *fileNew1 = theSession->Parts()->FileNew();
    
        //设置模板文件名称
        fileNew1->SetTemplateFileName("model-plain-1-mm-template.prt");
    
        //设置创建新Part进入的模块
        fileNew1->SetApplication(FileNewApplicationModeling);
    
        //设置新Part的单位
        fileNew1->SetUnits(Part::UnitsMillimeters);
    
        //设置新Part的路径名称
        fileNew1->SetNewFileName("D:\1\2\123.prt");
    
        //设置是否使用空模板文件
        fileNew1->SetUseBlankTemplate(false);
    
        //是否将新创建的Part设置显示部件
        fileNew1->SetMakeDisplayedPart(true);
    
        //调用Commit方法创建新Part
        NXObject *nXObject1 = fileNew1->Commit();
    
        //销毁FileNew临时对象
        fileNew1->Destroy();
    
        //动态类型转换为Part类对象
        Part *newPart(dynamic_cast<Part*>(nXObject1));
    
        //打印当前prt名字
        theSession->ListingWindow()->Open();
        theSession->ListingWindow()->WriteLine(newPart->Name());
    }
    
    阿飞
    2021年6月7日

    API说明

    1.通过PartCollection对象创建FileNew实例

     

    2.公共成员函数继承自NXOpen::Buider

    演示

     ------------------------------------------------------------------------------------------------------

    • 部件文件加载

      PartCollection集合对象提供了方法从系统中加载一个Part文件得到一个Part类对象

    NX12+VS2015

    #include <uf_defs.h> #include <NXOpen/NXException.hxx> #include <NXOpen/Session.hxx> #include <NXOpen/Builder.hxx> #include <NXOpen/FileNew.hxx> #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/Session.hxx> #include <NXOpen/ListingWindow.hxx> using namespace NXOpen; extern "C" DllExport int ufusr_ask_unload() { return (int)NXOpen::Session::LibraryUnloadOptionImmediately; } extern "C" DllExport void ufusr(char *param, int *retCode, int paramLen) { //通过Session的静态函数得到NX Session实例对象 Session *theSession = NXOpen::Session::GetSession(); //打开一个已存的Part文件,加载到NX Session中,不改变当前的显示部件 NXOpen::PartLoadStatus *loadStatus; theSession->Parts()->Open("D:\1\2\model1.prt", &loadStatus); } 阿飞 2021年6月8日

    API说明

    1.打开一个已存的Part文件,加载到NX Session中,不改变当前的显示部件

    演示

    NX12+VS2015
    
    
    #include <uf_defs.h>
    #include <NXOpen/NXException.hxx>
    #include <NXOpen/Session.hxx>
    #include <NXOpen/Builder.hxx>
    #include <NXOpen/FileNew.hxx>
    #include <NXOpen/NXObject.hxx>
    #include <NXOpen/Part.hxx>
    #include <NXOpen/PartCollection.hxx>
    #include <NXOpen/Session.hxx>
    #include <NXOpen/ListingWindow.hxx>
    
    using namespace NXOpen;
    
    extern "C" DllExport int ufusr_ask_unload()
    {
        return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
    }
    
    extern "C" DllExport void ufusr(char *param, int *retCode, int paramLen)
    {
        //通过Session的静态函数得到NX Session实例对象
        Session *theSession = NXOpen::Session::GetSession();
    
        //打来一个已存的Part文件,加载到NX Session中,并设置为当前的显示部件
        NXOpen::PartLoadStatus *loadStatus;//不能加载的Part文件及错误原因
        theSession->Parts()->OpenDisplay("D:\1\2\model1.prt", &loadStatus);
    }
    
    阿飞
    2021年6月8日

    API说明

    1.打来一个已存的Part文件,加载到NX Session中,并设置为当前的显示部件

    演示

    还没写完,明天继续更新内容~

    阿飞

    2021年6月8日

    作者: 阿飞

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

    关于作者:专注NX开发、VC++开发、数据库、三维建模领域,请多多赐教!

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可留言(博客文章底部留言)咨询.

  • 相关阅读:
    linux
    python(4)
    python(4)
    python(4)–yield实现异步
    python(4)-迭代器 和 生成器
    JavaScript函数参数问题
    文字垂直居中
    Window-document-javascript
    Java的StringTokenizer类
    Web应用与Spring MVC锁session
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/14860109.html
Copyright © 2011-2022 走看看