Hi Tim,
... a bit more than you wanted ...
This is not original .. I don't know the author
using System;
using System.Collections;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Interop;
namespace ClassLibrary1
{
public class DatabaseExample
{
//Insert an external file as a block definition via .NET
[CommandMethod("CopyBlock")]
public void InsertAsBlock()
{
string sourceFileName = @"C:\BlockToCopy.dwg";
try
{
using(Database sourceDatabase = GetDatabaseFromFile(sourceFileName))
{
//Insert the blocks database into a new Block Table Record
HostApplicationServices.WorkingDatabase.Insert("BlockToCopy", sourceDatabase, false);
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
string errorMessage = GetMessageFromErrorStatus(e.ErrorStatus);
MessageBox.Show(errorMessage, "Copy Block Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//Copy a block definition from an external file via ActiveX
[CommandMethod("CopyBlockX")]
public void CopyBlockActiveX ()
{
string sourceFileName = @"C:\OftenUsedBlocks.dwg";
string blockName = "ActiveX";
IAxDbDocument dbxDoc = null;
AcadDatabase activeDocument = (AcadDatabase)HostApplicationServices.WorkingDatabase.AcadDatabase;
try
{
//Open the drawing file
dbxDoc = new AxDbDocumentClass();
dbxDoc.Open(sourceFileName, null);
//Place the selected block definition in an array,
//to prepare it for copying
AcadBlock blockDef = dbxDoc.Blocks.Item(blockName);
AcadBlock[] objectsToCopy = {blockDef};
object idPair = null;
//Copy the block definition into the active drawing
dbxDoc.CopyObjects(objectsToCopy, activeDocument.Blocks, ref idPair);
}
catch{}
finally
{
dbxDoc = null;
}
}
#region Private Methods
private Database GetDatabaseFromFile(string fileName)
{
//create a blank database
Database databaseFromFile = new Database(false, true);
//read the database from file into the blank database
databaseFromFile.ReadDwgFile(fileName, System.IO.FileShare.None, false, null);
return databaseFromFile;
}
private string GetMessageFromErrorStatus(int errorCode)
{
return Enum.GetName(typeof(ErrorStatus), errorCode);
}
#endregion
}
}