MPolygon

The MPOLYGON entity is not a core DXF entity and is not supported by every CAD application or DXF library.

The MPolygon class is very similar to the Hatch class with small differences in supported DXF attributes and features.

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).

MPOLYGON supports ezdxf.entities.Gradient settings like HATCH for DXF R2004+. 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: Optional[Tuple[int, int, int]]

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 a definition line is a 4-tuple (angle, base_point, offset, dash_length_items), the pattern definition should be designed for scaling factor 1 and angle 0.

  • angle: line angle in degrees

  • base-point: 2-tuple (x, y)

  • offset: 2-tuple (dx, dy)

  • 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 scaling factor

  • angle – rotation angle in degrees

set_pattern_scale(scale: float) None

Set scaling of pattern definition to scale.

Starts always from the original base scaling, set_pattern_scale(1) reset the pattern scaling to the original appearance as defined by the pattern designer, but only if the the pattern attribute dxf.pattern_scale represents the actual scaling, it is not possible to recreate the original pattern scaling from the pattern definition itself.

Parameters

scale – pattern scaling factor

set_pattern_angle(angle: float) None

Set rotation of pattern definition to angle in degrees.

Starts always from the original base rotation 0, set_pattern_angle(0) reset the pattern rotation to the original appearance as defined by the pattern designer, but only if the the pattern attribute dxf.pattern_angle represents the actual rotation, it is not possible to recreate the original rotation from the pattern definition itself.

Parameters

angle – rotation angle in degrees

set_solid_fill(color: int = 7, style: int = 1, rgb: RGB = 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

Set Hatch and MPolygon to pattern fill mode. Removes all gradient related data. The pattern definition should be designed for scaling factor 1. 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 – angle of pattern fill in degrees

  • scale – pattern scaling as float

  • 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: Tuple[int, int, int] = (0, 0, 0), color2: Tuple[int, int, int] = (255, 255, 255), rotation: float = 0.0, centered: float = 0.0, one_color: int = 0, tint: float = 0.0, name: str = 'LINEAR') None

Set Hatch and MPolygon to gradient fill mode and removes all pattern fill related data. Gradient support requires DXF R2004+. 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) MPolygon

Transform entity by transformation matrix m inplace.