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图片的更多相关文章
- NX二次开发-将工程图视图+尺寸的最大边界导出图片
/***************************************************************************** ** ** ExportPicture.c ...
- NX二次开发-UFUN工程图导入视图UF_DRAW_import_view
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...
- NX二次开发-UFUN工程图初始化视图信息UF_DRAW_initialize_view_info
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...
- NX二次开发-UFUN工程图更新视图UF_DRAW_update_one_view
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...
- NX二次开发-UFUN工程图插入PNG图片UF_DRF_create_image_from_file
#include <uf.h> #include <uf_drf.h> UF_initialize(); //插入PNG char* file_name = "D:\ ...
- NX二次开发-获取工程图尺寸的值UF_DRF_ask_dim_info
UF_initialize(); //遍历所有尺寸 ; tag_t DimTag = NULL_TAG; UF_OBJ_cycle_objs_in_part1(UF_PART_ask_display_ ...
- 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 ...
- NX二次开发-UFUN工程图表格注释获取某一行的tag函数UF_TABNOT_ask_nth_row
NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...
- NX二次开发-UFUN工程图表格注释获取某一列的tag函数UF_TABNOT_ask_nth_column
NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...
随机推荐
- shell脚本之用户管理
#!/usr/bin/env bash ############################### # 脚本名称 : userManager.sh # # 脚本功能 : 账号管理 # # 脚本参数 ...
- Hive速览
一.概述 Hive由Facebook开源,是一个构建在Hadoop之上的数据仓库工具 将结构化的数据映射成表 支持类SQL查询,Hive中称为HQL 1.读模式 2.Hive架构 3.使用Hive的原 ...
- Java 基础 - 继承
子类继承父类的private字段么? Oracle的Java Documentation对Inheritance的定义: 很直白,定义里面就告诉你了这不叫继承.继承的意思是你可以对其进行直接的调用和修 ...
- Kylin-2.6.2集群部署
1. 集群节点规划与说明 rzx1 all rzx2 query rzx3 query 说明: Kylin节点角色有三种: all: 包含query和job query: 查询节点 job: 工作节点 ...
- thinkcmf链接多个数据库
1.打开/data/conf/config.php 'db1'=>[ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '', / ...
- 深入理解Magento – 第四章 – 模型和ORM基础
深入理解Magento 作者:Alan Storm 翻译:Hailong Zhang 第四章 – 模型和ORM基础 对于任何一个MVC架构,模型(Model)层的实现都是占据了很大一部分.对于Mage ...
- 表格排序tablesort小案列
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 使用gulp管理sass文件
前提是npm和ruby已经安装好 1. 新建文件夹myproject,cd进入文件夹 再npm init 初始化 2.npm install gulp --save-dev 为项目添加gulp,并将g ...
- Tomcat运行错误示例
tomcat运行错误示例 当出现这种错误时,如果是自己配置的tomcat,需要找/conf/server.xml文件. 如果是使用的eclipse tomcat 插件,需要在你的工作空间 找到 如下文 ...
- Codeforces 1169B Pairs
题目链接:http://codeforces.com/contest/1169/problem/B 题意:给你 m 对数 ,问你能不能在 1 − n 之间找到俩个不相等的 x 和 y 使得 对于前面每 ...