Class ImportSVG
- Namespace
- CADability
- Assembly
- CADability.dll
Reads simple SVG files and converts their elements (lines, rectangles, circles, ellipses, polylines, polygons and paths with Bézier and elliptical arc segments) into IGeoObjects. The transformations of nested elements are accumulated on a stack of ModOp2Ds; the virtual CreateXxx methods may be overridden in a derived class to customize the objects that are created. Use Import(string) to import a file.
public class ImportSVG
- Inheritance
-
ImportSVG
- Inherited Members
Constructors
ImportSVG()
Creates an importer with the identity transformation and an empty result list. Call Import(string) to read a file.
public ImportSVG()
Fields
listStack
The lists the created objects are collected in. The bottom entry collects the result of Import(string);
CreatePath(string, ModOp2D) pushes a list for each sub-path and pops it when the sub-path is complete, so the CreateXxx
methods always add to the list on top (listStack.Peek()). Overriding methods should do the same.
public Stack<GeoObjectList> listStack
Field Value
Methods
CreateCircle(float, float, float, ModOp2D)
Called for a circle element. Creates a full circle (an Ellipse) and adds it to the current list;
override it to create something else.
protected virtual void CreateCircle(float cx, float cy, float r, ModOp2D transform)
Parameters
cxfloatx coordinate of the center in SVG user units
cyfloaty coordinate of the center
rfloatthe radius
transformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
CreateCubicBezier(Vector2, Vector2, Vector2, Vector2, ModOp2D)
Called for the C and S commands of a path. Creates a cubic Bézier curve (a BSpline of degree
3 with the four points as poles) and adds it to the current list. When a control point coincides with an end point or
with the other control point, the curve is quadratic and CreateQuadraticBezier(Vector2, Vector2, Vector2, ModOp2D) is called instead.
protected virtual void CreateCubicBezier(ImportSVG.Vector2 start, ImportSVG.Vector2 control1, ImportSVG.Vector2 control2, ImportSVG.Vector2 end, ModOp2D transform)
Parameters
startImportSVG.Vector2the current point, start of the curve
control1ImportSVG.Vector2the first control point
control2ImportSVG.Vector2the second control point
endImportSVG.Vector2the end point of the curve
transformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
CreateEllipse(float, float, float, float, ModOp2D)
Called for an ellipse element. Creates an axis-parallel Ellipse and adds it to the current list;
override it to create something else.
protected virtual void CreateEllipse(float cx, float cy, float rx, float ry, ModOp2D transform)
Parameters
cxfloatx coordinate of the center in SVG user units
cyfloaty coordinate of the center
rxfloatthe radius in x direction
ryfloatthe radius in y direction
transformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
CreateEllipticalArc(Vector2, float, float, float, bool, bool, Vector2, ModOp2D)
Called for the A command of a path. Computes the center and the axes of the ellipse from the end point
parameterization of SVG (SVG 1.1, appendix F.6), creates the elliptical arc from start to
end and adds it to the current list. Radii that are too small to reach the end point are scaled up,
as the specification requires.
protected virtual void CreateEllipticalArc(ImportSVG.Vector2 start, float frx, float fry, float xAxisRotation, bool largeArcFlag, bool sweepFlag, ImportSVG.Vector2 end, ModOp2D transform)
Parameters
startImportSVG.Vector2the current point, start of the arc
frxfloatthe radius in x direction (before the rotation)
fryfloatthe radius in y direction (before the rotation)
xAxisRotationfloatthe rotation of the x axis of the ellipse in degrees
largeArcFlagbooltrue to take the arc of more than 180° of the two candidates
sweepFlagbooltrue to run in the direction of increasing angles (clockwise on the screen, as the y axis of SVG points downwards)
endImportSVG.Vector2the end point of the arc
transformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
CreateLine(float, float, float, float, ModOp2D)
Called for a line element, for the straight segments of a path and for its closing segment (Z). Creates a
Line in the x/y plane and adds it to the current list; override it to create something else.
protected virtual void CreateLine(float x1, float y1, float x2, float y2, ModOp2D transform)
Parameters
x1floatx coordinate of the start point in SVG user units
y1floaty coordinate of the start point
x2floatx coordinate of the end point
y2floaty coordinate of the end point
transformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
CreatePath(string, ModOp2D)
Called for a path element. Parses the path data (the commands M, L, H, V, C, S, Q, T, A and Z, absolute and
relative) and calls CreateLine(float, float, float, float, ModOp2D), CreateCubicBezier(Vector2, Vector2, Vector2, Vector2, ModOp2D), CreateQuadraticBezier(Vector2, Vector2, Vector2, ModOp2D) and
CreateEllipticalArc(Vector2, float, float, float, bool, bool, Vector2, ModOp2D) for the segments. The segments of each sub-path (started by M) are then joined to a
Path: when the element has a fill style, a planar Face in that colour is made from
the closed outline, and the path itself is added only when there is a stroke style too; without a fill the path
is added. Nothing is created for empty path data.
protected virtual void CreatePath(string data, ModOp2D transform)
Parameters
datastringthe
dattribute of the elementtransformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
CreatePolygon(string, ModOp2D)
Called for a polygon element. Parses the points attribute (x/y pairs separated by commas or white space),
creates a closed Polyline (the first point is repeated at the end) and adds it to the current list;
override it to create something else.
protected virtual void CreatePolygon(string points, ModOp2D transform)
Parameters
pointsstringthe
pointsattribute of the elementtransformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
CreatePolyline(string, ModOp2D)
Called for a polyline element. Parses the points attribute (x/y pairs separated by commas or white space),
creates an open Polyline and adds it to the current list; override it to create something else.
protected virtual void CreatePolyline(string points, ModOp2D transform)
Parameters
pointsstringthe
pointsattribute of the elementtransformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
CreateQuadraticBezier(Vector2, Vector2, Vector2, ModOp2D)
Called for the Q and T commands of a path. Creates a quadratic Bézier curve (a BSpline of
degree 2 with the three points as poles) and adds it to the current list; override it to create something else.
protected virtual void CreateQuadraticBezier(ImportSVG.Vector2 start, ImportSVG.Vector2 control, ImportSVG.Vector2 end, ModOp2D transform)
Parameters
startImportSVG.Vector2the current point, start of the curve
controlImportSVG.Vector2the control point
endImportSVG.Vector2the end point of the curve
transformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
CreateRect(float, float, float, float, ModOp2D)
Called for a rect element. Creates a closed Polyline through the four corners (rounded corners are
ignored) and adds it to the current list; override it to create something else.
protected virtual void CreateRect(float x, float y, float width, float height, ModOp2D transform)
Parameters
xfloatx coordinate of the corner with the smallest coordinates, in SVG user units
yfloaty coordinate of that corner
widthfloatextent in x
heightfloatextent in y
transformModOp2Dthe transformation from the local coordinates of the element to the coordinates of the drawing; it is applied to the curve before the curve is added
EnterGroup(ModOp2D, Dictionary<string, string>)
Called when a g element is entered, before its children are processed. Does nothing; override it to handle
group properties, e.g. to start a Block that collects the children, and override ExitGroup()
to finish it.
protected virtual void EnterGroup(ModOp2D transform, Dictionary<string, string> styles)
Parameters
transformModOp2Dthe accumulated transformation of the group, including those of all enclosing elements
stylesDictionary<string, string>the style declarations of the group element (name to value, case-insensitive names); may be empty
ExitGroup()
Called when a g element has been processed completely, after EnterGroup(ModOp2D, Dictionary<string, string>) and the children. Does
nothing; override it together with EnterGroup(ModOp2D, Dictionary<string, string>).
protected virtual void ExitGroup()
Import(string)
Imports the SVG file and calls the corresponding CreateXxx method for each element found. The result is mirrored at a horizontal axis through its center, because the y axis of SVG points downwards.
public GeoObjectList Import(string fileName)
Parameters
fileNamestringpath of the SVG file
Returns
- GeoObjectList
the imported objects, or null if the file could not be read
ParseSvgColor(string)
Converts an SVG colour value to a Color: the #rgb and #rrggbb notations,
rgb(r,g,b) and rgba(r,g,b,a) with absolute or percent components, and the named colours of HTML.
public static Color ParseSvgColor(string value)
Parameters
valuestringthe colour text, e.g. the value of a
fillorstrokestyle
Returns
- Color
the colour, or Empty for null, an empty string,
noneand an unknown colour name; malformedrgb()components throw a FormatException