GfxAttribs¶
The ezdxf.gfxattribs
module provides the GfxAttribs
class to
create valid attribute dictionaries for the most often used DXF attributes
supported by all graphical DXF entities. The advantage of using this class
is auto-completion support by IDEs and an instant validation of the attribute
values.
import ezdxf
from ezdxf.gfxattribs import GfxAttribs
doc = ezdxf.new()
msp = doc.modelspace()
attribs = GfxAttribs(layer="MyLayer", color=ezdxf.colors.RED)
line = msp.add_line((0, 0), (1, 0), dxfattribs=attribs)
circle = msp.add_circle((0, 0), radius=1.0, dxfattribs=attribs)
# Update DXF attributes of existing entities:
attribs = GfxAttribs(layer="MyLayer2", color=ezdxf.colors.BLUE)
# Convert GfxAttribs() to dict(), but this method cannot reset
# attributes to the default values like setting layer to "0".
line.update_dxf_attribs(dict(attribs))
# Using GfxAttribs.asdict(default_values=True), can reset attributes to the
# default values like setting layer to "0", except for true_color and
# transparency, which do not have default values, their absence is the
# default value.
circle.update_dxf_attribs(attribs.asdict(default_values=True))
# Remove true_color and transparency by assigning None
attribs.transparency = None # reset to transparency by layer!
attribs.rgb = None
Validation features:
- layer - string which can not contain certain characters:
<>/\":;?*=`
- color - AutoCAD Color Index (ACI) value as integer in the range from 0 to 257
- rgb - true color value as (red, green, blue) tuple, all channel values as integer values in the range from 0 to 255
- linetype - string which can not contain certain characters:
<>/\":;?*=`
, does not check if the linetype exists - lineweight - integer value in the range from 0 to 211, see Lineweights for valid values
- transparency - float value in the range from 0.0 to 1.0 and -1.0 for transparency by block
- ltscale - float value > 0.0
-
class
ezdxf.gfxattribs.
GfxAttribs
(*, layer: str = '0', color: int = 256, rgb: Optional[Tuple[int, int, int]] = None, linetype: str = 'ByLayer', lineweight: int = -1, transparency: Optional[float] = None, ltscale: float = 1.0)¶ Represents often used DXF attributes of graphical entities.
Parameters: - layer (str) – layer name as string
- color (int) – AutoCAD Color Index (ACI) color value as integer
- rgb – RGB true color (red, green, blue) tuple, each channel value in the
range from 0 to 255,
None
for not set - linetype (str) – linetype name, does not check if the linetype exist!
- lineweight (int) – see Lineweights documentation for valid values
- transparency (float) – transparency value in the range from 0.0 to 1.0,
where 0.0 is opaque and 1.0 if fully transparent, -1.0 for
transparency by block,
None
for transparency by layer - ltscale (float) – linetype scaling factor > 0.0, default factor is 1.0
Raises: DXFValueError
– invalid attribute value-
__str__
() → str¶ Return str(self).
-
__repr__
() → str¶ Return repr(self).
-
__iter__
() → Iterator[tuple[str, Any]]¶ Returns iter(self).
-
asdict
(default_values=False) → dict[str, Any]¶ Returns the DXF attributes as
dict
, returns also the default values if argument default_values isTrue
. The true_color and transparency attributes do not have default values, the absence of these attributes is the default value.
-
items
(default_values=False) → list[tuple[str, Any]]¶ Returns the DXF attributes as list of name, value pairs, returns also the default values if argument default_values is
True
. The true_color and transparency attributes do not have default values, the absence of these attributes is the default value.
-
classmethod
load_from_header
(doc: Drawing) → GfxAttribs¶ Load default DXF attributes from the HEADER section.
There is no default true color value and the default transparency is not stored in the HEADER section.
Loads following header variables:
$CLAYER
- current layer name$CECOLOR
- current ACI color$CELTYPE
- current linetype name$CELWEIGHT
- current lineweight$CELTSCALE
- current linetype scaling factor
-
write_to_header
(doc: Drawing) → None¶ Write DXF attributes as default values to the HEADER section.
Writes following header variables:
$CLAYER
- current layer name, if a layer table entry exist in doc$CECOLOR
- current ACI color$CELTYPE
- current linetype name, if a linetype table entry exist in doc$CELWEIGHT
- current lineweight$CELTSCALE
- current linetype scaling factor
-
classmethod
from_entity
(entity: DXFEntity) → GfxAttribs¶ Get the graphical attributes of an entity as
GfxAttribs
object.
-
classmethod
from_dict
(d: dict[str, Any]) → GfxAttribs¶ Construct
GfxAttribs
from a dictionary of raw DXF values.Supported attributes are:
- layer: layer name as string
- color: AutoCAD Color Index (ACI) value as int
- true_color: raw DXF integer value for RGB colors
- rgb: RGB tuple of int or
None
- linetype: linetype name as string
- lineweight: lineweight as int, see basic concept of Lineweights
- transparency: raw DXF integer value of transparency or a float in the range from 0.0 to 1.0
- ltscale: linetype scaling factor as float