How to insert a dwg block using Brx
public static ObjectIdCollection InsertBlock(string blockName)
{
ObjectIdCollection ids = new ObjectIdCollection();
Database Database = HostApplicationServices.WorkingDatabase;
_AcDb.TransactionManager manager = Database.TransactionManager;
using (Transaction trans = manager.StartTransaction())
{
BlockTable blockTable = (BlockTable)trans.GetObject(Database.BlockTableId, OpenMode.ForRead);
ObjectId id;
BlockTableRecord rec;
using (Database tempDb = new Database(false, true))
{
tempDb.ReadDwgFile(blockName, FileShare.Read, true, string.Empty);
id = Database.Insert(blockName, tempDb, false);
rec = trans.GetObject(id, OpenMode.ForWrite) as BlockTableRecord;
}
Point3d insPt = new Point3d(0, 0, 0);
BlockReference blockReference = new BlockReference(insPt, blockTable[blockName]);
ids.Add(rec.AppendEntity(blockReference));
trans.AddNewlyCreatedDBObject(blockReference, true);
trans.Commit();
}
return ids;
}
Comments
-
couldn't get the insert code thing to work, anyway have a look
public static class Commands
{
[CommandMethod("test")]
static public void test()
{
string blk = "C:/Temp/blk.dwg";
InsertBlock(blk,"blk");
}public static ObjectIdCollection InsertBlock(string blockFullPath, string blockName)
{ObjectIdCollection ids = new ObjectIdCollection();
Database database = HostApplicationServices.WorkingDatabase;
AcDb.TransactionManager manager = database.TransactionManager;using (Transaction trans = manager.StartTransaction())
{
ObjectId id;
BlockTable blockTable = (BlockTable)trans.GetObject(database.BlockTableId, OpenMode.ForRead);using (Database tempDb = new Database(false, true))
{
tempDb.ReadDwgFile(blockFullPath, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
id = database.Insert(blockName, tempDb, false);
}
BlockTableRecord rec = trans.GetObject(database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
Point3d insPt = new Point3d(0, 0, 0);
BlockReference blockReference = new BlockReference(insPt, id);
ids.Add(rec.AppendEntity(blockReference));
trans.AddNewlyCreatedDBObject(blockReference, true);
trans.Commit();
}
return ids;
}
}0 -
Thanks a lot Daniel, really appreciate it.It works and that was quick.0