using ControlzEx.Standard;
using DataEntity;
using DataEntity.Share;
using DataRWDAL;
using HandyControl.Data;
using NPOI.POIFS.Properties;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using XCommon.Image;
using XCommon.Log;
using XCommon.Tip;
using XCoreBLL;
using XHandler.Class;
using XHandler.Class.DataEx;
using XHandler.Controls;
using XHandler.View.MethodProperty;
using XImagingXhandler.XDAL;
namespace XHandler.View.Tabletop
{
///
/// 台面模板管理 的交互逻辑
///
public partial class TabletopManagement : UserControl
{
ObservableCollection tabletopTemplates; //系统中可用的台面模板数据集
OperateAuditLogBll operateAuditLogBll = new OperateAuditLogBll(); //向系统中提交操作时数据的类对象
ObservableCollection latticesList; //台面内获取到的所有板位数据集
public MainWindow mainWindow = null;//主窗体
#region 构造函数
public TabletopManagement()
{
InitializeComponent();
this.DataContext = tabletopTemplates;
}
#endregion
#region 加载绑定已创建的台面模板列表
public void BindingTabletopListOnListbox()
{
try
{
tabletopTemplates = TabletopTemplateDB.GetTabletopTemplateCollectionFromdb();
lbxTabletopList.ItemsSource = tabletopTemplates;
if (tabletopTemplates != null && tabletopTemplates.Count > 0)
{
lbxTabletopList.SelectedIndex = 0;
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("BindingTabletopListOnListbox Error ", ex);
}
}
#endregion
#region 加载绑定已创建的载架视图列表
public void BindingTubeLabwareOnListView()
{
try
{
ObservableCollection labwares = LabwareDB.GetSpecialShelfLabware();
//调用比例进行板材的像素转换
foreach (var item in labwares)
{
item.labware_length = ConvertScale.GetPixelDistanceByConvertScale(item.labware_length);
item.labware_width = ConvertScale.GetPixelDistanceByConvertScale(item.labware_width);
}
lvConsumable.ItemsSource = labwares;
}
catch (Exception ex)
{
}
}
#endregion
#region 选定台面模板后,处理更新台面板位数据
private void lbxTabletopList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lbxTabletopList.SelectedIndex < 0)
return;
gridTable.Children.Clear();
TabletopTemplate tabletopTemplate = lbxTabletopList.SelectedItem as TabletopTemplate;
if (tabletopTemplate != null)
{
//加载该台面模板
latticesList = TabletopTemplateDB.GetLatticesByTabletopTemplateId(tabletopTemplate.tabletopid);
foreach(Lattice s in latticesList)
{
Labware labware = LabwareDB.GetLabware(s.labware_id);
double widthPixel = labware.labware_width / 2;
double lengthPixel = labware.labware_length / 2;
ShelfLabware shelfLabware = new ShelfLabware()
{
ShelfWidth = widthPixel,
ShelfLength = lengthPixel,
ShelfName = s.lattice_num.ToString(),
ShelfDBId = s.lattice_id,
ShelfTrash = s.is_trash == 1 ? true : false
};
shelfLabware.AllowDrop = false;
labware.lattice_pixel_x = s.lattice_pixel_x;
labware.lattice_pixel_y = s.lattice_pixel_y;
shelfLabware.DataContext = (Labware)labware;
shelfLabware.MouseLeftButtonDown += ShelfLabware_MouseLeftButtonDown;
shelfLabware.PreviewMouseLeftButtonUp += ShelfLabware_MouseLeftButtonUp;
QueryContinueDrag += DoQueryContinueDrag;
shelfLabware.Loaded += ShelfLabwareInitial_Loaded;
shelfLabware.PreviewMouseMove += ShelfLabware_PreviewMouseMove;
shelfLabware.PreviewMouseRightButtonDown += ShelfLabware_PreviewMouseRightButtonDown;
shelfLabware.ContextMenu = FindResource("tableLatticeMenu") as ContextMenu;
gridTable.Children.Add(shelfLabware);
System.Threading.Thread.Sleep(10);
}
}
}
#endregion
#region 选定的台面,加载板位完成后,对板位属性的处理
private void ShelfLabwareInitial_Loaded(object sender, RoutedEventArgs e)
{
ShelfLabware shelfLabware = sender as ShelfLabware;
shelfLabware.HorizontalAlignment = HorizontalAlignment.Left;
shelfLabware.VerticalAlignment = VerticalAlignment.Top;
if(shelfLabware.ShelfTrash)
{
shelfLabware.ShelfTrash = false;
shelfLabware.ShelfTrash = true;
}
//Canvas.SetLeft(shelfLabware, (shelfLabware.DataContext as Labware).lattice_pixel_x);
//Canvas.SetTop(shelfLabware, (shelfLabware.DataContext as Labware).lattice_pixel_y);
shelfLabware.Margin = new Thickness((shelfLabware.DataContext as Labware).lattice_pixel_x, (shelfLabware.DataContext as Labware).lattice_pixel_y, 0, 0);
}
#endregion
#region 页面加载事件,绑定数据处理
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
BindingTabletopListOnListbox();
BindingTubeLabwareOnListView();
//CalculationPixels();
SetTabletopGraduated();
}
#endregion
#region 重置台面
private void btnResetTabletop_Click(object sender, RoutedEventArgs e)
{
//判断已加的板位是否保存过,保存过则更新数据库
List shelfLabwares = Utilities.FindAllVisualChild(gridTable);
foreach (ShelfLabware s in shelfLabwares)
{
if (!string.IsNullOrEmpty(s.ShelfDBId))
{
Lattice lattice = LatticeDB.GetLatticeDataByIdFromdb(s.ShelfDBId);
lattice.lattice_state = 0;
TabletopRelLatticeDB.DeleteLatticeFromdb(lattice);
}
else
{
}
}
gridTable.Children.Clear();
}
#endregion
#region 新加一个台面模板
///
/// 新加一个台面模板
///
///
///
public TabletopTemplate AddNewTabletopTemplate(string name)
{
TabletopTemplate newTabletopTemplate = new TabletopTemplate();
newTabletopTemplate.tabletopname = name;
newTabletopTemplate.tabletopstate = 1;
newTabletopTemplate.usestate = 0;
newTabletopTemplate.tabletopid = Guid.NewGuid().ToString();
int ret = TabletopTemplateDB.AddTabletopTemplateIntodb(newTabletopTemplate);
if (ret > 0)
{
tabletopTemplates.Add(newTabletopTemplate);
PlsSetProperty plsSetProperty = new PlsSetProperty($"台面模板:{name} 添加成功", false);
plsSetProperty.closeEvent += PlsSetProperty_closeEvent;
System.Windows.Window wnd = Application.Current.MainWindow;
Grid parent = Utilities.FindVisualChild(wnd);
parent.Children.Add(plsSetProperty);
}
else
{
newTabletopTemplate = null;
PlsSetProperty plsSetProperty = new PlsSetProperty($"台面模板:{name} 添加失败", false);
plsSetProperty.closeEvent += PlsSetProperty_closeEvent;
System.Windows.Window wnd = Application.Current.MainWindow;
Grid parent = Utilities.FindVisualChild(wnd);
parent.Children.Add(plsSetProperty);
}
return newTabletopTemplate;
}
#endregion
#region 关闭新建时弹出的窗口
private void PlsSetProperty_closeEvent(object sender, EventArgs e)
{
System.Windows.Window wnd = Application.Current.MainWindow;
Grid grid = Utilities.FindVisualChild(wnd);
UIElement element = sender as UIElement;
grid.Children.Remove(element);
}
#endregion
#region 布局当前台面的刻度尺
///
/// 布局当前台面的刻度尺
///
public void SetTabletopGraduated()
{
double length = Shared.SoftwareInformation.realtabletoplength;//mm
double width = Shared.SoftwareInformation.realtabletopwidth;//mm
gridTable.Width = gridBaseTabletop.Width = length / 2;//统一缩小2倍,即1个像素代表2mm
gridTable.Height = gridBaseTabletop.Height = width / 2;
}
#endregion
#region 获取当前给定显示台面区域的像素长度,映射出的实际长度
///
/// 获取当前给定显示台面区域的像素长度,映射出的实际长度
///
public void CalculationPixels()
{
double length = 150d;//Shared.SoftwareInformation.realtabletoplength;//mm
double width = 125d;//Shared.SoftwareInformation.realtabletopwidth;//mm
double lengthPixelVal = MillimetersToPixelsWidth(length);
double widthPixedVal = MillimetersToPixelsWidth(width);
//计算像素比例,然后设置控件长宽
double ratioOfTabletop = widthPixedVal / lengthPixelVal;
int zoomVal = 1, zoomBaseVal = 1;
int i = 0;
double calLengthVal = lengthPixelVal / zoomVal;
while (calLengthVal > 1000)
{
i++;
zoomVal = zoomBaseVal * i;
calLengthVal = lengthPixelVal / zoomVal;
}
Shared.tabletopReduceTimes = zoomVal;
//设置台面控件像素长宽
double tabletopPixedXVal = lengthPixelVal / zoomVal;
double tabletopPixedYVal = tabletopPixedXVal * ratioOfTabletop;
gridBaseTabletop.Width = tabletopPixedXVal;
gridBaseTabletop.Height = tabletopPixedYVal;
//计算每个像素对应实际台面多少mm
double disX = 1 * zoomVal;
double disY = 1 * zoomVal;
// 假设1像素等于2.54毫米(约等于一英寸)
disX = PixelsCalculationHelper.PixelsToInches(1 * zoomVal, PixelsCalculationHelper.DpiX) * 2.54;
disY = PixelsCalculationHelper.PixelsToInches(1 * zoomVal, PixelsCalculationHelper.DpiY) * 2.54;
//50mm占用多少个像素
double countPixelX = 50d / disX;
double countPixelY = 50d / disY;
}
#endregion
#region 毫米转换为像素
///
/// 毫米转换为像素
///
/// length是毫米
/// 像素值
public static double MillimetersToPixelsWidth(double length) //length是毫米,1厘米=10毫米
{
System.Windows.Forms.Panel p = new System.Windows.Forms.Panel();
System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(p.Handle);
IntPtr hdc = g.GetHdc();
int width = GetDeviceCaps(hdc, 4); // HORZRES
int pixels = GetDeviceCaps(hdc, 8); // BITSPIXEL
g.ReleaseHdc(hdc);
return (((double)pixels / (double)width) * (double)length);
}
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int Index); //导入系统方法,获取当前分辨率下的像素和距离值
#endregion
TextBlock toolTip = null; //鼠标在台面内移动过程中弹出的提示对象
System.Windows.Point mouseMovePos = new System.Windows.Point(0, 0); //鼠标在台面内移动时,实时存储鼠标点的坐标数据
#region 鼠标在台面上移动事件,移动过程中显示坐标
private void gridTable_MouseMove(object sender, MouseEventArgs e)
{
// 获取鼠标相对于Grid的位置
mouseMovePos = e.GetPosition(gridTable);
//像素坐标值转换为cm值
double length = (double)mouseMovePos.X * 2 / 10d;
double width = (double)mouseMovePos.Y * 2 / 10d;
// 显示坐标值
if (toolTip == null)
{
toolTip = new TextBlock();
toolTip.Text = "(x:" + length.ToString() + ",y:" + width.ToString() + ")";
toolTip.Background = Brushes.Transparent;
toolTip.Foreground = Brushes.Black;
toolTip.Loaded += ToolTip_Loaded;
gridTable.Children.Add(toolTip);
}
else
{
toolTip.Margin = new Thickness(mouseMovePos.X, mouseMovePos.Y, 0, 0);
toolTip.Text = "(x:" + length.ToString() + ",y:" + width.ToString() + ")";
}
if (e.LeftButton == MouseButtonState.Pressed)
{
System.Windows.Point currentPosition = e.GetPosition(gridTable);
var currentPoint = e.GetPosition(gridTable);
Console.WriteLine("currentPoint x:{0},y:{1}", currentPoint.X, currentPoint.Y);
double left = Math.Min(startPoint.X, currentPoint.X);
double top = Math.Min(startPoint.Y, currentPoint.Y);
double gwidth = Math.Abs(startPoint.X - currentPoint.X);
double height = Math.Abs(startPoint.Y - currentPoint.Y);
//Grid.SetLeft(selectionRectangle, left);
//Grid.SetTop(selectionRectangle, top);
//gridTable.Children.Add(selectionRectangle);
//Grid.SetRow(selectionRectangle, 0);
//Grid.SetColumn(selectionRectangle, 0);
if (selectionRectangle != null)
{
selectionRectangle.Width = gwidth;
selectionRectangle.Height = height;
selectionRectangle.Margin = new Thickness(left, top, 0, 0);
}
if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > SystemParameters.MinimumHorizontalDragDistance)
|| (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > SystemParameters.MinimumVerticalDragDistance))
{
if (currentSelectedShelf == null)
{
return;
}
DragDropAdorner adorner = new DragDropAdorner((UIElement)currentSelectedShelf);
mAdornerLayer = AdornerLayer.GetAdornerLayer(gridTable);
mAdornerLayer.Add(adorner);
Labware lb = currentSelectedShelf.DataContext as Labware;
LabwareEx labwareEx = new LabwareEx(lb);
DataObject data = new DataObject("LabwareEx", labwareEx);
DragDrop.DoDragDrop(currentSelectedShelf, data, DragDropEffects.Move);
mAdornerLayer.Remove(adorner);
mAdornerLayer = null;
}
if(!isDragging)
{
isDragging= true;
}
if (!isPressing)
{
isPressing = true;
}
}
}
#endregion
#region 提示框加载显示完成后的处理
private void ToolTip_Loaded(object sender, RoutedEventArgs e)
{
toolTip.HorizontalAlignment = HorizontalAlignment.Left;
toolTip.VerticalAlignment= VerticalAlignment.Top;
toolTip.Margin = new Thickness(mouseMovePos.X, mouseMovePos.Y, 0, 0);
//mAdornerLayerTool.Remove(adornerTool);
//mAdornerLayerTool = null;
}
#endregion
private System.Windows.Point _lastMouseDown; //鼠标左键按下选中板位列表中某个板位时,鼠标点的位置
private AdornerLayer mAdornerLayer = null; //拖动选中的板位耗材时,控制跟随显示板位的对象
#region 鼠标左键按下选中板位列表中某个板位的事件
private void lvConsumable_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
_lastMouseDown = e.GetPosition(lvConsumable);
}
}
#endregion
#region 鼠标从选中某个列表中的板位后,开始移动时的处理
private void lvConsumable_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
System.Windows.Point currentPosition = e.GetPosition(lvConsumable);
if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > SystemParameters.MinimumHorizontalDragDistance)
|| (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > SystemParameters.MinimumVerticalDragDistance))
{
HitTestResult result = VisualTreeHelper.HitTest(lvConsumable, currentPosition);
if (result == null)
return;
ListViewItem lvi = Utilities.FindVisualParent(result.VisualHit);
if (lvi == null)
return;
if (lvConsumable.SelectedItem == null)
return;
DragDropAdorner adorner = new DragDropAdorner((UIElement)lvi);
mAdornerLayer = AdornerLayer.GetAdornerLayer(lvConsumable);
mAdornerLayer.Add(adorner);
Labware lb = lvConsumable.SelectedItem as Labware;
LabwareEx labwareEx = new LabwareEx(lb);
DataObject data = new DataObject("LabwareEx", labwareEx);
ShelfLabware slabware= Utilities.FindLastVisualChild(lvi);
DragDrop.DoDragDrop(slabware, data, DragDropEffects.Copy);
if (mAdornerLayer == null)
mAdornerLayer = AdornerLayer.GetAdornerLayer(lvConsumable);
{
mAdornerLayer.Remove(adorner);
mAdornerLayer = null;
}
}
isDragging = true;
}
}
#endregion
#region 继续拖动控制,实时更新拖动对象的显示UI
private void DoQueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
if (mAdornerLayer != null)
{
mAdornerLayer.Update();
}
}
#endregion
#region 从板位耗材列表中拖放板位后或从台面内拖放某个板位后,放置板位的处理
System.Windows.Point mouseDropPos=new System.Windows.Point(0,0); //存储某个板位被放置后的时刻,鼠标点的位置数据
private void gridTable_Drop(object sender, DragEventArgs e)
{
//再次取得目标gridTable内鼠标的坐标位置
mouseDropPos = e.GetPosition(gridTable);
if (e.Effects == DragDropEffects.Move)
{
}
LabwareEx lb = e.Data.GetData("LabwareEx") as LabwareEx;
if (e.Effects == DragDropEffects.Copy ||
e.Effects == DragDropEffects.Move)
{
//lb.label = canvas.Name;
}
if (isDragging)
{
isDragging = false;
if (!isPressing)
{
//构建一个Shelf对象
//根据两1个像素,计算出板位的款和高;
double widthPixel = lb.labware_width / 2;
double lengthPixel = lb.labware_length / 2;
int countOfShelfInGD = Utilities.FindAllVisualChild(gridTable).Count;
ShelfLabware shelfLabware = new ShelfLabware()
{
ShelfWidth = widthPixel,
ShelfLength = lengthPixel,
ShelfName = "P" + (countOfShelfInGD + 1).ToString(),
//Width= lengthPixel,
//Height= widthPixel
};
shelfLabware.AllowDrop = false;
shelfLabware.DataContext = (Labware)lb;
shelfLabware.MouseLeftButtonDown += ShelfLabware_MouseLeftButtonDown;
shelfLabware.PreviewMouseLeftButtonUp += ShelfLabware_MouseLeftButtonUp;
QueryContinueDrag += DoQueryContinueDrag;
shelfLabware.Loaded += ShelfLabware_Loaded;
shelfLabware.PreviewMouseMove += ShelfLabware_PreviewMouseMove;
shelfLabware.PreviewMouseRightButtonDown += ShelfLabware_PreviewMouseRightButtonDown;
shelfLabware.ContextMenu = FindResource("tableLatticeMenu") as ContextMenu;
gridTable.Children.Add(shelfLabware);
}
else
{
double left = currentSelectedShelf.Margin.Left;
double top = currentSelectedShelf.Margin.Top;
//Canvas.SetLeft(currentSelectedShelf, left + e.GetPosition(gridTable).X - dragStartPoint.X);
//Canvas.SetTop(currentSelectedShelf, top + e.GetPosition(gridTable).Y - dragStartPoint.Y);
//原始位置
//新位置
double currentPixelXVal = e.GetPosition(gridTable).X;
double currentPixelYVal = e.GetPosition(gridTable).Y;
//变化
double increaseXVal = dragStartPoint.X - currentPixelXVal;
double increaseYVal = dragStartPoint.Y - currentPixelYVal;
if (increaseXVal > 0)
{
left = left - increaseXVal;
}
else
{
left = left + increaseXVal;
}
if (increaseYVal > 0)
{
top = top - increaseYVal;
}
else
{
top = top + increaseYVal;
}
currentSelectedShelf.Margin = new Thickness(currentPixelXVal, currentPixelYVal, 0, 0);
currentSelectedShelf.ReleaseMouseCapture();
currentSelectedShelf = null;
isPressing = false;
}
}
else
{
if (isPressing)
{
if (currentSelectedShelf != null)
{
currentSelectedShelf.ReleaseMouseCapture();
currentSelectedShelf = null;
}
isPressing = false;
}
else
{
//构建一个Shelf对象
//根据两1个像素,计算出板位的款和高;
double widthPixel = lb.labware_width / 2;
double lengthPixel = lb.labware_length / 2;
int countOfShelfInGD = Utilities.FindAllVisualChild(gridTable).Count;
ShelfLabware shelfLabware = new ShelfLabware()
{
ShelfWidth = widthPixel,
ShelfLength = lengthPixel,
ShelfName = "P" + (countOfShelfInGD + 1).ToString(),
//Width= lengthPixel,
//Height= widthPixel
};
shelfLabware.AllowDrop = false;
shelfLabware.DataContext = (Labware)lb;
shelfLabware.MouseLeftButtonDown += ShelfLabware_MouseLeftButtonDown;
shelfLabware.PreviewMouseLeftButtonUp += ShelfLabware_MouseLeftButtonUp;
QueryContinueDrag += DoQueryContinueDrag;
shelfLabware.Loaded += ShelfLabware_Loaded;
shelfLabware.PreviewMouseMove += ShelfLabware_PreviewMouseMove;
shelfLabware.PreviewMouseRightButtonDown += ShelfLabware_PreviewMouseRightButtonDown;
shelfLabware.ContextMenu = FindResource("tableLatticeMenu") as ContextMenu;
gridTable.Children.Add(shelfLabware);
}
}
}
#endregion
#region 鼠标左键点击台面上某个板位后抬起左键事件
private void ShelfLabware_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isPressing= false;
Console.WriteLine("si1");
}
#endregion
#region 鼠标右键点击台面上某个板位后,创建右键菜单
private void ShelfLabware_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
ShelfLabware selectedShelfLabware = sender as ShelfLabware;
if (selectedShelfLabware == null)
return;
if (selectedShelfLabware != null)
{
ContextMenu menu = FindResource("tableLatticeMenu") as ContextMenu;
MenuItem miTrash = menu.Items[0] as MenuItem;
MenuItem miUnTrash = menu.Items[1] as MenuItem;
if (selectedShelfLabware.ShelfTrash)
{
miTrash.IsEnabled = false;
miUnTrash.IsEnabled = true;
}
else
{
miTrash.IsEnabled = true;
miUnTrash.IsEnabled = false;
}
}
}
#endregion
#region 鼠标点移动到某个台面板位上时,阻止坐标提示显示
private void ShelfLabware_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (toolTip != null)
{
gridTable.Children.Remove(toolTip);
toolTip = null;
}
//e.Handled = true;
}
#endregion
#region 台面上的板位完成加载显示后需要处理的内容
private void ShelfLabware_Loaded(object sender, RoutedEventArgs e)
{
ShelfLabware shelfLabware=sender as ShelfLabware;
shelfLabware.HorizontalAlignment = HorizontalAlignment.Left;
shelfLabware.VerticalAlignment = VerticalAlignment.Top;
shelfLabware.Margin = new Thickness(mouseDropPos.X, mouseDropPos.Y, 0, 0);
}
#endregion
private bool isPressing = false; //鼠标左键是否按住台面上的某个板位:true,false
private bool isDragging = false; //鼠标左键按住后是否拖动,true,false
private System.Windows.Point dragStartPoint; //鼠标左键按住台面上的某个板位位置后,获取该位置坐标点
private ShelfLabware currentSelectedShelf = null; //鼠标左键按住台面上的某个板位,该板位对象
#region 鼠标左键在台面显示区域内按下事件
private void ShelfLabware_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isPressing = true;
dragStartPoint = e.GetPosition(gridTable);
if (e.ChangedButton == MouseButton.Left)
{
//_lastMouseDown = e.GetPosition(sender as ShelfLabware);
currentSelectedShelf = sender as ShelfLabware;
//currentSelectedShelf.CaptureMouse();
double widthPixel = (currentSelectedShelf.DataContext as Labware).labware_width / 2;
double lengthPixel = (currentSelectedShelf.DataContext as Labware).labware_length / 2;
currentSelectedShelf.Width = lengthPixel;
currentSelectedShelf.Height = widthPixel;
}
}
#endregion
#region 鼠标左键从台面显示区域内抬起事件
private void gridTable_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isDragging)
{
isDragging = false;
if (currentSelectedShelf != null)
{
currentSelectedShelf.ReleaseMouseCapture();
}
}
if(isPressing)
{
isPressing = false;
}
if (selectionRectangle != null)
{
gridTable.Children.Remove(selectionRectangle);
}
//ShelfLabware shelfLabware = parentVisual as ShelfLabware;
//循环构建板位对象,添加到集合中
foreach (ShelfLabware item in Utilities.FindAllVisualChild(gridTable))
{
Point endPoint = e.GetPosition(gridTable);
Rect rect = new Rect(startPoint, endPoint);
// 遍历canvas内的所有控件,根据控件的位置和选择框位置判断是否选中
foreach (ShelfLabware element in Utilities.FindAllVisualChild(gridTable))//UIElement element in canvas.Children)
{
//VisualTreeHelper.get
if (rect.Contains(new Point(item.Margin.Left,item.Margin.Top)))
{
// 选中控件的逻辑
// 例如: element.IsSelected = true;
//if (item.ShelfName.Equals(shelfLabware.ShelfName))
//{
item.IsSelected = true;
//}
}
}
}
}
#endregion
#region 鼠标从台面模板内移出事件
private void gridTable_MouseLeave(object sender, MouseEventArgs e)
{
if (toolTip != null)
{
gridTable.Children.Remove(toolTip);
toolTip = null;
}
}
#endregion
#region 台面模板内板位右键移除选中的板位
private void tableLatticeMenuDelete_Click(object sender, RoutedEventArgs e)
{
MenuItem mi = e.Source as MenuItem;
ContextMenu menu = mi.Parent as ContextMenu;
ShelfLabware selectedShelfLabware = menu.PlacementTarget as ShelfLabware;
if (selectedShelfLabware == null )
return;
if (selectedShelfLabware != null)
{
int deletedIndex= Convert.ToInt32(selectedShelfLabware.ShelfName.Substring(1, selectedShelfLabware.ShelfName.Length - 1));
gridTable.Children.Remove(selectedShelfLabware);
//latticesList.Remove(latticesList.FirstOrDefault(x => x.lattice_num.Equals(selectedShelfLabware.ShelfName)));//集合中移除
if(!string.IsNullOrEmpty(selectedShelfLabware.ShelfDBId))
{
Lattice lattice = LatticeDB.GetLatticeDataByIdFromdb(selectedShelfLabware.ShelfDBId);
lattice.lattice_state = 0;
TabletopRelLatticeDB.DeleteLatticeFromdb(lattice);
}
else
{
}
//TabletopRelLatticeDB.DeleteLatticeFromdb(LatticeDB.GetLatticeDataByIdFromdb(selectedShelfLabware.ShelfName,Shared.DeviceArmList.FirstOrDefault(x=>x.arm_type.Equals(1)).device_arm_id,Shared.SoftwareInformation.software_device_number));
//所有label向前移动一位
List shelfLabwares = Utilities.FindAllVisualChild(gridTable);
foreach(var item in shelfLabwares)
{
int index = Convert.ToInt32(item.ShelfName.Substring(1, item.ShelfName.Length - 1));
if(deletedIndex
/// 保存当前选中的台面模板
///
public void SaveTabletopTemplate()
{
if (lbxTabletopList.SelectedIndex < 0)
{
ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "保存", Environment.NewLine, "请选中一个台面模板再保存!"), InfoType.Warning);
return;
}
TabletopTemplate tabletopTemplate = lbxTabletopList.SelectedItem as TabletopTemplate;
if (tabletopTemplate != null)
{
List lattices = new List();
string operateContent = string.Empty;
int sum = 0;
//循环构建板位对象,添加到集合中
foreach (ShelfLabware item in Utilities.FindAllVisualChild(gridTable))
{
Lattice lattice = new Lattice();
lattice.software_information_id = Shared.SoftwareInformation.software_information_id;
lattice.lattice_num = item.ShelfName;
operateContent += "板位:" + item.ShelfName;
lattice.lattice_X = 0d;
lattice.lattice_Y = 0d;
lattice.lattice_Z = 0d;
lattice.lattice_length = item.ShelfLength*2;
lattice.lattice_width= item.ShelfWidth*2;
lattice.lattice_lp_X = 0d;
lattice.lattice_lp_Y = 0d;
lattice.lattice_lp_Z = 0d;
lattice.gripper_rotational = 0d;
lattice.gripper_over_rotational = 0d;
lattice.device_arm_id = Shared.DeviceArmList.FirstOrDefault(x => x.arm_type.Equals(1)).device_arm_id;
lattice.is_trash = 0;
lattice.lattice_ch = 0;
lattice.lattice_pixel_x = item.Margin.Left;
lattice.lattice_pixel_y = item.Margin.Top;
lattice.labware_id = (item.DataContext as Labware).labware_id;
lattice.lattice_state = 1;
lattice.is_trash = item.ShelfTrash ? 1 : 0;
lattices.Add(lattice);
if(!string.IsNullOrEmpty(item.ShelfDBId))
{
lattice.lattice_id= item.ShelfDBId;
int ret = TabletopRelLatticeDB.UpdateLatticeFromdb(lattice); //更新lattice
TabletopRelLattice tabletopRelLattice = new TabletopRelLattice()
{
lattice_id = Convert.ToInt32(lattice.lattice_id),
tabletopid = tabletopTemplate.tabletopid,
lattice_pixel_x = lattice.lattice_pixel_x,
lattice_pixel_y = lattice.lattice_pixel_y,
labware_id = lattice.labware_id,
isbaselattice = 1
};
int ret1 = TabletopRelLatticeDB.UpdateTabletopRelLatticeFromdb(tabletopRelLattice);//更新关系坐标
if (ret>0&&ret1>0)
{
sum++;
}
}
else
{
int index = TabletopRelLatticeDB.GetMaxLattceId();//maxDBid
int ret = TabletopRelLatticeDB.AddLatticeIntodb(lattice, index + 1, tabletopTemplate.tabletopid);
if (ret > 0)
{
sum++;
item.ShelfDBId = (index + 1).ToString();
}
}
}
//板位添加进入数据库
//int result=TabletopRelLatticeDB.AddLatticeIntodb(lattices, index+1, tabletopTemplate.tabletopid);
if(sum == Utilities.FindAllVisualChild(gridTable).Count)//成功
{
PlsSetProperty plsSetProperty = new PlsSetProperty($"台面模板:{tabletopTemplate.tabletopname} 保存成功", false);
plsSetProperty.closeEvent += PlsSetProperty_closeEvent;
System.Windows.Window wnd = Application.Current.MainWindow;
Grid parent = Utilities.FindVisualChild(wnd);
parent.Children.Add(plsSetProperty);
OperateAuditLogDB.AddOperateAuditLogIntodb(operateAuditLogBll.GenerateOperateAuditLog("修改", operateContent, Shared.User.username, "台面模板", "", "", "台面模板[id:" + tabletopTemplate.tabletopid + "]", "成功"));
}
else
{
PlsSetProperty plsSetProperty = new PlsSetProperty($"台面模板:{tabletopTemplate.tabletopname} 保存失败", false);
plsSetProperty.closeEvent += PlsSetProperty_closeEvent;
System.Windows.Window wnd = Application.Current.MainWindow;
Grid parent = Utilities.FindVisualChild(wnd);
parent.Children.Add(plsSetProperty);
OperateAuditLogDB.AddOperateAuditLogIntodb(operateAuditLogBll.GenerateOperateAuditLog("修改", operateContent, Shared.User.username, "台面模板", "", "", "台面模板[id:" + tabletopTemplate.tabletopid + "]", "失败"));
}
}
}
#endregion
#region 设置为垃圾桶菜单
private void tableMenuSetTrash_Click(object sender, RoutedEventArgs e)
{
MenuItem mi = e.Source as MenuItem;
ContextMenu menu = mi.Parent as ContextMenu;
ShelfLabware selectedShelfLabware = menu.PlacementTarget as ShelfLabware;
if (selectedShelfLabware == null)
return;
if (selectedShelfLabware != null)
{
//设置垃圾桶
if (string.IsNullOrEmpty(selectedShelfLabware.ShelfDBId))
{
selectedShelfLabware.ShelfTrash = true;
}
else
{
selectedShelfLabware.ShelfTrash = true;
Lattice lattice = LatticeDB.GetLatticeDataByIdFromdb(selectedShelfLabware.ShelfDBId);
lattice.is_trash = 1;
int ret=LatticeDB.UpdateLatticeIntodb(lattice);
}
}
}
#endregion
#region 取消垃圾桶
private void tableMenuSetUnTrash_Click(object sender, RoutedEventArgs e)
{
MenuItem mi = e.Source as MenuItem;
ContextMenu menu = mi.Parent as ContextMenu;
ShelfLabware selectedShelfLabware = menu.PlacementTarget as ShelfLabware;
if (selectedShelfLabware == null)
return;
if (selectedShelfLabware != null)
{
//取消垃圾桶
if (string.IsNullOrEmpty(selectedShelfLabware.ShelfDBId))
{
selectedShelfLabware.ShelfTrash = false;
}
else
{
selectedShelfLabware.ShelfTrash = false;
Lattice lattice = LatticeDB.GetLatticeDataByIdFromdb(selectedShelfLabware.ShelfDBId);
lattice.is_trash = 0;
int ret = LatticeDB.UpdateLatticeIntodb(lattice);
}
}
}
#endregion
#region 台面模板右键设置应用到此设备菜单事件
private void tableMenuSetSystemAppTemplate_Click(object sender, RoutedEventArgs e)
{
MenuItem mi = e.Source as MenuItem;
ContextMenu menu = mi.Parent as ContextMenu;
var selectedTabletopTemplateListItem = menu.PlacementTarget as ListBox;
PlsToolTipWin plsToolTipWin = null;
plsToolTipWin = new PlsToolTipWin(Properties.SettingsTextResource.strTabletopManageSetApplicationTipInfo);
plsToolTipWin.ShowDialog();
// 确认卸载
if (plsToolTipWin.DialogResult == true)
{
TabletopTemplate selectedTabletopTemplate = selectedTabletopTemplateListItem.SelectedItem as TabletopTemplate;
if (selectedTabletopTemplate == null)
return;
if (selectedTabletopTemplate != null)
{
ObservableCollection tabletopTemplates = TabletopTemplateDB.GetTabletopTemplateCollectionFromdb();
int sum = 0;
//设置所有模板
foreach (TabletopTemplate t in tabletopTemplates)
{
if (t.tabletopid.Equals(selectedTabletopTemplate.tabletopid))
{
t.usestate = 1;
}
else
{
t.usestate = 0;
}
int ret = TabletopTemplateDB.UpdateTabletopTemplateIntodb(t);
if (ret > 0)
{
sum++;
}
}
if (sum == tabletopTemplates.Count)
{
ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "设置", Environment.NewLine, "设置应用到此设备成功!"), InfoType.Warning);
}
else
{
ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "设置", Environment.NewLine, "设置应用到此设备失败!"), InfoType.Warning);
}
BindingTabletopListOnListbox();
//刷新系统台面,刷新前
if(mainWindow!=null)
{
mainWindow.testDesign = null;
if(RunWnd.hadRun)
{
mainWindow.runWnd= null;
}
}
}
}
}
#endregion
#region 台面模板列表右键删除菜单事件
private void tableMenuDeleteTabletopTemplate_Click(object sender, RoutedEventArgs e)
{
MenuItem mi = e.Source as MenuItem;
ContextMenu menu = mi.Parent as ContextMenu;
var selectedTabletopTemplateListItem = menu.PlacementTarget as ListBox;
TabletopTemplate selectedTabletopTemplate = selectedTabletopTemplateListItem.SelectedItem as TabletopTemplate;
if (selectedTabletopTemplate == null)
return;
if (selectedTabletopTemplate != null)
{
selectedTabletopTemplate.tabletopstate = 0;
int ret = TabletopTemplateDB.UpdateTabletopTemplateIntodb(selectedTabletopTemplate);
if (ret >0)
{
ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "删除", Environment.NewLine, "删除台面模板成功!"), InfoType.Warning);
}
else
{
ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "删除", Environment.NewLine, "删除台面模板失败!"), InfoType.Warning);
}
//刷新list
gridTable.Children.Clear();
BindingTabletopListOnListbox();
}
}
#endregion
#region 台面模板列表右键,使用出厂台面
private void tableMenuSetSystemDefaultAppTemplate_Click(object sender, RoutedEventArgs e)
{
PlsToolTipWin plsToolTipWin = null;
plsToolTipWin = new PlsToolTipWin(Properties.SettingsTextResource.strTabletopManageSetApplicationTipInfo);
plsToolTipWin.ShowDialog();
// 确认卸载
if (plsToolTipWin.DialogResult == true)
{
ObservableCollection tabletopTemplates = TabletopTemplateDB.GetTabletopTemplateCollectionFromdb();
int sum = 0;
//设置所有模板
foreach (TabletopTemplate t in tabletopTemplates)
{
t.usestate = 0;
int ret = TabletopTemplateDB.UpdateTabletopTemplateIntodb(t);
if (ret > 0)
{
sum++;
}
}
if (sum == tabletopTemplates.Count)
{
ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "设置", Environment.NewLine, "设置出厂台面成功!"), InfoType.Warning);
}
else
{
if (tabletopTemplates.Count == 0)
{
ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "设置", Environment.NewLine, "设置出厂台面成功!"), InfoType.Warning);
}
else
{
ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "设置", Environment.NewLine, "设置出厂台面失败!"), InfoType.Warning);
}
}
BindingTabletopListOnListbox();
//刷新系统台面,刷新前
if (mainWindow != null)
{
mainWindow.testDesign = null;
if (RunWnd.hadRun)
{
mainWindow.runWnd = null;
}
}
}
}
#endregion
#region 多选板位 可顶部对齐/左对齐
public void AssignmentShelfLabware()
{
//if(Keyboard.)
}
#endregion
#region 开启选中范围检查处理
private System.Windows.Point startPoint;
private Rectangle selectionRectangle;
private void gridTable_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
startPoint = e.GetPosition(gridTable);
Console.WriteLine("startPoint x:{0},y:{1}",startPoint.X,startPoint.Y);
//循环清除所有选中状态
foreach (ShelfLabware item in Utilities.FindAllVisualChild(gridTable))
{
item.IsSelected = false;
}
//selectionRectangle = new Rectangle();
//gridTable.Children.Add(selectionRectangle);
//Canvas.SetLeft(selectionRectangle, startPoint.X);
//Canvas.SetTop(selectionRectangle, startPoint.Y);
//selectionRectangle.Fill = Brushes.Transparent;
//selectionRectangle.Stroke = Brushes.Blue;
//selectionRectangle.StrokeDashArray = new DoubleCollection() { 2, 2 };1232
HitTestResult result = VisualTreeHelper.HitTest(gridTable, startPoint);
var parentVisual = VisualTreeHelper.GetParent(result.VisualHit);
if (parentVisual is ShelfLabware)
{
ShelfLabware shelfLabware = parentVisual as ShelfLabware;
//循环构建板位对象,添加到集合中
foreach (ShelfLabware item in Utilities.FindAllVisualChild(gridTable))
{
if (item.ShelfName.Equals(shelfLabware.ShelfName))
{
item.IsSelected = true;
}
}
}
}
catch(Exception ex)
{
LoggerHelper.ErrorLog("gridTable_MouseLeftButtonDown error", ex);
}
}
#endregion
#region 选中的板位,顶部对其
private void btnTop_Click(object sender, RoutedEventArgs e)
{
try
{
ShelfLabware minNumShelfLabware = new ShelfLabware();
List selectedShelfLabwares = GetAllSelectedShelfLabware(out minNumShelfLabware);
foreach (ShelfLabware shelf in selectedShelfLabwares)
{
shelf.Margin = new Thickness(shelf.Margin.Left, minNumShelfLabware.Margin.Top, 0, 0);
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("btnTop_Click error", ex);
}
}
#endregion
#region 选中的板位,左侧对其
private void btnLeft_Click(object sender, RoutedEventArgs e)
{
try
{
ShelfLabware minNumShelfLabware = new ShelfLabware();
List selectedShelfLabwares = GetAllSelectedShelfLabware(out minNumShelfLabware);
foreach (ShelfLabware shelf in selectedShelfLabwares)
{
shelf.Margin = new Thickness(minNumShelfLabware.Margin.Left, shelf.Margin.Top, 0, 0);
}
}
catch(Exception ex)
{
LoggerHelper.ErrorLog("btnLeft_Click error", ex);
}
}
#endregion
#region 获取所有当前选中的板位对象,按小编号在前排序
private List GetAllSelectedShelfLabware(out ShelfLabware minNumShelfLabware)
{
List shelfLabwaresResult = new List();
List shelfLabwares = new List();
minNumShelfLabware = null;
//循环获取所有选中状态的板位
foreach (ShelfLabware item in Utilities.FindAllVisualChild(gridTable))
{
if(item.IsSelected == true)
{
shelfLabwares.Add(item);
}
}
int currentTotal = shelfLabwares.Count;
for (int i=0;i< currentTotal; i++)
{
ShelfLabware shelfLabware = GetMinIdShelfLabware(shelfLabwares);
if(i==0)
{
minNumShelfLabware = shelfLabware;
}
shelfLabwaresResult.Add(shelfLabware);
shelfLabwares.Remove(shelfLabware);
}
return shelfLabwaresResult;
}
#endregion
#region 求出最小的板位对象
private ShelfLabware GetMinIdShelfLabware(List shelfLabwares)
{
ShelfLabware shelfLabware = null;
ShelfLabware firstShelfLabware = shelfLabwares[0];
shelfLabware= firstShelfLabware;
int num = Convert.ToInt32(firstShelfLabware.ShelfName.Substring(1));
for (int i = 1; i < shelfLabwares.Count; i++)
{
if(num> Convert.ToInt32(shelfLabwares[i].ShelfName.Substring(1)))
{
shelfLabware = shelfLabwares[i];
num = Convert.ToInt32(shelfLabwares[i].ShelfName.Substring(1));
}
}
return shelfLabware;
}
#endregion
}
}