Import a geometry from a dxf file into a Specificed cell and layer with python macros

Hello

I currently import my geometries from DXF-files from the GUI with Import>Other files into current.
This works perfectly fine, but as I have to this repetitively for a high number of geometries I want to write a python macro to automize this import.
From the documentation and other discussions here, I put together the following script:

'''
Info:
Scrip that imports a geometry from a dxf file into an existing layout on a specified layer& Cell
'''

#Package imports
import pya


#Get Current Layout
lyt=pya.CellView.active().layout()
#Get Top Cell
TopCell=lyt.top_cell()
#Define the desired layer to input the geometry
lyr=lyt.layer(pya.LayerInfo(2,0))
#Define the resulting shapes:
Shapes=TopCell.shapes(lyr); # Shapes to put in the Die labels

#Define the Path of the dxf file to import
dxfPath="Path/File.dxf" #just an example

#Define the load options
load_options = pya.LoadLayoutOptions()
load_options.dxf_dbu=0.001
load_options.dxf_unit=1
load_options.dxf_polyline_mode=2

#Set the layer map of the load options
lmap=pya.LayerMap()
lmap.map("0-255/0-255", lyr)
load_options.dxf_set_layer_map(lmap,True)

#Read/Load the dxf file:
lyt.read(dxfPath, load_options)

I don't get any errors when running this script, however there is no geometry imported.
I think it has something to do with layermapping asI don't fully understand how this works.

My overall goal is to load a geometry from a dxf file and import it into a specified cell and layer of an existing lyaout.
What do I need to change/adapt?

Best regards and thanks for the help,
Stephan

Comments

  • edited February 25

    Hi @Stephan_Kohler,

    to emulate the "import layout" behavior you need to first read the DXF file and then copy over the content to the target layout.

    Like here:

    dxfPath="path/to/dxf"
    
    tmp = pya.Layout()
    
    # Define the load options
    load_options = pya.LoadLayoutOptions()
    load_options.dxf_dbu = 0.001
    load_options.dxf_unit = 1
    load_options.dxf_polyline_mode = 2
    
    # Set the layer map of the load options
    # Each mapping is of the form
    # <name>: <layer>/<datatype>
    lmap = pya.LayerMap()
    lmap.map("ONE: 1/0")
    lmap.map("L2D0: 2/0")
    lmap.map("LayerThree: 3/0")
    load_options.dxf_set_layer_map(lmap, False)
    
    # Load the DXF file
    tmp.read(dxfPath, load_options)
    
    lyt.top_cell().copy_tree(tmp.top_cell())
    
    pya.LayoutView.current().add_missing_layers()
    

    The layer mapping is best done the way it is shown above using a mapping string. In my case, the DXF file had three layers called "ONE", "L2D0" and "LayerThree".

    Also, don't forget "add_missing_layers" - otherwise the layout view will now show the new layers.

    Matthias

  • Hello Matthias

    Thank you a lot for your quick and good support.
    I copied your concept and my "automized" import now works perfect. :smiley: :+1:

    I also figured out that if I add: "pya.LayoutView.current().add_missing_layers()" to my initial code, it works but shows the geometry on a new layer "0 0/0". So I assume that the layer mapping I used was wrong.

    Best regards and many thanks,
    Stephan

  • Very good :)

    0/0 is a special layer in DXF (a kind of wildcard). Usually, geometry should not show up there if they are properly assigned to a layer, but it can appear inside blocks that lack destination layer mapping.

    Matthias

Sign In or Register to comment.