好久没有来写UG二次开发方面的博客文章了。
UGPart(*.prt)是一个复合文档。Windows系统里有不少常用的文件都是复合文档,比如Office里的xls、doc等等。
复合文档不仅包括文本而且包括图形、电子表格数据、声音、视频图像以及其它信息。UGPart(*.prt)里在除了包括模型数据之外还包括用于预览显示的图像,获取该图像数据在某些工程中是很有必要的。
通过下面的代码可以遍历复合文档,了解其内部文档结构:
////////////////////////////////////////////////////////////////////////////// // 版权所有(C) 2011 All right reserved // 文件名称: cycle_compound_doc.cpp // 当前版本: 1.0.0.1 // 作 者: 翔剑风鸣 (zale_lzj@163.com) // 设计日期: 2011年4月19日 // 内容摘要: 遍历复合文档,显示其相关文档结构 // // 编译环境:VS2008,控制台工程 ////////////////////////////////////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <objbase.h> #include <assert.h> #include <iostream> void DisplayStorageTree(IStorage* pStorage) { using namespace std; const char* const type_names[] = { "STGTY_STORAGE", "STGTY_STREAM", "STGTY_LOCKBYTES", "STGTY_PROPERTY" }; IEnumSTATSTG* penum = NULL; STATSTG statstg; HRESULT hResult; memset(&statstg, 0, sizeof(statstg)); hResult = pStorage->EnumElements(NULL, NULL, NULL, &penum); if (FAILED(hResult)) { assert(false); return; } hResult = penum->Next(1, &statstg, NULL); while (S_OK == hResult) { cout << type_names[statstg.type - STGTY_STORAGE] << " "; wcout << statstg.pwcsName << endl; if (statstg.type == STGTY_STORAGE) { cout << endl << "enum substorage begin" << endl; IStorage* pSubStorage = NULL; HRESULT hr = pStorage->OpenStorage(statstg.pwcsName, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0, &pSubStorage); assert(hr == S_OK); DisplayStorageTree(pSubStorage); cout << "enum substorage end" << endl << endl; } if (NULL != statstg.pwcsName) CoTaskMemFree(statstg.pwcsName); hResult = penum->Next(1, &statstg, NULL); } } int cycle_compound_doc(const char* file_name) { IStorage* pStorage = NULL; HRESULT hResult; wchar_t path_file[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, file_name, -1, path_file, MAX_PATH); hResult = StgOpenStorage(path_file, NULL, STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &pStorage); if (hResult != S_OK) return -1; DisplayStorageTree(pStorage); pStorage->Release(); pStorage = NULL; return 0; } int main(int argc, char *argv[]) { const char* file_name = "D:\\Example\\7260 f.prt"; cycle_compound_doc(file_name); return 0; }
执行结果如下所示:
part—>images-->preview 这就是UGPart的预览图像,可获取该部分所包含的数据存储成为图像文件或直接用于程序。(推荐使用高版本的VS工具,对图像文件的处理更容易些)
图纸的预览图像也存放于images之下。