package com.teamcenter.rac.kernel; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Vector; /** * 此类继承系统InterfaceDatasetAction类,用于重写preProcess方法在打开 数据集 时前操作。 * * @author yangh * @version 1.0, 2015-01-6 */ public class OpenDatasetSignoff implements InterfaceDatasetAction { @Override public boolean postProcess(TCComponentDataset arg0, String arg1, int arg2) { // TODO Auto-generated method stub return false; } @SuppressWarnings("deprecation") @Override public int preProcess(TCComponentDataset tccomponentdataset, AEShell arg1, int arg2) { // System.out.println(arg1.askSelectedFileName()); // TODO Auto-generated method stub TCPreferenceService tcp = tccomponentdataset.getSession() .getPreferenceService(); // type+"_signoff" type+"_signoff"+"_attr" // IMAN_master_form_rev:FormType // Rev.item_id=域名 // Form.object_name=域名 try { StringBuffer itemMsgSb = new StringBuffer(); TCComponentItemRevision tempRev = (TCComponentItemRevision) tccomponentdataset .getReferenceProperty("item_revision");// 获得当前数据集所在版本对象 if (tempRev == null) { return 0; } tempRev.refresh(); if (tempRev != null) { if (tccomponentdataset.getType().equals("TM2_AutoCAD") || tccomponentdataset.getType().equals("JF3_DWG")) { openCad(); String tempType = tempRev.getType(); this.getClass().getProtectionDomain().getCodeSource() .getLocation().getPath(); String itemID = ""; if (tempType.equals("TM2_TedrawingRevision")) { TCComponent[] comps = tempRev .getReferenceListProperty("IMAN_master_form_rev"); if (comps.length > 0) itemID = comps[0].getStringProperty("tm2_tuhao"); } else { itemID = tempRev.getStringProperty("item_id"); } itemMsgSb .append(itemID) .append("|") .append(tempRev .getStringProperty("item_revision_id")); // tempRev.getStringProperty("item_revision_id"); String fileName = System.getenv("TEMP") + "\\open_cad_info.txt"; try { BufferedWriter wr = new BufferedWriter(new FileWriter( fileName)); wr.write(itemMsgSb.toString()); wr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("版本类型:" + tempRev.getType()); String str1 = tcp.getString( TCPreferenceService.TC_preference_site, tempRev.getType() + "_signoff"); String[] str2 = tcp.getStringArray( TCPreferenceService.TC_preference_site, tempRev.getType() + "_signoff_attr"); if (str1 != null && str1.indexOf(":") > 0 && str2 != null && str2.length > 0) {// 含有获取Form的属性情况 String rel = str1.split(":")[0]; String formType = str1.split(":")[1]; System.out.println("关系名称:" + rel); TCComponent[] subComponent = tempRev .getRelatedComponents(rel); for (int i = 0; i < subComponent.length; i++) { subComponent[i].refresh(); System.out.println("遍历的Form类型:" + subComponent[i].getType() + " ReadForm =" + formType); if (subComponent[i] instanceof TCComponentForm && subComponent[i].getType().equals(formType)) { System.out.println("ReadForm"); DatasetParser dp = new DatasetParser(tempRev, (TCComponentForm) subComponent[i]); Vector txtFileContent = dp .parsePreference(str2); String txtFilePath = System.getenv("TEMP") + "\\signoff.txt";// 为了VB程序操作的方便将生成的txt文档的路径和名字写死了。 TxtFileRWHelper helper = new TxtFileRWHelper( txtFilePath); try { helper.writeTxtFile(txtFileContent); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } else { System.out.println("NOT FORM"); } } } else if (str2 != null && str2.length > 0) {// 只获取版本属性情况 DatasetParser dp = new DatasetParser(tempRev); Vector txtFileContent = dp.parsePreference(str2); String txtFilePath = System.getenv("TEMP") + "\\signoff.txt";// 为了VB程序操作的方便将生成的txt文档的路径和名字写死了。 TxtFileRWHelper helper = new TxtFileRWHelper(txtFilePath); try { System.out.println("txtFileContent============:" + txtFileContent); helper.writeTxtFile(txtFileContent); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } catch (TCException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } public void openCad() { String path = this.getClass().getProtectionDomain().getCodeSource() .getLocation().getPath(); String exePath = path.substring(1, path.indexOf("com.connor.signoff")) .replace("/", "\\"); exePath = exePath + ""; File file = new File(exePath); if (file != null && file.exists()) { System.out.println("执行注册表修改"); Runtime rn = Runtime.getRuntime(); Process p = null; try { p = rn.exec("\"" + exePath + "\""); } catch (Exception e) { System.out.println("Error exec!"); } } else { System.out.println("不存在" + exePath + "文件"); } } /* * 解析数据集 */ class DatasetParser { private TCComponentItemRevision rev; private TCComponentForm form; public DatasetParser(TCComponentItemRevision rev, TCComponentForm form) { this.rev = rev; this.form = form; } public DatasetParser(TCComponentItemRevision rev) { this.rev = rev; } // 根据首选项值,获得对应对象的内容,并返回需要写入txt文档中的内容 public Vector parsePreference(String[] preferenceValues) throws TCException { Vector realm_attr = new Vector();// “域名=属性值”的集合 for (int i = 0; i < preferenceValues.length; i++) { String[] obj_attrs = preferenceValues[i].split("="); if (obj_attrs.length == 2) { int index = obj_attrs[0].indexOf('.'); if (index != -1) { String obj = obj_attrs[0].substring(0, index); String att = obj_attrs[0].substring(index + 1); StringBuffer sb = new StringBuffer(obj_attrs[1]);// 域名 sb.append("="); // = if ("Rev".equals(obj)) { // 属性值 String value = ""; String proValue = rev.getProperty(att); if (proValue != null) value = proValue; sb.append(value); } else if ("Form".equals(obj)) { String value = ""; String proValue = form.getProperty(att); if (proValue != null) value = proValue; sb.append(value); } realm_attr.add(sb.toString()); } else { return null; } } else { return null; } } return realm_attr; } } /* * 读,写Text文档 */ class TxtFileRWHelper { private File txtFile = null; private BufferedReader br = null; private BufferedWriter bw = null; public TxtFileRWHelper(String txtFilePath) { this.txtFile = new File(txtFilePath); if (!this.txtFile.exists()) { try { this.txtFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // 读 Text文件 // 返回值:Text文件内容,Vector每个成员相当于一行数据 public Vector readTxtFile() throws IOException { Vector txtFileContent = new Vector(); br = new BufferedReader(new FileReader(txtFile)); if (!txtFile.canRead()) { txtFile.setReadable(true); } String line = br.readLine(); while (line != null) { txtFileContent.add(line); line = br.readLine(); } br.close(); br = null; return txtFileContent; } // 写text文件 // 入参:VectorVector中的每个成员作为一行数据处理 public void writeTxtFile(Vector txtFileContent) throws IOException { bw = new BufferedWriter(new FileWriter(txtFile)); if (!txtFile.canWrite()) { txtFile.setWritable(true); } for (int i = 0; i < txtFileContent.size(); i++) { bw.write(txtFileContent.get(i)); if (i != txtFileContent.size() - 1) { bw.write("|"); } } bw.close(); bw = null; } // 删除text 文件 public void deleteTxtFile() { if (txtFile.exists()) { txtFile.delete(); } } } }