Files
MetroGauges-Old/MetroGauges/Controls/DesignControl.xaml.cs
2026-02-23 17:02:55 +08:00

792 lines
27 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.Collections.ObjectModel;
using System.Threading;
using MetroGauges.Controls.DrawTools;
using MetroGauges.Controls.BlockShaps;
using Xceed.Wpf.Toolkit.PropertyGrid;
using MetroGauges.Database.Enitys;
using MetroGauges.Database;
namespace MetroGauges.Controls
{
/// <summary>
/// DesignControl.xaml 的交互逻辑
/// </summary>
public partial class DesignControl : MapBase
{
private AdornerLayer m_DesignLayer;
private PropertyGrid m_MeataPropertyGrid = new PropertyGrid() { ShowSearchBox = false, NameColumnWidth = 100, ShowSortOptions = false, ShowAdvancedOptions = false, IsCategorized = false };
private TransformGroup inverseRander = new TransformGroup();
private int axisLength = 800; //坐标轴长度
private double maxCoordinate = 7500; //最大坐标值
private double scalNumber; // 计算每个刻度数值
private List<UIElement> xyScales = new List<UIElement>();
//手动画的图形
private List<FrameworkElement> drShaps = new List<FrameworkElement>();
private BlockManage m_BlockManage;
public event EventHandler<HighlighEvent> HighlighEvent;
private BlockInfo m_blockInfo;
#region
public bool IsView { get; set; }
public List<FrameworkElement> DrawShaps
{
get
{
return drShaps;
}
}
public AdornerLayer DesignLayer { get { return m_DesignLayer; } }
/// <summary> </summary>
public ScaleTransform MapRootScale { get { return _MapRootScale; } }
/// <summary> </summary>
public TranslateTransform MapRootTranslate { get { return _MapRootTranslate; } }
/// <summary> </summary>
public double MapScale { get; set; }
public double ScaleRale {
get {
return scalNumber / 10;
}
}
public bool ScaleVisble {
set
{
foreach (var item in this.xyScales)
{
if (value)
{
this.x_axis.Visibility = Visibility.Visible;
this.x_axisArrow.Visibility = Visibility.Visible;
this.y_axis.Visibility = Visibility.Visible;
this.y_axisArrow.Visibility = Visibility.Visible;
item.Visibility = Visibility.Visible;
this.x_label.Visibility = Visibility.Visible;
this.y_label.Visibility = Visibility.Visible;
this.o_label.Visibility = Visibility.Visible;
}
else
{
this.x_axis.Visibility = Visibility.Collapsed;
this.x_axisArrow.Visibility = Visibility.Collapsed;
this.y_axis.Visibility = Visibility.Collapsed;
this.y_axisArrow.Visibility = Visibility.Collapsed;
item.Visibility = Visibility.Collapsed;
this.x_label.Visibility = Visibility.Collapsed;
this.y_label.Visibility = Visibility.Collapsed;
this.o_label.Visibility = Visibility.Collapsed;
}
}
}
get
{
return true;
}
}
public BlockManage BlockManage {
get {
return m_BlockManage;
}
}
#endregion
public DesignControl()
{
InitializeComponent();
m_MeataPropertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(m_MeataPropertyGrid_PropertyValueChanged);
this.Loaded += new RoutedEventHandler(DesignControl_Loaded);
this.m_Root = Root;
this.m_ChildContainer = ChildContainer;
this.m_RootKeyHook = RootKeyHook;
inverseRander.Children.Add(new ScaleTransform() { ScaleY = -1 });
//this.drawLines();
//画坐标
this.DrawAxis();
m_BlockManage = new BlockManage(canvasDesign, this.ScaleRale);
m_BlockManage.HighlighEvent += Shap_HighlighEvent;
}
/// <summary>
/// 处理选中的块和手动的图形
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Shap_HighlighEvent(object sender, HighlighEvent e)
{
if (sender is DrawToolBase)
{
if (m_BlockManage.SelectedBlock != null)
{
foreach (var item in m_BlockManage.SelectedBlock.Block)
{
if (item is Shape)
{
(item as Shape).Stroke = Brushes.White;
}
}
m_BlockManage.SelectedBlock = null;
}
}
else
{
if (this.CrrentDrawTool !=null && this.CrrentDrawTool.SelectedShape != null)
{
if (this.CrrentDrawTool.SelectedShape is Shape)
{
(this.CrrentDrawTool.SelectedShape as Shape).Stroke = Brushes.White;
}
else
{
(this.CrrentDrawTool.SelectedShape as TextBlock).Foreground = new SolidColorBrush(Colors.White);
}
this.CrrentDrawTool.SelectedShape = null;
}
}
if (HighlighEvent != null)
{
HighlighEvent(sender, e);
}
}
public void MoveSelectedShapbyX(double x)
{
if (this.CrrentDrawTool != null && this.CrrentDrawTool.SelectedShape != null)
{
this.CrrentDrawTool.MoveSelectedShapeByX(x);
}
else
{
if (m_BlockManage.SelectedBlock != null)
{
m_BlockManage.MoveBlockByX(m_BlockManage.SelectedBlock.BlockName, x);
}
}
}
public void MoveSelectedShapbyY(double y)
{
if (this.CrrentDrawTool != null && this.CrrentDrawTool.SelectedShape != null)
{
this.CrrentDrawTool.MoveSelectedShapeByY(y);
}
else
{
if (m_BlockManage.SelectedBlock != null)
{
m_BlockManage.MoveBlockByY(m_BlockManage.SelectedBlock.BlockName, y);
}
}
}
/// <summary>
/// 导出手动画的图形
/// </summary>
/// <param name="dxf"></param>
/// <param name="layer"></param>
public void DrawDxfShap(netDxf.DxfDocument dxf, netDxf.Tables.Layer layer)
{
List<FrameworkElement> shapes = new List<FrameworkElement>();
foreach (var item in DrawShaps)
{
shapes.Add(item);
}
if (shapes.Count > 0)
{
BlockOpr blockOpr = new BlockOpr() { dxf = dxf, layer = layer, ScaleRale = ScaleRale };
blockOpr.ExctecOpr(shapes, OprType.DrawDxfBlocks);
}
}
/// <summary>
/// 手动画图工具
/// </summary>
/// <param name="DrawAction"></param>
public void CreateTool(DrawActionType DrawAction)
{
if (this.CrrentDrawTool != null)
{
this.CrrentDrawTool.Deactivated();
}
switch (DrawAction)
{
case DrawActionType.Polyline:
this.CrrentDrawTool = new PolylineTool(this);
break;
case DrawActionType.Rectangle:
this.CrrentDrawTool = new RectangleTool(this);
break;
case DrawActionType.Circle:
this.CrrentDrawTool = new CircleTool(this);
break;
case DrawActionType.Text:
this.CrrentDrawTool = new TextTool(this);
break;
}
this.CrrentDrawTool.HighlighEvent += Shap_HighlighEvent;
}
void DesignControl_MouseMove(object sender, MouseEventArgs e)
{
Point p = e.GetPosition(this);
txtXY.Text = string.Format("X:{0} Y:{1}", p.X, p.Y);
}
void DesignControl_Loaded(object sender, RoutedEventArgs e)
{
//设置图形居中
TranslateTransform moveCanvas = new TranslateTransform() { X = this.ActualWidth / 2 - 100, Y = -100 }; //右移
(canvasDesign.RenderTransform as TransformGroup).Children.Add(moveCanvas);
m_DesignLayer = AdornerLayer.GetAdornerLayer(canvasDesign);
//foreach (var item in m_ModelCtrls)
//{
// m_DesignLayer.Add(new MyCanvasAdorner(item));
//}
//如果不去掉事件,引发不了工具选择事件
//Root.MouseLeftButtonDown -= this.m_Root_MouseLeftButtonDown;
//Root.MouseLeftButtonUp -= this.m_Root_MouseLeftButtonUp;
//Root.MouseMove -= this.m_Root_MouseMove;
//SelectTool tool = new SelectTool();
//m_ToolHepler.OnClick(tool);
//gridEditPanel.SizeChanged += new SizeChangedEventHandler(gridEditPanel_SizeChanged);
}
private void CanvasDesign_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
}
#region
TextBlock o_label, x_label, y_label;
private void DrawAxis()
{
FontFamily fontFaminly = new FontFamily("Arial");
o_label = new TextBlock() { Text = "o" };
o_label.FontFamily = fontFaminly;
o_label.FontSize = 20;
o_label.FontStyle = FontStyles.Italic;
o_label.Foreground = new SolidColorBrush(Colors.White);
Canvas.SetLeft(o_label, -10);
this.canvasDesign.Children.Add(o_label);
x_label = new TextBlock() { Text = "x" };
x_label.FontFamily = fontFaminly;
x_label.FontSize = 20;
x_label.FontStyle = FontStyles.Italic;
//x_label.RenderTransform = inverseRander;
x_label.Foreground = new SolidColorBrush(Colors.White);
Canvas.SetLeft(x_label, axisLength);
this.canvasDesign.Children.Add(x_label);
y_label = new TextBlock() { Text = "y" };
y_label.FontFamily = fontFaminly;
y_label.FontSize = 20;
y_label.FontStyle = FontStyles.Italic;
//y_label.RenderTransform = inverseRander;
y_label.Foreground = new SolidColorBrush(Colors.White);
Canvas.SetTop(y_label, -axisLength);
Canvas.SetLeft(y_label, -15);
this.canvasDesign.Children.Add(y_label);
this.x_axis.Width = axisLength;
this.y_axis.Height = axisLength;
this.y_axisArrow.RenderTransform = inverseRander;
this.DrawXScale();
this.DrawYScale();
}
/// <summary>
/// 画X轴刻度线
/// </summary>
private void DrawXScale()
{
int scaleCount = axisLength / 10; //计算刻度数
scalNumber = maxCoordinate / (scaleCount -5); //计算每个刻度数值
for (int i = 1; i < scaleCount; i++)
{
Line x_scale = new Line
{
StrokeEndLineCap = PenLineCap.Triangle,
StrokeThickness = 1,
Stroke = new SolidColorBrush(Colors.White),
X1 = i * 10 //原点x=10,每10px作1个刻度
};
x_scale.X2 = x_scale.X1; //在x轴上的刻度线起点和终点相同
x_scale.Y1 = -2; //与原点坐标相同
if (i % 5 == 0)//每5个刻度添加一个大刻度
{
x_scale.StrokeThickness = 3;//把刻度线加粗一点
x_scale.Y2 = x_scale.Y1 + 8;//刻度线长度为8px
DrawScaleLable(x_scale.X1, x_scale.Y1-10, (i * scalNumber).ToString("0"));
}
else
{
x_scale.Y2 = x_scale.Y1 + 4;//刻度线长度为4px
}
//置反Y轴
x_scale.Y1 = -x_scale.Y1;
x_scale.Y2 = -x_scale.Y2;
this.canvasDesign.Children.Add(x_scale);
this.xyScales.Add(x_scale);
}
}
/// <summary>
/// 画Y轴刻度线
/// </summary>
private void DrawYScale()
{
int scaleCount = axisLength / 10; //计算刻度数 每10px作一个刻度
scalNumber = maxCoordinate / (scaleCount -5); //计算每个刻度数值
for (int i = 1; i < scaleCount; i += 1)//由于y轴短一些所以在此作出判断只作25个刻度
{
//作出Y轴的刻度
Line y_scale = new Line
{
StrokeEndLineCap = PenLineCap.Triangle,
StrokeThickness = 1,
Stroke = new SolidColorBrush(Colors.White),
X1 = 0 //在y轴上的刻度线的起点与原点相同
};
y_scale.Y1 = i * 10; //每10px作一个刻度
y_scale.Y2 = y_scale.Y1; //起点和终点y坐标相同
if (i % 5 == 0)
{
y_scale.StrokeThickness = 3;
y_scale.X2 = y_scale.X1 + 8;//刻度线长度为4px
//标度
DrawScaleLable(y_scale.X1-40, y_scale.Y1+10, (i * scalNumber).ToString("0") );
}
else
{
y_scale.X2 = y_scale.X1 + 4;//刻度线长度为8px
}
//置反Y轴
y_scale.Y1 = -y_scale.Y1;
y_scale.Y2 = -y_scale.Y2;
canvasDesign.Children.Add(y_scale);
this.xyScales.Add(y_scale);
}
}
/// <summary>
/// 画刻度
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="text"></param>
private void DrawScaleLable(double x, double y, string text)
{
TextBlock x_ScaleLabel = new TextBlock{ Text = text, Foreground = new SolidColorBrush(Colors.White) };
Canvas.SetLeft(x_ScaleLabel, x);
Canvas.SetTop(x_ScaleLabel, -y);
this.canvasDesign.Children.Add(x_ScaleLabel);
this.xyScales.Add(x_ScaleLabel);
}
public void DrawText(string text,Point point)
{
TextBlock textBlock = new TextBlock();
textBlock.Text = text;
textBlock.Foreground = new SolidColorBrush(Colors.White);
textBlock.DataContext = point;
Canvas.SetLeft(textBlock, point.X );
Canvas.SetTop(textBlock, point.Y);
this.canvasDesign.Children.Add(textBlock);
DrawShaps.Add(textBlock);
}
private void drawLines()
{
Color _lineColor = Color.FromArgb(0xFF, 0x66, 0x66, 0x66);
// draw lines
for (int x = -4000; x <= 4000; x += 100)
{
Line verticalLine = new Line
{
Stroke = new SolidColorBrush(_lineColor),
X1 = x,
Y1 = -4000,
X2 = x,
Y2 = 4000,
StrokeThickness = 1
};
//if ( x % 1000 == 0 )
//{
// verticalLine.StrokeThickness = 6;
//}
//else
//{
// verticalLine.StrokeThickness = 2;
//}
this.canvasDesign.Children.Add(verticalLine);
//_gridLines.Add(verticalLine);
}
for (int y = -4000; y <= 4000; y += 100)
{
Line horizontalLine = new Line
{
Stroke = new SolidColorBrush(_lineColor),
X1 = -4000,
Y1 = y,
X2 = 4000,
Y2 = y,
StrokeThickness = 1
};
//if (y % 1000 == 0)
//{
// horizontalLine.StrokeThickness = 6;
//}
//else
//{
// horizontalLine.StrokeThickness = 2;
//}
this.canvasDesign.Children.Add(horizontalLine);
//_gridLines.Add(horizontalLine);
}
//Line horizontalLinedd = new Line
//{
// Stroke = new SolidColorBrush(Colors.Red),
// X1 = 0,
// Y1 = 0,
// X2 = 0,
// Y2 = 5000/5,
// StrokeThickness = 5
//};
//this.canvasDesign.Children.Add(horizontalLinedd);
}
#endregion
/// <summary>
/// 单击空白处
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void EmptyCliecked()
{
}
void gridEditPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.NewSize.Width != 0)
lastEditPanelWidth = rightColumn.Width.Value;
}
/// <summary>
/// 显示编辑面版
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void showRightPanel_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ShowEditPanel(m_blockInfo);
e.Handled = true;
}
/// <summary>
/// 隐藏编辑面版
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBlock_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
HideEditPanel();
e.Handled = true;
}
private double lastEditPanelWidth = 220;
/// <summary>
///
/// </summary>
public void ShowEditPanel( BlockInfo block)
{
m_blockInfo = block;
if (rightColumn.Width.Value == 0)
rightColumn.Width = new GridLength(lastEditPanelWidth);
showColumn.Width = new GridLength(0);
gridSplitter.Visibility = System.Windows.Visibility.Visible;
BindPropertyGrid(block);
}
/// <summary>
///
/// </summary>
/// <param name="title"></param>
/// <param name="uiElement"></param>
public void ShowEditPanel(string title, UIElement uiElement)
{
RightContain.Children.Clear();
if (uiElement != null)
{
//labTitle.Text = title;
gridSplitter.Visibility = System.Windows.Visibility.Visible;
RightContain.Children.Add(uiElement);
ShowEditPanel(m_blockInfo);
}
}
/// <summary>
///
/// </summary>
public void HideEditPanel()
{
rightColumn.Width = new GridLength(0);
gridSplitter.Visibility = System.Windows.Visibility.Collapsed;
showColumn.Width = new GridLength(20);
//labTitle.Text = "";
RightContain.Children.Clear();
}
#region
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Root_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
base.m_Root_MouseLeftButtonDown(sender, e);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Root_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
base.m_Root_MouseLeftButtonUp(sender, e);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Root_MouseMove(object sender, MouseEventArgs e)
{
base.m_Root_MouseMove(sender, e);
}
#endregion
#region private:envent:m_Root_MouseRightButtonUp
protected override void m_Root_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
base.m_Root_MouseRightButtonUp(sender, e);
}
#endregion
public void UpdatePropertyGriFiledValue(string name,string value)
{
PropertyItem pItem = m_MeataPropertyGrid.Properties.FirstOrDefault(p => p.DisplayName == name);
if (pItem != null)
{
pItem.Value= value;
}
}
private void BindPropertyGrid(BlockInfo block)
{
if (block == null) return;
if (block.Istemplate == 0)
{
FieldInfo fieldInfo = block.FieldData.FirstOrDefault(p => p.Id == 9999);
if (fieldInfo == null)
{
FieldInfo fieldInfoX = new FieldInfo();
fieldInfoX.Display = "X坐标";
fieldInfoX.Fieldvalue = (block.Locationx * ScaleRale).ToString("0.00");
fieldInfoX.Fieldname = "x";
fieldInfoX.Id = 9999;
fieldInfoX.Blockid = block.Id;
block.FieldData.Add(fieldInfoX);
FieldInfo fieldInfoy = new FieldInfo();
fieldInfoy.Display = "Y坐标";
fieldInfoy.Fieldname = "y";
fieldInfoy.Id = 9998;
fieldInfoy.Blockid = block.Id;
fieldInfoy.Fieldvalue = (-block.Locationy * ScaleRale).ToString("0.00");
block.FieldData.Add(fieldInfoy);
}
}
object obj = Utily.CreatNewClassBydt(block.Name, block.FieldData);
m_MeataPropertyGrid.SelectedObject = obj;
m_MeataPropertyGrid.Tag = obj;
if (m_MeataPropertyGrid.Properties != null)
{
PropertyItem pItem = m_MeataPropertyGrid.Properties.FirstOrDefault(p => p.DisplayName == "TypeId");
if (pItem != null)
pItem.Visibility = System.Windows.Visibility.Collapsed;
pItem = m_MeataPropertyGrid.Properties.FirstOrDefault(p => p.DisplayName == "Default");
if (pItem != null)
pItem.Visibility = System.Windows.Visibility.Collapsed;
pItem = m_MeataPropertyGrid.Properties.FirstOrDefault(p => p.DisplayName == "ConstructorPropertys");
if (pItem != null)
pItem.Visibility = System.Windows.Visibility.Collapsed;
foreach (var item in block.FieldData)
{
PropertyItem propertyItem = m_MeataPropertyGrid.Properties.FirstOrDefault(p => p.DisplayName == item.Display && p.Value.ToString() == item.Fieldvalue);
if (propertyItem != null)
{
propertyItem.Tag = item;
}
}
RightContain.Children.Clear();
RightContain.Children.Add(m_MeataPropertyGrid);
}
}
void m_MeataPropertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
{
PropertyItem propertyItem = e.OriginalSource as PropertyItem;
FieldInfo fieldInfo = propertyItem.Tag as FieldInfo;
if (fieldInfo.Display == "X坐标")
{
if (this.BlockManage.SelectedBlock == null) return;
double x;
if (double.TryParse(propertyItem.Value.ToString(), out x))
{
double moveX= x - this.BlockManage.SelectedBlock.BlockEnity.Locationx * ScaleRale;
MoveSelectedShapbyX(moveX);
this.BlockManage.CopyBlock(this.BlockManage.SelectedBlock.BlockName);
this.BlockManage.SelectedBlock.BlockEnity.Locationx = double.Parse(propertyItem.Value.ToString())/ ScaleRale;
FieldInfo field = this.BlockManage.SelectedBlock.BlockEnity.FieldData.FirstOrDefault(p => p.Id == 9999);
if (field != null)
{
BlockDAL.UpdateBlockLocation(field.Blockid, this.BlockManage.SelectedBlock.BlockEnity.Locationx, this.BlockManage.SelectedBlock.BlockEnity.Locationy);
}
}
}
else if (fieldInfo.Display == "Y坐标")
{
if (this.BlockManage.SelectedBlock == null) return;
double y;
if (double.TryParse(propertyItem.Value.ToString(), out y))
{
double moveY = -y - this.BlockManage.SelectedBlock.BlockEnity.Locationy * ScaleRale;
MoveSelectedShapbyY(-moveY);
this.BlockManage.CopyBlock(this.BlockManage.SelectedBlock.BlockName);
this.BlockManage.SelectedBlock.BlockEnity.Locationy = -double.Parse(propertyItem.Value.ToString()) / ScaleRale;
FieldInfo field= this.BlockManage.SelectedBlock.BlockEnity.FieldData.FirstOrDefault(p => p.Id == 9998);
if (field != null)
{
BlockDAL.UpdateBlockLocation(field.Blockid, this.BlockManage.SelectedBlock.BlockEnity.Locationx, this.BlockManage.SelectedBlock.BlockEnity.Locationy);
}
}
}
else
{
fieldInfo.Fieldvalue = propertyItem.Value.ToString();
BlockDAL.UpdateField(fieldInfo);
}
}
public void AddPropertyValueChanged()
{
m_MeataPropertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(m_MeataPropertyGrid_PropertyValueChanged);
}
public void RemovePropertyValueChanged()
{
m_MeataPropertyGrid.PropertyValueChanged -= new PropertyValueChangedEventHandler(m_MeataPropertyGrid_PropertyValueChanged);
}
}
}