Esempio n. 1
0
        //--------------------------------------------------------------------------------------------------

        protected void AddEllipse(Geom2d_Ellipse geom2DEllipse, double first, double last, bool reverse)
        {
            if (Math.Abs(last - first) < geom2DEllipse.Period())
            {
                // Ellipse arc
                var start = geom2DEllipse.Value(reverse ? last : first);
                var end   = geom2DEllipse.Value(reverse ? first : last);

                // Add the midpoint seems to be the simplest way to improve the BB
                BoundingBox?.Add(geom2DEllipse.Value(first + (last - first) / 2));

                // Get parameters of ellipse
                double radiusX       = geom2DEllipse.MajorRadius();
                double radiusY       = geom2DEllipse.MinorRadius();
                double angleX        = Maths.NormalizeAngleDegree(geom2DEllipse.XAxis().Angle(Ax2d.OX).ToDeg());
                double length        = last - first;
                double halfParameter = (geom2DEllipse.LastParameter() - geom2DEllipse.FirstParameter()) / 2.0;
                var    size          = (length < halfParameter) ? SvgArcSize.Small : SvgArcSize.Large;
                var    sweep         = geom2DEllipse.Position().Sense() > 0
                    ? (reverse ? SvgArcSweep.Negative : SvgArcSweep.Positive)
                    : (reverse ? SvgArcSweep.Positive : SvgArcSweep.Negative);

                AddToPath(new SvgPathSegArc(start, radiusX, radiusY, angleX, size, sweep, end));
            }
            else
            {
                // Complete ellipse
                if (reverse)
                {
                    geom2DEllipse = geom2DEllipse.Reversed() as Geom2d_Ellipse;
                }

                double rotationAngle = geom2DEllipse.XAxis().Angle(Ax2d.OX);

                var segment = new SvgEllipseElement(geom2DEllipse.Location(), geom2DEllipse.MajorRadius(), geom2DEllipse.MinorRadius());
                segment.Transforms.Add(new SvgRotateTransform(Maths.NormalizeAngleDegree(rotationAngle.ToDeg())));
                CurrentGroup.Children.Add(segment);

                // Enlarge BB
                var f = geom2DEllipse.FirstParameter();
                var q = (geom2DEllipse.LastParameter() - f) / 4;
                BoundingBox?.Add(geom2DEllipse.Value(f));
                BoundingBox?.Add(geom2DEllipse.Value(f + q));
                BoundingBox?.Add(geom2DEllipse.Value(f + q * 2));
                BoundingBox?.Add(geom2DEllipse.Value(f + q * 3));
            }
        }
        //--------------------------------------------------------------------------------------------------

        void _ImportEllipse(SvgEllipseElement ellipse)
        {
            var centerPoint = ellipse.Center;
            var radiusX     = ellipse.RadiusX;
            var radiusY     = ellipse.RadiusY;

            if (radiusX <= 0 || radiusY <= 0)
            {
                return;
            }

            var rimPointX = ellipse.RimPointX;
            var rimPointY = ellipse.RimPointY;

            var centerIndex = _AddPoint(centerPoint);
            var rimIndexX   = _AddPoint(rimPointX);
            var rimIndexY   = _AddPoint(rimPointY);

            _Segments.Add(new SketchSegmentEllipse(centerIndex, rimIndexX, rimIndexY));
        }
        //--------------------------------------------------------------------------------------------------

        void _AddUnconnectableSegment(SketchSegment sketchSegment)
        {
            if (sketchSegment is SketchSegmentCircle)
            {
                var sketchCircle = (SketchSegmentCircle)sketchSegment;
                var radius       = sketchCircle.Radius(_Sketch.Points);
                if (radius > 0)
                {
                    var center = _Sketch.Points[sketchCircle.CenterPoint];
                    _Group.Children.Add(new SvgCircleElement(center, radius));
                    return;
                }
            }
            else if (sketchSegment is SketchSegmentEllipse)
            {
                var sketchEllipse = (SketchSegmentEllipse)sketchSegment;
                var curve         = sketchEllipse.MakeCurve(_Sketch.Points) as Geom2d_Ellipse;
                if (curve != null)
                {
                    var    center         = _Sketch.Points[sketchEllipse.CenterPoint];
                    var    element        = new SvgEllipseElement(center, curve.MajorRadius(), curve.MinorRadius());
                    var    majorAxisPoint = _Sketch.Points[sketchEllipse.MajorAxisPoint(_Sketch.Points)];
                    double rotAngle       = new Ax2d(center, Dir2d.DX).Angle(new Ax2d(center, new Vec2d(center, majorAxisPoint).ToDir()));

                    element.Transforms.Add(new SvgRotateTransform(rotAngle.ToDeg(), element.Center));

                    _Group.Children.Add(element);
                    return;
                }
            }
            else
            {
                Messages.Warning($"Unconnectable segment {_Sketch.Segments.FirstOrDefault(x => x.Value == sketchSegment).Key} of type {sketchSegment.GetType().Name} does not have a method to be exported to a SVG element.");
            }
            Messages.Warning($"Unconnectable segment {_Sketch.Segments.FirstOrDefault(x => x.Value == sketchSegment).Key} of type {sketchSegment.GetType().Name} could not be exported to SVG element.");
        }
Esempio n. 4
0
        internal virtual bool Read(XmlReader reader, SvgConverter conv)
        {
            var transformString = reader.GetAttribute("transform");

            if (!transformString.IsNullOrEmpty())
            {
                Transforms.AddRange(SvgTransform.Create(transformString, conv));
            }

            conv.PushTransform(Transforms);
            var depth = reader.Depth;

            reader.Read();
            while (!reader.EOF && reader.Depth > depth)
            {
                if (reader.NodeType != XmlNodeType.Element)
                {
                    reader.Read();
                    continue;
                }

                SvgElement newElement = null;
                switch (reader.LocalName.ToLower())
                {
                case "g":
                    newElement = new SvgGroupElement();
                    break;

                case "circle":
                    newElement = new SvgCircleElement();
                    break;

                case "ellipse":
                    newElement = new SvgEllipseElement();
                    break;

                case "path":
                    newElement = new SvgPathElement();
                    break;

                case "line":
                    newElement = new SvgLineElement();
                    break;

                case "rect":
                    newElement = new SvgRectElement();
                    break;
                }

                if (newElement != null)
                {
                    if (newElement.Read(reader, conv))
                    {
                        Children.Add(newElement);
                    }
                }
                else
                {
                    reader.Skip();
                }
            }

            return(true);
        }