Here you go: (I insert the other file exploded, but you can omit this step)
internal DBObjectCollection InsertDWGFile(string dwgName, Point2d insertion, double rotation, Scale3d blockScale, string layer)
{
try
{
// Find the file
string fullDWGName = HostApplicationServices.Current.FindFile(Properties.Settings.Default.BlockFolder + "\\" + dwgName + ".dwg", dataBase, FindFileHint.Default);
// Create block's file database
Database blockDB = new Database(false, false);
blockDB.ReadDwgFile(fullDWGName, System.IO.FileShare.Read, true, "");
// Insert block definition
ObjectId blockID = dataBase.Insert(dwgName, blockDB, true);
// Close block file
blockDB.Dispose();
// Insert a block instance
BlockReference blockInstance = new BlockReference(new Point3d(insertion.X, insertion.Y, 0), blockID);
blockInstance.Rotation = rotation;
blockInstance.ScaleFactors = blockScale;
blockInstance.Layer = layer;
// Explode the block instance
DBObjectCollection entities = new DBObjectCollection();
blockInstance.Explode(entities);
// Delete the block definition
blockInstance.Dispose();
// Change the layer of the block's entities
foreach (Entity entity in entities)
if (entity.Layer == "0") entity.Layer = layer;
// Delete the block reference from the database
currentTransaction.GetObject(blockID, OpenMode.ForWrite).Erase(true);
return entities;
}
// If no file was found
catch (Autodesk.AutoCAD.Runtime.Exception)
{
MText errorText = new MText();
errorText.Location = new Point3d(insertion.X, insertion.Y, 0);
errorText.Rotation = rotation;
errorText.TextHeight = TextHeight;
errorText.Contents ="Failed to locate file:\\P" +
(Properties.Settings.Default.BlockFolder + "\\" +
dwgName).Replace("\\", "\\\\") + ".dwg"; errorText.Attachment = AttachmentPoint.MiddleCenter;
errorText.Layer = layer;
DBObjectCollection rtrn = new DBObjectCollection();
rtrn.Add(errorText);
return rtrn;
}
}