NX二次开发-UF_EVAL_ask_arc()获得圆弧中心UF_EVAL_ask_arc()

//GetArcCenter

// Mandatory UF Includes
#include <uf.h>
#include <uf_modl.h>
#include <uf_obj.h>
#include <uf_eval.h>
#include <uf_ui.h>
#include <uf_object_types.h>

// Internal Includes
#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Session.hxx>

// Std C++ Includes

using namespace NXOpen;
using std::string;
using std::exception;
using std::stringstream;
using std::endl;



//------------------------------------------------------------------------------
// 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:
	BasePart *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()->BaseWork();
	displayPart = theSession->Parts()->BaseDisplay();
	
}

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




//------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{

	// TODO: add your code here
//获得实体tag值
    tag_t object = NULL_TAG;
    UF_MODL_ask_object(UF_solid_type, UF_solid_body_subtype, &object);

//通过体获得边
    uf_list_p_t edgeList;
    UF_MODL_ask_body_edges(object, &edgeList);

    int count = 0;
    UF_MODL_ask_list_count(edgeList, &count);

    UF_UI_open_listing_window();
    for (int i = 0; i < count; ++i)
    {
        tag_t edgeTag = NULL_TAG;
        UF_MODL_ask_list_item(edgeList, i, &edgeTag);
        int edgeType = 0;
        UF_MODL_ask_edge_type(edgeTag, &edgeType);

        if (UF_MODL_CIRCULAR_EDGE == edgeType)
        {
            UF_EVAL_p_t evaluator;
            UF_EVAL_initialize(edgeTag, &evaluator);//使用UF_EVAL_ask_arc()函数需要先进行初始化

            UF_EVAL_arc_t arcCoords;
            UF_EVAL_ask_arc(evaluator, &arcCoords);//绝对坐标系下的中心点

            double ori[3] = { 0.0 };
            ori[0] = arcCoords.center[0];
            ori[1] = arcCoords.center[1];
            ori[2] = arcCoords.center[2];

            /*UF_CURVE_arc_t arcCoords;
            UF_CURVE_ask_arc_data(edgeTag, &arcCoords);//该函数同样可获得中心点,只是坐标系不同(我也不太理解,希望可以留言解惑,谢谢)
            ori[0] = arcCoords.arc_center[0];
            ori[1] = arcCoords.arc_center[1];
            ori[2] = arcCoords.arc_center[2];*/

            char msg[256];
            sprintf_s(msg, "中心坐标为:\nX:%.0f\nY:%.0f\nZ:%.0f\n", ori[0], ori[1], ori[2]);
            UF_UI_write_listing_window(msg);
        }
    }
	
}

//------------------------------------------------------------------------------
// 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
        UF_initialize();
		MyClass *theMyClass;
		theMyClass = new MyClass();
		theMyClass->do_it();
		delete theMyClass;
        UF_terminate();
	}
    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;
}

posted @ 2020-10-22 17:46  YellowBoss  阅读(1152)  评论(0)    收藏  举报