CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-klayout

KLayout is a high performance layout viewer and editor that supports GDS and OASIS files and more formats

Pending
Overview
Eval results
Files

file-io.mddocs/

File I/O and Format Support

Comprehensive support for reading and writing various Electronic Design Automation (EDA) file formats with configurable import/export options and format-specific features.

Capabilities

Supported File Formats

KLayout supports industry-standard EDA file formats for layout data exchange:

  • GDS2/GDSII: Industry standard binary format for IC layouts
  • OASIS: Open Artwork System Interchange Standard
  • CIF: Caltech Intermediate Format
  • DXF: AutoCAD Drawing Exchange Format
  • LEF/DEF: Library Exchange Format and Design Exchange Format
  • Magic: Magic VLSI layout tool format
  • Gerber: PCB manufacturing format (basic support)

Loading Layout Files

class LoadLayoutOptions:
    def __init__(self):
        """Create default loading options."""
    
    def select_cell(self, cell_name: str) -> None:
        """
        Select a specific cell to load (top cell).
        
        Parameters:
        - cell_name: Name of the cell to load as top cell
        """
    
    def select_all_cells(self) -> None:
        """Select all cells for loading."""
    
    def layer_map(self, layer_map: LayerMap) -> None:
        """
        Set layer mapping for import.
        
        Parameters:
        - layer_map: Mapping between file layers and target layers
        """
    
    def create_other_layers(self, create: bool) -> None:
        """
        Control whether to create layers not in layer map.
        
        Parameters:
        - create: If True, create unmapped layers automatically
        """
    
    def text_enabled(self, enabled: bool) -> None:
        """Enable or disable text import."""
    
    def properties_enabled(self, enabled: bool) -> None:
        """Enable or disable property import."""

def read_layout(filename: str, options: LoadLayoutOptions = None) -> Layout:
    """
    Read a layout file and return a Layout object.
    
    Parameters:
    - filename: Path to the layout file
    - options: Optional loading configuration
    
    Returns:
    Layout: The loaded layout
    """

# Layout method for reading into existing layout
class Layout:
    def read(self, filename: str, options: LoadLayoutOptions = None) -> None:
        """
        Read layout data from file into this layout.
        
        Parameters:
        - filename: Path to the layout file
        - options: Optional loading configuration
        """

Saving Layout Files

class SaveLayoutOptions:
    def __init__(self):
        """Create default saving options."""
    
    def set_format(self, format: str) -> None:
        """
        Set output format explicitly.
        
        Parameters:
        - format: Format name ("GDS2", "OASIS", "CIF", "DXF", etc.)
        """
    
    def select_cell(self, cell_index: int) -> None:
        """
        Select specific cell to save as top cell.
        
        Parameters:
        - cell_index: Index of cell to save
        """
    
    def add_layer(self, layer_index: int, layer_info: LayerInfo) -> None:
        """
        Add layer to output with specific layer info.
        
        Parameters:
        - layer_index: Internal layer index
        - layer_info: Target layer specification
        """
    
    def scale_factor(self, factor: float) -> None:
        """
        Set coordinate scaling factor.
        
        Parameters:
        - factor: Scaling factor for coordinates
        """
    
    def write_context_info(self, write: bool) -> None:
        """Enable writing of context information."""
    
    def write_file_properties(self, write: bool) -> None:
        """Enable writing of file-level properties."""

# Layout method for saving
class Layout:
    def write(self, filename: str, options: SaveLayoutOptions = None) -> None:
        """
        Write layout data to file.
        
        Parameters:
        - filename: Output file path
        - options: Optional save configuration
        """

Layer Mapping

class LayerMap:
    def __init__(self):
        """Create empty layer mapping."""
    
    def map(self, source: LayerInfo, target: LayerInfo) -> None:
        """
        Map source layer to target layer.
        
        Parameters:
        - source: Source layer specification
        - target: Target layer specification
        """
    
    def map_expr(self, source_expr: str, target: LayerInfo) -> None:
        """
        Map layers using expression to target layer.
        
        Parameters:
        - source_expr: Layer selection expression
        - target: Target layer specification
        """
    
    def unmap(self, source: LayerInfo) -> None:
        """Remove mapping for source layer."""
    
    def clear(self) -> None:
        """Clear all mappings."""

class LayerInfo:
    def __init__(self, layer: int, datatype: int = 0, name: str = ""):
        """
        Layer specification with layer number, datatype, and optional name.
        
        Parameters:
        - layer: Layer number
        - datatype: Datatype number (default 0)
        - name: Optional layer name
        """
    
    layer: int
    datatype: int
    name: str
    
    def is_named(self) -> bool:
        """Check if layer has a name."""

Format-Specific Options

GDS2 Options

class GDS2WriterOptions:
    def write_timestamps(self, write: bool) -> None:
        """Enable writing of timestamps."""
    
    def write_cell_properties(self, write: bool) -> None:
        """Enable writing of cell properties."""
    
    def libname(self, name: str) -> None:
        """Set library name in GDS2 header."""
    
    def user_units(self, units: float) -> None:
        """Set user units (typically 1e-6 for micrometers)."""
    
    def database_units(self, units: float) -> None:
        """Set database units (typically 1e-9 for nanometers)."""

OASIS Options

class OASISWriterOptions:
    def compression_level(self, level: int) -> None:
        """
        Set compression level (0-9).
        
        Parameters:
        - level: Compression level (0=none, 9=maximum)
        """
    
    def write_cblocks(self, write: bool) -> None:
        """Enable writing of compressed blocks."""
    
    def strict_mode(self, strict: bool) -> None:
        """Enable strict OASIS compliance mode."""

Usage Examples

Basic File Operations

import klayout.db as db

# Read a GDS file
layout = db.Layout()
layout.read("input.gds")

# Process layout...

# Write to different format
layout.write("output.oas")  # OASIS format
layout.write("output.cif")  # CIF format

Advanced Loading with Options

import klayout.db as db

# Create loading options
options = db.LoadLayoutOptions()

# Only load specific cell
options.select_cell("TOP_CELL")

# Create layer mapping
layer_map = db.LayerMap()
layer_map.map(db.LayerInfo(1, 0), db.LayerInfo(10, 0))  # Map layer 1/0 to 10/0
layer_map.map(db.LayerInfo(2, 0), db.LayerInfo(11, 0))  # Map layer 2/0 to 11/0
options.layer_map(layer_map)

# Disable text import
options.text_enabled(False)

# Load with options
layout = db.Layout()
layout.read("complex_design.gds", options)

Selective Cell Export

import klayout.db as db

layout = db.Layout()
layout.read("large_design.gds")

# Find specific cell
cell = layout.cell("MEMORY_BLOCK")

# Create save options for specific cell
options = db.SaveLayoutOptions()
options.select_cell(cell.cell_index)

# Add only specific layers
layer1 = layout.layer(db.LayerInfo(1, 0))
layer2 = layout.layer(db.LayerInfo(2, 0))
options.add_layer(layer1, db.LayerInfo(1, 0))
options.add_layer(layer2, db.LayerInfo(2, 0))

# Save only the selected cell and layers
layout.write("memory_block_only.gds", options)

Format Conversion with Scaling

import klayout.db as db

# Read layout in database units
layout = db.Layout()
layout.read("design_in_nm.gds")

# Create save options with scaling
options = db.SaveLayoutOptions()
options.scale_factor(0.001)  # Convert from nm to μm

# Write with scaling
layout.write("design_in_um.gds", options)

Layer Management During I/O

import klayout.db as db

# Create complex layer mapping
layer_map = db.LayerMap()

# Map multiple source layers to single target
layer_map.map(db.LayerInfo(1, 0), db.LayerInfo(100, 0))
layer_map.map(db.LayerInfo(1, 1), db.LayerInfo(100, 0))  # Merge datatypes

# Map named layers
layer_map.map(db.LayerInfo(0, 0, "METAL1"), db.LayerInfo(10, 0))
layer_map.map(db.LayerInfo(0, 0, "METAL2"), db.LayerInfo(20, 0))

# Use expression mapping
layer_map.map_expr("1/*", db.LayerInfo(100, 0))  # All datatypes of layer 1

options = db.LoadLayoutOptions()
options.layer_map(layer_map)
options.create_other_layers(False)  # Don't create unmapped layers

layout = db.Layout()
layout.read("input_with_many_layers.gds", options)

Working with File Properties

import klayout.db as db

# Read layout and access file properties
layout = db.Layout()
layout.read("design_with_properties.gds")

# Check for properties on layout
if layout.properties_id() != 0:
    props = layout.properties(layout.properties_id())
    print("Layout properties:", props)

# Write with properties enabled
options = db.SaveLayoutOptions()
options.write_file_properties(True)
options.write_context_info(True)

layout.write("output_with_properties.gds", options)

Install with Tessl CLI

npx tessl i tessl/pypi-klayout

docs

database-operations.md

file-io.md

hierarchical-design.md

index.md

layout-viewer.md

shape-operations.md

transformations.md

verification.md

tile.json