using MetroGauges.Controls;
using MetroGauges.Controls.BlockShaps;
using MetroGauges.Controls.DrawTools;
using MetroGauges.Database;
using MetroGauges.Database.Enitys;
using MetroGauges.Database.Forms;
using MetroGauges.Model;
using MetroGauges.ViewModel;
using Microsoft.Win32;
using netDxf;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Globalization;
using System.IO;
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.Shapes;
namespace MetroGauges
{
///
/// wpfCadView.xaml 的交互逻辑
///
public partial class wpfCadView : Window
{
private PointCollection othCdPts;
private PointCollection pgCdPts;
private PointCollection revOthCdPts;
private PointCollection revPgCdPts;
private DgViewModel exPortdatas; // 界限数据
ObservableCollection blockGroups;
BlockOpr blockOpr = new BlockOpr();
TransformGroup viewTransformGroup = new TransformGroup();
ScaleTransform viewScaleTransform = new ScaleTransform();
public Window ParentWindow { get; set; }
#region "构造函数"
public wpfCadView()
{
InitializeComponent();
this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
}
///
/// 车辆界限
///
///
public wpfCadView(DgViewModel k)
{
InitializeComponent();
this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
this.Loaded += WpfCadView_Loaded;
exPortdatas = new DgViewModel() { DataType = DataTypes.KineModel, LayerName = k.LayerName, Color = k.Color };
foreach (var item in k.Items)
{
exPortdatas.Items.Add(item);
}
DrawKinePoint(exPortdatas);
DrawCurve(exPortdatas);
}
///
///
///
///
public wpfCadView(DgViewModel l)
{
InitializeComponent();
this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
this.Loaded += WpfCadView_Loaded;
exPortdatas = new DgViewModel() { DataType = DataTypes.LiEquiModel, LayerName = l.LayerName, Color = l.Color };
foreach (var item in l.Items)
{
exPortdatas.Items.Add(item);
}
DrawKinePoint(exPortdatas);
DrawCurve(exPortdatas);
}
public wpfCadView(DgViewModel c)
{
InitializeComponent();
this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
this.Loaded += WpfCadView_Loaded;
exPortdatas = new DgViewModel() { DataType = DataTypes.CurEquiModel, LayerName = c.LayerName, Color = c.Color };
foreach (var item in c.Items)
{
exPortdatas.Items.Add(item);
}
DrawKinePoint(exPortdatas);
DrawCurve(exPortdatas);
}
#endregion
private void WpfCadView_Loaded(object sender, RoutedEventArgs e)
{
this.designControl.LayoutRoot.Focus();
this.designControl.HighlighEvent += DesignControl_HighlighEvent;
this.designControl.MouseRightButtonDown += DesignControl_MouseRightButtonDown;
this.designControl.MouseMove += DesignControl_MouseMove;
this.Closed += WpfCadView_Closed;
this.smalldesign.ScaleVisble = false;
this.smalldesign.CanWheel = false;
this.smalldesign.BlockManage.IsView = true;
//Controls.GeoWidget w1 = new Controls.GeoWidget
//{
// Width = 500,
// Height = 550,
// BlockName = "块1"
//};
//this.designControl.canvasDesign.Children.Add(w1);
//Canvas.SetTop(w1, -100);
//Canvas.SetLeft(w1, 100);
//this.designControl.BlockManage.AddBlock("块1");
//this.designControl.BlockManage.AddBlock("三轨");
//this.designControl.BlockManage.AddBlock("消火栓");
//tvBlocks.ItemsSource = this.designControl.BlockManage.BlockNames();
this.PreviewKeyUp += WpfCadView_KeyDown;
BindBlockData();
}
private void WpfCadView_Closed(object sender, EventArgs e)
{
if (ParentWindow != null)
{
ParentWindow.Show();
}
}
private void BindBlockData()
{
try
{
blockGroups = BlockDAL.GetBlockGroups();
tvBlocks.ItemsSource = blockGroups;
}
catch (Exception ex)
{
LogManager.WriteLog("error", "BindBlockData:" + ex.Message);
//throw;
}
}
private void WpfCadView_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete || e.Key == Key.Decimal)
{
removBlock_Click(sender, e);
}
else
{
//this.designControl.Root.Focus();
if (Keyboard.IsKeyDown(Key.LeftCtrl))
{
if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)
{
if (this.designControl.BlockManage.SelectedBlock == null) return;
Point point = new Point();
Point point1 = new Point();
blockOpr.ScaleRale = this.designControl.ScaleRale;
blockOpr.GetBlockPoints(this.designControl.BlockManage.SelectedBlock.Block, out point, out point1);
this.designControl.BlockManage.DragPoint = point;
this.designControl.BlockManage.CopyBlock(this.designControl.BlockManage.SelectedBlock.BlockName);
switch (e.Key)
{
case Key.Left:
this.designControl.BlockManage.MoveBlock(this.designControl.BlockManage.SelectedBlock.BlockName, new Point() { X = point.X - 1, Y = point.Y });
break;
case Key.Right:
this.designControl.BlockManage.MoveBlock(this.designControl.BlockManage.SelectedBlock.BlockName, new Point() { X = point.X + 1, Y = point.Y });
break;
case Key.Up:
this.designControl.BlockManage.MoveBlock(this.designControl.BlockManage.SelectedBlock.BlockName, new Point() { X = point.X, Y = point.Y - 1 });
break;
case Key.Down:
this.designControl.BlockManage.MoveBlock(this.designControl.BlockManage.SelectedBlock.BlockName, new Point() { X = point.X, Y = point.Y + 1 });
break;
}
SetLocation(this.designControl.BlockManage.SelectedBlock, point);
if (this.designControl.BlockManage.SelectedBlock.BlockEnity != null)
{
//更新最后调整位置:矩形外框右下角位置
BlockDAL.UpdateBlockLocation(this.designControl.BlockManage.SelectedBlock.BlockEnity.Id, point.X, point.Y);
this.designControl.BlockManage.SelectedBlock.BlockEnity.Locationx = point.X;
this.designControl.BlockManage.SelectedBlock.BlockEnity.Locationy = point.Y;
}
}
}
}
}
private void DesignControl_HighlighEvent(object sender, HighlighEvent e)
{
if (sender is BlockManage)
{
if (e.Block.BlockEnity.Istemplate == 0)
{
SetLocation(e.Block, e.Location);
tvBlocks.Focusable = false;
this.designControl.txtXY.Focus();
}
}
}
private void SetLocation(BlockData blockData,Point location)
{
designControl.RemovePropertyValueChanged();
FieldInfo fieldInfox = blockData.BlockEnity.FieldData.FirstOrDefault(p => p.Id == 9999);
if (fieldInfox != null)
{
fieldInfox.Fieldvalue = (location.X * designControl.ScaleRale).ToString("0.00");
designControl.UpdatePropertyGriFiledValue("X坐标", fieldInfox.Fieldvalue);
}
FieldInfo fieldInfoy = blockData.BlockEnity.FieldData.FirstOrDefault(p => p.Id == 9998);
if (fieldInfoy != null)
{
fieldInfoy.Fieldvalue = (-location.Y * designControl.ScaleRale).ToString("0.00");
designControl.UpdatePropertyGriFiledValue("Y坐标", fieldInfoy.Fieldvalue);
}
designControl.AddPropertyValueChanged();
}
#region 数据绘制相关
///
/// 控制点表格
///
///
private void DrawKineTable(DgViewModel vm)
{
ObservableCollection dm = vm.Items;
if (dm.Count == 0) return;
Point startPoint = new Point() { X = 500, Y = -850 };
this.designControl.DrawText("车辆轮廓线坐标表(单位:mm)", new Point() { X = startPoint.X, Y = startPoint.Y - 20 });
double count = Math.Ceiling(dm.Count / 10.0); //dm.Count / 10;
int index = 0;
for (int k = 1; k <= count; k++)
{
int col = 10;
if (k * 10 > dm.Count)
{
col = dm.Count - ((k - 1) * 10);
}
OutTable outTable = new OutTable(this.designControl.canvasDesign) { Cols = col + 1 };
outTable.StartPoint = new Point() { X = startPoint.X, Y = startPoint.Y + (k - 1) * 120 };
for (int i = 0; i < 3; i++)
{
Row row = outTable.NewRow();
}
outTable.DrawShaps = this.designControl.DrawShaps;
outTable.Rows[0].Cells[0].Text = "控制点";
outTable.Rows[1].Cells[0].Text = "X";
outTable.Rows[2].Cells[0].Text = "Y";
for (int i = 1; i <= col; i++)
{
int cellindex = index * 10 + (i - 1);
outTable.Rows[0].Cells[i].Text = dm[cellindex].Name;
}
//X
for (int i = 1; i <= col; i++)
{
int cellindex = index * 10 + (i - 1);
outTable.Rows[1].Cells[i].Text = dm[cellindex].X.ToString();
}
//Y
for (int i = 1; i <= col; i++)
{
int cellindex = index * 10 + (i - 1);
outTable.Rows[2].Cells[i].Text = dm[cellindex].Y.ToString();
}
outTable.DrawTable();
index++;
}
}
///
/// 绘控制点
///
///
private void DrawKinePoint(DgViewModel vm)
{
double xyRate = this.designControl.ScaleRale;
ObservableCollection dm = vm.Items;
for (int i = 0; i < dm.Count; i++)
{
Ellipse dataEllipse = new Ellipse
{
Fill = new SolidColorBrush(Color.FromRgb(28, 120, 135)),
Width = 8,
Height = 8,
ToolTip = dm[i].Name + "(" + dm[i].X + "," + dm[i].Y + ")",
//ToolTip = String.Format("{0}{1}{2}{3}{4}{5}", dm[i].Name, "(", dm[i].X, ",", dm[i].Y, ")")
};
Canvas.SetLeft(dataEllipse, dm[i].X / xyRate - 4);//-4是为了补偿圆点的大小,到精确的位置
Canvas.SetTop(dataEllipse, -dm[i].Y / xyRate - 4);
this.designControl.canvasDesign.Children.Add(dataEllipse);
}
DrawKineTable(vm);
}
private void DrawPolyline(PointCollection ptcol, Brush brush)
{
Polyline pl = new Polyline
{
Stroke = brush,
StrokeThickness = 1,
Points = ptcol,
};
this.designControl.canvasDesign.Children.Add(pl);
}
///
/// 绘制轮阔曲线
///
private void DrawCurve(DgViewModel vm)
{
double xyRate = this.designControl.ScaleRale;
ObservableCollection dm = vm.Items;
othCdPts = new PointCollection();
pgCdPts = new PointCollection();
revOthCdPts = new PointCollection();
revPgCdPts = new PointCollection();
if (vm.DataType == DataTypes.LiEquiModel)
{
for (int i = 0; i < dm.Count; i++)
{
if (dm[i].Position2 == MetroGauges.General.PositionII.Pantograph)
{
pgCdPts.Add(new System.Windows.Point(dm[i].X / xyRate, -dm[i].Y / xyRate));
revPgCdPts.Add(new System.Windows.Point(-dm[i].X / xyRate, -dm[i].Y / xyRate));
}
else
{
othCdPts.Add(new System.Windows.Point(dm[i].X / xyRate, -dm[i].Y / xyRate));
revOthCdPts.Add(new System.Windows.Point(-dm[i].X / xyRate, -dm[i].Y / xyRate));
}
}
}
else
{
for (int i = 0; i < dm.Count; i++)
{
if (dm[i].Position == MetroGauges.General.PositionI.Pantograph)
{
pgCdPts.Add(new System.Windows.Point(dm[i].X / xyRate, -dm[i].Y / xyRate));
revPgCdPts.Add(new System.Windows.Point(-dm[i].X / xyRate, -dm[i].Y / xyRate));
}
else
{
othCdPts.Add(new System.Windows.Point(dm[i].X / xyRate, -dm[i].Y / xyRate));
revOthCdPts.Add(new System.Windows.Point(-dm[i].X / xyRate, -dm[i].Y / xyRate));
}
}
}
Brush color = new SolidColorBrush(Color.FromRgb(vm.Color.R, vm.Color.G, vm.Color.B));
DrawPolyline(pgCdPts, color);
DrawPolyline(othCdPts, color);
DrawPolyline(revPgCdPts, color);
DrawPolyline(revOthCdPts, color);
}
#endregion
#region 窗体操作
private void HeaderZone_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
DragMove();
}
private void WindowClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void WindowMinimize_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void WindowMaximize_Click(object sender, RoutedEventArgs e)
{
if(this.WindowState == WindowState.Maximized)
this.WindowState = WindowState.Normal;
else
this.WindowState = WindowState.Maximized;
}
#endregion
#region dxf 导出
///
/// 导出车辆界限
///
private void ExportKineData(string fileName)
{
ObservableCollection othCoordPoints = new ObservableCollection();
ObservableCollection pgCoordPoints = new ObservableCollection();
ObservableCollection dm = exPortdatas.Items;
for (int i = 0; i < dm.Count; i++)
{
if (dm[i].Position == General.PositionI.Pantograph)
{
pgCoordPoints.Add(dm[i]); //受电弓
}
else
{
othCoordPoints.Add(dm[i]);
}
}
if (pgCoordPoints.Count == 0 || othCoordPoints.Count == 0)
{
MessageBox.Show("数据不全", "错误");
return;
}
DxfDocument dxf = new DxfDocument();
Layer lyr = new Layer(exPortdatas.LayerName);
lyr.Color = exPortdatas.Color;
DrawDxfData(dxf, lyr, pgCoordPoints);
DrawDxfData(dxf, lyr, othCoordPoints);
designControl.BlockManage.DrawDxfBlocks(dxf,lyr);
designControl.DrawDxfShap(dxf, lyr);
dxf.DrawingVariables.AcadVer = netDxf.Header.DxfVersion.AutoCad2010;
dxf.Save(fileName);
//dxf.DrawingVariables.
MessageBox.Show("导出成功", "错误");
}
private void DrawDxfData(DxfDocument dxf , Layer layer, ObservableCollection models)
{
double[] PtSt = new double[2];
double[] PtEn = new double[2];
string Corder = models[0].Name;
PtSt[0] = models[0].X;
PtSt[1] = models[0].Y;
for (int i = 1; i < models.Count; i++)
{
PtEn[0] = models[i].X;
PtEn[1] = models[i].Y;
//右边的点才画出点号和坐标
//PtEn[0] = models[i].X;
//cadHelper.DrawText(Corder + "(" + PtSt[0] + "," + PtSt[1] + ")", PtSt);
string strCoder = Corder + "(" + PtSt[0] + "," + PtSt[1] + ")";
netDxf.Entities.Line line = new netDxf.Entities.Line(new Vector2(PtSt[0], PtSt[1]), new Vector2(PtEn[0], PtEn[1]));
line.Layer = layer;
dxf.AddEntity(line);
TextStyle style = new TextStyle("myStye", "宋体");
netDxf.Entities.Text text = new netDxf.Entities.Text(strCoder, new Vector2(PtSt[0], PtSt[1]), 12.0f, style);
text.Layer = layer;
text.Alignment = netDxf.Entities.TextAlignment.BottomLeft;
dxf.AddEntity(text);
// 画左边的对称线
netDxf.Entities.Line line2 = new netDxf.Entities.Line(new Vector2(-PtSt[0], PtSt[1]), new Vector2(-PtEn[0], PtEn[1]));
line2.Layer = layer;
dxf.AddEntity(line2);
PtSt[0] = PtEn[0];
PtSt[1] = PtEn[1];
Corder = models[i].Name;
if (models.Count - 1 == i) //最后一个点 右边
{
netDxf.Entities.Text text1 = new netDxf.Entities.Text(strCoder, new Vector2(PtEn[0], PtEn[1]), 12.0f, style);
text1.Layer = layer;
text1.Alignment = netDxf.Entities.TextAlignment.BottomLeft;
dxf.AddEntity(text);
//cadHelper.DrawText(Corder + "(" + PtSt[0] + "," + PtSt[1] + ")", PtEn);
}
}
}
#endregion
private void ScalVisible_Click(object sender, RoutedEventArgs e)
{
designControl.ScaleVisble = this.scalVisbleChk.IsChecked.Value;
}
private void txtScale_PreviewKeyDown(object sender, KeyEventArgs e)
{
Utily.isInputNumber(e);
}
private void txtScale_LostFocus(object sender, RoutedEventArgs e)
{
}
///
/// 图形偏移
///
///
///
private void txtXY_TextChanged(object sender, TextChangedEventArgs e)
{
if (designControl == null) return;
if (!string.IsNullOrWhiteSpace((sender as TextBox).Text) && (sender as TextBox).Text!="-")
{
string name = (sender as TextBox).Name;
switch (name)
{
case "txtleftmove":
//designControl.MoveSelectedShapbyX(-double.Parse(txtleftmove.Text));
break;
case "txtrightmove":
//designControl.MoveSelectedShapbyX(double.Parse(txtrightmove.Text));
break;
case "txtupmove":
//designControl.MoveSelectedShapbyY(double.Parse(txtupmove.Text));
break;
case "txtdownmove":
//designControl.MoveSelectedShapbyY(-double.Parse(txtdownmove.Text));
break;
}
//更新位置信息
designControl.BlockManage.SendHighlighEvent(true);
}
}
///
/// 删除选中的块或其它相关图形
///
///
///
private void removBlock_Click(object sender, RoutedEventArgs e)
{
if (designControl.BlockManage.SelectedBlock != null)
{
foreach (var item in designControl.BlockManage.SelectedBlock.Block)
{
designControl.canvasDesign.Children.Remove(item);
}
designControl.BlockManage.Blocks.Remove(designControl.BlockManage.SelectedBlock.BlockName);
designControl.BlockManage.SelectedBlock = null;
}
else if(designControl.CrrentDrawTool != null && designControl.CrrentDrawTool.SelectedShape !=null)
{
designControl.DrawShaps.Remove(designControl.CrrentDrawTool.SelectedShape);
designControl.canvasDesign.Children.Remove(designControl.CrrentDrawTool.SelectedShape);
designControl.CrrentDrawTool.SelectedShape = null;
}
}
#region 工具条按钮
///
/// 导出Dxf 文档
///
///
///
private void btExport_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Cad dxf文件(*.dxf)|*.dxf";
//默认保存格式
sfd.DefaultExt = "dxf";
//自动添加扩展名
sfd.AddExtension = true;
if (sfd.ShowDialog() == true)
{
ExportKineData(sfd.FileName);
}
}
///
/// 反转
///
///
///
private void ReverseBlock_Click(object sender, RoutedEventArgs e)
{
designControl.BlockManage.ReverseBlock();
}
private void btPolyline_Click(object sender, RoutedEventArgs e)
{
this.designControl.CreateTool(DrawActionType.Polyline);
}
private void btText_Click(object sender, RoutedEventArgs e)
{
this.designControl.CreateTool(DrawActionType.Text);
}
private void btRect_Click(object sender, RoutedEventArgs e)
{
this.designControl.CreateTool(DrawActionType.Rectangle);
}
private void btCircle_Click(object sender, RoutedEventArgs e)
{
this.designControl.CreateTool(DrawActionType.Circle);
}
private void btnormal_Click(object sender, RoutedEventArgs e)
{
this.designControl.Fit();
}
///
/// 缩放块
///
///
///
private void txtScale_TextChanged(object sender, TextChangedEventArgs e)
{
if (designControl == null) return;
double vlaue;
try
{
vlaue = double.Parse(txtScale.Text);
}
catch
{
return;
}
designControl.BlockManage.ScaleBlock(vlaue);
}
#endregion
#region 分组块操作
private bool isAddBlock = false;
private BlockData blockData;
private void DesignControl_MouseMove(object sender, MouseEventArgs e)
{
if (isAddBlock && blockData != null)
{
if(!blockData.Visblie)
{
blockData.Visblie = true;
}
System.Windows.Point currentPoint = e.GetPosition(this.designControl.canvasDesign);
this.designControl.BlockManage.MoveBlock(blockData.BlockName, currentPoint);
}
}
private void DesignControl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
//isAddBlock = false;
//blockData = null;
}
//添加块
private void addBlock_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
string name = (sender as TextBlock).Tag.ToString().Replace(".dxf", "");
//isAddBlock = true;
Shape drapShape = this.designControl.BlockManage.AddBlock(name);
blockData = this.designControl.BlockManage.Blocks[drapShape.Name];
TreeViewItem treeViewItem = Utily.GetNearestContainer(sender as TextBlock);
blockData.BlockEnity = treeViewItem.DataContext as BlockInfo;
//添加的是块信息
if (blockData.BlockEnity.Istemplate == 0)
{
if (drapShape != null)
{
Point currentPoint = new Point();
Point point = new Point();
if (blockData.BlockEnity.Locationx == 0 && blockData.BlockEnity.Locationy == 0)
{
currentPoint = new Point() { X = 100, Y = -100 };
point = new Point();
if (drapShape is Line)
{
point.X = (drapShape as Line).X1;
point.Y = (drapShape as Line).Y1;
}
}
else //默认位置
{
Point point1 = new Point();
blockOpr.ScaleRale = this.designControl.ScaleRale;
blockOpr.GetBlockPoints(blockData.Block, out point, out point1);
//用户调整的最后位置
currentPoint = new Point() { X = blockData.BlockEnity.Locationx, Y = blockData.BlockEnity.Locationy };
}
this.designControl.BlockManage.DragPoint = point;
this.designControl.BlockManage.CopyBlock(drapShape.Name);
this.designControl.BlockManage.MoveBlock(drapShape.Name, currentPoint);
}
}
}
catch (Exception ex)
{
LogManager.WriteLog("error",ex.Message);
MessageBox.Show("程序异常");
}
}
///
/// 添加分组
///
///
///
private void addGroup_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
frmAddGroup frm = new frmAddGroup();
frm.WindowStartupLocation = WindowStartupLocation.CenterScreen;
frm.ShowDialog();
bool reulst = frm.DialogResult.Value;
try
{
if (reulst)
{
BlockDAL.CreateGroup(frm.txtGroup.Text);
this.BindBlockData();
}
}
catch (Exception ex)
{
LogManager.WriteLog("error", "addGroup:" + ex.Message);
}
}
///
/// 导入块
///
///
///
private void import_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;//该值确定是否可以选择多个文件
dialog.Title = "请选要导入的dxf块文件";
dialog.Filter = "块文件(*.dxf)|*.dxf";
if (dialog.ShowDialog().Value)
{
string[] file = dialog.FileNames;
int groupid =int.Parse((sender as TextBlock).Tag.ToString());
string m_BlockPath = AppDomain.CurrentDomain.BaseDirectory + "Blocks\\";
if (!Directory.Exists(m_BlockPath))
{
Directory.CreateDirectory(m_BlockPath);
}
foreach (var item in file)
{
string name = System.IO.Path.GetFileNameWithoutExtension(item);
int id = BlockDAL.CheckBlock(name);
if (id >0)
{
if (MessageBox.Show($"已存在块:{name},确定覆盖吗?", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
BlockDAL.DeleteBlock(id);
}
else
{
continue;
}
}
BlockInfo blockInfo = new BlockInfo();
blockInfo.Filepath = System.IO.Path.GetFileName(item);
blockInfo.Name = name;
blockInfo.Groupid = groupid;
if (groupid == 4)
{
blockInfo.Istemplate = 1;
}
bool result= BlockDAL.CreateBlock(blockInfo);
if (result)
{
System.IO.File.Copy(item, m_BlockPath + blockInfo.Filepath,true);
}
}
this.BindBlockData();
}
}
///
/// 删除组
///
///
///
private void delGroup_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (MessageBox.Show("确定要删除该分类分类下的块数据吗?", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
int id =int.Parse((sender as TextBlock).Tag.ToString());
BlockDAL.delGroup(id);
this.BindBlockData();
}
}
///
/// 更新块名称
///
///
///
private void TreeNodeEdit_NodeUpdated(object sender, NodeCheckInputArgs e)
{
try
{
BlockInfo blockInfo = (sender as TreeNodeEdit).DataContext as BlockInfo;
if (blockInfo != null)
{
BlockDAL.UpdateBlockName(blockInfo.Id, blockInfo.Name);
blockInfo.ToolTip = blockInfo.Name;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
{
BlockInfo blockInfo = tvBlocks.SelectedItem as BlockInfo;
if (blockInfo !=null)
{
//移除之前block
if (this.smalldesign.BlockManage.Blocks.Count > 0)
{
this.smalldesign.BlockManage.RemoveBlock(this.smalldesign.BlockManage.Blocks.FirstOrDefault().Value);
}
if (blockInfo.Istemplate == 0) //选中构件
{
viewTransformGroup.Children.Clear();
TranslateTransform moveCanvas = new TranslateTransform() { X = 100, Y = 200 }; //右移
viewTransformGroup.Children.Add(moveCanvas);
this.smalldesign.canvasDesign.RenderTransform = viewTransformGroup;
Shape drapShape = this.smalldesign.BlockManage.AddBlock(blockInfo.Filepath);
BlockData block = this.smalldesign.BlockManage.Blocks[drapShape.Name];
block.BlockEnity = blockInfo;
Point point = new Point();
Point point1 = new Point();
blockOpr.ScaleRale = this.smalldesign.ScaleRale;
blockOpr.GetBlockPoints(block.Block, out point, out point1);
this.smalldesign.BlockManage.DragPoint = point;
this.smalldesign.BlockManage.CopyBlock(drapShape.Name);
this.smalldesign.BlockManage.MoveBlock(drapShape.Name, new Point() { X = 0, Y = 0 });
}
else //选中模板 预览
{
//缩放预览
viewTransformGroup.Children.Clear();
viewScaleTransform.ScaleX = 0.4;
viewScaleTransform.ScaleY = 0.4;
viewTransformGroup.Children.Add(viewScaleTransform);
TranslateTransform moveCanvas = new TranslateTransform() { X = 115, Y = 200 }; //右移
viewTransformGroup.Children.Add(moveCanvas);
this.smalldesign.canvasDesign.RenderTransform = viewTransformGroup;
this.smalldesign.BlockManage.AddBlock(blockInfo.Filepath);
}
designControl.ShowEditPanel(blockInfo);
}
}
private void TreeViewItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
TreeViewItem item = Utily.VisualUpwardSearch(e.OriginalSource as DependencyObject) as TreeViewItem;
tvBlocks.ContextMenu = null;
if (item != null)
{
item.IsSelected = true;
item.ContextMenu = null;
BlockInfo entity = tvBlocks.SelectedItem as BlockInfo;
if (entity != null)
item.ContextMenu = GetDBRightContextMenu(entity, item);
}
e.Handled = true;
}
// 获得右键菜单
public ContextMenu GetDBRightContextMenu(BlockInfo entity, TreeViewItem item)
{
ContextMenu menu = new ContextMenu();
MenuItem menuItem = new MenuItem() { Header = "删除", Tag = entity };
menuItem.Icon = new Image { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("./Resources/Icon/DeleteRed16.png", UriKind.Relative)) };
menuItem.Click += new RoutedEventHandler(delMenu_Click);
MenuItem ProMuItem = new MenuItem() { Header = "设置属性", Tag = entity };
ProMuItem.Icon = new Image { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("./Resources/Icon/table.png", UriKind.Relative)) };
ProMuItem.Click += ProMuItem_Click;
MenuItem reNameItem = new MenuItem() { Header = "重命名", Tag = item, DataContext= entity };
//ProMuItem.Icon = new Image { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("./Resources/Icon/table.png", UriKind.Relative)) };
reNameItem.Click += ReNameItem_Click; ;
menu.Items.Add(ProMuItem);
menu.Items.Add(menuItem);
menu.Items.Add(reNameItem);
return menu;
}
private void ReNameItem_Click(object sender, RoutedEventArgs e)
{
TreeNodeEdit edit = Utily.SearchVisualTree((sender as MenuItem).Tag as DependencyObject) as TreeNodeEdit;
if (edit != null)
edit.IsInEditMode = true;
}
private void ProMuItem_Click(object sender, RoutedEventArgs e)
{
BlockInfo entity = (sender as MenuItem).Tag as BlockInfo;
ProSetting proSetting = new ProSetting();
proSetting.BindData(entity);
proSetting.ShowDialog();
}
private void delMenu_Click(object sender, RoutedEventArgs e)
{
try
{
BlockInfo entity = (sender as MenuItem).Tag as BlockInfo;
BlockDAL.DeleteBlock(entity.Id);
BlockGroup blockGroup= blockGroups.Where(p => p.Id == entity.Groupid).FirstOrDefault();
blockGroup.Blocks.Remove(entity);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
///
/// 添加矩形隧道截面
///
///
///
private void btsuidaoRect_Click(object sender, RoutedEventArgs e)
{
string[] xy = ConfigurationManager.AppSettings["recLocation"].Split(',');
Shape drapShape = this.designControl.BlockManage.AddBlock("矩形轨道");
blockData = this.designControl.BlockManage.Blocks[drapShape.Name];
Point point = new Point();
Point point1 = new Point();
blockOpr.GetBlockPoints(blockData.Block, out point, out point1);
this.designControl.BlockManage.DragPoint = point;
this.designControl.BlockManage.CopyBlock(drapShape.Name);
this.designControl.BlockManage.MoveBlock(drapShape.Name, new Point() { X = double.Parse(xy[0]), Y = double.Parse(xy[1]) });
}
///
/// 添加圆形隧道截面
///
///
///
private void btsuidaoCircle_Click(object sender, RoutedEventArgs e)
{
string[] xy = ConfigurationManager.AppSettings["cirLocation"].Split(',');
Shape drapShape = this.designControl.BlockManage.AddBlock("圆形隧道");
blockData = this.designControl.BlockManage.Blocks[drapShape.Name];
Point point = new Point();
Point point1 = new Point();
blockOpr.ScaleRale = this.designControl.ScaleRale;
blockOpr.GetBlockPoints(blockData.Block, out point, out point1);
this.designControl.BlockManage.DragPoint = point;
this.designControl.BlockManage.CopyBlock(drapShape.Name);
this.designControl.BlockManage.MoveBlock(drapShape.Name, new Point() { X = double.Parse(xy[0]), Y = double.Parse(xy[1]) });
}
///
/// 添加马啼形隧道截面
///
///
///
private void btsuidaomadixin_Click(object sender, RoutedEventArgs e)
{
string[] xy = ConfigurationManager.AppSettings["madiLocation"].Split(',');
Shape drapShape = this.designControl.BlockManage.AddBlock("马蹄形隧道");
blockData = this.designControl.BlockManage.Blocks[drapShape.Name];
Point point = new Point();
Point point1 = new Point();
blockOpr.ScaleRale = this.designControl.ScaleRale;
blockOpr.GetBlockPoints(blockData.Block, out point, out point1);
this.designControl.BlockManage.DragPoint = point;
this.designControl.BlockManage.CopyBlock(drapShape.Name);
this.designControl.BlockManage.MoveBlock(drapShape.Name, new Point() { X= double.Parse(xy[0]), Y = double.Parse(xy[1]) });
}
#endregion
}
public class GroupDelConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value.ToString() == "隧道模板")
{
return "Collapsed";
}
else
{
return "Visible";
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return "";
}
}
}