//register a new drawing group for the cloned sheet
int dgId = drawingManager.findNewDrawingGroupId();
dgg.addCluster( dgId, 0 );
dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
EscherDgRecord dg = null;
for(Iterator<EscherRecord> it = escherContainer.getChildIterator(); it.hasNext();) {
EscherRecord er = it.next();
if(er instanceof EscherDgRecord) {
dg = (EscherDgRecord)er;
//update id of the drawing in the cloned sheet
dg.setOptions( (short) ( dgId << 4 ) );
} else if (er instanceof EscherContainerRecord){
// iterate over shapes and re-generate shapeId
EscherContainerRecord cp = (EscherContainerRecord)er;
for(Iterator<EscherRecord> spIt = cp.getChildRecords().iterator(); spIt.hasNext();) {
EscherContainerRecord shapeContainer = (EscherContainerRecord)spIt.next();
for(EscherRecord shapeChildRecord : shapeContainer.getChildRecords()) {
int recordId = shapeChildRecord.getRecordId();
if (recordId == EscherSpRecord.RECORD_ID){
EscherSpRecord sp = (EscherSpRecord)shapeChildRecord;
int shapeId = drawingManager.allocateShapeId((short)dgId, dg);
//allocateShapeId increments the number of shapes. roll back to the previous value
dg.setNumShapes(dg.getNumShapes()-1);
sp.setShapeId(shapeId);
} else if (recordId == EscherOptRecord.RECORD_ID){
EscherOptRecord opt = (EscherOptRecord)shapeChildRecord;
EscherSimpleProperty prop = (EscherSimpleProperty)opt.lookup(
EscherProperties.BLIP__BLIPTODISPLAY );