导入多个 DXF 文件时实体错误

Entities wrong when importing multiple DXF files

我有 3 个不同的 BlockTableRecord 想要更新。 我正在加载包含每个块定义的实体的外部 .dxf 文件。现在,当我删除旧实体并加载新实体时,它工作得很好,但一旦我开始更新第二个(第三个等)实体,它就会再次使用第一个 BlockTableRecord 的实体。它甚至发生在保存并重新加载整个绘图后。

GetPositionBlock() 获取 BlockTableRecord 我想根据其 XData 更新。错误不在这个方法中,因为它得到了正确的块来更新。

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;
        }
    }
}

我仔细检查了每个值。它从不使用相同的 guid、名称或类似的东西。问题似乎出在 blockId_dataBase.Insert(...) 中的某处。似乎某些值在第一次使用时被覆盖或设置,之后没有任何重置。 有什么想法会导致这个问题吗?非常感谢任何提示或帮助。

我认为你的问题出在这里:

ObjectId blockId = _dataBase.Insert(Guid.NewGuid().ToString(), sourceDb, false)

_Database 对象不是您在使用方法 GetExternalBlock 时所在的数据库。我个人会使用 WblockCloneObjects 方法从外部绘图中获取块。 我的代码:

 public bool GetExternBlok(string filePath, string blockName)
    {
        bool functionReturnValue = true;
        Document doc = Application.DocumentManager.MdiActiveDocument;

        using (DocumentLock  myLock = doc.LockDocument())
        {
            using (Database OpenDb = new Database(false, false))
            {
                OpenDb.ReadDwgFile(filePath, System.IO.FileShare.Read, true, "");
                //readwrite
                ObjectIdCollection sourceidsCollection = new ObjectIdCollection();
                ObjectId sourceBlockId = ObjectId.Null;

                using (Transaction tr = OpenDb.TransactionManager.StartTransaction())
                {

                    BlockTable bt = (BlockTable)tr.GetObject(OpenDb.BlockTableId, OpenMode.ForRead);

                    if (bt.Has(blockName))
                    {
                        sourceidsCollection.Add(bt[blockName]);
                        sourceBlockId = bt[blockName];
                    }

                    if (sourceidsCollection.Count != 0)
                    {
                        Database destdb = doc.Database;
                        IdMapping iMap = new IdMapping();
                        OpenDb.WblockCloneObjects(sourceidsCollection, destdb.BlockTableId, iMap, DuplicateRecordCloning.Replace, false);

                        using (Transaction t = destdb.TransactionManager.StartTransaction())
                        {
                            ObjectId targetBlockId = ObjectId.Null;
                            BlockTable b = (BlockTable)t.GetObject(destdb.BlockTableId, OpenMode.ForRead);

                            if (b.Has(blockName))
                            {
                                targetBlockId = b[blockName];
                            }

                            t.Commit();

                        }
                    }
                    else
                    {
                        functionReturnValue = false;
                    }

                    tr.Commit();

                }
            }
        }
        return functionReturnValue;
    }