NX二次开发-获得图纸页尺寸的最大边界做VC截图

/*****************************************************************************
**
** 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[1024];
    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[133];
        UF_get_fail_message(irc, err);
        ECHO("*** ERROR code %d at line %d in %s: ",
            irc, line, file);
        ECHO("+++ %s ", err);
        ECHO("%s; ", 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 = 0;
       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 = 0; i < num_views; i++)
       {
              double view_borders[4];
              UF_CALL(UF_DRAW_ask_view_borders(view_tag[i], view_borders));//视图最大边界
              //添加到vector
              Xmin.push_back(view_borders[0]);
              Ymin.push_back(view_borders[1]);
              Xmax.push_back(view_borders[2]);
              Ymax.push_back(view_borders[3]);
       }
       //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() - 1;
       sort(Ymax.begin(), Ymax.end());
       Ymax.erase(unique(Ymax.begin(), Ymax.end()), Ymax.end());
       int YmaxBig1 = Ymax.size() - 1;
       
       //遍历所有尺寸
       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 = 0;
              double dim_origin[3];
              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[0] > Xmax[XmaxBig1])
              {
                     //添加到vector
                     Xmax.push_back(dim_origin[0]);
              }
              else if (dim_origin[0] < Xmin[0])
              {
                     Xmin.push_back(dim_origin[0]);
              }
              else if (dim_origin[1] > Ymax[YmaxBig1])
              {      
                     Ymax.push_back(dim_origin[1]);
              }
              else if (dim_origin[1] < Ymin[0])
              {
                     Ymin.push_back(dim_origin[1]);
              }
              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() - 1;
       sort(Ymax.begin(), Ymax.end());
       Ymax.erase(unique(Ymax.begin(), Ymax.end()), Ymax.end());
       int YmaxBig = Ymax.size() - 1;
       //创建直线
       UF_CURVE_line_t line_coords1;
       line_coords1.start_point[0] = Xmin[0];
       line_coords1.start_point[1] = Ymin[0];
       line_coords1.start_point[2] = 0;
       line_coords1.end_point[0] = Xmin[0];
       line_coords1.end_point[1] = Ymax[YmaxBig];
       line_coords1.end_point[2] = 0;
       tag_t line[4];
       UF_CALL(UF_CURVE_create_line(&line_coords1, &line[0]));
       UF_CURVE_line_t line_coords2;
       line_coords2.start_point[0] = Xmin[0];
       line_coords2.start_point[1] = Ymax[YmaxBig];
       line_coords2.start_point[2] = 0;
       line_coords2.end_point[0] = Xmax[XmaxBig];
       line_coords2.end_point[1] = Ymax[YmaxBig];
       line_coords2.end_point[2] = 0;
       UF_CALL(UF_CURVE_create_line(&line_coords2, &line[1]));
       UF_CURVE_line_t line_coords3;
       line_coords3.start_point[0] = Xmax[XmaxBig];
       line_coords3.start_point[1] = Ymax[YmaxBig];
       line_coords3.start_point[2] = 0;
       line_coords3.end_point[0] = Xmax[XmaxBig];
       line_coords3.end_point[1] = Ymin[0];
       line_coords3.end_point[2] = 0;
       UF_CALL(UF_CURVE_create_line(&line_coords3, &line[2]));
       UF_CURVE_line_t line_coords4;
       line_coords4.start_point[0] = Xmax[XmaxBig];
       line_coords4.start_point[1] = Ymin[0];
       line_coords4.start_point[2] = 0;
       line_coords4.end_point[0] = Xmin[0];
       line_coords4.end_point[1] = Ymin[0];
       line_coords4.end_point[2] = 0;
       UF_CALL(UF_CURVE_create_line(&line_coords4, &line[3]));
       //将图纸页导出PDF
       ExportPdf(drawing_tag, "D:\PNG\lsy.cgm", "D:\PNG\lsy.pdf");
       //转换
       char Pdf2Png[256];
       sprintf_s(Pdf2Png, "D:\Pdf2PngTools\Pdf2Png.exe %s %s", "D:\PNG\lsy.pdf", "D:\PNG\lsy");
       //判断文件是否存在
       int status = 0;
       UF_CALL(UF_CFI_ask_file_exist("D:\Pdf2PngTools\Pdf2Png.exe", &status));
       if (status != 0)
       {
              uc1601("提示:D:\Pdf2PngTools\Pdf2Png.exe程序不存在", 1);
              return;
       }
       Sleep(1000);//这个地方必须得延迟一下,要不然调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[0];
       double DrawW = drawing_info.size.custom_size[1];
       //图片裁剪
       CString filepathname = "D:\PNG\lsy1.png", filepathname1 = "D:\PNG\lsy123.png";
       int width = 0, height = 0;
       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[0]))*AA;
       double Ydistance = (DrawH - (Ymax[YmaxBig] - Ymin[0]))*BB;
       //图片裁剪,创建新的png
       p_w_picpath1.Create(width - Xdistance, height - Ydistance, p_w_picpath.GetBPP()); // 创建一个目标存储对象
       p_w_picpath.BitBlt(p_w_picpath1.GetDC(), 0, 0, width - Xdistance, height - Ydistance, Xmin[0] * 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 );
}
 
阿飞
2021年8月2日
原文地址:https://www.cnblogs.com/nxopen2018/p/14880082.html