Scripting way to get all the vertices of a polygon?

edited March 2018 in Python scripting

Related to this question, there must be a scripting way to iterate through all the vertices of a polygon and a) print them out, or b) delete some of them to clean up a design if they meet certain conditions (eg, if N of them are in a very straight line only the endpoints need to be kept), or c) snap them to a grid if X or Y are within distance D of the grid, etc.

Also, can this iteration be done over all the polygons in the layer?

Is there an example of a script like this (in Python or Ruby) that I can use to get started?

Thanks in advance,
Rick

Comments

  • edited November -1

    Here's a starting point. Click on the polygon and run this script in the IDE (press F5 to get the IDE). It simply grabs the points of the polygon. The other functions you mention are certainly possible. For (b), I'd make an array of the xy coordinates, delete one, then make a NEW polygon and write it to the layout. For (c) similarly.

    # Extracts the xy points of the selected shape(s) and puts in csv file.
    module ExtractPoints
    
      outfile = "C:/path/to/output.csv"
      file = File.open(outfile, "w")
      file.puts("x,y") # Header row
    
      include RBA
    
      ly = CellView::active.layout
      lv = Application::instance.main_window.current_view
      cell = ly.top_cell
    
      lv.each_object_selected { |obj|
        shape = obj.shape
        if shape.is_polygon?
          shape.polygon.each_point_hull { |pt|
            x, y = pt.x*ly.dbu, pt.y*ly.dbu
            p "#{x}, #{y}" # Print it to the console
            file.puts "#{x}, #{y}" # Put it in a file
          }
        end
      }
      file.close
    
    end
    
  • edited March 2018

    Thanks David,
    That is a great starting point. Examples for Klayout was a great help, also. Item b) is almost working.

    Note to self: this code is Ruby, not Python. Fine - I'm learning two languages for the price of one!

    Rick

  • edited November -1

    Hi David,

    I noticed the "Examples" first time now ... great thing! Thanks a lot!

    Matthias

Sign In or Register to comment.