diff --git a/src/db/db/dbDeepShapeStore.cc b/src/db/db/dbDeepShapeStore.cc index 9a2cd9599..687008b9c 100644 --- a/src/db/db/dbDeepShapeStore.cc +++ b/src/db/db/dbDeepShapeStore.cc @@ -1244,7 +1244,7 @@ DeepShapeStore::cell_mapping_to_original (unsigned int layout_index, db::Layout std::map::const_iterator icm = cm_skipped_variants.find (var_org); if (icm != cm_skipped_variants.end ()) { - // create the variant clone in the original layout too and delete this cell + // create the variant clone in the original layout too VariantsCollectorBase::copy_shapes (*into_layout, np->second, icm->second.original_cell); new_variants.push_back (std::make_pair (np->second, icm->second.original_cell)); @@ -1269,22 +1269,32 @@ DeepShapeStore::cell_mapping_to_original (unsigned int layout_index, db::Layout // copy cell instances for the new variants - // collect the cells that are handled during cell mapping - - // we do not need to take care of them when creating variants, - // but there may be others inside "into_layout" which are - // not present in the DSS and for which we need to copy the - // instances. - std::vector mapped = cm->second.target_cells (); - std::sort (mapped.begin (), mapped.end ()); + std::map variant_to_org; + for (auto vv = new_variants.begin (); vv != new_variants.end (); ++vv) { + variant_to_org.insert (std::make_pair (vv->first, vv->second)); + } // Copy the variant instances - but only those for cells which are not handled by the cell mapping object. for (auto vv = new_variants.begin (); vv != new_variants.end (); ++vv) { - const db::Cell &from = into_layout->cell (vv->second); - db::Cell &to = into_layout->cell (vv->first); + const db::Cell &from = into_layout->cell (vv->second); // original + db::Cell &to = into_layout->cell (vv->first); // variant + + // Collect and copy the cells which are not mapped already. + // Skip variant original cells if their variants are included. + std::set dont_copy; + + for (auto c = to.begin_child_cells (); ! c.at_end (); ++c) { + auto v2o = variant_to_org.find (*c); + if (v2o != variant_to_org.end ()) { + dont_copy.insert (v2o->second); + } else { + dont_copy.insert (*c); + } + } + for (db::Cell::const_iterator i = from.begin (); ! i.at_end (); ++i) { - auto m = std::lower_bound (mapped.begin (), mapped.end (), i->cell_index ()); - if (m == mapped.end () || *m != i->cell_index ()) { + if (dont_copy.find (i->cell_index ()) == dont_copy.end ()) { to.insert (*i); } } diff --git a/src/db/unit_tests/dbDeepShapeStoreTests.cc b/src/db/unit_tests/dbDeepShapeStoreTests.cc index 4a01e194c..d14be57fe 100644 --- a/src/db/unit_tests/dbDeepShapeStoreTests.cc +++ b/src/db/unit_tests/dbDeepShapeStoreTests.cc @@ -341,3 +341,44 @@ TEST(7_RestoreWithCellSelection2) db::compare_layouts (_this, ly, tl::testdata () + "/algo/dss_bug2_au.gds"); } +TEST(8_RestoreWithCellSelection3) +{ + db::Layout ly; + + { + std::string fn (tl::testdata ()); + fn += "/algo/dss_bug3.gds"; + tl::InputStream stream (fn); + db::Reader reader (stream); + reader.read (ly); + } + + unsigned int l2 = ly.get_layer (db::LayerProperties (2, 0)); + + db::Cell &top_cell = ly.cell (*ly.begin_top_down ()); + + db::RecursiveShapeIterator in_it (ly, top_cell, l2); + db::RecursiveShapeIterator other_it (ly, top_cell, l2); + + std::set us; + us.insert (ly.cell_by_name ("X").second); + us.insert (ly.cell_by_name ("C3").second); + in_it.unselect_cells (us); + + std::set us2; + us2.insert (top_cell.cell_index ()); + other_it.unselect_cells (us2); + other_it.select_cells (us); + + db::DeepShapeStore dss; + + db::Region in_region (in_it, dss); + db::Region other_region (other_it, dss); + in_region += other_region; + + ly.clear_layer (l2); + in_region.insert_into (&ly, top_cell.cell_index (), l2); + + db::compare_layouts (_this, ly, tl::testdata () + "/algo/dss_bug3_au.gds"); +} + diff --git a/testdata/algo/dss_bug3.gds b/testdata/algo/dss_bug3.gds new file mode 100644 index 000000000..39c289ac5 Binary files /dev/null and b/testdata/algo/dss_bug3.gds differ diff --git a/testdata/algo/dss_bug3_au.gds b/testdata/algo/dss_bug3_au.gds new file mode 100644 index 000000000..45c745946 Binary files /dev/null and b/testdata/algo/dss_bug3_au.gds differ