大概思路是将每个视图导出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. thymeleaf onclick方法向js方法传递参数

    如下图 这个错误并不影响 请放心使用

  2. h5开发与pc开发的差异性

    1. viewport 将layout viewport 设置成ideal viewport . width=device-width,也可以设置 initial-scale=1,最好两者都有. wi ...

  3. 【记录】spring boot 图片上传与显示

    问题:spring boot 使用的是内嵌的tomcat, 文件上传指定目录时不知道文件上传到哪个地方,不知道访问路径. //部署到服务器的tomcat上时通常使用这种方式request.getSer ...

  4. delphi 设备函数GetDeviceCaps函数

    {说明:以下内容来源于网络,修改多处错误所得 2019.10.04 } GetDeviceCaps 函数功能:该函数检索指定设备的设备指定信息.该函数经常用在操作打印机等设备中.函数原型:int Ge ...

  5. PHP FILTER_UNSAFE_RAW 过滤器

    定义和用法 FILTER_UNSAFE_RAW 过滤器不进行任何过滤,去除或编码特殊字符. 该过滤器删除那些对应用程序有潜在危害的数据.它用于去除标签以及删除或编码不需要的字符. 如果不规定标志,则该 ...

  6. Shiro学习(24)在线回话管理

    有时候需要显示当前在线人数.当前在线用户,有时候可能需要强制某个用户下线等:此时就需要获取相应的在线用户并进行一些操作. 本章基于<第十六章 综合实例>代码构建. 会话控制器 Java代码 ...

  7. 「CTS2019 | CTSC2019」随机立方体 解题报告

    「CTS2019 | CTSC2019」随机立方体 据说这是签到题,但是我计数学的实在有点差,这里认真说一说. 我们先考虑一些事实 如果我们在位置\((x_0,y_0,z_0)\)钦定了一个极大数\( ...

  8. bzoj1046题解

    [解题思路] 先倒着求一遍LIS,然后对于每个询问L从左到右找到第一个大于等于L的上升序列即可.复杂度O(N(log2N+M)). [参考代码] #pragma GCC optimize(2) #in ...

  9. [NOIP模拟测试7]visit 题解(组合数学+CRT+Lucas定理)

    Orz 因为有T的限制,所以不难搞出来一个$O(T^3)$的暴力dp 但我没试 据说有30分? 正解的话显然是组合数学啦 首先$n,m$可能为负,但这并没有影响, 我们可以都把它搞成正的 即都看作向右 ...

  10. cd 命令行进入目标文件夹

    当我在默认路径中使用cd命令时,如果我要进入D:\mytext 文件夹,那么直接使用cd D:\mytext 是不行的 正确的使用是先使用d:进入D盘,然后再进入mytext文件夹