Package: gdspy
# GDSPY README[![Boost Software License - Version 1.0](https://img.shields.io/github/license/heitzmann/gdspy.svg)](http://www.boost.org/LICENSE_1_0.txt)
[![Documentation Status](https://readthedocs.org/projects/gdspy/badge/?version=stable)](https://gdspy.readthedocs.io/en/stable/?badge=stable)
[![tests](https://github.com/heitzmann/gdspy/actions/workflows/testing.yml/badge.svg)](https://github.com/heitzmann/gdspy/actions/workflows/testing.yml)
[![Appveyor Status](https://ci.appveyor.com/api/projects/status/pr49a6bhxvbqwocy?svg=true)](https://ci.appveyor.com/project/heitzmann/gdspy)
[![Downloads](https://img.shields.io/github/downloads/heitzmann/gdspy/total.svg)](https://github.com/heitzmann/gdspy/releases)Gdspy is a Python module for creation and manipulation of GDSII stream files.
Key features for the creation of complex CAD layouts are included:Boolean operations on polygons (AND, OR, NOT, XOR) based on clipping algorithmPolygon offset (inward and outward rescaling of polygons)Efficient point-in-polygon solutions for large array setsGdspy also includes a simple layout viewer.Typical applications of Gdspy are in the fields of electronic chip design, planar lightwave circuit design, and mechanical engineering.## Future of GdspyIn trying to improve the performance of Gdspy for large layouts, we ended up concluding that the best way to reach our goal was to rewrite the critical parts of the library as a C extension.
It turns out that beside obvious functions, method calling has a big impact in performance due to the overhead it introduces.
The best solution was to re-design the whole project as a C++ library with a thin Python wrapper: thus was born [Gdstk, the GDSII Tool Kit](https://github.com/heitzmann/gdstk).Therefore, version 1.6 will be the last major release of Gdspy, with development focused only on bug fixes.
Users are encouraged to move from Gdspy to Gdstk: although their API is not 100% compatible, the new module should be familiar enough to allow a quick transition.## Installation### Dependencies:[Python](https://www.python.org/) (tested with versions 2.7, 3.6, 3.7, and 3.8)[Numpy](http://numpy.scipy.org/)C compiler (needed only if built from source)Tkinter (optional: needed for theLayoutViewerGUI)[Sphinx](https://www.sphinx-doc.org/) (optional: to build the documentation)### Linux / OS XOption 1: using [pip](https://docs.python.org/3/installing/):`sh python-mpip install--usergdspy `Option 2: download the source from [github](https://github.com/heitzmann/gdspy) and build/install with:`sh python setup.py install `### WindowsThe preferred option is to install pre-compiled binaries from [here](https://github.com/heitzmann/gdspy/releases).Installation viapipand building from source as above are also possible, but an appropriate [build environment](https://wiki.python.org/moin/WindowsCompilers) is required for compilation of the C extension modules.## DocumentationThe complete documentation is available [here](http://gdspy.readthedocs.io/).The source files can be found in thedocsdirectory.## SupportHelp support Gdspy development by [donating via PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=JD2EUE2WPPBQQ) or [sponsoring me on GitHub](https://github.com/sponsors/heitzmann)## History of changes### Version 1.6.13 (Apr 26, 2023)
* Allow ill-formed GDSII label anchors to be correctly loaded.### Version 1.6.12 (Jun 2, 2022)
* Fix inCell.get_texttypes.
* Allow labels to inherit transforms throughget_labels.### Version 1.6.11 (Jan 14, 2022)
* Fix inCell.write_svgwhen missing references.
* Speed improvements inCell.remove_polygons(thanks Troy for the contribution).### Version 1.6.10 (Nov 14, 2021)
* Fix inCell.get_polygons### Version 1.6.9 (Sep 23, 2021)
* Fix inCell.get_polygonswith specified layer and datatype.
* Raise error for duplicate cells when reading a GDSII file.### Version 1.6.8 (Aug 2, 2021)
* Fix inbooleanfor complex geometries that freeze the operation.### Version 1.6.7 (Jul 14, 2021)
* Fixes inbooleanfor bugs with self-intersecting holes and holes horizontal edges.
* Fix bug in warning message.### Version 1.6.6 (Jun 09, 2021)
* Fix error inPath.smoothnot finding_hobbyfunction.
* Allow precision specification in SVG output.### Version 1.6.5 (Jun 08, 2021)
* Support GDSII files with 0-padding at the end.
* Allow fixing and modifying GDSII file timestamps.
*Thanks Troy Tamas and Joaquin Matres for the fixes### Version 1.6.4 (Apr 23, 2021)
* Fix missing module import (thanks Troy Tamas for the fix).### Version 1.6.3 (Dec 28, 2020)
* Fix bounding box edge case (thanks Troy Tamas for the fix).### Version 1.6.2 (Dec 18, 2020)
* More efficient bounding box calculation (thanks to Troy Tamas for the contribution).
* Fix Label creation bug.### Version 1.6.1 (Oct 22, 2020)
* Fix SVG output whenLabelcontains special characters.### Version 1.6 (Aug 12, 2020)
* Added support for element properties.
* Added transformation support toCell.copy.
* Layer/datatype filtering inget_polygonsforCell,CellReferenceandCellArray.
* Layer/datatype filtering inLayoutViewer.
* Removed global cache_bounding_boxes.  Only cells cache their bounding boxes.
* Bug fixes (thanks Daniel Hwang for the contributions).
* Bug fix inCell.copywhere the whole dependency tree would be copied on a deep copy creation.### Version 1.5.2 (Feb 01, 2020)
* Added support for importing GDSII files containing BOX elements.
* Bug fix inGdsLibrary.extract(thanks collineps for finding the problem).### Version 1.5 (Dec 20, 2019)
* NewCell.write_svgfunction to export an SVG image of the cell.
* NewGdsLibrary.new_cellfunction to quickly create and add cells to a library.
*GdsLibrary.addcan update references when a cell is overwritten.
* AddedGdsLibrary.removeto allow cells to be properly removed from libraries.
* AddedGdsLibrary.rename_cellto rename cells in libraries.
* AddedGdsLibrary.replace_referencesto easily replace referenced cells in libraries.
*GdsLibrary.addcan add dependencies recursively.
* Iterating overGdsLibraryobjects yields all its cells.
* Iterating overCellobjects yield all its polygons, paths, labels and references.
* Breaking change to*.to_gdsfunctions in order to improve write efficiency (this should not be a problem for most users, sincegdspy.write_gdsandCell.write_gdsremain the same).
* Breaking change: renamedGdsLibrary.cell_dicttoGdsLibrary.cells.
* Deprecated:gdspy.current_library,gdspy.write_gds,gdspy.fast_boolen,GdsLibrary.extract.
* Bug fixes and better tests forFlexPathandRobustPath.### Version 1.4.3 (Nov 11, 2019)
* Bug fix forFlexPathandRobustPathreferences.### Version 1.4.2 (Oct 01, 2019)
* Bug fix inFlexPath.### Version 1.4.1 (Sep 20, 2019)
* Bug fixes (thanks to DerekK88 and Sequencer for the patches).### Version 1.4 (May 18, 2019)
* Revised [documentation](http://gdspy.readthedocs.io/).
* NewFlexPathandRobustPathclasses: more efficient path generation when using the original GDSII path specification.
* NewCurveclass: SVG-like polygon creation.
* AddedPolygonSet.mirror(thanks to Daan Waardenburg for the contribution).
* AddedPath.bezierto create paths based on Bézier curves.
* AddedPath.smoothto create paths based on smooth interpolating curves.
* Addedget_gds_unitsto get units used in a GDSII file without loading.
* Addedget_binary_cellsto load only the binary GDSII representation of cell from a file.
* Added argumenttolerancetoRound,Path.arc,Path.turn, andPath.parametricto automatically control the number of points in the final polygons.
* Added argumentbinary_cellsto GDSII writing functions to supportget_binary_cells.
* Added argumentrename_templatetoGdsLibrary.read_gdsfor flexible cell renaming (thanks to @yoshi74ls181 for the contribution).
* Changed return value ofsliceto avoid creating emptyPolygonSet.
* Added argumenttimestampto GDSII writing functions.
* ImprovedRoundto support creating ellipses.
* Added support for unlimited number of points per polygon.
* Added support for BGNEXTN and ENDEXTN when reading a GDSII file.
* Polygon creation warnings are now controlled bypoly_warnings.
* IncorrectanchorinLabelnow raises an error, instead of emitting a warning.
* Added correct support for radius inPolygonSet.filleton a per-vertex basis.
* Speed improvements in GDSII file generation (thanks to @fbeutel for the contribution) and geometry creation.
* Font rendering example using [matplotlib](https://matplotlib.org/) (thanks Hernan Pastoriza for the contribution).
* Expanded test suite.### Version 1.3.2 (Mar 14, 2019)
* Small fix for building on Mac OS X Mojave.### Version 1.3.1 (Jun 29, 2018)
*PolygonSetbecomes the base class for all polygons, in particularPolygonandRectangle.
* AddedCell.remove_polygonsandCell.remove_labelsfunctions to allow filtering a cell contents based, for example, on each element’s layer.
* AddedPolygonSet.scaleutility method.
* AddedPolygonSet.get_bounding_boxutility method.
* Added argumenttimestamptoCell.to_gds,GdsLibrary.write_gdsandGdsWriter.
* Addedunitandprecisionarguments toGdsLibraryinitialization and removed from itswrite_gdsmethod.
* Changed the meaning of argumentunitinGdsLibrary.read_gds.
* Improvedsliceto avoid errors when slicing in multiple positions at once.
* ImprovedPolygonSet.fractureto reduce number of function calls.
* Removed incorrect absolute flags for magnification and rotation inCellReferenceandCellArray.
* Minor bug fixes.
* Documentation fixes.
* Removed deprecated classes and functions.### Version 1.2.1 (Dec 5, 2017)
*GdsLibrarycan be created directly from a GDSII file
* Added return value toGdsLibrary.read_gds* Fixed return value ofGdsLibrary.add### Version 1.2 (Oct 21, 2017)
* Added newgdsii_hashfunction.
* Addedprecisionparameter to_chop,Polygon.fracture,Polygon.fillet,PolygonSet.fracture,PolygonSet.fillet, andslice.
* Included labels in flatten operations (addedget_labelstoCell,CellReference, andCellArray).
* Fixed bug in the bounding box cache of reference copies.
* Fixed bug in_chopthat affectedPolygon.fracture,PolygonSet.fracture, andslice.
* Other minor bug fixes.### Version 1.1.2 (Mar 19, 2017)
* Update clipper library to 6.4.2 to fix bugs introduced in the last update.
* License change to Boost Software License v1.0.### Version 1.1.1 (Jan 27, 2017)
* Patch to fix installation issue (missing README file in zip).### Version 1.1 (Jan 20, 2017)
* Introduction ofGdsLibraryto allow user to work with multiple library simultaneously.
* DeprecatedGdsImportin favor ofGdsLibrary.
* Renamedgds_printtowrite_gdsandGdsPrinttoGdsWriter.
* Development changed to Python 3 (Python 2 supported via [python-future](http://python-future.org/)).
* Added photonics example.
* Added test suite.
* Clipper library updated to last version.
* Fixedinsidefunction sometimes reversing the order of the output.
* Fixed rounding error infast_boolean.
* Fixed argumentdeep_copybeing inverted inCell.copy.
* Bug fixes introduced by numpy (thanks to Adam McCaughan for the contribution).### Version 1.0 (Sep 11, 2016)
* Changed to “new style” classes (thanks to Adam McCaughan for the contribution).
* Added a per-point radius specification forPolygon.fillet(thanks to Adam McCaughan for the contribution).
* Addedinsidefucntion to perform point-in-polygon tests (thanks to @okianus for the contribution).
* Moved from distutils to setuptools for better Windows support.### Version 0.9 (Jul 17, 2016)
* Added option to join polygons before applying anoffset.
* Added atranslatemethod to geometric entities (thanks John Bell for the commit).
* Bug fixes.### Version 0.8.1 (May 6, 2016)
* Newfast_booleanfunction based on the [Clipper](http://www.angusj.com/delphi/clipper.php) library with much better performance than the oldboolean.
* Changedoffsetsignature to also use the [Clipper](http://www.angusj.com/delphi/clipper.php) library (this changebreaks compatibilitywith previous versions).
* Bug fix for error when importing some labels from GDSII files.### Version 0.7.1 (June 26, 2015)
* Rebased to GitHub.
* Changed source structure and documentation.### Version 0.7 (June 12, 2015)
* New feature:offsetfunction.
* NewGdsPrintclass for incremental GDSII creation (thanks to Jack Sankey for the contribution).### Version 0.6 (March 31, 2014)
* Default number of points forRound,Path.arc, andPath.turnchanged to resolution of 0.01 drawing units.
*Path.parametricaccepts callablefinal_distanceandfinal_widthfor non-linear tapering.
* Added argumentendstoPolyPath.
* Added (limited) support for PATHTYPE inGdsImport.
* A warning is issued when aPathcurve has width larger than twice its radius (self-intersecting polygon).
* Added a random offset to the patterns inLayoutViewer.
*LayoutViewershows cell labels for referenced cells.
*get_polygonsreturns (referenced) cell name ifdepth< 1 andby_specis True.
* Bug fix inget_bounding_boxwhen empty cells are referenced.
* Bug fixes inGdsImportand many speed improvements in bounding box calculations (thanks to Gene Hilton for the patch).### Version 0.5 (October 30, 2013) - NOT COMPATIBLE WITH PREVIOUS VERSIONS
* MajorLayoutViewerimprovements (not backwards compatible).
* The layer argument has been repositioned in the argument list in all functions (not backwards compatible).
* Renamed argumentby_layertoby_spec(not backwards compatible).
* Error is raised for polygons with more vertices than possible in the GDSII format.
* Removed the global state variable for default datatype.
* Addedget_datatypestoCell.
* Added argumentsingle_datatypetoCell.flatten.
* Removedgds_imageand dropped the optional PIL dependency.### Version 0.4.1 (June 5, 2013)
* Added argumentaxis_offsettoPath.segmentallowing creation of asymmetric tapers.
* Added missing argumentx_reflectiontoLabel.
* Created a global state variable to override the default datatype.
* Bug fix inCellArray.get_bounding_box(thanks to George McLean for the fix)### Version 0.4 (October 25, 2012)
*Cell.get_bounding_boxreturnsNonefor empty cells.
* Added a cache for bounding boxes for faster computation, especially for references.
* Added support for text elements withLabelclass.
* Improved the emission of warnings.
* Added a tolerance parameter toboolean.
* Added better print descriptions to classes.
* Bug fixes in boolean involving results with multiple holes.### Version 0.3.1 (May 24, 2012)
* Bug fix in the fracture method forPolygonSet.### Version 0.3a (May 03, 2012)
* Bug fix in the fracture method forPolygonandPolygonSet.### Version 0.3 (April 25, 2012)
* Support for Python 3.2 and 2.7
* Further improvements to thebooleanfunction via caching.
* Added methodsget_bounding_boxandget_layerstoCell.
* Added methodtop_leveltoGdsImport.
* Added support for importing GDSII path elements.
* Added an argument to control the verbosity of the import function.
* Layer -1 (referenced cells) sent to the bottom of the layer list by default inLayoutViewer* The text and background of the layer list inLayoutViewernow reflect the colors of the outlines and canvas backgroung.
* Changed default background color inLayoutViewer* Thanks to Gene Hilton for the contributions!### Version 0.2.9 (December 14, 2011)
* AttributeCell.cell_listchanged toCell.cell_dict.
* Changed the signature of the operation inboolean.
* Order of cells passed toLayoutVieweris now respected in the GUI.
* Complete re-implementation of the boolean function as a C extension for improved performance.
* Removed precision argument inboolean. It is fixed at 1e-13 for merging close points, otherwise machine precision is used.
*gds_imagenow accepts cell names as input.
* Added optional argumentdepthtoget_polygons* Added option to convert layers and datatypes in imported GDSII cells.
* Argumentexclude_layersfromLayoutViewerchanged tohidden_layersand behavior changed accordingly.
* Shift + Right-clicking on a layer the layer-list ofLayoutVIewerhides/unhides all other layers.
* New buttons to zoom in and out inLayoutViewer.
* Referenced cells below a configurable depth are now represented by theirs bounding boxes inLayoutViewer.### Version 0.2.8 (June 21, 2011)
* GDSII file import
* GDSII output automatically include required referenced cells.
*gds_printalso accepts file name as input.
* Outlines are visible by default inLayoutViewer.
* Added background color option inLayoutViewer.
* Right-clicking on the layer list hides/unhides the target layer inLayoutViewer.
*Cell.cell_listis now a dictionary indexed by name, instead of a list.
* Added option to exclude created cells from the global list of cells kept inCell.cell_list.
*CellReferenceandCellArrayaccept name of cells as input.
* Submodules lost their own__version__.### Version 0.2.7 (April 2, 2011)
* Bug fixed in theboolean, which affected the way polygons with more vertices then the maximum were fractured.
*gds_imageaccepts an extra color argument for the image background.
* Screenshots takes fromLayoutViewerhave the same background color as the viewer.
* The functionsbooleanandslicenow also acceptCellReferenceandCellArrayas input.
* Added the methodfracturetoPolygonandPolygonSetto automatically slice polygons into parts with a predefined maximal number of vertices.
* Added the methodfillettoPolygonandPolygonSetto round corners of polygons.### Version 0.2.6 (February 28, 2011)
* When saving a GDSII file,ValueErroris raised if cell names are duplicated.
* Save screenshot fromLayoutViewer.
*gds_imageaccepts cells, instead of lists.
* Outlines supported bygds_image.
*LayoutViewerstores bounding box information for all visited layers to save rendering time.### Version 0.2.5 (December 10, 2010)
* Empty cells no longer break the LayoutViewer.
* Removed thegds_viewfunction, superseded by the LayoutViewer, along with all dependencies to matplotlib.
* Fixed a bug inbooleanwhich affected polygons with series of collinear vertices.
* Added a function toslicepolygons along straight lines parallel to an axis.### Version 0.2.4 (September 04, 2010)
* Added shortcut to Extents in LayoutViewer:Homeorakeys.
*PolygonSetis the new base class forRound, which might bring some incompatibility issues with older scripts.
*Roundelements,PolyPath,L1Path, andPath arc,turnandparametricsections are now automatically fractured into pieces defined by a maximal number of points.
* Default value formax_pointsin boolean changed to 199.
* Removed the flag to disable the warning about polygons with more than 199 vertices.  The warning is shown only forPolygonandPolygonSet.
* Fixed a bug impeding parallelparametricpaths to change their distance to each other.### Version 0.2.3 (August 09, 2010)
* Added thePolyPathclass to easily create paths with sharp corners.
* AllowNoneas item in the colors parameter ofLayoutViewerto make layers invisible.
* Added color outline mode toLayoutViewer(change outline color with the shift key pressed)
* Increased the scroll region of theLayoutViewercanvas
* Added a fast scroll mode: control + drag 2nd mouse button
* Created a new sample script### Version 0.2.2 (July 29, 2010)
* Changed the cursor insideLayoutViewerto standard arrow.
* Fixed bugs with the windows version ofLayoutViewer(mouse wheel and ruler tool).### Version 0.2.1 (July 29, 2010)
* Bug fix:gds_imagedisplays an error message instead of crashing whenPILis not found.
* Added classLayoutViewer, which uses Tkinter (included in all Python distributions) to display the GDSII layout with better controls then thegds_viewfunction. This eliminates thematplotlibrequirement for the viewer functionality.
* New layer colors extending layers 0 to 63.### Version 0.2.0 (July 19, 2010)
* Fixed a bug on theturnmethod ofPath.
* Fixed a bug on thebooleanfunction that would give an error when not usingPolygonorPolygonSetas input objects.
* Added the methodget_polygonstoCell,CellReferenceandCellArray.
* Added a copy method toCell.
* Added aflattenmethod toCellto remove references (or array references) to other cells.
* Fracturebooleanoutput polygons based on the number of vertices to respect the 199 GDSII limit.### Version 0.1.9 (June 04, 2010)
* AddedL1Pathclass for Manhattan geometry (L1 norm) paths.### Version 0.1.8 (May 10, 2010)
* Removed the argumentfillfromgds_viewand added a more flexible one:style.
* Fixed a rounding error on thebooleanoperator affecting polygons with holes.
* Added a rotate method toPolygonSet.
* Added a warning whenPolygonSethas more than 199 points
* Added a flag to disable the warning about polygons with more than 199 points.
* Added aturnmethod toPath, which is easier to use thanarc.
* Added a direction attribute toPathto keep the information used by thesegmentandturnmethods.### Version 0.1.7 (April 12, 2010)
* New visualization option: save the geometry directly to an image file (lower memory use).
* New functionality added: boolean operations on polygons (polygon clipping).
* All classes were adapted to work with the boolean operations.
* The attribute size in the initializer of classTextdoes not have a default value any longer.
* The name of the argumentformatin the functiongds_viewwas changed tofill(to avoid confusion with the built-in functionformat).### Version 0.1.6 (December 15,  2009)
* Sample script now include comments and creates an easier to understand GDSII example.
* Improved floating point to integer rounding, which fixes the unit errors at the last digit of the precision in the GDSII file.
* Fixed the font for character 5.
* Added a flag togds_viewto avoid the automatic call tomatplotlib.pyplot.show().
* Ingds_view, if a layer number is greater than the number of formats defined, the formats are cycled.### Version 0.1.5a (November 15, 2009)
* Class Text correctly interpretsnandtcharacters.
* Better documentation format, using the Sphinx engine and the numpy format.### Version 0.1.4 (October 5, 2009)
* ClassTextre-written with a different font with no overlaps and correct size.### Version 0.1.3a (July 29 2009)
* Fixed the functionto_gdsof classRectangle.### Version 0.1.3 (July 27, 2009)
* Added the datatype field to all elements of the GDSII structure.### Version 0.1.2 (July 11, 2009)
* Added thegds_viewfunction to display the GDSII structure using the matplotlib module.
* Fixed a rotation bug in the CellArray class.
* Module published under the GNU General Public License (GPL)### Version 0.1.1 (May 12, 2009)
* Added attributecell_listto class Cell to hold a list of all Cell created.
* Set the default argumentcells=Cell.cell_listin the functiongds_print.
* Added member to calculate the area for each element type.
* Added member to calculate the total area of a Cell or the area by layer.
* Included the possibility of creating objects in user-defined units, not only nanometers.### Version 0.1.0 (May 1, 2009)
* Initial release.