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


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:

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
#Get Top Cell
#Define the desired layer to input the geometry
#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()

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

#Read/Load the dxf file:, 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,


  • 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:

    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()"ONE: 1/0")"L2D0: 2/0")"LayerThree: 3/0")
    load_options.dxf_set_layer_map(lmap, False)
    # Load the DXF file, load_options)

    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.


  • 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,

  • 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.


Sign In or Register to comment.