How to write the code ?
Why, using the SKILL IDE of course! :)
But seriously though... Here's one strategy that, while not particularly efficient, has the virtue of being relatively straightforward to implement:
1. Use dbProduceOverlapInst() on the layout cellview bBox to find all instances and their hierarchical paths.
2. Filter out the instances you are interested in by examining their cellnames.
3. Use dbCopyFig() in conjunction with dbGetHierPathTransform() to copy the instances to the top level.
If dbProduceOverlapInst() consumes too much memory, it's not really difficult to just walk the hierarchy and make a list of only the instances you want to bring up, along with their hierPaths--or don't even bother making a list at all: just copy them up as you find them!