From 954d2c2eb0dad1628c7e8dee321aa3c995322b51 Mon Sep 17 00:00:00 2001 From: fbeutel Date: Mon, 28 Dec 2020 19:57:09 +0100 Subject: [PATCH 1/3] Suggestion for new format for the desc data. --- gdshelpers/geometry/chip.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/gdshelpers/geometry/chip.py b/gdshelpers/geometry/chip.py index 6e8fdcd..639d3eb 100644 --- a/gdshelpers/geometry/chip.py +++ b/gdshelpers/geometry/chip.py @@ -208,9 +208,29 @@ def get_dlw_data(self): return dlw_data def get_desc(self): - desc = self.desc.copy() - desc['cells'] = {cell['cell'].name: dict(offset=tuple(cell['origin']), angle=cell['angle'] or 0, - **cell['cell'].get_desc()) for cell in self.cells} + """ + Creates and returns a dictionary containing all the 'desc' data of this cell and all children. + The returned dictionary contains two keys: + + * `root`: The name of the root cell (the cell from which `get_desc` was called) + * `cells`: Dictionary mapping cell names to the desc data of each cell as well as cell references to other cells + (with origin and angle). + + :return: Dictionary containing the `desc` data for this cell and all cells referenced by this cell + """ + def walk_cells(cell, out_dict): + if cell.name not in out_dict: + out_dict[cell.name] = { + 'cells': {child['cell'].name: dict(offset=tuple(child['origin']), + angle=child['angle'] or 0) for child in cell.cells}, + **cell.desc + } + + for child in cell.cells: + walk_cells(child['cell'], out_dict) + + desc = dict(root=self.name, cells=dict()) + walk_cells(self, desc['cells']) return desc def get_fractured_layer_dict(self, max_points=4000, max_line_points=4000): From 092adf9a0c6ca3af1290ca8addceaf1ddf8adc03 Mon Sep 17 00:00:00 2001 From: fbeutel Date: Mon, 28 Dec 2020 20:08:11 +0100 Subject: [PATCH 2/3] Add test case --- gdshelpers/tests/test_chip.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gdshelpers/tests/test_chip.py b/gdshelpers/tests/test_chip.py index 2f67050..3002e41 100644 --- a/gdshelpers/tests/test_chip.py +++ b/gdshelpers/tests/test_chip.py @@ -180,3 +180,16 @@ def test_dlw(self): top.add_cell(child2, [0, 0]) with self.assertRaises(ValueError): top.add_cell(child2, [100, 0]) + + def test_desc(self): + cell = Cell("desctest") + c1 = Cell("child1") + c1.add_to_desc("foo", 123) + cell.add_cell(c1, origin=(0, 0)) + cell.add_cell(c1, origin=(100, 0), angle=2) + + d = cell.get_desc() + self.assertTrue("child1" in d["cells"]) + self.assertTrue("desctest" in d["cells"]) + self.assertEqual("desctest", d["root"]) + self.assertEqual(2, len(d["cells"]["desctest"]["cells"])) From a25d9f1af5930620bbc7c0d04d162576f771d446 Mon Sep 17 00:00:00 2001 From: fbeutel Date: Mon, 28 Dec 2020 20:19:03 +0100 Subject: [PATCH 3/3] Allow multiple children cells with equal names --- gdshelpers/geometry/chip.py | 12 +++++++----- gdshelpers/tests/test_chip.py | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/gdshelpers/geometry/chip.py b/gdshelpers/geometry/chip.py index 639d3eb..2bbf436 100644 --- a/gdshelpers/geometry/chip.py +++ b/gdshelpers/geometry/chip.py @@ -220,11 +220,13 @@ def get_desc(self): """ def walk_cells(cell, out_dict): if cell.name not in out_dict: - out_dict[cell.name] = { - 'cells': {child['cell'].name: dict(offset=tuple(child['origin']), - angle=child['angle'] or 0) for child in cell.cells}, - **cell.desc - } + cellrefs = [] + for child in cell.cells: + child_dict = child.copy() + child_dict['cell'] = child_dict['cell'].name + cellrefs.append(child_dict) + + out_dict[cell.name] = {'cells': cellrefs, **cell.desc} for child in cell.cells: walk_cells(child['cell'], out_dict) diff --git a/gdshelpers/tests/test_chip.py b/gdshelpers/tests/test_chip.py index 3002e41..3c101de 100644 --- a/gdshelpers/tests/test_chip.py +++ b/gdshelpers/tests/test_chip.py @@ -193,3 +193,4 @@ def test_desc(self): self.assertTrue("desctest" in d["cells"]) self.assertEqual("desctest", d["root"]) self.assertEqual(2, len(d["cells"]["desctest"]["cells"])) + self.assertEqual("child1", d["cells"]["desctest"]["cells"][0]["cell"])