Coverage for klayout_pex/klayout/shapes_pb2_converter.py: 60%

35 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-08 18:54 +0000

1# 

2# -------------------------------------------------------------------------------- 

3# SPDX-FileCopyrightText: 2024-2025 Martin Jan Köhler and Harald Pretl 

4# Johannes Kepler University, Institute for Integrated Circuits. 

5# 

6# This file is part of KPEX 

7# (see https://github.com/martinjankoehler/klayout-pex). 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21# SPDX-License-Identifier: GPL-3.0-or-later 

22# -------------------------------------------------------------------------------- 

23# 

24 

25import klayout.db as kdb 

26import klayout_pex_protobuf.kpex.geometry.shapes_pb2 as shapes_pb2 

27 

28 

29class ShapesConverter: 

30 def __init__(self, dbu: float): 

31 self.dbu = dbu 

32 

33 def klayout_point(self, point: shapes_pb2.Point) -> kdb.Point: 

34 # FIXME: there is no PointWithProperties yet 

35 return kdb.Point(point.x, point.y) 

36 

37 def klayout_polygon(self, polygon: shapes_pb2.Polygon) -> kdb.Polygon: 

38 points_kly = [self.klayout_point(pt) for pt in polygon.hull_points] 

39 polygon_kly = kdb.Polygon(points_kly) 

40 if len(polygon.net) >= 1: 

41 polygon_kly = kdb.PolygonWithProperties(polygon_kly, {'net': polygon.net}) 

42 return polygon_kly 

43 

44 def klayout_region(self, region: shapes_pb2.Region) -> kdb.Region: 

45 region_kly = kdb.Region() 

46 for polygon in region.polygons: 

47 region_kly.insert(self.klayout_polygon(polygon)) 

48 return region_kly 

49 

50 def klayout_box(self, box: shapes_pb2.Box) -> kdb.Box: 

51 box_kly = kdb.Box(box.lower_left.x, 

52 box.lower_left.y, 

53 box.upper_right.x, 

54 box.upper_right.y) 

55 if box.net: 

56 box_kly = kdb.BoxWithProperties(box_kly, {'net': box.net}) 

57 return box_kly 

58 

59 def klayout_point_to_pb(self, 

60 point_kly: kdb.Point, 

61 point_pb: shapes_pb2.Point): 

62 point_pb.x = point_kly.x 

63 point_pb.y = point_kly.y 

64 

65 def klayout_polygon_to_pb(self, 

66 polygon_kly: kdb.Polygon, 

67 polygon_pb: shapes_pb2.Polygon): 

68 net_name = polygon_kly.property('net') 

69 if net_name: 

70 polygon_pb.net = net_name 

71 for p_kly in polygon_kly.each_point_hull(): 

72 self.klayout_point_to_pb(p_kly, polygon_pb.hull_points.add()) 

73 

74 def klayout_region_to_pb(self, 

75 region_kly: kdb.Region, 

76 region_pb: shapes_pb2.Region): 

77 for pgn_kly in region_kly: 

78 self.klayout_polygon_to_pb(pgn_kly, region_pb.polygons.add())