public void Init(bool recalc = false, bool clear = false) { if (IsInit || recalc) { Helper.Update(this); if (clear) { Zoom = 1; Figure = new Figure(); DPI = DPIDefault * Zoom; } if (recalc) { MyCanvas.Children.Clear(); } else { IsInit = false; } if (DPI < 1) { DPI = DPIDefault; } ZoomBox.Text = $"Zoom: 1:{Zoom}"; CenterX = MyCanvas.ActualWidth / 2; CenterY = MyCanvas.ActualHeight / 2; MyCanvas.Children.AddRange(Helper.DrawLine(CenterX, 0, CenterX, MyCanvas.ActualHeight, false, 1)); //vertical line MyCanvas.Children.AddRange(Helper.DrawLine(0, CenterY, MyCanvas.ActualWidth, CenterY, false, 1)); //horizontal line for (double i = CenterY; i > 0; i -= DPI) { if (i != CenterY) { MyCanvas.Children.AddRange(Helper.DrawLine(CenterX - 5 * Zoom, i, CenterX + 5 * Zoom, i, false, 1)); } } for (double i = CenterX; i < MyCanvas.ActualWidth; i += DPI) { if (i != CenterX) { MyCanvas.Children.AddRange(Helper.DrawLine(i, CenterY - 5 * Zoom, i, CenterY + 5 * Zoom, false, 1)); } } for (double i = CenterY; i < MyCanvas.ActualHeight; i += DPI) { if (i != CenterY) { MyCanvas.Children.AddRange(Helper.DrawLine(CenterX - 5 * Zoom, i, CenterX + 5 * Zoom, i, false, 1)); } } for (double i = CenterX; i > 0; i -= DPI) { if (i != CenterX) { MyCanvas.Children.AddRange(Helper.DrawLine(i, CenterY - 5 * Zoom, i, CenterY + 5 * Zoom, false, 1)); } } if (recalc && !clear) { Helper.ReWriteLines(); } if (IsOpenFile.HasValue && IsOpenFile.Value == true) { IsOpenFile = null; UploadFile(); } } }
private void UploadFile() { var dialog = new OpenFileDialog(); if (dialog.ShowDialog() == true) { var extensionIndex = dialog.FileName.IndexOf(".txt"); if (extensionIndex > -1 && extensionIndex == dialog.FileName.Length - 4) { int index = 0; var readData = File.ReadAllLines(dialog.FileName); if (readData != null && readData.Length >= 8) { if (Data == null) { Data = new Data(); } if (!string.IsNullOrWhiteSpace(readData[index])) { Data.Name = readData[index++].Trim(); if (!string.IsNullOrWhiteSpace(readData[index])) { Data.Material = readData[index++].Trim(); if (!string.IsNullOrWhiteSpace(readData[index]) && int.TryParse(readData[index++].Trim(), out int figureCount)) { try { var vertices = new Dictionary <string, int>(); for (var i = index; i < index + figureCount; i++) { if (!string.IsNullOrWhiteSpace(readData[i]) && !string.IsNullOrWhiteSpace(readData[i + figureCount]) && int.TryParse(readData[i + figureCount].Trim(), out int value) && value > 0) { vertices.Add(readData[i].Trim(), value); } else { //Helper.Error(type); return; } } index += figureCount * 2; foreach (var item in vertices) { var figure = new Figure(item.Key) { ID = GetID() }; for (int i = index; i < index + item.Value - 1; i++) { if (readData[i].GetCoordinates(out double x1, out double y1) && readData[i + 1].GetCoordinates(out double x2, out double y2)) { figure.BaseLines.Add(new MyLine(x1, y1, x2, y2)); } else { //Helper.Error(type); return; } } if (figure.BaseLines.Count != 4) { //Helper.Error(type); return; } index += item.Value; Data.Figures.Add(figure); } //Helper.FiguresPanel(ShowFiguresPanel); Figure = Data.Figures[0]; if (Math.Abs(Figure.BaseLines[0].X1.ToDouble()) == Math.Abs(Figure.BaseLines[2].X1.ToDouble())) { Figure.Type = FigureType.rectangle; Helper.InitRectangleParameters(Math.Abs(Figure.BaseLines[0].X1.ToDouble() * 2), Math.Abs(Figure.BaseLines[0].Y1.ToDouble() * 2)); } else { Figure.Type = FigureType.trapeze; Helper.InitTrapezeParameters(Math.Abs(Figure.BaseLines[0].X1.ToDouble() * 2), Math.Abs(Figure.BaseLines[2].X1.ToDouble() * 2), Math.Abs(Figure.BaseLines[0].Y1.ToDouble() * 2)); } Helper.Update(this); Helper.InitFiguresPanelContent(); Helper.DrawFigure(Figure.BaseLines[0], Figure.BaseLines[1], Figure.BaseLines[2], Figure.BaseLines[3], Figure.Type); RightMenuShow.IsEnabled = true; RightMenuHide.IsEnabled = true; return; }