|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using KPlan.Logger;
|
|
|
using System.Reflection;
|
|
|
using System.IO;
|
|
|
using System.Configuration;
|
|
|
using System.Collections.Specialized;
|
|
|
using System.Windows.Forms;
|
|
|
using System.Windows;
|
|
|
using ICSharpCode.SharpZipLib.Zip;
|
|
|
using ICSharpCode.SharpZipLib.Checksum;
|
|
|
using System.Text.RegularExpressions;
|
|
|
using Teamcenter.Soa.Client.Model;
|
|
|
using Eplan.EplApi.MasterData;
|
|
|
using Eplan.EplApi.DataModel;
|
|
|
using System.IO.Compression;
|
|
|
using ZipFile = System.IO.Compression.ZipFile;
|
|
|
|
|
|
namespace KPlan.Util {
|
|
|
class KUtil {
|
|
|
|
|
|
|
|
|
|
|
|
public static Configuration APP_CONFIG;
|
|
|
static KUtil() {
|
|
|
InitAppConfig();
|
|
|
}
|
|
|
|
|
|
|
|
|
public static void SetOwnerWindow(Window window) {
|
|
|
System.Windows.Interop.WindowInteropHelper helper = new System.Windows.Interop.WindowInteropHelper(window);
|
|
|
helper.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static void TestZip() {
|
|
|
KUtil.ZipDirectory(@"C:\Users\Public\EPLAN\Data", "c:\\test1.zip", null, null);
|
|
|
KUtil.ReplaceZipContentName("c:\\test1.zip");
|
|
|
KUtil.UnZipDirectory("c:\\test1.zip", "c:\\testtest2");
|
|
|
}
|
|
|
|
|
|
public static void ReplaceZipContentName(string zipFilePath) {
|
|
|
Eplan.EplApi.Base.Languages languages = new Eplan.EplApi.Base.Languages();
|
|
|
if (languages.GuiLanguage.GetNumber() != Eplan.EplApi.Base.ISOCode.Language.L_zh_CN) {
|
|
|
return;
|
|
|
}
|
|
|
KUtil.Log("替换压缩包内文件名称为中文:"+zipFilePath);
|
|
|
Dictionary<string, string> replaceNames = new Dictionary<string, string>();
|
|
|
string config = KUtil.GetConfigValue(KConfigure.EPLAN_DATA_SECTION, KConfigure.EPLAN_EN_TO_CN);
|
|
|
if (KUtil.IsEmpty(config)) {
|
|
|
KUtil.Log("未配置语言转换");
|
|
|
return;
|
|
|
}
|
|
|
string[] configs = config.Split(';');
|
|
|
int len = configs == null ? 0 : configs.Length;
|
|
|
for(int i = 0; i < len; i++) {
|
|
|
string[] split = configs[i].Split('=');
|
|
|
if (split.Length == 2) {
|
|
|
string en = split[0].Trim();
|
|
|
string cn = split[1].Trim();
|
|
|
if (!KUtil.IsEmpty(en) && !KUtil.IsEmpty(cn)) {
|
|
|
KUtil.Log("转换:"+en+" -> "+cn);
|
|
|
replaceNames.Add(en + "/", cn + "/");
|
|
|
replaceNames.Add(en + "\\", cn + "\\");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
ZipArchive archive = null;
|
|
|
using (archive = System.IO.Compression.ZipFile.Open(zipFilePath, ZipArchiveMode.Update)) {
|
|
|
var entries = archive.Entries.ToArray();
|
|
|
foreach (var entry in entries) {
|
|
|
// KPlan.Util.KUtil.Log(entry.FullName);
|
|
|
string oldName = entry.FullName;
|
|
|
string newName = oldName;
|
|
|
foreach (string key in replaceNames.Keys) {
|
|
|
if (newName.StartsWith(key)) {
|
|
|
newName =replaceNames[key]+newName.Substring(key.Length);
|
|
|
}
|
|
|
if (newName.Contains("/" + key)) {
|
|
|
newName = newName.Replace("/" + key, "/" + replaceNames[key]);
|
|
|
}
|
|
|
if (newName.Contains("\\" + key)) {
|
|
|
newName = newName.Replace("\\" + key, "\\" + replaceNames[key]);
|
|
|
}
|
|
|
}
|
|
|
if (!oldName.Equals(newName)) {
|
|
|
//KUtil.Log("替换文件:" + oldName + " -> " + newName);
|
|
|
var newEntry = archive.CreateEntry(newName);
|
|
|
using (var a = entry.Open())
|
|
|
using (var b = newEntry.Open())
|
|
|
a.CopyTo(b);
|
|
|
entry.Delete();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static bool RegexMatch(string str, string reg) {
|
|
|
string[] split = reg.Split('*');
|
|
|
List<int> index = new List<int>();
|
|
|
int len = split == null ? 0 : split.Length;
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
string sub = split[i];
|
|
|
if (string.IsNullOrEmpty(sub)) {
|
|
|
continue;
|
|
|
}
|
|
|
int ind = str.IndexOf(sub);
|
|
|
if (ind < 0) {
|
|
|
return false;
|
|
|
}
|
|
|
index.Add(ind);
|
|
|
index.Add(ind < 0 ? ind : ind + sub.Length);
|
|
|
}
|
|
|
len = index.Count;
|
|
|
if (len >= 2) {
|
|
|
for (int i = 0; i < len - 1; i++) {
|
|
|
if (index[i] > index[i + 1]) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public static string RemoveSpace(string str) {
|
|
|
if (str == null) {
|
|
|
return null;
|
|
|
}
|
|
|
return str;
|
|
|
//return str.Replace(" ", "");
|
|
|
}
|
|
|
|
|
|
public static string ToString<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
|
|
|
return "[" + string.Join(",", dictionary.Select(kv => kv.Key + "=" + kv.Value).ToArray()) + "]";
|
|
|
}
|
|
|
|
|
|
public static void DispatcherShow(Window win, String msg) {
|
|
|
win.Dispatcher.Invoke(new Action(delegate {
|
|
|
System.Windows.MessageBox.Show(win, msg);
|
|
|
}));
|
|
|
}
|
|
|
private static void InitAppConfig() {
|
|
|
try {
|
|
|
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
|
|
|
map.ExeConfigFilename = Environment.GetEnvironmentVariable("FMS_HOME") + @"\EPlan\EPLAN.EplAddin.KPlan.dll.config";
|
|
|
//map.ExeConfigFilename = @"D:\Siemens\Teamcenter12\tccs\EPlan\EPLAN.EplAddin.KPlan.dll.config";
|
|
|
APP_CONFIG = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
|
|
|
if (!File.Exists(map.ExeConfigFilename)) {
|
|
|
System.Windows.Forms.MessageBox.Show("配置文件不存在,集成功能将无法正常使用。请检查路径:\n"+map.ExeConfigFilename,"提示");
|
|
|
}
|
|
|
}
|
|
|
catch (System.Exception ex) {
|
|
|
System.Windows.Forms.MessageBox.Show("加载配置文件异常,集成功能将无法正常使用。\n" + ex.Message, "错误");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void Log(string format, params object[] args) {
|
|
|
TDefaultLogger.CreateInstance().LogAgent.DebugFormat(format, args);
|
|
|
}
|
|
|
|
|
|
public static string GetFilePath(string folder, string fileName, string ext) {
|
|
|
string filePath = folder + "\\" + fileName + "." + ext;
|
|
|
int index = 1;
|
|
|
while (File.Exists(filePath)) {
|
|
|
filePath = folder + "\\" + fileName + "("+(index++)+")." + ext;
|
|
|
}
|
|
|
return filePath;
|
|
|
}
|
|
|
|
|
|
public static void LogErr(Exception ex) {
|
|
|
if (ex.InnerException == null) {
|
|
|
KUtil.Log(ex.Message + "\r\n" + ex.StackTrace);
|
|
|
}
|
|
|
else {
|
|
|
KUtil.Log(ex.Message + "\r\n" + ex.StackTrace + "\r\n" + ex.InnerException.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static bool IsEmpty(string str) {
|
|
|
return str == null || "".Equals(str.Trim());
|
|
|
}
|
|
|
|
|
|
public static string GetCurrentDir() {
|
|
|
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
|
|
|
UriBuilder uri = new UriBuilder(codeBase);
|
|
|
string path = Uri.UnescapeDataString(uri.Path);
|
|
|
return Path.GetDirectoryName(path);
|
|
|
}
|
|
|
|
|
|
//public static string GetAppConfigValue(string configName) {
|
|
|
// //var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
|
|
|
// KeyValueConfigurationElement config = APP_CONFIG.AppSettings.Settings[configName];
|
|
|
// if (config == null) {
|
|
|
// return "";
|
|
|
// }
|
|
|
// return config.Value == null ? "" : config.Value;
|
|
|
//}
|
|
|
|
|
|
public static string GetConfigValue(string sec, string configName) {
|
|
|
AppSettingsSection section = APP_CONFIG.Sections.Get(sec) as AppSettingsSection;// g1/s1
|
|
|
if (section == null) {
|
|
|
return "";
|
|
|
}
|
|
|
KeyValueConfigurationElement config = section.Settings[configName];
|
|
|
if (config == null) {
|
|
|
return "";
|
|
|
}
|
|
|
return config.Value == null ? "" : config.Value;
|
|
|
}
|
|
|
|
|
|
public static Dictionary<string,string> GetConfigValue(string sec) {
|
|
|
AppSettingsSection section = APP_CONFIG.Sections.Get(sec) as AppSettingsSection;// g1/s1
|
|
|
Dictionary<string, string> res = new Dictionary<string, string>();
|
|
|
if (section == null) {
|
|
|
return res;
|
|
|
}
|
|
|
foreach (string key in section.Settings.AllKeys) {
|
|
|
KeyValueConfigurationElement config = section.Settings[key];
|
|
|
string val = "";
|
|
|
if (config != null) {
|
|
|
val = config.Value == null ? "" : config.Value;
|
|
|
}
|
|
|
res.Add(key, val);
|
|
|
}
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
public static void ClearConfigSection(string sec) {
|
|
|
AppSettingsSection section = APP_CONFIG.Sections.Get(sec) as AppSettingsSection;// g1/s1
|
|
|
if (section == null) {
|
|
|
return;
|
|
|
}
|
|
|
section.Settings.Clear();
|
|
|
APP_CONFIG.Save();
|
|
|
}
|
|
|
|
|
|
//public static void SetConfigValue(string key, string value) {
|
|
|
// KeyValueConfigurationElement config = APP_CONFIG.AppSettings.Settings[key];
|
|
|
// if (config == null) {
|
|
|
// APP_CONFIG.AppSettings.Settings.Add(new KeyValueConfigurationElement(key, value));
|
|
|
// }
|
|
|
// else {
|
|
|
// config.Value = value;
|
|
|
// }
|
|
|
// APP_CONFIG.Save();
|
|
|
//}
|
|
|
|
|
|
public static void SetConfigValue(string sec,string key, string value) {
|
|
|
AppSettingsSection section = APP_CONFIG.Sections.Get(sec) as AppSettingsSection;// g1/s1
|
|
|
if (section == null) {
|
|
|
section = new AppSettingsSection();
|
|
|
APP_CONFIG.Sections.Add(sec, section);
|
|
|
}
|
|
|
KeyValueConfigurationElement config = section.Settings[key];
|
|
|
if (config == null) {
|
|
|
section.Settings.Add(new KeyValueConfigurationElement(key, value));
|
|
|
}
|
|
|
else {
|
|
|
config.Value = value;
|
|
|
}
|
|
|
APP_CONFIG.Save();
|
|
|
}
|
|
|
|
|
|
public static void SetConfigValue(string sec, Dictionary<string,string> configMap) {
|
|
|
AppSettingsSection section = APP_CONFIG.Sections.Get(sec) as AppSettingsSection;// g1/s1
|
|
|
if (section == null) {
|
|
|
section = new AppSettingsSection();
|
|
|
APP_CONFIG.Sections.Add(sec, section);
|
|
|
}
|
|
|
foreach (string key in configMap.Keys) {
|
|
|
string value = configMap[key];
|
|
|
KeyValueConfigurationElement config = section.Settings[key];
|
|
|
if (config == null) {
|
|
|
section.Settings.Add(new KeyValueConfigurationElement(key, value));
|
|
|
}
|
|
|
else {
|
|
|
config.Value = value;
|
|
|
}
|
|
|
}
|
|
|
APP_CONFIG.Save();
|
|
|
}
|
|
|
|
|
|
public static bool ZipDirectory(string folderToZip, string zipedFile, string password, string skipFolder) {
|
|
|
bool result = false;
|
|
|
if (!Directory.Exists(folderToZip))
|
|
|
return result;
|
|
|
if (File.Exists(zipedFile)) {
|
|
|
File.Delete(zipedFile);
|
|
|
}
|
|
|
ZipFile.CreateFromDirectory(folderToZip, zipedFile);
|
|
|
/*FastZip fz = new FastZip();
|
|
|
fz.EntryFactory = new ZipEntryFactory { IsUnicodeText = true };
|
|
|
fz.CreateEmptyDirectories = true;
|
|
|
fz.CreateZip(zipedFile, folderToZip, true,"", skipFolder);*/
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public static void UnZipDirectory(string zipFilePath,string unZipDir) {
|
|
|
//ZipFile.ExtractToDirectory(zipFilePath, unZipDir);
|
|
|
FastZip fz = new FastZip();
|
|
|
fz.EntryFactory = new ZipEntryFactory { IsUnicodeText = true };
|
|
|
fz.CreateEmptyDirectories = true;
|
|
|
fz.ExtractZip(zipFilePath, unZipDir, null);
|
|
|
}
|
|
|
|
|
|
private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName, string skipFolder) {
|
|
|
bool result = true;
|
|
|
string[] folders, files;
|
|
|
ZipEntry ent = null;
|
|
|
FileStream fs = null;
|
|
|
Crc32 crc = new Crc32();
|
|
|
|
|
|
|
|
|
string[] skipFolders = Regex.Split(skipFolder, "@@", RegexOptions.IgnoreCase);
|
|
|
|
|
|
try {
|
|
|
ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/"));
|
|
|
zipStream.PutNextEntry(ent);
|
|
|
zipStream.Flush();
|
|
|
|
|
|
files = Directory.GetFiles(folderToZip);
|
|
|
foreach (string file in files) {
|
|
|
if (skipFolders.Contains<string>(Path.GetFileName(file))) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
fs = File.OpenRead(file);
|
|
|
|
|
|
byte[] buffer = new byte[fs.Length];
|
|
|
fs.Read(buffer, 0, buffer.Length);
|
|
|
ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)));
|
|
|
ent.DateTime = DateTime.Now;
|
|
|
ent.Size = fs.Length;
|
|
|
|
|
|
fs.Close();
|
|
|
|
|
|
crc.Reset();
|
|
|
crc.Update(buffer);
|
|
|
|
|
|
ent.Crc = crc.Value;
|
|
|
zipStream.PutNextEntry(ent);
|
|
|
zipStream.Write(buffer, 0, buffer.Length);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
catch {
|
|
|
result = false;
|
|
|
}
|
|
|
finally {
|
|
|
if (fs != null) {
|
|
|
fs.Close();
|
|
|
fs.Dispose();
|
|
|
}
|
|
|
if (ent != null) {
|
|
|
ent = null;
|
|
|
}
|
|
|
GC.Collect();
|
|
|
GC.Collect(1);
|
|
|
}
|
|
|
|
|
|
folders = Directory.GetDirectories(folderToZip);
|
|
|
foreach (string folder in folders) {
|
|
|
if (skipFolders.Contains<string>(Path.GetFileName(folder))) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if (!ZipDirectory(folder, zipStream, folderToZip, skipFolder))
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public static bool FileIsUsed(string fileFullName) {
|
|
|
Boolean result = false;
|
|
|
|
|
|
//判断文件是否存在,如果不存在,直接返回 false
|
|
|
if (!System.IO.File.Exists(fileFullName)) {
|
|
|
result = false;
|
|
|
|
|
|
}//end: 如果文件不存在的处理逻辑
|
|
|
else {//如果文件存在,则继续判断文件是否已被其它程序使用
|
|
|
|
|
|
//逻辑:尝试执行打开文件的操作,如果文件已经被其它程序使用,则打开失败,抛出异常,根据此类异常可以判断文件是否已被其它程序使用。
|
|
|
System.IO.FileStream fileStream = null;
|
|
|
try {
|
|
|
fileStream = System.IO.File.Open(fileFullName, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
|
|
|
|
|
|
result = false;
|
|
|
}
|
|
|
catch (System.IO.IOException) {
|
|
|
result = true;
|
|
|
}
|
|
|
catch (System.Exception) {
|
|
|
result = true;
|
|
|
}
|
|
|
finally {
|
|
|
if (fileStream != null) {
|
|
|
fileStream.Close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}//end: 如果文件存在的处理逻辑
|
|
|
|
|
|
//返回指示文件是否已被其它程序使用的值
|
|
|
return result;
|
|
|
|
|
|
}//end method FileIsUsed
|
|
|
|
|
|
|
|
|
public static void Sync_Property_to_TC(Teamcenter.Soa.Client.Model.Strong.ItemRevision rev, MDPart part) {
|
|
|
Dictionary<string, string> eplan_to_tc = KUtil.GetConfigValue(KConfigure.PART_SYNC_EPLAN_TO_TC_SECTION);
|
|
|
Dictionary<string, string> tcProps = new Dictionary<string, string>();
|
|
|
ModelObject[] mo = TCUtil.Refresh(rev);
|
|
|
TCUtil.GetProperties(false, mo, "object_string");
|
|
|
KUtil.Log("同步对象属性:" + part.PartNr + "->" + rev.Object_string);
|
|
|
foreach (string key in eplan_to_tc.Keys) {
|
|
|
string value = EplanUtil.GetPropValue(part, eplan_to_tc[key]);
|
|
|
//KUtil.Log(">> 读取数据:" + eplan_to_tc[key] + " = " + value);
|
|
|
tcProps.Add(key, value);
|
|
|
}
|
|
|
TCUtil.SetProperties(rev, tcProps);
|
|
|
//发送分类
|
|
|
//kk 20200408 EPLAN2.4
|
|
|
//MDPartsDatabaseItem.Enums.ProductTopGroup topGroupE = (MDPartsDatabaseItem.Enums.ProductTopGroup)Enum.Parse(typeof(MDPartsDatabaseItem.Enums.ProductTopGroup), part.Properties.ARTICLE_PRODUCTTOPGROUP.ToString());
|
|
|
//string topGroup = topGroupE.ToString();
|
|
|
|
|
|
string topGroup = part.GenericProductGroup.ToString();
|
|
|
string group = part.ProductGroup.ToString();
|
|
|
KUtil.Log("准备发送分类,Group = " + topGroup + "." + group);
|
|
|
string tcClassId = KUtil.GetConfigValue(KConfigure.PART_SYNC_CLASSIFICATION, topGroup+"."+group);
|
|
|
if (KUtil.IsEmpty(tcClassId)) {
|
|
|
KUtil.Log("未配置tc分类id");
|
|
|
}
|
|
|
else {
|
|
|
KUtil.Log("发送分类:" + tcClassId);
|
|
|
TCUtil.SendToClassification(tcClassId, rev);
|
|
|
}
|
|
|
KUtil.Log("同步完成");
|
|
|
}
|
|
|
|
|
|
public static void Sync_ProjProperty_to_TC(Teamcenter.Soa.Client.Model.Strong.ItemRevision rev, Project project) {
|
|
|
Dictionary<string, string> eplan_to_tc = KUtil.GetConfigValue(KConfigure.PROJ_SYNC_EPLAN_TO_TC_SECTION);
|
|
|
Dictionary<string, string> tcProps = new Dictionary<string, string>();
|
|
|
ModelObject[] mo = TCUtil.Refresh(rev);
|
|
|
TCUtil.GetProperties(false, mo, "object_string");
|
|
|
KUtil.Log("同步对象属性:" + project.ProjectName + "->" + rev.Object_string);
|
|
|
foreach (string key in eplan_to_tc.Keys) {
|
|
|
string value = EplanUtil.GetPropValue(project, eplan_to_tc[key]);
|
|
|
KUtil.Log(">> 读取数据:" + eplan_to_tc[key] + " = " + value);
|
|
|
tcProps.Add(key, value);
|
|
|
}
|
|
|
TCUtil.SetProperties(rev, tcProps);
|
|
|
KUtil.Log("同步完成");
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void Sync_Property_to_EPlan(MDPartsDatabase mdPartsDatabase, Teamcenter.Soa.Client.Model.Strong.ItemRevision rev, MDPart part) {
|
|
|
Dictionary<string, string> tc_to_eplan = KUtil.GetConfigValue(KConfigure.PART_SYNC_TC_TO_EPLAN_SECTION);
|
|
|
Dictionary<string, string> eplanProps = new Dictionary<string, string>();
|
|
|
ModelObject[] mo = TCUtil.Refresh(rev);
|
|
|
TCUtil.GetProperties(false, mo, "object_string");
|
|
|
KUtil.Log("同步对象属性:" + rev.Object_string + "->" + part.PartNr);
|
|
|
foreach (string key in tc_to_eplan.Keys) {
|
|
|
string value = TCUtil.GetRevConfigProp(rev, tc_to_eplan[key]);
|
|
|
KUtil.Log(">> 读取数据:" + tc_to_eplan[key] + " = " + value);
|
|
|
eplanProps.Add(key, value);
|
|
|
}
|
|
|
EplanUtil.SetProperties(mdPartsDatabase, part, eplanProps);
|
|
|
KUtil.Log("同步完成");
|
|
|
}
|
|
|
}
|
|
|
}
|