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?
Solved! Go to Solution.
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?
Solved! Go to Solution.
Solved by _gile. Go to Solution.
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();
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();
Can't find what you're looking for? Ask the community or share your knowledge.