How to convert gds file to dxf file through script

Hello, everyone
I am a newbie in klayout. Please tell me how to convert gds to dxf through a script. The following is the py code I wrote.
"import sys
from pya import *

def main():
# input_file and output_file are the paths of input and output files respectively
input_file = "D:\line4.GDS"
output_file = "D:\line4.dxf"

print("Input GDS file:", input_file)
print("Output DXF file:", output_file)

layout = Layout()
layout.read(input_file)
print("111")
try:
    dxf_stream = DxfOutput()
    print("222")
except Exception as e:
    print("Error:", e)
    return


print("222")

layout.write(dxf_stream)
print("333")

dxf_stream.save_as(output_file)
print("444")

print("Conversion complete.")

if name == "main":
main()
"
But I can't get the dxf file correctly. I hope I can get your help.

Comments

  • Hi @wildwolfcj,

    first, please format your code with a single line with triple backticks before and after (Markdown notation, same as on GitHub for example). Specifically Python code is impossible to read otherwise.

    Essentially the core functionality is:

    layout = Layout()
    layout.read("input.gds")
    layout.write("input.dxf")
    

    So what is DxfStream supposed to be? Did you get this code from ChatGPT?

    Matthias

  • Hello Matthias
    Thank you very much for your answer. The above code is provided by GPT. I will test the code you gave.
    I'm new to klayout script programming, can you give me some reference material?

  • Hello Matthias
    The code you improved can correctly convert gds format into dxf format, but the following error occurs when using netdxf to parse dxf files.

    Hope to get your guidance, thank you very much

  • edited April 2024

    Hi @wildwolfcj,

    KLayout writes a very simplistic form of DXF. DXF writing is not in the scope of the KLayout project. I may improve that in the future, but I am not an AutoCAD user, nor do I have specific knowledge about DXF.

    Apparently people have successfully used "ezdxf" to read KLayout-generated DXF files.

    You can try replacing the AutoCAD version by "AC1015" (AutoCAD 2000) which appears to be the first version supported by netdxf. The version is in line 8 of the generated DXF file.

    I'm afraid there is little else I can do as of now.

    Matthias

  • Hi @Matthias
    Thank you for your reply, your advice is very important to me

  • edited April 2024

    Hi @Matthias
    I call py code passing parameters through this command line
    D :\codes\GDSII\klayout-0.27.13-win64\klayout-0.27.13-win64>klayout_app.exe -zz -r D :\project\pythonProject\convert_gds_to_dxf.py D :\line4.GDS D :\line44 .dxf > d :\test123.txt
    Why can't D :\line4.GDS and D :\line44.dxf be passed into the py parameters correctly?

  • Because KLayout is not a Python interpreter. It only embeds one.

    You have to use variables:

    klayout -rd varname=value ...
    

    Inside your script you can use the variable like this:

    # prints "value":
    print(varname)
    

    Matthias

Sign In or Register to comment.