using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using KPlan.Util;
using System.Data;
using Teamcenter.Soa.Client.Model;
using System.ComponentModel;
using Teamcenter.Soa.Client.Model.Strong;
using System.IO;
using Eplan.EplApi.DataModel;
using Eplan.EplApi.Base;
using Eplan.EplApi.ApplicationFramework;
using Action = System.Action;
using Eplan.EplApi.HEServices;
using System.Collections.Specialized;
using Label = System.Windows.Controls.Label;
namespace KPlan.Forms {
///
/// OpenFromTC.xaml 的交互逻辑
///
public partial class OpenFromTC : Window {
private string queryName;
private Dictionary queryFieldMap = new Dictionary();
private DataTable queryResultData = new DataTable();
private List queryResultConfig = new List();
private List queryResultWidth = new List();
private KBackgroundWorker bgWorker_Query;
private KBackgroundWorker bgWorker_Homer;
private KBackgroundWorker bgWorker_Open;
private ModelObject selectedObject;
public OpenFromTC() {
InitializeComponent();
KUtil.SetOwnerWindow(this);
LoadFromConfiguration();
bgWorker_Query = new KBackgroundWorker(this);
bgWorker_Query.backgroundWorker.DoWork += Async_Query;
bgWorker_Homer = new KBackgroundWorker(this);
bgWorker_Homer.backgroundWorker.DoWork += Async_LoadHome;
bgWorker_Open = new KBackgroundWorker(this);
bgWorker_Open.backgroundWorker.DoWork += Async_Open;
}
private void Async_Open(object sender, DoWorkEventArgs e) {
ModelObject mo = (ModelObject)e.Argument;
if (mo == null) {
return;
}
bgWorker_Open.Progress("正在打开...", 1);
string eplanDSType = KUtil.GetConfigValue(KConfigure.PROJ_SYNC_SECTION, KConfigure.PROJ_SYNC_ZW_DSTYPE);
List target = new List();
if (TCUtil.IsType(mo.SoaType, "Dataset")) {
//if (!TCUtil.IsType(mo.SoaType, TCUtil.TYPE_EPLAN)) {
// KUtil.DispatcherShow(this, "请选择" + TCUtil.TYPE_EPLAN + "数据集进行操作");
// return;
//}
target.Add((Dataset)mo);
}else if (TCUtil.IsType(mo.SoaType, "ItemRevision")) {
ModelObject[] children = TCUtil.GetChildFromPref(mo);
for(int i = 0; i < children.Length; i++) {
if (TCUtil.IsType(children[i].SoaType, eplanDSType)) {
target.Add((Dataset)children[i]);
}
}
}
else {
KUtil.DispatcherShow(this,"请选择版本或数据集进行操作");
return;
}
if (target.Count == 0) {
KUtil.DispatcherShow(this, "当前对象下不存在EPLAN数据集");
return;
}
if (target.Count > 1) {
KUtil.DispatcherShow(this,"当前对象下存在多个数据集,请选择要打开的数据集");
return;
}
Dataset ds = target[0];
List imanFiles = TCUtil.GetDatasetFile(ds, null);
TCUtil.GetProperties(false, TCUtil.Refresh(ds), "object_string");
if (imanFiles.Count == 0) {
KUtil.DispatcherShow(this, "数据集<"+ds.Object_string+">下没有找到引用文件");
return;
}
if (imanFiles.Count > 1) {
KUtil.DispatcherShow(this, "数据集<" + ds.Object_string + ">下找到多个引用文件");
return;
}
FileInfo file = TCUtil.GetFileInfo(imanFiles[0]);
if (file == null) {
KUtil.DispatcherShow(this, "从数据集<"+ds.Object_string+">下载引用文件失败");
return;
}
if (file.Attributes.ToString().IndexOf("ReadOnly") != -1) {
file.Attributes = FileAttributes.Normal;
}
if (file.FullName.ToLower().EndsWith("zw1")) {
string projName = file.Name.Replace(".zw1", "").Replace(".ZW1", "");
KUtil.Log("打开项目:"+projName+" - "+file.FullName);
string targetFolder = KUtil.GetConfigValue(KConfigure.EPLAN_DATA_SECTION, KConfigure.EPLAN_WORKSAPCE);
if (KUtil.IsEmpty(targetFolder)) {
KUtil.DispatcherShow(this, "未配置项目解压路径");
return;
}
if (!Directory.Exists(targetFolder)) {
Directory.CreateDirectory(targetFolder);
}
RestoreProject(targetFolder,file.FullName, projName);
}
else {
System.Diagnostics.Process.Start(file.FullName);
}
}
private void openDs(ModelObject mo, Progress progress) {
if (mo == null) {
return;
}
string eplanDSType = KUtil.GetConfigValue(KConfigure.PROJ_SYNC_SECTION, KConfigure.PROJ_SYNC_ZW_DSTYPE);
List target = new List();
if (TCUtil.IsType(mo.SoaType, "Dataset")) {
//if (!TCUtil.IsType(mo.SoaType, TCUtil.TYPE_EPLAN)) {
// KUtil.DispatcherShow(this, "请选择" + TCUtil.TYPE_EPLAN + "数据集进行操作");
// return;
//}
target.Add((Dataset)mo);
}
else if (TCUtil.IsType(mo.SoaType, "ItemRevision")) {
ModelObject[] children = TCUtil.GetChildFromPref(mo);
for (int i = 0; i < children.Length; i++) {
if (TCUtil.IsType(children[i].SoaType, eplanDSType)) {
target.Add((Dataset)children[i]);
}
}
}
else {
KUtil.DispatcherShow(this, "请选择版本或数据集进行操作");
return;
}
if (target.Count == 0) {
KUtil.DispatcherShow(this, "当前对象下不存在EPLAN数据集");
return;
}
if (target.Count > 1) {
KUtil.DispatcherShow(this, "当前对象下存在多个数据集,请选择要打开的数据集");
return;
}
Dataset ds = target[0];
List imanFiles = TCUtil.GetDatasetFile(ds, null);
TCUtil.GetProperties(false, TCUtil.Refresh(ds), "object_string");
if (imanFiles.Count == 0) {
KUtil.DispatcherShow(this, "数据集<" + ds.Object_string + ">下没有找到引用文件");
return;
}
if (imanFiles.Count > 1) {
KUtil.DispatcherShow(this, "数据集<" + ds.Object_string + ">下找到多个引用文件");
return;
}
FileInfo file = TCUtil.GetFileInfo(imanFiles[0]);
if (file == null) {
KUtil.DispatcherShow(this, "从数据集<" + ds.Object_string + ">下载引用文件失败");
return;
}
if (file.Attributes.ToString().IndexOf("ReadOnly") != -1) {
file.Attributes = FileAttributes.Normal;
}
Close();
progress.BeginPart(100.0, "");
progress.ShowImmediately();
progress.SetAllowCancel(true);
if (file.FullName.ToLower().EndsWith("zw1")) {
string projName = file.Name.Replace(".zw1", "").Replace(".ZW1", "");
KUtil.Log("打开项目:" + projName + " - " + file.FullName);
string targetFolder = KUtil.GetConfigValue(KConfigure.EPLAN_DATA_SECTION,KConfigure.EPLAN_WORKSAPCE);
if (KUtil.IsEmpty(targetFolder)) {
KUtil.DispatcherShow(this, "未配置项目解压路径");
return;
}
if (!Directory.Exists(targetFolder)) {
Directory.CreateDirectory(targetFolder);
}
using (new LockingStep()) {
RestoreProject(targetFolder,file.FullName, projName);
}
}
else {
System.Diagnostics.Process.Start(file.FullName);
}
}
private bool RestoreProject(string folderPath,string zipName, string projName) {
Restore restore = new Restore();
StringCollection archives = new StringCollection();
archives.Add(zipName);
restore.Project(archives, folderPath, projName, false, true);
ProjectManager manager = new ProjectManager();
if (File.Exists(folderPath + "\\" + projName + ".elk")) {
manager.OpenProject(folderPath + "\\" + projName + ".elk");
}
else if (File.Exists(folderPath + "\\" + projName + ".ell")) {
manager.OpenProject(folderPath + "\\" + projName + ".ell");
}
else {
throw new Exception("未找到项目文件");
}
/*try {
manager = new ProjectManager();
context = new ActionCallingContext();
context.AddParameter("TYPE", "PROJECT");
context.AddParameter("ARCHIVENAME", zipName);
context.AddParameter("PROJECTNAME", projName);
context.AddParameter("UNPACKPROJECT", "0");
// progress = new Progress("SimpleProgress");
//progress.BeginPart(100.0, "");
//progress.ShowImmediately();
// progress.SetAllowCancel(true);
interpreter = new CommandLineInterpreter();
if (interpreter.Execute("restore", context)) {
//MessageBox.Show("该项目不能恢复!");
}
// progress.EndPart(true);
manager.OpenProject(PathMap.SubstitutePath(projName));
}
catch (Exception exception1) {
KUtil.LogErr(exception1);
KUtil.DispatcherShow(this, exception1.Message);
}*/
return true;
}
private void b_Open_Click(object sender, RoutedEventArgs e) {
if (this.selectedObject == null) {
MessageBox.Show(this, "请选择要打开的对象");
}
else {
Progress progress = new Progress("SimpleProgress");
try {
openDs(selectedObject, progress);
progress.EndPart(true);
}
catch(System.Exception ex) {
KUtil.LogErr(ex);
progress.EndPart(true);
MessageBox.Show(this,"打开出错:"+ex.Message);
}
//bgWorker_Open.Start(this.selectedObject);
}
}
private void LoadFromConfiguration() {
//加载查询条件
LoadQueryFields();
//加载查询结果
LoadQueryTable();
}
private void LoadQueryTable() {
queryResultData.Rows.Clear();
string queryResults = KUtil.GetConfigValue(KConfigure.QUERY_PROJ_SECTION, KConfigure.QUERY_PROJ_RESULTS);
string[] queryResultSplit = queryResults.Split(';');
int len = queryResultSplit.Length;
for (int i = 0; i < len; i++) {
string queryResult = queryResultSplit[i].Trim();
if (KUtil.IsEmpty(queryResult)) {
continue;
}
string titile = queryResult;
string config = queryResult;
int width = 50;
string[] split = queryResult.Split(',');
if (split.Length > 1) {
titile = split[0].Trim();
config = split[1].Trim();
}
if (split.Length > 2) {
Int32.TryParse(split[2].Trim(), out width);
}
queryResultConfig.Add(config);
queryResultWidth.Add(width);
queryResultData.Columns.Add(new DataColumn() {
ColumnName = titile,
DataType = i == 0 ? typeof(TableRowData) : typeof(string)
});
}
//queryResultData.Rows.Add(new object[] { 1 });
datagrid_SearchResult.DataContext = queryResultData.DefaultView;
}
private void LoadQueryFields() {
string heightConfig = KUtil.GetConfigValue(KConfigure.QUERY_PROJ_SECTION, KConfigure.QUERY_PROJ_HEIGHT);
if(!KUtil.IsEmpty(heightConfig)){
double h = 0;
Double.TryParse(heightConfig,out h);
if (h > 0) {
rd_QueryField.Height = new GridLength(h);
}
}
this.queryName = KUtil.GetConfigValue(KConfigure.QUERY_PROJ_SECTION, KConfigure.QUERY_PROJ_NAME);
string queryFields = KUtil.GetConfigValue(KConfigure.QUERY_PROJ_SECTION, KConfigure.QUERY_PROJ_FIELDS);
string[] queryFieldSplit = queryFields.Split(';');
int len = queryFieldSplit.Length;
for (int i = 0; i < len; i++) {
string queryField = queryFieldSplit[i].Trim();
if (KUtil.IsEmpty(queryField)) {
continue;
}
string disName = queryField;
string realName = queryField;
string[] split = queryField.Split(',');
if (split.Length == 2) {
disName = split[0].Trim();
realName = split[1].Trim();
}
if (queryFieldMap.ContainsKey(realName)) {
continue;
}
RowDefinition rd = new RowDefinition();
rd.Height = new GridLength(1.0, GridUnitType.Auto);
grid_QueryField.RowDefinitions.Add(rd);
Label label = new Label();
label.Content = disName;
label.HorizontalContentAlignment=HorizontalAlignment.Right;
Grid.SetRow(label,queryFieldMap.Count);
Grid.SetColumn(label,0);
grid_QueryField.Children.Add(label);
TextBox textBox = new TextBox();
Grid.SetRow(textBox, queryFieldMap.Count);
Grid.SetColumn(textBox, 1);
textBox.Margin = new Thickness(0, 2, 0, 8);
textBox.Height = 24;
textBox.VerticalContentAlignment = VerticalAlignment.Center;
grid_QueryField.Children.Add(textBox);
queryFieldMap.Add(realName, textBox);
}
}
private void Async_Query(object sender, DoWorkEventArgs e) {
Dictionary fields = (Dictionary)e.Argument;
ModelObject[] queryRes=null;
try {
bgWorker_Query.Progress("正在查询...", 1);
queryRes = TCUtil.query(queryName, fields);
}
catch (System.Exception ex) {
this.Dispatcher.Invoke(new Action(delegate {
MessageBox.Show(this, "查询出错:" + ex.Message, "错误");
}));
KUtil.LogErr(ex);
return;
}
if (queryRes == null) {
return;
}
//提取信息
TCUtil.dmService.RefreshObjects(queryRes);
try {
int len = queryRes.Length;
for (int i = 0; i < len; i++) {
if (bgWorker_Query.IsCancel()) {
e.Cancel = true;
break;
}
bgWorker_Query.Progress("加载数据 " + (i + 1) + "/" + len + " ", (i + 1) * 100 / len);
TableRowData data = new TableRowData(queryRes[i]);
object[] rowData = data.GetRowData(i + 1, queryResultConfig);
this.Dispatcher.Invoke(new Action(delegate {
queryResultData.Rows.Add(rowData);
}));
}
}
catch (System.Exception ex) {
this.Dispatcher.Invoke(new Action(delegate {
MessageBox.Show(this, "读取数据出错:" + ex.Message, "错误");
}));
KUtil.LogErr(ex);
return;
}
}
private void Async_LoadHome(object sender, DoWorkEventArgs e) {
TCTreeItem parent = (TCTreeItem)e.Argument;
if (parent == null) {
//tree_Home.Items.Clear();
if (TCUtil.user == null) {
throw new Exception("加载Home失败,用户未登录");
}
Folder home = TCUtil.user.Home_folder;
//TreeViewItem node_home = null;
bgWorker_Homer.Progress("加载中...", 0);
TCTreeItem tree_home = null;
this.Dispatcher.Invoke(new Action(delegate {
tree_home = new TCTreeItem(home);
tree_Home.Items.Add(tree_home);
tree_home.Items.Add(new TCTreeItem(null));
}));
}
else {
Cycle_Home(parent, e);
}
//this.Dispatcher.Invoke(new Action(delegate {
// MessageBox.Show(this,"Home下对象数量"+mo.Length);
//}));
}
private void Cycle_Home(TCTreeItem treeItem, DoWorkEventArgs e) {
if (!(treeItem.Items.Count == 1 && ((TCTreeItem)treeItem.Items.GetItemAt(0)).mo == null)) {
return;
}
this.Dispatcher.Invoke(new Action(delegate {
treeItem.Items.Clear();
}));
ModelObject parent = treeItem.mo;
ModelObject[] children = null;
//var t = parent.GetType();
SoaType soaType = parent.SoaType;
ModelObject[] mo = TCUtil.Refresh(parent);
if (TCUtil.IsType(soaType,"Folder")) {
TCUtil.GetProperties(false, mo, "contents");
children = ((Folder)parent).Contents;
}
else if (TCUtil.IsType(soaType, "Item") || TCUtil.IsType(soaType, "ItemRevision")) {
children = TCUtil.GetChildFromPref(parent);
}
//else if(TCUtil.IsType(soaType,"Item")){
// TCUtil.GetProperties(false, mo, "displayable_revisions");
// children = ((Item)parent).Displayable_revisions;
//}
//else if (TCUtil.IsType(soaType,"ItemRevision")) {
// TCUtil.GetProperties(false, mo, "IMAN_specification");
// children = ((ItemRevision)parent).GetProperty("IMAN_specification").ModelObjectArrayValue;
//}
else if (TCUtil.IsType(soaType,"Dataset")) {
children = null;
}
else {
return;
}
if (children == null) {
return;
}
if (bgWorker_Query.IsCancel()) {
e.Cancel = true;
return;
}
string eplanDSType = KUtil.GetConfigValue(KConfigure.PROJ_SYNC_SECTION, KConfigure.PROJ_SYNC_ZW_DSTYPE);
ItemCollection node_children = null;
this.Dispatcher.Invoke(new Action(delegate {
node_children = treeItem.Items;
}));
int len = children.Length;
for (int i=0;i 0) {
this.Dispatcher.Invoke(new Action(delegate {
TCTreeItem viewNode = new TCTreeItem(parent, "View", "folder_16.png");
node_children.Add(viewNode);
ItemCollection view_children = viewNode.Items;
for(int i = 0; i < bomCnt; i++) {
if (bgWorker_Query.IsCancel()) {
e.Cancel = true;
return;
}
bgWorker_Homer.Progress("加载视图...( " + (i + 1) + " / " + bomCnt + " )", (i + 1) * 100 / bomCnt);
TCTreeItem cc = new TCTreeItem(bomChildren[i]);
view_children.Add(cc);
cc.Items.Add(new TCTreeItem(null));
}
}));
}
}
}
private void WindowLoaded(object sender, RoutedEventArgs e) {
bgWorker_Homer.Start(null);
}
private void ExpandHomeTree(object sender, RoutedEventArgs e) {
TCTreeItem item = e.OriginalSource as TCTreeItem;
bgWorker_Homer.Start(item);
}
private void b_Query_Click(object sender, RoutedEventArgs e) {
queryResultData.Rows.Clear();
//查询
Dictionary fields= new Dictionary();
foreach (string key in queryFieldMap.Keys) {
string value = queryFieldMap[key].Text;
if (!KUtil.IsEmpty(value)) {
fields.Add(key, value);
}
}
if (fields.Count == 0) {
MessageBox.Show(this, "请输入查询条件", "提示");
return;
}
//异步执行
bgWorker_Query.Start(fields);
//bgWorker = new KBgWorker2(this,backgroundWorker);
//backgroundWorker.RunWorkerAsync(fields);
}
private void b_Reset_Click(object sender, RoutedEventArgs e) {
foreach (TextBox tb in queryFieldMap.Values) {
tb.Text = "";
}
queryResultData.Rows.Clear();
}
//private void DataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
// ScrollViewer scv = (ScrollViewer)sender;
// scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta);
// e.Handled = true;
//}
private void QueryTableLoaded(object sender, RoutedEventArgs e) {
int len = queryResultWidth.Count;
for (int i = 0; i < len; i++) {
datagrid_SearchResult.Columns[i].Width = queryResultWidth[i];
}
}
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) {
int index = datagrid_SearchResult.SelectedIndex;
if (index < 0) {
tabItem_SearchTarget.Visibility = Visibility.Hidden;
tab_home.SelectedIndex = 0;
selectedObject = null;
return;
}
tabItem_SearchTarget.Visibility = Visibility.Visible;
tab_home.SelectedIndex = 1;
try {
tree_SearchTarget.Items.Clear();
DataRow row = queryResultData.Rows[index];
TableRowData ss = row.Field(0);
ModelObject mo = ss.mo;
this.selectedObject = mo;
ModelObject[] mos = TCUtil.Refresh(mo);
TCUtil.GetProperties(false, mos, "object_string");
tabItem_SearchTarget.Header = mo.GetPropertyDisplayableValue("object_string");
TCTreeItem target = new TCTreeItem(mo);
tree_SearchTarget.Items.Add(target);
target.Items.Add(new TCTreeItem(null));
this.Dispatcher.Invoke(new Action(delegate {
target.ExpandSubtree();
}));
} catch (System.Exception ex) {
KUtil.LogErr(ex);
MessageBox.Show(ex.Message);
}
}
private void tree_SelectionChanged(object sender, RoutedPropertyChangedEventArgs