original_docstring: add ports from polygons in certain layers
  
      markers at port center, so half of the marker goes inside and half ouside the port
  
      Args:
          component: to read polygons from and to write ports to
          port_layer2type: dict of layer to port_type
          port_type2layer: dict of port_type to layer
          inside: True-> markers  inside. False-> markers at center
  
      .. code::
             _______________
            |               |
            |               |
            |               |
           |||              |
           |||              |
            |               |
            |      __       |
            |_______________|
                   __
  
      dx < dy: port is east or west
          x > xc: east
          x < xc: west
  
      dx > dy: port is north or south
          y > yc: north
          y < yc: south
  
      dx = dy
          x > xc: east
          x < xc: west
  
      
docstring: add ports from polygons in certain layers
  markers at port center, so half of the marker goes inside and half ouside the port
docstring_tokens: add ports from polygons in certain layers markers at port center so half of the marker goes inside and half ouside the port
code: def add_ports_from_markers_center(
      component: Component,
      port_layer2type=port_layer2type,
      port_type2layer=port_type2layer,
      inside: bool = False,
      tol=0.1,
  ):
      """add ports from polygons in certain layers
  
      markers at port center, so half of the marker goes inside and half ouside the port
  
      Args:
          component: to read polygons from and to write ports to
          port_layer2type: dict of layer to port_type
          port_type2layer: dict of port_type to layer
          inside: True-> markers  inside. False-> markers at center
  
      .. code::
             _______________
            |               |
            |               |
            |               |
           |||              |
           |||              |
            |               |
            |      __       |
            |_______________|
                   __
  
      dx < dy: port is east or west
          x > xc: east
          x < xc: west
  
      dx > dy: port is north or south
          y > yc: north
          y < yc: south
  
      dx = dy
          x > xc: east
          x < xc: west
  
      """
      i = 0
      xc = component.x
      yc = component.y
      xmax = component.xmax
      xmin = component.xmin
      ymax = component.ymax
      ymin = component.ymin
  
      for port_layer, port_type in port_layer2type.items():
          port_markers = read_port_markers(component, [port_layer])
  
          for p in port_markers.polygons:
              dy = p.ymax - p.ymin
              dx = p.xmax - p.xmin
              x = p.x
              y = p.y
              layer = port_type2layer[port_type]
              pxmax = p.xmax
              pxmin = p.xmin
              pymax = p.ymax
              pymin = p.ymin
  
              if dx < dy and x > xc:  # east
                  orientation = 0
                  width = dy
                  if inside:
                      x = p.xmax
              elif dx < dy and x < xc:  # west
                  orientation = 180
                  width = dy
                  if inside:
                      x = p.xmin
              elif dx > dy and y > yc:  # north
                  orientation = 90
                  width = dx
                  if inside:
                      y = p.ymax
              elif dx > dy and y < yc:  # south
                  orientation = 270
                  width = dx
                  if inside:
                      y = p.ymin
              # port markers have same width and height
              # check which edge (E, W, N, S) they are closer to
              elif pxmax > xmax - tol:  # east
                  orientation = 0
                  width = dy
                  x = p.xmax
              elif pxmin < xmin + tol:  # west
                  orientation = 180
                  width = dy
                  x = p.xmin
              elif pymax > ymax - tol:  # north
                  orientation = 90
                  width = dx
                  y = p.ymax
              elif pymin < ymin + tol:  # south
                  orientation = 270
                  width = dx
                  y = p.ymin
              else:
                  raise ValueError(f"port marker x={x} y={y}, dx={dx}, dy={dy}")
  
              component.add_port(
                  i,
                  midpoint=(x, y),
                  width=width,
                  orientation=orientation,
                  port_type=port_type,
                  layer=layer,
              )
              i += 1
code_tokens: def add_ports_from_markers_center ( component : Component , port_layer2type = port_layer2type , port_type2layer = port_type2layer , inside : bool = False , tol = 0.1 , ) : i = 0 xc = component . x yc = component . y xmax = component . xmax xmin = component . xmin ymax = component . ymax ymin = component . ymin for port_layer , port_type in port_layer2type . items ( ) : port_markers = read_port_markers ( component , [ port_layer ] ) for p in port_markers . polygons : dy = p . ymax - p . ymin dx = p . xmax - p . xmin x = p . x y = p . y layer = port_type2layer [ port_type ] pxmax = p . xmax pxmin = p . xmin pymax = p . ymax pymin = p . ymin if dx < dy and x > xc : orientation = 0 width = dy if inside : x = p . xmax elif dx < dy and x < xc : orientation = 180 width = dy if inside : x = p . xmin elif dx > dy and y > yc : orientation = 90 width = dx if inside : y = p . ymax elif dx > dy and y < yc : orientation = 270 width = dx if inside : y = p . ymin elif pxmax > xmax - tol : orientation = 0 width = dy x = p . xmax elif pxmin < xmin + tol : orientation = 180 width = dy x = p . xmin elif pymax > ymax - tol : orientation = 90 width = dx y = p . ymax elif pymin < ymin + tol : orientation = 270 width = dx y = p . ymin else : raise ValueError ( f"port marker x={x} y={y}, dx={dx}, dy={dy}" ) component . add_port ( i , midpoint = ( x , y ) , width = width , orientation = orientation , port_type = port_type , layer = layer , ) i += 1
short_docstring: add ports from polygons in certain layers
  markers at port center, so half of the marker goes inside and half ouside the port
short_docstring_tokens: add ports from polygons in certain layers markers at port center so half of the marker goes inside and half ouside the port
comment: """add ports from polygons in certain layers
  
      markers at port center, so half of the marker goes inside and half ouside the port
  
      Args:
          component: to read polygons from and to write ports to
          port_layer2type: dict of layer to port_type
          port_type2layer: dict of port_type to layer
          inside: True-> markers  inside. False-> markers at center
  
      .. code::
             _______________
            |               |
            |               |
            |               |
           |||              |
           |||              |
            |               |
            |      __       |
            |_______________|
                   __
  
      dx < dy: port is east or west
          x > xc: east
          x < xc: west
  
      dx > dy: port is north or south
          y > yc: north
          y < yc: south
  
      dx = dy
          x > xc: east
          x < xc: west
  
      """$SEP$# east$SEP$# west$SEP$# north$SEP$# south$SEP$# port markers have same width and height$SEP$# check which edge (E, W, N, S) they are closer to$SEP$# east$SEP$# west$SEP$# north$SEP$# south