目录
一、概述
二、实现的功能
三、代码实现以及详细解析
一、概述
在NX二次开发过程中,我们为了效率经常会进行Open C的计时统计,这个实例可用于收集关于Open C的计时信息程序,并且在计时测试中很有用。该实例通过UF_begin_timer启动一个计时器,创建一个块,然后调用UF_end_timer来停止,统计建模时长,并将cpu_time和real_time进行窗口显示以及将信息保存到D:\\GetRunningTime.txt中。当然在复杂的案例中多个计时器可以在任何时间处于活动状态,也可以有几个子计时器单独计时子操作。
二、实现的功能
1、统计创建块的cpu时长和运行总时长;
2、将信息在信息窗口中显示并进内容保存到D:\\GetRunningTime.txt中。
三、代码实现以及详细解析
//NXOpen_GetRunningTime
// 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>
#include <NXOpen/Session.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_modl.h"
#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:
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
UF_initialize();
//启动计时器
UF_timer_t timer = { 0 };
UF_begin_timer(&timer);
//创建块
UF_FEATURE_SIGN sign = UF_NULLSIGN;
double corner_pt[3] = { 0.0, 0.0, 0.0 };
char * edge_len[3] = { "100", "100", "100" };
tag_t blk_obj_id = NULL_TAG;
UF_MODL_create_block1(sign, corner_pt, edge_len, &blk_obj_id);
//结束计时器
UF_timer_values_t ValueTime = { 0 };
UF_end_timer(timer, &ValueTime);
//打印统计信息
std::ostringstream temp;
temp << "耗时:cpu time:" << ValueTime.cpu_time << "\n" << "耗时:real time:" << ValueTime.real_time;
std::string tempStr = temp.str();
/*NXOpen::Session* theSession = NXOpen::Session::GetSession();*/
NXOpen::ListingWindow* list = theSession->ListingWindow();
list->SelectDevice(NXOpen::ListingWindow::DeviceType::DeviceTypeFileAndWindow, "D:\\GetRunningTime.txt");
if (!list->IsOpen()) list->Open();
list->WriteLine(tempStr.c_str());
list->Close();
list->SelectDevice(NXOpen::ListingWindow::DeviceType::DeviceTypeWindow,"");
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;
}
运行效果如下: