21
Blocks / Numbering multileader with user block content
« Last post by fixo on March 24, 2013, 10:38:52 PM »In this code used multileader with attributed block "Callout Bubble - Imperial",
after this block is inserted, then deleted, we can create multileader with block "_DetailCallout".
Then we can to number multileader this way:
Code: [Select]
[CommandMethod("mab")]
public static void MleaderBlockNumbering()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
try
{
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false) as BlockTableRecord;
string prefix = ""; string suffix = ""; string atag = "SHEETNUMBER";// used "_DetailCallout" block fro Annotation palette
int start; int step; bool first = true;
ObjectId attnum = ObjectId.Null;
PromptStringOptions psto = new PromptStringOptions("\nPrefix: ");
psto.AllowSpaces = true;
PromptResult res;
res = ed.GetString(psto);
if (res.Status != PromptStatus.None)
prefix = res.StringResult;
psto = new PromptStringOptions("\nSuffix: ");
psto.AllowSpaces = true;
res = ed.GetString(psto);
if (res.Status != PromptStatus.None)
suffix = res.StringResult;
PromptIntegerOptions pio = new PromptIntegerOptions("");
pio.Message = "\nStarting number: ";
// Restrict input to positive and non-negative values
pio.AllowZero = false;
pio.AllowNegative = false;
pio.DefaultValue = 1;
PromptIntegerResult pires = ed.GetInteger(pio);
if (pires.Status != PromptStatus.OK) return;
start = pires.Value;
pio = new PromptIntegerOptions("");
pio.Message = "\nIncrement step: ";
// Restrict input to positive and non-negative values
pio.AllowZero = false;
pio.AllowNegative = false;
pio.DefaultValue = 1;
pires = ed.GetInteger(pio);
if (pires.Status != PromptStatus.OK) return;
step = pires.Value;
doc.TransactionManager.EnableGraphicsFlush(true);
//------------------------------------------------------------------------------------------
while (true)
{
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForRemoval = "\nWrong object type or multileader with no block selected";
pso.MessageForAdding = "\nSelect multileader with block: ";
pso.SingleOnly = true;
pso.SinglePickInSpace = true;
TypedValue[] tvs = new TypedValue[] {
new TypedValue((int) DxfCode.Operator, "<AND"),
new TypedValue((int) DxfCode.Start, "multileader"),
new TypedValue((int) DxfCode.Operator, "<NOT"),
new TypedValue((int) DxfCode.Operator, "<OR"),
new TypedValue(296, 0), new TypedValue(296, 1),
new TypedValue((int) DxfCode.Operator, "OR>"),
new TypedValue((int) DxfCode.Operator, "NOT>"),
new TypedValue((int) DxfCode .Operator, "AND>")
};
SelectionFilter sf = new SelectionFilter(tvs);
PromptSelectionResult psr = ed.GetSelection(pso, sf);
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\nWrong selection");
break;
}
Entity ent;
ObjectId id = ObjectId.Null;
SelectionSet sset = psr.Value;
ObjectId[] ids = sset.GetObjectIds();
id = ids[0];
ent = (Entity)tr.GetObject(id, OpenMode.ForRead, false);
MLeader mlead = ent as MLeader;
if (mlead == null) return;
mlead.UpgradeOpen();
if (first)
{
ObjectId blkid = mlead.BlockContentId;
BlockTableRecord mbtr = (BlockTableRecord)tr.GetObject(blkid, OpenMode.ForRead, false) as BlockTableRecord;
foreach (ObjectId attid in mbtr)
{
DBObject obj = tr.GetObject(attid, OpenMode.ForRead);
if (obj is AttributeDefinition)
{
AttributeDefinition attdef = obj as AttributeDefinition;
if (attdef.Tag == atag)
{
attnum = attdef.ObjectId;
break;
}
}
}
first = false;
}
AttributeReference attref = mlead.GetBlockAttribute(attnum);
attref.TextString = prefix + start.ToString() + suffix;
mlead.SetBlockAttribute(attnum, attref);
start = start + step;
tr.TransactionManager.QueueForGraphicsFlush();
}
doc.TransactionManager.FlushGraphics();
tr.Commit();
ed.UpdateScreen();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace);
}
finally
{
}
}
Recent Posts