When cloning objects, what does IdMapping do? (AutoCAD .net API)

_config.yml

When cloning objects, what does IdMapping do?

In today’s post we are gonna learn about cloning. In particular, we are gonna make sense of this Idmapping thing. What on earth is it?

Let’s keep it to simple objects such as circles, lines, points and BlockReferences.

Here is some code which I prepared earlier:

private void CopyItems()
        {
            Document doc = Application.DocumentManager.CurrentDocument;
            Database db = doc.Database;

            //Get the collection of objectIDs which we wish to clone

            ObjectIdCollection idsOfObjectsToClone = GetIDsOfObjectsToClone();

            ObjectId modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);

            IdMapping mapping = new IdMapping();

            db.DeepCloneObjects(idsOfObjectsToClone, modelSpaceId, mapping, false);

		// Now all the new objects are stored in the 
		// IdMapping instance, ‘mapping’. We can now 
		// access all the new cloned objects. They are 
		// stored as key-value pairs within the mapping container instance.

            using (DocumentLock docLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    foreach (IdPair idMapPair in mapping)
                    {
                        Entity ent = tr.GetObject(idMapPair.Value, OpenMode.ForWrite) as Entity;
                        if (ent != null)
                        {
                            ent.TransformBy(Matrix3d.Displacement(_translationVector));
                        }
                        else
                        {
                            doc.Editor.WriteMessage("\n Warning: Some items failed to copy. Please do it manually!");
                        }
                    }
                    tr.Commit();
                } 
            }
        }

What does the ObjectARX Reference guide say about this?

True to form it concisely provides a sound academic description – useful to the expert who already knows, but perplexing, to say the least, to the one who is looking it up.

“IdMapping is used by the deep clone operation to map ObjectIds between original objects and their clones (using IdPairs).”

Ok so what is cloning?

Think of Dolly the sheep. Now you want to make a photocopy of Dolly – the sheep. That’s a complicated thing.

What if I said I want a copy of your house? Well a clever dick could just hand me a copy of my title certificate – which is great – so I would have two “references” to the same house, but I would not have two houses. I would have only one house. Supposing I could photocopy a house – so that now I would have two houses, but only one house address. Surely the second house needs an address otherwise how could anybody know where it is, or get to it. The house must be accessible.

It was precisely this problem that IdMapping tries to solve.

Answer: The IdMapping instance stores the references to the newly cloned objects

When an object is cloned, the address of that new object (or the “reference” to that object) is stored in the IdMapping variable, “mapping”. If we want to get to that new Object’s reference, we simply look up the corresponding original object. And voila: now have an address to the new object so we can access it any time we want.

I hope that helps.

Written on March 7, 2017