• NX二次开发-将工程图视图+尺寸的最大边界导出图片


      1 /*****************************************************************************
      2 **
      3 ** ExportPicture.cpp
      4 **
      5 ** Description:
      6 **     Contains Unigraphics entry points for the application.
      7 **
      8 *****************************************************************************/
      9 
     10 /* Include files */
     11 #include <stdarg.h>
     12     #include <strstream>
     13     #include <iostream>
     14     using std::ostrstream;
     15     using std::endl;    
     16     using std::ends;
     17     using std::cerr;
     18 #include <uf.h>
     19 #include <uf_ui_types.h>
     20 #include <uf_ui.h>
     21 #include <uf_exit.h>
     22 
     23 //头文件
     24 #include <uf.h>
     25 #include <uf_ui.h>
     26 #include <uf_modl.h>
     27 #include <uf_curve.h>
     28 #include <uf_obj.h>
     29 #include <uf_draw.h>
     30 #include <uf_drf.h>
     31 #include <vector>
     32 #include <algorithm>
     33 #include <uf_cgm.h>
     34 #include <iostream>
     35 #include <windows.h>
     36 #include <sstream>
     37 #include <uf_cfi.h>
     38 #include <atlimage.h>
     39 #include <uf_part.h>
     40 #include <uf_disp.h>
     41 
     42 using namespace std;
     43 
     44 static void ECHO(char *format, ...)
     45 {
     46     char msg[1024];
     47     va_list args;
     48     va_start(args, format);
     49     vsnprintf_s(msg, sizeof(msg), _TRUNCATE, format, args);
     50     va_end(args);
     51     UF_UI_open_listing_window();
     52     UF_UI_write_listing_window(msg);
     53     UF_print_syslog(msg, FALSE);
     54 }
     55 
     56 #define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))
     57 
     58 static int report_error( char *file, int line, char *call, int irc)
     59 {
     60     if (irc)
     61     {
     62         char err[133];
     63 
     64         UF_get_fail_message(irc, err);
     65         ECHO("*** ERROR code %d at line %d in %s:
    ",
     66             irc, line, file);
     67         ECHO("+++ %s
    ", err);
     68         ECHO("%s;
    ", call);
     69     }
     70 
     71     return(irc);
     72 }
     73 
     74 
     75 void ExportPdf(tag_t drawing_tag, char* outFilePath, char* outPdfFilePath)
     76 {
     77     if (drawing_tag != NULL_TAG)
     78     {
     79         UF_CGM_export_options_t export_options;
     80         UF_CGM_ask_default_export_options(&export_options);
     81         //UF_CGM_ask_session_export_options(&export_options);//用这个函数也可以初始化
     82         export_options.reason = UF_CGM_pdf_reason;
     83         UF_CGM_set_session_export_options(&export_options);
     84 
     85         UF_CGM_export_cgm(drawing_tag, &export_options, outFilePath); //导出成CGM文件
     86 
     87         //将CGM转换成PDF
     88         char* GetName = NULL;
     89         UF_translate_variable("UGII_BASE_DIR", &GetName);//获取NX主目录
     90         std::ostringstream tempstring;
     91         tempstring << GetName << "\NXPLOT\bin\pdf\cgm2pdf.exe " << outFilePath << " " << outPdfFilePath;
     92         std::string covertvalule = tempstring.str();
     93         WinExec(covertvalule.c_str(), SW_HIDE); //打开PDF转换器,并转换
     94         tempstring.str("");
     95         tempstring.clear();
     96     }
     97 }
     98 
     99 /*****************************************************************************
    100 **  Activation Methods
    101 *****************************************************************************/
    102 /*  Explicit Activation
    103 **      This entry point is used to activate the application explicitly, as in
    104 **      "File->Execute UG/Open->User Function..." */
    105 extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
    106 {
    107     /* Initialize the API environment */
    108     if( UF_CALL(UF_initialize()) ) 
    109     {
    110         /* Failed to initialize */
    111         return;
    112     }
    113     
    114     /* TODO: Add your application code here */
    115 
    116     //获得当前图纸页tag
    117     tag_t drawing_tag = NULL_TAG;
    118     UF_CALL(UF_DRAW_ask_current_drawing(&drawing_tag));
    119 
    120     //打开当前图纸页
    121     UF_CALL(UF_DRAW_open_drawing(drawing_tag));
    122 
    123     //获得图纸页上的视图
    124     int num_views = 0;
    125     tag_p_t view_tag = NULL_TAG;
    126     UF_CALL(UF_DRAW_ask_views(drawing_tag, &num_views, &view_tag));
    127 
    128     //定义vector
    129     vector<double> Xmin;
    130     vector<double> Ymin;
    131     vector<double> Xmax;
    132     vector<double> Ymax;
    133     //获得每个视图的边界
    134     //[0] - X min
    135     //[1] - Y min
    136     //[2] - X max
    137     //[3] - Y max
    138     for (int i = 0; i < num_views; i++)
    139     {
    140         double view_borders[4];
    141         UF_CALL(UF_DRAW_ask_view_borders(view_tag[i], view_borders));//视图最大边界
    142 
    143         //添加到vector
    144         Xmin.push_back(view_borders[0]);
    145         Ymin.push_back(view_borders[1]);
    146         Xmax.push_back(view_borders[2]);
    147         Ymax.push_back(view_borders[3]);
    148     }
    149 
    150     //vector排序
    151     sort(Xmin.begin(), Xmin.end());
    152     Xmin.erase(unique(Xmin.begin(), Xmin.end()), Xmin.end());
    153 
    154     sort(Ymin.begin(), Ymin.end());
    155     Ymin.erase(unique(Ymin.begin(), Ymin.end()), Ymin.end());
    156 
    157     sort(Xmax.begin(), Xmax.end());
    158     Xmax.erase(unique(Xmax.begin(), Xmax.end()), Xmax.end());
    159     int XmaxBig1 = Xmax.size() - 1;
    160 
    161     sort(Ymax.begin(), Ymax.end());
    162     Ymax.erase(unique(Ymax.begin(), Ymax.end()), Ymax.end());
    163     int YmaxBig1 = Ymax.size() - 1;
    164     
    165     //遍历所有尺寸
    166     vector<tag_t> DimAll;
    167     tag_t DimTag = NULL_TAG;
    168     UF_CALL(UF_OBJ_cycle_objs_in_part(UF_PART_ask_display_part(), UF_dimension_type, &DimTag));
    169     while (DimTag != NULL_TAG)
    170     {
    171         //获得每个尺寸的坐标原点
    172         int dim_subtype = 0;
    173         double dim_origin[3];
    174         UF_DRF_dim_info_p_t dim_info;
    175         UF_CALL(UF_DRF_ask_dim_info(DimTag, &dim_subtype, dim_origin, &dim_info));
    176 
    177         DimAll.push_back(DimTag);
    178 
    179         if (dim_origin[0] > Xmax[XmaxBig1])
    180         {
    181             //添加到vector
    182             Xmax.push_back(dim_origin[0]);
    183         }
    184         else if (dim_origin[0] < Xmin[0])
    185         {
    186             Xmin.push_back(dim_origin[0]);
    187 
    188         }
    189         else if (dim_origin[1] > Ymax[YmaxBig1])
    190         {    
    191             Ymax.push_back(dim_origin[1]);
    192         }
    193         else if (dim_origin[1] < Ymin[0])
    194         {
    195             Ymin.push_back(dim_origin[1]);
    196         }
    197 
    198         UF_CALL(UF_OBJ_cycle_objs_in_part(UF_PART_ask_display_part(), UF_dimension_type, &DimTag));
    199     }
    200 
    201     //vector排序
    202     sort(Xmin.begin(), Xmin.end());
    203     Xmin.erase(unique(Xmin.begin(), Xmin.end()), Xmin.end());
    204 
    205     sort(Ymin.begin(), Ymin.end());
    206     Ymin.erase(unique(Ymin.begin(), Ymin.end()), Ymin.end());
    207 
    208     sort(Xmax.begin(), Xmax.end());
    209     Xmax.erase(unique(Xmax.begin(), Xmax.end()), Xmax.end());
    210     int XmaxBig = Xmax.size() - 1;
    211 
    212     sort(Ymax.begin(), Ymax.end());
    213     Ymax.erase(unique(Ymax.begin(), Ymax.end()), Ymax.end());
    214     int YmaxBig = Ymax.size() - 1;
    215 
    216     //创建直线
    217     UF_CURVE_line_t line_coords1;
    218     line_coords1.start_point[0] = Xmin[0];
    219     line_coords1.start_point[1] = Ymin[0];
    220     line_coords1.start_point[2] = 0;
    221     line_coords1.end_point[0] = Xmin[0];
    222     line_coords1.end_point[1] = Ymax[YmaxBig];
    223     line_coords1.end_point[2] = 0;
    224     tag_t line[4];
    225     UF_CALL(UF_CURVE_create_line(&line_coords1, &line[0]));
    226 
    227     UF_CURVE_line_t line_coords2;
    228     line_coords2.start_point[0] = Xmin[0];
    229     line_coords2.start_point[1] = Ymax[YmaxBig];
    230     line_coords2.start_point[2] = 0;
    231     line_coords2.end_point[0] = Xmax[XmaxBig];
    232     line_coords2.end_point[1] = Ymax[YmaxBig];
    233     line_coords2.end_point[2] = 0;
    234     UF_CALL(UF_CURVE_create_line(&line_coords2, &line[1]));
    235 
    236     UF_CURVE_line_t line_coords3;
    237     line_coords3.start_point[0] = Xmax[XmaxBig];
    238     line_coords3.start_point[1] = Ymax[YmaxBig];
    239     line_coords3.start_point[2] = 0;
    240     line_coords3.end_point[0] = Xmax[XmaxBig];
    241     line_coords3.end_point[1] = Ymin[0];
    242     line_coords3.end_point[2] = 0;
    243     UF_CALL(UF_CURVE_create_line(&line_coords3, &line[2]));
    244 
    245     UF_CURVE_line_t line_coords4;
    246     line_coords4.start_point[0] = Xmax[XmaxBig];
    247     line_coords4.start_point[1] = Ymin[0];
    248     line_coords4.start_point[2] = 0;
    249     line_coords4.end_point[0] = Xmin[0];
    250     line_coords4.end_point[1] = Ymin[0];
    251     line_coords4.end_point[2] = 0;
    252     UF_CALL(UF_CURVE_create_line(&line_coords4, &line[3]));
    253 
    254     //将图纸页导出PDF
    255     ExportPdf(drawing_tag, "D:\PNG\lsy.cgm", "D:\PNG\lsy.pdf");
    256 
    257     //转换
    258     char Pdf2Png[256];
    259     sprintf_s(Pdf2Png, "D:\Pdf2PngTools\Pdf2Png.exe %s %s", "D:\PNG\lsy.pdf", "D:\PNG\lsy");
    260 
    261     //判断文件是否存在
    262     int status = 0;
    263     UF_CALL(UF_CFI_ask_file_exist("D:\Pdf2PngTools\Pdf2Png.exe", &status));
    264     if (status != 0)
    265     {
    266         uc1601("提示:D:\Pdf2PngTools\Pdf2Png.exe程序不存在", 1);
    267         return;
    268     }
    269 
    270     Sleep(1000);//这个地方必须得延迟一下,要不然调EXE导出就会报错
    271 
    272     //调EXE,PDF转PNG
    273     system(Pdf2Png);
    274 
    275     //获得图纸页的大小
    276     UF_DRAW_info_t drawing_info;
    277     UF_CALL(UF_DRAW_ask_drawing_info(drawing_tag, &drawing_info));
    278     double DrawH = drawing_info.size.custom_size[0];
    279     double DrawW = drawing_info.size.custom_size[1];
    280 
    281     //图片裁剪
    282     CString filepathname = "D:\PNG\lsy1.png", filepathname1 = "D:\PNG\lsy123.png";
    283     int width = 0, height = 0;
    284     CImage p_w_picpath, p_w_picpath1;
    285     p_w_picpath.Load(filepathname); //加载图片 
    286     width = p_w_picpath.GetWidth();
    287     height = p_w_picpath.GetHeight();
    288 
    289     //计算1毫米等于多少像素(图纸尺寸和图纸PNG像素对比)
    290     double AA = width / DrawW;
    291     double BB = height / DrawH;
    292 
    293     //计算距离(图纸尺寸和矩形最大边界间距)
    294     double Xdistance = (DrawW - (Xmax[XmaxBig] - Xmin[0]))*AA;
    295     double Ydistance = (DrawH - (Ymax[YmaxBig] - Ymin[0]))*BB;
    296 
    297     //图片裁剪,创建新的png
    298     p_w_picpath1.Create(width - Xdistance, height - Ydistance, p_w_picpath.GetBPP()); // 创建一个目标存储对象 
    299     p_w_picpath.BitBlt(p_w_picpath1.GetDC(), 0, 0, width - Xdistance, height - Ydistance, Xmin[0] * AA, (DrawH - Ymax[YmaxBig])*BB, SRCCOPY);  //COPY原图的局部到目标对象里
    300     p_w_picpath1.Save(filepathname1);  // 保存处理后的图片
    301     p_w_picpath1.ReleaseDC();   // 释放资源
    302     p_w_picpath1.Destroy();  // 销毁资源
    303 
    304 
    305     /* Terminate the API environment */
    306     UF_CALL(UF_terminate());
    307 }
    308 
    309 /*****************************************************************************
    310 **  Utilities
    311 *****************************************************************************/
    312 
    313 /* Unload Handler
    314 **     This function specifies when to unload your application from Unigraphics.
    315 **     If your application registers a callback (from a MenuScript item or a
    316 **     User Defined Object for example), this function MUST return
    317 **     "UF_UNLOAD_UG_TERMINATE". */
    318 extern int ufusr_ask_unload( void )
    319 {
    320     return( UF_UNLOAD_IMMEDIATELY );
    321 }
    322 
    323 Caesar卢尚宇
    324 2019年11月23日

  • 相关阅读:
    session
    php增删改查,分页
    sql题
    php简单的数据增删改查
    php简单登录注册验证
    js题
    jQHTML(获取内容和属性)
    jQ效果(动画)
    Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)
    POJ 2891 中国剩余定理(不互素)
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/11920531.html
Copyright © 2020-2023  润新知