<?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: Get coordinate from block by C.# in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991995#M21615</link>
    <description>&lt;P&gt;Try this way, without any extension method.&lt;/P&gt;
&lt;PRE&gt;        // gets the polyline vertices
        public static IEnumerable&amp;lt;Point3d&amp;gt; GetVertices(Polyline pline)
        {
            for (int i = 0; i &amp;lt; pline.NumberOfVertices; i++)
            {
                yield return pline.GetPoint3dAt(i);
            }
        }

        [CommandMethod("GETCOORDINATESFROMBLOCK")]
        public static void GetCoordinatesFromBlock()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect block reference: ");
            options.SetRejectMessage("\nSelected object is not a block reference.");
            options.AddAllowedClass(typeof(BlockReference), true);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // get the selected block reference
                var blockReference = (BlockReference)tr.GetObject(result.ObjectId, OpenMode.ForRead);

                // get the block definition
                var blockDefinition = (BlockTableRecord)tr.GetObject(blockReference.BlockTableRecord, OpenMode.ForRead);

                // collect the polyline vertices in the block definition and transform them as the block reference
                var points =
                    blockDefinition                                             // from the block definition
                    .Cast&amp;lt;ObjectId&amp;gt;()                                           // get all polylines
                    .Where(id =&amp;gt; id.ObjectClass.DxfName == "LWPOLYLINE")
                    .Select(id =&amp;gt; (Polyline)tr.GetObject(id, OpenMode.ForRead))
                    .SelectMany(pl =&amp;gt; GetVertices(pl))                          // collect the polylines vertices
                    .Distinct()                                                 // remove duplicated points
                    .Select(p =&amp;gt; p.TransformBy(blockReference.BlockTransform))  // transform each point as the block reference
                    .OrderByDescending(p =&amp;gt; p.Y)                                // order points by Y descending
                    .ThenBy(p =&amp;gt; p.X);                                          // then by X

                // print the points coordinates on the command line and add a red circle on each one
                var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (var pt in points)
                {
                    ed.WriteMessage($"\n{pt:0.00}");
                    var circle = new Circle() { Center = pt, Radius = 1.0, ColorIndex = 1 };
                    space.AppendEntity(circle);
                    tr.AddNewlyCreatedDBObject(circle, true);
                }

                tr.Commit();
            }
        }&lt;/PRE&gt;</description>
    <pubDate>Wed, 28 Aug 2019 07:35:23 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2019-08-28T07:35:23Z</dc:date>
    <item>
      <title>Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8958754#M21607</link>
      <description>&lt;P&gt;Hi everybody,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a grid into block and want to get coordinate from this grid, but i want to select Block and then read grid into Block, and then define location on the model, or anyway other, can everybody see image and file .DWG below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using by C.#&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2019-08-10_9-03-40.png" style="width: 838px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/665685i0029E809E2B4838A/image-size/large?v=v2&amp;amp;px=999" role="button" title="2019-08-10_9-03-40.png" alt="2019-08-10_9-03-40.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;</description>
      <pubDate>Sat, 10 Aug 2019 02:14:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8958754#M21607</guid>
      <dc:creator>anhtrung</dc:creator>
      <dc:date>2019-08-10T02:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8958837#M21608</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should provide more details on how yo want to get these coordintes and show what you have tried so far.&lt;/P&gt;
&lt;P&gt;Anyway, you always can transform coordinates from the block defnition (BlockTableRecord) into block reference coordinates using the transformation matrix returned by BlockReference.BlockTransform property.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 06:50:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8958837#M21608</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2019-08-10T06:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8967343#M21609</link>
      <description>&lt;P&gt;it's that mean i have a block, and i want get all coordinates into this block, you can open attached file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2019 08:24:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8967343#M21609</guid>
      <dc:creator>anhtrung</dc:creator>
      <dc:date>2019-08-15T08:24:22Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8968030#M21610</link>
      <description>&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class="" title=""&gt;You should be able to get inspired by this little sample.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;using System.Collections.Generic;
using System.Linq;

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace GetCoordinatesFromBlockSample
{
    public class Commands
    {
        [CommandMethod("GETCOORDINATESFROMBLOCK")]
        public static void GetCoordinatesFromBlock()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect block reference: ");        
            options.SetRejectMessage("\nSelected object is not a block reference.");
            options.AddAllowedClass(typeof(BlockReference), true);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // get the selected block reference
                var blockReference = result.ObjectId.GetObject&amp;lt;BlockReference&amp;gt;();

                // get the block definition
                var blockDefinition = blockReference.BlockTableRecord.GetObject&amp;lt;BlockTableRecord&amp;gt;();

                // collect the polyline vertices in the block definition and transform them as the block reference
                var points =
                    blockDefinition                                             // from the block definition
                    .GetObjects&amp;lt;Polyline&amp;gt;()                                     // get app polylines
                    .SelectMany(pl =&amp;gt; pl.GetVertices())                         // collect the polylines vertices
                    .Distinct()                                                 // remove duplicated points
                    .Select(p =&amp;gt; p.TransformBy(blockReference.BlockTransform))  // transform each point as the block reference
                    .OrderByDescending(p =&amp;gt; p.Y)                                // order points by Y descending
                    .ThenBy(p =&amp;gt; p.X);                                          // then by X

                // print the points coordinates on the command mine and add a red circle on each one
                var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (var pt in points)
                {
                    ed.WriteMessage($"\n{pt:0.00}");
                    var circle = new Circle() { Center = pt, Radius = 2.0, ColorIndex = 1 };
                    space.AppendEntity(circle);
                    tr.AddNewlyCreatedDBObject(circle, true);
                }

                tr.Commit();
            }
        }
    }

    static class ExtensionMethods
    {
        // opens the ObjecId in the specified mode
        public static T GetObject&amp;lt;T&amp;gt;(this ObjectId id, OpenMode mode = OpenMode.ForRead) where T : DBObject =&amp;gt;
            (T)id.GetObject(mode);

        // gets all the the objects of type T in the BlockTable record
        public static IEnumerable&amp;lt;T&amp;gt; GetObjects&amp;lt;T&amp;gt;(this BlockTableRecord btr, OpenMode mode = OpenMode.ForRead) where T : Entity
        {
            var rxClass = RXObject.GetClass(typeof(T));
            foreach (ObjectId id in btr)
            {
                if (id.ObjectClass.IsDerivedFrom(rxClass))
                    yield return id.GetObject&amp;lt;T&amp;gt;(mode);
            }
        }

        // gets the polyline vertices
        public static IEnumerable&amp;lt;Point3d&amp;gt; GetVertices(this Polyline pline)
        {
            for (int i = 0; i &amp;lt; pline.NumberOfVertices; i++)
            {
                yield return pline.GetPoint3dAt(i);
            }
        }
    }
}
&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Aug 2019 14:39:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8968030#M21610</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2019-08-15T14:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991711#M21611</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your sharing code, but they have a error when i copy into my visual, i don't know why it's that. You can see attached image below.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2019-08-28_10-53-05.png" style="width: 979px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/671503i4F31DC47EFA5427D/image-size/large?v=v2&amp;amp;px=999" role="button" title="2019-08-28_10-53-05.png" alt="2019-08-28_10-53-05.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Help me please.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 03:57:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991711#M21611</guid>
      <dc:creator>anhtrung</dc:creator>
      <dc:date>2019-08-28T03:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991814#M21612</link>
      <description>&lt;P&gt;Using the lambda operator (=&amp;gt;) for single statement returned value came with C#6 (visual Studio 2015).&lt;/P&gt;
&lt;P&gt;For prior C# versions,&amp;nbsp; just use the standard return keyword and braces.&lt;/P&gt;
&lt;P&gt;Replace:&lt;/P&gt;
&lt;PRE&gt;        // opens the ObjecId in the specified mode
        public static T GetObject&amp;lt;T&amp;gt;(this ObjectId id, OpenMode mode = OpenMode.ForRead) where T : DBObject =&amp;gt;
            (T)id.GetObject(mode); &lt;/PRE&gt;
&lt;P&gt;with:&lt;/P&gt;
&lt;PRE&gt;        // opens the ObjecId in the specified mode
        public static T GetObject&amp;lt;T&amp;gt;(this ObjectId id, OpenMode mode = OpenMode.ForRead) where T : DBObject
            { return (T)id.GetObject(mode); }&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Aug 2019 05:47:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991814#M21612</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2019-08-28T05:47:36Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991934#M21613</link>
      <description>&lt;P&gt;it's still error you can see attached image below.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2019-08-28_13-38-24.png" style="width: 994px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/671555i1B67B4ABAD1FD27C/image-size/large?v=v2&amp;amp;px=999" role="button" title="2019-08-28_13-38-24.png" alt="2019-08-28_13-38-24.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 07:02:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991934#M21613</guid>
      <dc:creator>anhtrung</dc:creator>
      <dc:date>2019-08-28T07:02:35Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991951#M21614</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your support, but i see a lot of problem when i copy it into my code. can you send help me your file ?&lt;/P&gt;&lt;P&gt;thanks so much.&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2019-08-28_14-09-22.png" style="width: 966px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/671556i7902F971DF24216A/image-size/large?v=v2&amp;amp;px=999" role="button" title="2019-08-28_14-09-22.png" alt="2019-08-28_14-09-22.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 07:11:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991951#M21614</guid>
      <dc:creator>anhtrung</dc:creator>
      <dc:date>2019-08-28T07:11:26Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991995#M21615</link>
      <description>&lt;P&gt;Try this way, without any extension method.&lt;/P&gt;
&lt;PRE&gt;        // gets the polyline vertices
        public static IEnumerable&amp;lt;Point3d&amp;gt; GetVertices(Polyline pline)
        {
            for (int i = 0; i &amp;lt; pline.NumberOfVertices; i++)
            {
                yield return pline.GetPoint3dAt(i);
            }
        }

        [CommandMethod("GETCOORDINATESFROMBLOCK")]
        public static void GetCoordinatesFromBlock()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect block reference: ");
            options.SetRejectMessage("\nSelected object is not a block reference.");
            options.AddAllowedClass(typeof(BlockReference), true);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // get the selected block reference
                var blockReference = (BlockReference)tr.GetObject(result.ObjectId, OpenMode.ForRead);

                // get the block definition
                var blockDefinition = (BlockTableRecord)tr.GetObject(blockReference.BlockTableRecord, OpenMode.ForRead);

                // collect the polyline vertices in the block definition and transform them as the block reference
                var points =
                    blockDefinition                                             // from the block definition
                    .Cast&amp;lt;ObjectId&amp;gt;()                                           // get all polylines
                    .Where(id =&amp;gt; id.ObjectClass.DxfName == "LWPOLYLINE")
                    .Select(id =&amp;gt; (Polyline)tr.GetObject(id, OpenMode.ForRead))
                    .SelectMany(pl =&amp;gt; GetVertices(pl))                          // collect the polylines vertices
                    .Distinct()                                                 // remove duplicated points
                    .Select(p =&amp;gt; p.TransformBy(blockReference.BlockTransform))  // transform each point as the block reference
                    .OrderByDescending(p =&amp;gt; p.Y)                                // order points by Y descending
                    .ThenBy(p =&amp;gt; p.X);                                          // then by X

                // print the points coordinates on the command line and add a red circle on each one
                var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (var pt in points)
                {
                    ed.WriteMessage($"\n{pt:0.00}");
                    var circle = new Circle() { Center = pt, Radius = 1.0, ColorIndex = 1 };
                    space.AppendEntity(circle);
                    tr.AddNewlyCreatedDBObject(circle, true);
                }

                tr.Commit();
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Aug 2019 07:35:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8991995#M21615</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2019-08-28T07:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8992021#M21616</link>
      <description>&lt;P&gt;it's great with me. i can use it now.&lt;/P&gt;&lt;P&gt;Thanks for your the enthusiastic support&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 07:55:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/8992021#M21616</guid>
      <dc:creator>anhtrung</dc:creator>
      <dc:date>2019-08-28T07:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Get coordinate from block by C.#</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/9703526#M21617</link>
      <description>&lt;P&gt;Thank you very much! I did not immediately come across your post and spent a lot of time) I do not use AutoCAD as my main activity. I was trying to pull the text out of the blocks and drag them into model space so that I could just select all the text in the same programmatic way. When cloning to a layer from a block, I got some kind of offsets. What I just did not do to compensate for the difference) It turned out that everything had already been done and it was necessary to transform the position. But it came to the implementation of Graham scan to correctly sort the points ... and everything turned out to be in vain)&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 22:54:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-coordinate-from-block-by-c/m-p/9703526#M21617</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-08-20T22:54:10Z</dc:date>
    </item>
  </channel>
</rss>

