Merging two polygons in a layer

Hi All,

I'm struggling with simply merging two polygons in a layer with Python scripting. Can someone help me figure this us?

Many thanks in advance.



  • edited March 2021

    there is a thread here on Ruby
    this can be converted to Python if needed if you know how,
    you will have to figure out the Booleans to satisfy your requirements

  • @tagger5896 Thanks very much for your response. I saw this thread before. However, I feel like this should be much easier than the thread. Also, what I'm trying to do should be a bit different from that thread.

    I'm trying to create a layout from scratch by scripting it. First, I create an array of polygons and then another array of different polygons that overlap the first ones. Then, I want to merge these two sets of polygons that make up a part of my layout.

    Thanks again for your help.

  • edited March 2021


  • edited March 2021

    1.) If by "merge" you mean "lump together", you can just insert all polygons into the same cell/shapes container

    2.) If by "merge" you mean "join polygons into one" ("union"), the easiest way to achieve this to work with the Region object as the thread suggests in a subcontext:

    p1 = ... 1st polygon ...
    p2 = ... 2nd polygon ...
    r = pya.Region(p1) + pya.Region(p2)
    for p r.merged().each():
      ... p gives you the polygons of the merged set (which there will be one in your case) ...

    Region is far more general than Polygon and covers all corner cases such as polygons touching in one point and offers much more functionality. It's basically a collection of polygons.


  • @Matthias Thanks very much for your thorough answer. That saved my life! :)

  • @Matthias @tagger5896
    I used the following script to merge the polygons, but it seems not working. Do I make a mistake here? :/

    for n in range(0, 4):
          box4 = pya.DBox(0.0, 0.0, self.planacon_port_width, self.planacon_port_height).moved(-(self.planacon_cavity_width/2)+self.port4_dis_left, -(self.planacon_cavity_length/2)+self.port4_dis_bottom)
          box4b = pya.DBox(0.0, 0.0, 1.1*self.port_extension_length, self.port_extension_width).moved(-(self.planacon_cavity_width/2)+self.port4_dis_left+self.planacon_port_width-(0.1*self.port_extension_length), -(self.planacon_cavity_length/2)+self.port4_dis_bottom+(self.planacon_port_height/2)-(self.port_extension_width/2))
          port4 = pya.DPolygon(box4)
          port4 = pya.DCplxTrans(1.0, False, False , 0.0,(self.planacon_port_height+self.planacon_port_pitch) * n) * port4
          port4_ext = pya.DPolygon(box4b)
          port4_ext = pya.DCplxTrans(1.0, False, False, 0.0, (self.planacon_port_height+self.planacon_port_pitch) * n) * port4_ext
          merged_port = pya.Region(port4)+pya.Region(port4_ext)
  • The "Merge" function perfectly works as mentioned above by @Matthias or by @tagger5896 when I make a layout and work within the layout environment as follow:

    region = pya.Region(planacon.begin_shapes_rec(metalrf))

    However, if I want to make a pcell, the above script of course doesn't work. My question is how I can change this script to be able to merge also within a pcell.

    Thank you for your support.

  • In a PCell you can also use the Region class. Instead of using a RecursiveShapeIterator (such as delivered by begin_shapes_rec), populate the region by inserting polygons with "insert".


Sign In or Register to comment.