Hatch

class Hatch

Introduced in DXF version R13 (AC1012), dxftype is HATCH.

Fills an enclosed area defined by one or more boundary paths with a hatch pattern, solid fill, or gradient fill. Create Hatch in layouts and blocks by factory function add_hatch().

All points in OCS as (x, y) tuples (elevation is the z-axis value).

DXF Attributes for Hatch

Common DXF Attributes for DXF R13 or later

Hatch.dxf.pattern_name

pattern name as string

Hatch.dxf.solid_fill
Hatch.dxf.associative
  • 1 = associative hatch
  • 0 = not associative hatch

Associations not handled by ezdxf, you have to set the handles to the associated DXF entities by yourself.

Hatch.dxf.hatch_style
  • 0 = normal
  • 1 = outer
  • 2 = ignore

(search for AutoCAD help for more information)

Hatch.dxf.pattern_type
  • 0 = user
  • 1 = predefined
  • 2 = custom (???)
Hatch.dxf.pattern_angle

Pattern angle in degrees (360 deg = circle)

Hatch.dxf.pattern_scale
Hatch.dxf.pattern_double

1 = double else 0

Hatch.dxf.n_seed_points

Count of seed points (better user: Hatch.get_seed_points())

Hatch.dxf.elevation

Z value represents the elevation height of the OCS

Hatch Attributes

Hatch.has_solid_fill

True if hatch has a solid fill else False. (read only)

Hatch.has_pattern_fill

True if hatch has a pattern fill else False. (read only)

Hatch.has_gradient_fill

True if hatch has a gradient fill else False. A hatch with gradient fill has also a solid fill. (read only)

Hatch.bgcolor

Property background color as (r, g, b) tuple, rgb values in range 0..255 (read/write/del)

usage:

color = hatch.bgcolor  # get background color as (r, g, b) tuple
hatch.bgcolor = (10, 20, 30)  # set background color
del hatch.bgcolor  # delete background color
Hatch.edit_boundary()

Context manager to edit hatch boundary data, yields a BoundaryPathData object.

Hatch.edit_pattern()

Context manager to edit hatch pattern data, yields a PatternData object.

Hatch.set_pattern_definition(lines)

Setup hatch pattern definition by a list of definition lines and a definition line is a 4-tuple [angle, base_point, offset, dash_length_items]

  • angle: line angle in degrees
  • base-point: (x, y) tuple
  • offset: (dx, dy) tuple, added to base point for next line and so on
  • dash_length_items: list of dash items (item > 0 is a line, item < 0 is a gap and item == 0.0 is a point)
Parameters:lines (list) – list of definition lines
Hatch.set_solid_fill(color=7, style=1, rgb=None)

Set Hatch to solid fill mode and removes all gradient and pattern fill related data.

Parameters:
  • color (int) – ACI (AutoCAD Color Index) in range 0 to 256, (0 = BYBLOCK; 256 = BYLAYER)
  • style (int) – hatch style (0 = normal; 1 = outer; 2 = ignore)
  • rgb (tuple) – true color value as (r, g, b) tuple - has higher priority than color. True color support requires at least DXF version AC1015.
Hatch.set_gradient(color1=(0, 0, 0), color2=(255, 255, 255), rotation=0., centered=0., one_color=0, tint=0., name='LINEAR')

Set Hatch to gradient fill mode and removes all pattern fill related data. Gradient support requires at least DXF version AC1018. A gradient filled hatch is also a solid filled hatch.

Parameters:
  • color1 (tuple) – (r, g, b) tuple for first color, rgb values as int in range 0..255
  • color2 (tuple) – (r, g, b) tuple for second color, rgb values as int in range 0..255
  • rotation (float) – rotation in degrees (360 deg = circle)
  • centered (int) – determines whether the gradient is centered or not
  • one_color (int) – 1 for gradient from color1 to tinted color1
  • tint (float) – determines the tinted target color1 for a one color gradient. (valid range 0.0 to 1.0)
  • name (str) – name of gradient type, default ‘LINEAR’

Valid gradient type names are:

  • LINEAR
  • CYLINDER
  • INVCYLINDER
  • SPHERICAL
  • INVSPHERICAL
  • HEMISPHERICAL
  • INVHEMISPHERICAL
  • CURVED
  • INVCURVED
Hatch.get_gradient()

Get gradient data, returns a GradientData object.

Hatch.edit_gradient()

Context manager to edit hatch gradient data, yields a GradientData object.

Hatch.set_pattern_fill(name, color=7, angle=0., scale=1., double=0, style=1, pattern_type=1, definition=None)

Set Hatch to pattern fill mode. Removes all gradient related data.

Parameters:
  • color (int) – AutoCAD Color Index in range 0 to 256, (0 = BYBLOCK; 256 = BYLAYER)
  • angle (float) – angle of pattern fill in degrees (360 deg = circle)
  • scale (float) – pattern scaling
  • double (int) – double flag
  • style (int) – hatch style (0 = normal; 1 = outer; 2 = ignore)
  • pattern_type (int) – pattern type (0 = user-defined; 1 = predefined; 2 = custom) ???
  • definition (list) – list of definition lines and a definition line is a 4-tuple [angle, base_point, offset, dash_length_items], see Hatch.set_pattern_definition()
Hatch.get_seed_points()

Get seed points as list of (x, y) points, I don’t know why there can be more than one seed point. All points in OCS (elevation is the Z value).

Hatch.set_seed_points(points)

Set seed points, points is a list of (x, y) tuples, I don’t know why there can be more than one seed point. All points in OCS (elevation is the Z value)

Hatch Boundary Helper Classes

class BoundaryPathData

Defines the borders of the hatch, a hatch can consist of more than one path.

BoundaryPathData.paths

List of all boundary paths. Contains PolylinePath and EdgePath objects. (read/write)

BoundaryPathData.add_polyline_path(path_vertices, is_closed=1, flags=1)

Create and add a new PolylinePath object.

Parameters:
  • path_vertices (list) – list of polyline vertices as (x, y) or (x, y, bulge) tuples.
  • is_closed (int) – 1 for a closed polyline else 0
  • flags (int) – external(1) or outermost(16) or default (0)
BoundaryPathData.add_edge_path(flags=1)

Create and add a new EdgePath object.

Parameters:flags (int) – external(1) or outermost(16) or default (0)
BoundaryPathData.clear()

Remove all boundary paths.

class PolylinePath

A polyline as hatch boundary path.

PolylinePath.path_type_flags

(bit coded flags)

  • 0 = default
  • 1 = external
  • 2 = polyline, will be set by ezdxf
  • 16 = outermost

My interpretation of the path_type_flags, see also Tutorial for Hatch:

  • external - path is part of the hatch outer border
  • outermost - path is completely inside of one or more external paths
  • default - path is completely inside of one or more outermost paths

If there are troubles with AutoCAD, maybe the hatch entity contains the pixel size tag (47) - delete it hatch.AcDbHatch.remove_tags([47]) and maybe the problem is solved. ezdxf does not use the pixel size tag, but it can occur in DXF files created by other applications.

PolylinePath.is_closed

True if polyline path is closed else False.

PolylinePath.vertices

List of path vertices as (x, y, bulge) tuples. (read/write)

PolylinePath.source_boundary_objects

List of handles of the associated DXF entities for associative hatches. There is no support for associative hatches by ezdxf you have to do it all by yourself. (read/write)

PolylinePath.set_vertices(vertices, is_closed=1)

Set new vertices for the polyline path, a vertex has to be a (x, y) or a (x, y, bulge) tuple.

PolylinePath.clear()

Removes all vertices and all links to associated DXF objects (PolylinePath.source_boundary_objects).

class EdgePath

Boundary path build by edges. There are four different edge types: LineEdge, ArcEdge, EllipseEdge of SplineEdge. Make sure there are no gaps between edges. AutoCAD in this regard is very picky. ezdxf performs no checks on gaps between the edges.

EdgePath.path_type_flags

(bit coded flags)

  • 0 = default
  • 1 = external
  • 16 = outermost

see PolylinePath.path_type_flags

EdgePath.edges

List of boundary edges of type LineEdge, ArcEdge, EllipseEdge of SplineEdge

EdgePath.source_boundary_objects

Required for associative hatches, list of handles to the associated DXF entities.

EdgePath.clear()

Delete all edges.

EdgePath.add_line(start, end)

Add a LineEdge from start to end.

Parameters:
  • start (tuple) – start point of line, (x, y) tuple
  • end (tuple) – end point of line, (x, y) tuple
EdgePath.add_arc(center, radius=1., start_angle=0., end_angle=360., is_counter_clockwise=0)

Add an ArcEdge.

Parameters:
  • center (tuple) – center point of arc, (x, y) tuple
  • radius (float) – radius of circle
  • start_angle (float) – start angle of arc in degrees
  • end_angle (float) – end angle of arc in degrees
  • is_counter_clockwise (int) – 1 for yes 0 for no
EdgePath.add_ellipse(center, major_axis_vector=(1., 0.), minor_axis_length=1., start_angle=0., end_angle=360., is_counter_clockwise=0)

Add an EllipseEdge.

Parameters:
  • center (tuple) – center point of ellipse, (x, y) tuple
  • major_axis (tuple) – vector of major axis as (x, y) tuple
  • ratio (float) – ratio of minor axis to major axis as float
  • start_angle (float) – start angle of ellipse in degrees
  • end_angle (float) – end angle of ellipse in degrees
  • is_counter_clockwise (int) – 1 for yes 0 for no
EdgePath.add_spline(fit_points=None, control_points=None, knot_values=None, weights=None, degree=3, rational=0, periodic=0)

Add a SplineEdge.

Parameters:
  • fit_points (list) – points through which the spline must go, at least 3 fit points are required. list of (x, y) tuples
  • control_points (list) – affects the shape of the spline, mandatory amd AutoCAD crashes on invalid data. list of (x, y) tuples
  • knot_values (list) – (knot vector) mandatory and AutoCAD crashes on invalid data. list of floats; ezdxf provides two tool functions to calculate valid knot values: ezdxf.tools.knot_values(n_control_points, degree) and ezdxf.tools.knot_values_uniform(n_control_points, degree)
  • weights (list) – weight of control point, not mandatory, list of floats.
  • degree (int) – degree of spline
  • rational (int) – 1 for rational spline, 0 for none rational spline
  • periodic (int) – 1 for periodic spline, 0 for none periodic spline

Warning

Unlike for the spline entity AutoCAD does not calculate the necessary knot_values for the spline edge itself. On the contrary, if the knot_values in the spline edge are missing or invalid AutoCAD crashes.

class LineEdge

Straight boundary edge.

LineEdge.start

Start point as (x, y) tuple. (read/write)

LineEdge.end

End point as (x, y) tuple. (read/write)

class ArcEdge

Arc as boundary edge.

ArcEdge.center

Center point of arc as (x, y) tuple. (read/write)

ArcEdge.radius

Arc radius as float. (read/write)

ArcEdge.start_angle

Arc start angle in degrees (360 deg = circle). (read/write)

ArcEdge.end_angle

Arc end angle in degrees (360 deg = circle). (read/write)

ArcEdge.is_counter_clockwise

1 for counter clockwise arc else 0. (read/write)

class EllipseEdge

Elliptic arc as boundary edge.

EllipseEdge.major_axis_vector

Ellipse major axis vector as (x, y) tuple. (read/write)

EllipseEdge.minor_axis_length

Ellipse minor axis length as float. (read/write)

EllipseEdge.radius

Ellipse radius as float. (read/write)

EllipseEdge.start_angle

Ellipse start angle in degrees (360 deg = circle). (read/write)

EllipseEdge.end_angle

Ellipse end angle in degrees (360 deg = circle). (read/write)

EllipseEdge.is_counter_clockwise

1 for counter clockwise ellipse else 0. (read/write)

class SplineEdge

Spline as boundary edge.

SplineEdge.degree

Spline degree as int. (read/write)

SplineEdge.rational

1 for rational spline else 0. (read/write)

SplineEdge.periodic

1 for periodic spline else 0. (read/write)

SplineEdge.knot_values

List of knot values as floats. (read/write)

SplineEdge.control_points

List of control points as (x, y) tuples. (read/write)

SplineEdge.fit_points

List of fit points as (x, y) tuples. (read/write)

SplineEdge.weights

List of weights (of control points) as floats. (read/write)

SplineEdge.start_tangent

Spline start tangent (vector) as (x, y) tuple. (read/write)

SplineEdge.end_tangent

Spline end tangent (vector) as (x, y) tuple. (read/write)

Hatch Pattern Definition Helper Classes

class PatternData
PatternData.lines

List of pattern definition lines (read/write). see PatternDefinitionLine

PatternData.new_line(angle=0., base_point=(0., 0.), offset=(0., 0.), dash_length_items=None)

Create a new pattern definition line, but does not add the line to the PatternData.lines attribute.

PatternData.add_line(angle=0., base_point=(0., 0.), offset=(0., 0.), dash_length_items=None)

Create a new pattern definition line and add the line to the PatternData.lines attribute.

PatternData.clear()

Delete all pattern definition lines.

class PatternDefinitionLine

Represents a pattern definition line, use factory function PatternData.new_line() to create new pattern definition lines.

PatternDefinitionLine.angle

Line angle in degrees (circle = 360 deg). (read/write)

PatternDefinitionLine.base_point

Base point as (x, y) tuple. (read/write)

PatternDefinitionLine..offset

Offset as (x, y) tuple. (read/write)

PatternDefinitionLine.dash_length_items

List of dash length items (item > 0 is line, < 0 is gap, 0.0 = dot). (read/write)

Hatch Gradient Fill Helper Classes

class GradientData
GradientData.color1

First rgb color as (r, g, b) tuple, rgb values in range 0 to 255. (read/write)

GradientData.color2

Second rgb color as (r, g, b) tuple, rgb values in range 0 to 255. (read/write)

GradientData.one_color

If one_color is 1 - the hatch is filled with a smooth transition between color1 and a specified tint of color1. (read/write)

GradientData.rotation

Gradient rotation in degrees (circle = 360 deg). (read/write)

GradientData.centered

Specifies a symmetrical gradient configuration. If this option is not selected, the gradient fill is shifted up and to the left, creating the illusion of a light source to the left of the object. (read/write)

GradientData.tint

Specifies the tint (color1 mixed with white) of a color to be used for a gradient fill of one color. (read/write)