NX二次开发-将工程图上的每个视图导出PNG图片

时间:2023-03-10 03:47:53
NX二次开发-将工程图上的每个视图导出PNG图片

大概思路是将每个视图导出PDF,在调另一个项目的EXE(PDF转PNG)

 //ExportDrawViewPng

 // 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_draw.h>
#include <uf_cfi.h>
#include <uf_drf.h>
#include <uf_obj.h>
#include <uf_part.h>
#include <uf_cgm.h>
#include <windows.h>
#include <NXOpen/PrintPDFBuilder.hxx>
#include <NXOpen/PlotManager.hxx> // Std C++ Includes
#include <iostream>
#include <sstream> using namespace NXOpen;
using namespace std;
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*); void ExportPdf(tag_t drawing_tag, char* outFilePath, char* outPdfFilePath); 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);
} void MyClass::ExportPdf(tag_t drawing_tag, char* outFilePath, char* outPdfFilePath)
{
if (drawing_tag != NULL_TAG)
{
UF_CGM_export_options_t export_options;
UF_CGM_ask_default_export_options(&export_options);
//UF_CGM_ask_session_export_options(&export_options);//用这个函数也可以初始化
export_options.reason = UF_CGM_pdf_reason;
UF_CGM_set_session_export_options(&export_options); UF_CGM_export_cgm(drawing_tag, &export_options, outFilePath); //导出成CGM文件 //将CGM转换成PDF
NXOpen::NXString nxbasedir = theSession->GetEnvironmentVariableValue("UGII_BASE_DIR");//获取NX主目录
std::ostringstream tempstring;
tempstring << nxbasedir.GetLocaleText() << "\\NXPLOT\\bin\\pdf\\cgm2pdf.exe " << outFilePath << " " << outPdfFilePath;
std::string covertvalule = tempstring.str();
WinExec(covertvalule.c_str(), SW_HIDE); //打开PDF转换器,并转换
tempstring.str("");
tempstring.clear();
}
} //------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{ // TODO: add your code here UF_initialize(); //获得当前图纸页tag
tag_t drawing_tag = NULL_TAG;
UF_DRAW_ask_current_drawing(&drawing_tag); //打开图纸页
UF_DRAW_open_drawing(drawing_tag); //遍历图纸所有尺寸
vector<tag_t> DimAll;
tag_t DimTag = NULL_TAG;
UF_OBJ_cycle_objs_in_part1(UF_PART_ask_display_part(), UF_dimension_type, &DimTag);//遍历所有尺寸
while (DimTag != NULL_TAG)
{
DimAll.push_back(DimTag);//把所有尺寸tag添加到vector里 UF_OBJ_cycle_objs_in_part1(UF_PART_ask_display_part(), UF_dimension_type, &DimTag);//遍历所有尺寸
} int Count = ;
//获得图纸页上的所有视图
int num_views = ;
tag_t* view_tag = NULL_TAG;
UF_DRAW_ask_views(drawing_tag, &num_views, &view_tag);
for (int i = ; i < num_views; i++)
{
//获得视图的最大边界
double view_borders[];
UF_DRAW_ask_view_borders(view_tag[i], view_borders); //获得视图原点
double ViewOrigin[];
ViewOrigin[] = (view_borders[] - view_borders[]) / + view_borders[];
ViewOrigin[] = (view_borders[] - view_borders[]) / + view_borders[]; //更新视图
UF_DRAW_update_one_view(drawing_tag, view_tag[i]); //先移动下视图位置,为视图移动到下一张图纸页上做准备
double drawing_reference_point1[] = { , };
UF_DRAW_move_view(view_tag[i], drawing_reference_point1); //更新视图
UF_DRAW_update_one_view(drawing_tag, view_tag[i]); //获取视图上面尺寸的最大边界
//由最大边界来定义新的图纸页大小
//此步骤还没写
//...... //新建图纸页
char msg[];
sprintf_s(msg, "%d", Count);
UF_DRAW_info_t DrawingInfo;
DrawingInfo.size_state = UF_DRAW_CUSTOM_SIZE;
DrawingInfo.size.custom_size[] = view_borders[] - view_borders[] + ;
DrawingInfo.size.custom_size[] = view_borders[] - view_borders[] + ;
DrawingInfo.drawing_scale = 1.0;
DrawingInfo.units = UF_PART_METRIC;
DrawingInfo.projection_angle = UF_DRAW_FIRST_ANGLE_PROJECTION;
tag_t DrawTAG1 = NULL_TAG;
UF_DRAW_create_drawing(msg, &DrawingInfo, &DrawTAG1); //移动视图到其他图纸页
UF_DRAW_move_view_to_drawing(view_tag[i], DrawTAG1); //移动每个视图到居中位置
double drawing_reference_point[] = { (view_borders[] - view_borders[] + ) / , (view_borders[] - view_borders[] + ) / };
UF_DRAW_move_view(view_tag[i], drawing_reference_point); //转换(设置导出路径)
char msgCGM[];
sprintf_s(msgCGM, "D:\\PNG\\lsy%d.cgm", Count); char msgPDF[];
sprintf_s(msgPDF, "D:\\PNG\\lsy%d.pdf", Count); //将图纸页导出PDF
ExportPdf(DrawTAG1, msgCGM, msgPDF); //转换
char Pdf2Png[];
string A = "D:\\PNG\\";
string B = msg;
string PngName = A + B;
sprintf_s(Pdf2Png, "D:\\Pdf2PngTools\\Pdf2Png.exe %s %s", msgPDF, PngName.c_str()); //判断文件是否存在
int status = ;
UF_CFI_ask_file_exist("D:\\Pdf2PngTools\\Pdf2Png.exe", &status);
if (status != )
{
uc1601("提示:D:\\Pdf2PngTools\\Pdf2Png.exe程序不存在", );
return;
} Sleep();//这个地方必须得延迟一下,要不然连续调EXE导出就会报错 //调EXE,PDF转PNG
system(Pdf2Png); //删除.pdf和.cgm
DeleteFile(msgPDF);//.pdf
DeleteFile(msgCGM);//.cgm Count++;
} 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;
} Caesar卢尚宇
2019年11月23日

NX二次开发-将工程图上的每个视图导出PNG图片