Spline¶
- class ezdxf.render.Spline(points: Iterable[UVec] | None = None, segments: int = 100)¶
This class can be used to render B-splines into DXF R12 files as approximated
Polyline
entities. The advantage of this class over theR12Spline
class is, that this is a real 3D curve, which means that the B-spline vertices do have to be located in a flat plane, and no UCS class is needed to place the curve in 3D space.See also
The newer
BSpline
class provides the advanced vertex interpolation methodflattening()
.- __init__(points: Iterable[UVec] | None = None, segments: int = 100)¶
- Parameters:
points – spline definition points
segments – count of line segments for approximation, vertex count is segments + 1
- subdivide(segments: int = 4) None ¶
Calculate overall segment count, where segments is the sub-segment count, segments = 4, means 4 line segments between two definition points e.g. 4 definition points and 4 segments = 12 overall segments, useful for fit point rendering.
- Parameters:
segments – sub-segments count between two definition points
- render_as_fit_points(layout: BaseLayout, degree: int = 3, method: str = 'chord', dxfattribs: dict | None = None) None ¶
Render a B-spline as 2D/3D
Polyline
, where the definition points are fit points.2D spline vertices uses:
add_polyline2d()
3D spline vertices uses:
add_polyline3d()
- Parameters:
layout –
BaseLayout
objectdegree – degree of B-spline (order = degree + 1)
method – “uniform”, “distance”/”chord”, “centripetal”/”sqrt_chord” or “arc” calculation method for parameter t
dxfattribs – DXF attributes for
Polyline
- render_open_bspline(layout: BaseLayout, degree: int = 3, dxfattribs=None) None ¶
Render an open uniform B-spline as 3D
Polyline
. Definition points are control points.- Parameters:
layout –
BaseLayout
objectdegree – degree of B-spline (order = degree + 1)
dxfattribs – DXF attributes for
Polyline
- render_uniform_bspline(layout: BaseLayout, degree: int = 3, dxfattribs=None) None ¶
Render a uniform B-spline as 3D
Polyline
. Definition points are control points.- Parameters:
layout –
BaseLayout
objectdegree – degree of B-spline (order = degree + 1)
dxfattribs – DXF attributes for
Polyline
- render_closed_bspline(layout: BaseLayout, degree: int = 3, dxfattribs=None) None ¶
Render a closed uniform B-spline as 3D
Polyline
. Definition points are control points.- Parameters:
layout –
BaseLayout
objectdegree – degree of B-spline (order = degree + 1)
dxfattribs – DXF attributes for
Polyline
- render_open_rbspline(layout: BaseLayout, weights: Iterable[float], degree: int = 3, dxfattribs=None) None ¶
Render a rational open uniform BSpline as 3D
Polyline
. Definition points are control points.- Parameters:
layout –
BaseLayout
objectweights – list of weights, requires a weight value (float) for each definition point.
degree – degree of B-spline (order = degree + 1)
dxfattribs – DXF attributes for
Polyline
- render_uniform_rbspline(layout: BaseLayout, weights: Iterable[float], degree: int = 3, dxfattribs=None) None ¶
Render a rational uniform B-spline as 3D
Polyline
. Definition points are control points.- Parameters:
layout –
BaseLayout
objectweights – list of weights, requires a weight value (float) for each definition point.
degree – degree of B-spline (order = degree + 1)
dxfattribs – DXF attributes for
Polyline
- render_closed_rbspline(layout: BaseLayout, weights: Iterable[float], degree: int = 3, dxfattribs=None) None ¶
Render a rational B-spline as 3D
Polyline
. Definition points are control points.- Parameters:
layout –
BaseLayout
objectweights – list of weights, requires a weight value (float) for each definition point.
degree – degree of B-spline (order = degree + 1)
dxfattribs – DXF attributes for
Polyline
R12Spline¶
- class ezdxf.render.R12Spline(control_points: Iterable[UVec], degree: int = 2, closed: bool = True)¶
DXF R12 supports 2D B-splines, but Autodesk do not document the usage in the DXF Reference. The base entity for splines in DXF R12 is the POLYLINE entity. The spline itself is always in a plane, but as any 2D entity, the spline can be transformed into the 3D object by elevation and extrusion (OCS, UCS).
This way it was possible to store the spline parameters in the DXF R12 file, to allow CAD applications to modify the spline parameters and rerender the B-spline afterward again as polyline approximation. Therefore, the result is not better than an approximation by the
Spline
class, it is also just a POLYLINE entity, but maybe someone need exact this tool in the future.- __init__(control_points: Iterable[UVec], degree: int = 2, closed: bool = True)¶
- Parameters:
control_points – B-spline control frame vertices
degree – degree of B-spline, only 2 and 3 is supported
closed –
True
for closed curve
- render(layout: BaseLayout, segments: int = 40, ucs: UCS | None = None, dxfattribs=None) Polyline ¶
Renders the B-spline into layout as 2D
Polyline
entity. Use anUCS
to place the 2D spline in the 3D space, seeapproximate()
for more information.- Parameters:
layout –
BaseLayout
objectsegments – count of line segments for approximation, vertex count is segments + 1
ucs –
UCS
definition, control points in ucs coordinates.dxfattribs – DXF attributes for
Polyline
- approximate(segments: int = 40, ucs: UCS | None = None) list[UVec] ¶
Approximate the B-spline by a polyline with segments line segments. If ucs is not
None
, ucs defines anUCS
, to transform the curve into OCS. The control points are placed xy-plane of the UCS, don’t use z-axis coordinates, if so make sure all control points are in a plane parallel to the OCS base plane (UCS xy-plane), else the result is unpredictable and depends on the CAD application used to open the DXF file - it may crash.
Bezier¶
- class ezdxf.render.Bezier¶
Render a bezier curve as 2D/3D
Polyline
.The
Bezier
class is implemented with multiple segments, each segment is an optimized 4 point bezier curve, the 4 control points of the curve are: the start point (1) and the end point (4), point (2) is start point + start vector and point (3) is end point + end vector. Each segment has its own approximation count.See also
The new
ezdxf.path
package provides many advanced construction tools based on thePath
class.- start(point: UVec, tangent: UVec) None ¶
Set start point and start tangent.
- Parameters:
point – start point
tangent – start tangent as vector, example: (5, 0, 0) means a horizontal tangent with a length of 5 drawing units
- append(point: UVec, tangent1: UVec, tangent2: UVec | None = None, segments: int = 20)¶
Append a control point with two control tangents.
- Parameters:
point – control point
tangent1 – first tangent as vector “left” of the control point
tangent2 – second tangent as vector “right” of the control point, if omitted tangent2 = -tangent1
segments – count of line segments for the polyline approximation, count of line segments from the previous control point to the appended control point.
- render(layout: BaseLayout, force3d: bool = False, dxfattribs=None) None ¶
Render Bezier curve as 2D/3D
Polyline
.- Parameters:
layout –
BaseLayout
objectforce3d – force 3D polyline rendering
dxfattribs – DXF attributes for
Polyline
EulerSpiral¶
- class ezdxf.render.EulerSpiral(curvature: float = 1)¶
Render an euler spiral as a 3D
Polyline
or aSpline
entity.This is a parametric curve, which always starts at the origin (0, 0).
- __init__(curvature: float = 1)¶
- Parameters:
curvature – Radius of curvature
- render_polyline(layout: BaseLayout, length: float = 1, segments: int = 100, matrix: Matrix44 | None = None, dxfattribs=None)¶
Render curve as
Polyline
.- Parameters:
layout –
BaseLayout
objectlength – length measured along the spiral curve from its initial position
segments – count of line segments to use, vertex count is segments + 1
matrix – transformation matrix as
Matrix44
dxfattribs – DXF attributes for
Polyline
- Returns:
- render_spline(layout: BaseLayout, length: float = 1, fit_points: int = 10, degree: int = 3, matrix: Matrix44 | None = None, dxfattribs=None)¶
Render curve as
Spline
.- Parameters:
layout –
BaseLayout
objectlength – length measured along the spiral curve from its initial position
fit_points – count of spline fit points to use
degree – degree of B-spline
matrix – transformation matrix as
Matrix44
dxfattribs – DXF attributes for
Spline
- Returns:
Random Paths¶
Random path generators for testing purpose.
- ezdxf.render.random_2d_path(steps: int = 100, max_step_size: float = 1.0, max_heading: float = math.pi / 2, retarget: int = 20) Iterable[Vec2] ¶
Returns a random 2D path as iterable of
Vec2
objects.- Parameters:
steps – count of vertices to generate
max_step_size – max step size
max_heading – limit heading angle change per step to ± max_heading/2 in radians
retarget – specifies steps before changing global walking target
- ezdxf.render.random_3d_path(steps: int = 100, max_step_size: float = 1.0, max_heading: float = math.pi / 2.0, max_pitch: float = math.pi / 8.0, retarget: int = 20) Iterable[Vec3] ¶
Returns a random 3D path as iterable of
Vec3
objects.- Parameters:
steps – count of vertices to generate
max_step_size – max step size
max_heading – limit heading angle change per step to ± max_heading/2, rotation about the z-axis in radians
max_pitch – limit pitch angle change per step to ± max_pitch/2, rotation about the x-axis in radians
retarget – specifies steps before changing global walking target