It looks like you're new here. If you want to get involved, click one of these buttons!
Hello everyone:
When I convert a client's gds file to dxf, an error message appears: "Top-level cell is not unique - DXF can only store a single top cell"
Is there a way to use the klayout script to automatically detect the top cell? And merge multiple top cells into one?
Hope to get your help
Comments
Hi,
Automatically detecting the multi-top cells is easy.
But I do not understand what you mean by
merge multiple top cells into one
.At least, it should be very risky because merging can easily destroy the designer's intentions.
A designer (providing a GDS file) should specify which cell should be treated as the top cell if the GDS file contains multiple top cell candidates.
I have prepared three GDS files using KLayout's test data set.
To be continued.
Cont.
The attached Python script
f2514.py
has the following options.With the
-o
option, it can generate the DXF files.If there are multiple top cells, DXF files will be top-cell-wise.
To be continued.
Cont.
Generated DXF files are attached here.
I have confirmed that the script
f2514.py
can run on Linux, Windows 10, and Mac.Kazzz-S
Hi sekigawa:
Thank you very much for your reply. My requirement is to convert each top cell in the gds file into a dxf file. I don't want to run the klayout gui interface. I want to achieve this function through the command line under Windows. Because I am a newbie to klayout, I will study the f2514.py file carefully and report any progress immediately.
In addition, I wrote a simple script, but why can't I get the passed parameters?
I really hope to get your guidance
Hi,
OK.
f2514.py
already assumed these requirements. To runf2514.py
in a terminal, you need to install:1. an ordinary Python3 (I'm using 3.11.x) ===> https://www.python.org/downloads/
and
2. The klayout Python Module ===> https://www.klayout.org/klayout-pypi/
KLayout's GUI application is NOT required.
After installing the Python 3.11.x, execute
python3 -m pip install klayout
for installing klayout-pypi.Take your time
I believe https://www.klayout.org/klayout-pypi/ is a good starting point.
Kazzz-S
Cont.
In the second half, you try to execute the script by running the KLayout GUI application in batch mode. In fact, this is not required for your application, as I mentioned above.
I found your script can run if the arguments "123 456" are not passed. Am I right?
In such a use, the klayout executable is not behaving as a normal Python.
To pass a variable to the script via the klayout executable, we must use a special option
-rd <name>=<value>
.The list below shows all the options for the klayout executable captured on Mac.
If I pass the arguments, they are assumed to be files, like
Kazzz-S
Hi,
The environment settings shown in the attached document (thanks to ChatGPT) are required to run a Python script on a Windows terminal.
Just for your information.
Kazzz-S
hi sekigawa:
Thank you for your patience
I have fully understood the content of f2514.py, which can realize my needs, but I now have two problems now
1 I run on the Windows system, but I did not find Which Klayout, only klayout_app.exe klayout_vo_app.exe, I am looking at the command line to find by where klayout to find
2 -RD = I have tested to pass the parameters in this way in the command line
klayout_vo_app -b -r C: \ users \ rj_84 \ klayout \ pymacros \ new_folder \ new_python_macro_1.py rd 123 = 456
But how to get this parameter in py?
I hope to get your guidance, thank you again!
hi sekigawa:
I tried to obtain the 123 parameters in PY code in PY code that failed to get the 123 parameters
Hi @wildwolfcj,
You need neither


klayout_app.exe
norklayout_vo_app.exe
as long as you use the KLayout Python Module ===> https://www.klayout.org/klayout-pypi/.It is a different product from the KLayout GUI application.
If your application does not need to use the full KLayout's features, like in this case, using this module should suffice.
However, we must use the KLayout GUI application to check the conversion data and results visually.
Running the KLayout GUI application in batch mode (you are trying now) was a single solution before this Python module was provided.
Suppose you invoke like
klayout_app.exe -b -r /path/to/your/script.py -rd param1=123 -rd param2=abc
.Then,
/path/to/your/script.py
will internally have global string objectparam1
andparam2
.The object names are case-sensitive.
Beware that since
param1
is to be a number, you must type-cast it to an integer or float inside the script before use.The attached ZIP file contains a similar GDS2-->DXF converter I wrote many years ago.
gds2dxf.py
is the main command line I/F.kgds2dxf.py
is the actual worker.The former invokes the latter as a subprocess.
Compare this with
f2514.py
. Do you think which is leaner and more elegant?Again, I emphasize that you should use the KLayout Python Module for your application.
Hope I could convince you.
Kazzz-S
Dear @wildwolfcj,
I agree with @sekigawa, for such applications, it is easier to use the Python module. Specifically as it installs with the major Python distributions on Windows, such as Anaconda.
I'd like to add a few things:
Cell#write
on each top cell to write that one to a specific file, like this (Python):For the options see the "dxf_..." attributes here: https://www.klayout.de/doc-qt5/code/class_SaveLayoutOptions.html#k_1
Regards,
Matthias
hi Matthias and sekigawa:
Thank you very much for your patient guidance. According to the above information, I have been able to successfully convert gds containing multiple topcells into different dxf files. I will study the KLayout Python Module method to convert in the future.
Thanks again
hi Matthias and sekigawa:
After gds successfully converted dxf, I went to get the unit of the dxf file, but the value I got was
public enum DrawingUnits { // // 摘要: // Unitless. Unitless, // // 摘要: // Inches. Inches, // // 摘要: // Feet. Feet, // // 摘要: // Miles. Miles, // // 摘要: // Millimeters. Millimeters, // // 摘要: // Centimeters. Centimeters, // // 摘要: // Meters. Meters, // // 摘要: // Kilometers. Kilometers, // // 摘要: // Microinches. Microinches, // // 摘要: // Mils (a thousandth of an inch). Mils, // // 摘要: // Yards. Yards, // // 摘要: // Angstroms. Angstroms, // // 摘要: // Nanometers. Nanometers, // // 摘要: // Microns. Microns, // // 摘要: // Decimeters. Decimeters, // // 摘要: // Decameters Decameters, // // 摘要: // Hectometers. Hectometers, // // 摘要: // Gigameters. Gigameters, // // 摘要: // AstronomicalUnits. AstronomicalUnits, // // 摘要: // LightYears. LightYears, // // 摘要: // Parsecs. Parsecs, // // 摘要: // US Survey Feet USSurveyFeet, // // 摘要: // US Survey Inches USSurveyInches, // // 摘要: // US Survey Yards USSurveyYards, // // 摘要: // US Survey Miles USSurveyMiles } }
Unitless, how to convert the units in gds to dxf?
Hope to get your help
Hi,
How did you get the above strings?
Basically, a DXF file is unitless.

The
UNITS
in the sample GDS2 files are:To be continues.
Cont.
If you assume
DXF's coordinate is in [mm]
, you must provide some more parameters to theSaveLayoutOptions
object like:The attached
f2415b.zip
containsf2415b.py
, a modified version off2415.py
.It also contains
(1) ringo.dxf
and(2)ringo_units.dxf
.(1) was generated by
f2415b.py
. Then, I generated (2) from (1) by manually inserting$INSUNITS
(see above ChatGPT's instruction).Both Autodesk DWG Trueview and QCad Professional view the two DXF files.
Kazzz-S
Cont.
Suppose
f2514.py
convertedringo.gds
toringo-A.dxf
; similarlyf2514b.py
toringo-B.dxf
.As seen in the images below, the coordinate values in the former DXF are 1000 times as large as the latter.
However, if the coordinate unit in the former is assumed to be [micron] and the latter be [mm], both designs are identical.
Kazzz-S
Hi sekigawa:
Thank you very much for your patient reply, it helps me a lot, thank you again
Very good, thanks @sekigawa for clarifying this
I understand that DXF has views to provide dimensional drawings, but my knowledge is very limited here. I want to look into EazyDXF (https://pypi.org/project/ezdxf/) to see how they are embedding views, but I did not have time for this yet.
Matthias