Coder Social home page Coder Social logo

Comments (3)

heitzmann avatar heitzmann commented on August 19, 2024

@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.

peterxsu avatar peterxsu commented on August 19, 2024

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.

heitzmann avatar heitzmann commented on August 19, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.