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(); //拾取草图尺寸对 ...
随机推荐
- 【转】Spring+Websocket实现消息的推送
本文主要有三个步骤 1.用户登录后建立websocket连接,默认选择websocket连接,如果浏览器不支持,则使用sockjs进行模拟连接 2.建立连接后,服务端返回该用户的未读消息 3.服务端进 ...
- 最长上升子序列(LIS)问题
最长上升子序列(LIS)问题 此处我们只讨论严格单调递增的子序列求法. 前面O(n2)的算法我们省略掉,直接进入O(nlgn)算法. 方法一:dp + 树状数组 定义dp[i]:末尾数字是i时最长上升 ...
- 以字符集为位数的字符串hash——上海网络赛G
先预处理一个hash[a][b]:开头字符为a, 结尾字符是b,中间的字符hash值为hs的的hash表,保存的是出现次数 对于一个子串求hash值的策略:设hash值是个26位的数,每新增一个字符, ...
- NX二次开发-OLE/COM向EXCEL表格中插入图片
今晚有一个兄弟问我怎么往EXCEL里插入图片(加工程序单中需要插入图片),这个我之前也没弄过,回复了他一句不知道,后来刚刚干完游戏吃完鸡,就去VC++的书上翻了翻,还真的被我翻到了.VC++的方法往E ...
- python内置模块-random
print(random.randint(1,10)) 生成随机整数,下限必须小于上限print(random.randrange(1,10)) 生成随机整数,参数为([start],stop,[st ...
- linux基础知识汇总(四)--ps grep命令
转:http://www.cnblogs.com/allen8807/archive/2010/11/10/1873843.html http://www.cnblogs.com/end/archiv ...
- (转)Linux环境进程间通信----系统 V 消息队列列
转:http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点.作为早期unix通 ...
- “今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛 )--E. DoveCCL and Resistance
题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/D来源:牛客网 题目描述 ...
- cgo 和 Go 语言是两码事
cgo不是Go 借用 JWZ的一句话 有些人,当他们面临一个问题时,认为“我知道,我会使用 cgo ”.那么现在,他们有了两个问题. 最近有人在 Gopher 的 Slack Channel 上使用 ...
- 安装nodejs nvm
安装nodejs sudo apt-get install nodejs sudo apt-get install npm 安装nvm https://www.runoob.com/w3cnote/n ...