大概思路是将每个视图导出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图片的更多相关文章

  1. NX二次开发-将工程图视图+尺寸的最大边界导出图片

    /***************************************************************************** ** ** ExportPicture.c ...

  2. NX二次开发-UFUN工程图导入视图UF_DRAW_import_view

    NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...

  3. NX二次开发-UFUN工程图初始化视图信息UF_DRAW_initialize_view_info

    NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...

  4. NX二次开发-UFUN工程图更新视图UF_DRAW_update_one_view

    NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...

  5. NX二次开发-UFUN工程图插入PNG图片UF_DRF_create_image_from_file

    #include <uf.h> #include <uf_drf.h> UF_initialize(); //插入PNG char* file_name = "D:\ ...

  6. NX二次开发-获取工程图尺寸的值UF_DRF_ask_dim_info

    UF_initialize(); //遍历所有尺寸 ; tag_t DimTag = NULL_TAG; UF_OBJ_cycle_objs_in_part1(UF_PART_ask_display_ ...

  7. NX二次开发-UFUN工程图表格注释section转tag函数UF_TABNOT_ask_tabular_note_of_section

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  8. NX二次开发-UFUN工程图表格注释获取某一行的tag函数UF_TABNOT_ask_nth_row

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  9. NX二次开发-UFUN工程图表格注释获取某一列的tag函数UF_TABNOT_ask_nth_column

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

随机推荐

  1. Web UI开发神器—Kendo UI for jQuery数据管理之过滤操作

    Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...

  2. 【JZOJ1914】【BZOJ2125】最短路

    description 给一个N个点M条边的连通无向图,满足每条边最多属于一个环,有Q组询问,每次询问两点之间的最短路径. analysis 建出圆方树后,可以知道仙人掌上每一个方点连着的边双其实就是 ...

  3. 用php 生成 excel 表格

    //引用新建对象require "../phpexcel/Classes/PHPExcel.php"; $excel = new PHPExcel(); 建表格 //Excel表格 ...

  4. 搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程

    http://blog.chinaunix.net/uid-20639775-id-154497.html

  5. 学习Caffe(一)安装Caffe

    Caffe是一个深度学习框架,本文讲阐述如何在linux下安装GPU加速的caffe. 系统配置是: OS: Ubuntu14.04 CPU: i5-4690 GPU: GTX960 RAM: 8G ...

  6. iOS 7 认识 TextKit

    本文由 伯乐在线 - 和谐老约翰 翻译自 Max Seelemann.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. iOS7 的发布给开发者的案头带来了很多新工具.其中一个就是 TextKit( ...

  7. 51nod-1366 贫富差距——并查集

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1366 #include <iostream> # ...

  8. P1435 回文字串

    P1435 回文字串 题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最 ...

  9. 非JAVA客户端与mina使用 PrefixedStringCodecFactory 通讯

    与C++,C#不同,java的写入字节顺序是从高到低(左低到右高) 例如 内存数据:{ 0x67,0x45,0x23,0x01} ,java int值是:0x6745231  而C++是:0x1234 ...

  10. c#委托(Delegates)--基本概念及使用 转发

    在我这菜鸟理解上,委托就是可以用方法名调用另一方法的便捷方法,可以简化switch等语句的重复.最近做项目的时候恰好需要用到委托,便来复习及学习委托的使用.嗯...本人以前并没有用过,只是稍微知道而已 ...