NX二次开发:保存时导出PDF并打开

打印 上一主题 下一主题

主题 918|帖子 918|积分 2754

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

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

鼠扑

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表