1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
.. _table_painter:
.. module:: ezdxf.addons.tablepainter
TablePainter
============
This is an add-on for drawing tables build from DXF primitives.
This add-on was created for porting :mod:`dxfwrite` projects to :mod:`ezdxf` and
was not officially documented for :mod:`ezdxf` versions prior the 1.0 release.
For the 1.0 version of :mod:`ezdxf`, this class was added as an officially
documented add-on because full support for the ACAD_TABLE entity
is very unlikely due to the enormous complexity for both the entity itself,
and for the required infrastructure and also the lack of a usable
documentation to implement all that features.
.. important::
This add-on is not related to the ACAD_TABLE entity at all and and does not
create ACAD_TABLE entities!
The table cells can contain multi-line text or BLOCK references. You can
create your own cell types by extending the :class:`CustomCell` class.
The cells are addressed by zero-based row and column indices. A table cell can
span over multiple columns and/or rows.
A :class:`TextCell` can contain multi-line text with an arbitrary rotation angle
or letters stacked from top to bottom. The :class:`~ezdxf.addons.MTextSurrogate`
add-on is used to create multi-line text compatible to DXF version R12.
A :class:`BlockCell` contains block references (INSERT entities), if the block
definition contains attribute definitions as ATTDEF entities, these attributes
can be added automatically to the block reference as ATTRIB entities.
.. note::
The DXF format does not support clipping boxes ot paths, therefore the
render method of any cell can render beyond the borders of the cell!
Tutorial
--------
Set up a new DXF document:
.. code-block:: Python
import ezdxf
from ezdxf.enums import MTextEntityAlignment
from ezdxf.addons import TablePainter
doc = ezdxf.new("R2000") # required for lineweight support
doc.header["$LWDISPLAY"] = 1 # show lineweights
doc.styles.add("HEAD", font="OpenSans-ExtraBold.ttf")
doc.styles.add("CELL", font="OpenSans-Regular.ttf")
Create a new :class:`TablePainter` object with four rows and four columns, the
insert location is the default render location but can be overriden in the
:meth:`render` method:
.. code-block:: Python
table = TablePainter(
insert=(0, 0), nrows=4, ncols=4, cell_width=6.0, cell_height=2.0
)
Create a new :class:`CellStyle` object for the table-header called "head":
.. code-block:: Python
table.new_cell_style(
"head",
text_style="HEAD",
text_color=ezdxf.colors.BLUE,
char_height=0.7,
bg_color=ezdxf.colors.LIGHT_GRAY,
align=MTextEntityAlignment.MIDDLE_CENTER,
)
Redefine the default :class:`CellStyle` for the content cells:
.. code-block:: Python
# reset default cell style
default_style = table.get_cell_style("default")
default_style.text_style = "CELL"
default_style.char_height = 0.5
default_style.align = MTextEntityAlignment.BOTTOM_LEFT
Set the table-header content:
.. code-block:: Python
for col in range(4):
table.text_cell(0, col, f"Head[{col}]", style="head")
Set the cell content:
.. code-block:: Python
for row in range(1, 4):
for col in range(4):
# cell style is "default"
table.text_cell(row, col, f"Cell[{row}, {col}]")
Add a red frame around the table-header:
.. code-block:: Python
# new cell style is required
red_frame = table.new_cell_style("red-frame")
red_borderline = table.new_border_style(color=ezdxf.colors.RED, lineweight=35)
# set the red borderline style for all cell borders
red_frame.set_border_style(red_borderline)
# create the frame object
table.frame(0, 0, 4, style="red-frame")
Render the table into the modelspace and export the DXF file:
.. code-block:: Python
# render the table, shifting the left-bottom of the table to the origin:
table.render(doc.modelspace(), insert=(0, table.table_height))
th = table.table_height
tw = table.table_width
doc.set_modelspace_vport(height=th * 1.5, center=(tw/2, th/2))
doc.saveas("table_tutorial.dxf")
.. image:: gfx/table_painter_addon.png
:align: center
.. seealso::
- Example script: `table_painter_addon.py`_
TablePainter
------------
.. autoclass:: TablePainter
.. attribute:: bg_layer_name: str
background layer name, layer for the background SOLID entities,
default is "TABLEBACKGROUND"
.. attribute:: fg_layer_name: str
foreground layer name, layer for the cell content, default is
"TABLECONTENT"
.. attribute:: grid_layer_name: str
table grid layer name, layer for the cell border lines, default is
"TABLEGRID"
.. autoproperty:: table_width
.. autoproperty:: table_height
.. automethod:: set_col_width
.. automethod:: set_row_height
.. automethod:: text_cell
.. automethod:: block_cell
.. automethod:: set_cell
.. automethod:: get_cell
.. automethod:: new_cell_style
.. automethod:: get_cell_style
.. automethod:: new_border_style
.. automethod:: frame
.. automethod:: render
Cell
----
.. class:: Cell
Abstract base class for table cells.
TextCell
--------
.. class:: TextCell
Implements a cell type containing a multi-line text. Uses the
:class:`~ezdxf.addons.MTextSurrogate` add-on to render the multi-line
text, therefore the content of these cells is compatible to DXF R12.
.. important::
Use the factory method :meth:`TablePainter.text_cell` to
instantiate text cells.
BlockCell
---------
.. autoclass:: BlockCell
Implements a cell type containing a block reference.
.. important::
Use the factory method :meth:`TablePainter.block_cell` to
instantiate block cells.
CustomCell
----------
.. class:: CustomCell
Base class to implement custom cells. Overwrite the :meth:`render` method
to render the cell. The custom cell type has to be instantiated by the
user and added to the table by the :meth:`TablePainter.set_cell` method.
.. automethod:: render
The render space is defined by the argument `coords` which is a tuple of
4 float values in the order: left, right, top, bottom. These values are
layout coordinates in drawing units.
The DXF format does not support clipping boxes, therefore the render method
can render beyond these borders!
CellStyle
---------
.. autoclass:: CellStyle
.. attribute:: text_style: str
:class:`~ezdxf.entities.Textstyle` name as string, ignored by :class:`BlockCell`
.. attribute:: char_height: float
text height in drawing units, ignored by :class:`BlockCell`
.. attribute:: line_spacing: float
line spacing in percent, distance of line base points = :attr:`char_height`
* :attr:`line_spacing`, ignored by :class:`BlockCell`
.. attribute:: scale_x: float
text stretching factor (width factor) or block reference x-scaling factor
.. attribute:: scale_y: float
block reference y-scaling factor, ignored by :class:`TextCell`
.. attribute:: text_color: int
:ref:`ACI` for text, ignored by :class:`BlockCell`
.. attribute:: rotation: float
text or block rotation in degrees
.. attribute:: stacked: bool
Stacks letters of :class:`TextCell` instances from top to bottom without
rotating the characters if ``True``, ignored by :class:`BlockCell`
.. attribute:: align: MTextEntityAlignment
text and block alignment, see :class:`ezdxf.enums.MTextEntityAlignment`
.. attribute:: margin_x: float
left and right cell margin in drawing units
.. attribute:: margin_y: float
top and bottom cell margin in drawing units
.. attribute:: bg_color: int
cell background color as :ref:`ACI`, ignored by :class:`BlockCell`
.. attribute:: left: BorderStyle
left cell border style
.. attribute:: top: BorderStyle
top cell border style
.. attribute:: right: BorderStyle
right cell border style
.. attribute:: bottom: BorderStyle
bottom cell border style
.. automethod:: set_border_status
.. automethod:: set_border_style
.. automethod:: get_default_border_style
BorderStyle
-----------
.. autoclass:: BorderStyle
.. attribute:: status: bool
border status, ``True`` for visible, ``False`` for hidden
.. attribute:: color: int
:ref:`ACI`
.. attribute:: linetype: str
linetype name as string, default is "BYLAYER"
.. attribute:: lineweight: int
lineweight as int, default is by layer
.. attribute:: priority: int
drawing priority, higher values cover lower values
.. _table_painter_addon.py: https://github.com/mozman/ezdxf/blob/master/examples/addons/table_painter_addon.py
|