Copying a layer and its content to a new file

edited May 2022 in Python scripting

I have tried the following code:

filename="..."
import pya
lo = pya.LoadLayoutOptions()
layout.read(filename+".dxf")
layout2 = pya.Layout()
layout.copy_layer(layout.layer(40,0),layout2.layer(40,0))
filename2=filename+"_EL"
layout2.write(filename2+".dxf")

Problems:
1. This script generates a new dxf file adding to it the layer 40/0 but not its content. I would like to copy all the shapes in the layer.
2. I would like to iterate on all layers generating a new dxf file for each layer with name filename+"_"+layername. Here the problem is that on my Linux machine I am not able to create a LayoutView (see my other discussion). Therefore I can not use the following iteration code which is described in another thread:

lv = pya.LayoutView.current()
li = lv.begin_layers()
while not li.at_end():
..........

Comments

  • Well ...

    I guess there is much confusion about the concepts here.

    First of all, a LayerView is only needed to display a layout. "layers" in the LayoutView context describe how the layers are displayed (colors etc.), which layers are shown and which are ignored etc.

    Behind the scene (sometimes called the "database"), there are the Layout objects which store the geometrical data. Layers in the context of a Layout object are designated by a short descriptor (layer/datatype numbers in GDS) and addressed in the API by a "layer index" - this is a simple integer.

    Beside that and orthogonal to the layer stack there is a the cell hierarchy. There typically is a single top cell and a couple of child cells forming the hierarchy tree.

    Copying over a layer from one layout to another is by far not as simple as calling "copy" - this is an intra-layout copy only.

    If you want to extract a layer from layout, the easiest way is to save the original layout with specific "SaveLayoutOptions" that only select certain layers. The code is like this:

    import klayout.db as db
    
    ly = db.Layout()
    ly.read("x.gds")
    
    for li in ly.layer_indexes():
    
      lp = ly.get_info(li)
    
      target = "x_%d-%d.gds" % (lp.layer, lp.datatype)
    
      opt = db.SaveLayoutOptions()
    
      # specifies to write only layer index "li" with target layer/datatype taken 
      # from the original "lp":
      opt.add_layer(li, lp)
    
      ly.write(target, opt)
    
      print(target + " written.")
    

    (assuming you're using the PyPI python module or the pymod). If you use the Klayout app or the "strmrun" binary, replace "import klayout.db as db" by "import pya as db".

    Matthias

  • Thanks for the explanation Mathhias. I will use this snippet of code to improve my python script for generating a step file.
    You may look at the post https://github.com/realthunder/FreeCAD/issues/419
    to see the current status of this script. As you may see there I have used:

    import pya
    layout = pya.Layout()

    But I prefer your solution:

    import klayout.db as db
    layout = db.Layout()

    because it is available in the installed package "klayout_0.27.9-1_amd64.deb"

    Also your way to separate layer data on different files is better than mine (reading many times from original file and deleting all other layers).

    Apart of these changes, to complete the script, it is necessary to read the stackup and to perform the extrudes.
    May I ask you if there is an automatic way to read the stackup of the klayout project created from the the dxf file. After opening the dxf file with klayout I may look in the technology manager for the location of the lyp file associated with the used technology. But where is stored this information ?
    I can not find any klayout project file associated with the dxf file.

  • @wsteffe I'm not sure if I understand correctly. A .lyp file is only required to display layout. If you read a DXF file you will find a number of layers with the layer names from the DXF file in the Layout object.

    A DXF file will read "named" layers and you can get the name from

    lp = ly.get_info(li)
    # gives the layer name
    lp.name
    

    If you use the PyPI module you cannot access the .lyp properties as there is not layout view.

    Matthias

  • edited May 2022

    Sorry, I have wrongly written lyp file. I should have written instead lyt file (that one including Z stack info).

    For the moment I am assuming that the user of my script has to extract the z information from the lyt file (maintaining the same format) and put it into another file with extension ".stack". Then he may call the script with following syntax:
    layout2fc.py -stack stack_file dxf_file

    See also https://forum.freecadweb.org/viewtopic.php?f=22&p=596822#p596822

    In a feature (when the current problems are solved) you may want to integrate my script into a klayout export command. Then you will have to automatically extract the z data and pass it to the script.

Sign In or Register to comment.