File: hatch.rst

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (170 lines) | stat: -rw-r--r-- 5,085 bytes parent folder | download
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
.. _tut_hatch:

Tutorial for Hatch
==================

Create hatches with one boundary path
-------------------------------------

The simplest form of the :class:`~ezdxf.entities.Hatch` entity has one polyline
path with only straight lines as boundary path:

.. literalinclude:: src/hatch/solid_hatch_polyline_path.py

But like all polyline entities the polyline path can also have bulge values:

.. literalinclude:: src/hatch/solid_hatch_polyline_path_with_bulge.py

The most flexible way to define a boundary path is the edge path. An edge path
can have multiple edges and each edge can be one of the following elements:

    - line :meth:`EdgePath.add_line`
    - arc :meth:`EdgePath.add_arc`
    - ellipse :meth:`EdgePath.add_ellipse`
    - spline :meth:`EdgePath.add_spline`

Create a solid hatch with an edge path (ellipse) as boundary path:

.. literalinclude:: src/hatch/solid_hatch_ellipse.py

Create hatches with multiple boundary paths (islands)
-----------------------------------------------------

The DXF attribute :attr:`hatch_style` defines the island detection style:

=== ========================================================
0   nested - altering filled and unfilled areas
1   outer - area between `external` and `outermost` path is filled
2   ignore - `external` path is filled
=== ========================================================

.. literalinclude:: src/hatch/solid_hatch_islands.py
    :lines: 11-27

This is also the result for all 4 paths and :attr:`hatch_style` set to ``2``
(ignore).

.. image:: gfx/hatch-island-01.png
    :align: center

.. literalinclude:: src/hatch/solid_hatch_islands.py
    :lines: 31-36

This is also the result for all 4 paths and :attr:`hatch_style` set to ``1``
(outer).

.. image:: gfx/hatch-island-02.png
    :align: center

.. literalinclude:: src/hatch/solid_hatch_islands.py
    :lines: 40-45

.. image:: gfx/hatch-island-03.png
    :align: center

.. literalinclude:: src/hatch/solid_hatch_islands.py
    :lines: 49-56

.. image:: gfx/hatch-island-04.png
    :align: center

The expected result of combinations of various :attr:`hatch_style` values and
paths `flags`, or the handling of overlapping paths is not documented by the
DXF reference, so don't ask me, ask Autodesk or just try it by yourself
and post your experience in the forum.

Example for Edge Path Boundary
------------------------------

.. literalinclude:: src/hatch/edge_path.py
    :lines: 8-56

.. image:: gfx/hatch-edge-path.png
    :align: center

Associative Boundary Paths
--------------------------

A HATCH entity can be associative to a base geometry, which means if the base
geometry is edited in a CAD application the HATCH get the same modification.
Because `ezdxf` is **not** a CAD application, this association is **not**
maintained nor verified by `ezdxf`, so if you modify the base geometry
afterwards the geometry of the boundary path is not updated and no verification
is done to check if the associated geometry matches the boundary path, this
opens many possibilities to create invalid DXF files: USE WITH CARE.

This example associates a LWPOLYLINE entity to the hatch created from the
LWPOLYLINE vertices:

.. literalinclude:: src/hatch/assoc_hatch.py
    :lines: 8-24

An :class:`EdgePath` needs associations to all geometry entities forming the
boundary path.

Predefined Hatch Pattern
------------------------

Use predefined hatch pattern by name:

.. code-block:: Python

    hatch.set_pattern_fill("ANSI31", scale=0.5)


.. image:: gfx/hatch-predefined-pattern.png
    :align: center

Load Hatch Patterns From File
-----------------------------

CAD applications store the hatch patterns in pattern files with the file extension 
``.pat``. The following script shows how to load and use these pattern files:

.. code-block:: Python

    from ezdxf.tools import pattern

    EXAMPLE = """; a pattern file

    *SOLID, Solid fill
    45, 0,0, 0,.125
    *ANSI31, ANSI Iron, Brick, Stone masonry
    45, 0,0, 0,.125
    *ANSI32, ANSI Steel
    45, 0,0, 0,.375
    45, .176776695,0, 0,.375
    *ANSI33, ANSI Bronze, Brass, Copper
    45, 0,0, 0,.25
    45, .176776695,0, 0,.25, .125,-.0625
    *ANSI34, ANSI Plastic, Rubber
    45, 0,0, 0,.75
    45, .176776695,0, 0,.75
    45, .353553391,0, 0,.75
    45, .530330086,0, 0,.75
    """

    hatch = msp.add_hatch()
    # load your pattern file from the file system as string:
    # with open("pattern_file.pat", "rt") as fp:
    #      EXAMPLE = fp.read()
    patterns = pattern.parse(EXAMPLE)

    hatch.set_pattern_fill(
        "MyPattern",
        color=7,
        angle=0,  # the overall rotation of the pattern in degrees
        scale=1.0,  # overall scaling of the pattern
        style=0,  # normal hatching style
        pattern_type=0,  # user-defined
        # pattern name without the preceding asterisk
        definition=patterns["ANSI34"],  
    )
    points = [(0, 0), (10, 0), (10, 10), (0, 10)]
    hatch.paths.add_polyline_path(points)
    msp.add_lwpolyline(points, close=True, dxfattribs={"color": 1})


.. seealso::

    :ref:`tut_hatch_pattern`