<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: attribute value to mleader in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11437136#M11638</link>
    <description>&lt;P&gt;you need to add:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;using System.Linq;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 21 Sep 2022 20:06:06 GMT</pubDate>
    <dc:creator>norman.yuan</dc:creator>
    <dc:date>2022-09-21T20:06:06Z</dc:date>
    <item>
      <title>attribute value to mleader</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11430240#M11630</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if there any Idea how can I get the attribute value from the dynamic block to mleader, once the mleader touches the block?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CADGeek_0-1663574963971.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1117648i6D98D273759EDF1E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CADGeek_0-1663574963971.png" alt="CADGeek_0-1663574963971.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2022 08:10:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11430240#M11630</guid>
      <dc:creator>CAD-Geek</dc:creator>
      <dc:date>2022-09-19T08:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: attribute value to mleader</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11433433#M11631</link>
      <description>&lt;P&gt;any replies would be appreciated. Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 13:03:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11433433#M11631</guid>
      <dc:creator>CAD-Geek</dc:creator>
      <dc:date>2022-09-20T13:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: attribute value to mleader</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11433753#M11632</link>
      <description>&lt;P&gt;When you say "once the mleader toches...", you want to describe how the "touching" happens, or how the mleader leader is created? Is the leader created by user using Acad built-in command? Or, you have code to create the leader?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it is former, you would have to handle some events, such as CommandEnded, or Database.ObjectAppended... to know when the command that user uses to create the leader is ended and identify the latest leader entity being created. Then your code needs to find the leader's pointing location to identify the target block reference at that location. If found, get the attribute value and update the leader's text/annotation. Or, you can even consider to use Overrule to overrule MLeader/Leader to force its text/Annotation's update as long as a target block reference can be locationed at its pointing location. With his approach, user user built-in acad command and expect the leader showing different text/annotation. However, user may have to know/be aware of,&amp;nbsp; that he/she needs to press Enter/Space to skip the chance to enter leader's text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it is the latter, it would be very easy, because if your code is to build the leader, you must have already asked the user to pick the leader's pointing location, or even to pick the target block, so, your code should either already know the block reference, or be able to locate the block reference, hence the attribute value. So, your code simply uses the attribute value as the leader's text/Annotation. That is, you code your own custom leader command for this particular operation. It is the usual/easier way to do, and it is straightforward for user to understand: "you want to do special leader? user my command".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd go with latter. Hopefully you do know how to create a leader with code (or at leader how to create entity in general).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 14:53:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11433753#M11632</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2022-09-20T14:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: attribute value to mleader</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11435771#M11633</link>
      <description>&lt;P&gt;Tks, &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/543921"&gt;@norman.yuan&lt;/a&gt; for your kind reply. here is the code that I'm using to place mleader.&lt;/P&gt;&lt;P&gt;refer to that &lt;A href="https://adndevblog.typepad.com/autocad/2012/05/how-to-create-mleader-objects-in-net.html" target="_blank" rel="noopener"&gt;link.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;What I want to achieve is, when the user picks the first point (which is touching the block that contains the attribute) then the user picks the endpoint when mleader placing, its value will be extracted from that block.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I'm not clear, maybe I can be more clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static void BlockMLeader()
        {
            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("N"))
                {
                    ed.WriteMessage("\nYou need to define a \"BlkLeader\" first...");
                    return;
                }
                PromptPointResult ppr;
                PromptPointOptions ppo = new PromptPointOptions("");
                // Prompt for the start point
                ppo.Message = "\nEnter the start point: ";
                ppr = doc.Editor.GetPoint(ppo);
                Point3d ptStart = ppr.Value;
                // Exit if the user presses ESC or cancels the command
                if (ppr.Status == PromptStatus.Cancel) return;
                // Prompt for the end point
                ppo.Message = "\nEnter the end point: ";
                ppo.UseBasePoint = true;
                ppo.BasePoint = ptStart;
                ppr = doc.Editor.GetPoint(ppo);
                Point3d ptEnd = ppr.Value;
                if (ppr.Status == PromptStatus.Cancel) return;

                MLeader leader = new MLeader();
                leader.SetDatabaseDefaults();
                leader.ContentType = ContentType.BlockContent;
                leader.BlockContentId = table["N"];
                leader.BlockPosition = new Point3d(ptEnd[0] + 5, ptEnd[1] + 5, 0);
                leader.ArrowSize = 100;
                leader.LandingGap = 120;
                leader.DoglegLength = 100;
                int i = leader.AddLeader();
                leader.AddLeaderLine(i);
                leader.AddFirstVertex(i, ptStart);
                leader.AddLastVertex(i, ptEnd);

                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();
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 10:51:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11435771#M11633</guid>
      <dc:creator>CAD-Geek</dc:creator>
      <dc:date>2022-09-21T10:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: attribute value to mleader</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11436084#M11634</link>
      <description>&lt;P&gt;Well, looks like you have the code to create the leader, and the only thing left out is to identify the given block reference at the leader pointing location (the "start point" your code ask the user to pick).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There could be different ways to identify the block as soon as the user picks the first point. Follow is one example, assuming you know the block's name and the target attribute's tag (if the block has multiple attributes):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;private bool GetLeaderPoint(Editor ed, string blockName, string attTag, out Point3d pickedPoint, out string leaderText)
{
    pickedPoint = Point3d.Origin;
    leaderText = "";

    var res = ed.GetPoint(
        "\nSelect point at the block to be annotated:");
    if (res.Status != PromptStatus.OK) return false;

    pickedPoint = res.Value;

    leaderText = GetBlockAtPoint(ed, pickedPoint, blockName, attTag);
    return true;
}

private string GetBlockAtPoint(Editor ed, Point3d pt, string blkName, string attTag)
{
    var attValue = "";

    // find all blocks with give block name
    // then identiy the closest one as the one at the picked point
    var filter = new SelectionFilter(new[] { new TypedValue((int)DxfCode.Start, "INSERT") });
    var res = ed.SelectAll(filter);
    if (res.Status != PromptStatus.OK) return null;

    var ids = res.Value.GetObjectIds();
    var db = ids[0].Database;
    var foundblks = new List&amp;lt;(ObjectId blkId, double distance)&amp;gt;();
    using (var tran = db.TransactionManager.StartTransaction())
    {
        // collect all blocks with the given name
        foreach (var id in ids)
        {
            var blk = (BlockReference)tran.GetObject(id, OpenMode.ForRead);
            if (blk.Name.ToUpper() == blkName.ToUpper())
            {
                foundblks.Add((id, pt.DistanceTo(blk.Position)));
            }
        }

        // Identify the closest one and retrieve attribute value with given tag
        if (foundblks.Count&amp;gt;0)
        {
            var blkId = (from found in foundblks orderby found.distance descending select found.blkId).First();
            var blk = (BlockReference)tran.GetObject(blkId, OpenMode.ForRead);
            foreach (ObjectId id in blk.AttributeCollection)
            {
                var att = (AttributeReference)tran.GetObject(id, OpenMode.ForRead);
                if (att.TextString.ToUpper()==attTag.ToUpper())
                {
                    attValue = att.TextString;
                    break;
                }
            }
        }

        tran.Commit();
    }

    return attValue;
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, you would just replace your code of the portion for the first point picking:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;                BlockTable table = Tx.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord model = Tx.GetObject(table[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                if (!table.Has("N"))
                {
                    ed.WriteMessage("\nYou need to define a \"BlkLeader\" first...");
                    return;
                }
                PromptPointResult ppr;
                PromptPointOptions ppo = new PromptPointOptions("");
                // Prompt for the start point
                //========== updated code to select start point========
                Point3d ptStart;
                string laederText;
                while(true)
                {
                   if (!GetLeaderPoint(
                       doc.Editor,[blkName], [attTag], out ptStart, out leaderText)) return;
                   if (string.IsNullOrEmpty(leaderText))
                   {
                      doc.Editor("\nCannot identiy target block at selected point! Please retry!");
                   }
                   else
                   {
                      break;
                   }
                }
                //============= End of the updated code ========

                // Prompt for the end point
                ppo.Message = "\nEnter the end point: ";
                ppo.UseBasePoint = true;
                ppo.BasePoint = ptStart;
                ppr = doc.Editor.GetPoint(ppo);
                Point3d ptEnd = ppr.Value;
                if (ppr.Status == PromptStatus.Cancel) return;

                MLeader leader = new MLeader();
                leader.SetDatabaseDefaults();
                leader.ContentType = ContentType.BlockContent;
                leader.BlockContentId = table["N"];
                leader.BlockPosition = new Point3d(ptEnd[0] + 5, ptEnd[1] + 5, 0);
                leader.ArrowSize = 100;
                leader.LandingGap = 120;
                leader.DoglegLength = 100;
                int i = leader.AddLeader();
                leader.AddLeaderLine(i);
                leader.AddFirstVertex(i, ptStart);
                leader.AddLastVertex(i, ptEnd);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use Editor.GetEntity(), instead of using GetPoint() for the first point selection, where you are user to pick the target block, and then use the PromptEntityResult.PickedPoint as the start point. The downside of this is the picking cursor is an entity selection cursor (a small square), not a "crosshair" meant for point selection. But it is acceptable, IMO. The benefit of this is you do not have to write code to identify target block, because the user has selected it and you can go ahead to retrieve the target attribute's value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 13:08:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11436084#M11634</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2022-09-21T13:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: attribute value to mleader</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11436178#M11635</link>
      <description>&lt;P&gt;Tks again for your reply. I have this error&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CADGeek_0-1663767477588.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1118791iE18B67CC93C8D15B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CADGeek_0-1663767477588.png" alt="CADGeek_0-1663767477588.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 13:38:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11436178#M11635</guid>
      <dc:creator>CAD-Geek</dc:creator>
      <dc:date>2022-09-21T13:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: attribute value to mleader</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11436372#M11636</link>
      <description>&lt;P&gt;Well, I am not sure which version of .NET framework you are targeting: if it is 4.7 or 4.8, the C# version should support this kind of "implicit" Tuple type. But you can simply explicitly declare the foundblks as List&amp;lt;Tuple&amp;lt;ObjectId, double&amp;gt;&amp;gt;, or just as Dictionary&amp;lt;ObjectId, double&amp;gt;. Something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var foundblks = new Dictionary&amp;lt;ObjectId, double&amp;gt;();&lt;/P&gt;
&lt;P&gt;... ...&lt;/P&gt;
&lt;P&gt;foreach (var id in ids)&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; var blk=(BlockReference)tran.GetObject(id, Open....);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if (blk.Name.ToUpper()==blkName.ToUpper())&lt;/P&gt;
&lt;P&gt;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; foundblk.Add(id, pt.DistanceTo(blk.Position));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if (foundblks.Count&amp;gt;0)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; var blkId = (from found in foundblks orderby found.Value descending select found.Key).First()&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 14:39:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11436372#M11636</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2022-09-21T14:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: attribute value to mleader</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11437080#M11637</link>
      <description>&lt;P&gt;Thanks again&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/543921"&gt;@norman.yuan&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;I face the same error. the .NET framework target is 4.8. Have you tried your code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CADGeek_1-1663789316096.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1118962i7419C795186CF483/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CADGeek_1-1663789316096.png" alt="CADGeek_1-1663789316096.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CADGeek_0-1663789097354.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1118961i357CF40DC26D27EE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CADGeek_0-1663789097354.png" alt="CADGeek_0-1663789097354.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 19:41:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11437080#M11637</guid>
      <dc:creator>CAD-Geek</dc:creator>
      <dc:date>2022-09-21T19:41:12Z</dc:date>
    </item>
    <item>
      <title>Re: attribute value to mleader</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11437136#M11638</link>
      <description>&lt;P&gt;you need to add:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;using System.Linq;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 20:06:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-value-to-mleader/m-p/11437136#M11638</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2022-09-21T20:06:06Z</dc:date>
    </item>
  </channel>
</rss>

