/// <summary> /// Determine the actual plane matrix from the selected plane of this behavior /// </summary> /// <param name="element"></param> /// <returns></returns> protected Matrix4x4 GetUsedPlane(NotuiElement element) { ActualPlaneSelection = UseSelectedPlane == SelectedPlane.ParentPlane ? (element.Parent != null ? SelectedPlane.ParentPlane : SelectedPlane.ViewAligned) : UseSelectedPlane; Matrix4x4 usedplane; switch (ActualPlaneSelection) { case SelectedPlane.ParentPlane: usedplane = element.Parent.DisplayMatrix; break; case SelectedPlane.OwnPlane: usedplane = element.DisplayMatrix; break; case SelectedPlane.ViewAligned: usedplane = Matrix4x4.CreateFromQuaternion(element.Context.ViewOrientation) * Matrix4x4.CreateTranslation(element.DisplayMatrix.Translation); break; default: usedplane = Matrix4x4.CreateFromQuaternion(element.Context.ViewOrientation) * Matrix4x4.CreateTranslation(element.DisplayMatrix.Translation); break; } return(usedplane); }
public List <Point3D> Arc(Point3D Start, Point3D End, Point3D Center, int NumPoints, bool clockwise, SelectedPlane Plane = SelectedPlane.XY) { var list = new List <Point3D>(NumPoints); // TODO: Check R same for both /* Create Vectors */ Vector ortsvectorcenter = new Vector(); Vector ortsvectorstart = new Vector(); Vector ortsvectorend = new Vector(); /*Select Plane*/ // XY // lokal vectors switch (Plane) { case SelectedPlane.XY: ortsvectorcenter = new Vector(Center.X, Center.Y); ortsvectorstart = new Vector(Start.X, Start.Y); ortsvectorend = new Vector(End.X, End.Y); break; case SelectedPlane.ZX: // ZX ortsvectorcenter = new Vector(Center.X, Center.Z); ortsvectorstart = new Vector(Start.X, Start.Z); ortsvectorend = new Vector(End.X, End.Z); break; case SelectedPlane.YZ: // YZ ortsvectorcenter = new Vector(Center.Y, Center.Z); ortsvectorstart = new Vector(Start.Y, Start.Z); ortsvectorend = new Vector(End.Y, End.Z); break; default: ortsvectorcenter = new Vector(Center.X, Center.Y); ortsvectorstart = new Vector(Start.X, Start.Y); ortsvectorend = new Vector(End.X, End.Y); break; } // Vectors for use Vector start = ortsvectorstart - ortsvectorcenter; Vector end = ortsvectorend - ortsvectorcenter; /* Calculate Angle between the start and the end vector beginning at the rotation * center point*/ double alpha = 0; alpha = SignedAngle2(start, end); // If alpha <0 then it is in clockwise direction. // If alpha > 0 then it is in counterclockwise direction if (clockwise) { if (alpha > 0) { alpha = Math.PI * 2 - alpha; alpha *= -1; } else if (alpha == 0) { alpha = Math.PI * -2; } } else { if (alpha < 0) { alpha = Math.PI * 2 + alpha; } else if (alpha == 0) { alpha = Math.PI * 2; } } if (alpha == 0) { return(list); } /*Rotate vector step by step. For this we calculate an vector(array) with angles which should be used to rotate the vector to*/ var anglesteps = alpha / (NumPoints); // -1 'cause start is already included by for - loop Vector ortsvectorrotated = new Vector(); if (alpha < 0) { for (double w = 0; w >= alpha; w += anglesteps) { Vector coor = RotateVector2d(start.X, start.Y, w); ortsvectorrotated = coor + ortsvectorcenter; switch (Plane) { case SelectedPlane.XY: list.Add(new Point3D(ortsvectorrotated.X, ortsvectorrotated.Y, Start.Z)); break; case SelectedPlane.ZX: list.Add(new Point3D(ortsvectorrotated.X, Start.Y, ortsvectorrotated.Y)); break; case SelectedPlane.YZ: list.Add(new Point3D(Start.X, ortsvectorrotated.X, ortsvectorrotated.Y)); break; default: break; } } } else { for (double w = 0; w <= alpha; w += anglesteps) { Vector coor = RotateVector2d(start.X, start.Y, w); ortsvectorrotated = coor + ortsvectorcenter; switch (Plane) { case SelectedPlane.XY: list.Add(new Point3D(ortsvectorrotated.X, ortsvectorrotated.Y, Start.Z)); break; case SelectedPlane.ZX: list.Add(new Point3D(ortsvectorrotated.X, Start.Y, ortsvectorrotated.Y)); break; case SelectedPlane.YZ: list.Add(new Point3D(Start.X, ortsvectorrotated.X, ortsvectorrotated.Y)); break; default: break; } } } return(list); }
public async Task Load() { #region Read File Content OpenFileDialog odialog = new OpenFileDialog(); if (odialog.ShowDialog() == true) { using (StreamReader sr = File.OpenText(odialog.FileName)) { CNCFileContent = string.Empty; CNCFileContentArray = new ObservableCollection <string>(Regex.Split(await sr.ReadToEndAsync(), "\r\n|\r|\n")); int e = 0; foreach (string item in CNCFileContentArray) { CNCFileContent += item + '\n'; if (e > 1000) { break; } e++; } } } #endregion PlotViewModel.PointList.Clear(); PlotViewModel.LineList.Clear(); await Task.Run(() => { #region Get Points List <Point3D> coll = new List <Point3D>(); Point3D currentcoor = new Point3D(0, 0, 0); Point3D lastcoor = new Point3D(0, 0, 0); string xstr = string.Empty, ystr = string.Empty, zstr = string.Empty; double x, y, z; string currmovmodstr = "g90"; SelectedPlane currplane = SelectedPlane.XY; foreach (string item in CNCFileContentArray) { // Remove Comments var ite = Regex.Replace(item, @"\(.*?\)", ""); // check for global/relativ instruction string ret = Regex.Match(ite, @"(G90)|(G91)").Groups[0].Value.ToLower(); if (ret.ToLower().Contains("g90")) { currmovmodstr = "g90"; } else if (ret.ToLower().Contains("g91")) { currmovmodstr = "g91"; } ret = Regex.Match(ite, @"(G17)|(G18)|(G19)").Groups[0].Value.ToLower(); //var gr = Regex.Match(ite, @"(G17)|(G18)|(G19)").Groups; if (ret.ToLower().Contains("g17")) { currplane = SelectedPlane.XY; } else if (ret.ToLower().Contains("g18")) { currplane = SelectedPlane.ZX; } else if (ret.ToLower().Contains("g19")) { currplane = SelectedPlane.YZ; } // Find possible X xstr = Regex.Match(ite, @"X(-?[0-9]*.[0-9]*)").Groups[1].Value; ystr = Regex.Match(ite, @"Y(-?[0-9]*.[0-9]*)").Groups[1].Value; zstr = Regex.Match(ite, @"Z(-?[0-9]*.[0-9]*)").Groups[1].Value; // Save last coordinate lastcoor.X = currentcoor.X; lastcoor.Y = currentcoor.Y; lastcoor.Z = currentcoor.Z; if (!string.IsNullOrEmpty(xstr) || !string.IsNullOrEmpty(ystr) || !string.IsNullOrEmpty(zstr)) { try { if (!string.IsNullOrEmpty(xstr)) { x = double.Parse(xstr); if (currmovmodstr == "g90") { currentcoor.X = x; } else { currentcoor.Y += x; } } else { x = currentcoor.X; } } catch (FormatException ex) { x = currentcoor.X; } try { if (!string.IsNullOrEmpty(ystr)) { y = double.Parse(ystr); if (currmovmodstr == "g90") { currentcoor.Y = y; } else { currentcoor.Y += y; } } else { y = currentcoor.Y; } } catch (FormatException ex) { y = currentcoor.Y; } try { if (!string.IsNullOrEmpty(zstr)) { z = double.Parse(zstr); if (currmovmodstr == "g90") { currentcoor.Z = z; } else { currentcoor.Z += z; } } else { z = currentcoor.Z; } } catch (FormatException ex) { z = currentcoor.Z; } var p = new Point3D(); p.X = currentcoor.X; p.Y = currentcoor.Y; p.Z = currentcoor.Z; /* * Usage * G2 Xnnn Ynnn Innn Jnnn Ennn Fnnn (Clockwise Arc) * G3 Xnnn Ynnn Innn Jnnn Ennn Fnnn (Counter-Clockwise Arc) * Parameters * Xnnn The position to move to on the X axis * Ynnn The position to move to on the Y axis * Innn The point in X space from the current X position to maintain a constant distance from * Jnnn The point in Y space from the current Y position to maintain a constant distance from * Ennn The amount to extrude between the starting point and ending point * Fnnn The feedrate per minute of the move between the starting point and ending point (if supplied) */ //Check if a line or a arc var g = Regex.Match(ite, @"([G][2]|[G][3]) ").Groups; string arcstr = Regex.Match(ite, @"([G][2]|[G][3]) ").Groups[0].Value.ToLower(); Point3D end; Point3D center; Point3D start; List <Point3D> plist = new List <Point3D>(); if (!string.IsNullOrEmpty(arcstr)) { string istr = Regex.Match(ite, @"I(-?[0-9]*.[0-9]*)").Groups[1].Value; string jstr = Regex.Match(ite, @"J(-?[0-9]*.[0-9]*)").Groups[1].Value; string rstr = Regex.Match(ite, @"R(-?[0-9]*.[0-9]*)").Groups[1].Value; double I = 0, J = 0, R = 0; if (!string.IsNullOrEmpty(istr) || !string.IsNullOrEmpty(jstr)) { I = double.Parse(istr); J = double.Parse(jstr); } else if (!string.IsNullOrEmpty(rstr)) { R = double.Parse(rstr); } if (!string.IsNullOrEmpty(istr) || !string.IsNullOrEmpty(jstr)) { start = new Point3D(lastcoor.X, lastcoor.Y, lastcoor.Z); end = new Point3D(currentcoor.X, currentcoor.Y, currentcoor.Z); center = new Point3D(lastcoor.X + I, lastcoor.Y + J, lastcoor.Z); plist = Arc(start, end, center, 10, arcstr.ToLower().Contains("g2"), currplane); } else if (!string.IsNullOrEmpty(rstr)) { start = new Point3D(lastcoor.X, lastcoor.Y, lastcoor.Z); end = new Point3D(currentcoor.X, currentcoor.Y, currentcoor.Z); plist = ArcR(start, end, 10, arcstr.ToLower().Contains("g2"), currplane); } } foreach (var il in plist) { coll.Add(il); } coll.Add(new Point3D(currentcoor.X, currentcoor.Y, currentcoor.Z)); } } List <Point3D> llist = new List <Point3D>(); Point3D firstpoint, secondpoint; for (int i = 0; i < coll.Count - 1; i++) { firstpoint = coll[i]; secondpoint = coll[i + 1]; // Add Line llist.Add(firstpoint); llist.Add(secondpoint); } PlotViewModel.LineList = llist; PlotViewModel.PointList = coll; #endregion }); }