Exemple #1
0
def test_align():
    D = Device()
    # Create different-sized rectangles and add them to D then distribute them
    [
        D.add_ref(
            pg.rectangle(size=[n * 15 + 20, n * 15 + 20]).move((n, n * 4)))
        for n in [0, 2, 3, 1, 2]
    ]
    D.distribute(elements='all', direction='x', spacing=5, separation=True)
    # Align top edges
    D.align(elements='all', alignment='ymax')
    h = D.hash_geometry(precision=1e-4)
    assert (h == '38025959a80e46e47eabcf3f096c6273427dabc3')

    D = Device()
    # Create different-sized rectangles and add them to D then distribute them
    [
        D.add_ref(
            pg.rectangle(size=[n * 15 + 20, n * 15 + 20]).move((n, n * 4)))
        for n in [0, 2, 3, 1, 2]
    ]
    D.distribute(elements='all', direction='x', spacing=5, separation=True)
    # Align top edges
    D.align(elements='all', alignment='y')
    h = D.hash_geometry(precision=1e-4)
    assert (h == 'ed32ee1ce1f3da8f6216020877d6c1b64097c600')
Exemple #2
0
def test_distribute():
    D = Device()
    # Create different-sized rectangles and add them to D
    [
        D.add_ref(
            pg.rectangle(size=[n * 15 + 20, n * 15 + 20]).move((n, n * 4)))
        for n in [0, 2, 3, 1, 2]
    ]
    # Distribute all the rectangles in D along the x-direction with a separation of 5
    D.distribute(
        elements='all',  # either 'all' or a list of objects
        direction='x',  # 'x' or 'y'
        spacing=5,
        separation=True)

    h = D.hash_geometry(precision=1e-4)
    assert (h == '1aa688d7dfb59e94d28dd0d9b8f324ff30281d70')

    D = Device()
    [
        D.add_ref(
            pg.rectangle(size=[n * 15 + 20, n * 15 + 20]).move((n, n * 4)))
        for n in [0, 2, 3, 1, 2]
    ]
    D.distribute(elements='all',
                 direction='x',
                 spacing=100,
                 separation=False,
                 edge='xmin')
    h = D.hash_geometry(precision=1e-4)
    assert (h == '18be0ef1db78095233d2f3ae5f065d9f453a6c07')
D_packed_list = pg.packer(
    D_list,  # Must be a list or tuple of Devices
    spacing=4,  # Minimum distance between adjacent shapes
    aspect_ratio=(1, 1),  # Shape of the box
    max_size=(500,
              500),  # Limits the size into which the shapes will be packed
    density=1.05,  # Values closer to 1 pack tighter but require more computation
    sort_by_area=True,  # Pre-sorts the shapes by area
    verbose=False,
)

# Put all packed bins into a single device and spread them out with distribute()
F = Device()
[F.add_ref(D) for D in D_packed_list]
F.distribute(elements='all', direction='x', spacing=100, separation=True)
qp(F)

# Note that the packing problem is an NP-complete problem, so `pg.packer()`
# may be slow if there are more than a few hundred Devices to pack (in that
# case, try pre-packing a few dozen at a time then packing the resulting
# bins). Requires the `rectpack` python package.

#==============================================================================
# Distributing shapes
#==============================================================================
# The `distribute()` function allows you to space out elements within a Device
# evenly in the x or y direction.  It is meant to duplicate the distribute
# functionality present in Inkscape / Adobe Illustrator

# Say we start out with a few random-sized rectangles we want to space out
Exemple #4
0
                      precision=1e-6,
                      num_divisions=[1, 1],
                      layer=0)
Tshrink = pg.offset(T,
                    distance=-1.5,
                    join_first=True,
                    precision=1e-6,
                    num_divisions=[1, 1],
                    layer=0)

# Plot the original geometry, the expanded, and the shrunk versions
D = Device()
t1 = D.add_ref(T)
t2 = D.add_ref(Texpanded)
t3 = D.add_ref(Tshrink)
D.distribute([t1, t2, t3], direction='x', spacing=5)
qp(D)  # quickplot the geometry
create_image(D, 'offset')

# example-invert
import phidl.geometry as pg
from phidl import quickplot as qp

E = pg.ellipse(radii=(10, 5))
D = pg.invert(E, border=0.5, precision=1e-6, layer=0)
qp(D)  # quickplot the geometry
create_image(D, 'invert')

# example-boolean
import phidl.geometry as pg
from phidl import quickplot as qp