[CommandMethod("ReplaceB1")]
public static void ReplaceBlock()
{
string name = GetBlockName();
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
ReplaceBlock(db, name, "MHF-400X600");
}
public static string GetBlockName()
{
string name = "NoName";
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions options = new PromptEntityOptions("\nSelect block reference");
options.SetRejectMessage("\nSelect only block reference");
options.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult acSSPrompt = ed.GetEntity(options);
using ( Transaction tx = db.TransactionManager.StartTransaction() )
{
BlockReference blockRef = tx.GetObject(acSSPrompt.ObjectId, OpenMode.ForRead) as BlockReference;
BlockTableRecord block = null;
if ( blockRef.IsDynamicBlock )
{
//get the real dynamic block name.
block = tx.GetObject(blockRef.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
}
else
{
block = tx.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
}
if ( block != null )
{
name = block.Name;
}
tx.Commit();
}
return name;
}
private static void ReplaceBlock(Database db, string oldName, string newName)
{
using ( var tr = db.TransactionManager.StartTransaction() )
{
var bt = (BlockTable) tr.GetObject(db.BlockTableId, OpenMode.ForRead);
// check if the block table contains old block
if ( !bt.Has(oldName) )
{
Application.ShowAlertDialog(oldName + " not found");
return;
}
var oldBlockId = bt[ oldName ];
// check if the block table contains new block
ObjectId newBlockId;
if ( bt.Has(newName) )
{
newBlockId = bt[ newName ];
}
// try to insert new bloc from a DWG file in search paths
else
{
try
{
var filename = HostApplicationServices.Current.FindFile("M:\\MozDownloads\\Блоки для Ч\\Блоки для ИЧ\\MHF\\" + newName + ".dwg", db, FindFileHint.Default);
using ( var sourceDb = new Database(false, true) )
{
sourceDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, true, null);
newBlockId = db.Insert(newName, sourceDb, true);
}
}
catch ( System.Exception )
{
Application.ShowAlertDialog(newName + " not found");
return;
}
}
var oldBtr = (BlockTableRecord) tr.GetObject(oldBlockId, OpenMode.ForRead);
foreach ( ObjectId id in oldBtr.GetBlockReferenceIds(true, true) )
{
var br = (BlockReference) tr.GetObject(id, OpenMode.ForWrite);
br.BlockTableRecord = newBlockId;
CreateBlockReference(newName, "0", br.Position);
}
tr.Commit();
}
}
public static void CreateBlockReference(string blockName, string blockRefLayer, Point3d blockRefPosition)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
using ( tr )
{
BlockTable bt = (BlockTable) db.BlockTableId.GetObject(OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord) bt[ blockName ].GetObject(OpenMode.ForRead);
BlockTableRecord curSpace_btr = (BlockTableRecord) tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
BlockReference bRef = new BlockReference(blockRefPosition, btr.ObjectId);
bRef.SetDatabaseDefaults();
bRef.Layer = blockRefLayer;
curSpace_btr.AppendEntity(bRef);
tr.AddNewlyCreatedDBObject(bRef, true);
tr.Commit();
}
}