File: binpacking.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 (282 lines) | stat: -rw-r--r-- 5,796 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
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
.. _bin_packing:

Bin-Packing Add-on
==================

.. module:: ezdxf.addons.binpacking

This add-on is based on the 3D bin packing module `py3dbp`_ hosted on `PyPI`_.
Both sources of this package are MIT licensed like `ezdxf` itself.

The Bin Packing Problem
-----------------------

Quote from the `Wikipedia`_ article:

    The bin packing problem is an optimization problem, in which items of different
    sizes must be packed into a finite number of bins or containers, each of a fixed
    given capacity, in a way that minimizes the number of bins used.

Example
-------

This code replicates the example used by the `py3dbp`_ package:

.. code-block:: python

    from typing import List
    import ezdxf
    from ezdxf import colors
    from ezdxf.addons import binpacking as bp

    SMALL_ENVELOPE = ("small-envelope", 11.5, 6.125, 0.25, 10)
    LARGE_ENVELOPE = ("large-envelope", 15.0, 12.0, 0.75, 15)
    SMALL_BOX = ("small-box", 8.625, 5.375, 1.625, 70.0)
    MEDIUM_BOX = ("medium-box", 11.0, 8.5, 5.5, 70.0)
    MEDIUM_BOX2 = ("medium-box-2", 13.625, 11.875, 3.375, 70.0)
    LARGE_BOX = ("large-box", 12.0, 12.0, 5.5, 70.0)
    LARGE_BOX2 = ("large-box-2", 23.6875, 11.75, 3.0, 70.0)

    ALL_BINS = [
        SMALL_ENVELOPE,
        LARGE_ENVELOPE,
        SMALL_BOX,
        MEDIUM_BOX,
        MEDIUM_BOX2,
        LARGE_BOX,
        LARGE_BOX2,
    ]


    def build_packer():
        packer = bp.Packer()
        packer.add_item("50g [powder 1]", 3.9370, 1.9685, 1.9685, 1)
        packer.add_item("50g [powder 2]", 3.9370, 1.9685, 1.9685, 2)
        packer.add_item("50g [powder 3]", 3.9370, 1.9685, 1.9685, 3)
        packer.add_item("250g [powder 4]", 7.8740, 3.9370, 1.9685, 4)
        packer.add_item("250g [powder 5]", 7.8740, 3.9370, 1.9685, 5)
        packer.add_item("250g [powder 6]", 7.8740, 3.9370, 1.9685, 6)
        packer.add_item("250g [powder 7]", 7.8740, 3.9370, 1.9685, 7)
        packer.add_item("250g [powder 8]", 7.8740, 3.9370, 1.9685, 8)
        packer.add_item("250g [powder 9]", 7.8740, 3.9370, 1.9685, 9)
        return packer


    def make_doc():
        doc = ezdxf.new()
        doc.layers.add("FRAME", color=colors.YELLOW)
        doc.layers.add("ITEMS")
        doc.layers.add("TEXT")
        return doc


    def main(filename):
        bins: List[bp.Bin] = []
        for box in ALL_BINS:
            packer = build_packer()
            packer.add_bin(*box)
            packer.pack(bp.PickStrategy.BIGGER_FIRST)
            bins.extend(packer.bins)
        doc = make_doc()
        bp.export_dxf(doc.modelspace(), bins, offset=(0, 20, 0))
        doc.saveas(filename)


    if __name__ == "__main__":
        main("py3dbp_example.dxf")



.. image:: gfx/binpacking-example.png

.. seealso::

    - `example1`_ script
    - `example2`_ script

Packer Classes
--------------

.. autoclass:: AbstractPacker

    .. attribute:: bins

        List of containers to fill.

    .. attribute:: items

        List of items to pack into the :attr:`bins`.

    .. autoproperty:: is_packed

    .. autoproperty:: unfitted_items

    .. automethod:: __str__

    .. automethod:: append_bin

    .. automethod:: append_item

    .. automethod:: get_fill_ratio

    .. automethod:: get_capacity

    .. automethod:: get_total_weight

    .. automethod:: get_total_volume

    .. automethod:: pack

Packer
~~~~~~

.. autoclass:: Packer

    .. automethod:: add_bin

    .. automethod:: add_item

FlatPacker
~~~~~~~~~~

.. autoclass:: FlatPacker

    .. automethod:: add_bin

    .. automethod:: add_item

Bin Classes
-----------

.. autoclass:: Bin

    .. attribute:: name

        Name of then container as string.

    .. attribute:: width

    .. attribute:: height

    .. attribute:: depth

    .. attribute:: max_weight

    .. autoproperty:: is_empty

    .. automethod:: __str__

    .. automethod:: copy

    .. automethod:: reset

    .. automethod:: put_item

    .. automethod:: get_capacity

    .. automethod:: get_total_weight

    .. automethod:: get_total_volume

    .. automethod:: get_fill_ratio

Box Class
~~~~~~~~~

.. autoclass:: Box

Envelope Class
~~~~~~~~~~~~~~

.. autoclass:: Envelope


Item Class
~~~~~~~~~~

.. autoclass:: Item

    .. attribute:: payload

        Arbitrary Python object.

    .. attribute:: width

    .. attribute:: height

    .. attribute:: depth

    .. attribute:: weight

    .. autoproperty:: bbox

    .. autoproperty:: rotation_type

    .. autoproperty:: position

    .. automethod:: copy

    .. automethod:: __str__

    .. automethod:: get_volume

    .. automethod:: get_dimension

    .. automethod:: get_transformation

FlatItem Class
~~~~~~~~~~~~~~

.. autoclass:: FlatItem

Functions
---------

.. autofunction:: shuffle_pack

Enums
-----

RotationType
~~~~~~~~~~~~

.. autoclass:: RotationType

    .. attribute:: WHD

    .. attribute:: HWD

    .. attribute:: HDW

    .. attribute:: DHW

    .. attribute:: DWH

    .. attribute:: WDH

PickStrategy
~~~~~~~~~~~~

.. autoclass:: PickStrategy

    .. attribute:: BIGGER_FIRST

    .. attribute:: SMALLER_FIRST

    .. attribute:: SHUFFLE


Credits
-------

    - `py3dbp`_ package by Enzo Ruiz Pelaez
    - `bp3d`_ by `gedex` - github repository on which `py3dbp`_ is based, written in Go
    - Optimizing three-dimensional bin packing through simulation (`PDF`_)

.. _py3dbp: https://github.com/enzoruiz/3dbinpacking
.. _bp3d: https://github.com/gedex/bp3d
.. _PyPI: https://pypi.org/project/py3dbp/
.. _Wikipedia: https://en.wikipedia.org/wiki/Bin_packing_problem
.. _example1: https://github.com/mozman/ezdxf/blob/master/examples/addons/binpacking.py
.. _example2: https://github.com/mozman/ezdxf/blob/master/examples/addons/binpacking2.py
.. _PDF: https://github.com/enzoruiz/3dbinpacking/blob/master/erick_dube_507-034.pdf