layouttk 0.3.0

GDSII and OASIS layout toolkit for layer extraction, verification, and window clipping
Documentation
//! Deterministic generated layouts with grid, scope and hierarchy assertions.
use layouttk::{
    Element, Format, Layer, Layout, Point, Property, Repetition, Transform, read_from, write_to,
};
use std::collections::BTreeSet;

fn generated(seed: u64) -> Layout {
    let mut random = seed;
    let mut next = || {
        random = random.wrapping_mul(6364136223846793005).wrapping_add(1);
        (random >> 33) as i64
    };
    let mut chip = Layout::new("LIBRARY", 1e-9).unwrap();
    chip.add_cell("TOP").unwrap();
    chip.add_cell("LEAF").unwrap();
    for index in 0..64 {
        let layer = Layer {
            number: (index % 3) as u32,
            datatype: (index % 2) as u32,
        };
        let x = next() % 4096 - 2048;
        let y = next() % 4096 - 2048;
        let mut item = Element::Polygon {
            layer,
            points: vec![
                Point { x, y },
                Point {
                    x: x + 1 + next() % 16,
                    y,
                },
                Point {
                    x,
                    y: y + 1 + next() % 32,
                },
            ],
        };
        item.set_properties(vec![Property::gds(
            17,
            format!("p{index}-{seed}").into_bytes(),
        )]);
        chip.add_element("LEAF", item).unwrap();
    }
    chip.add_element(
        "TOP",
        Element::Reference {
            cell: "LEAF".into(),
            origin: Point { x: 77, y: -31 },
            transform: Transform::default(),
            array: None,
        },
    )
    .unwrap();
    chip
}

#[test]
fn decode_filter_and_encode_preserve_the_same_stored_records() {
    let chosen = BTreeSet::from([Layer {
        number: 1,
        datatype: 1,
    }]);
    for seed in [1, 4, 8, 73, 109, 555, 910, 2026] {
        for format in [Format::Gdsii, Format::Oasis] {
            let mut chip = generated(seed);
            if format == Format::Oasis {
                for (i, shape) in chip.cells[1].elements.iter_mut().enumerate() {
                    if i % 3 == 0 {
                        shape
                            .set_repetition(Some(Repetition::Offsets(vec![
                                Point { x: -5, y: 9 },
                                Point { x: 0, y: 0 },
                                Point { x: 17, y: -1 },
                            ])))
                            .unwrap();
                    }
                }
            }
            let mut encoded = vec![];
            write_to(&chip, &mut encoded, format).unwrap();
            let rebuilt = read_from(encoded.as_slice(), None).unwrap();
            assert_eq!(chip, rebuilt);
            for cell in &mut chip.cells {
                cell.elements
                    .retain(|e| e.layer().is_none_or(|l| chosen.contains(&l)));
            }
            assert_eq!(chip, read_from(encoded.as_slice(), Some(&chosen)).unwrap());
        }
    }
}