[CommandMethod("BCC:MAV")]
public void Test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
//PromptIntegerOptions BCCADJUST = new PromptIntegerOptions("\nEnter Amount: ");//<<--- since we arent specifying any options in here its redundant
//PromptIntegerResult BCCADJUSTMENT = ed.GetInteger("\nEnter Amount: ");
//double BCCADJVALUE = BCCADJUSTMENT.Value;
PromptDoubleResult pdr = ed.GetDouble("\nEnter Amount: ");
if (pdr.Status != PromptStatus.OK) return;
SelectionFilter sf
= new SelectionFilter
(new TypedValue
[1] { new TypedValue
((int)DxfCode
.Start,
"INSERT") });//<<<--- a selection filter to get only block references
//while (0==0)
try
{
while (true)//<<--- the placement you used bothered my little brain, was stuck in my own while loop. if this loop fails, I want my program to exit
{
PromptSelectionResult psr = ed.GetSelection(sf);//<<<-----use a selection filter to avoid a crash on casting to block reference
if (psr.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Iterate the selection set
foreach (ObjectId brId in psr.Value.GetObjectIds())
{
// Open the block reference
BlockReference br = (BlockReference)tr.GetObject(brId, OpenMode.ForRead);//<<<<----- since you dont use a selection filter here this object could potentially not be a BR and crash
//Could also skip selection and go forward with:
//BlockReference br = tr.GetObject(brId, OpenMode.ForRead) as BlockReference;
//if (br == null) continue;
ed.WriteMessage("\n\nBlock: {0}", br.Name);
// Iterate the attribute collection
foreach (ObjectId attId in br.AttributeCollection)
{
// Open the attribute reference
//AttributeReference att = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);//<--- opening needlessly for write causes unneeded undo file entries
//instead:
AttributeReference att = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
ed.WriteMessage("\nOld {0} = {1} ", att.Tag, att.TextString);
try
{
double attValue = Convert.ToDouble(att.TextString);//<<---- since you are converting to double here would you be better to prompt for double instead of int?
double finalValue = attValue + pdr.Value;
ed.WriteMessage("\nAdjusted {0} => {1}", att.Tag, finalValue);
//upgrade here because we're about to make a modification
att.UpgradeOpen();//<-- we only reach this point if you are dealing with a number to be converted
att.TextString = finalValue.ToString();
att.DowngradeOpen();
}
catch { ed.WriteMessage("\nOne or more attributes don't contain numbers. It was skipped :)"); }
}
}
tr.Commit();
}
}
}
catch (System.Exception e)
{ Application.ShowAlertDialog(String.Format("Will Says You Failed Horribly :P\n{0}", e.Message)); } //<<<----- This function will return if the user presses escape: this code is only reached on catastrophic failure
}