Comments (3)
@peterxsu
It's not a simple matter of duplicate names that define identical subcells. Cells could have the same name and different contents. It could be dangerous for users to fall into this trap without knowing it.
In your case, if you know that cells with the same name are identical, the following code should give you an idea of what you have to do to merge the files:
import gdspy
def create_gdsii(n):
fname = 'file{}.gds'.format(n)
cell1 = gdspy.Cell('REFERENCED', True)
cell1.add(gdspy.Rectangle((0, -0.5), (1, 0.5)))
cell1.add(gdspy.Label('Cell from {} '.format(fname), (0, 0), 'e'))
cell2 = gdspy.Cell('COPY{}'.format(n), True)
cell2.add(gdspy.CellArray(cell1, 1, 1 + n, (0, 2)))
lib = gdspy.GdsLibrary()
lib.add([cell1, cell2])
lib.write_gds(fname)
return fname
# Create 2 example files
file1 = create_gdsii(1)
file2 = create_gdsii(2)
# Load first file
lib1 = gdspy.GdsLibrary(infile=file1)
# Extract all cells:
for cell in lib1.top_level():
lib1.extract(cell)
# Load second file
lib2 = gdspy.GdsLibrary(infile=file2)
# Change references based on cell names
# WARNING: this is no guarantee that the cells are identical!
for cell in lib2.cell_dict.values():
for element in cell.elements:
if (isinstance(element, gdspy.CellArray) or
isinstance(element, gdspy.CellReference)):
name = element.ref_cell.name
if name in gdspy.current_library.cell_dict:
element.ref_cell = gdspy.current_library.cell_dict[name]
# Manually extract all cells skipping duplicate names
for name, cell in lib2.cell_dict.items():
if name not in gdspy.current_library.cell_dict:
gdspy.current_library.add(cell)
gdspy.LayoutViewer()
Note that, if you are using several files, I'd advise against using the global gdspy.current_library
. Instead, you can work with a merged_lib
as follows:
import gdspy
def create_gdsii(n):
fname = 'file{}.gds'.format(n)
cell1 = gdspy.Cell('REFERENCED', True)
cell1.add(gdspy.Rectangle((0, -0.5), (1, 0.5)))
cell1.add(gdspy.Label('Cell from {} '.format(fname), (0, 0), 'e'))
cell2 = gdspy.Cell('COPY{}'.format(n), True)
cell2.add(gdspy.CellArray(cell1, 1, 1 + n, (0, 2)))
lib = gdspy.GdsLibrary()
lib.add([cell1, cell2])
lib.write_gds(fname)
return fname
# Create 2 example files
file1 = create_gdsii(1)
file2 = create_gdsii(2)
# Load first file into merged library
merged_lib = gdspy.GdsLibrary(infile=file1)
# Load second file
lib2 = gdspy.GdsLibrary(infile=file2)
# Change references based on cell names
# WARNING: this is no guarantee that the cells are identical!
for cell in lib2.cell_dict.values():
for element in cell.elements:
if (isinstance(element, gdspy.CellArray) or
isinstance(element, gdspy.CellReference)):
name = element.ref_cell.name
if name in merged_lib.cell_dict:
element.ref_cell = merged_lib.cell_dict[name]
# Manually extract all cells skipping duplicate names
for name, cell in lib2.cell_dict.items():
if name not in merged_lib.cell_dict:
merged_lib.add(cell)
gdspy.LayoutViewer(merged_lib)
from gdspy.
Thanks @heitzmann . Sorry for not seeing this earlier.
Is there a reason that add has the overwrite_duplicate functionality then, and that's not considered too dangerous to give as an option?
from gdspy.
@peterxsu I see your point. The difference there is that when using GdsLibrary.add
, the user is expected to be creating all cells in Gdspy, meaning that they know what they're doing. By giving this option in GdsLibrary.extract
I fell the issues with the references could be more problematic, because they are not really explicit.
But I think you are right, if the somewhat dangerous option is already there, we might as well make it available everywhere. Implemented in 150945a
from gdspy.
Related Issues (20)
- stuck in hierarchically with CellReferences HOT 3
- RobustPath : Unsupported types for -: 'tuple' and 'tuple' HOT 1
- Use scaling to reduce file size HOT 1
- the direction of polygon in the cellreference is wrong HOT 3
- may be it is a bug for GdsLibrary.new_cell() HOT 6
- 'Cell' object has no attribute 'elements' HOT 1
- gdspy installation in python 3.10. HOT 1
- How can I get the information of the instance?
- Use gdspy in Klayout Macro developement (reopened) HOT 1
- How to read a gds file data and export it as PNG? HOT 1
- gdspy.inside only checks for inside the bbox HOT 1
- GdsLibrary object has no attribute 'cells' HOT 2
- Multiple cells with name: FGCCTE in GDSII file HOT 2
- result of get_bounding_box() for a polygon is wrong HOT 2
- conda install gdspy
- overwrite_duplicate not working with (use_current_library = True) HOT 1
- After gdspy boolean OR operation, help HOT 1
- [0.9.46] Python extension build fails when the PYPI tarball is used HOT 1
- Python tests fail HOT 1
- Merging polygons with gdspy boolean vs Klayout merge produces different results
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gdspy.