【问题标题】:Programmatically Import Block Into AutoCAD (C#)以编程方式将块导入 AutoCAD (C#)
【发布时间】:2015-08-12 04:11:04
【问题描述】:

我正在为 AutoCAD 编写一个插件,并希望导入它将在开始时使用的所有块,以确保它们在需要时可用。为此,我使用这种方法

public static void ImportBlocks(string[] filesToTryToImport, string filter = "")
{
    foreach (string blockToImport in filesToTryToImport)
    {
        if (blockToImport.Contains(filter))
        {
            Database sourceDb = new Database(false, true); //Temporary database to hold data for block we want to import
            try
            {
                sourceDb.ReadDwgFile(blockToImport, System.IO.FileShare.Read, true, ""); //Read the DWG into a side database
                ObjectIdCollection blockIds = new ObjectIdCollection(); // Create a variable to store the list of block identifiers

                Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = sourceDb.TransactionManager;

                using (Transaction myT = tm.StartTransaction())
                {
                    // Open the block table
                    BlockTable bt = (BlockTable)tm.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);

                    // Check each block in the block table
                    foreach (ObjectId btrId in bt)
                    {
                        BlockTableRecord btr = (BlockTableRecord)tm.GetObject(btrId, OpenMode.ForRead, false);
                        // Only add named & non-layout blocks to the copy list
                        if (!btr.IsAnonymous && !btr.IsLayout)
                        {
                            blockIds.Add(btrId);
                        }
                        btr.Dispose();
                    }
                }
                // Copy blocks from source to destination database
                IdMapping mapping = new IdMapping();
                sourceDb.WblockCloneObjects(blockIds, _database.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
                _editor.WriteMessage("\nCopied " + blockIds.Count.ToString() + " block definitions from " + blockToImport + " to the current drawing.");
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                _editor.WriteMessage("\nError during copy: " + ex.Message);
            }
            finally
            {
                sourceDb.Dispose();
            }
        }
    }
}

该方法似乎有效,因为它成功执行。但是,当我通过 AutoCAD 的界面在图形中插入一个块时,它不会显示为一个选项,当我尝试以编程方式插入它时,它会引发 FileNotFound 异常,这意味着它不起作用。这种方法有什么问题?提前致谢!

编辑:这是一个不太复杂的测试方法

public static void ImportSingleBlock(string fileToTryToImport)
{
    using (Transaction tr = _database.TransactionManager.StartTransaction())
    {
        Database sourceDb = new Database(false, true); //Temporary database to hold data for block we want to import
        try
        {
            sourceDb.ReadDwgFile(fileToTryToImport, System.IO.FileShare.Read, true, ""); //Read the DWG into a side database
            _database.Insert(fileToTryToImport, sourceDb, false);
            _editor.WriteMessage("\nSUCCESS: " + fileToTryToImport);
        }
        catch (Autodesk.AutoCAD.Runtime.Exception ex)
        {
            _editor.WriteMessage("\nERROR: " + fileToTryToImport);
        }
        finally
        {
            sourceDb.Dispose();
        }
        tr.Commit();
    }
}

[CommandMethod("TESTSINGLEBLOCKIMPORTING")]
public void TestSingleBlockImporting()
{
    OpenFileDialog ofd = new OpenFileDialog();
    DialogResult result = ofd.ShowDialog();
    if (result == DialogResult.Cancel) //Ending method on cancel
    {
        return;
    }
    string fileToTryToImport = ofd.FileName;
    using (Transaction tr = _database.TransactionManager.StartTransaction())
    {
        EntityMethods.ImportSingleBlock(fileToTryToImport);
        tr.Commit();
    }
}

This file 是我要导入的块。希望这能激励某人,因为我现在非常迷茫。

【问题讨论】:

    标签: c# autocad autocad-plugin


    【解决方案1】:

    您的代码是正确的,应该可以工作。事实上,我确实尝试过并且效果很好。您可能缺少 Commit() 外部事务(您在其中调用此 ImportBlocs() 方法)。检查:

    using (Transaction trans = _database.TransactionManager.StartTransaction())
    {
      ImportBlocks(... parameters here ...);
      trans.Commit(); // remember to call this commit, if omitted, Abort() is assumed
    }
    

    【讨论】:

    • 没有为我修复它...这是我要导入的块,可能问题出在块 drive.google.com/file/d/0BwwMHXVXHuDjaVZ4NUlNbHNUN0E/…
    • 啊好吧,这幅画“是”一个块(而不是“包含”一个块)。您的原始代码正在读取所有块,但您的绘图不包含任何块。对于这样的绘图,您应该调用 Database.Insert() 传递 .dwg 路径,这将返回新块定义的 ObjectId,然后您可以使用它来创建 BlockReference。
    猜你喜欢
    • 2021-12-07
    • 2016-10-23
    • 2017-07-18
    • 2011-05-30
    • 1970-01-01
    • 2011-05-28
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多