//-------------------------------------------------------------------------------------------------- #endregion #region Paths protected void InitPathExport() { CurrentPath = new SvgPathElement(); _PathStart = new Pnt2d(); _PathPosition = new Pnt2d(); _SubPathStartIndex = 0; }
//-------------------------------------------------------------------------------------------------- protected SvgPathElement FinalizePath() { ClosePath(); SvgPathElement path = CurrentPath; if (path?.Segments.Count > 0) { CurrentGroup.Children.Add(path); } CurrentPath = null; return(path); }
//-------------------------------------------------------------------------------------------------- void _ImportPath(SvgPathElement path) { if (path.Segments.Count < 1) { return; } int startPoint = -1; int firstPoint = -1; foreach (var pathSegment in path.Segments) { switch (pathSegment) { case SvgPathSegMoveto moveToSegment: startPoint = _AddPoint(pathSegment.Start); if (firstPoint == -1) { firstPoint = startPoint; } break; case SvgPathSegLineto lineSegment: _ImportLineSegment(lineSegment, ref startPoint); break; case SvgPathSegClosePath closePathSegment: _ImportClosePathSegment(closePathSegment, ref startPoint, ref firstPoint); break; case SvgPathSegArc arcSegment: _ImportArcSegment(arcSegment, ref startPoint); break; case SvgPathSegCurvetoCubic cubicCurveSegment: _ImportCubicCurveSegment(cubicCurveSegment, ref startPoint); break; case SvgPathSegCurvetoQuadratic quadraticCurveSegment: _ImportQuadraticCurveSegment(quadraticCurveSegment, ref startPoint); break; default: var endPoint = _AddPoint(pathSegment.End); startPoint = endPoint; Messages.Warning("Imported SVG shape does have an unsupported path segment type " + pathSegment.GetType().Name + ". [ " + path.ID + " ]"); break; } } }
//-------------------------------------------------------------------------------------------------- protected void ClosePath() { SvgPathElement path = CurrentPath; if (path?.Segments.Count > 0) { if (_PathPosition != Pnt2d.Origin && _AreEqual(_PathStart, _PathPosition)) { path.Segments.Add(new SvgPathSegClosePath()); } } _PathPosition = new Pnt2d(); _PathStart = new Pnt2d(); }
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); }
//-------------------------------------------------------------------------------------------------- MemoryStream _Export() { _Group = new SvgGroupElement { ID = _Sketch.Name, Style = new SvgStyle { StrokeWidth = 0.1f, StrokeColor = Color.Black, FillColor = null } }; var freeSegments = new Dictionary <int, SketchSegment>(_Sketch.Segments); while (freeSegments.Any()) { var currentSegmentKvp = freeSegments.First(); freeSegments.Remove(currentSegmentKvp.Key); var currentSegment = currentSegmentKvp.Value; if ((currentSegment.StartPoint == -1) || (currentSegment.EndPoint == -1)) { // Unconnected segment _AddUnconnectableSegment(currentSegment); continue; } // Start path _CurrentPath = new SvgPathElement(); _FirstPathEndPoint = -1; _LastPathEndPoint = -1; // search for connected while (freeSegments.Any()) { currentSegmentKvp = freeSegments.FirstOrDefault(kvp => kvp.Value.IsConnected(currentSegment)); if (currentSegmentKvp.Value == null) { // disconnected segment break; } if (_FirstPathEndPoint == -1) { // First connection, get winding order and write the move to start position if ((currentSegment.EndPoint == currentSegmentKvp.Value.StartPoint) || (currentSegment.EndPoint == currentSegmentKvp.Value.EndPoint)) { var startPoint = _Sketch.Points[currentSegment.StartPoint]; _CurrentPath.Segments.Add(new SvgPathSegMoveto(startPoint)); _LastPathEndPoint = currentSegment.StartPoint; _FirstPathEndPoint = currentSegment.StartPoint; } else { var startPoint = _Sketch.Points[currentSegment.EndPoint]; _CurrentPath.Segments.Add(new SvgPathSegMoveto(startPoint)); _LastPathEndPoint = currentSegment.EndPoint; _FirstPathEndPoint = currentSegment.EndPoint; } // add first element if (!_AddPathSegment(currentSegment)) { break; } } // Next connected, write currentSegment = currentSegmentKvp.Value; freeSegments.Remove(currentSegmentKvp.Key); if (!_AddPathSegment(currentSegment)) { break; } } if (_LastPathEndPoint == -1) { // Standalone element, write as single segment var startPoint = _Sketch.Points[currentSegment.StartPoint]; _CurrentPath.Segments.Add(new SvgPathSegMoveto(startPoint)); _AddPathSegment(currentSegment); } // Close segment, if needed if (_FirstPathEndPoint == _LastPathEndPoint) { _CurrentPath.Segments.Add(new SvgPathSegClosePath()); } // Add to group _Group.Children.Add(_CurrentPath); } var doc = new SvgDocument { Width = 100, Height = 100, DotsPerInch = DotsPerInch }; doc.Children.Add(_Group); return(doc.WriteToStream()); }