GithubHelp home page GithubHelp logo

Invert Entire GDS File about gdspy HOT 7 CLOSED

dorianherle avatar dorianherle commented on May 11, 2024
Invert Entire GDS File

from gdspy.

Comments (7)

heitzmann avatar heitzmann commented on May 11, 2024 3

It's hard to properly define a border and guarantee connectivity, if that's what you're looking for. You can use the offset function if connectivity is not an issue:

import gdspy

# Sample GDSII
c0 = gdspy.Cell('REF')
c0.add(gdspy.Rectangle((-2, -2), (2, 2)))

c1 = gdspy.Cell('MAIN')
c1.add(gdspy.Round((0, 0), 20, inner_radius=10))
c1.add(gdspy.Rectangle((0, 0), (20, 10)))
c1.add(gdspy.CellReference(c0, magnification=2))
c1.add(gdspy.CellArray(c0, 5, 5, (10, 10), (0, -20), rotation=45))

# Create an envolope by offseting the whole geometry
envelope = gdspy.offset(c1.get_polygons(), 2, join_first=True, layer=3)

# Invert cell MAIN
c2 = gdspy.Cell('INV')
c2.add(gdspy.fast_boolean(envelope, c1.get_polygons(), 'not'))
c1.add(envelope)

gdspy.LayoutViewer()

Otherwise, you should first get the convex hull of the geometry, and use that for inversion (with a proper offset, as well):

import gdspy
import numpy
import scipy.spatial

# Sample GDSII
c0 = gdspy.Cell('REF')
c0.add(gdspy.Rectangle((-2, -2), (2, 2)))

c1 = gdspy.Cell('MAIN')
c1.add(gdspy.Round((0, 0), 20, inner_radius=10))
c1.add(gdspy.Rectangle((0, 0), (20, 10)))
c1.add(gdspy.CellReference(c0, magnification=2))
c1.add(gdspy.CellArray(c0, 5, 5, (10, 10), (0, -20), rotation=45))

# Find convex hull and offset it by some amount
allpoints = numpy.vstack(c1.get_polygons())
hull = scipy.spatial.ConvexHull(allpoints)
envelope = gdspy.offset([allpoints[hull.vertices, :]], 2)

# Invert cell MAIN
c2 = gdspy.Cell('INV')
c2.add(gdspy.fast_boolean(envelope, c1.get_polygons(), 'not'))

gdspy.LayoutViewer()

from gdspy.

heitzmann avatar heitzmann commented on May 11, 2024 2

The example I showed before includes reference arrays. It works for any GDS layout.
Did you try to run it?

from gdspy.

heitzmann avatar heitzmann commented on May 11, 2024

Yes, you'll have to do a Boolean operation to subtract the existing geometry from a large enough rectangle (or whatever shape you prefer). Keep in mind that Boolean operations are computationally expensive, so it could take a long time to process depending on the number of vertices and polygons in the file.

Here is an example:

import gdspy

# Sample GDSII
c0 = gdspy.Cell('REF')
c0.add(gdspy.Rectangle((-2, -2), (2, 2)))

c1 = gdspy.Cell('MAIN')
c1.add(gdspy.Round((0, 0), 10, inner_radius=8))
c1.add(gdspy.Rectangle((0, 0), (20, 10)))
c1.add(gdspy.CellReference(c0, magnification=2))
c1.add(gdspy.CellArray(c0, 5, 5, (10, 10), (0, -20), rotation=45))

# Find bounding box and expand it by `margin`
margin = 2
p0, p1 = c1.get_bounding_box()
p0 = (p0[0] - margin, p0[1] - margin)
p1 = (p1[0] + margin, p1[1] + margin)

# Invert cell MAIN
c2 = gdspy.Cell('INV')
c2.add(gdspy.fast_boolean(gdspy.Rectangle(p0, p1), c1.get_polygons(), 'not'))

gdspy.LayoutViewer()

from gdspy.

dorianherle avatar dorianherle commented on May 11, 2024

Dear Lucas

thanks a lot for your answer. In my case, my gds file contains many cells and cell arrays. This is done as to create a memory efficient gds file. Here is an example:

import numpy
import gdspy

# parameters
diameter = 2
period = 4
N = 16

    
# initializing cells
unit_cell = gdspy.Cell('circle_unit_cell')    
array_cell = gdspy.Cell('circle_array_cell')

# unit_cell (1 circle)
unit_circle = gdspy.Round((0, 0), diameter/2, layer=1)
unit_cell.add(unit_circle)

# array_of unit_cels (N x N circles)
circle_array = gdspy.CellArray(unit_cell, N, N, (period, period))
array_cell.add(circle_array)


gdspy.LayoutViewer()

Then... how can I invert it ?

from gdspy.

dorianherle avatar dorianherle commented on May 11, 2024

Oh, please excuse me... I somehow missed that. Thanks a lot !! Yes ! It works ! Very nice ! Just one more follow up question: As you can see in your example there is a lot of space added when inverting the gds, because we apply the boolean to a simple rectangle. Do you think that it would be possible to create a border which traces along the shape of the gds file and then to use that to invert it ? Do you see what I mean ?

from gdspy.

dorianherle avatar dorianherle commented on May 11, 2024

Wow ! I am very impressed by your capabilities and your code ! Very nice.

If I compare it:

  1. Using Offset:
    offset

  2. Using Convex Hull:
    convex_hull

--> Convex Hull was what I was looking for ! :)
Thank you so much for helping me and creating this amazing package! 🥇

from gdspy.

heitzmann avatar heitzmann commented on May 11, 2024

No problem! I hope it works well for your case!

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.