Region constructor

edited January 3 in KLayout Support


For Region constructor, I want to ask whether we can specify the method to construct it? I mean like in Python, we can use keyword to restrict them, then we can use specific method to construct our classes.

Currently, I think the region constructor in KLayout select the way to create the class by determining the input automatically. But when I wrap the pya.Polygon into my own class, then I create my own polygon and use region to combine them, and the constructor failed. Here is the code, you can reproduce such problem.

import pya


class MyPolygon:
    def __init__(self, pts):
        self.obj = pya.DPolygon(pts)


if __name__ == '__main__':
    polygon1 = MyPolygon([pya.Point(0, 0),
                          pya.Point(0, 20),
                          pya.Point(20, 20),
                          pya.Point(20, 0),
                          pya.Point(0, 0)])
    polygon2 = MyPolygon([pya.Point(0, 0),
                          pya.Point(0, -20),
                          pya.Point(-20, -20),
                          pya.Point(-20, 0),
                          pya.Point(0, 0), ])
    print(polygon1.obj)  # (0,0;0,20;20,20;20,0;0,0)
    print(polygon2.obj)  # (-20,-20;-20,0;0,0;0,0;0,-20)
    print(type(polygon1.obj))  # <class 'klayout.pyacore.DPolygon'>
    print(type(polygon2.obj))  # <class 'klayout.pyacore.DPolygon'>
    region = pya.Region([polygon1.obj, polygon2.obj])

The error information are seen like below.

Comments

  • class MyPolygon:
        def __init__(self, pts):
            self.obj = pya.DPolygon(pts)
                           ^^^^^^^^
    

    vs

    class MyPolygon:
        def __init__(self, pts):
            self.obj = pya.Polygon(pts)
                           ^^^^^^^^
    

  • edited January 6

    Oh my god, is this mean that for region stuff, we can not use float coordinates? Is this mean that if we want to deal with nm, we need to use dbu as coordinate first, and to make integer coordinates, and then we convert to um? Seems this is not convenient. It will take a lot of effort!

  • The constructors from Polygon to DPolygon and from DPolygon to Polygon are provided, but not between an array of them, which is the root cause.
    Therefore, the two cases below work if you want to keep pya.DPolygon in class MyPolygon.
    I think f2650C.py would be practical.

    f2650B.py

    import pya
    
    class MyPolygon:
        def __init__(self, pts):
            self.obj = pya.DPolygon(pts)
    #                  ^^^^^^^^^^^^
    
    if __name__ == '__main__':
        polygon1 = MyPolygon([pya.Point(0, 0),
                              pya.Point(0, 20),
                              pya.Point(20, 20),
                              pya.Point(20, 0),
                              pya.Point(0, 0)])
        polygon2 = MyPolygon([pya.Point(0, 0),
                              pya.Point(0, -20),
                              pya.Point(-20, -20),
                              pya.Point(-20, 0),
                              pya.Point(0, 0), ])
        print(polygon1.obj)  # (0,0;0,20;20,20;20,0;0,0)
        print(polygon2.obj)  # (-20,-20;-20,0;0,0;0,0;0,-20)
        print(type(polygon1.obj))  # <class 'klayout.pyacore.DPolygon'>
        print(type(polygon2.obj))  # <class 'klayout.pyacore.DPolygon'>
        #region = pya.Region([polygon1.obj, polygon2.obj])
        region = pya.Region(polygon1.obj)
        #                   ^^^^^^^^^^^^ Single DPolygon; not an array of DPolygon
    
    """
    (0,0;0,20;20,20;20,0;0,0)
    (-20,-20;-20,0;0,0;0,0;0,-20)
    <class 'klayout.pyacore.DPolygon'>
    <class 'klayout.pyacore.DPolygon'>
    """
    

    f2650C.py

    import pya
    
    class MyPolygon:
        def __init__(self, pts):
            self.obj = pya.DPolygon(pts)
    #                  ^^^^^^^^^^^^
    
    if __name__ == '__main__':
        polygon1 = MyPolygon([pya.Point(0, 0),
                              pya.Point(0, 20),
                              pya.Point(20, 20),
                              pya.Point(20, 0),
                              pya.Point(0, 0)])
        polygon2 = MyPolygon([pya.Point(0, 0),
                              pya.Point(0, -20),
                              pya.Point(-20, -20),
                              pya.Point(-20, 0),
                              pya.Point(0, 0), ])
        print(polygon1.obj)  # (0,0;0,20;20,20;20,0;0,0)
        print(polygon2.obj)  # (-20,-20;-20,0;0,0;0,0;0,-20)
        print(type(polygon1.obj))  # <class 'klayout.pyacore.DPolygon'>
        print(type(polygon2.obj))  # <class 'klayout.pyacore.DPolygon'>
        dbu    = 0.001
        _dbu   = 1.0/dbu
        region = pya.Region([pya.Polygon(polygon1.obj*_dbu), pya.Polygon(polygon2.obj*_dbu)])
        #                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    """
    (0,0;0,20;20,20;20,0;0,0)
    (-20,-20;-20,0;0,0;0,0;0,-20)
    <class 'klayout.pyacore.DPolygon'>
    <class 'klayout.pyacore.DPolygon'>
    """
    
  • Yes, got it. Thanks

Sign In or Register to comment.