plotter.py
1 # SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-or-later OR CERN-OHL-S-2.0+ OR Apache-2.0 2 from matplotlib import pyplot as plt 3 import shapely.geometry as sh_geo 4 import descartes 5 6 from ...technology import geometry as _geo 7 from ...design import layout as _lay 8 from ...design.layout import layout_ as _laylay 9 10 from ... import _util 11 12 13 __all__ = ["Plotter"] 14 15 16 class Plotter: 17 def __init__(self, plot_specs={}): 18 self.plot_specs = dict(plot_specs) 19 20 def plot(self, obj): 21 if _util.is_iterable(obj): 22 for item in obj: 23 self.plot(item) 24 elif isinstance(obj, _lay.LayoutT): 25 self.plot(obj._sublayouts) 26 elif isinstance(obj, _laylay._MaskShapesSubLayout): 27 for ms in obj.shapes: 28 self.plot(ms) 29 elif isinstance(obj, _geo.MaskShape): 30 ax = plt.gca() 31 draw_args = self.plot_specs.get(obj.mask.name, {}) 32 for ps in obj.shape.pointsshapes: 33 sh_poly = sh_geo.Polygon((p.x, p.y) for p in ps.points) 34 patch = descartes.PolygonPatch(sh_poly, **draw_args) 35 ax.add_patch(patch) 36 else: 37 raise NotImplementedError(f"plotting obj of type '{obj.__class__.__name__}'")