Teamcenter_SOA开发:使用查询构建器查询数据
Teamcenter中的查询构建器可以查询对象信息,今天使用Item Name这个查询来查询Teamcenter中的对象,并打印出对象的信息。搭建环境参见Teamcenter_SOA开发:使用SOA登录Teamcenter - huangym1 - 博客园 (cnblogs.com)
1 //tcnx_projectexe.h 2 #pragma once 3 // Mandatory UF Includes 4 #include <uf.h> 5 #include <uf_object_types.h> 6 #include <uf_ugmgr.h> 7 #include <uf_part.h> 8 #include <uf_disp.h> 9 #include <uf_modl.h> 10 #include <uf_obj.h> 11 #include <uf_assem.h> 12 13 // Internal+External Includes 14 #include <NXOpen/Annotations.hxx> 15 #include <NXOpen/Assemblies_Component.hxx> 16 #include <NXOpen/Assemblies_ComponentAssembly.hxx> 17 #include <NXOpen/Body.hxx> 18 #include <NXOpen/BodyCollection.hxx> 19 #include <NXOpen/Face.hxx> 20 #include <NXOpen/Line.hxx> 21 #include <NXOpen/NXException.hxx> 22 #include <NXOpen/NXObject.hxx> 23 #include <NXOpen/Part.hxx> 24 #include <NXOpen/PartCollection.hxx> 25 #include <NXOpen/Session.hxx> 26 27 #include <NXOpen/PDM_PdmSession.hxx> 28 #include <NXOpen/PDM_SoaConnectionHandle.hxx> 29 #include <teamcenter/soa/client/Connection.hxx> 30 #include <teamcenter/services/core/DatamanagementService.hxx> 31 #include <teamcenter/soa/common/Version.hxx> 32 33 // check mate 34 #include <NXOpen/Validate_ValidationManager.hxx> 35 #include <NXOpen/Validate_Validator.hxx> 36 #include <NXOpen/Validate_ValidatorOptions.hxx> 37 #include <NXOpen/Validate_Parser.hxx> 38 39 // Std C++ Includes 40 #include <iostream> 41 #include <sstream> 42 #include <stdio.h> 43 #include <stdlib.h> 44 45 using namespace NXOpen; 46 using namespace Teamcenter::Soa::Client; 47 using namespace Teamcenter::Services::Core; 48 using std::string; 49 using std::exception; 50 using std::stringstream; 51 using std::endl; 52 using std::cout; 53 using std::cerr; 54 55 #define MAX_UGMGR_NAME_LEN 1024 56 #define CREATION_DATE 1 57 #define MODIFICATION_DATE 2 58 static int indent_level = 0; 59 60 #define CHECK( func_ ) \ 61 ifail = (func_); \ 62 if (ifail != 0) {\ 63 printf("ERROR: %s returned %d", # func_, ifail); \ 64 return ifail;} 65 66 #define PRINT( content_ ) \ 67 { int ii; \ 68 for (ii = 0; ii < indent_level; ii++) \ 69 { printf(" "); } \ 70 printf content_; \ 71 printf("\n"); } 72 73 74 //tcnx_projectexe.cpp 75 #include "tcnx_projectexe.h" 76 #include "teamcenter/clientx/AppXSession.hxx" 77 #include "teamcenter/hello/HomeFolder.hxx" 78 #include "teamcenter/hello/Query.hxx" 79 #include <teamcenter/soa/client/model/User.hxx> 80 #include <teamcenter/soa/client/RuntimeException.hxx> 81 82 using namespace Teamcenter::ClientX; 83 using namespace Teamcenter::Services::Core; 84 using namespace Teamcenter::Soa::Common; 85 using namespace Teamcenter::Soa::Client; 86 using namespace Teamcenter::Soa::Client::Model; 87 using namespace Teamcenter::Schemas::Soa::_2006_03::Exceptions; 88 using namespace Teamcenter::Hello; 89 90 91 //=================== 92 // Entry Point 93 //=================== 94 #ifdef WIN32 95 int _tmain(int argc, _TCHAR* argv[]) 96 #else 97 int main(int argc, char* argv[]) 98 #endif 99 { 100 try{ 101 102 #ifdef _UFUGMGR 103 int _errCode = 0; 104 const char** consolePara = (const char**)(argv); 105 logical is_active; 106 _errCode = UF_is_ugmanager_active(&is_active);// 判断ugmanager环境是否已经初始化 107 if (!is_active) 108 _errCode = UF_UGMGR_initialize(argc, consolePara);// 初始化ugmanager环境 109 110 //do_it(); 111 //_errCode = invokePdmServer(); 112 113 _errCode = UF_UGMGR_terminate(); 114 return _errCode; 115 #else 116 if (argc > 1){ 117 if (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "-h") == 0){ 118 cout << "usage: Hello [-host http://server:port/tc] " << endl; 119 return 0; 120 } 121 } 122 123 std::string serverHost = "http://plmdev:7001/tc"; 124 if (argc > 2 && strcmp(argv[1], "-host") == 0) 125 serverHost = argv[2]; 126 127 string _username, _password; 128 std::vector<std::string> _tempStr; 129 for (int idx = 0; idx < argc; idx++){ 130 _tempStr.push_back(argv[idx]); 131 if (string(argv[idx]).find("-u=") != std::string::npos){ 132 _username = string(argv[idx]).substr(string(argv[idx]).find_first_of("=") + 1); 133 } 134 else if (string(argv[idx]).find("-p=") != std::string::npos){ 135 _password = string(argv[idx]).substr(string(argv[idx]).find_first_of("=") + 1); 136 } 137 } 138 139 AppXSession session(serverHost, _username, _password); 140 HomeFolder home; 141 Query query; 142 143 try{ 144 User* user = session.login(); 145 if (!user) 146 return 0; 147 148 home.listHomeFolder(user); 149 query.queryItems(); 150 151 session.logout(); 152 } 153 catch (Teamcenter::Soa::Client::RuntimeException& e){ 154 cout << e.getMessage() << endl; 155 cout << "The application will terminate." << endl; 156 } 157 return 0; 158 #endif 159 160 } 161 catch (const NXException& e1){ 162 cerr << "NXException: " << e1.ErrorCode() << endl; 163 cerr << e1.Message() << endl; 164 } 165 catch (const exception& e2){ 166 cerr << "Exception: " << e2.what() << endl; 167 } 168 catch (...){ 169 cerr << "Unknown Exception: " << endl; 170 } 171 }
1 //Query.hpp 2 #ifndef TEAMCENTER_HELLO_QUERY_HXX 3 #define TEAMCENTER_HELLO_QUERY_HXX 4 5 namespace Teamcenter 6 { 7 namespace Hello 8 { 9 class Query; 10 class Query 11 { 12 public: 13 14 void queryItems(); 15 }; 16 } 17 } //end namespace 18 #endif 19 20 //Query.cpp 21 #include "Query.hxx" 22 #include "../clientx/AppXSession.hxx" 23 24 #include <teamcenter/schemas/soa/_2006_03/exceptions/ServiceException.hxx> 25 #include <teamcenter/services/query/SavedqueryService.hxx> 26 #include <teamcenter/services/core/DatamanagementService.hxx> 27 #include <teamcenter/soa/client/model/ImanQuery.hxx> 28 #include <teamcenter/soa/client/NotLoadedException.hxx> 29 30 #include <iostream> 31 #include <vector> 32 33 using namespace std; 34 using namespace Teamcenter::ClientX; 35 using namespace Teamcenter::Hello; 36 using namespace Teamcenter::Schemas::Soa::_2006_03::Exceptions; 37 using namespace Teamcenter::Services::Query; 38 using namespace Teamcenter::Services::Core; 39 using namespace Teamcenter::Soa::Common; 40 using namespace Teamcenter::Soa::Client; 41 using namespace Teamcenter::Soa::Client::Model; 42 43 44 void Query::queryItems() 45 { 46 ImanQuery *query; 47 SavedqueryService* queryService = SavedqueryService::getService(AppXSession::getConnection()); 48 DatamanagementService* dmService = DatamanagementService::getService(AppXSession::getConnection()); 49 50 try 51 { 52 SavedqueryService::GetSavedQueriesResponse savedQueries = queryService->getSavedQueries(); 53 54 if (savedQueries.queries.size() == 0){ 55 cout << "There are no saved queries in the system." << endl; 56 return; 57 } 58 for (size_t i = 0; i < savedQueries.queries.size(); i++) 59 { 60 if (savedQueries.queries[i].name == "Item Name") 61 { 62 query = savedQueries.queries[i].query; 63 break; 64 } 65 } 66 } 67 catch (ServiceException& e) 68 { 69 cout << "GetSavedQueries service request failed." << endl; 70 cout << e.getMessage() << endl; 71 return; 72 } 73 if (query == nullptr){ 74 cout << "There is not an 'Item Name' query." << endl; 75 return; 76 } 77 78 try 79 { 80 // Search for all Items, returning a maximum of 25 objects 81 vector<Teamcenter::Services::Query::_2008_06::Savedquery::QueryInput> savedQueryInputs; 82 vector< ModelObject* > limitList; 83 vector< string > entries; 84 vector< string > values; 85 entries.push_back("Item Name"); 86 values.push_back("*"); 87 Teamcenter::Services::Query::_2008_06::Savedquery::QueryInput savedQueryInput; 88 savedQueryInput.query = query; 89 savedQueryInput.maxNumToReturn = 300; 90 savedQueryInput.limitList = limitList; 91 savedQueryInput.entries = entries; 92 savedQueryInput.values = values; 93 savedQueryInput.resultsType = 0; 94 savedQueryInputs.push_back(savedQueryInput); 95 96 SavedqueryService::SavedQueriesResponse queryResponse = queryService->executeSavedQueries(savedQueryInputs); 97 SavedqueryService::QueryResults found = queryResponse.arrayOfResults[0]; 98 99 cout << "" << endl; 100 cout << "Found Items:" << endl; 101 102 // Page through the results 10 at a time 103 for (size_t i = 0; i < found.objectUIDS.size(); i += 10) 104 { 105 size_t pageSize = (i + 10 < found.objectUIDS.size()) ? 10 : found.objectUIDS.size() - i; 106 107 vector<string> uids; 108 for (size_t j = 0; j < pageSize; j++){ 109 uids.push_back(found.objectUIDS[i + j]); 110 } 111 ServiceData sd = dmService->loadObjects(uids); 112 vector<ModelObject* > foundObjs = sd.getPlainObjs(); 113 AppXSession::printObjects(foundObjs); 114 } 115 } 116 catch (ServiceException& e){ 117 cout << "ExecuteSavedQuery service request failed." << endl; 118 cout << e.getMessage() << endl; 119 return; 120 } 121 }
程序调试运行查询GIF动图:
黄河远上白云间,一片孤城万仞山。
羌笛何须怨杨柳,春风不度玉门关。
诗人初到凉州,面对黄河、边城的辽阔景象,又耳听着《折杨柳》曲,有感而发,写成了这首表现戍守边疆的士兵思念家乡情怀的诗作。
诗的前两句描绘了西北边地广漠壮阔的风光。首句抓住自下(游)向上(游)、由近及远眺望黄河的特殊感受,描绘出“黄河远上白云间”的动人画面:汹涌澎湃波浪滔滔的黄河竟像一条丝带迤逦飞上云端。写得真是神思飞跃,气象开阔。诗人的另一名句“黄河入海流”,其观察角度与此正好相反,是自上而下的目送;而李白的“黄河之水天上来”,虽也写观望上游,但视线运动却又由远及近,与此句不同。“黄河入海流”和“黄河之水天上来”,同是着意渲染黄河一泻千里的气派,表现的是动态美。而“黄河远上白云间”,方向与河的流向相反,意在突出其源远流长的闲远仪态,表现的是一种静态美。同时展示了边地广漠壮阔的风光,不愧为千古奇句。