.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problems using IdMapping after running WblockCloneObjects

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
nshupeFMPE3
185 Views, 2 Replies

Problems using IdMapping after running WblockCloneObjects

I've been testing moving selected items from one drawing to another.
I prompt the user to select what they would like to move.
I then move those objects to the "0" layer, as I dont want to bring layers from the source file to the destination.
I run this to move the objects over

static void CopyObjects(string destFileName, ObjectIdCollection idsToCopy)
{
    using (Database destDb = new Database(false, true))
    {
        destDb.ReadDwgFile(TemplatePath, System.IO.FileShare.Read, true, "");

        using (Transaction sourceTr = Active.Database.TransactionManager.StartOpenCloseTransaction())
        using (Transaction destTr = destDb.TransactionManager.StartOpenCloseTransaction())
        using(BlockTable destBt = destTr.GetObject(destDb.BlockTableId, OpenMode.ForRead, false, true) as BlockTable)
        {
            try
            {
                var mapping = new IdMapping();
                Active.Database.WblockCloneObjects(idsToCopy, destBt[BlockTableRecord.ModelSpace], mapping, DuplicateRecordCloning.Ignore,
                    false);

                _mapping = mapping;

                destTr.Commit();
                sourceTr.Commit();
            }
            catch
            {
                destTr.Abort();
                sourceTr.Abort();
            }
        }

        destDb.SaveAs(destFileName, DwgVersion.Current);
    }
    
}

And then after I take the IdMapping and use Contains and Lookup to get the object id for the other side. But the ObjectId for the other side is not the same type? 

I've tested by selecting just 1 polyline. And when I look through the mapping, I find one polyline on the source side of the pairs. But the destination side is of type Layer?

CopyObjects(DESTINATION_FILE, ids);

using (Database destDb = new Database(false, true))
{
    destDb.ReadDwgFile(DESTINATION_FILE, FileShare.Read, true, "");

    using (Transaction destTr = destDb.TransactionManager.StartOpenCloseTransaction())
    {
        try
        {
            foreach (IdPair pair in _mapping)
            {
                Active.WriteMessage($"\nSource: {pair.Key.ObjectClass.DxfName} | Destination: {pair.Value.ObjectClass.DxfName}");
            }

            destTr.Commit();
        }
        catch
        {
            destTr.Abort();
        }
    }
}



How do I get the ObjectId for the corresponding object in the destination file?

Labels (3)
2 REPLIES 2
Message 2 of 3
_gile
in reply to: nshupeFMPE3

Hi,

The ObjectId are unique per session and re-attributed at each new session. You get your IDMapping from a Database instance and try to use it in another one. This cannot work.

 

You have to use this IDMapping within the scope of the using (Database destBb ...) statement where you get it.

 

Or, if you want to be able to get the added objects after the database is closed, you have to use the objects Handles which are persistant per drawing.

 

Handle[] _handles;
[...]
_handles = mapping
    .Cast<IdPair>()
    .Where(p => p.IsCloned && p.IsPrimary)
    .Select(p => p.Value.Handle)
    .ToArray();

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3
nshupeFMPE3
in reply to: _gile

Thank you so much, I cant believe I didn't think of that. It's obvious now!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta