I have 3 different BlockTableRecord
's that I want to update.
I am loading external .dxf files that contain the entities for each block definition. Now when I delete the old entities and load in the new ones it works perfectly fine but as soon as I start updating the second (third, etc.) one it uses the entities of the first BlockTableRecord
again. It even happens after saving and reloading the whole drawing.
GetPositionBlock()
gets the BlockTableRecord
I want to update based on its XData. The error is not in this method as it gets the right block to update.
public void Update(string fileName)
{
using (_doc.LockDocument())
{
using Transaction transaction = _dataBase.TransactionManager.StartTransaction())
{
BlockTableRecord blockTableRecord = GetPositionBlock();
// ...delete old entities
BlockTableRecord externalblock = GetExternalBlock(fileName);
ObjectIdCollection objs = new ObjectIdCollection();
foreach (ObjectId objId in externalBlock)
{
objs.Add(objId);
}
blockTableRecord.AssumeOwnershipPf(objs);
transaction.Commit();
}
}
}
private BlockTableRecord GetExternalBlock(string fileName)
{
DBObjectCollection dbObjColleciton = new DBObjectCollection();
using (Transaction transaction = _doc.TransactionManager.StartTransaction())
{
using (Database sourceDb = new Database(false, true))
{
sourceDb.DxfIn(fileName, null);
ObjectId blockId = _dataBase.Insert(Guid.NewGuid().ToString(), sourceDb, false)
BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockId, OpenMode.ForRead);
transaction.Commit();
return blockTableRecord;
}
}
}
I double-checked every value. It never uses the same guid, name or anything like that. It seems like the problem lies somewhere in blockId
or in _dataBase.Insert(...)
. It seems as some value is overwritten or set on the first use and after that nothing resets it.
Any ideas what could cause this problem? Any hints or help is much appreciated.
I think your problem is here :
the _Database object is not the database you are in when using method GetExternalBlock. Personally I would use the WblockCloneObjects Method to get a block from an external drawing. my code: