/// <summary> /// https://stackoverflow.com/questions/16667072/how-to-draw-arc-with-radius-and-start-and-stop-angle /// </summary> /// <param name="drawing"></param> /// <param name="thisDC"></param> /// <param name="pen"></param> /// <param name="center"></param> /// <param name="radius"></param> /// <param name="startAngle"></param> /// <param name="endAngle"></param> /// <param name="mdoelToCanvas"></param> protected static void DrawArc(IDrawing drawing, DrawingContext thisDC, Pen pen, CADPoint center, double radius, double startAngle, double endAngle, bool mdoelToCanvas = true) { CADPoint centerInCanvas = mdoelToCanvas ? drawing.ModelToCanvas(center) : center; double radiusInCanvas = mdoelToCanvas ? drawing.ModelToCanvas(radius) : radius; double startAngleInCanvas = MathUtils.NormalizeRadianAngle(-startAngle); double endAngleInCanvas = MathUtils.NormalizeRadianAngle(-endAngle); // double angle = endAngle - startAngle; if (endAngle < startAngle) { angle += Utils.PI * 2; } if (radiusInCanvas > 0) { double a0 = startAngleInCanvas < 0 ? startAngleInCanvas + 2 * Math.PI : startAngleInCanvas; double a1 = endAngleInCanvas < 0 ? endAngleInCanvas + 2 * Math.PI : endAngleInCanvas; if (a1 < a0) { a1 += Math.PI * 2; } SweepDirection d = SweepDirection.Counterclockwise; bool large; bool SmallAngle = false; if (SmallAngle) { large = false; d = (a1 - a0) > Math.PI ? SweepDirection.Counterclockwise : SweepDirection.Clockwise; } else { large = (Math.Abs(a1 - a0) < Math.PI); } Point p0 = centerInCanvas.AsWPF() + new Vector(Math.Cos(a0), Math.Sin(a0)) * radiusInCanvas; Point p1 = centerInCanvas.AsWPF() + new Vector(Math.Cos(a1), Math.Sin(a1)) * radiusInCanvas; List <PathSegment> segments = new List <PathSegment> { new ArcSegment(p1, new Size(radiusInCanvas, radiusInCanvas), 0.0, large, d, true) }; List <PathFigure> figures = new List <PathFigure> { new PathFigure(p0, segments, true) { IsClosed = false } }; thisDC.DrawGeometry(null, pen, new PathGeometry(figures, FillRule.EvenOdd, null)); } }
public static void DrawRay(IDrawing owner, DrawingContext thisDC, Pen pen, CADPoint basePoint, CADVector direction, bool mdoelToCanvas = true) { CADVector dir; CADPoint basePnt; if (mdoelToCanvas) { basePnt = owner.ModelToCanvas(basePoint); CADPoint otherPnt = owner.ModelToCanvas(basePoint + direction); dir = (otherPnt - basePnt).normalized; } else { basePnt = basePoint; dir = direction; dir.Normalize(); } double xk = double.MinValue; double yk = double.MinValue; if (basePnt.X > 0 && basePnt.X < 10000 && basePnt.Y > 0 && basePnt.Y < 10000) { xk = 1; yk = 1; } else { if (dir.Y != 0) { double k = -basePnt.Y / dir.Y; if (k >= 0) { xk = basePnt.X + k * dir.X; } } if (dir.X != 0) { double k = -basePnt.X / dir.X; if (k >= 0) { yk = basePnt.Y + k * dir.Y; } } } if (xk > 0 || (xk == 0 && dir.X * dir.Y >= 0) || yk > 0 || (yk == 0 && dir.X * dir.Y >= 0)) { CADPoint epnt = basePnt + 10000 * dir; DrawLine(owner, thisDC, pen, new CADPoint(basePnt.X, basePnt.Y), new CADPoint(epnt.X, epnt.Y), false); } }
protected static void DrawLine(IDrawing owner, DrawingContext thisDC, Pen pen, CADPoint startPoint, CADPoint endPoint, bool mdoelToCanvas = true) { var p1 = startPoint; var p2 = endPoint; if (mdoelToCanvas) { p1 = owner.ModelToCanvas(new CADPoint(startPoint.X, startPoint.Y)); p2 = owner.ModelToCanvas(new CADPoint(endPoint.X, endPoint.Y)); } thisDC.DrawLine(pen, new Point(p1.X, p1.Y), new Point(p2.X, p2.Y)); }
protected static void DrawCircle(IDrawing drawing, DrawingContext thisDC, Brush fillBrush, Pen pen, CADPoint center, double radiusX, double radiusY, bool mdoelToCanvas) { var p = center; var rx = radiusX; var ry = radiusY; if (mdoelToCanvas) { p = drawing.ModelToCanvas(new CADPoint(center.X, center.Y)); rx = drawing.ModelToCanvas(radiusX); ry = drawing.ModelToCanvas(radiusY); } thisDC.DrawEllipse(fillBrush, pen, new Point(p.X, p.Y), rx, ry); }
//public void Draw() //{ // foreach (KeyValuePair<ObjectId, List<GripPoint>> kvp in _gripPnts) // { // foreach (GripPoint gripPnt in kvp.Value) // { // double width = 10; // double height = 10; // CADPoint posInCanvas = _drawing.ModelToCanvas(gripPnt.Position); // posInCanvas.X -= width / 2; // posInCanvas.Y -= height / 2; // _drawing.FillRectangle(graphics, Color.Blue, posInCanvas, width, height, CSYS.Canvas); // } // } //} public CADPoint Snap(CADPoint posInCanvas) { CADPoint posInModel = _drawing.CanvasToModel(posInCanvas); foreach (KeyValuePair <ObjectId, List <GripPoint> > kvp in _gripPnts) { int index = -1; foreach (GripPoint gripPnt in kvp.Value) { ++index; double width = 10; double height = 10; CADPoint gripPosInCanvas = _drawing.ModelToCanvas(gripPnt.Position); gripPosInCanvas.X -= width / 2; gripPosInCanvas.Y -= height / 2; Rectangle2 rect = new Rectangle2(gripPosInCanvas, width, height); if (MathUtils.IsPointInRectangle(posInCanvas, rect)) { currentGripPoint = gripPnt; currentGripEntityId = kvp.Key; currentGripPointIndex = index; return(gripPnt.Position); } } } currentGripPoint = null; currentGripEntityId = ObjectId.Null; currentGripPointIndex = -1; return(posInModel); }
protected static void DrawRectangle(IDrawing owner, DrawingContext thisDC, Brush fillBrush, Pen pen, CADPoint startPoint, double width, double height, bool mdoelToCanvas = true) { var p = startPoint; if (mdoelToCanvas) { p = owner.ModelToCanvas(new CADPoint(startPoint.X, startPoint.Y)); } thisDC.DrawRectangle(fillBrush, pen, new Rect(new Point(p.X, p.Y), new Size(width, height))); }
public void Draw(IDrawing drawing) { if (drawing == null) { return; } using (DrawingContext thisDC = this.RenderOpen()) { Brush br = new SolidColorBrush(CanvasPalette.WPFColor(drawing.AxesColor)); Pen _pen = new Pen(br, drawing.AxesThickness); // // Draw arrows double rArrLen = drawing.AxesLength * 0.15; double rArrWidth = drawing.AxesLength * 0.03; // Dont apply scaling on coordinate axes. // They must have constant size. Point zeroPnt = new Point(0.0, 0.0); zeroPnt = drawing.ModelToCanvas(zeroPnt.AsCAD(), false).AsWPF(); Point xPnt = new Point(drawing.AxesLength - rArrLen, 0.0); xPnt = drawing.ModelToCanvas(xPnt.AsCAD(), false).AsWPF(); Point yPnt = new Point(0.0, drawing.AxesLength - rArrLen); yPnt = drawing.ModelToCanvas(yPnt.AsCAD(), false).AsWPF(); thisDC.DrawLine(_pen, zeroPnt, xPnt); thisDC.DrawLine(_pen, zeroPnt, yPnt); // X arrow PathFigure arrow_X = new PathFigure(); arrow_X.IsClosed = true; Point xPnt0 = new Point(drawing.AxesLength, 0); xPnt0 = drawing.ModelToCanvas(xPnt0.AsCAD(), false).AsWPF(); arrow_X.StartPoint = xPnt0; Point xPnt1 = new Point(drawing.AxesLength - rArrLen, -rArrWidth); xPnt1 = drawing.ModelToCanvas(xPnt1.AsCAD(), false).AsWPF(); arrow_X.Segments.Add(new LineSegment(xPnt1, true)); Point xPnt2 = new Point(drawing.AxesLength - rArrLen, rArrWidth); xPnt2 = drawing.ModelToCanvas(xPnt2.AsCAD(), false).AsWPF(); arrow_X.Segments.Add(new LineSegment(xPnt2, true)); // Y arrow PathFigure arrow_Y = new PathFigure(); arrow_Y.IsClosed = true; Point yPnt0 = new Point(0, drawing.AxesLength); yPnt0 = drawing.ModelToCanvas(yPnt0.AsCAD(), false).AsWPF(); arrow_Y.StartPoint = yPnt0; Point yPnt1 = new Point(-rArrWidth, drawing.AxesLength - rArrLen); yPnt1 = drawing.ModelToCanvas(yPnt1.AsCAD(), false).AsWPF(); arrow_Y.Segments.Add(new LineSegment(yPnt1, true)); Point yPnt2 = new Point(rArrWidth, drawing.AxesLength - rArrLen); yPnt2 = drawing.ModelToCanvas(yPnt2.AsCAD(), false).AsWPF(); arrow_Y.Segments.Add(new LineSegment(yPnt2, true)); PathGeometry pg = new PathGeometry(new[] { arrow_X, arrow_Y }); thisDC.DrawGeometry(br, _pen, pg); // // Draw text FontFamily ff = new FontFamily("Arial"); Typeface tf = new Typeface(ff, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal); FormattedText text_X = new FormattedText("X", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, tf, drawing.AxesTextSize, br); // Positive Y direction because default WPF axes have Y directed to bottom. // Here we draw in default WPF system. // Read coom in SimpleCAD.ModelToCanvas(). thisDC.DrawText(text_X, xPnt + new Vector(0, 3)); FormattedText text_Y = new FormattedText("Y", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, tf, drawing.AxesTextSize, br); // Negative Y direction because default WPF axes have Y directed to bottom. // Here we draw in default WPF system. // Read coom in SimpleCAD.ModelToCanvas(). thisDC.DrawText(text_Y, yPnt + new Vector(-drawing.AxesTextSize, -drawing.AxesTextSize)); } }
public static void DrawXLine(IDrawing owner, DrawingContext thisDC, Pen pen, CADPoint basePoint, CADVector direction, bool mdoelToCanvas = true) { CADVector dir; CADPoint basePnt; if (mdoelToCanvas) { basePnt = owner.ModelToCanvas(basePoint); CADPoint otherPnt = owner.ModelToCanvas(basePoint + direction); dir = (otherPnt - basePnt).normalized; } else { basePnt = basePoint; dir = direction; dir.Normalize(); } double xk = double.MinValue; double yk = double.MinValue; if (dir.Y != 0) { double k = basePnt.Y / dir.Y; xk = basePnt.X - k * dir.X; } if (dir.X != 0) { double k = basePnt.X / dir.X; yk = basePnt.Y - k * dir.Y; } if (xk > 0 || (xk == 0 && dir.X * dir.Y >= 0)) { CADPoint spnt = new CADPoint(xk, 0); if (dir.Y < 0) { dir = -dir; } CADPoint epnt = spnt + 10000 * dir; DrawLine(owner, thisDC, pen, new CADPoint((float)spnt.X, (float)spnt.Y), new CADPoint((float)epnt.X, (float)epnt.Y), false); } else if (yk > 0 || (yk == 0 && dir.X * dir.Y >= 0)) { CADPoint spnt = new CADPoint(0, yk); if (dir.X < 0) { dir = -dir; } CADPoint epnt = spnt + 10000 * dir; DrawLine(owner, thisDC, pen, new CADPoint((float)spnt.X, (float)spnt.Y), new CADPoint((float)epnt.X, (float)epnt.Y), false); } }