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

400 lines
20 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KPlan.Util;
using Teamcenter.Schemas.Soa._2006_03.Exceptions;
using Teamcenter.Services.Strong.Core;
using Teamcenter.Services.Strong.Core._2007_01.DataManagement;
using Teamcenter.Soa.Client.Model;
using Teamcenter.Soa.Client.Model.Strong;
namespace KPlan.RefActions {
class TCTool {
public static ArrayList BomTitleName = new ArrayList();
public static ArrayList BomPropertyKey = new ArrayList();
public static ArrayList P_ARTICLE_TYPENRs = new ArrayList();
public static ArrayList P_ARTICLE_ERPNRs = new ArrayList();
public static string lineCount = "";
public static string deleteXML = "TRUE";
static public string ZW1Name = "EPLAN数据包";
static public string PDFName = "PDF图纸";
public static Dictionary<string, string> customBomPropConfig = new Dictionary<string, string>();
public static void ReadConfigs() {
BomTitleName.Clear();
BomPropertyKey.Clear();
P_ARTICLE_TYPENRs.Clear();
P_ARTICLE_ERPNRs.Clear();
customBomPropConfig.Clear();
string bomConfig = KUtil.GetConfigValue(KConfigure.CHECK_SECTION, KConfigure.SAVE_BOM_PROP);
//KUtil.Log(bomConfig);
string[] bomConfigSplit = bomConfig.Split(';');
foreach(string c in bomConfigSplit) {
string[] ss = c.Split('=');
if (ss.Length == 2) {
// KUtil.Log(ss[0]+" = "+ss[1]);
string tc = ss[0].Trim();
string eplan = ss[1].Trim();
if (!KUtil.IsEmpty(tc) && !KUtil.IsEmpty(eplan)) {
if (customBomPropConfig.ContainsKey(eplan)) {
KUtil.Log("重复配置:"+tc+"="+eplan);
}
else {
customBomPropConfig.Add(eplan, tc);
}
}
}
}
string config = KUtil.GetConfigValue(KConfigure.REF_SECTION, KConfigure.REF_BOM_TITILE);
string[] split = config.Split(';');
foreach (string c in split) {
string[] ss = c.Split('=');
if (ss.Length == 2) {
BomTitleName.Add(ss[0].Trim());
BomPropertyKey.Add(ss[1].Trim());
}
}
string erpcode = KUtil.GetConfigValue(KConfigure.REF_SECTION, KConfigure.REF_ERPCODE);
string[] erpcodeSplit = erpcode.Split(';');
foreach (string c in erpcodeSplit) {
string[] ss = c.Split('=');
if (ss.Length == 2) {
if (ss[0].Trim().StartsWith("P_ARTICLE_TYPENR")) {
P_ARTICLE_TYPENRs.Add(ss[1].Trim());
}
else {
P_ARTICLE_ERPNRs.Add(ss[1].Trim());
}
}
}
lineCount = KUtil.GetConfigValue(KConfigure.REF_SECTION, KConfigure.REF_LINECOUNT);
deleteXML = KUtil.GetConfigValue(KConfigure.REF_SECTION, KConfigure.REF_DELETEXML);
}
public static string getCNString(string descString) {
string target_descr = "";
if (descString.Contains("@") && descString.Contains(";")) {
string[] arrStr = descString.Split(';');
int arrLen = arrStr.Length;
for (int cnt = 0; cnt < arrLen; cnt++) {
if (arrStr[cnt].StartsWith("zh_CN") || arrStr[cnt].StartsWith("??")) {
target_descr = arrStr[cnt].Split('@')[1];
break;
}
}
}
else {
target_descr = descString;
}
return target_descr;
}
public static short checkZipDatasetCheckout(Dataset dataset) {
try {
ModelObject[] objects2 = { dataset };
String[] attributes2 = { "is_modifiable", "checked_out", "checked_out_user" };
//TcTool.dmService.RefreshObjects(objects2);
TCUtil.dmService.GetProperties(objects2, attributes2);
//2019.05.14下载TC数据至EPLAN 不用检查
Teamcenter.Soa.Client.Model.Strong.User checkUserInfo = dataset.Checked_out_user as Teamcenter.Soa.Client.Model.Strong.User;
if (checkUserInfo != null) {
if (checkUserInfo.Uid != TCUtil.user.Uid) {
MessageBox.Show("数据集由其他用户签出,无法保存数据集!", "提示");
return 2;
}
}
}
catch (Teamcenter.Soa.Exceptions.NotLoadedException ex) {
MessageBox.Show("错误:\r\n" + ex.Message, "提示");
return 11;
}
return 0;
}
public static ItemRevision getLastItemRev(String item_id) {
GetItemFromIdInfo OneInfo = new GetItemFromIdInfo();
GetItemFromIdInfo[] Infos = new GetItemFromIdInfo[1];
GetItemFromIdPref Pref = new GetItemFromIdPref();
OneInfo.ItemId = item_id;
Infos[0] = OneInfo;
GetItemFromIdResponse InfoResp = TCUtil.dmService.GetItemFromId(Infos, 1, Pref); // 1个数是最新版本
// MessageBox.Show(item_id + "-------------" + InfoResp.Output.Length, "提示");
if (InfoResp.Output.Length > 0) {
if (InfoResp.Output[0].ItemRevOutput.Length > 0) {
ItemRevision itemRev = InfoResp.Output[0].ItemRevOutput[0].ItemRevision;
ModelObject[] modelobject = { itemRev };
//MessageBox.Show(item_id + "-------------" + itemRev.Uid, "提示");
//dmService.RefreshObjects(modelobject);
//MessageBox.Show("RefreshObjects", "提示");
String[] attributes = { "item_id", "object_name", "item_revision_id", "creation_date", "owning_user" };
TCUtil.dmService.GetProperties(modelobject, attributes);
//MessageBox.Show("item_revision_id----------" + itemRev.Item_revision_id, "提示");
//return InfoResp.Output[0].ItemRevOutput[0].ItemRevision;
return itemRev;
}
}
return null;
}
public static short CreateOrUpdateBom(ItemRevision parentItemRevision, List<string> childItem_id, List<string> childItem_num, Dictionary<string,List<string>> custPropMap) {
//Console.WriteLine("CreateOrUpdateBom------------------"+1);
int childCount = childItem_id.Count;
//Console.WriteLine("CreateOrUpdateBom------------------"+2);
ReservationService res = ReservationService.getService(Teamcenter.ClientX.Session.getConnection());
//Console.WriteLine("CreateOrUpdateBom------------------"+3);
Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.RelativeStructureChildInfo[] children_infos = new Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.RelativeStructureChildInfo[childCount];
//Console.WriteLine("CreateOrUpdateBom------------------"+4);
for (int i = 0; i < childCount; i++) {
// Console.WriteLine("CreateOrUpdateBom------------------"+5);
// Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.AttributesInfo[] attr_infos = new Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.AttributesInfo[2];
Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.AttributesInfo[] attr_infos = new Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.AttributesInfo[1+custPropMap.Count];
//Console.WriteLine("CreateOrUpdateBom------------------"+6);
attr_infos[0] = new Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.AttributesInfo();
attr_infos[0].Name = "bl_quantity";
attr_infos[0].Value = childItem_num[i];
//Console.WriteLine("CreateOrUpdateBom------------------"+7);
int index = 1;
foreach(string key in custPropMap.Keys) {
//KUtil.Log(key+":"+custPropMap[key].Count);
string val = custPropMap[key][i];
//KUtil.Log((i+1)+". 添加BOM同步信息" + key+" = "+val);
attr_infos[index] = new Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.AttributesInfo();
attr_infos[index].Name = key;
attr_infos[index].Value =val;
index++;
}
/*attr_infos[1] = new Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.AttributesInfo();
//attr_infos[1].Name = "YSR8_InstallP_bom";//安川项目
attr_infos[1].Name = "SF6_InstallP_bom";//毛思达
// attr_infos[1].Name = "SF2_InstallP_bom";//仇俊永
attr_infos[1].Value = childItem_installpos[i];
attr_infos[2] = new Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.AttributesInfo();
attr_infos[2].Name = "SF6_Manufacturer";//毛思达
attr_infos[2].Value = childItem_manufacturer[i];*/
//Console.WriteLine("CreateOrUpdateBom------------------"+8);
Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.RelOccInfo occ_info = new Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.RelOccInfo();
occ_info.AttrsToSet = attr_infos;
//Console.WriteLine("CreateOrUpdateBom------------------"+9);
ItemRevision itemrev = getLastItemRev(childItem_id[i]);
//Console.WriteLine("CreateOrUpdateBom------------------"+10);
if (itemrev == null) {
MessageBox.Show("获取版本对象失败!\r\n" + childItem_id[i], "提示");
return 1;
}
//Console.WriteLine("CreateOrUpdateBom------------------"+11);
Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.RelativeStructureChildInfo children_info = new Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.RelativeStructureChildInfo();
children_info.Child = itemrev;
children_info.OccInfo = occ_info;
children_infos[i] = children_info;
}
Teamcenter.Services.Strong.Cad._2007_12.StructureManagement.CreateOrUpdateRelativeStructureInfo2[] struct_infos = new Teamcenter.Services.Strong.Cad._2007_12.StructureManagement.CreateOrUpdateRelativeStructureInfo2[1];
Teamcenter.Services.Strong.Cad._2007_12.StructureManagement.CreateOrUpdateRelativeStructureInfo2 struct_info = new Teamcenter.Services.Strong.Cad._2007_12.StructureManagement.CreateOrUpdateRelativeStructureInfo2();
struct_info.ChildInfo = children_infos;
struct_info.Parent = parentItemRevision;
struct_info.Precise = false;
struct_infos[0] = struct_info;
ModelObject[] objpirs = { parentItemRevision };
String[] attris = { "structure_revisions" };
//dmService.RefreshObjects(objpirs);
ServiceData s_data = TCUtil.dmService.GetProperties(objpirs, attris);
//Console.WriteLine("CreateOrUpdateBom------------------"+12);
Teamcenter.Soa.Client.Model.Strong.PSBOMViewRevision ps_rev = null;
try {
for (int j = 0; j < parentItemRevision.Structure_revisions.Length; j++) {
ps_rev = parentItemRevision.Structure_revisions[j];
ModelObject[] objs2 = { ps_rev };
String[] attris2 = { "is_modifiable", "object_string", "checked_out" };
//dmService.RefreshObjects(objs2);
TCUtil.dmService.GetProperties(objs2, attris2);
if (!ps_rev.Is_modifiable) {
MessageBox.Show("当前用户无权修改BOM", "提示");
return 11;
}
if (ps_rev.Checked_out == "Y") {
MessageBox.Show("BOM已经签出请确认签入后再执行本操作", "提示");
return 12;
}
string bvr_name = ps_rev.Object_string;
// Console.WriteLine("bvr_name------------------" + bvr_name);
ModelObject[] objbvrs = new ModelObject[1];
objbvrs[0] = ps_rev;
s_data = res.Checkout(objbvrs, "", "");
if (s_data.sizeOfPartialErrors() > 0) {
MessageBox.Show("签出BOM失败请重新执行操作", "提示");
return 13;
}
}
}
catch (ServiceException ex) {
MessageBox.Show(ex.Message);
return 14;
}
Console.WriteLine("CreateOrUpdateBom------------------" + 13);
Teamcenter.Services.Strong.Cad.StructureManagementService ss_service = Teamcenter.Services.Strong.Cad.StructureManagementService.getService(Teamcenter.ClientX.Session.getConnection());
Teamcenter.Services.Strong.Cad._2007_12.StructureManagement.CreateOrUpdateRelativeStructurePref2 struct_pref = new Teamcenter.Services.Strong.Cad._2007_12.StructureManagement.CreateOrUpdateRelativeStructurePref2();
try {
Teamcenter.Services.Strong.Cad._2007_01.StructureManagement.CreateOrUpdateRelativeStructureResponse cursResp =
ss_service.CreateOrUpdateRelativeStructure(struct_infos, "view", true, struct_pref);
// ss_service.DeleteRelativeStructure
if (cursResp.Output.Count == 0) {
// MessageBox.Show("Bom删除成功\n");
//}else{
MessageBox.Show("Bom创建失败", "提示");
}
}
catch (System.Exception ex) {
MessageBox.Show(ex.Message);
return 21;
}
finally {
if (ps_rev != null) {
ModelObject[] checkIn_vec = new ModelObject[1];
checkIn_vec[0] = ps_rev;
try {
res.Checkin(checkIn_vec);
}
catch (ServiceException ex) {
MessageBox.Show(ex.Message);
}
}
}
return 0;
}
public static Dataset getDataset(ItemRevision itemRevision, string dsType, string dsName) {
GetItemFromIdPref pref = new GetItemFromIdPref();
Teamcenter.Services.Strong.Core._2007_06.DataManagement.ExpandGRMRelationsPref myPref = new Teamcenter.Services.Strong.Core._2007_06.DataManagement.ExpandGRMRelationsPref();
Teamcenter.Services.Strong.Core._2007_06.DataManagement.RelationAndTypesFilter2 myFilter = new Teamcenter.Services.Strong.Core._2007_06.DataManagement.RelationAndTypesFilter2();
myFilter.RelationName = "IMAN_specification";
string[] typeVec = new string[1];
typeVec[0] = dsType;
myFilter.ObjectTypeNames = typeVec;
myPref.ExpItemRev = false;
Teamcenter.Services.Strong.Core._2007_06.DataManagement.RelationAndTypesFilter2[] myFilterVec = { myFilter };
myPref.Info = myFilterVec;
ModelObject[] primaryObjects = { itemRevision };
Teamcenter.Services.Strong.Core._2007_06.DataManagement.ExpandGRMRelationsResponse myResp = TCUtil.dmService.ExpandGRMRelationsForPrimary(primaryObjects, myPref);
for (int k = 0; k < myResp.Output.Length; k++) {
Teamcenter.Services.Strong.Core._2007_06.DataManagement.ExpandGRMRelationsOutput grmOutput = myResp.Output[k];
for (int l = 0; l < grmOutput.OtherSideObjData.Length; l++) {
Teamcenter.Services.Strong.Core._2007_06.DataManagement.ExpandGRMRelationsData otherSideData = grmOutput.OtherSideObjData[l];
if (otherSideData.OtherSideObjects.Length > 0) {
for (int m = 0; m < otherSideData.OtherSideObjects.Length; m++) {
//Teamcenter.Soa.Client.Model.ServiceData sData;
Dataset dataset = otherSideData.OtherSideObjects[m] as Dataset;
string ds_name = dataset.Object_string;
if (ds_name == dsName) {
return dataset;
}
}
}
}
}
return null;
}
public static short checkDatasetCheckout(Dataset dataset) {
try {
ModelObject[] objects2 = { dataset };
String[] attributes2 = { "is_modifiable", "checked_out", "checked_out_user" };
//TcTool.dmService.RefreshObjects(objects2);
TCUtil.dmService.GetProperties(objects2, attributes2);
if (!dataset.Is_modifiable) {
MessageBox.Show("数据集为只读格式,无法保存数据集!", "提示");
return 1;
}
Teamcenter.Soa.Client.Model.Strong.User checkUserInfo = dataset.Checked_out_user as Teamcenter.Soa.Client.Model.Strong.User;
if (checkUserInfo != null) {
if (checkUserInfo.Uid != TCUtil.user.Uid) {
MessageBox.Show("数据集由其他用户签出,无法保存数据集!", "提示");
return 2;
}
}
}
catch (Teamcenter.Soa.Exceptions.NotLoadedException ex) {
MessageBox.Show("错误:\r\n" + ex.Message, "提示");
return 11;
}
return 0;
}
public static Dataset createDataset(ItemRevision itemRevision, string type, string name) {
// MessageBox.Show("数组越界--------------1", "提示");
Teamcenter.Services.Strong.Core._2008_06.DataManagement.DatasetProperties2[] data_tts = new Teamcenter.Services.Strong.Core._2008_06.DataManagement.DatasetProperties2[1];
// MessageBox.Show("数组越界--------------2", "提示");
Teamcenter.Services.Strong.Core._2008_06.DataManagement.DatasetProperties2 one_data_tt = new Teamcenter.Services.Strong.Core._2008_06.DataManagement.DatasetProperties2();
//MessageBox.Show("数组越界--------------3", "提示");
one_data_tt.ClientId = "esappCreateDatasetClientID";
//MessageBox.Show("数组越界--------------4", "提示");
one_data_tt.Type = type;
one_data_tt.Name = name;
one_data_tt.Description = name;
one_data_tt.Container = null;
// MessageBox.Show("数组越界--------------5", "提示");
data_tts[0] = one_data_tt;
//MessageBox.Show("数组越界--------------6", "提示");
Teamcenter.Services.Strong.Core._2006_03.DataManagement.CreateDatasetsResponse dsResp = TCUtil.dmService.CreateDatasets2(data_tts);
//MessageBox.Show("数组越界--------------7", "提示");
TCUtil.ThrowServiceDataError(dsResp.ServiceData);
Dataset createdataset = dsResp.Output[0].Dataset;
//MessageBox.Show("数组越界--------------8", "提示");
if (createdataset == null) {
MessageBox.Show("创建数据集失败!", "提示");
return null;
}
//MessageBox.Show("数组越界--------------9", "提示");
string clientid = dsResp.Output[0].ClientId;
//MessageBox.Show("数组越界--------------10", "提示");
Teamcenter.Services.Strong.Core._2006_03.DataManagement.Relationship[] rela_vec = new Teamcenter.Services.Strong.Core._2006_03.DataManagement.Relationship[1];
// MessageBox.Show("数组越界--------------11", "提示");
Teamcenter.Services.Strong.Core._2006_03.DataManagement.Relationship one_rela = new Teamcenter.Services.Strong.Core._2006_03.DataManagement.Relationship();
//MessageBox.Show("数组越界--------------12", "提示");
one_rela.ClientId = "";
one_rela.PrimaryObject = itemRevision;
one_rela.SecondaryObject = createdataset;
one_rela.RelationType = "IMAN_specification";
one_rela.UserData = null;
rela_vec[0] = one_rela;
Teamcenter.Services.Strong.Core._2006_03.DataManagement.CreateRelationsResponse reResp = TCUtil.dmService.CreateRelations(rela_vec);
TCUtil.ThrowServiceDataError(reResp.ServiceData);
if (reResp.Output.Length == 0) {
MessageBox.Show("创建数据集关系失败,可能的原因是没有所选电气子项目的权限!", "提示");
return null;
}
return createdataset;
}
public static int checkItem(string item_id) {
GetItemFromIdInfo OneInfo = new GetItemFromIdInfo();
GetItemFromIdInfo[] Infos = new GetItemFromIdInfo[1];
GetItemFromIdPref Pref = new GetItemFromIdPref();
OneInfo.ItemId = item_id;
Infos[0] = OneInfo;
GetItemFromIdResponse InfoResp = TCUtil.dmService.GetItemFromId(Infos, 1, Pref); // 1个数是最新版本
return InfoResp.Output.Length;
}
}
}