MPolygon

The MPOLYGON entity is not a core DXF entity and is not supported by all CAD applications and DXF libraries. The MPolygon class is very similar to the Hatch class with small differences in the supported features and DXF attributes.

The boundary paths of the MPOLYGON are visible and use the graphical DXF attributes of the main entity like dxf.color, dxf.linetype and so on. The solid filling is only visible if the attribute dxf.solid_fill is 1, the color of the solid fill is defined by dxf.fill_color as AutoCAD Color Index (ACI). The MPOLYGON supports ezdxf.entities.Gradient settings like HATCH for DXF R2004 and newer. This feature is used by method MPolygon.set_solid_fill() to set a solid RGB fill color as linear gradient, this disables pattern fill automatically. The MPOLYGON does not support associated source path entities, because the MPOLYGON also represents the boundary paths as visible graphical objects. Hatch patterns are supported, but the hatch style tag is not supported, the default hatch style is ezdxf.const.HATCH_STYLE_NESTED and the style flags of the boundary paths are ignored. Background color for pattern fillings is supported, set background color by property MPolygon.bgcolor as RGB tuple.

Note

Background RGB fill color for solid fill and pattern fill is set differently!

Autodesk products do support polyline paths including bulges. An example for edge paths as boundary paths is not available or edge paths are not supported. Ezdxf does not export MPOLYGON entities including edge paths! The BoundaryPaths.edge_to_polyline_paths() method converts all edge paths to simple polyline paths with approximated curves, this conversion has to be done explicit.

See also

For more information see the ezdxf.entities.Hatch documentation.

Subclass of

ezdxf.entities.DXFGraphic

DXF type

'MPOLYGON'

Factory function

ezdxf.layouts.BaseLayout.add_mpolygon()

Inherited DXF attributes

Common graphical DXF attributes

Required DXF version

DXF R2000 ('AC1015')

class ezdxf.entities.MPolygon
dxf.pattern_name

Pattern name as string

dxf.solid_fill

1

solid fill, better use: MPolygon.set_solid_fill()

0

pattern fill, better use: MPolygon.set_pattern_fill()

(search AutoCAD help for more information)

dxf.pattern_type

0

user

1

predefined

2

custom

dxf.pattern_angle

Actual pattern angle in degrees (float). Changing this value does not rotate the pattern, use set_pattern_angle() for this task.

dxf.pattern_scale

Actual pattern scaling factor (float). Changing this value does not scale the pattern use set_pattern_scale() for this task.

dxf.pattern_double

1 = double pattern size else 0. (int)

dxf.elevation

Z value represents the elevation height of the OCS. (float)

paths

BoundaryPaths object.

pattern

Pattern object.

gradient

Gradient object.

property has_solid_fill: bool

True if entity has a solid fill. (read only)

property has_pattern_fill: bool

True if entity has a pattern fill. (read only)

property has_gradient_data: bool

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

property bgcolor: RGB | None

Set pattern fill background color as (r, g, b)-tuple, rgb values in the range [0, 255] (read/write/del)

usage:

r, g, b = entity.bgcolor  # get pattern fill background color
entity.bgcolor = (10, 20, 30)  # set pattern fill background color
del entity.bgcolor  # delete pattern fill background color
set_pattern_definition(lines: Sequence, factor: float = 1, angle: float = 0) None

Setup pattern definition by a list of definition lines and the definition line is a 4-tuple (angle, base_point, offset, dash_length_items). The pattern definition should be designed for a pattern scale factor of 1 and a pattern rotation angle of 0.

  • angle: line angle in degrees

  • base-point: (x, y) tuple

  • offset: (dx, dy) tuple

  • 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 of definition lines

  • factor – pattern scale factor

  • angle – rotation angle in degrees

set_pattern_scale(scale: float) None

Sets the pattern scale factor and scales the pattern definition.

The method always starts from the original base scale, the set_pattern_scale(1) call resets the pattern scale to the original appearance as defined by the pattern designer, but only if the pattern attribute dxf.pattern_scale represents the actual scale, it cannot restore the original pattern scale from the pattern definition itself.

Parameters:

scale – pattern scale factor

set_pattern_angle(angle: float) None

Sets the pattern rotation angle and rotates the pattern definition.

The method always starts from the original base rotation of 0, the set_pattern_angle(0) call resets the pattern rotation angle to the original appearance as defined by the pattern designer, but only if the pattern attribute dxf.pattern_angle represents the actual pattern rotation, it cannot restore the original rotation angle from the pattern definition itself.

Parameters:

angle – pattern rotation angle in degrees

set_solid_fill(color: int = 7, style: int = 1, rgb: RGB | None = None)

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

Parameters:
  • colorAutoCAD Color Index (ACI), (0 = BYBLOCK; 256 = BYLAYER)

  • style – hatch style is not supported by MPOLYGON, just for symmetry to HATCH

  • rgb – true color value as (r, g, b)-tuple - has higher priority than color. True color support requires DXF R2004+

set_pattern_fill(name: str, color: int = 7, angle: float = 0.0, scale: float = 1.0, double: int = 0, style: int = 1, pattern_type: int = 1, definition=None) None

Sets the pattern fill mode and removes all gradient related data.

The pattern definition should be designed for a scale factor 1 and a rotation angle of 0 degrees. The predefined hatch pattern like “ANSI33” are scaled according to the HEADER variable $MEASUREMENT for ISO measurement (m, cm, … ), or imperial units (in, ft, …), this replicates the behavior of BricsCAD.

Parameters:
  • name – pattern name as string

  • color – pattern color as AutoCAD Color Index (ACI)

  • angle – pattern rotation angle in degrees

  • scale – pattern scale factor

  • double – double size flag

  • style – hatch style (0 = normal; 1 = outer; 2 = ignore)

  • pattern_type – pattern type (0 = user-defined; 1 = predefined; 2 = custom)

  • definition – list of definition lines and a definition line is a 4-tuple [angle, base_point, offset, dash_length_items], see set_pattern_definition()

set_gradient(color1: RGB = RGB(0, 0, 0), color2: RGB = RGB(255, 255, 255), rotation: float = 0.0, centered: float = 0.0, one_color: int = 0, tint: float = 0.0, name: str = 'LINEAR') None

Sets the gradient fill mode and removes all pattern fill related data, requires DXF R2004 or newer. A gradient filled hatch is also a solid filled hatch.

Valid gradient type names are:

  • “LINEAR”

  • “CYLINDER”

  • “INVCYLINDER”

  • “SPHERICAL”

  • “INVSPHERICAL”

  • “HEMISPHERICAL”

  • “INVHEMISPHERICAL”

  • “CURVED”

  • “INVCURVED”

Parameters:
  • color1 – (r, g, b)-tuple for first color, rgb values as int in the range [0, 255]

  • color2 – (r, g, b)-tuple for second color, rgb values as int in the range [0, 255]

  • rotation – rotation angle in degrees

  • centered – determines whether the gradient is centered or not

  • one_color – 1 for gradient from color1 to tinted color1

  • tint – determines the tinted target color1 for a one color gradient. (valid range 0.0 to 1.0)

  • name – name of gradient type, default “LINEAR”

transform(m: Matrix44) DXFPolygon

Transform entity by transformation matrix m inplace.