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.

514 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.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Eplan.EplApi.Base;
using Eplan.EplApi.DataModel;
using Eplan.EplApi.HEServices;
using Eplan.EplApi.MasterData;
using ICSharpCode.SharpZipLib.Zip;
using Teamcenter.Soa.Client.Model.Strong;
using Project = Eplan.EplApi.DataModel.Project;
namespace KPlan.Util {
class EplanUtil {
public static MDPartsManagement mdPartsManagement;
//public static MDPartsDatabase mdPartsDatabase;
public static Eplan.EplApi.Base.ISOCode.Language Language = Eplan.EplApi.Base.ISOCode.Language.L_en_US;
public static Eplan.EplApi.Base.ISOCode.Language Language_CN = Eplan.EplApi.Base.ISOCode.Language.L_zh_CN;
static EplanUtil() {
mdPartsManagement = new MDPartsManagement();
//mdPartsDatabase = mdPartsManagement.OpenDatabase();
//mdPartsDatabase.UpdateScheme();
//KUtil.Log("更新数据库索引...");
//mdPartsDatabase.UpdateSearchIndex();
//KUtil.Log("更新完成");
}
public static MDPartsDatabase OpenDatabase() {
return mdPartsManagement.OpenDatabase();
}
public static void UpdateAndClose(MDPartsDatabase mdPartsDatabase) {
Progress progress = new Progress("SimpleProgress");
progress.SetTitle("");
progress.SetAllowCancel(false);
progress.BeginPart(100.0, "");
try {
mdPartsDatabase.UpdateSearchIndex();
}catch(System.Exception ex) {
KUtil.LogErr(ex);
System.Windows.MessageBox.Show(ex.Message);
}
finally {
progress.EndPart(true);
mdPartsDatabase.Close();
}
}
public static string GetProjectCode(Project currentProject) {
return EplanUtil.GetPropValue(currentProject.Properties.PROJ_DRAWINGNUMBER);
}
public static void ExportPartList(Project currentProject, string exportFile, PartsService.Format format) {
KUtil.Log("导出Partlist" + exportFile);
PartsService ps = new PartsService();
ps.ExportPartsList(currentProject, exportFile, format);
ps.Dispose();
}
public static Eplan.EplApi.DataModel.Project Create(string projectLinkFilePath, string projectTemplateFilePath, bool overwrite) {
ProjectManager projectManager = new ProjectManager();
Eplan.EplApi.DataModel.Project project = null;
using (new LockingStep()) // needed
{
// Exists
if (projectManager.ExistsProject(projectLinkFilePath) && overwrite == false) {
project = OpenProject(projectLinkFilePath);
}
// New
if (!projectManager.ExistsProject(projectLinkFilePath) || overwrite == true) {
try {
project = projectManager.CreateProject(projectLinkFilePath, projectTemplateFilePath);
}catch(ProjectCreationException ex) {
KUtil.LogErr(ex);
throw new Exception("无法创建项目,需要手动升级模板");
}
}
}
return project;
}
public static Eplan.EplApi.DataModel.Project OpenProject(string projectLinkFilePath, ProjectManager.OpenMode openMode = ProjectManager.OpenMode.Standard, bool upgradeIfNeeded = true) {
if (!File.Exists(projectLinkFilePath)) {
throw new FileNotFoundException("EPLAN project link file not found", projectLinkFilePath);
}
using (new LockingStep()) {
ProjectManager projectManager = new ProjectManager();
projectManager.LockProjectByDefault = false;
Project project = projectManager.GetProject(projectLinkFilePath);
// Check if openMode is OK
if (project != null) {
bool reOpen = false;
switch (openMode) {
case ProjectManager.OpenMode.Standard:
if (project.IsExclusive || project.IsReadOnly) {
reOpen = true;
}
break;
case ProjectManager.OpenMode.ReadOnly:
if (!project.IsReadOnly) {
reOpen = true;
}
break;
case ProjectManager.OpenMode.Exclusive:
if (!project.IsExclusive) {
reOpen = true;
}
break;
default: throw new ArgumentOutOfRangeException(nameof(openMode), openMode, null);
}
if (reOpen) {
project.Close();
project = OpenProject(projectLinkFilePath, openMode);
}
return project;
}
//kk 20200408 EPLAN2.4
//return projectManager.OpenProject(projectLinkFilePath, openMode);
return projectManager.OpenProject(projectLinkFilePath, openMode, upgradeIfNeeded);
}
}
public static MDPart[] GetParts(MDPartsDatabase mdPartsDatabase, int topGroupId, int groupId) {
MDPart[] res = null;
try {
MDPartsDatabaseItemPropertyList filter = new MDPartsDatabaseItemPropertyList();
filter.ARTICLE_PRODUCTTOPGROUP.Set(topGroupId);
filter.ARTICLE_PRODUCTGROUP.Set(groupId);
MDPartsDatabaseItemPropertyList props = new MDPartsDatabaseItemPropertyList();
//props.ARTICLE_PARTNR = "-";
res = mdPartsDatabase.GetParts(filter, props);
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
}
return res;
}
public static MDPart GetPart(MDPartsDatabase mdPartsDatabase, string partNr) {
try {
return mdPartsDatabase.GetPart(partNr);
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
}
return null;
}
public static List<MDPart> GetUnsyncedParts(MDPartsDatabase mdPartsDatabase, string orderNr, string erpNr) {
List<MDPart> res = new List<MDPart>();
KUtil.Log("查询未同步部件");
try {
string[] atex = new string[] { KPlan.Forms.KPartSync.APPLYING, Forms.KPartSync.NOT_SYNCED, "" };
for (int i = 0; i < atex.Length; i++) {
MDPartsDatabaseItemPropertyList filter = new MDPartsDatabaseItemPropertyList();
filter.ARTICLE_CERTIFICATE_ATEX = atex[i];
if (!KUtil.IsEmpty(orderNr))
filter.ARTICLE_PARTNR = orderNr;
if (!KUtil.IsEmpty(erpNr))
filter.ARTICLE_ERPNR = erpNr;
MDPartsDatabaseItemPropertyList props = new MDPartsDatabaseItemPropertyList();
MDPart[] parts = mdPartsDatabase.GetParts(filter, props);
int cnt = parts == null ? 0 : parts.Length;
KUtil.Log(atex[i] + " -> " + cnt);
if (cnt > 0) {
res.AddRange(parts);
}
}
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
}
KUtil.Log("结果总数:" + res.Count);
return res;
}
public static MDPart[] SearchPartByOrderNo(MDPartsDatabase mdPartsDatabase, string orderNr) {
if (string.IsNullOrWhiteSpace(orderNr)) {
return null;
}
KUtil.Log("通过订货号查询部件:" + orderNr);
try {
MDPartsDatabaseItemPropertyList filter = new MDPartsDatabaseItemPropertyList();
filter.ARTICLE_PARTNR = orderNr;
MDPartsDatabaseItemPropertyList props = new MDPartsDatabaseItemPropertyList();
MDPart[] res = mdPartsDatabase.GetParts(filter, props);
KUtil.Log("结果总数:" + (res == null ? 0 : res.Length));
return res;
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
}
return null;
}
public static MDPart[] GetUnsyncedParts_old(MDPartsDatabase mdPartsDatabase, string orderNr, string erpNr) {
try {
MDPartsDatabaseItemPropertyList filter = new MDPartsDatabaseItemPropertyList();
filter.ARTICLE_CERTIFICATE_ATEX = "";
if (!KUtil.IsEmpty(orderNr))
filter.ARTICLE_PARTNR = orderNr;
if (!KUtil.IsEmpty(erpNr))
filter.ARTICLE_ERPNR = erpNr;
MDPartsDatabaseItemPropertyList props = new MDPartsDatabaseItemPropertyList();
return mdPartsDatabase.GetParts(filter,props);
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
}
return null;
}
public static MDPart[] QueryParts(MDPartsDatabase mdPartsDatabase, string erpNr) {
try {
if (erpNr==null) {
return null;
}
MDPartsDatabaseItemPropertyList filter = new MDPartsDatabaseItemPropertyList();
//filter.ARTICLE_ERPNR = erpNr;
//filter.ARTICLE_PARTNR = "PXC.3031212";
return mdPartsDatabase.GetParts(filter);
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
}
return null;
}
public static MDPart[] QueryParts(MDPartsDatabase mdPartsDatabase, string orderNr, string erpNr) {
try {
if (KUtil.IsEmpty(orderNr) && KUtil.IsEmpty(erpNr)) {
return null;
}
MDPartsDatabaseItemPropertyList filter = new MDPartsDatabaseItemPropertyList();
if (!KUtil.IsEmpty(orderNr))
filter.ARTICLE_ERPNR = orderNr;
if (!KUtil.IsEmpty(erpNr))
filter.ARTICLE_ORDERNR = erpNr;
return mdPartsDatabase.GetParts(filter);
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
}
return null;
}
public static MDPart[] QueryParts2(MDPartsDatabase mdPartsDatabase, string orderNr, string erpNr)
{
try
{
if (KUtil.IsEmpty(orderNr) && KUtil.IsEmpty(erpNr))
{
return null;
}
MDPartsDatabaseItemPropertyList filter = new MDPartsDatabaseItemPropertyList();
if (!KUtil.IsEmpty(orderNr))
filter.ARTICLE_ERPNR = orderNr;
if (!KUtil.IsEmpty(erpNr))
filter.ARTICLE_ORDERNR = erpNr;
return mdPartsDatabase.GetParts(filter);
}
catch (System.Exception ex)
{
KUtil.LogErr(ex);
}
return null;
}
public static void Test(Progress progress ){
}
public static string GetPropValue(MDPart part, string propName) {
if (part == null || KUtil.IsEmpty(propName)) {
return "";
}
string language = "";
string[] split = propName.Split('@');
if (split.Length == 2) {
language = split[1].Trim();
propName = split[0].Trim();
}
Type t = typeof(MDPartsDatabaseItemPropertyList);
System.Reflection.PropertyInfo propInfo = null;
try {
propInfo = t.GetProperty(propName, new Type[] { });
}
catch(System.Exception ex) {
KUtil.LogErr(ex);
}
if (propInfo == null) {
throw new Exception("部件不存在属性:"+propName);
}
MDPropertyValue val = propInfo.GetValue(part.Properties) as MDPropertyValue;
if (val.IsEmpty) {
return "";
}
if (val == null) { return ""; }
string res;
if (KUtil.IsEmpty(language)) {
res = val.ToString();
}
else {
KUtil.Log("获取多语言数据:" + propName + " = " + val);
res = val.ToString((ISOCode.Language)Enum.Parse(typeof(ISOCode.Language), language));
}
if (!KUtil.IsEmpty(res)) {
if ("ARTICLE_PRODUCTGROUP".Equals(propName)) {
MDPartsDatabaseItem.Enums.ProductGroup group;
if(Enum.TryParse(res,out group)) {
res = MDPartsDatabaseItem.GetProductGroupName(group);
}
}
else if ("ARTICLE_PRODUCTSUBGROUP".Equals(propName)) {
MDPartsDatabaseItem.Enums.ProductSubGroup group;
if (Enum.TryParse(res, out group)) {
res = MDPartsDatabaseItem.GetProductSubGroupName(group);
}
}else if ("ARTICLE_PRODUCTTOPGROUP".Equals(propName)){
MDPartsDatabaseItem.Enums.ProductTopGroup group;
if (Enum.TryParse(res, out group)) {
res = MDPartsDatabaseItem.GetProductTopGroupName(group);
}
}
}
return res == null ? "" : res;
}
public static string GetPropValue(Project part, string propName) {
if (part == null || KUtil.IsEmpty(propName)) {
return "";
}
string language = "";
string[] split = propName.Split('@');
if (split.Length == 2) {
language = split[1].Trim();
propName = split[0].Trim();
}
Type t = typeof(ProjectPropertyList);
System.Reflection.PropertyInfo propInfo = null;
try {
propInfo = t.GetProperty(propName, new Type[] { });
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
}
if (propInfo == null) {
throw new Exception("项目不存在属性:" + propName);
}
PropertyValue val = propInfo.GetValue(part.Properties) as PropertyValue;
if (val == null) { return ""; }
if (val.IsEmpty) {
return "";
}
string res;
if (KUtil.IsEmpty(language)) {
res = val.ToString();
}
else {
KUtil.Log("获取多语言数据:" + propName + " = " + val);
res = val.ToString(GetLanguage(language));
}
//string res = val.ToString(EplanUtil.Language); 属性同步的时候用全值比较好吧
//string res = val.ToString();
return res == null ? "" : res;
}
public static void SetProperties(MDPartsDatabase mdPartsDatabase, MDPart part, Dictionary<string, string> eplanProps) {
if (part == null || eplanProps == null || eplanProps.Count == 0) {
return;
}
MDPartsDatabaseTransaction trans = mdPartsDatabase.CreateTransaction();
Type t = typeof(MDPartsDatabaseItemPropertyList);
System.Reflection.PropertyInfo propInfo = null;
MDPartsDatabaseItemPropertyList propertyList = part.Properties;
try {
foreach (string key in eplanProps.Keys) {
string val = eplanProps[key];
string language = "";
string propName = key;
string[] split = key.Split('@');
if (split.Length == 2) {
language = split[1].Trim();
propName = split[0].Trim();
}
KUtil.Log("设置属性:" + key + " = " + val);
propInfo = t.GetProperty(propName, new Type[] { });
if (propInfo == null) {
throw new Exception("部件不存在属性:" + propName);
}
MDPropertyValue propVal = propInfo.GetValue(part.Properties) as MDPropertyValue;
//MDPropertyValue propVal = new MDPropertyValue();
if (KUtil.IsEmpty(language)) {
propVal.Set(val);
}
else {
MultiLangString ms;
ISOCode.Language lan = GetLanguage(language);
if (propVal.IsEmpty) {
ms = new MultiLangString();
ms.AddString(lan, val);
}
else {
KUtil.Log("获取多语言数据:" + key + " = " + propVal);
ms = propVal.ToMultiLangString();
ms.DeleteString(lan);
ms.AddString(lan, val);
}
propVal.Set(ms);
}
propInfo.SetValue(propertyList, propVal);
}
trans.Commit();
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
trans.Rollback();
throw new Exception("设置部件属性出错:" + ex.Message);
}
finally {
trans.Dispose();
}
}
public static MDPart NewPart(MDPartsDatabase mdPartsDatabase, ItemRevision rev, string partNr) {
MDPart part = mdPartsDatabase.AddPart(partNr);
string[] classIds = TCUtil.GetClassId(rev);
string groupConfig = "";
if (classIds != null && classIds.Length > 0) {
string classId = classIds[0];
KUtil.Log("查询分类对应类别:"+classId);
Dictionary<string,string> map = KUtil.GetConfigValue(KConfigure.PART_SYNC_CLASSIFICATION);
foreach(string key in map.Keys) {
string val = map[key];
if (classId.Equals(val)) {
groupConfig = key;
KUtil.Log("找到类别:"+groupConfig);
break;
}
}
}
if (!KUtil.IsEmpty(groupConfig)) {
string[] split = groupConfig.Split('.');
if (split.Length == 2) {
//kk 20200408 EPLAN2.4
//MDPartsDatabaseItem.Enums.ProductTopGroup topGroup = (MDPartsDatabaseItem.Enums.ProductTopGroup)Enum.Parse(typeof(MDPartsDatabaseItem.Enums.ProductTopGroup), split[0]);
//MDPartsDatabaseItem.Enums.ProductGroup group = (MDPartsDatabaseItem.Enums.ProductGroup)Enum.Parse(typeof(MDPartsDatabaseItem.Enums.ProductGroup), split[1]);
//part.Properties.ARTICLE_PRODUCTTOPGROUP.Set((int)topGroup);
//part.Properties.ARTICLE_PRODUCTGROUP.Set((int)group);
part.GenericProductGroup =(MDPartsDatabaseItem.Enums.ProductTopGroup)Enum.Parse(typeof( MDPartsDatabaseItem.Enums.ProductTopGroup),split[0]);
part.ProductGroup =(MDPartsDatabaseItem.Enums.ProductGroup)Enum.Parse(typeof(MDPartsDatabaseItem.Enums.ProductGroup),split[1]);
}
}
return part;
}
public static ISOCode.Language GetLanguage(string language) {
return (ISOCode.Language)Enum.Parse(typeof(ISOCode.Language), language);
}
public static string GetPropValue(PropertyValue mdProp) {
if (mdProp == null || mdProp.IsEmpty) {
return "";
}
return mdProp.ToString(); ;
}
public static string GetMDPropValue(MDPropertyValue mdProp) {
if (mdProp == null || mdProp.IsEmpty) {
return "";
}
return mdProp.ToString(); ;
}
public static string GetMDPropValue(MDPropertyValue mdProp,ISOCode.Language language) {
if (mdProp == null || mdProp.IsEmpty) {
return "";
}
return mdProp.ToString(language); ;
}
public static MDPropertyValue GetMDPropValue(MDPart part, string propName) {
if (part == null || KUtil.IsEmpty(propName)) {
return "";
}
Type t = typeof(MDPartsDatabaseItemPropertyList);
System.Reflection.PropertyInfo propInfo = null;
try {
propInfo = t.GetProperty(propName, new Type[] { });
}
catch (System.Exception ex) {
KUtil.LogErr(ex);
}
if (propInfo == null) {
throw new Exception("不存在属性:" + propName);
}
MDPropertyValue val = propInfo.GetValue(part.Properties) as MDPropertyValue;
return val;
}
}
}