Through the Interface——用.NET从外部DWG文件导入块

本文介绍了一种使用C#代码从外部DWG文件导入块到AutoCAD当前图形的方法。通过创建一个侧面数据库,该方法能够读取指定的DWG文件,并将其中的所有非布局、具名块复制到目标AutoCAD图形中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:Import blocks from an external DWG file using .NET

我们将用一个“side database”来从另外一个在内存中加载的,但并不加载到AutoCAD编辑器中的图形来把块导入到编辑器中的当前图形中

下面是C#代码。内部的注释描述了进行的操作。顺便说一下,代码可以很容易地转换成一个可以在AutoCAD外部使用的RealDWG应用(我们只需要简单地把destDb从MdiActiveDocument数据库改为HostApplicationServices的WorkingDatabase并使用另一个的用户界面来获取/输出字符串给用户)。

using System;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System.Collections.Generic;

namespace BlockImport
{
  public class BlockImportClass
  {
    [CommandMethod("IB")]
    public void ImportBlocks()
    {
      DocumentCollection dm =
          Application.DocumentManager;
      Editor ed = dm.MdiActiveDocument.Editor;
      Database destDb = dm.MdiActiveDocument.Database;
      Database sourceDb = new Database(false, true);
      PromptResult sourceFileName;
      try
      {
        // Get name of DWG from which to copy blocks
        sourceFileName =
          ed.GetString("\nEnter the name of the source drawing: ");
        // Read the DWG into a side database
        sourceDb.ReadDwgFile(sourceFileName.StringResult,
                            System.IO.FileShare.Read,
                            true,
                            "");

        // Create a variable to store the list of block identifiers
        ObjectIdCollection blockIds = new ObjectIdCollection();

        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,
                                    destDb.BlockTableId,
                                    mapping,
                                    DuplicateRecordCloning.Replace,
                                    false);
        ed.WriteMessage("\nCopied "
                        + blockIds.Count.ToString()
                        + " block definitions from "
                        + sourceFileName.StringResult
                        + " to the current drawing.");
      }
      catch(Autodesk.AutoCAD.Runtime.Exception ex)
      {
          ed.WriteMessage("\nError during copy: " + ex.Message);
      }
      sourceDb.Dispose();
    }
  }
}

这一切就是这么简单。上的各种对象/属性/方法的更多信息,可以在ObjectARX参考中查找。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值