找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1064|回复: 0

[分享] Import blocks from an external DWG file using .NET

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-9 02:47:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
http://through-the-interface.typepad.com/through_the_interface/2006/08/import_blocks_f.html

Import blocks from an external DWG file using .NET
We're going to use a "side database" - a drawing that is loaded in memory, but not into the AutoCAD editor - to import the blocks from another drawing into the one active in the editor.
Here's some C# code. The inline comments describe what is being done along the way. Incidentally, the code could very easily be converted into a RealDWG application that works outside of AutoCAD (we would simply need to change the destDb from the MdiActiveDocument's Database to the HostApplicationServices' WorkingDatabase, and use a different user interface for getting/presenting strings from/to the user).
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();
    }
  }
}





论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-16 02:22 , Processed in 0.453393 second(s), 34 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表