Inserting a dynamic block with c#

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi Guys!
This would be my first time writing in a forum.
So, I have been reading guides and copying codes to insert a block into current model space but unfortunately, none of it has worked. There is no error but the block was not inserted into the model space.
If you can direct me to any link that I can make as a reference or study, it would be a big help.
Here are some of my codes.
private void InsertBlock(string blockPath, string blockName)
{
var dm = AcApplication.DocumentManager;
var doc = dm.MdiActiveDocument;
var ed = doc.Editor;
using (var lockDoc = doc.LockDocument())
{
Database db = AcApplication.DocumentManager.MdiActiveDocument.Database;
ImportBlock(blockPath, blockName);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
var blockBtr = bt.Cast<ObjectId>().Select(s => (BlockTableRecord)trans.GetObject(s, OpenMode.ForRead)).
Where(s => s.IsDynamicBlock && s.Name == blockName).FirstOrDefault();
BlockTableRecord blockDef = bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
//ms = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
var point = new Point3d(0, 0, 0);
BlockReference blockRef = new BlockReference(point, blockBtr.ObjectId);
JigBlockMove jig = new JigBlockMove(blockRef);
var pr = ed.Drag(jig);
var entity = jig.GetEntity();
ms.AppendEntity(entity);
trans.AddNewlyCreatedDBObject(entity, true);
trans.Commit();
}
}
}
private void ImportBlock(string sourceFileName, string blockName)
{
if (!File.Exists(sourceFileName)) return;
DocumentCollection dm = AcApplication.DocumentManager;
var ed = dm.MdiActiveDocument.Editor;
Database destDb = dm.MdiActiveDocument.Database;
using (Database sourceDb = new Database(false, true))
{
try
{
// Read the DWG into a side database
sourceDb.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, 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);
// if the block table contains 'blockName', add its ObjectId to the collection
if (bt.Has(blockName))
blockIds.Add(bt[blockName]);
}
// Copy the block from source to destination database
if (blockIds.Count > 0)
{
IdMapping mapping = new IdMapping();
sourceDb.WblockCloneObjects(blockIds, destDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
}
//ed.WriteMessage("\nCopied 1 block definitions from "+ sourceFileName+ " to the current drawing.");
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\nError during copy: " + ex.Message + "\n" + ex.StackTrace);
}
}
}