NX二次開發:儲存時匯出PDF並開啟

2023-03-25 06:02:04

該工程為在儲存時執行開發的功能,函數入口點ufput。其他還有新建、開啟、另存等都可以加入開發的操作,具體看UF_EXIT下的介紹。

使用者出口是一個可選特性,允許你在NX中某些預定義的位置(或出口)自動執行Open C API程式。如果你進入其中一個出口,NX會檢查你是否定義了指向Open C API程式位置的指標。如果定義了指標,NX將執行Open C API程式。指標是一個環境變數。

注意:

一定要設定環境變數指向自己生成的DLL。例如:USER_FILE=E:\workspace\Infore\tcnx_project\application\tcnx_project.dll

  1 // Mandatory UF Includes
  2 #include <uf.h>
  3 #include <uf_object_types.h>
  4 #include <uf_draw.h>
  5 #include <uf_part.h>
  6 
  7 // Internal+External Includes
  8 #include <NXOpen/Annotations.hxx>
  9 #include <NXOpen/Assemblies_Component.hxx>
 10 #include <NXOpen/Assemblies_ComponentAssembly.hxx>
 11 #include <NXOpen/Body.hxx>
 12 #include <NXOpen/BodyCollection.hxx>
 13 #include <NXOpen/Face.hxx>
 14 #include <NXOpen/Line.hxx>
 15 #include <NXOpen/NXException.hxx>
 16 #include <NXOpen/NXObject.hxx>
 17 #include <NXOpen/Part.hxx>
 18 #include <NXOpen/PartCollection.hxx>
 19 #include <NXOpen/Session.hxx>
 20 
 21 #include <NXOpen/PrintPDFBuilder.hxx>
 22 #include <NXOpen/PlotManager.hxx>
 23 #include <NXOpen/Drawings_DrawingSheet.hxx>
 24 #include <NXOpen/NXObjectManager.hxx>
 25 
 26 // Std C++ Includes
 27 #include <iostream>
 28 #include <sstream>
 29 #include <vector>
 30 #include <string>
 31 #include <algorithm>
 32 #include <tchar.h>
 33 #include <atlconv.h>
 34 #include <shellapi.h>
 35 
 36 #include <windows.h>
 37 #undef CreateDialog
 38 #pragma comment(lib,"shell32.lib")
 39 
 40 using namespace NXOpen;
 41 using std::string;
 42 using std::exception;
 43 using std::stringstream;
 44 using std::endl;
 45 using std::cout;
 46 using std::cerr;
 47 
 48 
 49 //------------------------------------------------------------------------------
 50 // Unload Handler
 51 //------------------------------------------------------------------------------
 52 extern "C" DllExport int ufusr_ask_unload()
 53 {
 54     return (int)NXOpen::Session::LibraryUnloadOptionImmediately;// 偵錯用
 55     //return (int)NXOpen::Session::LibraryUnloadOptionAtTermination;// 程式釋出用
 56     //return (int)NXOpen::Session::LibraryUnloadOptionExplicitly;
 57 }
 58 
 59 int exportDwg2PDF(double &xDimension, double &yDimension, std::string &waterRemark, tag_t &sheetTAG, std::string &exportPath, bool appendStatus)
 60 {
 61     try{
 62         if (xDimension < 200 || yDimension < 200 || sheetTAG == NULL_TAG || exportPath.empty() == true) 
 63             return -1;
 64 
 65         NXOpen::Session *theSession = NXOpen::Session::GetSession();
 66         NXOpen::Part *workPart(theSession->Parts()->Work());
 67         NXOpen::Part *displayPart(theSession->Parts()->Display());
 68         NXOpen::PrintPDFBuilder *printPDFBuilder1;
 69         printPDFBuilder1 = workPart->PlotManager()->CreatePrintPdfbuilder();
 70 
 71         printPDFBuilder1->SetScale(1.0);
 72         printPDFBuilder1->SetSize(NXOpen::PrintPDFBuilder::SizeOptionScaleFactor);
 73         printPDFBuilder1->SetOutputText(NXOpen::PrintPDFBuilder::OutputTextOptionPolylines);
 74         printPDFBuilder1->SetXDimension(xDimension);
 75         printPDFBuilder1->SetYDimension(yDimension);
 76         printPDFBuilder1->SetColors(NXOpen::PrintPDFBuilder::ColorBlackOnWhite);
 77         printPDFBuilder1->SetWidths(NXOpen::PrintPDFBuilder::WidthCustomThreeWidths);
 78         printPDFBuilder1->SetRasterImages(true);
 79         printPDFBuilder1->SetImageResolution(NXOpen::PrintPDFBuilder::ImageResolutionOptionHigh);
 80         printPDFBuilder1->SetAddWatermark(true);
 81         printPDFBuilder1->SetWatermark(waterRemark.c_str());
 82         printPDFBuilder1->SetAppend(appendStatus);
 83 
 84         std::vector<NXOpen::NXObject *> sheets1(1);
 85         NXOpen::Drawings::DrawingSheet *drawingSheet1(dynamic_cast<NXOpen::Drawings::DrawingSheet *>(NXOpen::NXObjectManager::Get(sheetTAG)));
 86         sheets1[0] = drawingSheet1;
 87         printPDFBuilder1->SourceBuilder()->SetSheets(sheets1);
 88         printPDFBuilder1->SetFilename(exportPath);
 89 
 90         NXOpen::NXObject *nXObject1;
 91         nXObject1 = printPDFBuilder1->Commit();
 92         printPDFBuilder1->Destroy();
 93         return 0;
 94     }
 95     catch (const exception& e2){
 96         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
 97         throw;
 98     }
 99 }
100 
101 int getSheetInfos()
102 {
103     // 獲取顯示部件及圖紙資訊
104     int _errCode = 0;
105     tag_t dispTAG = UF_PART_ask_display_part();
106     char part_fspec[MAX_FSPEC_BUFSIZE] = { 0 };
107     if (_errCode = UF_PART_ask_part_name(dispTAG, part_fspec) != 0) return _errCode;
108 
109     std::string strPartName(part_fspec);
110     transform(strPartName.begin(), strPartName.end(), strPartName.begin(), ::tolower);
111     if (strPartName.find("dwg") == string::npos) return -1;
112 
113     int num_draws = 0;
114     tag_t *drawTAGs = nullptr;
115     if (_errCode = UF_DRAW_ask_drawings(&num_draws, &drawTAGs) != 0)
116         return _errCode;
117 
118     string export_path = strPartName.substr(0, strPartName.find_last_of("."));
119     for (int idx = 0; idx < num_draws; idx++){
120         // 匯出PDF
121         UF_DRAW_info_t drawInfos;
122         _errCode = UF_DRAW_ask_drawing_info(drawTAGs[0], &drawInfos);
123         double xDimension = drawInfos.size.custom_size[0];
124         double yDimension = drawInfos.size.custom_size[1];
125         _errCode = exportDwg2PDF(xDimension, yDimension, string("huangym1\r\n2023-03-25"), drawTAGs[idx], export_path + ".pdf", false);
126         string tempStr(export_path + ".pdf");
127 
128         // 開啟PDF
129         USES_CONVERSION;
130         const WCHAR * cLineChar = A2W(tempStr.c_str());
131 
132         SHELLEXECUTEINFO sei;
133         ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));//使用前最好清空
134         sei.cbSize = sizeof(SHELLEXECUTEINFO);//管理員許可權執行cmd,最基本的使用與 ShellExecute 類似
135         sei.lpFile = cLineChar;
136         sei.nShow = SW_SHOW;
137         sei.lpVerb = _T("open");
138         BOOL bResult = ShellExecuteEx(&sei);
139         if (bResult)//執行成功
140         {
141             if (sei.hProcess)//指定 SEE_MASK_NOCLOSEPROCESS 並其成功執行,則 hProcess 將會返回執行成功的程序控制程式碼
142                 WaitForSingleObject(sei.hProcess, INFINITE);//等待執行完畢
143         }
144     }
145     if (drawTAGs){
146         UF_free(drawTAGs);
147         drawTAGs = nullptr;
148     }
149     return _errCode;
150 }
151 
152 //========================
153 // 儲存操作入口點函數
154 //========================
155 extern "C" DllExport void ufput()
156 {
157     try{
158         if (UF_initialize()) return;
159 
160         getSheetInfos();
161 
162         UF_terminate();
163     }
164     catch (const NXException& e1)
165     {
166         UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
167     }
168     catch (const exception& e2)
169     {
170         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
171     }
172     catch (...)
173     {
174         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
175     }
176 }

 

GIF動圖展示: