How to create MLeader objects in .Net?

By Philippe Leefsma

 

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,

     &#
160;     
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();

    }

<

p style=”line-height: normal;margin: 0in 0in 0pt” class=”MsoNormal”>}


Comments

2 responses to “How to create MLeader objects in .Net?”

  1. Hi thanks for the great article.
    I have one question if I want to use built in source blocks like Box, Circle, detail callout,etc as blockcontent how I access them from drawing.
    thanks

  2. I’m not sure what you mean, there is no such thing as built-in blocks like box or circle, those are entities that can be inserted inside a block. So if the block already exist, you have to use its ObjectId that can be found if you know the block name, this is pretty much what is exposed in that sample. Otherwise you may have to create the block on the fly and insert whatever entities you want inside it. We have multiple samples on that blog that should help you create a block.

Leave a Reply to Philippe LeefsmaCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading