1 #include <iostream> 2 #include<iomanip> 3 #include "cgns_io.h" 4 5 using namespace std; 6 7 8 void readChildrenByIdentifier(int index_file, double id) { 9 int result; 10 char errorMessage[100]; 11 /** 12 * @brief 获取节点的子节点数 13 **/ 14 int numOfChild = 0; 15 result = cgio_number_children(index_file, id, &numOfChild); 16 if (CGIO_ERR_NONE != result) { 17 cgio_error_message(errorMessage); 18 cout << "GetChildNum Of Node(id = " << setprecision(50) << id << ") Error: " << errorMessage << endl; 19 } 20 else { 21 char name[50]; 22 cgio_get_name(index_file, id, name); 23 cout << "ChildNum Of Node(name = " << name << ") = " << numOfChild << endl; 24 } 25 /** 26 * @brief 如果没有子节点则返回 27 **/ 28 if (numOfChild < 1) { 29 return; 30 } 31 /** 32 * @brief 获取节点的子节点的标识符、名称等信息 33 **/ 34 int numOfReturnChild; 35 double* childIds = new double[numOfChild]; 36 char** childNames = new char*[numOfChild]; 37 38 for (int i = 0; i < numOfChild; i++) 39 { 40 childNames[i] = new char[50]; 41 } 42 result = cgio_children_ids(index_file, id, 1, numOfChild, &numOfReturnChild, childIds); 43 if (CGIO_ERR_NONE != result) { 44 cgio_error_message(errorMessage); 45 cout << "GetChildIds Error: " << errorMessage << endl; 46 } 47 else { 48 for (int i = 1; i <= numOfReturnChild; i++) 49 { 50 char name[50]; char label[50]; char type[5]; 51 cgio_get_name(index_file, childIds[i - 1], name); 52 cgio_get_name(index_file, childIds[i - 1], label); 53 cgio_get_data_type(index_file, childIds[i - 1], type); 54 //注意double类型数据用cout输出时的格式 55 //cout << "Children[" << i << "] Id = " << setprecision(50) << childIds[i - 1] << ", Name = " << name << ", Label = " << label << ", Type = " << type << endl; 56 //cout << "Name = " << name << endl; 57 58 /** 59 * @brief 继续迭代获取子节点 60 **/ 61 readChildrenByIdentifier(index_file, childIds[i - 1]); 62 } 63 } 64 } 65 66 int main() { 67 int result; 68 char errorMessage[100]; 69 string filename = "F:/SomeData/data_631meshfile_formatcgns/axie_new_volume.cgns"; 70 71 72 /** 73 * @brief 获取文件类型ADF\HDF5\ADF2 74 **/ 75 int file_type; 76 result = cgio_check_file(filename.c_str(), &file_type); 77 if (CGIO_ERR_NONE == result) { 78 switch (file_type) 79 { 80 case CGIO_FILE_ADF: 81 cout << "FileType: ADF;" << endl; 82 break; 83 case CGIO_FILE_HDF5: 84 cout << "FileType: HDF5;" << endl; 85 break; 86 case CGIO_FILE_ADF2: 87 cout << "FileType: ADF2;" << endl; 88 break; 89 default: 90 cout << "未知的文件格式;" << endl; 91 break; 92 } 93 } 94 else 95 { 96 cgio_error_message(errorMessage); 97 cout << "Get File Type Error: " << errorMessage << endl; 98 } 99 100 /** 101 * @brief 获取文件的标识符 102 **/ 103 int index_file; 104 result = cgio_open_file(filename.c_str(), CGIO_MODE_READ, file_type, &index_file); 105 if (CGIO_ERR_NONE != result) { 106 cgio_error_message(errorMessage); 107 cout << "Open Error: " << errorMessage << endl; 108 } 109 else { 110 cout << "Success to open cgns file!" << endl; 111 } 112 113 /** 114 * @brief 获取根节点的标识符 115 **/ 116 double rootid; 117 result = cgio_get_root_id(index_file, &rootid); 118 if (CGIO_ERR_NONE != result) { 119 cgio_error_message(errorMessage); 120 cout << "GetRootId Error: " << errorMessage << endl; 121 } 122 else { 123 cout << "rootid = " << setprecision(50) << rootid << endl; 124 } 125 126 127 readChildrenByIdentifier(index_file, rootid); 128 129 130 cgio_close_file(index_file); 131 132 return 0; 133 }
运行结果: