#include "kutil.h" #include #include #include #include #include "libxl.h" #include #include #include #include #include #include #include #include #include #include #include #include "libxl.h" #include #include #include #include #include #include #include #include #include #include #include #include #include std::string getTimestamp() { std::time_t now = std::time(nullptr); char buf[100] = ""; std::strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", std::localtime(&now)); return std::string(buf); } std::string getTempDirectory() { const char* tempDir = getenv("TEMP"); return std::string(tempDir); } std::string generatePDFFileName() { std::string timestamp = getTimestamp(); std::string tempDir = getTempDirectory(); return tempDir + "\\工艺合本_" + timestamp + ".pdf"; } // 创建空的PDF文件 int createEmptyPDFFile(const std::string& pdfFilePath) { // 创建空的PDF文件 std::ofstream pdfFile(pdfFilePath, std::ios::binary); if (!pdfFile) { std::cerr << "PDF文件创建失败: " << pdfFilePath << std::endl; return -1; } // 关闭文件 pdfFile.close(); std::cout << "空PDF文件已创建: " << pdfFilePath << std::endl; return 0; } /* * 生成唯一时间戳 */ std::string getUniqueTimestamp() { // 获取当前时间点 auto now = std::chrono::system_clock::now(); // 将时间点转换为 time_t std::time_t now_time_t = std::chrono::system_clock::to_time_t(now); // 获取纳秒部分 auto nanoseconds = std::chrono::duration_cast(now.time_since_epoch()).count() % 1000000000; // 将 time_t 转换为本地时间 std::tm now_tm = *std::localtime(&now_time_t); // 格式化时间字符串 std::ostringstream oss; oss << std::put_time(&now_tm, "%Y%m%d%H%M%S") << "" << std::setw(9) << std::setfill('0') << nanoseconds; return oss.str(); } /** * * @HandlerName: F6_MEP_Merge * @HandlerType: ActionHandler * @Description: 工艺路线下工序的PDF工艺生成合本工艺 * @author: hcj * @date: 2024年10月24日 * */ int F6_MEP_Merge(EPM_action_message_t msg) { // Define error message printf("Define error message\n"); string err = ""; // Check if folder and file exist, if not, create them const char* directory_path = "D:\\log"; const char* file_path = "D:\\log\\F6_MEP_Merge.log"; printf("开始获取日志文件F6_MEP_Merge.log\n"); CreateDirectoryAndFile(directory_path, file_path); // Open log file in append mode printf("Open log file in append mode\n"); auto logFile = std::make_unique(file_path, std::ios::app); if (!logFile->is_open()) { return -1; } // Get current time printf("Get current time\n"); auto now = std::chrono::system_clock::now(); std::time_t time1 = std::chrono::system_clock::to_time_t(now); std::string timeStr = std::ctime(&time1); timeStr = timeStr.substr(0, timeStr.size() - 1); *logFile << "[" << timeStr << "] "; *logFile << "=========================工艺路线下工序的PDF工艺生成合本工艺start===================" << std::endl; tag_t rootTask = NULLTAG; printf("Get task object\n"); // Get task object EPM_ask_root_task(msg.task, &rootTask); //获取参数 printf("获取参数\n"); char* argflag = NULL, * argvalue = NULL, * arg = NULL; char checkType[1024] = ""; char itemChildType[1024] = ""; char topRelation[1024] = ""; char relation[1024] = ""; int arg_cnt = TC_number_of_arguments(msg.arguments); if (arg_cnt > 0) { for (int i = 0; i < arg_cnt; i++) { //获取下一个参数(从0开始) arg = TC_next_argument(msg.arguments); //获取参数的名称和值 ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue); if (strcmp(argflag, "itemType") == 0) { if (argvalue != NULL) { strcpy(checkType, argvalue); *logFile << "[" << timeStr << "] " << "获取流程参数itemType:" << checkType << std::endl; } } if (strcmp(argflag, "itemChildType") == 0) { if (argvalue != NULL) { strcpy(itemChildType, argvalue); *logFile << "[" << timeStr << "] " << "获取流程参数itemChildType:" << checkType << std::endl; } } if (strcmp(argflag, "topRelation") == 0) { if (argvalue != NULL) { strcpy(topRelation, argvalue); *logFile << "[" << timeStr << "] " << "获取流程参数topRelation:" << topRelation << std::endl; } } if (strcmp(argflag, "relation") == 0) { if (argvalue != NULL) { strcpy(relation, argvalue); *logFile << "[" << timeStr << "] " << "获取流程参数relation:" << relation << std::endl; } } } } printf("获取参数\n"); int attachments_num = 0; tag_t* attachments = NULLTAG; *logFile << "[" << timeStr << "] " << "开始获取流程目标下的bom对象" << std::endl; EPM_ask_attachments(rootTask, EPM_target_attachment, &attachments_num, &attachments); *logFile << "[" << timeStr << "] " << "目标下的对象个数:" << attachments_num << std::endl; for (int i = 0; i < attachments_num; i++) { tag_t compont = attachments[i]; //获取类型 char* type; AOM_ask_value_string(compont, "object_type", &type); printf("object_type:%s\n", type); vector types; Split(checkType, ",", types); *logFile << "[" << timeStr << "] " << "判断目标对象类型" << type << "是否包含在" << checkType << "里面" << std::endl; if (std::find(types.begin(), types.end(), type) != types.end()) { printf("yes\n"); *logFile << "[" << timeStr << "] " << "存在" << std::endl; //开始遍历工艺BOM //获取bom行数据 校验数据 tag_t top_line = NULLTAG, window = NULLTAG; *logFile << "[" << timeStr << "] " << "开始创建 BOM 窗口" << std::endl; ITKCALL(BOM_create_window(&window)); // 调用 BOM_create_window 创建 BOM 窗口 *logFile << "[" << timeStr << "] " << "BOM 窗口创建完成" << std::endl; // 设置顶级 BOM *logFile << "[" << timeStr << "] " << "开始设置顶级 BOM" << std::endl; ITKCALL(BOM_set_window_top_line(window, NULLTAG, compont, NULLTAG, &top_line)); // 设置顶级 BOM 行 *logFile << "[" << timeStr << "] " << "顶级 BOM 设置完成" << std::endl; //在当前的temp目录下创建一个空的pdf文件 准备合并工序pdf std::string pdfFilePath = generatePDFFileName(); // 创建空的PDF文件 std::ofstream pdfFile(pdfFilePath, std::ios::binary); if (!pdfFile) { EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, "PDF文件创建失败"); return -1; } // 关闭文件 pdfFile.close(); createEmptyPDFFile(pdfFilePath); int result = createEmptyPDFFile(pdfFilePath); if (result == 0) { std::cout << "PDF文件创建成功" << std::endl; } else { std::cerr << "PDF文件创建失败" << std::endl; } *logFile << "[" << timeStr << "] " << "PDF文件已创建: " << pdfFilePath << std::endl; char cmd[16384] = ""; strcpy(cmd, "java -jar D:\\Siemens\\Teamcenter13\\bin\\MERGEPDF.jar"); strcat(cmd, " \""); strcat(cmd, pdfFilePath.c_str()); //获取工艺下的pdf文件 *logFile << "[" << timeStr << "] " << "获取工艺下的pdf文件" << std::endl; vector topr; Split(topRelation, ",", topr); for (int b = 0; b < topr.size(); b++) { *logFile << "[" << timeStr << "] " << "获取" << topr[b] << "关系下的pdf数据集" << std::endl; tag_t* gypdf_tags = NULLTAG; int gypdf_num = NULL; ITKCALL(AOM_ask_value_tags(compont, topr[b].c_str(), &gypdf_num, &gypdf_tags)); tag_t gypdf = NULLTAG; for (int n = 0; n < gypdf_num; n++) { char* pdftype = ""; char* pdfname = ""; ITKCALL(AOM_ask_value_string(gypdf_tags[n], "object_type", &pdftype)); char* name1 = NULL; char* name2 = NULL; if (strcmp(pdftype, "PDF") == 0) { //获取文件地址 char* fileName = NULL; ITKCALL(AOM_ask_value_string(gypdf_tags[n], "object_name", &fileName)); char* documType = "PDF_Reference"; printf("开始复制文件到file空间\n"); tag_t ref_object = NULLTAG; AE_reference_type_t reference_type; //char ref_name[WSO_name_size_c + 1] = "excel"; *logFile << "[" << timeStr << "] " << "documType" << documType << std::endl; ITKCALL(AE_ask_dataset_named_ref2(gypdf_tags[n], documType, &reference_type, &ref_object)); if (reference_type == AE_PART_OF) { char sendname[SS_MAXPATHLEN] = ""; strcpy(sendname, getenv("TEMP")); strcat(sendname, "\\"); strcat(sendname, getUniqueTimestamp().c_str()); strcat(sendname, "-"); strcat(sendname, fileName); *logFile << "[" << timeStr << "] " << "sendname" << sendname << std::endl; FILE* file = fopen(sendname, "r"); if (file) { fclose(file); // 关闭文件 remove(sendname); // 删除文件 } ITKCALL(IMF_export_file(ref_object, sendname)); strcat(cmd, "\" \""); strcat(cmd, sendname); } } } } //获取工序 int count; tag_t* children_line; ITKCALL(BOM_line_ask_all_child_lines(top_line, &count, &children_line)); *logFile << "[" << timeStr << "] " << "工序行数: " << count << std::endl; *logFile << "[" << timeStr << "] " << "开始遍历工艺下的工序对象" << std::endl; for (int j = 0; j < count; j++) { tag_t revobj = NULL; *logFile << "[" << timeStr << "] " << "获取工序的版本对象" << std::endl; ITKCALL(AOM_ask_value_tag(children_line[j], "bl_line_object", &revobj)); // 获取版本对象 char* childtype; AOM_ask_value_string(revobj, "object_type", &childtype); vector childtypes; Split(itemChildType, ",", childtypes); *logFile << "[" << timeStr << "] " << "判断子对象类型" << childtype << "是否包含在" << itemChildType << "里面" << std::endl; if (std::find(childtypes.begin(), childtypes.end(), childtype) != childtypes.end()) { *logFile << "[" << timeStr << "] " << "存在" << std::endl; vector real; Split(relation, ",", real); for (int b1 = 0; b1 < real.size(); b1++) { //获取LY6_translatePDF关系下的pdf数据集 *logFile << "[" << timeStr << "] " << "获取" << real[b1] << "关系下的pdf数据集" << std::endl; tag_t* pdf_tags = NULLTAG; int pdf_num = NULL; ITKCALL(AOM_ask_value_tags(revobj, real[b1].c_str(), &pdf_num, &pdf_tags)); tag_t pdf = NULLTAG; for (int n = 0; n < pdf_num; n++) { char* pdftype = ""; char* pdfname = ""; ITKCALL(AOM_ask_value_string(pdf_tags[n], "object_type", &pdftype)); char* name1 = NULL; char* name2 = NULL; if (strcmp(pdftype, "PDF") == 0) { //获取文件地址 char* fileName = NULL; ITKCALL(AOM_ask_value_string(pdf_tags[n], "object_name", &fileName)); char* documType = "PDF_Reference"; printf("开始复制文件到file空间\n"); tag_t ref_object = NULLTAG; AE_reference_type_t reference_type; //char ref_name[WSO_name_size_c + 1] = "excel"; *logFile << "[" << timeStr << "] " << "documType" << documType << std::endl; ITKCALL(AE_ask_dataset_named_ref2(pdf_tags[n], documType, &reference_type, &ref_object)); if (reference_type == AE_PART_OF) { char sendname[SS_MAXPATHLEN] = ""; strcpy(sendname, getenv("TEMP")); strcat(sendname, "\\"); strcat(sendname, getUniqueTimestamp().c_str()); strcat(sendname, "-"); strcat(sendname, fileName); *logFile << "[" << timeStr << "] " << "sendname" << sendname << std::endl; FILE* file = fopen(sendname, "r"); if (file) { fclose(file); // 关闭文件 remove(sendname); // 删除文件 } ITKCALL(IMF_export_file(ref_object, sendname)); strcat(cmd, "\" \""); strcat(cmd, sendname); } } } } } } strcat(cmd, "\""); *logFile << "[" << timeStr << "] " << "cmd: " << cmd << std::endl; system(cmd); //挂载数据集 *logFile << "[" << timeStr << "] " << "查找工艺下LY6_mergePDF关系下是否存在pdf数据集" << std::endl; tag_t* word_tags = NULLTAG; int word_num = 0; char* comName; ITKCALL(AOM_ask_value_tags(compont, "LY6_mergePDF", &word_num, &word_tags)); ITKCALL(AOM_ask_value_string(compont, "item_id", &comName)) tag_t pdf = NULLTAG; for (int n = 0; n < word_num; n++) { char* type = ""; char* pdfname = ""; ITKCALL(AOM_ask_value_string(word_tags[n], "object_type", &type)); ITKCALL(AOM_ask_value_string(word_tags[n], "object_name", &pdfname)); if (strcmp(type, "PDF") == 0 && strcmp(comName, pdfname) == 0) { pdf = word_tags[n]; break; } } if (pdf == NULLTAG) { *logFile << "[" << timeStr << "] " << "没有找到PDF数据集,开始进行创建" << std::endl; int rfCount; tag_t pdfTypeTag = NULLTAG, tool = NULLTAG, relationTag, * rfTags, newrelation; ITKCALL(AE_find_datasettype2("PDF", &pdfTypeTag)); ITKCALL(AE_create_dataset_with_id(pdfTypeTag, comName, "", "", "A", &pdf)); ITKCALL(AE_ask_datasettype_def_tool(pdfTypeTag, &tool)); AOM_lock(pdf); ITKCALL(AE_set_dataset_tool(pdf, tool)); ITKCALL(AE_set_dataset_format2(pdf, "PDF_Reference")); ITKCALL(AOM_save(pdf)); AOM_unlock(pdf); if (pdf == NULLTAG) { *logFile << "[" << timeStr << "] " << "创建失败" << std::endl; } else { *logFile << "[" << timeStr << "] " << "创建成功" << std::endl; ITKCALL(GRM_find_relation_type("LY6_mergePDF", &relationTag)); if (relationTag == NULLTAG) { } ITKCALL(GRM_list_secondary_objects_only(compont, relationTag, &rfCount, &rfTags)); ITKCALL(GRM_create_relation(compont, pdf, relationTag, NULLTAG, &newrelation)); ITKCALL(GRM_save_relation(newrelation)); } } if (pdf != NULL) { char namefix[256] = ""; strcpy(namefix, comName); strcat(namefix, ".pdf"); import_dataset_file_binary(pdf, "/temp/", "PDF_Reference", "pdf", (char*)pdfFilePath.c_str(), namefix); } } else { printf("no\n"); } } return ITK_ok; }