VS环境下 C++版本:
1 int GetPreviewImage(const TCHAR* prtFile, const TCHAR* imageFile) 2 { 3 IStorage* pStorage = NULL; 4 HRESULT hResult = StgOpenStorage(prtFile, NULL, STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &pStorage); 5 if (hResult != S_OK) 6 { 7 return -1; 8 } 9 10 IStorage* pSubStorage = NULL; 11 hResult = pStorage->OpenStorage(_T("images"), NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0, &pSubStorage); 12 if (hResult != S_OK) 13 { 14 return -1; 15 } 16 17 IStream *pStream = NULL; 18 hResult = pSubStorage->OpenStream( L"preview", NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStream ); 19 if (hResult != S_OK) 20 { 21 return -1; 22 } 23 24 const int nLen = 10*1024*1024;// 准备读入的长度 25 byte* image = new byte[nLen]; 26 if (NULL == image) 27 { 28 return -1; 29 } 30 31 ULONG actRead; 32 pStream->Read( 33 image, // 存放放入的数据的缓冲区 34 nLen, // 要读入数据的长度,如不清楚可以设为较大的数 35 &actRead // 实际读入的长度 36 ); 37 38 pStream->Release(); 39 pStorage->Release(); 40 pSubStorage->Release(); 41 42 ofstream outfile(imageFile, ios::binary); 43 outfile.write((char*)image, actRead); 44 outfile.close(); 45 delete image; 46 return 0; 47 }
PS:在GTAC上看到官方提供了一个C#版本,https://solutions.industrysoftware.automation.siemens.com/view.php?sort=desc&q=get+part+preview+image+&file_type=text&i=nx_api3944&k=2&o=0
实现如下:
1 NX API 2 Sample NX Open .NET C# program : create bitmap from selected part preview 3 Note: 4 5 GTAC provides programming examples for illustration only, and assumes that you are familiar 6 with the programming language being demonstrated and the tools used to create and debug procedures. 7 GTAC support professionals can help explain the functionality of a particular procedure, but we 8 will not modify these examples to provide added functionality or construct procedures to meet your specific needs. 9 10 11 12 // Shows existing preview image from work part or selected part 13 // and also works external with 'run_journal' 14 using System; 15 using System.Drawing; 16 using System.Collections.Generic; 17 using System.ComponentModel; 18 using System.Data; 19 using System.Text; 20 using System.Windows.Forms; 21 using NXOpen; 22 23 public class Preview : Form 24 { 25 public static Session theSession; 26 public static Part wp; 27 public static ListingWindow lw; 28 29 private System.Windows.Forms.Button preview_btn; 30 private System.Windows.Forms.PictureBox pictureBox1; 31 private System.Windows.Forms.Button close_btn; 32 private System.Windows.Forms.Button load_btn; 33 private System.Windows.Forms.Button save_btn; 34 private TextBox textRelease; 35 36 public Preview() 37 { 38 InitializeComponent(); 39 } 40 41 [STAThread] 42 public static void Main(string[] args) 43 { 44 try 45 { 46 theSession = Session.GetSession(); 47 48 wp = theSession.Parts.Work; 49 lw = theSession.ListingWindow; 50 51 Preview preview = new Preview(); 52 preview.ShowDialog(); 53 } 54 catch (NXException ex) 55 { 56 MessageBox.Show(ex.Message); 57 Console.WriteLine(ex.Message); 58 } 59 60 } 61 62 private void getPreview(Part prt) 63 { 64 BasePart.HistoryEventInformation[] hInfo = prt.GetHistoryInformation(); 65 string strInfo = ""; 66 for (int i = 0; i < hInfo.Length; i++) 67 { 68 strInfo += hInfo[i] + "\r\n"; 69 } 70 textRelease.Text = strInfo; 71 72 int width = 0; 73 int height = 0; 74 int[] pixels = null; 75 76 prt.GetPreviewImage(out width, out height, out pixels); 77 if (width > 0 && height > 0) 78 { 79 Bitmap image1 = createBitmap(width, height, pixels); 80 pictureBox1.Image = image1; 81 save_btn.Enabled = true; 82 } 83 else MessageBox.Show("Part has no valid preview", "Information"); 84 } 85 86 private Bitmap createBitmap(int w, int h, int[] px) 87 { 88 //lw.WriteLine("Bitmap Image Size: " + w.ToString() + "x" + h.ToString()); 89 Bitmap bm = new Bitmap(w, h); 90 91 for (int y = 0; y < h; y++) 92 { 93 for (int x = 0; x < w; x++) 94 { 95 int pixel = px[y * w + x]; 96 Color col1 = Color.FromArgb(pixel); 97 Color col2 = Color.FromArgb(255, col1); 98 //lw.WriteLine(col2.ToString()); 99 bm.SetPixel(x, y, col2); 100 } 101 } 102 103 return bm; 104 } 105 106 public static DialogResult select_part_file(ref String filename) 107 { 108 OpenFileDialog ofd = new OpenFileDialog(); 109 DialogResult result; 110 111 ofd.Title = "Choose NX part file"; 112 ofd.AddExtension = true; 113 ofd.DefaultExt = "prt"; 114 ofd.Filter = "NX Part files (*.prt)|*.prt|All files (*.*)|*.*"; 115 ofd.FilterIndex = 1; 116 ofd.ShowHelp = true; 117 118 // start browsing at current folder, uncomment for any other default directory 119 // ofd.InitialDirectory = GetEnvironmentVariable("UGII_BASE_DIR"); 120 // ofd.InitialDirectory = GetEnvironmentVariable("UGII_BASE_DIR") + "\Moldwizard"; 121 // ofd.InitialDirectory = "c:\mypath1\mypath2"; 122 result = ofd.ShowDialog(); 123 filename = ofd.FileName; 124 ofd.Dispose(); 125 return result; 126 } 127 128 private void preview_btn_Click(object sender, EventArgs e) 129 { 130 if (wp == null) 131 { 132 MessageBox.Show("No Part", "Information"); 133 return; 134 } 135 else 136 { 137 getPreview(wp); 138 } 139 } 140 141 private void load_btn_Click(object sender, EventArgs e) 142 { 143 String filename = ""; 144 if (select_part_file(ref filename) == DialogResult.OK) 145 { 146 PartLoadStatus loadStatus = null; 147 int failCount = 0; 148 149 Part mypart = theSession.Parts.Open(filename, out loadStatus); 150 151 if (loadStatus.NumberUnloadedParts > 0) 152 failCount = reportLoadStatus(loadStatus); 153 else 154 { 155 getPreview(mypart); 156 mypart.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.CloseModified, null); 157 } 158 } 159 } 160 161 public static int reportLoadStatus(PartLoadStatus loadStatus) 162 { 163 164 lw.Open(); 165 166 int counter = 0; 167 168 do 169 { 170 171 string file = loadStatus.GetPartName(counter); 172 string failure = loadStatus.GetStatusDescription(counter); 173 int statusCode = loadStatus.GetStatus(counter); 174 MessageBox.Show("File: " + file + "\nCode: " + statusCode + "\nStatus: " + failure, "Unloaded Part"); 175 176 counter += 1; 177 } 178 while (!(counter == loadStatus.NumberUnloadedParts)); 179 180 181 return counter; 182 } 183 184 private void save_btn_Click(object sender, EventArgs e) 185 { 186 SaveFileDialog save = new SaveFileDialog(); 187 save.Filter = "Bitmap File(*.bmp)|*.bmp"; 188 if (save.ShowDialog() == DialogResult.OK) 189 { 190 pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Bmp); 191 } 192 } 193 194 private void close_btn_Click(object sender, EventArgs e) 195 { 196 Close(); 197 } 198 199 private void InitializeComponent() 200 { 201 this.preview_btn = new System.Windows.Forms.Button(); 202 this.pictureBox1 = new System.Windows.Forms.PictureBox(); 203 this.close_btn = new System.Windows.Forms.Button(); 204 this.load_btn = new System.Windows.Forms.Button(); 205 this.save_btn = new System.Windows.Forms.Button(); 206 this.textRelease = new System.Windows.Forms.TextBox(); 207 ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 208 this.SuspendLayout(); 209 // 210 // preview_btn 211 // 212 this.preview_btn.Location = new System.Drawing.Point(14, 481); 213 this.preview_btn.Name = "preview_btn"; 214 this.preview_btn.Size = new System.Drawing.Size(75, 23); 215 this.preview_btn.TabIndex = 0; 216 this.preview_btn.Text = "Work Part"; 217 this.preview_btn.UseVisualStyleBackColor = true; 218 this.preview_btn.Click += new System.EventHandler(this.preview_btn_Click); 219 // 220 // pictureBox1 221 // 222 this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 223 this.pictureBox1.Location = new System.Drawing.Point(14, 12); 224 this.pictureBox1.Name = "pictureBox1"; 225 this.pictureBox1.Size = new System.Drawing.Size(534, 400); 226 this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; 227 this.pictureBox1.TabIndex = 1; 228 this.pictureBox1.TabStop = false; 229 // 230 // close_btn 231 // 232 this.close_btn.Location = new System.Drawing.Point(473, 481); 233 this.close_btn.Name = "close_btn"; 234 this.close_btn.Size = new System.Drawing.Size(75, 23); 235 this.close_btn.TabIndex = 2; 236 this.close_btn.Text = "Close"; 237 this.close_btn.UseVisualStyleBackColor = true; 238 this.close_btn.Click += new System.EventHandler(this.close_btn_Click); 239 // 240 // load_btn 241 // 242 this.load_btn.Location = new System.Drawing.Point(95, 481); 243 this.load_btn.Name = "load_btn"; 244 this.load_btn.Size = new System.Drawing.Size(75, 23); 245 this.load_btn.TabIndex = 3; 246 this.load_btn.Text = "Load Part"; 247 this.load_btn.UseVisualStyleBackColor = true; 248 this.load_btn.Click += new System.EventHandler(this.load_btn_Click); 249 // 250 // save_btn 251 // 252 this.save_btn.Enabled = false; 253 this.save_btn.Location = new System.Drawing.Point(191, 481); 254 this.save_btn.Name = "save_btn"; 255 this.save_btn.Size = new System.Drawing.Size(75, 23); 256 this.save_btn.TabIndex = 3; 257 this.save_btn.Text = "Save BMP"; 258 this.save_btn.UseVisualStyleBackColor = true; 259 this.save_btn.Click += new System.EventHandler(this.save_btn_Click); 260 // 261 // textRelease 262 // 263 this.textRelease.BackColor = System.Drawing.SystemColors.Control; 264 this.textRelease.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 265 this.textRelease.Location = new System.Drawing.Point(14, 418); 266 this.textRelease.Multiline = true; 267 this.textRelease.Name = "textRelease"; 268 this.textRelease.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; 269 this.textRelease.Size = new System.Drawing.Size(534, 57); 270 this.textRelease.TabIndex = 4; 271 // 272 // Preview 273 // 274 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 275 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 276 this.ClientSize = new System.Drawing.Size(563, 516); 277 this.Controls.Add(this.load_btn); 278 this.Controls.Add(this.save_btn); 279 this.Controls.Add(this.close_btn); 280 this.Controls.Add(this.pictureBox1); 281 this.Controls.Add(this.preview_btn); 282 this.Controls.Add(this.textRelease); 283 this.Name = "Preview"; 284 this.Text = "NX Part Preview"; 285 ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); 286 this.ResumeLayout(false); 287 this.PerformLayout(); 288 289 } 290 291 }