Can I remove a connected line in images file .dxf

i have a code to convert file dxf to png.

layout = db.Layout()
layout.read(input_dxf_file)
layout_view = lay.LayoutView()
layout_view.show_layout(layout, False)
layout_view.set_config("grid-visible", "false")
layout_view.set_config("background-color", "#FFFFFF")
layout_view.set_config("child-context-color", "#000000")
layout_view.set_config("no-stipple", "true")
layout_view.max_hier()

top_cell = layout.cell(0)
layout_view.active_cellview().cell = top_cell
for lyp in layout_view.each_layer():
    if lyp.layer_index() == 1:
        lyp.frame_color = blue

box = top_cell.bbox()
layout_view.save_image_with_options(output_dxf_file, box.width()//10, box.height()/10)

my output is:

my expected output is:

Could you give me an idea. Thanks a lot

Comments

  • edited November 2023

    Hi hieund,

    Here's an example of how to remove cut lines and only show border lines.
    This script is to split the holes in every polygons into separate shape and insert them back into the same layer.

    import pya
    input_file = r"dxf path"
    output_img = r"image path"
    
    layout_view = pya.LayoutView()
    layout_view.load_layout(input_file, True)
    layout_view.set_config("grid-visible", "false")
    layout_view.set_config("background-color", "#FFFFFF")
    layout_view.set_config("child-context-color", "#000000")
    layout_view.set_config("no-stipple", "true")
    layout_view.max_hier()
    
    cell    = layout_view.active_cellview().cell
    layout  = cell.layout()
    linfo   = layout.layer_infos()[0]
    layer   = layout.layer(linfo)
    
    region  = pya.Region([ shape.polygon for shape in cell.each_shape(layer) ])
    #put everything in frst layer into a region
    
    cell.shapes(layer).clear()
    # remove shapes in target layer after shapes is transfered to region
    
    cell.shapes(layer).insert(region.hulls())
    # insert the outer border of each shape, holes excluded.
    
    cell.shapes(layer).insert(region.holes())
    # insert the hole border of each shape
    
    layout_view.save_image_with_options(output_img, 500, 500)
    
  • edited November 2023

    @RawrRanger Thank for your help but i try in this file but it not working

  • Hi hieund,

    I've revised the example, the majot different is to flatten the top cell before dumpping shape to region.

    import pya
    input_file = r"dxf path"
    output_img = r"image path"
    
    layout_view = pya.LayoutView()
    layout_view.load_layout(input_file, True)
    layout_view.set_config("grid-visible", "false")
    layout_view.set_config("background-color", "#FFFFFF")
    layout_view.set_config("child-context-color", "#000000")
    layout_view.set_config("no-stipple", "true")
    layout_view.max_hier()
    
    lid     = 1
    # you might need to change Layer id if you have multiple layers
    
    cell    = layout_view.active_cellview().cell
    
    cell.flatten(True)
    #this remove cell structure to ensure 'each shape' works correctly
    
    region = pya.Region([ shape.polygon for shape in cell.each_shape(lid)]).merge()
    #put everything in frst layer into a region
    
    cell.shapes(lid).clear()
    # remove shapes in target layer after shapes is transfered to region
    
    cell.shapes(lid).insert(region.hulls())
    # insert the outer border of each shape, holes excluded.
    
    cell.shapes(lid).insert(region.holes())
    # insert the hole border of each shape
    
    layout_view.save_image_with_options(output_img, 1500, 1500)
    
  • edited November 2023

    Hi Mr RawrRanger,

    Im thanks a lot for your support and guidance and I really appreciate it.

    But i see a abnormal about image convert, there are maybe missing some box


    It doesn't like my expected result.

    So, Is this a limitation of the klayout library ?

    Because, I try to convert but i get the same result like you ( ~ missing some box):

    Thanks you so much .... :* :* :*

  • Hi hieund,

    This version extracts out lines from every shapes and removes the cut lines.

    This approach cannot determine which area is Cu traces and which is spacing

    so I cannot hight light the Cu spacing region with another color.

    another difference is the rect shapes in the layout, this will be preserved unless addidional shape filter is applied.

    here's the result:

    import pya
    
    input_file = r"dxf path"
    output_img = r"image path"
    
    layout_view = pya.LayoutView()
    layout_view.load_layout(input_file, True)
    layout_view.set_config("grid-visible", "false")
    layout_view.set_config("background-color", "#FFFFFF")
    layout_view.set_config("child-context-color", "#000000")
    layout_view.set_config("no-stipple", "true")
    layout_view.max_hier()
    
    lid     = 1
    cell    = layout_view.active_cellview().cell
    
    def layerToRegion(cell, lid):
        region = pya.Region()
        rsi    = cell.begin_shapes_rec(lid)
        while not(rsi.at_end()):
            region.insert(rsi.shape().polygon.transformed(rsi.trans()))
            rsi.next()
        return region
    
    region = layerToRegion(cell, lid)
    cell.clear()
    for i in range(5) : cell.shapes(lid).insert(region.merged(i))
    layout_view.save_image_with_options(output_img, 1500, 1500)
    
  • Thanks a lot Mr @RawrRanger,

    Could I answer one more question ?

    How can I fill black color for all holes and hulls like that in your example code:

    Thanks and best regard.
    Hieund

  • edited November 2023

    Hi hieund,

    Following script kind of work for this particular layout, due to the soruce data have alot of stacked polygons,

    If the source pattern stacking rule changes, this code will not work.

    The process is to dump all shapes into region, and use this line to process the holes.

    result = region.merged() - (region.merged().holes() & region.merged(2).holes())

    This hard coded to remove shapes that in side a loop shape of stacked polygon.
    and the criteria for stacked polygon is the shape stacked twice.

    For color filling, I changed background into black and fill the layer into solid white to match the color theme.

    import pya
    
    input_file = r"C:\Users\scott\Downloads\random.dxf"
    output_img = r"C:\Users\scott\Downloads\random.png"
    
    layout_view = pya.LayoutView()  
    layout_view.load_layout(input_file, True)
    layout_view.set_config("grid-visible", "false")
    layout_view.set_config("background-color", "#000000")
    layout_view.max_hier()
    
    def layerToRegion(cell, lid):
        region = pya.Region()
        rsi    = cell.begin_shapes_rec(lid)
        while not(rsi.at_end()):
            region.insert(rsi.shape().polygon.transformed(rsi.trans()))
            rsi.next()
        return region
    
    def setAllLayerColor(layout_view, colorHex):
        layer_iter = layout_view.begin_layers()
        while not(layer_iter.at_end()):
            lyp                    = layer_iter.current()
            new_lyp                = lyp.dup()
            new_lyp.fill_color     = int(colorHex, 16)
            new_lyp.frame_color    = 0
            new_lyp.dither_pattern = 0
            layout_view.set_layer_properties(layer_iter, new_lyp)
            layer_iter.next()
        layout_view.add_missing_layers() 
    
    lid     = 1
    cell    = layout_view.active_cellview().cell
    layout  = cell.layout()
    region  = layerToRegion(cell, lid)
    cell.clear()
    
    result   = region.merged() - (region.merged().holes() & region.merged(2).holes())
    
    cell.shapes(lid).insert(result)
    setAllLayerColor(layout_view, "FFFFFF")
    layout_view.save_image_with_options(output_img, 1500, 1500)
    
  • @RawrRanger thanks a lot for your supported and I really appreciated that.

  • About how this line works.

    result = region.merged() - (region.merged().holes() & region.merged(2).holes())

  • Hi @RawrRanger, Thanks for the nice scripts, they are impressive!

    Hi @hieund,
    In my opinion, this is a limitation not only of KLayout, but of any commercial CAD tool.
    The reason lies in your original design itself.
    Let me explain this case by case.

    Case-1: AutoCAD and QCad comparison of the original design

    As shown below, they disagree. To me, both are wrong.

    Let's close to a specific location selected by KLayout.

    In fact, there are three polygons involved here; green, yellow, and pink.

    This creates a kind of three-body problem.
    That is, in what order and with what polarity (dark or clear) should each polygon be plotted?
    The potential ambiguity in the design causes the different (unexpected) results.

    A few years ago, I was faced with the exact same problem that a young engineer in my company had designed.
    The dialogue between us went something like this.


    Me: What were you going to do? Your design will cause a problem in a mask shop after the tape out.
    He: I was trying to create an alignment mark made from two polygons (green and yellow) with the same polarity.
    Me: OK! Then why pink?.
    He: I was taught this by a senior tutor when I was a trainee.
    By adding this (pink) polygon, the design looks like the final mask pattern in my CAD.
    Me: Hmmm
    Do you understand that a DXF file is basically a set of lines?
    And there is no concept of inside and outside? (*)
    He: No, sir.
    Me: That CAD is not suitable for designing a photomask.
    Do you think you can convince your boss to change the CAD system?
    He: Never, sir!!!


    (*) https://github.com/KLayout/klayout/issues/243#issuecomment-475077326

    Case-2: AutoCAD and QCad comparison of the design without pink

    They agree, as shown below. This is the case my young guy wanted.

    Case-3: AutoCAD and QCad comparison of the design without pink and yellow

    They also agree, as shown below. This is the case you wanted.

    Summary

    1. Correct the original design to eliminate the ambiguity.
    2. If you ultimately want a high-resolution black-and-white image without cut lines, consider generating a Gerber file with the appropriate polarities.
    3. Convert the Gerber file to a PDF file using another tool such as Gerber2PDF.

    I've attached relevant files.

    Best regards,
    Kazzz-S

  • Hi Kazzz-S

    Thanks for the explanation, matching the output from DXF format is always a guessing game,

    Shape filtering with conditions only works for certain layout and often breaks if encounter complex cases.

  • Dear all,

    thanks for this impressive discussion!

    @sekigawa's explanation exactly hits the point. DXF does not specify filled polygons. Instead it specifies the boundaries and it is up the the consumer to decide what is inside and outside.

    KLayout applies the even-odd rule. So closed contours form some "inside" and every point that is inside an odd number of contours is "filled" while points that are inside an even number of contours is not. In the cases I know this leads to the correct results. KLayout has some options to handle the formation of contours, specifically how to stitch contours from edges with small mismatches. Playing with the options my give better results, but still there is room for interpretation.

    Matthias

Sign In or Register to comment.