| 1 |
libGDSII is a C++ library for working with GDSII binary data files, intended primarily for use with the computational electromagnetism codes |
| 2 |
[scuff-em](http://homerreid.github.io/scuff-em-documentation) |
| 3 |
and |
| 4 |
[meep](http://meep.readthedocs.org) |
| 5 |
but sufficiently general-purpose to allow other uses as well. |
| 6 |
|
| 7 |
The packages consists of |
| 8 |
|
| 9 |
+ a C++ library (`libGDSII`) with API functions for reading, processing, and exporting GDSII files |
| 10 |
|
| 11 |
+ a command-line executable code (`GDSIIConvert`) for reporting statistics on GDSII geometries and |
| 12 |
exporting them to other file formats, notably including the [GMSH](http://gmsh.info) geometry format. |
| 13 |
|
| 14 |
# Installation |
| 15 |
|
| 16 |
```bash |
| 17 |
% git clone http://github.com/HomerReid/libGDSII |
| 18 |
% cd libGDSII |
| 19 |
% sh autogen.sh --prefix=/path/to/installation/prefix |
| 20 |
% make install |
| 21 |
``` |
| 22 |
|
| 23 |
This will install the `GDSIIConvert` executable in `$(prefix)/bin` |
| 24 |
and the `libGDSII.a` and/or `libGDSII.so` library binaries in `$(prefix)/lib`. |
| 25 |
|
| 26 |
# Using the `GDSIIConvert` command-line tool |
| 27 |
|
| 28 |
The GDSII file referenced in the following examples is |
| 29 |
[this silicon-photonics transceiver](https://github.com/lukasc-ubc/SiEPIC-Tools/blob/master/Examples/GSiP/RingModTransceiver/Layouts/GSiP_4_RingFilter.gds) |
| 30 |
from the [SiEPIC-Tools](https://github.com/lukasc-ubc) project: |
| 31 |
|
| 32 |
+ Lukas Chrostowski, Zeqin Lu, Jonas Flueckiger, Xu Wang, Jackson Klein, |
| 33 |
Amy Liu, Jaspreet Jhoja, James Pond, |
| 34 |
["Design and simulation of silicon photonic schematics and layouts,"](http://edx.org/course/silicon-photonics-design-fabrication-ubcx-phot1x) |
| 35 |
Proc. SPIE 9891, Silicon Photonics and Photonic Integrated Circuits V, 989114 (May 13, 2016); doi:10.1117/12.2230376. |
| 36 |
|
| 37 |
## Convert to [GMSH](http://gmsh.info) format: Option `--gmsh` |
| 38 |
|
| 39 |
```bash |
| 40 |
% GDSIIConvert --GMSH GSiP_4_RingFilter.gds |
| 41 |
|
| 42 |
Read 2080 data records from file GSiP_4_RingFilter.gds. |
| 43 |
Wrote 91 text strings to GSiP_4_RingFilter.pp. |
| 44 |
Detecting metallization structures on layer 1: ... 34 polygons on layer 1 |
| 45 |
Detecting metallization structures on layer 3: ... 30 polygons on layer 3 |
| 46 |
Detecting metallization structures on layer 7: ... 2 polygons on layer 7 |
| 47 |
Detecting metallization structures on layer 10: ... 78 polygons on layer 10 |
| 48 |
Detecting metallization structures on layer 20: ... 1 polygons on layer 20 |
| 49 |
Detecting metallization structures on layer 21: ... 1 polygons on layer 21 |
| 50 |
Detecting metallization structures on layer 44: ... 12 polygons on layer 44 |
| 51 |
Detecting metallization structures on layer 47: ... 30 polygons on layer 47 |
| 52 |
Detecting metallization structures on layer 60: ... 2 polygons on layer 60 |
| 53 |
Detecting metallization structures on layer 63: ... 2 polygons on layer 63 |
| 54 |
Detecting metallization structures on layer 66: ... 87 polygons on layer 66 |
| 55 |
Detecting metallization structures on layer 68: ... 16 polygons on layer 68 |
| 56 |
Detecting metallization structures on layer 69: ... 46 polygons on layer 69 |
| 57 |
Detecting metallization structures on layer 81: ... 2 polygons on layer 81 |
| 58 |
Detecting metallization structures on layer 733: ... 2 polygons on layer 733 |
| 59 |
Wrote 345 metallization polygons to GSiP_4_RingFilter.geo. |
| 60 |
Thank you for your support. |
| 61 |
``` |
| 62 |
|
| 63 |
This produces two files: the GMSH geometry file `GSiP_4_RingFilter.geo,` |
| 64 |
describing the polygons, and the GMSH post-processing file `GSiP_4_RingFilter.pp` |
| 65 |
describing the text strings. These can be opened in GMSH for visualization: |
| 66 |
|
| 67 |
```bash |
| 68 |
% gmsh GSiP_4_RingFilter.geo GSiP_4_RingFilter.pp |
| 69 |
``` |
| 70 |
|
| 71 |
 |
| 72 |
|
| 73 |
### Extracting individual layers |
| 74 |
|
| 75 |
If you only want to extract polygons on a single layer, you can |
| 76 |
add e.g. `--MetalLayer 47` to the `GDSIIConvert` command line: |
| 77 |
|
| 78 |
```bash |
| 79 |
% GDSIIConvert --GMSH --MetalLayer 47 GSiP_4_RingFilter.geo --FileBase GSIP_47 |
| 80 |
% GDSIIConvert --GMSH --MetalLayer 69 GSiP_4_RingFilter.geo --FileBase GSIP_69 |
| 81 |
``` |
| 82 |
|
| 83 |
## Print text description of geometry hierarchy: Option `--analyze` |
| 84 |
|
| 85 |
```bash |
| 86 |
|
| 87 |
% GDSIIConvert --analyze GSiP_4_RingFilter.gds |
| 88 |
|
| 89 |
Read 2080 data records from file GSiP_4_RingFilter.gds. |
| 90 |
* |
| 91 |
* File GSiP_4_RingFilter.gds: |
| 92 |
* Unit=1.000000e-06 meters (file units = {1.000000e-03,1.000000e-09}) |
| 93 |
* |
| 94 |
************************************************** |
| 95 |
** Library SiEPIC_GSiP: |
| 96 |
************************************************** |
| 97 |
-------------------------------------------------- |
| 98 |
** Struct 0: $$$CONTEXT_INFO$$$ |
| 99 |
-------------------------------------------------- |
| 100 |
Element 0: SREF (layer 0, datatype 0) |
| 101 |
(structure CIRCLE) |
| 102 |
(attribute 7: PCELL_CIRCLE) |
| 103 |
(attribute 6: P_actual_radius) |
| 104 |
(attribute 5: P_npoints) |
| 105 |
(attribute 4: P_handle___dpoint) |
| 106 |
(attribute 3: P_radius) |
| 107 |
(attribute 2: P_layer___layer) |
| 108 |
(attribute 1: LIB_Basic) |
| 109 |
(attribute 0: LIB_SiEPIC_GSiP) |
| 110 |
XY: 0 0 |
| 111 |
|
| 112 |
Element 1: SREF (layer 0, datatype 0) |
| 113 |
(structure OpticalFibre__micron) |
| 114 |
(attribute 1: CELL_OpticalFibre__micron) |
| 115 |
(attribute 0: LIB_SiEPIC_GSiP) |
| 116 |
XY: 0 0 |
| 117 |
|
| 118 |
Element 2: SREF (layer 0, datatype 0) |
| 119 |
(structure ROUND_PATH$) |
| 120 |
|
| 121 |
... |
| 122 |
... |
| 123 |
... |
| 124 |
Element 18: SREF (layer 0, datatype 0) |
| 125 |
(structure ROUND_PATH$) |
| 126 |
XY: 0 0 |
| 127 |
|
| 128 |
Element 19: SREF (layer 0, datatype 0) |
| 129 |
(structure ROUND_PATH$) |
| 130 |
XY: 0 0 |
| 131 |
|
| 132 |
Element 20: BOUNDARY (layer 20, datatype 0) |
| 133 |
XY: 228898 187725 228803 187791 228898 187931 228898 187725 |
| 134 |
|
| 135 |
Element 21: BOUNDARY (layer 21, datatype 0) |
| 136 |
XY: 234248 192138 234248 192595 234250 192366 234248 192138 |
| 137 |
|
| 138 |
Thank you for your support. |
| 139 |
``` |
| 140 |
|
| 141 |
## Print low-level description of GDSII file structure (data records): Option `--raw` |
| 142 |
|
| 143 |
```bash |
| 144 |
|
| 145 |
% GDSIIConvert --raw GSiP_4_RingFilter.gds |
| 146 |
|
| 147 |
Record 0: HEADER ( 1) = 600 |
| 148 |
Record 1: BGNLIB ( 12) = 2017 3 16 23 29 47 2017 3 16 23 29 47 |
| 149 |
Record 2: LIBNAME ( 1) = SiEPIC_GSiP |
| 150 |
Record 3: UNITS ( 2) = 0.001 1e-09 |
| 151 |
Record 4: BGNSTR ( 12) = 2017 3 16 23 29 47 2017 3 16 23 29 47 |
| 152 |
Record 5: STRNAME ( 1) = $$$CONTEXT_INFO$$$ |
| 153 |
Record 6: SREF = |
| 154 |
Record 7: SNAME ( 1) = CIRCLE |
| 155 |
Record 8: XY ( 2) = 0 0 |
| 156 |
Record 9: PROPATTR ( 1) = 7 |
| 157 |
Record 10: PROPVALUE ( 1) = PCELL_CIRCLE |
| 158 |
Record 11: PROPATTR ( 1) = 6 |
| 159 |
Record 12: PROPVALUE ( 1) = P_actual_radius |
| 160 |
... |
| 161 |
... |
| 162 |
... |
| 163 |
Record 2066: XY ( 2) = 0 0 |
| 164 |
Record 2067: ENDEL = |
| 165 |
Record 2068: BOUNDARY = |
| 166 |
Record 2069: LAYER ( 1) = 20 |
| 167 |
Record 2070: DATATYPE ( 1) = 0 |
| 168 |
Record 2071: XY ( 8) = 228898 187725 228803 187791 228898 187931 228898 187725 |
| 169 |
Record 2072: ENDEL = |
| 170 |
Record 2073: BOUNDARY = |
| 171 |
Record 2074: LAYER ( 1) = 21 |
| 172 |
Record 2075: DATATYPE ( 1) = 0 |
| 173 |
Record 2076: XY ( 8) = 234248 192138 234248 192595 234250 192366 234248 192138 |
| 174 |
Record 2077: ENDEL = |
| 175 |
Record 2078: ENDSTR = |
| 176 |
Record 2079: ENDLIB = |
| 177 |
Read 2080 data records from file SiEPIC/GSiP_4_RingFilter.gds. |
| 178 |
Thank you for your support. |
| 179 |
``` |
| 180 |
|
| 181 |
# Using the `libGDSII` API |
| 182 |
|
| 183 |
|
| 184 |
## Overview of the `libGDSII` API |
| 185 |
|
| 186 |
`libGDSII` exports a C++ class called `GDSIIData,` whose |
| 187 |
class constructor accepts the name of a binary GDSII file as input. |
| 188 |
|
| 189 |
Internally, the constructor maintains both a *hierarchical* representation |
| 190 |
of the geometry (involving structures that instantiate other structures, |
| 191 |
arrays of structure elements, etc.) and a *flat* representation, |
| 192 |
consisting simply of collections of polygons and text labels, |
| 193 |
indexed by the layer on which they appeared. |
| 194 |
|
| 195 |
The flat representation consists of |
| 196 |
|
| 197 |
+ an integer-valued array `Layers`, with `Layers[0], Layers[1], ...` the indices of the layers present in the GDSII file |
| 198 |
+ for each layer, an array of `Entity` structures, where each `Entity` is either a polygon or a text string: |
| 199 |
|
| 200 |
```C++ |
| 201 |
|
| 202 |
std::vector<int> Layers; // Layers[nl] = index of layer #nl in GDSII file |
| 203 |
|
| 204 |
typedef struct Entity |
| 205 |
{ char *Text; // if NULL, the entity is a polygon; otherwise it is a text string |
| 206 |
dVec XY; // vertex coordinates: 2 for a text string, 2N for an N-gon |
| 207 |
bool Closed; // true if there exists an edge connecting the last to the first vertex |
| 208 |
char *Label; // optional descriptive text, may be present or absent for polygons and texts |
| 209 |
} Entity; |
| 210 |
|
| 211 |
typedef std::vector<Entity> EntityList; |
| 212 |
typedef std::vector<EntityList> EntityTable; // EntityTable[nl][ne] = entity #ne on layer Layers[nl] |
| 213 |
|
| 214 |
``` |
| 215 |
|
| 216 |
In the flat representation, all GDSII geometry elements---including |
| 217 |
boundaries, boxes, paths, structure instantiations (`SREF`s), and |
| 218 |
arrays (`AREF`s)---are reduced to polygons, described by lists |
| 219 |
of *N>1* vertices, with each vertex having two coordinates (*x* and *y*). |
| 220 |
The *2N* vertex coordinates are stored internally in the `XY` field |
| 221 |
of `Entity`, a `double`-valued `std::vector.` The `Closed` flag |
| 222 |
indicates whether or not the polygon includes an edge connecting |
| 223 |
vertex *N* back to vertex *1*. |
| 224 |
|
| 225 |
If the `Text` field of `Entity` is non-NULL, the entity is not a polygon, |
| 226 |
but is instead a text string. In this case the `XY` array has length 2, |
| 227 |
the (*x,y*) coordinates of the text reference point. |
| 228 |
|
| 229 |
Both polygons and text strings may be accompanied by |
| 230 |
optional descriptive text, stored in the `*Label` field of `Entity.` |
| 231 |
|
| 232 |
`GDSIIData` provides API routines for extracting polygons from GDSII geometries. |
| 233 |
This can be done in a couple of ways: |
| 234 |
|
| 235 |
+ You can ask for a list of all polygons on a given layer. If there is more than one polygon on the layer, you will get them all, in the form of an unsorted array. |
| 236 |
|
| 237 |
+ You can specify a label (character string) and ask for only those polygons that contain the reference point of a text string, on the same layer, matching your string. You can restrict this search to a single layer, or search all layers in the file; in the latter case, only polygons that live on the same layer as the text string are returned. |
| 238 |
|
| 239 |
Arrays of polygons are returned in the form of a `vector` of `double`-valued `vectors`: |
| 240 |
|
| 241 |
```C++ |
| 242 |
typedef vector<double> dVec; |
| 243 |
typedef vector<dVec> PolygonList; // PolygonList[np][2*nv+0,2*nv+1] = x,y coords of vertex #nv in polygon #np |
| 244 |
``` |
| 245 |
|
| 246 |
## Sample code |
| 247 |
|
| 248 |
```C++ |
| 249 |
|
| 250 |
#include "libGDSII.h" |
| 251 |
using namespace std; |
| 252 |
using namespace libGDSII; |
| 253 |
|
| 254 |
/********************************************************************/ |
| 255 |
/* try to instantiate GDSIIData structure from binary GDSII file */ |
| 256 |
/********************************************************************/ |
| 257 |
GDSIIData *gdsIIData = new GDSIIData( string("GSiP_4_RingFilter.gds") ); |
| 258 |
|
| 259 |
if (gdsIIData->ErrMsg) |
| 260 |
{ printf("error: %s (aborting)\n",gdsIIData->ErrMsg->c_str()); |
| 261 |
exit(1); |
| 262 |
} |
| 263 |
|
| 264 |
/***************************************************************/ |
| 265 |
/* output a text-based description of the geometry */ |
| 266 |
/***************************************************************/ |
| 267 |
gdsIIData->WriteDescription(); // writes to console |
| 268 |
gdsIIData->WriteDescription("MyOutputFile"); |
| 269 |
|
| 270 |
/***************************************************************/ |
| 271 |
/* get all polygons on layer 3 *********************************/ |
| 272 |
/***************************************************************/ |
| 273 |
PolygonList Layer3Polygons = gdsIIData->GetPolygons(3); |
| 274 |
|
| 275 |
printf("Found %lu polygons on layer 3: \n",Layer3Polygons.size()); |
| 276 |
for(size_t np=0; np<Layer3Polygons.size(); np++) |
| 277 |
{ printf("Polygon #%lu has vertices: ",np); |
| 278 |
for(int nv=0; nv<Layer3Polygons[np].size()/2; nv++) |
| 279 |
printf(" {%e,%e} ",Layer3Polygons[np][2*nv+0],Layer3Polygons[2*nv+1]); |
| 280 |
printf("\n"); |
| 281 |
} |
| 282 |
|
| 283 |
/***************************************************************/ |
| 284 |
/* get all polygons on layer 3 that contain the reference point*/ |
| 285 |
/* of the text string "Geometry" (also on layer 3) */ |
| 286 |
/***************************************************************/ |
| 287 |
PolygonList Layer3Polygons = gdsIIData->GetPolygons("Geometry", 3); |
| 288 |
|
| 289 |
/***************************************************************/ |
| 290 |
/* get all polygons on any layer that contain the reference pt */ |
| 291 |
/* of the text string "Geometry" on the same layer */ |
| 292 |
/***************************************************************/ |
| 293 |
PolygonList Layer3Polygons = gdsIIData->GetPolygons("Geometry"); |
| 294 |
``` |
| 295 |
|
| 296 |
## Internal caching of `GDSIIData` structure |
| 297 |
|
| 298 |
In the above snippet, we create an instance of `class GSDIIData` |
| 299 |
and access the geometry by calling the class methods of this |
| 300 |
instance. `libGDSII` also provides an alternative interface |
| 301 |
in which you call non-class methods, passing only the GDSII file name: |
| 302 |
|
| 303 |
```C++ |
| 304 |
|
| 305 |
PolygonList Layer3Polygons = GetPolygons("MyGDSFile.GDS", 3); |
| 306 |
PolygonList Layer3Geometry = GetPolygons("MyGDSFile.GDS", "Geometry", 3); |
| 307 |
|
| 308 |
... |
| 309 |
... |
| 310 |
|
| 311 |
void ClearGDSIICache(); |
| 312 |
|
| 313 |
``` |
| 314 |
In this case what is going on is that `libGDSII` is creating and internally |
| 315 |
storing a `GDSIIData` structure for the given GDS file. Successive |
| 316 |
calls to `GetPolygons` with the same file name will re-use the cached data |
| 317 |
structure, saving the cost of rereading the GDS file. If you make |
| 318 |
subsequent calls to `GetPolygons` with a new GDS file, the cached data will |
| 319 |
be destroyed and allocated anew for the new file. |
| 320 |
|
| 321 |
When finished with a sequence of `GetPolygons()` calls of this form, |
| 322 |
call `ClearGDSIICache()` to deallocate memory associated with the |
| 323 |
internally-cached structures. |