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

本文关键字:实体 错误 文件 DXF 导入 | 更新日期: 2023-09-27 17:54:44

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

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

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

我仔细检查了每个值。它从不使用相同的向导、名称或类似的东西。似乎问题在于blockId_dataBase.Insert(...)的某个地方。似乎一些值在第一次使用时被覆盖或设置,之后没有重置它。你知道是什么导致了这个问题吗?如有任何提示或帮助,不胜感激。

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

我想你的问题在这里:

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

使用GetExternalBlock方法时,_Database对象不是您所在的数据库。就我个人而言,我会使用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;
    }