[CommandMethod("BlocksRenumbering", "brenum", CommandFlags.Modal | CommandFlags.Session | CommandFlags.Redraw)]
public static void TestRenumberBlocks()
{
// objects initializing
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
try
{
using (doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
PromptStringOptions psto = new PromptStringOptions("\nEnter a block name: ");
psto.AllowSpaces = true;
psto.DefaultValue = "Door";// change default block name here
PromptResult stres;
stres = ed.GetString(psto);
if (stres.Status != PromptStatus.OK)
return;
string oldblock = stres.StringResult;
ed.WriteMessage("\nBlock Name Entered:\t{0}\n", oldblock);
psto = new PromptStringOptions("\nEnter a tag name: ");
psto.AllowSpaces = true;
psto.DefaultValue = "SYM.";//change default tag name here
stres = ed.GetString(psto);
if (stres.Status != PromptStatus.OK)
return;
string tagname = stres.StringResult;
ed.WriteMessage("\nTag Name Entered:\t{0}\n", tagname);
PromptIntegerOptions pio = new PromptIntegerOptions("\nEnter an initial attribute value: ");
pio.AllowNegative = false;
pio.AllowNone = true;
pio.AllowZero = false;
pio.DefaultValue = 1;
PromptIntegerResult ires = ed.GetInteger(pio);
if (ires.Status != PromptStatus.OK)
return;
int init = ires.Value;
ed.WriteMessage("\nInitial Value Entered:\t{0}\n", init);
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!bt.Has(oldblock)) return;
ObjectId newblkId = bt[oldblock];
acadApp.SetSystemVariable("nomutt", 0);
TypedValue[] tvs = { new TypedValue(0, "insert"), new TypedValue(2, oldblock) };
SelectionFilter filt = new SelectionFilter(tvs);
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForRemoval = "You must select the blocks only!";
pso.MessageForAdding = "\nSelect blocks in the right order: ";
PromptSelectionResult res = ed.GetSelection(pso, filt);
if (res.Status != PromptStatus.OK) return;
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
SelectionSet sset = res.Value;
foreach (SelectedObject obj in sset)
{
Entity ent = (Entity)obj.ObjectId.GetObject(OpenMode.ForWrite, false, true) as Entity;
BlockReference oldblk = ent as BlockReference;
Autodesk.AutoCAD.DatabaseServices.AttributeCollection atts =
oldblk.AttributeCollection;
foreach (ObjectId id in atts)
{
Autodesk.AutoCAD.DatabaseServices.AttributeReference atref =
id.GetObject(OpenMode.ForWrite, false, true) as Autodesk.AutoCAD.DatabaseServices.AttributeReference;
if (atref.Tag == tagname.ToUpper())
{
atref.TextString = init.ToString();
init += 1;
break;
}
}
}
tr.Commit();
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
}
finally
{
acadApp.SetSystemVariable("nomutt", 0);
}
}