Below are two samples that illustrates MLeader creation in C#:
The first creates a MLeader with a MText content:
[CommandMethod("netTextMLeader")]
public static void netTextMLeader()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
BlockTable table = Tx.GetObject(
db.BlockTableId,
OpenMode.ForRead)
as BlockTable;
BlockTableRecord model = Tx.GetObject(
table[BlockTableRecord.ModelSpace],
OpenMode.ForWrite)
as BlockTableRecord;
MLeader leader = new MLeader();
leader.SetDatabaseDefaults();
leader.ContentType = ContentType.MTextContent;
MText mText = new MText();
mText.SetDatabaseDefaults();
mText.Width = 100;
mText.Height = 50;
mText.SetContentsRtf("MLeader");
mText.Location = new Point3d(4, 2, 0);
leader.MText = mText;
int idx = leader.AddLeaderLine(new Point3d(1, 1, 0));
leader.AddFirstVertex(idx, new Point3d(0, 0, 0));
model.AppendEntity(leader);
Tx.AddNewlyCreatedDBObject(leader, true);
Tx.Commit();
}
}
The second creates a MLeader with a Block content. It also handles the case where the MLeader block contains attributes and set them to a default value:
[CommandMethod("netBlockMLeader")]
public static void netBlockMLeader()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
BlockTable table = Tx.GetObject(
db.BlockTableId,
OpenMode.ForRead)
as BlockTable;
BlockTableRecord model = Tx.GetObject(
table[BlockTableRecord.ModelSpace],
OpenMode.ForWrite)
as BlockTableRecord;
if (!table.Has("BlkLeader"))
{
ed.WriteMessage(
"\nYou need to define a \"BlkLeader\" first...");
return;
}
MLeader leader = new MLeader();
leader.SetDatabaseDefaults();
leader.ContentType = ContentType.BlockContent;
leader.BlockContentId = table["BlkLeader"];
leader.BlockPosition = new Point3d(4, 2, 0);
int idx = leader.AddLeaderLine(new Point3d(1, 1, 0));
leader.AddFirstVertex(idx, new Point3d(0, 0, 0));
//Handle Block Attributes
int AttNumber = 0;
BlockTableRecord blkLeader = Tx.GetObject(
leader.BlockContentId,
OpenMode.ForRead)
as BlockTableRecord;
//Doesn't take in consideration oLeader.BlockRotation
Matrix3d Transfo = Matrix3d.Displacement(
leader.BlockPosition.GetAsVector());
foreach (ObjectId blkEntId in blkLeader)
{
AttributeDefinition AttributeDef = Tx.GetObject(
blkEntId,
OpenMode.ForRead)
as AttributeDefinition;
if (AttributeDef != null)
{
AttributeReference AttributeRef =
new AttributeReference();
AttributeRef.SetAttributeFromBlock(
AttributeDef,
Transfo);
AttributeRef.Position =
AttributeDef.Position.TransformBy(Transfo);
AttributeRef.TextString = "Attrib #" + (++AttNumber);
leader.SetBlockAttribute(blkEntId, AttributeRef);
}
}
model.AppendEntity(leader);
Tx.AddNewlyCreatedDBObject(leader, true);
Tx.Commit();
}
}