public void Draw(Bitmap bmp, IMarkGeometry geometry, bool showVertices = false) { using (var graphics = Graphics.FromImage(bmp)) { Draw(graphics, geometry, showVertices); } }
public bool AddChild(IMarkGeometry geometryIn, Color bgColor, Color fgColor) { if (AddChild_NoUpdate(geometryIn, bgColor, fgColor)) { Update(); return(true); } return(false); }
public void Draw(Bitmap bmp, IMarkGeometry geometry, Matrix4x4 transform, bool showVertices = false) { using (var graphics = Graphics.FromImage(bmp)) { var _geometry = (IMarkGeometry)geometry.Clone(); _geometry.Transform(transform); Draw(graphics, _geometry, showVertices); } }
public MarkGeometryTree(IMarkGeometry geometryIn, Color bgColor, Color fgColor) : base() { Fill = fgColor; BackgroundColor = bgColor; Geometry = geometryIn; Geometry.Fill = Fill; Update(); }
public static int CompareHandler(IMarkGeometry aIn, IMarkGeometry bIn) { if (aIn.Area > bIn.Area) { return(1); } else if (aIn.Area < bIn.Area) { return(-1); } return(0); }
private static void WriteGeometry(StreamWriter writerIn, IMarkGeometry geometryIn, string layerName = "0") { if (geometryIn is MarkGeometryPoint point) { writerIn.Write($"\n0\nPOINT\n100\nAcDbEntity\n8\n{layerName}100\nAcDbPoint\n10\n{point.X}\n20\n{point.Y}\n30\n{point.Z}"); } else if (geometryIn is MarkGeometryLine line) { writerIn.Write($"\n0\nLINE\n100\nAcDbEntity\n8\n{layerName}100\nAcDbLine\n10\n{line.StartPoint.X}\n20\n{line.StartPoint.Y}\n30\n{line.StartPoint.Z}\n11\n{line.EndPoint.X}\n21\n{line.EndPoint.Y}\n31\n{line.EndPoint.Z}"); } else if (geometryIn is MarkGeometryCircle circle) { writerIn.Write($"\n0\nCIRCLE\n100\nAcDbEntity\n8\n{layerName}100\nAcDbCircle\n10\n{circle.CentrePoint.X}\n20\n{circle.CentrePoint.Y}\n30\n{circle.CentrePoint.Z}\n40\n{circle.Radius}"); } else if (geometryIn is MarkGeometryArc arc) { writerIn.Write($"\n0\nARC\n100\nAcDbEntity\n8\n{layerName}100\nAcDbCircle\n10\n{arc.CentrePoint.X}\n20\n{arc.CentrePoint.Y}\n30\n{arc.CentrePoint.Z}\n40\n{arc.Radius}\n100\nAcDbArc\n50\n{arc.StartAngle}\n51\n{arc.EndAngle}"); } else if (geometryIn is MarkGeometrySpline spline) { int flag = spline.IsClosed ? 1 : spline.IsPeriodic ? 2 : 8; writerIn.Write($"\n0\nSPLINE\n100\nAcDbEntity\n8\n{layerName}100\nAcDbSpline\n210\n0\n220\n0\n230\n0\n70\n{flag}\n71\n{spline.Degree}\n72\n{spline.Knots.Count}\n73\n{spline.ControlPoints.Count}\n74\n{spline.FitPoints.Count}\n42\n1e-007\n43\n1e-007\n44\n1e-007"); foreach (var knot in spline.Knots) { writerIn.Write($"\n40\n{knot}"); } foreach (var cPoint in spline.ControlPoints) { writerIn.Write($"\n10\n{cPoint.X}\n20\n{cPoint.Y}\n30\n{cPoint.Z}"); } foreach (var fPoint in spline.FitPoints) { writerIn.Write($"\n11\n{fPoint.X}\n21\n{fPoint.Y}\n31\n{fPoint.Z}"); } } else if (geometryIn is MarkGeometryPath path) { writerIn.Write($"\n0\nLWPOLYLINE\n100\nAcDbEntity\n8\n{layerName}100\nAcDbPolyline\n90\n{path.Points.Count}\n70\n0"); foreach (var pPoint in path.Points) { writerIn.Write($"\n10\n{pPoint.X}\n20\n{pPoint.Y}"); } } }
private static (string Layer, IMarkGeometry Geometry) TryParseCircle(StreamReader readerIn) { string layerName = null; IMarkGeometry geometry = null; while (true) { var(found, line) = SkipUntil(readerIn, MatchLayerOrCircle); if (!found) { break; } switch (MatchLayerOrCircle.Match(line).Value.Trim()) { case "8": layerName = readerIn.ReadLine(); break; case "AcDbCircle": MarkGeometryPoint centre = TryParsePoint(readerIn); if (centre == null) { throw new Exception("Failed to parse centre point of arc"); } if (!SkipUntil(readerIn, MatchGroupCodeForRadius).Found) { throw new Exception("Failed to parse arc radius"); } double radius = double.Parse(readerIn.ReadLine()); geometry = new MarkGeometryCircle(centre, radius) { LayerName = layerName }; return(layerName, geometry); default: throw new Exception($"Matched circle attribute is not supported: `{line}`"); } } return(layerName, geometry); }
public bool AddChild_NoUpdate(IMarkGeometry vector, Color bgColor, Color fgColor) { if (Geometry == null) { if (!ShapeIsRegion(vector)) { return(false); } Geometry = vector; Geometry.Fill = fgColor; return(true); } if (!GeometricArithmeticModule.IsWithin2D(vector, Geometry)) { return(false); } foreach (var child in Children) { if (child is MarkGeometryTree && GeometricArithmeticModule.IsWithin2D(vector, child)) { if (!(child as MarkGeometryTree).AddChild_NoUpdate(vector, fgColor, bgColor)) { vector.Fill = bgColor; Children.Add(vector); } return(true); } } if (ShapeIsRegion(vector)) { Children.Add(new MarkGeometryTree(vector, fgColor, bgColor)); } else { vector.Fill = bgColor; Children.Add(vector); } return(true); }
private static (string Layer, IMarkGeometry Geometry) TryParseLine(StreamReader readerIn) { string layerName = null; IMarkGeometry geometry = null; while (true) { var(found, line) = SkipUntil(readerIn, MatchLayerOrLine); if (!found) { break; } switch (MatchLayerOrLine.Match(line).Value.Trim()) { case "8": layerName = readerIn.ReadLine(); break; case "AcDbLine": var startPoint = TryParsePoint(readerIn); var endPoint = TryParsePoint(readerIn); geometry = new MarkGeometryLine(startPoint, endPoint) { LayerName = layerName }; return(layerName, geometry); default: throw new Exception($"Matched circle attribute is not supported: `{line}`"); } } return(layerName, geometry); }
public virtual void AddDefault(IMarkGeometry geometryIn, double[] color) { if (geometryIn == null || color == null || color.Length < 3) { AddDefault(geometryIn); return; } // Update extents MaxX = Math.Max(geometryIn.Extents.MaxX, MaxX); MaxY = Math.Max(geometryIn.Extents.MaxY, MaxY); MinX = Math.Min(geometryIn.Extents.MinX, MinX); MinY = Math.Min(geometryIn.Extents.MinY, MinY); // Update Counter Count += 1; if (geometryIn is MarkGeometryPoint point) { _points.Add(new VertexGroup() { Color = color, Vertices = new List <double>() { point.X, point.Y } }); } else if (geometryIn is MarkGeometryLine line) { // add to default _lines.Add(new VertexGroup() { Color = color, Vertices = new List <double>() { line.StartPoint.X, line.StartPoint.Y, line.EndPoint.X, line.EndPoint.Y } }); } else if (geometryIn is MarkGeometryCircle circle) { var vtx = new VertexGroup(color); for (int i = 0; i <= circle.VertexCount; i++) { vtx.Add( (circle.CentrePoint.X + (circle.Radius * Math.Cos(i * Math.PI * 2 / circle.VertexCount))), (circle.CentrePoint.Y + (circle.Radius * Math.Sin(i * Math.PI * 2 / circle.VertexCount))) ); } _closedPolylines.Add(vtx); } else if (geometryIn is MarkGeometryArc arc) { var arcPath = new MarkGeometryPath(arc); var vtx = new VertexGroup(color); // add points for (int i = 0; i < arcPath.Points.Count; i++) { vtx.Add(arcPath.Points[i].X, arcPath.Points[i].Y); } if (arcPath.IsClosed) { _closedPolylines.Add(vtx); } else { _openPolylines.Add(vtx); } } else if (geometryIn is MarkGeometryPath path) { var vtx = new VertexGroup(color); // add points for (int i = 0; i < path.Points.Count; i++) { vtx.Add(path.Points[i].X, path.Points[i].Y); } if (path.IsClosed) { _closedPolylines.Add(vtx); } else { _openPolylines.Add(vtx); } } else if (geometryIn is IMarkGeometryWrapper wrapper) { wrapper.BeginGetAll((geometry) => { AddDefault(geometry, color); return(true); }); } Update(); }
private static void PrintExtents(string tag, IMarkGeometry geometry) { PrintExtents(tag, geometry.Extents); Console.WriteLine($"Area: {Math.Round(geometry.Area, 4)}"); Console.WriteLine($"Perimeter: {Math.Round(geometry.Perimeter, 4)}"); }
public static bool ShapeIsRegion(IMarkGeometry shape) { return(shape is MarkGeometryPath || shape is MarkGeometryCircle || shape is MarkGeometryRectangle || shape is MarkGeometryEllipse); }
private void Draw(Graphics graphics, IMarkGeometry geometry, bool showVertices) { if (geometry is MarkGeometryPoint point) { var pointRadius = 0.5 * DefaultPointWidth; graphics.FillEllipse( point.Fill == null ? DefaultPointColor : ToSolidBrush((Color)point.Fill), (float)(point.X - pointRadius), (float)(point.Y - pointRadius), DefaultPointWidth, DefaultPointWidth ); } else if (geometry is MarkGeometryLine line) { graphics.DrawLine( line.Stroke == null ? DefaultStroke : new Pen((Color)line.Stroke, DefaultStrokeWidth), (float)line.StartPoint.X, (float)line.StartPoint.Y, (float)line.EndPoint.X, (float)line.EndPoint.Y ); if (showVertices) { Draw(graphics, line.StartPoint, false); Draw(graphics, line.EndPoint, false); } } else if (geometry is MarkGeometryCircle circle) { graphics.FillEllipse( circle.Fill == null ? DefaultFillColor : ToSolidBrush((Color)circle.Fill), (float)circle.Extents.MinX, (float)circle.Extents.MinY, (float)(2 * circle.Radius), (float)(2 * circle.Radius) ); graphics.DrawEllipse( circle.Stroke == null ? DefaultStroke : new Pen((Color)circle.Stroke, DefaultStrokeWidth), (float)circle.Extents.MinX, (float)circle.Extents.MinY, (float)(2 * circle.Radius), (float)(2 * circle.Radius) ); } else if (geometry is MarkGeometryArc arc) { Draw(graphics, new MarkGeometryPath(arc), showVertices); //Draw(graphics, arc.Extents.Boundary, showVertices); //// TODO : Use filled pie for filled arc //graphics.DrawArc( // arc.Fill == null ? DefaultStroke : new Pen((Color)arc.Stroke, DefaultStrokeWidth), // (float)arc.Extents.MinX, // (float)arc.Extents.MinY, // (float)(2*arc.Radius), // (float)(2*arc.Radius), // (float)GeometricArithmeticModule.ToDegrees(arc.StartAngle), // convert from radians to degrees // (float)GeometricArithmeticModule.ToDegrees(arc.Angle) //); //if (showVertices) //{ // Draw(graphics, arc.StartPoint, false); // Draw(graphics, arc.EndPoint, false); //} } else if (geometry is MarkGeometryPath path) { var points = ((List <PointF>)path).ToArray(); if (path.IsClosed) { graphics.FillPolygon( path.Fill == null ? DefaultFillColor : ToSolidBrush((Color)path.Fill), points ); } graphics.DrawLines( path.Stroke == null ? DefaultStroke : new Pen((Color)path.Stroke, DefaultStrokeWidth), points ); if (showVertices) { for (int i = 0; i < path.Points.Count; i++) { Draw(graphics, path.Points[i], false); } } } else if (geometry is MarkGeometriesWrapper wrapper) { int n = GeometricArithmeticModule.Max <int>(new int[] { wrapper.Points.Count, wrapper.Arcs.Count, wrapper.Circles.Count, wrapper.Lines.Count, wrapper.Paths.Count }); for (int i = 0; i < n; i++) { if (i < wrapper.Points.Count) { Draw(graphics, wrapper.Points[i], showVertices); } if (i < wrapper.Arcs.Count) { Draw(graphics, wrapper.Arcs[i], showVertices); } if (i < wrapper.Circles.Count) { Draw(graphics, wrapper.Circles[i], showVertices); } if (i < wrapper.Lines.Count) { Draw(graphics, wrapper.Lines[i], showVertices); } if (i < wrapper.Paths.Count) { Draw(graphics, wrapper.Paths[i], showVertices); } } } else if (geometry is MarkGeometryTree tree) { foreach (var element in tree.Flatten()) { Draw(graphics, element, showVertices); } } }