using log4net;
using PdmSwPlugin.Common;
using PdmSwPlugin.Common.Entity;
using PdmSwPlugin.Common.Util;
using SolidWorks.Interop.sldworks;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using PdmSwPlugin.Common.Util.Http;
namespace PdmSwPlugin.MaterialSelect
{
///
/// MaterialSelectControl.xaml 的交互逻辑
///
[PdmSwPlugin(AppId = "", Title = "物料优选", Version = "0.1")]
public partial class MaterialSelectControl : UserControl, INotifyPropertyChanged
{
///
/// 日志处理
///
private static ILog Logger = LogManager.GetLogger("MaterialSelect");
#region 字段
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public void RaiseAndSetIfChanged(ref T old, T @new, [CallerMemberName] string propertyName = null)
{
old = @new;
if (propertyName != null)
{
RaisePropertyChanged(propertyName);
}
}
public static long GetWindowHandle(SldWorks sld)
{
MaterialSelectControl control = new MaterialSelectControl(sld);
// 只能用winform,代理wpf
ElementHost host = new ElementHost
{
Child = control,
AutoSize = true,
Dock = System.Windows.Forms.DockStyle.Fill
};
/// 下面这一段不要删,指不定哪天又用到了
System.Windows.Forms.Form form = new System.Windows.Forms.Form
{
ControlBox = false,
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None,
KeyPreview = true,
Dock = System.Windows.Forms.DockStyle.Fill
};
form.Controls.Add(host);
return form.Handle.ToInt64();
}
private string _partNo;
public string PartNo
{
get => _partNo;
set => RaiseAndSetIfChanged(ref _partNo, value);
}
private string _partModel;
public string PartModel
{
get => _partModel;
set => RaiseAndSetIfChanged(ref _partModel, value);
}
private string _partBrandName;
public string PartBrandName
{
get => _partBrandName;
set => RaiseAndSetIfChanged(ref _partBrandName, value);
}
private string _partBussinesstypeName;
public string PartBussinesstypeName
{
get => _partBussinesstypeName;
set
{
RaiseAndSetIfChanged(ref _partBussinesstypeName, value);
SelectBussinessType();
}
}
private string _selectedBtName;
public string SelectedBtName
{
get => _selectedBtName;
set
{
RaiseAndSetIfChanged(ref _selectedBtName, value);
SelectBussinesstypeSpec();
}
}
///
/// 底部状态栏内容
///
private string _statusBarText;
public string StatusBarText
{
get => _statusBarText;
set => RaiseAndSetIfChanged(ref _statusBarText, value);
}
///
/// 当前打开的文档路径
///
private string _activeDocPath;
public string ActiveDocPath
{
get => _activeDocPath;
set => RaiseAndSetIfChanged(ref _activeDocPath, value);
}
private string _order;
public string Order
{
get => _order;
set => RaiseAndSetIfChanged(ref _order, value);
}
private string _column;
public string Column
{
get => _column;
set => RaiseAndSetIfChanged(ref _column, value);
}
private ObservableCollection _btNames;
public ObservableCollection BtNames
{
get => _btNames;
set => RaiseAndSetIfChanged(ref _btNames, value);
}
public Dictionary BtCache { get; set; }
#region 分页数据
private long _currentPage;
public long CurrentPage
{
get => _currentPage;
set => RaiseAndSetIfChanged(ref _currentPage, value);
}
private long _totalPage;
public long TotalPage
{
get => _totalPage;
set => RaiseAndSetIfChanged(ref _totalPage, value);
}
private long _totalCount;
public long TotalCount
{
get { return _totalCount; }
set => RaiseAndSetIfChanged(ref _totalCount, value);
}
private long _size;
public long Size
{
get { return _size; }
set => RaiseAndSetIfChanged(ref _size, value);
}
private bool _loading = false;
public bool Loading
{
get => _loading;
set => RaiseAndSetIfChanged(ref _loading, value);
}
#endregion 分页数据
#endregion 字段
#region 控件
#endregion
///
/// 按钮查询函数
///
/// *
/// *
private void SimpleQuery(object sender, RoutedEventArgs e)
{
HttpSelectPartWithSpec(1, 10);
}
///
/// 重置参数并查询
///
///
///
private void ResetQuery(object sender, RoutedEventArgs e)
{
ResetQuery();
SimpleQuery(sender, e);
}
///
/// 详细查询按钮
///
///
///
private void SelectBussinesstypeSpec(object sender, RoutedEventArgs e)
{
SelectBussinesstypeSpec();
}
///
/// 重置规格按钮Click-清空规格参数并查询
///
///
///
private void ResetSpec(object sender, RoutedEventArgs e)
{
ResetSpec();
SimpleQuery(sender, e);
}
///
/// 换页
///
///
///
private void ChangePage(object sender, RoutedEventArgs e)
{
string type = (string)((Button)sender).Tag;
long nextPage;
switch (type)
{
case "1":
nextPage = CurrentPage + 1;
if (nextPage > TotalPage)
{
return;
}
HttpSelectPartWithSpec(nextPage, 10);
break;
case "-1":
nextPage = CurrentPage - 1;
if (nextPage < 1)
{
return;
}
HttpSelectPartWithSpec(nextPage, 10);
break;
case "2":
HttpSelectPartWithSpec(TotalPage, 10);
break;
case "-2":
HttpSelectPartWithSpec(1, 10);
break;
default:
break;
}
}
///
/// 下载物料图纸
///
/// *
/// *
private void LoadPartDrawingDoc(object sender, RoutedEventArgs e)
{
try
{
PdmPart part = table.SelectedItem as PdmPart;
DoAddCompnent(part);
e.Handled = true;
}
catch (Exception ex)
{
this.Error($"加载图纸失败!异常:{ex}");
}
// HttpClientCreator.GetClient().GetDownloadWithName("penApi/wpf/bt/eDrawing", "C:\\temp");
}
///
/// 清空所有参数
///
private void ResetQuery()
{
PartNo = null;
PartModel = null;
ResetSpec();
}
///
/// 清空规格参数
///
private void ResetSpec()
{
//PartBussinesstypeName = "";
//SelectedBtName = null;
//if (BtNames != null)
//{
// BtNames.Clear();
//}
//if (BtCache != null)
//{
// BtCache.Clear();
//}
ObservableCollection specs = (ObservableCollection)specTable.ItemsSource;
if (specs == null || specs.Count <= 0)
{
return;
}
foreach (PartSpec spec in specs)
{
spec.value = null;
}
}
///
/// 更新分页相关
///
/// 从web返回的分页数据
private void UpdatePage(Page page)
{
List cache = new List();
foreach (DataGridColumn col in table.Columns)
{
cache.Add(col.SortDirection);
}
CurrentPage = page.current;
TotalCount = page.total;
Size = page.size;
TotalPage = (long)Math.Ceiling(TotalCount * 1.0D / Size);
table.ItemsSource = page.records;
for (int i = 0; i < table.Columns.Count; i++)
{
table.Columns[i].SortDirection = cache[i];
}
}
///
/// 物料行选中事件,预览图纸
///
/// *
/// *
private void table_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
object[] selectedItems = (object[])e.AddedItems;
if (selectedItems.Length <= 0)
{
return;
}
PdmPart part = (PdmPart)selectedItems[0];
if (string.IsNullOrEmpty(part.drawingPath))
{
StatusBarText = $"服务器未返回 [{part.partNo}] 图纸路径";
return;
}
string path = GlobalConfig.SwFilePath + Path.DirectorySeparatorChar + part.drawingPath;
if (!File.Exists(path))
{
StatusBarText = $"未找到 [{part.partNo}] 图纸文件:{path}";
return;
}
StatusBarText = "";
eControl.FilePath = GlobalConfig.SwFilePath + Path.DirectorySeparatorChar + part.drawingPath;
//eControl.EDrawingHost.Dispose();
}
private void table_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
try
{
PdmPart part = table.SelectedItem as PdmPart;
DoAddCompnent(part);
}
catch (Exception ex)
{
Logger.Error("加载图纸失败!异常:{0}", ex);
this.Error($"加载图纸失败!异常:{ex}");
}
}
private void DoAddCompnent(PdmPart part)
{
string absolutePath = MoveFileToUserDir(part);
// string absolutePath = DownloadFileToUserDir(part);
if (!string.IsNullOrEmpty(absolutePath))
{
AddComponent(absolutePath);
}
}
///
/// 共享盘的方式
///
///
///
private string MoveFileToUserDir(PdmPart part)
{
string newPath = null;
if (!string.IsNullOrEmpty(part.drawingPath))
{
string absolutePath = GlobalConfig.SwFilePath + Path.DirectorySeparatorChar + part.drawingPath;
newPath = GlobalConfig.UserPartsDir + Path.DirectorySeparatorChar + part.drawingPath;
if (!File.Exists(newPath))
{
Directory.CreateDirectory(Path.GetDirectoryName(newPath));
File.Copy(absolutePath, newPath, true);
}
else if (!MD5Util.FileEquals(absolutePath, newPath))
{
// 防止文件只读
FileInfo info = new FileInfo(newPath);
info.IsReadOnly = false;
File.Delete(newPath);
File.Copy(absolutePath, newPath, true);
}
}
return newPath;
}
///
/// 下载的方式
///
///
///
private string DownloadFileToUserDir(PdmPart part)
{
string newPath = null;
if (!string.IsNullOrEmpty(part.drawingPath))
{
newPath = GlobalConfig.UserPartsDir + Path.DirectorySeparatorChar + part.drawingPath;
if (!File.Exists(newPath))
{
Directory.CreateDirectory(Path.GetDirectoryName(newPath));
HttpClient c = clientCreator.GetClient();
c.GetDownloadSync($"openApi/wpf/downloadCadFile/{part.id}", newPath);
}
else if (!MD5Util.IsFile(newPath, part.md5))
{
// 防止本地文件只读
FileInfo info = new FileInfo(newPath);
info.IsReadOnly = false;
File.Delete(newPath);
HttpClient c = clientCreator.GetClient();
c.GetDownloadSync($"openApi/wpf/downloadCadFile/{part.id}", newPath);
}
}
return newPath;
}
private void table_Sorting(object sender, DataGridSortingEventArgs e)
{
Column = e.Column.SortMemberPath;
if (e.Column.SortDirection == null)
{
e.Column.SortDirection = ListSortDirection.Ascending;
Order = "ASC";
}
else if (e.Column.SortDirection == ListSortDirection.Ascending)
{
e.Column.SortDirection = ListSortDirection.Descending;
Order = "DESC";
}
else
{
e.Column.SortDirection = null;
Column = null;
Order = "DESC";
}
HttpSelectPartWithSpec(1, 10);
e.Handled = true;
}
}
}