点击查看详细介绍

[KF] 借用KF的函数实现读取EXCEL表格内容(附源码) 精华帖

王牌飞行员 VIP会员 6年前 6944 68

购买与咨询 NX 二次开发视频教程,请联系微信号:13890821008  QQ号:85585969  


说明:UG二次开发借用KF的函数实现读取EXCEL表格内容(附源码)

效果:

被读取的表格:

image.png

读取后的数据:

image.png


源码:

//UF_KF_write_datea_to_excel

// Mandatory UF Includes
#include <uf.h>
#include <uf_object_types.h>
#include <uf_kf.h>
#include <uf_assem.h>
#include <uf_modl.h>
#include <uf_ui.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>

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

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

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


//------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------


void MyClass::do_it()
{
// TODO: add your code here
UF_initialize();

//对当前工作部件的进行KF类初始化
UF_KF_init_part(UF_ASSEM_ask_work_part());

//打开电子表格

●●●请先 登陆注册 后查看●●●

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


效果演示:


5

最新回复 (68)
  • 穿着隐身的 6年前
    1

    谢谢分享谢谢分享

  • 狙击兴 VIP会员 6年前
    2

    感谢楼主分享,多向楼主学习!楼主最棒!

  • buaa_kf VIP会员 6年前
    3

    谢谢分享,谢谢分享!

  • king_chinese VIP会员 6年前
    4

    谢谢分享谢谢分享

  • LIB105545 VIP会员 6年前
    5

    谢谢分享,谢谢分享

  • Zhaoyw 5年前
    6

    感谢楼主分享,谢谢!

  • boyue 5年前
    7

    谢谢分享,谢谢分享!

  • 传奇 5年前
    8

    NULL

  • litwo 5年前
    9

    感谢楼主分享,谢谢!

  • bao7659535 VIP会员 5年前
    10

    学习一下,谢谢分享

  • 87238527 VIP会员 5年前
    11

    谢谢分享,谢谢分享!

  • alan800119 5年前
    12

    感谢楼主分享,多向楼主学习!楼主最棒!

  • zby186 VIP会员 5年前
    13

    感谢楼主分享,多向楼主学习!楼主最棒!

  • allspark 5年前
    14

    楼主威武 正在学习 好资料 谢谢分享

  • loxa 5年前
    15

    感謝大大無私分享

请登录后发表新帖