0
# Database and Geometric Operations
1
2
Core functionality for layout database management, geometric primitive creation and manipulation, and fundamental operations for IC layout design and analysis.
3
4
## Capabilities
5
6
### Layout Database Management
7
8
The Layout class serves as the top-level container for all layout data, managing cells, layers, and metadata.
9
10
```python { .api }
11
class Layout:
12
def __init__(self):
13
"""Create a new empty layout."""
14
15
def create_cell(self, name: str) -> Cell:
16
"""
17
Create a new cell in the layout.
18
19
Parameters:
20
- name: Unique name for the cell
21
22
Returns:
23
Cell: The newly created cell object
24
"""
25
26
def cell(self, name: str) -> Cell:
27
"""
28
Retrieve an existing cell by name.
29
30
Parameters:
31
- name: Name of the cell to retrieve
32
33
Returns:
34
Cell: The cell object, or None if not found
35
"""
36
37
def read(self, filename: str, options: LoadLayoutOptions = None) -> None:
38
"""
39
Read layout data from a file.
40
41
Parameters:
42
- filename: Path to the layout file
43
- options: Optional loading configuration
44
"""
45
46
def write(self, filename: str, options: SaveLayoutOptions = None) -> None:
47
"""
48
Write layout data to a file.
49
50
Parameters:
51
- filename: Output file path
52
- options: Optional save configuration
53
"""
54
55
def layer(self, layer_info: LayerInfo) -> int:
56
"""
57
Get or create a layer index for the given layer info.
58
59
Parameters:
60
- layer_info: Layer specification (layer number and datatype)
61
62
Returns:
63
int: Internal layer index
64
"""
65
66
def insert_layer(self, layer_info: LayerInfo) -> int:
67
"""Insert a new layer and return its index."""
68
69
def delete_layer(self, layer_index: int) -> None:
70
"""Delete a layer by its index."""
71
72
def layer_info(self, layer_index: int) -> LayerInfo:
73
"""Get layer info for a given layer index."""
74
```
75
76
### Cell Management
77
78
Cells are the basic building blocks of hierarchical layout designs, containing shapes and instances of other cells.
79
80
```python { .api }
81
class Cell:
82
@property
83
def name(self) -> str:
84
"""Get the cell name."""
85
86
@property
87
def cell_index(self) -> int:
88
"""Get the cell's index in the layout."""
89
90
def shapes(self, layer: int) -> Shapes:
91
"""
92
Get the shapes container for a specific layer.
93
94
Parameters:
95
- layer: Layer index
96
97
Returns:
98
Shapes: Container for geometric shapes on this layer
99
"""
100
101
def insert(self, instance: CellInstArray) -> CellInstArray:
102
"""
103
Insert a cell instance into this cell.
104
105
Parameters:
106
- instance: Cell instance to insert
107
108
Returns:
109
CellInstArray: The inserted instance
110
"""
111
112
def move(self, transformation: Trans) -> None:
113
"""Move all content of the cell by the given transformation."""
114
115
def transform(self, transformation: CplxTrans) -> None:
116
"""Transform all content with complex transformation."""
117
118
def copy_tree(self, target_layout: Layout) -> Cell:
119
"""Copy this cell and its hierarchy to another layout."""
120
121
def flatten(self, levels: int = -1) -> None:
122
"""
123
Flatten the cell hierarchy by the specified number of levels.
124
125
Parameters:
126
- levels: Number of levels to flatten (-1 for all)
127
"""
128
```
129
130
### Geometric Primitives
131
132
#### Points and Vectors
133
134
```python { .api }
135
class Point:
136
def __init__(self, x: int, y: int):
137
"""
138
Create an integer coordinate point.
139
140
Parameters:
141
- x: X coordinate (database units)
142
- y: Y coordinate (database units)
143
"""
144
145
x: int
146
y: int
147
148
def distance(self, other: Point) -> float:
149
"""Calculate distance to another point."""
150
151
def moved(self, vector: Vector) -> Point:
152
"""Return a new point moved by the vector."""
153
154
class DPoint:
155
def __init__(self, x: float, y: float):
156
"""Create a double precision point."""
157
158
x: float
159
y: float
160
161
class Vector:
162
def __init__(self, x: int, y: int):
163
"""Create an integer vector."""
164
165
x: int
166
y: int
167
168
def length(self) -> float:
169
"""Calculate vector length."""
170
171
def angle(self) -> float:
172
"""Calculate vector angle in radians."""
173
174
class DVector:
175
def __init__(self, x: float, y: float):
176
"""Create a double precision vector."""
177
178
x: float
179
y: float
180
```
181
182
#### Boxes and Rectangles
183
184
```python { .api }
185
class Box:
186
def __init__(self, left: int, bottom: int, right: int, top: int):
187
"""
188
Create an integer coordinate box.
189
190
Parameters:
191
- left: Left coordinate
192
- bottom: Bottom coordinate
193
- right: Right coordinate
194
- top: Top coordinate
195
"""
196
197
left: int
198
bottom: int
199
right: int
200
top: int
201
202
def width(self) -> int:
203
"""Get box width."""
204
205
def height(self) -> int:
206
"""Get box height."""
207
208
def area(self) -> int:
209
"""Calculate box area."""
210
211
def center(self) -> Point:
212
"""Get box center point."""
213
214
def moved(self, vector: Vector) -> Box:
215
"""Return box moved by vector."""
216
217
def enlarged(self, dx: int, dy: int = None) -> Box:
218
"""Return enlarged box."""
219
220
class DBox:
221
def __init__(self, left: float, bottom: float, right: float, top: float):
222
"""Create a double precision box."""
223
224
left: float
225
bottom: float
226
right: float
227
top: float
228
```
229
230
#### Polygons
231
232
```python { .api }
233
class Polygon:
234
def __init__(self, points: list[Point] = None):
235
"""
236
Create a polygon from a list of points.
237
238
Parameters:
239
- points: List of points defining the polygon vertices
240
"""
241
242
def area(self) -> float:
243
"""Calculate polygon area."""
244
245
def perimeter(self) -> int:
246
"""Calculate polygon perimeter."""
247
248
def bbox(self) -> Box:
249
"""Get bounding box."""
250
251
def is_clockwise(self) -> bool:
252
"""Check if vertices are in clockwise order."""
253
254
def holes(self) -> int:
255
"""Get number of holes in polygon."""
256
257
def each_point_hull(self):
258
"""Iterate over hull points."""
259
260
def each_point_hole(self, hole_index: int):
261
"""Iterate over points of a specific hole."""
262
263
def moved(self, vector: Vector) -> Polygon:
264
"""Return polygon moved by vector."""
265
266
def transformed(self, trans: Trans) -> Polygon:
267
"""Return transformed polygon."""
268
269
class DPolygon:
270
def __init__(self, points: list[DPoint] = None):
271
"""Create a double precision polygon."""
272
273
def area(self) -> float:
274
"""Calculate polygon area."""
275
276
class SimplePolygon:
277
def __init__(self, points: list[Point] = None):
278
"""Create a simple (non-self-intersecting) polygon."""
279
```
280
281
#### Paths
282
283
```python { .api }
284
class Path:
285
def __init__(self, points: list[Point] = None, width: int = 0):
286
"""
287
Create a path with specified width.
288
289
Parameters:
290
- points: List of points defining the path centerline
291
- width: Path width in database units
292
"""
293
294
def width(self) -> int:
295
"""Get path width."""
296
297
def set_width(self, width: int) -> None:
298
"""Set path width."""
299
300
def length(self) -> int:
301
"""Calculate path length."""
302
303
def polygon(self) -> Polygon:
304
"""Convert path to polygon representation."""
305
306
def each_point(self):
307
"""Iterate over path points."""
308
309
class DPath:
310
def __init__(self, points: list[DPoint] = None, width: float = 0.0):
311
"""Create a double precision path."""
312
```
313
314
#### Edges
315
316
```python { .api }
317
class Edge:
318
def __init__(self, p1: Point, p2: Point):
319
"""
320
Create an edge between two points.
321
322
Parameters:
323
- p1: Start point
324
- p2: End point
325
"""
326
327
p1: Point
328
p2: Point
329
330
def length(self) -> float:
331
"""Calculate edge length."""
332
333
def dx(self) -> int:
334
"""Get X displacement."""
335
336
def dy(self) -> int:
337
"""Get Y displacement."""
338
339
def bbox(self) -> Box:
340
"""Get bounding box."""
341
342
def moved(self, vector: Vector) -> Edge:
343
"""Return edge moved by vector."""
344
345
class DEdge:
346
def __init__(self, p1: DPoint, p2: DPoint):
347
"""Create a double precision edge."""
348
```
349
350
#### Text Annotations
351
352
```python { .api }
353
class Text:
354
def __init__(self, text: str, trans: Trans = None):
355
"""
356
Create a text annotation.
357
358
Parameters:
359
- text: Text string
360
- trans: Transformation (position, rotation, etc.)
361
"""
362
363
@property
364
def string(self) -> str:
365
"""Get text string."""
366
367
def set_string(self, text: str) -> None:
368
"""Set text string."""
369
370
def transform(self) -> Trans:
371
"""Get text transformation."""
372
373
def bbox(self) -> Box:
374
"""Get text bounding box."""
375
376
class DText:
377
def __init__(self, text: str, trans: DTrans = None):
378
"""Create a double precision text annotation."""
379
```
380
381
### Shape Container Operations
382
383
```python { .api }
384
class Shapes:
385
def insert(self, shape) -> None:
386
"""
387
Insert a shape into the container.
388
389
Parameters:
390
- shape: Any geometric shape (Box, Polygon, Path, Text, etc.)
391
"""
392
393
def erase(self, shape) -> None:
394
"""Remove a shape from the container."""
395
396
def clear(self) -> None:
397
"""Remove all shapes."""
398
399
def size(self) -> int:
400
"""Get number of shapes in container."""
401
402
def empty(self) -> bool:
403
"""Check if container is empty."""
404
405
def each(self):
406
"""Iterate over all shapes."""
407
408
def each_overlapping(self, region: Box):
409
"""Iterate over shapes overlapping the given region."""
410
```
411
412
## Usage Examples
413
414
### Creating a Simple Layout
415
416
```python
417
import klayout.db as db
418
419
# Create layout and top cell
420
layout = db.Layout()
421
top_cell = layout.create_cell("TOP")
422
423
# Define layers
424
metal1 = layout.layer(db.LayerInfo(1, 0))
425
via1 = layout.layer(db.LayerInfo(2, 0))
426
427
# Create geometric shapes
428
rect = db.Box(0, 0, 1000, 500)
429
circle_points = []
430
import math
431
for i in range(32):
432
angle = 2 * math.pi * i / 32
433
x = int(500 + 200 * math.cos(angle))
434
y = int(250 + 200 * math.sin(angle))
435
circle_points.append(db.Point(x, y))
436
circle = db.Polygon(circle_points)
437
438
# Add shapes to layers
439
top_cell.shapes(metal1).insert(rect)
440
top_cell.shapes(via1).insert(circle)
441
442
# Save layout
443
layout.write("simple_layout.gds")
444
```
445
446
### Working with Cell Hierarchies
447
448
```python
449
import klayout.db as db
450
451
layout = db.Layout()
452
453
# Create cells
454
top_cell = layout.create_cell("TOP")
455
sub_cell = layout.create_cell("SUBCELL")
456
457
# Add content to subcell
458
layer = layout.layer(db.LayerInfo(1, 0))
459
sub_cell.shapes(layer).insert(db.Box(0, 0, 100, 100))
460
461
# Create instances of subcell in top cell
462
for x in range(0, 1000, 200):
463
for y in range(0, 500, 150):
464
trans = db.Trans(db.Point(x, y))
465
instance = db.CellInstArray(sub_cell.cell_index, trans)
466
top_cell.insert(instance)
467
468
layout.write("hierarchical_layout.gds")
469
```