Through the Interface: Import blocks from an external DWG file using .NET

May 2015

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


« Handling Automation errors in Visual LISP | Main | Breaking it down - a closer look at the C# code for importing blocks »

August 18, 2006

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



    public void ImportBlocks()


      DocumentCollection dm =


      Editor ed = dm.MdiActiveDocument.Editor;

      Database destDb = dm.MdiActiveDocument.Database;

      Database sourceDb = new Database(false, true);

      PromptResult sourceFileName;



        // 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





        // Create a variable to store the list of block identifiers

        ObjectIdCollection blockIds = new ObjectIdCollection();

        Autodesk.AutoCAD.DatabaseServices.TransactionManager tm =


        using (Transaction myT = tm.StartTransaction())


          // Open the block table

          BlockTable bt =




          // Check each block in the block table

          foreach (ObjectId btrId in bt)


            BlockTableRecord btr =




            // Only add named & non-layout blocks to the copy list

            if (!btr.IsAnonymous && !btr.IsLayout)





        // Copy blocks from source to destination database

        IdMapping mapping = new IdMapping();






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






And that's all there is to it. More information on the various objects/properties/methods used can be found in the ObjectARX Reference.


TrackBack URL for this entry:

Listed below are links to weblogs that reference Import blocks from an external DWG file using .NET:

blog comments powered by Disqus


10 Random Posts