NX二次开发-将工程图视图+尺寸的最大边界导出图片
/*****************************************************************************
**
** ExportPicture.cpp
**
** Description:
** Contains Unigraphics entry points for the application.
**
*****************************************************************************/ /* Include files */
#include <stdarg.h>
#include <strstream>
#include <iostream>
using std::ostrstream;
using std::endl;
using std::ends;
using std::cerr;
#include <uf.h>
#include <uf_ui_types.h>
#include <uf_ui.h>
#include <uf_exit.h> //头文件
#include <uf.h>
#include <uf_ui.h>
#include <uf_modl.h>
#include <uf_curve.h>
#include <uf_obj.h>
#include <uf_draw.h>
#include <uf_drf.h>
#include <vector>
#include <algorithm>
#include <uf_cgm.h>
#include <iostream>
#include <windows.h>
#include <sstream>
#include <uf_cfi.h>
#include <atlimage.h>
#include <uf_part.h>
#include <uf_disp.h> using namespace std; static void ECHO(char *format, ...)
{
char msg[];
va_list args;
va_start(args, format);
vsnprintf_s(msg, sizeof(msg), _TRUNCATE, format, args);
va_end(args);
UF_UI_open_listing_window();
UF_UI_write_listing_window(msg);
UF_print_syslog(msg, FALSE);
} #define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X))) static int report_error( char *file, int line, char *call, int irc)
{
if (irc)
{
char err[]; UF_get_fail_message(irc, err);
ECHO("*** ERROR code %d at line %d in %s:\n",
irc, line, file);
ECHO("+++ %s\n", err);
ECHO("%s;\n", call);
} return(irc);
} void 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
char* GetName = NULL;
UF_translate_variable("UGII_BASE_DIR", &GetName);//获取NX主目录
std::ostringstream tempstring;
tempstring << GetName << "\\NXPLOT\\bin\\pdf\\cgm2pdf.exe " << outFilePath << " " << outPdfFilePath;
std::string covertvalule = tempstring.str();
WinExec(covertvalule.c_str(), SW_HIDE); //打开PDF转换器,并转换
tempstring.str("");
tempstring.clear();
}
} /*****************************************************************************
** Activation Methods
*****************************************************************************/
/* Explicit Activation
** This entry point is used to activate the application explicitly, as in
** "File->Execute UG/Open->User Function..." */
extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
/* Initialize the API environment */
if( UF_CALL(UF_initialize()) )
{
/* Failed to initialize */
return;
} /* TODO: Add your application code here */ //获得当前图纸页tag
tag_t drawing_tag = NULL_TAG;
UF_CALL(UF_DRAW_ask_current_drawing(&drawing_tag)); //打开当前图纸页
UF_CALL(UF_DRAW_open_drawing(drawing_tag)); //获得图纸页上的视图
int num_views = ;
tag_p_t view_tag = NULL_TAG;
UF_CALL(UF_DRAW_ask_views(drawing_tag, &num_views, &view_tag)); //定义vector
vector<double> Xmin;
vector<double> Ymin;
vector<double> Xmax;
vector<double> Ymax;
//获得每个视图的边界
//[0] - X min
//[1] - Y min
//[2] - X max
//[3] - Y max
for (int i = ; i < num_views; i++)
{
double view_borders[];
UF_CALL(UF_DRAW_ask_view_borders(view_tag[i], view_borders));//视图最大边界 //添加到vector
Xmin.push_back(view_borders[]);
Ymin.push_back(view_borders[]);
Xmax.push_back(view_borders[]);
Ymax.push_back(view_borders[]);
} //vector排序
sort(Xmin.begin(), Xmin.end());
Xmin.erase(unique(Xmin.begin(), Xmin.end()), Xmin.end()); sort(Ymin.begin(), Ymin.end());
Ymin.erase(unique(Ymin.begin(), Ymin.end()), Ymin.end()); sort(Xmax.begin(), Xmax.end());
Xmax.erase(unique(Xmax.begin(), Xmax.end()), Xmax.end());
int XmaxBig1 = Xmax.size() - ; sort(Ymax.begin(), Ymax.end());
Ymax.erase(unique(Ymax.begin(), Ymax.end()), Ymax.end());
int YmaxBig1 = Ymax.size() - ; //遍历所有尺寸
vector<tag_t> DimAll;
tag_t DimTag = NULL_TAG;
UF_CALL(UF_OBJ_cycle_objs_in_part(UF_PART_ask_display_part(), UF_dimension_type, &DimTag));
while (DimTag != NULL_TAG)
{
//获得每个尺寸的坐标原点
int dim_subtype = ;
double dim_origin[];
UF_DRF_dim_info_p_t dim_info;
UF_CALL(UF_DRF_ask_dim_info(DimTag, &dim_subtype, dim_origin, &dim_info)); DimAll.push_back(DimTag); if (dim_origin[] > Xmax[XmaxBig1])
{
//添加到vector
Xmax.push_back(dim_origin[]);
}
else if (dim_origin[] < Xmin[])
{
Xmin.push_back(dim_origin[]); }
else if (dim_origin[] > Ymax[YmaxBig1])
{
Ymax.push_back(dim_origin[]);
}
else if (dim_origin[] < Ymin[])
{
Ymin.push_back(dim_origin[]);
} UF_CALL(UF_OBJ_cycle_objs_in_part(UF_PART_ask_display_part(), UF_dimension_type, &DimTag));
} //vector排序
sort(Xmin.begin(), Xmin.end());
Xmin.erase(unique(Xmin.begin(), Xmin.end()), Xmin.end()); sort(Ymin.begin(), Ymin.end());
Ymin.erase(unique(Ymin.begin(), Ymin.end()), Ymin.end()); sort(Xmax.begin(), Xmax.end());
Xmax.erase(unique(Xmax.begin(), Xmax.end()), Xmax.end());
int XmaxBig = Xmax.size() - ; sort(Ymax.begin(), Ymax.end());
Ymax.erase(unique(Ymax.begin(), Ymax.end()), Ymax.end());
int YmaxBig = Ymax.size() - ; //创建直线
UF_CURVE_line_t line_coords1;
line_coords1.start_point[] = Xmin[];
line_coords1.start_point[] = Ymin[];
line_coords1.start_point[] = ;
line_coords1.end_point[] = Xmin[];
line_coords1.end_point[] = Ymax[YmaxBig];
line_coords1.end_point[] = ;
tag_t line[];
UF_CALL(UF_CURVE_create_line(&line_coords1, &line[])); UF_CURVE_line_t line_coords2;
line_coords2.start_point[] = Xmin[];
line_coords2.start_point[] = Ymax[YmaxBig];
line_coords2.start_point[] = ;
line_coords2.end_point[] = Xmax[XmaxBig];
line_coords2.end_point[] = Ymax[YmaxBig];
line_coords2.end_point[] = ;
UF_CALL(UF_CURVE_create_line(&line_coords2, &line[])); UF_CURVE_line_t line_coords3;
line_coords3.start_point[] = Xmax[XmaxBig];
line_coords3.start_point[] = Ymax[YmaxBig];
line_coords3.start_point[] = ;
line_coords3.end_point[] = Xmax[XmaxBig];
line_coords3.end_point[] = Ymin[];
line_coords3.end_point[] = ;
UF_CALL(UF_CURVE_create_line(&line_coords3, &line[])); UF_CURVE_line_t line_coords4;
line_coords4.start_point[] = Xmax[XmaxBig];
line_coords4.start_point[] = Ymin[];
line_coords4.start_point[] = ;
line_coords4.end_point[] = Xmin[];
line_coords4.end_point[] = Ymin[];
line_coords4.end_point[] = ;
UF_CALL(UF_CURVE_create_line(&line_coords4, &line[])); //将图纸页导出PDF
ExportPdf(drawing_tag, "D:\\PNG\\lsy.cgm", "D:\\PNG\\lsy.pdf"); //转换
char Pdf2Png[];
sprintf_s(Pdf2Png, "D:\\Pdf2PngTools\\Pdf2Png.exe %s %s", "D:\\PNG\\lsy.pdf", "D:\\PNG\\lsy"); //判断文件是否存在
int status = ;
UF_CALL(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); //获得图纸页的大小
UF_DRAW_info_t drawing_info;
UF_CALL(UF_DRAW_ask_drawing_info(drawing_tag, &drawing_info));
double DrawH = drawing_info.size.custom_size[];
double DrawW = drawing_info.size.custom_size[]; //图片裁剪
CString filepathname = "D:\\PNG\\lsy1.png", filepathname1 = "D:\\PNG\\lsy123.png";
int width = , height = ;
CImage p_w_picpath, p_w_picpath1;
p_w_picpath.Load(filepathname); //加载图片
width = p_w_picpath.GetWidth();
height = p_w_picpath.GetHeight(); //计算1毫米等于多少像素(图纸尺寸和图纸PNG像素对比)
double AA = width / DrawW;
double BB = height / DrawH; //计算距离(图纸尺寸和矩形最大边界间距)
double Xdistance = (DrawW - (Xmax[XmaxBig] - Xmin[]))*AA;
double Ydistance = (DrawH - (Ymax[YmaxBig] - Ymin[]))*BB; //图片裁剪,创建新的png
p_w_picpath1.Create(width - Xdistance, height - Ydistance, p_w_picpath.GetBPP()); // 创建一个目标存储对象
p_w_picpath.BitBlt(p_w_picpath1.GetDC(), , , width - Xdistance, height - Ydistance, Xmin[] * AA, (DrawH - Ymax[YmaxBig])*BB, SRCCOPY); //COPY原图的局部到目标对象里
p_w_picpath1.Save(filepathname1); // 保存处理后的图片
p_w_picpath1.ReleaseDC(); // 释放资源
p_w_picpath1.Destroy(); // 销毁资源 /* Terminate the API environment */
UF_CALL(UF_terminate());
} /*****************************************************************************
** Utilities
*****************************************************************************/ /* Unload Handler
** This function specifies when to unload your application from Unigraphics.
** If your application registers a callback (from a MenuScript item or a
** User Defined Object for example), this function MUST return
** "UF_UNLOAD_UG_TERMINATE". */
extern int ufusr_ask_unload( void )
{
return( UF_UNLOAD_IMMEDIATELY );
} Caesar卢尚宇
2019年11月23日

NX二次开发-将工程图视图+尺寸的最大边界导出图片的更多相关文章
- 【NX二次开发】获取视图当前的剪辑边界UF_VIEW_ask_current_xy_clip()
UF_VIEW_ask_current_xy_clip()这个函数网上还没有详细的说明,我花了一点时间,详细得理解了一下函数返回的4个值的意思,作为一个猜想,希望有人能验证一下. 获取视图当前的剪辑边 ...
- 【NX二次开发】根据视图名称旋转视图,在布局中替换视图uc6464
uc6464("布局名","旧视图名","新视图名");输入布局名.旧视图名.新视图名.如果布局名为空则更新当前布局.如果旧视图名为空,则工 ...
- NX二次开发-将工程图上的每个视图导出PNG图片
大概思路是将每个视图导出PDF,在调另一个项目的EXE(PDF转PNG) //ExportDrawViewPng // Mandatory UF Includes #include <uf.h& ...
- 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二次开发-获取工程图尺寸的值UF_DRF_ask_dim_info
UF_initialize(); //遍历所有尺寸 ; tag_t DimTag = NULL_TAG; UF_OBJ_cycle_objs_in_part1(UF_PART_ask_display_ ...
- NX二次开发-UFUN读取图纸尺寸的值UF_DRF_ask_dimension_text
今天发现UF_DRF_ask_dim_info这个函数不能读带附件文本的尺寸,有附加文本dim_info->text_info->text->full_string;读出来的是附加文 ...
- NX二次开发-UFUN拾取草图尺寸对话框UF_UI_select_sketch_dimensions
#include <uf.h> #include <uf_ui.h> #include <uf_sket.h> UF_initialize(); //拾取草图尺寸对 ...
随机推荐
- vue 插槽 slot
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 高性能js之js文件的加载与解析
随着网站的发展,现在的网页已经离不开js,经常一个页面会引入大量的js.那么该如何合理的加载这些js? head标签中引入js文件可能是最常见的一种方式,但是这样会造成一个问题.因为j可以说是浏览器中 ...
- foreach循环的跳出
由于foreach循环中不像for循环可以直接通过return或break来终止当前循环,不过这里可以借助try...catch...来完成var arr = [1,2,3,4,5,6,7,8,9,1 ...
- h5判断设备是ios还是android
var u = navigator.userAgent, app = navigator.appVersion;var isAndroid = u.indexOf('Android') > -1 ...
- PHP ftp_nb_get() 函数
定义和用法 ftp_nb_get() 函数从 FTP 服务器上下载一个文件并保存到本地一个文件中.(无阻塞) 该函数返回下列值之一: FTP_FAILED(发送/获取失败) FTP_FINISHED( ...
- vue基础五
条件渲染 1.v-if 1.1<template>中v-if条件组 因为 v-if 是一个指令,需要将它添加到一个元素上.但是如果我们想切换多个元素呢?此时我们可以把一个<templ ...
- BZOJ 3236: [Ahoi2013]作业(莫队+树状数组)
传送门 解题思路 莫队+树状数组.把求\([a,b]\)搞成前缀和形式,剩下的比较裸吧,用\(cnt\)记一下数字出现次数.时间复杂度\(O(msqrt(n)log(n)\),莫名其妙过了. 代码 # ...
- JS对象的讲解
1.对象属性的可枚举性和所有权:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Enumerability_and_ownership_ ...
- vue2 开发环境部署 及 打包配置
一.脚手架工具(vue2 的脚手架工具是 vue-cli) 1.脚手架工具的安装 参考 : https://blog.csdn.net/wulala_hei/article/details/804 ...
- PHP fpassthru() 函数
定义和用法 fpassthru() 函数输出文件指针处的所有剩余数据. 该函数将给定的文件指针从当前的位置读取到 EOF,并把结果写到输出缓冲区. 语法 fpassthru(file) 参数 描述 f ...