<?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 I want to Open Block Editor, Select all Lines ,and Join them to Create Polyline, and then save and Close Block Editor. in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/i-want-to-open-block-editor-select-all-lines-and-join-them-to/m-p/12567024#M5419</link>
    <description>&lt;P&gt;I want to join all the lines inside the blocks to Create Polyline, like we do manually by editing a block Definition&amp;nbsp;, type JOIN command select all lines, and then save Block Definition&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am currently using following code but getting error at JoinEntities Line&lt;/P&gt;&lt;P&gt;i cannot use start and endpoints of Lines as Irregular Polyline may get Generated, as i have 100s of Lines to Join&lt;/P&gt;&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4476837"&gt;@ActivistInvestor&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; [CommandMethod("cb")]
        public void JoinLinesInBlocks()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                foreach (ObjectId btrId in bt)
                {
                    BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForRead) as BlockTableRecord;

                    if (!btr.IsLayout)
                    {
                        BlockTableRecord blockDef = tr.GetObject(btrId, OpenMode.ForRead) as BlockTableRecord;

                        Entity[] entityCollection;

                        // Collect line entities in the block reference
                        int i = 0;
                        foreach (ObjectId entId in blockDef)
                        {
                           
                            Entity entity = tr.GetObject(entId, OpenMode.ForRead) as Entity;
                            if (entity is Line || entity is Arc || entity is Polyline)
                            {
                                entityCollection[i] = entity; i++;
                            }
                        }

                        // Check if there are at least two entities to create a polyline
                        if (entityCollection.Count() &amp;gt; 1)
                        {
                            // Create a new polyline
                            Polyline polyline = new Polyline();
                            blockDef.UpgradeOpen();

                            // Join entities to create a polyline
                            polyline.JoinEntities(entityCollection);

                            // Save the modified block
                            blockDef.AppendEntity(polyline);
                            tr.AddNewlyCreatedDBObject(polyline, true);
                        }
                    }
                }

                // Commit the transaction
                tr.Commit();
            }
        }
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 18 Feb 2024 00:19:06 GMT</pubDate>
    <dc:creator>official.jaydeo</dc:creator>
    <dc:date>2024-02-18T00:19:06Z</dc:date>
    <item>
      <title>I want to Open Block Editor, Select all Lines ,and Join them to Create Polyline, and then save and Close Block Editor.</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-want-to-open-block-editor-select-all-lines-and-join-them-to/m-p/12567024#M5419</link>
      <description>&lt;P&gt;I want to join all the lines inside the blocks to Create Polyline, like we do manually by editing a block Definition&amp;nbsp;, type JOIN command select all lines, and then save Block Definition&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am currently using following code but getting error at JoinEntities Line&lt;/P&gt;&lt;P&gt;i cannot use start and endpoints of Lines as Irregular Polyline may get Generated, as i have 100s of Lines to Join&lt;/P&gt;&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4476837"&gt;@ActivistInvestor&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; [CommandMethod("cb")]
        public void JoinLinesInBlocks()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                foreach (ObjectId btrId in bt)
                {
                    BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForRead) as BlockTableRecord;

                    if (!btr.IsLayout)
                    {
                        BlockTableRecord blockDef = tr.GetObject(btrId, OpenMode.ForRead) as BlockTableRecord;

                        Entity[] entityCollection;

                        // Collect line entities in the block reference
                        int i = 0;
                        foreach (ObjectId entId in blockDef)
                        {
                           
                            Entity entity = tr.GetObject(entId, OpenMode.ForRead) as Entity;
                            if (entity is Line || entity is Arc || entity is Polyline)
                            {
                                entityCollection[i] = entity; i++;
                            }
                        }

                        // Check if there are at least two entities to create a polyline
                        if (entityCollection.Count() &amp;gt; 1)
                        {
                            // Create a new polyline
                            Polyline polyline = new Polyline();
                            blockDef.UpgradeOpen();

                            // Join entities to create a polyline
                            polyline.JoinEntities(entityCollection);

                            // Save the modified block
                            blockDef.AppendEntity(polyline);
                            tr.AddNewlyCreatedDBObject(polyline, true);
                        }
                    }
                }

                // Commit the transaction
                tr.Commit();
            }
        }
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Feb 2024 00:19:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-want-to-open-block-editor-select-all-lines-and-join-them-to/m-p/12567024#M5419</guid>
      <dc:creator>official.jaydeo</dc:creator>
      <dc:date>2024-02-18T00:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: I want to Open Block Editor, Select all Lines ,and Join them to Create Polyline, and then save and Close Block Editor.</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-want-to-open-block-editor-select-all-lines-and-join-them-to/m-p/12567406#M5420</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;To join lines and arcs into a polyline, the entity on which the JoinEntities method is called must be a polyline.&lt;/P&gt;
&lt;P&gt;You can try with something like this:&lt;/P&gt;
&lt;P&gt;Edit: More robust version&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static void MJoinToPolylines(IEnumerable&amp;lt;Curve&amp;gt; source, Transaction tr)
{
    if (source is null) throw new ArgumentNullException(nameof(source));
    if (tr is null) throw new ArgumentNullException(nameof(tr));

    var curves = source
        .Where (c =&amp;gt; c is Arc || c is Line || c is Polyline)
        .ToList();
    if (curves.Count == 0)
        return;
    var owner = (BlockTableRecord)tr.GetObject(curves[0].OwnerId, OpenMode.ForWrite);

    while (0 &amp;lt; curves.Count)
    {
        Polyline polyline = null;
        Curve curve = curves[0];
        switch (curve)
        {
            case Polyline pline:
                polyline = (Polyline)pline.Clone();
                break;
            case Line line:
                var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
                polyline = new Polyline();
                polyline.AddVertexAt(0, line.StartPoint.Convert2d(plane), 0.0, 0.0, 0.0);
                polyline.AddVertexAt(1, line.EndPoint.Convert2d(plane), 0.0, 0.0, 0.0);
                break;
            case Arc arc:
                plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
                polyline = new Polyline();
                double angle = arc.EndAngle - arc.StartAngle;
                if (angle &amp;lt; 0)
                    angle += Math.PI * 2;
                double bulge = Math.Tan(angle / 4);
                polyline.AddVertexAt(0, arc.StartPoint.Convert2d(plane), bulge, 0.0, 0.0);
                polyline.AddVertexAt(1, arc.EndPoint.Convert2d(plane), 0.0, 0.0, 0.0);
                break;
        }
        curves.Remove(curve);
        if (polyline != null)
        {
            owner.AppendEntity(polyline);
            tr.AddNewlyCreatedDBObject(polyline, true);
            try
            {
                var indices = polyline.JoinEntities(curves.ToArray());
                if (!curve.IsWriteEnabled) tr.GetObject(curve.ObjectId, OpenMode.ForWrite);
                curve.Erase();
                foreach (Curve c in indices.Cast&amp;lt;int&amp;gt;()
                                           .Select(i =&amp;gt; curves[i])
                                           .ToArray())
                {
                    curves.Remove(c);
                    if (!c.IsWriteEnabled) tr.GetObject(c.ObjectId, OpenMode.ForWrite);
                    c.Erase();
                }
            }
            catch
            {
                polyline.Erase();
            }
        }
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your command:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("cb")]
        public void JoinLinesInBlocks()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                var validClassNames = new List&amp;lt;string&amp;gt; { "AcDbArc", "AcDbLine", "AcDbPolyline" };
                foreach (ObjectId btrId in bt)
                {
                    var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                    if (!btr.IsLayout)
                    {
                        var curves = btr
                            .Cast&amp;lt;ObjectId&amp;gt;()
                            .Where(id =&amp;gt; validClassNames.Contains(id.ObjectClass.Name))
                            .Select(id =&amp;gt; (Curve)tr.GetObject(id, OpenMode.ForRead));
                        MJoinToPolylines(curves, tr);
                    }
                }
                tr.Commit();
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2024 15:58:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-want-to-open-block-editor-select-all-lines-and-join-them-to/m-p/12567406#M5420</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-02-19T15:58:56Z</dc:date>
    </item>
  </channel>
</rss>

