Table of Contents

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

Stack<GeoObjectList>

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

cx float

x coordinate of the center in SVG user units

cy float

y coordinate of the center

r float

the radius

transform ModOp2D

the 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

start ImportSVG.Vector2

the current point, start of the curve

control1 ImportSVG.Vector2

the first control point

control2 ImportSVG.Vector2

the second control point

end ImportSVG.Vector2

the end point of the curve

transform ModOp2D

the 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

cx float

x coordinate of the center in SVG user units

cy float

y coordinate of the center

rx float

the radius in x direction

ry float

the radius in y direction

transform ModOp2D

the 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

start ImportSVG.Vector2

the current point, start of the arc

frx float

the radius in x direction (before the rotation)

fry float

the radius in y direction (before the rotation)

xAxisRotation float

the rotation of the x axis of the ellipse in degrees

largeArcFlag bool

true to take the arc of more than 180° of the two candidates

sweepFlag bool

true to run in the direction of increasing angles (clockwise on the screen, as the y axis of SVG points downwards)

end ImportSVG.Vector2

the end point of the arc

transform ModOp2D

the 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

x1 float

x coordinate of the start point in SVG user units

y1 float

y coordinate of the start point

x2 float

x coordinate of the end point

y2 float

y coordinate of the end point

transform ModOp2D

the 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

data string

the d attribute of the element

transform ModOp2D

the 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

points string

the points attribute of the element

transform ModOp2D

the 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

points string

the points attribute of the element

transform ModOp2D

the 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

start ImportSVG.Vector2

the current point, start of the curve

control ImportSVG.Vector2

the control point

end ImportSVG.Vector2

the end point of the curve

transform ModOp2D

the 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

x float

x coordinate of the corner with the smallest coordinates, in SVG user units

y float

y coordinate of that corner

width float

extent in x

height float

extent in y

transform ModOp2D

the 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

transform ModOp2D

the accumulated transformation of the group, including those of all enclosing elements

styles Dictionary<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

fileName string

path 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

value string

the colour text, e.g. the value of a fill or stroke style

Returns

Color

the colour, or Empty for null, an empty string, none and an unknown colour name; malformed rgb() components throw a FormatException