Comments (7)
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.
The example I showed before includes reference arrays. It works for any GDS layout.
Did you try to run it?
from gdspy.
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.
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.
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.
Wow ! I am very impressed by your capabilities and your code ! Very nice.
If I compare it:
--> Convex Hull was what I was looking for ! :)
Thank you so much for helping me and creating this amazing package! 🥇
from gdspy.
No problem! I hope it works well for your case!
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.