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(); //拾取草图尺寸对 ...
随机推荐
- MyBatis 传递多个参数的几种方法
简介: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简 ...
- Delphi 打印纸张选项设置参数
{ paper selections } {$EXTERNALSYM DMPAPER_LETTER} DMPAPER_LETTER = 1; { Letter 8 12 x 11 in } {$EXT ...
- 行业顶级NoSQL成员坐阵,NoSQL数据库专场重点解析!
NoSQL数据库作为数据库市场最重要的组成之一,它的一举一动都影响着成千上万的企业.本专场邀请了行业顶级的NoSQL核心成员与大家共同展望NoSQL数据库的未来,阿里巴巴.MongoDB.Rediss ...
- QtConcurrent::run() 的使用
QFuture<T>run(constClass *object,T(Class::*fn)(Param1,Param2,Param3,Param4,Param5)const,constA ...
- 14、java实现poi操作excel,包括读和写日期格式,并且设置字体样式
1.首先大家来看导出的结果 下边就是导出的代码了 protected void testExcel() throws IOException{ String path=getServletContex ...
- 戏说 .NET GDI+系列学习教程(三、Graphics类的方法的总结)
- 14、testng.xml 设置用例执行顺序
目录如下: TestGroup.java 代码如下: package com.testng.cn; import org.testng.annotations.*; import static org ...
- KMP概念上小结
kmp算法的时间复杂度是O(m+n) 主要作用: 1.最长公共前后缀问题 2.原串中含有几个模式串问题 3.循环节问题
- Gradle任务
Gradle构建脚本描述一个或多个项目.每个项目都由不同的任务组成.任务是构建执行的一项工作.任务可以是编译一些类,将类文件存储到单独的目标文件夹中,创建JAR,生成Javadoc或将一些归档发布到存 ...
- Centos 7 技巧
查看系统版本详细信息 lsb_release -a 更改邮件MTA alternatives --config mta