NX二次开发-将工程图上的每个视图导出PNG图片

大概思路是将每个视图导出PDF,在调另一个项目的EXE(PDF转PNG)

  1 //ExportDrawViewPng
  2 
  3 // Mandatory UF Includes
  4 #include <uf.h>
  5 #include <uf_object_types.h>
  6 
  7 // Internal Includes
  8 #include <NXOpen/ListingWindow.hxx>
  9 #include <NXOpen/NXMessageBox.hxx>
 10 #include <NXOpen/UI.hxx>
 11 
 12 // Internal+External Includes
 13 #include <NXOpen/Annotations.hxx>
 14 #include <NXOpen/Assemblies_Component.hxx>
 15 #include <NXOpen/Assemblies_ComponentAssembly.hxx>
 16 #include <NXOpen/Body.hxx>
 17 #include <NXOpen/BodyCollection.hxx>
 18 #include <NXOpen/Face.hxx>
 19 #include <NXOpen/Line.hxx>
 20 #include <NXOpen/NXException.hxx>
 21 #include <NXOpen/NXObject.hxx>
 22 #include <NXOpen/Part.hxx>
 23 #include <NXOpen/PartCollection.hxx>
 24 #include <NXOpen/Session.hxx>
 25 
 26 
 27 #include <uf.h>
 28 #include <uf_ui.h>
 29 #include <uf_draw.h>
 30 #include <uf_cfi.h>
 31 #include <uf_drf.h>
 32 #include <uf_obj.h>
 33 #include <uf_part.h>
 34 #include <uf_cgm.h>
 35 #include <windows.h>
 36 #include <NXOpen/PrintPDFBuilder.hxx>
 37 #include <NXOpen/PlotManager.hxx>
 38 
 39 // Std C++ Includes
 40 #include <iostream>
 41 #include <sstream>
 42 
 43 using namespace NXOpen;
 44 using namespace std;
 45 using std::string;
 46 using std::exception;
 47 using std::stringstream;
 48 using std::endl;
 49 using std::cout;
 50 using std::cerr;
 51 
 52 
 53 //------------------------------------------------------------------------------
 54 // NXOpen c++ test class 
 55 //------------------------------------------------------------------------------
 56 class MyClass
 57 {
 58     // class members
 59 public:
 60     static Session *theSession;
 61     static UI *theUI;
 62 
 63     MyClass();
 64     ~MyClass();
 65 
 66     void do_it();
 67     void print(const NXString &);
 68     void print(const string &);
 69     void print(const char*);
 70 
 71     void ExportPdf(tag_t drawing_tag, char* outFilePath, char* outPdfFilePath);
 72 
 73 private:
 74     Part *workPart, *displayPart;
 75     NXMessageBox *mb;
 76     ListingWindow *lw;
 77     LogFile *lf;
 78 };
 79 
 80 //------------------------------------------------------------------------------
 81 // Initialize static variables
 82 //------------------------------------------------------------------------------
 83 Session *(MyClass::theSession) = NULL;
 84 UI *(MyClass::theUI) = NULL;
 85 
 86 //------------------------------------------------------------------------------
 87 // Constructor 
 88 //------------------------------------------------------------------------------
 89 MyClass::MyClass()
 90 {
 91 
 92     // Initialize the NX Open C++ API environment
 93     MyClass::theSession = NXOpen::Session::GetSession();
 94     MyClass::theUI = UI::GetUI();
 95     mb = theUI->NXMessageBox();
 96     lw = theSession->ListingWindow();
 97     lf = theSession->LogFile();
 98 
 99     workPart = theSession->Parts()->Work();
100     displayPart = theSession->Parts()->Display();
101 
102 }
103 
104 //------------------------------------------------------------------------------
105 // Destructor
106 //------------------------------------------------------------------------------
107 MyClass::~MyClass()
108 {
109 }
110 
111 //------------------------------------------------------------------------------
112 // Print string to listing window or stdout
113 //------------------------------------------------------------------------------
114 void MyClass::print(const NXString &msg)
115 {
116     if (!lw->IsOpen()) lw->Open();
117     lw->WriteLine(msg);
118 }
119 void MyClass::print(const string &msg)
120 {
121     if (!lw->IsOpen()) lw->Open();
122     lw->WriteLine(msg);
123 }
124 void MyClass::print(const char * msg)
125 {
126     if (!lw->IsOpen()) lw->Open();
127     lw->WriteLine(msg);
128 }
129 
130 
131 
132 void MyClass::ExportPdf(tag_t drawing_tag, char* outFilePath, char* outPdfFilePath)
133 {
134     if (drawing_tag != NULL_TAG)
135     {
136         UF_CGM_export_options_t export_options;
137         UF_CGM_ask_default_export_options(&export_options);
138         //UF_CGM_ask_session_export_options(&export_options);//用这个函数也可以初始化
139         export_options.reason = UF_CGM_pdf_reason;
140         UF_CGM_set_session_export_options(&export_options);
141 
142         UF_CGM_export_cgm(drawing_tag, &export_options, outFilePath); //导出成CGM文件
143 
144         //将CGM转换成PDF
145         NXOpen::NXString nxbasedir = theSession->GetEnvironmentVariableValue("UGII_BASE_DIR");//获取NX主目录
146         std::ostringstream tempstring;
147         tempstring << nxbasedir.GetLocaleText() << "\\NXPLOT\\bin\\pdf\\cgm2pdf.exe " << outFilePath << " " << outPdfFilePath;
148         std::string covertvalule = tempstring.str();
149         WinExec(covertvalule.c_str(), SW_HIDE); //打开PDF转换器,并转换
150         tempstring.str("");
151         tempstring.clear();
152     }
153 }
154 
155 
156 //------------------------------------------------------------------------------
157 // Do something
158 //------------------------------------------------------------------------------
159 void MyClass::do_it()
160 {
161 
162     // TODO: add your code here
163 
164     UF_initialize();
165 
166     //获得当前图纸页tag
167     tag_t drawing_tag = NULL_TAG;
168     UF_DRAW_ask_current_drawing(&drawing_tag);
169 
170     //打开图纸页
171     UF_DRAW_open_drawing(drawing_tag);
172 
173 
174     //遍历图纸所有尺寸
175     vector<tag_t> DimAll;
176     tag_t DimTag = NULL_TAG;
177     UF_OBJ_cycle_objs_in_part1(UF_PART_ask_display_part(), UF_dimension_type, &DimTag);//遍历所有尺寸
178     while (DimTag != NULL_TAG)
179     {
180         DimAll.push_back(DimTag);//把所有尺寸tag添加到vector里
181 
182         UF_OBJ_cycle_objs_in_part1(UF_PART_ask_display_part(), UF_dimension_type, &DimTag);//遍历所有尺寸
183     }
184 
185     int Count = 1;
186     //获得图纸页上的所有视图
187     int num_views = 0;
188     tag_t* view_tag = NULL_TAG;
189     UF_DRAW_ask_views(drawing_tag, &num_views, &view_tag);
190     for (int i = 0; i < num_views; i++)
191     {
192         //获得视图的最大边界
193         double view_borders[4];
194         UF_DRAW_ask_view_borders(view_tag[i], view_borders);
195 
196         //获得视图原点
197         double ViewOrigin[2];
198         ViewOrigin[0] = (view_borders[2] - view_borders[0]) / 2 + view_borders[0];
199         ViewOrigin[1] = (view_borders[3] - view_borders[1]) / 2 + view_borders[1];
200 
201         //更新视图
202         UF_DRAW_update_one_view(drawing_tag, view_tag[i]);
203 
204         //先移动下视图位置,为视图移动到下一张图纸页上做准备
205         double drawing_reference_point1[2] = { 50, 50 };
206         UF_DRAW_move_view(view_tag[i], drawing_reference_point1);
207 
208         //更新视图
209         UF_DRAW_update_one_view(drawing_tag, view_tag[i]);
210 
211         //获取视图上面尺寸的最大边界
212         //由最大边界来定义新的图纸页大小
213         //此步骤还没写
214         //......
215 
216 
217         //新建图纸页    
218         char msg[256];
219         sprintf_s(msg, "%d", Count);
220         UF_DRAW_info_t DrawingInfo;
221         DrawingInfo.size_state = UF_DRAW_CUSTOM_SIZE;
222         DrawingInfo.size.custom_size[0] = view_borders[3] - view_borders[1] + 50;
223         DrawingInfo.size.custom_size[1] = view_borders[2] - view_borders[0] + 50;
224         DrawingInfo.drawing_scale = 1.0;
225         DrawingInfo.units = UF_PART_METRIC;
226         DrawingInfo.projection_angle = UF_DRAW_FIRST_ANGLE_PROJECTION;
227         tag_t DrawTAG1 = NULL_TAG;
228         UF_DRAW_create_drawing(msg, &DrawingInfo, &DrawTAG1);
229 
230 
231         //移动视图到其他图纸页
232         UF_DRAW_move_view_to_drawing(view_tag[i], DrawTAG1);
233 
234         //移动每个视图到居中位置
235         double drawing_reference_point[2] = { (view_borders[2] - view_borders[0] + 60) / 2, (view_borders[3] - view_borders[1] + 60) / 2 };
236         UF_DRAW_move_view(view_tag[i], drawing_reference_point);
237 
238         //转换(设置导出路径)
239         char msgCGM[256];
240         sprintf_s(msgCGM, "D:\\PNG\\lsy%d.cgm", Count);
241 
242         char msgPDF[256];
243         sprintf_s(msgPDF, "D:\\PNG\\lsy%d.pdf", Count);
244 
245         //将图纸页导出PDF
246         ExportPdf(DrawTAG1, msgCGM, msgPDF);
247 
248         //转换
249         char Pdf2Png[256];
250         string A = "D:\\PNG\\";
251         string B = msg;
252         string PngName = A + B;
253         sprintf_s(Pdf2Png, "D:\\Pdf2PngTools\\Pdf2Png.exe %s %s", msgPDF, PngName.c_str());
254 
255         //判断文件是否存在
256         int status = 0;
257         UF_CFI_ask_file_exist("D:\\Pdf2PngTools\\Pdf2Png.exe", &status);
258         if (status != 0)
259         {
260             uc1601("提示:D:\\Pdf2PngTools\\Pdf2Png.exe程序不存在", 1);
261             return;
262         }
263 
264         Sleep(1000);//这个地方必须得延迟一下,要不然连续调EXE导出就会报错
265 
266         //调EXE,PDF转PNG
267         system(Pdf2Png);
268 
269         //删除.pdf和.cgm
270         DeleteFile(msgPDF);//.pdf
271         DeleteFile(msgCGM);//.cgm
272 
273         Count++;
274     }
275 
276 
277     UF_terminate();
278 }
279 
280 //------------------------------------------------------------------------------
281 // Entry point(s) for unmanaged internal NXOpen C/C++ programs
282 //------------------------------------------------------------------------------
283 //  Explicit Execution
284 extern "C" DllExport void ufusr(char *parm, int *returnCode, int rlen)
285 {
286     try
287     {
288         // Create NXOpen C++ class instance
289         MyClass *theMyClass;
290         theMyClass = new MyClass();
291         theMyClass->do_it();
292         delete theMyClass;
293     }
294     catch (const NXException& e1)
295     {
296         UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
297     }
298     catch (const exception& e2)
299     {
300         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
301     }
302     catch (...)
303     {
304         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
305     }
306 }
307 
308 
309 //------------------------------------------------------------------------------
310 // Unload Handler
311 //------------------------------------------------------------------------------
312 extern "C" DllExport int ufusr_ask_unload()
313 {
314     return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
315 }
316 
317 Caesar卢尚宇
318 2019年11月23日

猜你喜欢

转载自www.cnblogs.com/nxopen2018/p/11919820.html
今日推荐