一、概述
最近看到 一些文章比较有趣,所以做个记录,顺便写一下博客,附上全部代码,方便刚从事NX二次开发同僚的理解。本次主要模拟NX自带的测量工具中对两个实体对象进行测量距离。NX系统功能如下所示:
二、代码解析
主要代码:
void measureDistance(tag_t objTAG1, tag_t objTAG2, double &MeasureDistance_Max, double &MeasureDistance_Min)
{
double retMax = 0.0, retMin = 0.0;
NXOpen::Session* theSession = NXOpen::Session::GetSession();
NXOpen::Part* workPart(theSession->Parts()->Work());
//测量对象1
NXOpen::DisplayableObject* object1(dynamic_cast<NXOpen::DisplayableObject*>(NXObjectManager::Get(objTAG1)));
//测量对象2
NXOpen::DisplayableObject* object2(dynamic_cast<NXOpen::DisplayableObject*>(NXObjectManager::Get(objTAG2)));
NXOpen::MeasureDistance* measureDistance0;
NXOpen::MeasureDistance* measureDistance1;
// 获得最远距离
measureDistance0 = workPart->MeasureManager()->NewDistance(NULL, NXOpen::MeasureManager::MeasureType::MeasureTypeMaximum, object1, object2);
// 获得最近距离
measureDistance1 = workPart->MeasureManager()->NewDistance(NULL, NXOpen::MeasureManager::MeasureType::MeasureTypeMinimum, object1, object2);
// 获取值
MeasureDistance_Max = measureDistance0->Value();
MeasureDistance_Min = measureDistance1->Value();
delete measureDistance0, measureDistance1;
}
全部代码:
//NX12_NXOpenCPP_Wizard1
// 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>
// Std C++ Includes
#include <iostream>
#include <sstream>
#include <uf_defs.h>
#include <NXOpen/NXException.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/ModelingView.hxx>
#include <NXOpen/ModelingViewCollection.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/View.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/Builder.hxx>
#include <NXOpen/DatumAxis.hxx>
#include <NXOpen/DatumCollection.hxx>
#include <NXOpen/Direction.hxx>
#include <NXOpen/DirectionCollection.hxx>
#include <NXOpen/DisplayableObject.hxx>
#include <NXOpen/Expression.hxx>
#include <NXOpen/ExpressionCollection.hxx>
#include <NXOpen/MeasureDistanceBuilder.hxx>
#include <NXOpen/MeasureManager.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/ScCollector.hxx>
#include <NXOpen/SelectDisplayableObject.hxx>
#include <NXOpen/SelectDisplayableObjectList.hxx>
#include <NXOpen/SelectObject.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/SmartObject.hxx>
#include <NXOpen/MeasureDistance.hxx>
#include "uf_ui.h"
#include<NXOpen/NXObjectManager.hxx>
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:
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);
}
void measureDistance(tag_t objTAG1, tag_t objTAG2, double &MeasureDistance_Max, double &MeasureDistance_Min)
{
double retMax = 0.0, retMin = 0.0;
NXOpen::Session* theSession = NXOpen::Session::GetSession();
NXOpen::Part* workPart(theSession->Parts()->Work());
//测量对象1
NXOpen::DisplayableObject* object1(dynamic_cast<NXOpen::DisplayableObject*>(NXObjectManager::Get(objTAG1)));
//测量对象2
NXOpen::DisplayableObject* object2(dynamic_cast<NXOpen::DisplayableObject*>(NXObjectManager::Get(objTAG2)));
NXOpen::MeasureDistance* measureDistance0;
NXOpen::MeasureDistance* measureDistance1;
// 获得最远距离
measureDistance0 = workPart->MeasureManager()->NewDistance(NULL, NXOpen::MeasureManager::MeasureType::MeasureTypeMaximum, object1, object2);
// 获得最近距离
measureDistance1 = workPart->MeasureManager()->NewDistance(NULL, NXOpen::MeasureManager::MeasureType::MeasureTypeMinimum, object1, object2);
// 获取值
MeasureDistance_Max = measureDistance0->Value();
MeasureDistance_Min = measureDistance1->Value();
delete measureDistance0, measureDistance1;
}
//------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{
// TODO: add your code here
NXOpen::Session *theSession = NXOpen::Session::GetSession();
NXOpen::Part *workPart(theSession->Parts()->Work());
NXOpen::Part *displayPart(theSession->Parts()->Display());
NXOpen::Body *body1(dynamic_cast<NXOpen::Body *>(workPart->Bodies()->FindObject("BLOCK(1)")));
tag_t bodyTag1 = body1->Tag();
NXOpen::Body *body2(dynamic_cast<NXOpen::Body *>(workPart->Bodies()->FindObject("BLOCK(2)")));
tag_t bodyTag2 = body2->Tag();
double MeasureDistance_Max = 0.0;
double MeasureDistance_Min = 0.0;
measureDistance(bodyTag1, bodyTag2, MeasureDistance_Max, MeasureDistance_Min);
char msg1[256];
sprintf(msg1, "%f", MeasureDistance_Max);
char msg2[256];
sprintf(msg2, "%f", MeasureDistance_Min);
UF_UI_open_listing_window();//打开窗口
UF_UI_write_listing_window("两个测量对象最大距离");
UF_UI_write_listing_window(msg1);//打印
UF_UI_write_listing_window("\n");
UF_UI_write_listing_window("两个测量对象最小距离");
UF_UI_write_listing_window(msg2);//打印
UF_UI_write_listing_window("\n");
}
//------------------------------------------------------------------------------
// 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;
}
三、显示结果