You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

411 lines
14 KiB
Java

package com.sq.customization.dialogs;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.sq.customization.util.ExcelUtil;
import com.sq.customization.util.ProgressCustomDialog;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.cme.kernel.bvr.TCComponentMfgBvrOperation;
import com.teamcenter.rac.cme.kernel.bvr.TCComponentMfgBvrPart;
import com.teamcenter.rac.cme.kernel.bvr.TCComponentMfgBvrPlantBOP;
import com.teamcenter.rac.cme.kernel.bvr.TCComponentMfgBvrProcessLine;
import com.teamcenter.rac.cme.kernel.bvr.TCComponentMfgBvrProcessStation;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.stylesheet.PropertyTextField;
import com.teamcenter.rac.util.ButtonLayout;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PropertyLayout;
import com.teamcenter.rac.util.Registry;
public class AutoTransToolDialog extends AbstractAIFDialog {
private PropertyTextField text;
private PropertyTextField idtext;
private PropertyTextField svppstext;
private PropertyTextField FCCtext;
private PropertyTextField FCCDEStext;
private PropertyTextField dongtaitext;
private PropertyTextField KCDStext;
private JButton ok;
private JButton cancel;
private JButton directory;
private Registry registry = Registry.getRegistry(AutoTransToolDialog.class);
private TCSession session;
private InterfaceAIFComponent target;
Map<String,String> no_load = new HashMap<>();
Map<String,TCComponentMfgBvrOperation> op_nodesignmap = new HashMap<>();
public AutoTransToolDialog(Frame fm,InterfaceAIFComponent target) {
super(fm);
this.target = target;
op_nodesignmap.clear();
no_load.clear();
init();
}
private void init() {
this.setTitle("自动匹配替换工具");
this.setPreferredSize(new Dimension(500,400));
this.centerToScreen();
this.setLayout(new BorderLayout());
JPanel northPane = new JPanel(new PropertyLayout(10,10,5,5,10,10));
northPane.add("1.1.left",new JLabel("零件号:"));
idtext = new PropertyTextField();
idtext.setColumns(30);
idtext.setRequired(true);
northPane.add("1.2",idtext);
northPane.add("2.1.left",new JLabel("短SVPPS:"));
svppstext = new PropertyTextField();
svppstext.setColumns(30);
svppstext.setRequired(true);
northPane.add("2.2",svppstext);
northPane.add("3.1.left",new JLabel("FCC:"));
FCCtext = new PropertyTextField();
FCCtext.setColumns(30);
FCCtext.setRequired(true);
northPane.add("3.2",FCCtext);
northPane.add("4.1.left",new JLabel("FFC中文描述:"));
FCCDEStext = new PropertyTextField();
FCCDEStext.setColumns(30);
FCCDEStext.setRequired(true);
northPane.add("4.2",FCCDEStext);
northPane.add("5.1.left",new JLabel("动态扭矩:"));
dongtaitext = new PropertyTextField();
dongtaitext.setColumns(30);
dongtaitext.setRequired(true);
northPane.add("5.2",dongtaitext);
northPane.add("6.1.left",new JLabel("KCDS:"));
KCDStext = new PropertyTextField();
KCDStext.setColumns(30);
KCDStext.setRequired(true);
northPane.add("6.2",KCDStext);
northPane.add("7.1.left",new JLabel("数据文件:"));
text = new PropertyTextField();
text.setColumns(30);
text.setRequired(true);
text.setEnabled(false);
northPane.add("7.2",text);
directory = new JButton("...");
northPane.add("7.3",directory);
JPanel southpanel = new JPanel(new ButtonLayout());
ok = new JButton("确定");
cancel = new JButton("取消");
southpanel.add(ok);
southpanel.add(cancel);
this.add(BorderLayout.CENTER,northPane);
this.add(BorderLayout.SOUTH,southpanel);
this.setVisible(true);
cancel.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
AutoTransToolDialog.this.dispose();
}});
directory.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
filechooser.setFileSelectionMode(2);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"所有Excel文件(*.xlsx)", new String[] { "xlsx" });
filechooser.setFileFilter(filter);
int r = filechooser.showDialog(null, "选择");
if (r == 0) {
text.setText(filechooser.getSelectedFile().getAbsolutePath());
if (text.getText() != "")
ok.setEnabled(true);
else
ok.setEnabled(false);
}
else if (r == 1) {
text.setText("");
ok.setEnabled(false);
}
}});
ok.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String path = text.getText();
if( path != null && path.length() > 0) {
String idcol = idtext.getText();
String svppscol = svppstext.getText();
String fcccol = FCCtext.getText();
String fccdescol = FCCDEStext.getText();
String dongtaicol = dongtaitext.getText();
String kcdscol = KCDStext.getText();
File excel = new File(path);
if(!excel.exists()) {
MessageBox.post(AutoTransToolDialog.this,"文件路径不存在", "Message", MessageBox.INFORMATION);
return;
}
if(isValidExcelColumn(idcol) && isValidExcelColumn(svppscol) && isValidExcelColumn(fcccol)
&& isValidExcelColumn(fccdescol) && isValidExcelColumn(dongtaicol) && isValidExcelColumn(kcdscol)) {
op_nodesignmap.clear();
no_load.clear();
int idcol2 = columnToNumber(idcol.toUpperCase());
int svppscol2 = columnToNumber(svppscol.toUpperCase());
int fcccol2 = columnToNumber(fcccol.toUpperCase());
int fccdescol2 = columnToNumber(fccdescol.toUpperCase());
int dongtaicol2 = columnToNumber(dongtaicol.toUpperCase());
int kcdscol2 = columnToNumber(kcdscol.toUpperCase());
try {
//读取EXCEL
//先处理选中对象层级有design的工序获取没有design的工序
opBOMLineData(target);
if(op_nodesignmap.size() > 0) {
TCComponentBOMLine parent = null;
if(target instanceof TCComponentMfgBvrProcessStation) {
TCComponentMfgBvrProcessStation station = (TCComponentMfgBvrProcessStation) target;
String id = station.getProperty("bl_item_item_id");
no_load.put(id, id);
parent = station.parent();
}
else if(target instanceof TCComponentMfgBvrPlantBOP) {
TCComponentMfgBvrPlantBOP plant = (TCComponentMfgBvrPlantBOP) target;
String id = plant.getProperty("bl_item_item_id");
no_load.put(id, id);
parent = plant.parent();
}
else if(target instanceof TCComponentMfgBvrProcessLine) {
TCComponentMfgBvrProcessLine line = (TCComponentMfgBvrProcessLine) target;
String id = line.getProperty("bl_item_item_id");
no_load.put(id, id);
parent = line.parent();
}
if(parent != null) {
System.out.println(parent.getClass()+parent.getProperty("bl_indented_title"));
getparent(parent);
}
if(op_nodesignmap.size() > 0) {
System.out.println("整个结构没有匹配的紧固件");
}
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
AutoTransToolDialog.this.dispose();
}
else {
MessageBox.post(AutoTransToolDialog.this,"请填写正确的列比如A,B,C..", "Message", MessageBox.INFORMATION);
}
Thread thread = new Thread(){
public void run(){
ProgressCustomDialog.lbStatus.setText("正在创建 ....");
}
};
ProgressCustomDialog.show(thread, "执行中", null, null, null);
//session.queueOperation(operation);
}
else {
MessageBox.post(AutoTransToolDialog.this,"请选择文件", "Message", MessageBox.INFORMATION);
}
}});
}
public static int columnToNumber(String column) {
int result = 0;
for (int i = 0; i < column.length(); i++) {
char c = column.charAt(i);
result = result * 26 + (c - 'A' + 1); // 将字母转换为数字
}
return result;
}
// 判断列名是否符合A-Z, AA, AB等格式不区分大小写
public static boolean isValidExcelColumn(String column) {
// 将输入字符串转为大写字母并匹配正则表达式
return column != null && column.toUpperCase().matches("[A-Z]+");
}
//先处理选中对象层级有design的工序
private void opBOMLineData(InterfaceAIFComponent parent) throws Exception {
AIFComponentContext[] children = parent.getChildren();
for(AIFComponentContext child:children) {
InterfaceAIFComponent childcom = child.getComponent();
if(childcom instanceof TCComponentMfgBvrOperation) {
TCComponentMfgBvrOperation op = (TCComponentMfgBvrOperation) childcom;
String bl_rev_object_name = op.getProperty("bl_rev_object_name");
if(bl_rev_object_name.contains("预紧") || bl_rev_object_name.contains("终紧")
|| bl_rev_object_name.contains("紧固")|| bl_rev_object_name.contains("拧紧")
|| bl_rev_object_name.contains("接地")|| bl_rev_object_name.contains("搭铁")) {
AIFComponentContext[] design_children = op.getChildren();
boolean hasdesign = false;
for(AIFComponentContext designchild:design_children) {
InterfaceAIFComponent designchildcom = designchild.getComponent();
if(designchildcom instanceof TCComponentMfgBvrPart) {
hasdesign = true;
//获取design
}
}
if(!hasdesign) {
bl_rev_object_name = bl_rev_object_name.replaceAll("预紧", "");
bl_rev_object_name = bl_rev_object_name.replaceAll("终紧", "");
bl_rev_object_name = bl_rev_object_name.replaceAll("紧固", "");
bl_rev_object_name = bl_rev_object_name.replaceAll("拧紧", "");
// String bl_item_item_id = op.getProperty("bl_item_item_id");
op_nodesignmap.put(bl_rev_object_name, op);
}
}
//
}
else if(childcom instanceof TCComponentMfgBvrProcessStation || childcom instanceof TCComponentMfgBvrProcessLine
|| childcom instanceof TCComponentMfgBvrPlantBOP) {
opBOMLineData(childcom);
}
}
}
/**
* no_load:id
* */
private void getparent(TCComponentBOMLine parent)throws Exception{
if(op_nodesignmap.size() > 0) {
String parentid = parent.getProperty("bl_item_item_id");
AIFComponentContext[] children = parent.getChildren();
for (AIFComponentContext child : children) {
InterfaceAIFComponent childcom = child.getComponent();
String id = childcom.getProperty("bl_item_item_id");
if(op_nodesignmap.size() == 0) {
break;
}
if(no_load.containsKey(id)) {
continue;
}
if(childcom instanceof TCComponentMfgBvrOperation) {
TCComponentMfgBvrOperation op = (TCComponentMfgBvrOperation) childcom;
final String bl_rev_object_name = op.getProperty("bl_rev_object_name");
Set<String> keys = op_nodesignmap.keySet();
boolean found = false;
String haskey = "";
for (String key : keys) {
if (bl_rev_object_name.contains(key)) {
found = true;
haskey = key;
break; // 一旦找到了就退出循环
}
}
if(found) {
AIFComponentContext[] design_children = op.getChildren();
boolean hasdesign = false;
for(AIFComponentContext designchild:design_children) {
InterfaceAIFComponent designchildcom = designchild.getComponent();
if(designchildcom instanceof TCComponentMfgBvrPart) {
hasdesign = true;
//获取design
}
}
if(hasdesign) {
op_nodesignmap.remove(haskey);
}
}
}
else if(childcom instanceof TCComponentMfgBvrProcessStation || childcom instanceof TCComponentMfgBvrProcessLine
|| childcom instanceof TCComponentMfgBvrPlantBOP) {
getparent((TCComponentBOMLine)childcom);
}
if(op_nodesignmap.size() > 0) {
no_load.put(id, id);
}
else {
break;
}
}
//
if(op_nodesignmap.size() > 0) {
no_load.put(parentid, parentid);
if(parent.parent() != null)
getparent(parent.parent());
}
}
}
private void readExcel(File excelfile,int start_rownum,int idcol2,int svppscol2,int fcccol2
,int fccdescol2,int dongtaicol,int kcdscol2) {
Workbook work = null;
try(InputStream is = new FileInputStream(excelfile);){
if (excelfile.getAbsolutePath().endsWith(".xls"))
work = new HSSFWorkbook(is);
else {
work = new XSSFWorkbook(is);
}
Sheet sheet = work.getSheetAt(0);
int count = sheet.getPhysicalNumberOfRows();
for(int i = start_rownum; i < count ;i++) {
Row row = sheet.getRow(i);
String id = ExcelUtil.getStringCellValue(row.getCell(idcol2-1));
String svpps = ExcelUtil.getStringCellValue(row.getCell(svppscol2-1));
String fcc = ExcelUtil.getStringCellValue(row.getCell(fcccol2-1));
String fccdes = ExcelUtil.getStringCellValue(row.getCell(fccdescol2-1));
String dongtai = ExcelUtil.getStringCellValue(row.getCell(dongtaicol-1));
String kcds = ExcelUtil.getStringCellValue(row.getCell(kcdscol2-1));
System.out.println("id:"+id+" svpps:"+svpps+" fcc:"+fcc+" fccdes:"+fccdes+" dongtai:"+dongtai+" kcds:"+kcds);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}