You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The MLeader class represents a modern multi-leader annotation entity in AutoCAD (introduced in AutoCAD 2008). MLeaders are more flexible than legacy Leaders, supporting multiple leader lines, various content types (text, blocks, or none), and dedicated MLeader styles for consistent formatting.
using(Transactiontr=db.TransactionManager.StartTransaction()){BlockTableRecordbtr=tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite)asBlockTableRecord;// Create MLeaderMLeadermleader=newMLeader();mleader.SetDatabaseDefaults();// Set content type to MTextmleader.ContentType=ContentType.MTextContent;// Set text contentMTextmtext=newMText();mtext.Contents="This is an MLeader note";mleader.MText=mtext;// Add leader lineintleaderIndex=mleader.AddLeader();intlineIndex=mleader.AddLeaderLine(leaderIndex);// Set vertices (from text to feature)mleader.SetFirstVertex(lineIndex,newPoint3d(10,10,0));mleader.SetLastVertex(lineIndex,newPoint3d(0,0,0));// Add to drawingbtr.AppendEntity(mleader);tr.AddNewlyCreatedDBObject(mleader,true);tr.Commit();}
Example 2: Creating MLeader with Multiple Leader Lines
using(Transactiontr=db.TransactionManager.StartTransaction()){BlockTableRecordbtr=tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite)asBlockTableRecord;MLeadermleader=newMLeader();mleader.SetDatabaseDefaults();mleader.ContentType=ContentType.MTextContent;MTextmtext=newMText();mtext.Contents="Multiple Leaders";mleader.MText=mtext;// Add first leaderintleader1=mleader.AddLeader();intline1=mleader.AddLeaderLine(leader1);mleader.SetFirstVertex(line1,newPoint3d(10,10,0));mleader.SetLastVertex(line1,newPoint3d(0,0,0));// Add second leaderintleader2=mleader.AddLeader();intline2=mleader.AddLeaderLine(leader2);mleader.SetFirstVertex(line2,newPoint3d(10,10,0));mleader.SetLastVertex(line2,newPoint3d(5,-5,0));// Add third leaderintleader3=mleader.AddLeader();intline3=mleader.AddLeaderLine(leader3);mleader.SetFirstVertex(line3,newPoint3d(10,10,0));mleader.SetLastVertex(line3,newPoint3d(-5,5,0));btr.AppendEntity(mleader);tr.AddNewlyCreatedDBObject(mleader,true);tr.Commit();}
Example 3: Creating MLeader with Block Content
using(Transactiontr=db.TransactionManager.StartTransaction()){BlockTablebt=tr.GetObject(db.BlockTableId,OpenMode.ForRead)asBlockTable;BlockTableRecordbtr=tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite)asBlockTableRecord;MLeadermleader=newMLeader();mleader.SetDatabaseDefaults();// Set content type to Blockmleader.ContentType=ContentType.BlockContent;// Set block content (assuming "DetailTag" block exists)if(bt.Has("DetailTag")){mleader.BlockContentId=bt["DetailTag"];}// Add leaderintleaderIndex=mleader.AddLeader();intlineIndex=mleader.AddLeaderLine(leaderIndex);mleader.SetFirstVertex(lineIndex,newPoint3d(10,10,0));mleader.SetLastVertex(lineIndex,newPoint3d(0,0,0));btr.AppendEntity(mleader);tr.AddNewlyCreatedDBObject(mleader,true);tr.Commit();}
Example 4: Setting MLeader Properties
using(Transactiontr=db.TransactionManager.StartTransaction()){MLeadermleader=tr.GetObject(mleaderId,OpenMode.ForWrite)asMLeader;// Set leader line typemleader.LeaderLineType=LeaderType.SplineLeader;// Enable and set doglegmleader.EnableDogleg=true;mleader.DoglegLength=5.0;// Enable landingmleader.EnableLanding=true;// Set text attachmentmleader.TextAttachmentType=TextAttachmentType.AttachmentMiddle;// Set colormleader.ColorIndex=3;// Greentr.Commit();}
Example 5: Reading MLeader Information
using(Transactiontr=db.TransactionManager.StartTransaction()){MLeadermleader=tr.GetObject(mleaderId,OpenMode.ForRead)asMLeader;ed.WriteMessage("\n=== MLeader Information ===");ed.WriteMessage($"\nContent Type: {mleader.ContentType}");ed.WriteMessage($"\nLeader Line Type: {mleader.LeaderLineType}");ed.WriteMessage($"\nDogleg Enabled: {mleader.EnableDogleg}");ed.WriteMessage($"\nDogleg Length: {mleader.DoglegLength:F2}");ed.WriteMessage($"\nLanding Enabled: {mleader.EnableLanding}");if(mleader.ContentType==ContentType.MTextContent){ed.WriteMessage($"\nText Content: {mleader.MText.Contents}");}// Count leadersintleaderCount=0;for(inti=0;i<100;i++)// Arbitrary upper limit{try{IntegerCollectionlines=mleader.GetLeaderLineIndexes(i);if(lines!=null&&lines.Count>0)leaderCount++;elsebreak;}catch{break;}}ed.WriteMessage($"\nNumber of Leaders: {leaderCount}");tr.Commit();}
Example 6: Applying MLeader Style
using(Transactiontr=db.TransactionManager.StartTransaction()){MLeadermleader=tr.GetObject(mleaderId,OpenMode.ForWrite)asMLeader;// Get MLeader style dictionaryDBDictionarymleaderDict=tr.GetObject(db.MLeaderStyleDictionaryId,OpenMode.ForRead)asDBDictionary;if(mleaderDict.Contains("Annotative")){mleader.MLeaderStyle=mleaderDict.GetAt("Annotative");ed.WriteMessage("\nApplied 'Annotative' MLeader style");}tr.Commit();}
Example 7: Creating MLeader with No Content
using(Transactiontr=db.TransactionManager.StartTransaction()){BlockTableRecordbtr=tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite)asBlockTableRecord;// Create MLeader with no content (just leader lines)MLeadermleader=newMLeader();mleader.SetDatabaseDefaults();mleader.ContentType=ContentType.NoneContent;// Add leaderintleaderIndex=mleader.AddLeader();intlineIndex=mleader.AddLeaderLine(leaderIndex);mleader.SetFirstVertex(lineIndex,newPoint3d(10,0,0));mleader.SetLastVertex(lineIndex,newPoint3d(0,0,0));mleader.ColorIndex=1;// Redbtr.AppendEntity(mleader);tr.AddNewlyCreatedDBObject(mleader,true);tr.Commit();}
Example 8: Modifying MLeader Text
using(Transactiontr=db.TransactionManager.StartTransaction()){MLeadermleader=tr.GetObject(mleaderId,OpenMode.ForWrite)asMLeader;if(mleader.ContentType==ContentType.MTextContent){MTextmtext=mleader.MText;mtext.Contents="Updated text content\\PWith multiple lines";mtext.TextHeight=3.0;mleader.MText=mtext;ed.WriteMessage("\nUpdated MLeader text");}tr.Commit();}
Content Types
Type
Description
MTextContent
Multi-line text annotation
BlockContent
Block reference annotation
NoneContent
No content (leader lines only)
Leader Types
Type
Description
StraightLeader
Straight line segments
SplineLeader
Smooth spline curve
Text Attachment Types
Type
Description
AttachmentTop
Attach to top of text
AttachmentMiddle
Attach to middle of text
AttachmentBottom
Attach to bottom of text
MLeader vs Leader
Feature
Leader (Legacy)
MLeader (Modern)
Introduction
R13
2008
Multiple Leaders
No
Yes
Content Types
Limited
Text, Block, None
Styles
Dimension Style
MLeader Style
Flexibility
Basic
Advanced
Recommended
Legacy only
All new work
Best Practices
Use MLeader Styles: Create and use MLeader styles for consistency
Content Type: Choose appropriate content type for your needs
Multiple Leaders: Use for pointing to multiple features
Dogleg: Enable for horizontal landing line
Spline Leaders: Use for organic, curved appearance
Text Formatting: Use MText formatting codes for rich text
Block Content: Use for standardized callouts and tags
Layer Management: Place MLeaders on appropriate annotation layers