其他分享
首页 > 其他分享> > NX二次开发-一个简单的连接曲线例子剖析学会如何使用NXOPEN做二次开发

NX二次开发-一个简单的连接曲线例子剖析学会如何使用NXOPEN做二次开发

作者:互联网

事情的原油~

昨天晚上半夜12点半。失眠睡不着觉,看到我龙哥哥在QQ群里问问题,他想做一个连接曲线的功能,但是连接曲线的UFUN函数不会用,

录制连接曲线NXOPEN代码又不会录制,自己在谷歌上面找的一段代码,抄下来用,还真的做出来了。不得不佩服人家出国找资料的能力,

抄代码也是一绝啊,总比一些人成天吵着百度搜不到资料,资料太少要厉害的多。

但是他只会抄,也不会改,曲线的颜色也不是他想要的。

自己不会改颜色,跟他唠了一会,我觉得蛮好玩的,龙哥这人挺有意思的,我们同姓算是本家兄弟,虽然不是很熟。

就写了个例子给他。也记录一下录NXOPEN代码,改代码,转换等问题。

这个例子很简单,但是却很好的运用各种类型的转换。很多人不会用NXOPEN绝对不是因为不会录代码而不会,

绝大部分应该都是录出来的代码太多,不会改代码,不会数据类型转换,不会和UFUN结合传参。

而卡在门外,只默默的用UFUN。(其实我会用NXOPEN也是用了两年的时间,录得多了,看的多了,好像也就知道怎么改了,一开始也只会UFUN,

而且复杂的UFUN函数结构体啥的还不会用,到现在了,他妈的,也还是不咋会,反正稀里糊涂的吧)

例子中的所有代码,在帮助文档里全部可以查的到出处。

 

NX11+VS2013

 

创建连接曲线,并设置颜色。

UFUN C + NXOPEN C++ 混合编程

 

 

//Demo

// 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_modl.h>
#include <uf_obj.h>
#include <uf_disp.h>
#include <uf_curve.h>

#include <NXOpen/Edge.hxx>
#include <NXOpen/EdgeTangentRule.hxx>
#include <NXOpen/Features_Block.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/ScRuleFactory.hxx>
#include <NXOpen/NXObjectManager.hxx>
#include <NXOpen/Features_JoinCurves.hxx>
#include <NXOpen/Features_JoinCurvesBuilder.hxx>
#include <NXOpen/Preferences_ObjectPreferences.hxx>
#include <NXOpen/Preferences_PartObject.hxx>
#include <NXOpen/Preferences_PartPreferences.hxx>
#include <NXOpen/Spline.hxx>
#include <NXOpen/DisplayManager.hxx>
#include <NXOpen/DisplayModification.hxx>
#include <NXOpen/DisplayableObject.hxx>

// 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_solid_type, UF_all_subtype, UF_UI_SEL_FEATURE_ANY_EDGE }//定义选择边类型
    };
    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
    
    UF_initialize();

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);

        //创建连接曲线
        NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL);
        NXOpen::Features::JoinCurvesBuilder *joinCurvesBuilder1;
        joinCurvesBuilder1 = workPart->Features()->CreateJoinCurvesBuilder(nullNXOpen_Features_Feature);
        joinCurvesBuilder1->SetDistanceTolerance(0.001);//设定距离公差
        joinCurvesBuilder1->SetAngleTolerance(0.050000000000000003); //设定角度公差
        //返回收集要连接的输入曲线的部分
        joinCurvesBuilder1->Section()->SetDistanceTolerance(0.001);
        joinCurvesBuilder1->Section()->SetChainingTolerance(0.00095);
        joinCurvesBuilder1->Section()->SetAngleTolerance(0.050000000000000003);
        joinCurvesBuilder1->Section()->SetAllowedEntityTypes(NXOpen::Section::AllowTypesOnlyCurves);
        //传入选择的边tag
        NXOpen::Edge *edge1(dynamic_cast<NXOpen::Edge *>(NXOpen::NXObjectManager::Get(tObject)));//此处修改边的tag
        NXOpen::Edge *nullNXOpen_Edge(NULL);
        NXOpen::EdgeTangentRule *edgeTangentRule1;
        edgeTangentRule1 = workPart->ScRuleFactory()->CreateRuleEdgeTangent(edge1, nullNXOpen_Edge, true, 0.050000000000000003, false, false);
        joinCurvesBuilder1->Section()->AllowSelfIntersection(true);
        std::vector<NXOpen::SelectionIntentRule *> rules1(1);
        rules1[0] = edgeTangentRule1;
        NXOpen::NXObject *nullNXOpen_NXObject(NULL);
        NXOpen::Point3d helpPoint1(0, 0, 0);
        joinCurvesBuilder1->Section()->AddToSection(rules1, edge1, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen::Section::ModeCreate, false);
        NXOpen::NXObject *nXObject1;
        nXObject1 = joinCurvesBuilder1->Commit();//生成连接曲线特征

        //将joinCurvesBuilder1生成的NXObject对象转换成Features::JoinCurves连接曲线对象(它是特征类型,并不是曲线类型),怎么转换的,录制得出来的(方式1)
        NXOpen::Features::JoinCurves *joinCurves1(dynamic_cast<NXOpen::Features::JoinCurves *>(nXObject1));

        ////另外一种录制出来的转换方式(方式2)
        //NXOpen::Features::JoinCurves *joinCurves1(dynamic_cast<NXOpen::Features::JoinCurves *>(workPart->Features()->FindObject(nXObject1->JournalIdentifier())));
        
        //返回连接曲线特征创建的曲线对象(NXOPEN方式)
        std::vector< NXOpen::NXObject *>spline = joinCurves1->GetEntities();

        //提取连接曲线特征里的曲线对象(UFUN方式)
        int num_curves = 0;
        tag_t *feature_curves = NULL_TAG;
        UF_CURVE_ask_feature_curves(joinCurves1->Tag(), &num_curves, &feature_curves);

        //输出类型
        int type, subtype = 0;
        UF_OBJ_ask_type_and_subtype(spline[0]->Tag(), &type, &subtype);

        //获得首选项-对象-样条的颜色(这一步有点多此一举了,首选项是什么颜色,创建出来的样条就应该是什么颜色,不需要第二次在设置曲线颜色的,这里只是例子需要)
        int color_num = workPart->Preferences()->ObjectPreferences()->GetColor(Preferences::PartObject::ObjectTypeSpline);

        ////设置曲线颜色(UFUN)
        //UF_OBJ_set_color(spline[0]->Tag(), color_num);

        //设置曲线颜色(NXOPEN)
        NXOpen::DisplayModification *displayModification1;
        displayModification1 = theSession->DisplayManager()->NewDisplayModification();
        displayModification1->SetApplyToAllFaces(true);
        displayModification1->SetApplyToOwningParts(false);
        displayModification1->SetNewColor(186);//颜色号码
        std::vector<NXOpen::DisplayableObject *> objects1(1);
        //转换
        NXOpen::Spline *spline1(dynamic_cast<NXOpen::Spline *>(joinCurves1->FindObject(spline[0]->JournalIdentifier())));
        objects1[0] = spline1;
        displayModification1->Apply(objects1);
        delete displayModification1;

        //释放
        UF_free(feature_curves);

        goto L10;//跳转回去重新选
    }



    UF_terminate();

}

//------------------------------------------------------------------------------
// Entry point(s) for unmanaged internal NXOpen C/C++ programs
//------------------------------------------------------------------------------
//  Explicit Execution
extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
    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.");
    }
}


//------------------------------------------------------------------------------
// Unload Handler
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
    return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
}


阿飞
2021年8月29日

 

录制连接曲线→数据类型转换→对曲线设置颜色

 

挑重点的写,怎么搭建环境,怎么加头文件,怎么用UFUN函数,怎么录NXOPEN代码,不在文章写的范围。

1.录制连接曲线命令

        //创建连接曲线
        NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL);
        NXOpen::Features::JoinCurvesBuilder *joinCurvesBuilder1;
        joinCurvesBuilder1 = workPart->Features()->CreateJoinCurvesBuilder(nullNXOpen_Features_Feature);
        joinCurvesBuilder1->SetDistanceTolerance(0.001);//设定距离公差
        joinCurvesBuilder1->SetAngleTolerance(0.050000000000000003); //设定角度公差
        //返回收集要连接的输入曲线的部分
        joinCurvesBuilder1->Section()->SetDistanceTolerance(0.001);
        joinCurvesBuilder1->Section()->SetChainingTolerance(0.00095);
        joinCurvesBuilder1->Section()->SetAngleTolerance(0.050000000000000003);
        joinCurvesBuilder1->Section()->SetAllowedEntityTypes(NXOpen::Section::AllowTypesOnlyCurves);
        //传入选择的边tag
        NXOpen::Edge *edge1(dynamic_cast<NXOpen::Edge *>(NXOpen::NXObjectManager::Get(tObject)));//此处修改边的tag
        NXOpen::Edge *nullNXOpen_Edge(NULL);
        NXOpen::EdgeTangentRule *edgeTangentRule1;
        edgeTangentRule1 = workPart->ScRuleFactory()->CreateRuleEdgeTangent(edge1, nullNXOpen_Edge, true, 0.050000000000000003, false, false);
        joinCurvesBuilder1->Section()->AllowSelfIntersection(true);
        std::vector<NXOpen::SelectionIntentRule *> rules1(1);
        rules1[0] = edgeTangentRule1;
        NXOpen::NXObject *nullNXOpen_NXObject(NULL);
        NXOpen::Point3d helpPoint1(0, 0, 0);
        joinCurvesBuilder1->Section()->AddToSection(rules1, edge1, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen::Section::ModeCreate, false);
        NXOpen::NXObject *nXObject1;
        nXObject1 = joinCurvesBuilder1->Commit();//生成连接曲线特征

说说上面这段代码是怎么来的

1.找到连接曲线命令,录制一下它

录出来的代码如下,有一大堆,看着头疼,所以很多人不愿意用NXOPEN,没有UFUN简洁

// NX 11.0.2.7
// Journal created by Administrator on Sun Aug 29 13:23:58 2021 中国标准时间

//
#include <uf_defs.h>
#include <NXOpen/NXException.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/Builder.hxx>
#include <NXOpen/Edge.hxx>
#include <NXOpen/EdgeTangentRule.hxx>
#include <NXOpen/Features_Block.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/Features_JoinCurvesBuilder.hxx>
#include <NXOpen/GeometricUtilities_CurveOptions.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/ScRuleFactory.hxx>
#include <NXOpen/Section.hxx>
#include <NXOpen/SelectionIntentRule.hxx>
#include <NXOpen/Session.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)
{
    NXOpen::Session *theSession = NXOpen::Session::GetSession();
    NXOpen::Part *workPart(theSession->Parts()->Work());
    NXOpen::Part *displayPart(theSession->Parts()->Display());
    // ----------------------------------------------
    //   Menu: 插入(S)->派生曲线(U)->连结(J)...
    // ----------------------------------------------
    NXOpen::Session::UndoMarkId markId1;
    markId1 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, NXString("\345\274\200\345\247\213", NXString::UTF8));
    
    NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL);
    NXOpen::Features::JoinCurvesBuilder *joinCurvesBuilder1;
    joinCurvesBuilder1 = workPart->Features()->CreateJoinCurvesBuilder(nullNXOpen_Features_Feature);
    
    joinCurvesBuilder1->SetDistanceTolerance(0.001);
    
    joinCurvesBuilder1->SetAngleTolerance(0.050000000000000003);
    
    theSession->SetUndoMarkName(markId1, NXString("\350\277\236\347\273\223\346\233\262\347\272\277 \345\257\271\350\257\235\346\241\206", NXString::UTF8));
    
    joinCurvesBuilder1->Section()->SetDistanceTolerance(0.001);
    
    joinCurvesBuilder1->Section()->SetChainingTolerance(0.00095);
    
    joinCurvesBuilder1->Section()->SetAngleTolerance(0.050000000000000003);
    
    joinCurvesBuilder1->Section()->SetAllowedEntityTypes(NXOpen::Section::AllowTypesOnlyCurves);
    
    NXOpen::Session::UndoMarkId markId2;
    markId2 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, "section mark");
    
    NXOpen::Session::UndoMarkId markId3;
    markId3 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NULL);
    
    NXOpen::Features::Block *block1(dynamic_cast<NXOpen::Features::Block *>(workPart->Features()->FindObject("BLOCK(1)")));
    NXOpen::Edge *edge1(dynamic_cast<NXOpen::Edge *>(block1->FindObject("EDGE * 1 * 3 {(0,0,100)(0,50,100)(0,100,100) BLOCK(1)}")));
    NXOpen::Edge *nullNXOpen_Edge(NULL);
    NXOpen::EdgeTangentRule *edgeTangentRule1;
    edgeTangentRule1 = workPart->ScRuleFactory()->CreateRuleEdgeTangent(edge1, nullNXOpen_Edge, true, 0.050000000000000003, false, false);
    
    joinCurvesBuilder1->Section()->AllowSelfIntersection(true);
    
    std::vector<NXOpen::SelectionIntentRule *> rules1(1);
    rules1[0] = edgeTangentRule1;
    NXOpen::NXObject *nullNXOpen_NXObject(NULL);
    NXOpen::Point3d helpPoint1(0.0, 47.283993560949256, 100.0);
    joinCurvesBuilder1->Section()->AddToSection(rules1, edge1, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen::Section::ModeCreate, false);
    
    theSession->DeleteUndoMark(markId3, NULL);
    
    theSession->DeleteUndoMark(markId2, NULL);
    
    NXOpen::Session::UndoMarkId markId4;
    markId4 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXString("\350\277\236\347\273\223\346\233\262\347\272\277", NXString::UTF8));
    
    theSession->DeleteUndoMark(markId4, NULL);
    
    NXOpen::Session::UndoMarkId markId5;
    markId5 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXString("\350\277\236\347\273\223\346\233\262\347\272\277", NXString::UTF8));
    
    NXOpen::NXObject *nXObject1;
    nXObject1 = joinCurvesBuilder1->Commit();
    
    theSession->DeleteUndoMark(markId5, NULL);
    
    theSession->SetUndoMarkName(markId1, NXString("\350\277\236\347\273\223\346\233\262\347\272\277", NXString::UTF8));
    
    joinCurvesBuilder1->Destroy();
    
    // ----------------------------------------------
    //   Menu: 工具(T)->操作记录(J)->停止录制(S)
    // ----------------------------------------------
}

下面说哪些代码是要的,哪些是不要的

先看官方文档的介绍(C井的,对C艹一样适用,看懂原理就行了)

文档资料出处https://www.cnblogs.com/nxopen2018/articles/11664518.html

 一大堆,看不懂也没关系,能看懂多少就看懂多少。有个认知就行了。

回到我们的例子当中来

凡是带UndoMarkId的全部是不要的

 删完也就剩下这些了,还是有不少

 格式都是固定的了,直接抄过去到项目里。

我们要改的就一个地方,就是传参的地方。就这两句

我们录制命令的时候,是直接选择的边,但是它把块都给录上了。变成了先找块,在找块的某一边。

下面我们开始修改代码,块对我们是不需要的,我们只要对话框选边,传参边的tag就行了。

NXOpen::Edge *edge1(dynamic_cast<NXOpen::Edge *>(NXOpen::NXObjectManager::Get(tObject)));//此处修改边的tag

就这一句,就可以转换。tObject为边的tag,意思是将UFUN的边tag转换成NXOPEN的Edge类型(边),在NX二次开发中,不同的对象,都有自己不同的类型。这就涉及到了类型转换的问题。

转换传什么参数。它要什么,你传什么就行了,上面录制的时候,不是都已经看到了,FindObject里面要的是EDGE对象类型,那你就把你的UFUN边的tag给进去。

中间转换用NXOPEN的这个方法,注意要加头文件 #include <NXOpen/NXObjectManager.hxx>

 下面关于各种类型的一些简单介绍,在我们的例子当中,录制出来的是生成连接曲线特征,想要设置颜色只能对曲线设置颜色,不能对特征设置颜色,所以我们还要提取出特征里的曲线。

 2.类型转换,将连接曲线特征对象转换成曲线对象

        //将joinCurvesBuilder1生成的NXObject对象转换成Features::JoinCurves连接曲线对象(它是特征类型,并不是曲线类型),怎么转换的,录制得出来的(方式1)
        NXOpen::Features::JoinCurves *joinCurves1(dynamic_cast<NXOpen::Features::JoinCurves *>(nXObject1));

        ////另外一种录制出来的转换方式(方式2)
        //NXOpen::Features::JoinCurves *joinCurves1(dynamic_cast<NXOpen::Features::JoinCurves *>(workPart->Features()->FindObject(nXObject1->JournalIdentifier())));
        
        //返回连接曲线特征创建的曲线对象(NXOPEN方式)
        std::vector< NXOpen::NXObject *>spline = joinCurves1->GetEntities();

        //提取连接曲线特征里的曲线对象(UFUN方式)
        int num_curves = 0;
        tag_t *feature_curves = NULL_TAG;
        UF_CURVE_ask_feature_curves(joinCurves1->Tag(), &num_curves, &feature_curves);

        //输出类型
        int type, subtype = 0;
        UF_OBJ_ask_type_and_subtype(spline[0]->Tag(), &type, &subtype);

上面这段NXOPEN转换代码是怎么来的,将做详细介绍,UFUN的那段就不介绍了,现成的API翻帮助就行了。

怎么转换的,我也不知道,反正不知道,那就继续录制呗。

方法1连续录制

录制出来的代码如下,你会发现又是一大堆,实质我们需要的就两段

// NX 11.0.2.7
// Journal created by Administrator on Sun Aug 29 13:54:41 2021 中国标准时间

//
#include <uf_defs.h>
#include <NXOpen/NXException.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/Builder.hxx>
#include <NXOpen/DisplayManager.hxx>
#include <NXOpen/DisplayModification.hxx>
#include <NXOpen/DisplayableObject.hxx>
#include <NXOpen/Edge.hxx>
#include <NXOpen/EdgeTangentRule.hxx>
#include <NXOpen/Features_Block.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/Features_JoinCurves.hxx>
#include <NXOpen/Features_JoinCurvesBuilder.hxx>
#include <NXOpen/GeometricUtilities_CurveOptions.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/ScRuleFactory.hxx>
#include <NXOpen/Section.hxx>
#include <NXOpen/SelectionIntentRule.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/Spline.hxx>
#include <NXOpen/Update.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)
{
    NXOpen::Session *theSession = NXOpen::Session::GetSession();
    NXOpen::Part *workPart(theSession->Parts()->Work());
    NXOpen::Part *displayPart(theSession->Parts()->Display());
    // ----------------------------------------------
    //   Menu: 插入(S)->派生曲线(U)->连结(J)...
    // ----------------------------------------------
    NXOpen::Session::UndoMarkId markId1;
    markId1 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, NXString("\345\274\200\345\247\213", NXString::UTF8));
    
    NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL);
    NXOpen::Features::JoinCurvesBuilder *joinCurvesBuilder1;
    joinCurvesBuilder1 = workPart->Features()->CreateJoinCurvesBuilder(nullNXOpen_Features_Feature);
    
    joinCurvesBuilder1->SetDistanceTolerance(0.001);
    
    joinCurvesBuilder1->SetAngleTolerance(0.050000000000000003);
    
    theSession->SetUndoMarkName(markId1, NXString("\350\277\236\347\273\223\346\233\262\347\272\277 \345\257\271\350\257\235\346\241\206", NXString::UTF8));
    
    joinCurvesBuilder1->Section()->SetDistanceTolerance(0.001);
    
    joinCurvesBuilder1->Section()->SetChainingTolerance(0.00095);
    
    joinCurvesBuilder1->Section()->SetAngleTolerance(0.050000000000000003);
    
    joinCurvesBuilder1->Section()->SetAllowedEntityTypes(NXOpen::Section::AllowTypesOnlyCurves);
    
    NXOpen::Session::UndoMarkId markId2;
    markId2 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, "section mark");
    
    NXOpen::Session::UndoMarkId markId3;
    markId3 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NULL);
    
    NXOpen::Features::Block *block1(dynamic_cast<NXOpen::Features::Block *>(workPart->Features()->FindObject("BLOCK(1)")));
    NXOpen::Edge *edge1(dynamic_cast<NXOpen::Edge *>(block1->FindObject("EDGE * 1 * 2 {(100,0,100)(50,0,100)(0,0,100) BLOCK(1)}")));
    NXOpen::Edge *nullNXOpen_Edge(NULL);
    NXOpen::EdgeTangentRule *edgeTangentRule1;
    edgeTangentRule1 = workPart->ScRuleFactory()->CreateRuleEdgeTangent(edge1, nullNXOpen_Edge, true, 0.050000000000000003, false, false);
    
    joinCurvesBuilder1->Section()->AllowSelfIntersection(true);
    
    std::vector<NXOpen::SelectionIntentRule *> rules1(1);
    rules1[0] = edgeTangentRule1;
    NXOpen::NXObject *nullNXOpen_NXObject(NULL);
    NXOpen::Point3d helpPoint1(57.771598781542366, 0.0, 100.0);
    joinCurvesBuilder1->Section()->AddToSection(rules1, edge1, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen::Section::ModeCreate, false);
    
    theSession->DeleteUndoMark(markId3, NULL);
    
    theSession->DeleteUndoMark(markId2, NULL);
    
    NXOpen::Session::UndoMarkId markId4;
    markId4 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXString("\350\277\236\347\273\223\346\233\262\347\272\277", NXString::UTF8));
    
    theSession->DeleteUndoMark(markId4, NULL);
    
    NXOpen::Session::UndoMarkId markId5;
    markId5 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, NXString("\350\277\236\347\273\223\346\233\262\347\272\277", NXString::UTF8));
    
    NXOpen::NXObject *nXObject1;
    nXObject1 = joinCurvesBuilder1->Commit();
    
    theSession->DeleteUndoMark(markId5, NULL);
    
    theSession->SetUndoMarkName(markId1, NXString("\350\277\236\347\273\223\346\233\262\347\272\277", NXString::UTF8));
    
    joinCurvesBuilder1->Destroy();
    
    // ----------------------------------------------
    //   Menu: 编辑(E)->对象显示(J)...
    // ----------------------------------------------
    // ----------------------------------------------
    //   Dialog Begin 颜色
    // ----------------------------------------------
    NXOpen::Session::UndoMarkId markId6;
    markId6 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, "Edit Object Display");
    
    NXOpen::DisplayModification *displayModification1;
    displayModification1 = theSession->DisplayManager()->NewDisplayModification();
    
    displayModification1->SetApplyToAllFaces(true);
    
    displayModification1->SetApplyToOwningParts(false);
    
    displayModification1->SetNewColor(186);
    
    std::vector<NXOpen::DisplayableObject *> objects1(1);
    NXOpen::Features::JoinCurves *joinCurves1(dynamic_cast<NXOpen::Features::JoinCurves *>(nXObject1));
    NXOpen::Spline *spline1(dynamic_cast<NXOpen::Spline *>(joinCurves1->FindObject("CURVE 1")));
    objects1[0] = spline1;
    displayModification1->Apply(objects1);
    
    int nErrs1;
    nErrs1 = theSession->UpdateManager()->DoUpdate(markId6);
    
    delete displayModification1;
    // ----------------------------------------------
    //   Menu: 工具(T)->操作记录(J)->停止录制(S)
    // ----------------------------------------------
}

就这两段,转换方法,录制给带出来了,我们只要改里面传的参数就行了

    NXOpen::Features::JoinCurves *joinCurves1(dynamic_cast<NXOpen::Features::JoinCurves *>(nXObject1));
    NXOpen::Spline *spline1(dynamic_cast<NXOpen::Spline *>(joinCurves1->FindObject("CURVE 1")));

我们发现nXObject1实际就是上面我们一开始录制连接曲线最后

        NXOpen::NXObject *nXObject1;
        nXObject1 = joinCurvesBuilder1->Commit();//生成连接曲线特征

生成的那个对象。这里它是个曲线特征对象,你可以拿UF_OBJ_ask_type_and_subtype去打印下它的类型

        int type, subtype = 0;
        UF_OBJ_ask_type_and_subtype(nXObject1->Tag(), &type, &subtype);

是#define UF_feature_type                         205类型

这段就是说把nXObject1转换成NXOpen::Features::JoinCurves *joinCurves1对象类型。因为上面JoinCurvesBuilder->Commit返回的是NXObject对象

NXOpen::Features::JoinCurves *joinCurves1是连接曲线特征类型

在Features类里

 得到了Features::JoinCurves *joinCurves1类型后,我们还要去帮助里面翻这个类里的函数方法,

看有没有获得这个曲线特征(Feature)里样条曲线对象(Spline)的,这里可能会有人问为啥是Spline类型,

而不是line或者circle类型。

答案就是上面我们录制出来的代码就是Spline对象类型,说明它就是Spline。

不相信的话,还可以自己拿移刀去查看下。

 

 下面去找获得连接曲线特征里的样条曲线对象方法

 就是这个

 其实我也不知道是这个,反正是挨个get去测试的,打印输出,刚好到它这里,输出的就是spline类型

下面直接拿它返回的NXObject去用UFUN设置颜色就行了

        //将joinCurvesBuilder1生成的NXObject对象转换成Features::JoinCurves连接曲线对象(它是特征类型,并不是曲线类型),怎么转换的,录制得出来的(方式1)
        NXOpen::Features::JoinCurves *joinCurves1(dynamic_cast<NXOpen::Features::JoinCurves *>(nXObject1));

        //返回连接曲线特征创建的曲线对象(NXOPEN方式)
        std::vector< NXOpen::NXObject *>spline = joinCurves1->GetEntities();

        //输出类型
        int type, subtype = 0;
        UF_OBJ_ask_type_and_subtype(spline[0]->Tag(), &type, &subtype);

        ////设置曲线颜色(UFUN)
        //UF_OBJ_set_color(spline[0]->Tag(), color_num);

如果不想用UFUN去设置颜色,想用NXOPEN设置颜色也可以的。

        //设置曲线颜色(NXOPEN)
        NXOpen::DisplayModification *displayModification1;
        displayModification1 = theSession->DisplayManager()->NewDisplayModification();
        displayModification1->SetApplyToAllFaces(true);
        displayModification1->SetApplyToOwningParts(false);
        displayModification1->SetNewColor(186);//颜色号码
        std::vector<NXOpen::DisplayableObject *> objects1(1);
        //转换
        NXOpen::Spline *spline1(dynamic_cast<NXOpen::Spline *>(joinCurves1->FindObject(spline[0]->JournalIdentifier())));
        objects1[0] = spline1;
        displayModification1->Apply(objects1);
        delete displayModification1;

这段代码也是录制出来的,只要把转换的那一句传参数改掉就行了,

 重点是NXOpen::Spline *spline1(dynamic_cast<NXOpen::Spline *>(joinCurves1->FindObject("CURVE 1")));这句

里面传入的是CURVE对象类型

我们上面std::vector< NXOpen::NXObject *>spline = joinCurves1->GetEntities();返回的就是curve对象类型

spline是归属于curve的,line,arc等都是属于curve类型的。

我们拿这个参数值去传入进去

NXOpen::Spline *spline1(dynamic_cast<NXOpen::Spline *>(joinCurves1->FindObject(spline[0]->JournalIdentifier())));

至于为什么->JournalIdentifier()这样写,也是出自帮助的。

我们看上面joinCurves1->FindObject("")括号里写的是曲线对象的名字,直接find就是找到了,拿过来用。

而->JournalIdentifier()返回的也是个字符串对象的名字

 

 没办法,英语不会,软肋,全程靠翻译。

 

方法2单段录制,跟上一种差不多。

就是先录连接曲线的代码,然后停掉,

在开始录制修改颜色的代码,录它类型转换,find对象的代码。

要的就是数据类型,和输入输出参数。

    NXOpen::Features::JoinCurves *joinCurves1(dynamic_cast<NXOpen::Features::JoinCurves *>(workPart->Features()->FindObject("JOIN_CURVE(54)")));

这句和上面那句是等价的,用哪个都可以的。

 更改传参方式,也是直接nXObject1->JournalIdentifier得到字符串的名字传入进去

        ////另外一种录制出来的转换方式(方式2)
        //NXOpen::Features::JoinCurves *joinCurves1(dynamic_cast<NXOpen::Features::JoinCurves *>(workPart->Features()->FindObject(nXObject1->JournalIdentifier())));

 讲到这里这个例子也就结束了,

用NXOPEN的要点就是

1.会录代码

2.会删除没用不需要的代码

3.数据类型转换,更改传参的值,就跟你自己封装的函数,输入的参数是int还是double数据类型一回事,录出来的代码要啥对象类型,你就传啥

不知道怎么强制转换,没关系,那就去录代码。

 

几种转换方式

1.遇到FindObject的就传入用->JournalIdentifier()

2.遇到NXOPEN转UFUN的就直接->tag

3.遇到UFUN转NXOPEN的就直接NXOpen::NXObjectManager::Get(tag)

 

相关参考资料

https://www.cnblogs.com/nxopen2018/p/10957445.html

https://www.cnblogs.com/nxopen2018/category/2021548.html

 

阿飞

2021年8月29日

 

标签:Features,曲线,joinCurvesBuilder1,theSession,NX,NXOPEN,二次开发,NXOpen,include
来源: https://www.cnblogs.com/nxopen2018/p/15202379.html