<?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 : Add Entities in Layout in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6779072#M33344</link>
    <description>&lt;P&gt;Hi @Anonymous,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following sample certainly doesn't exactly match your request (that I'm not sure to completely understand), but you should probably get some inspiration from it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

[assembly: CommandClass(typeof(MultiInsertInLayout.Commands))]

namespace MultiInsertInLayout
{
    public class Commands
    {
        [CommandMethod("Test")]
        public static void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            Editor ed = doc.Editor;

            var selRes = ed.GetSelection();
            if (selRes.Status != PromptStatus.OK)
                return;

            var ptRes = ed.GetPoint("\nSpecify the base point: ");
            if (ptRes.Status != PromptStatus.OK)
                return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var btrId = CreateBlock("123", ptRes.Value.TransformBy(ed.CurrentUserCoordinateSystem), selRes.Value, true);
                var layout = ClearOrCreateLayout(db, "Plot");
                int nbInserts = MultiInsertInLayout(btrId, layout);
                ed.WriteMessage($"\nThe block has been inserted {nbInserts} times.");
                tr.Commit();
            }
        }

        private static ObjectId CreateBlock(string blockName, Point3d basePoint, SelectionSet selSet, bool convertToBlock)
        {
            var db = selSet[0].ObjectId.Database;
            var tr = db.TransactionManager.TopTransaction;
            var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
            BlockTableRecord btr;
            ObjectId btrId;
            if (bt.Has("123"))
            {
                btrId = bt["123"];
                btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForWrite);
                foreach (ObjectId id in btr)
                {
                    var ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                    ent.Erase();
                }
            }
            else
            {
                btr = new BlockTableRecord();
                btr.Name = "123";
                bt.UpgradeOpen();
                btrId = bt.Add(btr);
                tr.AddNewlyCreatedDBObject(btr, true);
            }
            var ids = new ObjectIdCollection(selSet.GetObjectIds());
            var mapping = new IdMapping();
            db.DeepCloneObjects(ids, btrId, mapping, false);
            var xform = Matrix3d.Displacement(basePoint.GetAsVector().Negate());
            foreach (IdPair pair in mapping)
            {
                if (pair.IsPrimary &amp;amp;&amp;amp; pair.IsCloned)
                {
                    var ent = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
                    ent.TransformBy(xform);
                    if (convertToBlock)
                    {
                        var source = (Entity)tr.GetObject(pair.Key, OpenMode.ForWrite);
                        source.Erase();
                    }
                }
            }
            if (convertToBlock)
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                var br = new BlockReference(basePoint, btrId);
                curSpace.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
            }

            return btrId;
        }

        private static Layout ClearOrCreateLayout(Database db, string layoutName)
        {
            var tr = db.TransactionManager.TopTransaction;
            var layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
            Layout layout;
            if (layoutDict.Contains(layoutName))
            {
                layout = (Layout)tr.GetObject(layoutDict.GetAt(layoutName), OpenMode.ForWrite);
            }
            else
            {
                var layoutId = LayoutManager.Current.CreateLayout(layoutName);
                layout = (Layout)tr.GetObject(layoutId, OpenMode.ForWrite);
            }
            LayoutManager.Current.CurrentLayout = layoutName;
            var btr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead);
            foreach (ObjectId id in btr)
            {
                var ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                ent.Erase();
            }
            return layout;
        }

        private static int MultiInsertInLayout(ObjectId btrId, Layout layout)
        {
            var db = btrId.Database;
            var tr = db.TransactionManager.TopTransaction;

            // get the block definition extents and center
            var extents = new Extents3d();
            var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
            foreach (ObjectId id in btr)
            {
                var ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                var bounds = ent.Bounds;
                if (bounds.HasValue)
                    extents.AddExtents(bounds.Value);
            }
            var blkWidth = extents.MaxPoint.X - extents.MinPoint.X;
            var blkHeight = extents.MaxPoint.Y - extents.MinPoint.Y;
            var blkCenter = new Point3d(
                (extents.MaxPoint.X + extents.MinPoint.X) / 2.0,
                (extents.MaxPoint.Y + extents.MinPoint.Y) / 2.0,
                0.0);

            // get the plot area extents
            var plotMargins = layout.PlotPaperMargins;
            double plotWidth = 0.0, plotHeight = 0.0;
            switch (layout.PlotRotation)
            {
                case PlotRotation.Degrees000:
                case PlotRotation.Degrees180:
                    plotWidth = layout.PlotPaperSize.X - plotMargins.MinPoint.X - plotMargins.MaxPoint.X;
                    plotHeight = layout.PlotPaperSize.Y - plotMargins.MinPoint.Y - plotMargins.MaxPoint.Y;
                    break;
                case PlotRotation.Degrees090:
                case PlotRotation.Degrees270:
                    plotWidth = layout.PlotPaperSize.Y - plotMargins.MinPoint.Y - plotMargins.MaxPoint.Y;
                    plotHeight = layout.PlotPaperSize.X - plotMargins.MinPoint.X - plotMargins.MaxPoint.X;
                    break;
            }

            // compute the scale so that the block can be inserted at least 4 times
            double scale = 1.0;
            while (plotWidth &amp;lt; 2.0 * scale * blkWidth || plotHeight &amp;lt; 2.0 * scale * blkHeight)
                scale /= 2.0;
            blkWidth *= scale;
            blkHeight *= scale;
            int columns = (int)(plotWidth / blkWidth);
            int rows = (int)(plotHeight / blkHeight);
            double colWidth = plotWidth / columns;
            double rowHeight = plotHeight / rows;

            // compute the displacement vector from block center to 'cell' center 
            var disp =
                new Vector3d(colWidth, rowHeight, 0.0) / 2.0 - blkCenter.GetAsVector() * scale;
                //new Vector3d(extents.MinPoint.X + extents.MaxPoint.X, extents.MinPoint.Y + extents.MaxPoint.Y, 0.0) * scale / 2.0;

            // insert the blocks
            var ps = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);
            for (int i = 0; i &amp;lt; rows; i++)
            {
                for (int j = 0; j &amp;lt; columns; j++)
                {
                    Point3d insPt = new Point3d(layout.PlotOrigin.X + j * colWidth, layout.PlotOrigin.Y + i * rowHeight, 0.0);
                    var br = new BlockReference(insPt + disp, btr.ObjectId) { ScaleFactors = new Scale3d(scale) };
                    ps.AppendEntity(br);
                    tr.AddNewlyCreatedDBObject(br, true);
                }
            }
            return rows * columns;
        }
    }
}
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 03 Jan 2017 09:18:33 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2017-01-03T09:18:33Z</dc:date>
    <item>
      <title>Creating Block of Entities and blocks are not getting added into the layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6770950#M33337</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. I am creating a block of entities and place it at some point in drawing , but it gets placed at some distance from the given point.&lt;/P&gt;&lt;P&gt;2. The blocks created are not getting added in the layout. i want to at least add 3 blocks in a layout by scaling it down.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have scaled the block accordingly but when i try to add the block in layout the block is not getting inserted in the particular layout as I am not able to get the points on the layout to insert the blocks. Is there any other way so that the the blocks can be inserted in layout automatically.?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 14:31:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6770950#M33337</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-12-27T14:31:34Z</dc:date>
    </item>
    <item>
      <title>Re : Add Entities in Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6770968#M33338</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's quite impossible to help you with so few informations.&lt;/P&gt;
&lt;P&gt;You should show some code and, perhaps, the block you try to insert.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 14:28:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6770968#M33338</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-12-27T14:28:47Z</dc:date>
    </item>
    <item>
      <title>Re : Add Entities in Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6771835#M33339</link>
      <description>&lt;P&gt;Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;Editor ed = doc.Editor;&lt;BR /&gt;Database db = doc.Database;&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;using (DocumentLock locDoc = ed.Document.LockDocument())&lt;BR /&gt;{&lt;BR /&gt;Transaction tr = db.TransactionManager.StartTransaction();&lt;BR /&gt;using (tr)&lt;BR /&gt;{&lt;BR /&gt;BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite, false);&lt;BR /&gt;BlockTableRecord btr = new BlockTableRecord();&lt;BR /&gt;btr.Name = "BLOCK1";&lt;BR /&gt;bt.UpgradeOpen();&lt;BR /&gt;bt.Add(btr);&lt;BR /&gt;tr.AddNewlyCreatedDBObject(btr, true);&lt;BR /&gt;btr.Explodable = false;&lt;BR /&gt;Entity entity = (Entity)tr.GetObject(objectId, OpenMode.ForRead);&lt;BR /&gt;Entity entClone = entity.Clone() as Entity;&lt;BR /&gt;&lt;BR /&gt;btr.AppendEntity(entClone);&lt;BR /&gt;tr.AddNewlyCreatedDBObject(entClone, true);&lt;BR /&gt;PromptPointResult pr = ed.GetPoint("\nEnter the insertion point: ");&lt;BR /&gt;if (pr.Status == PromptStatus.OK)&lt;BR /&gt;{&lt;BR /&gt;BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord; BlockReference br = new BlockReference(pr.Value, btr.ObjectId);&lt;BR /&gt;ms.AppendEntity(br);&lt;BR /&gt;tr.AddNewlyCreatedDBObject(br, true); Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen();&lt;BR /&gt;}&lt;BR /&gt;tr.Commit();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (Autodesk.AutoCAD.Runtime.Exception e)&lt;BR /&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/// &lt;SPAN&gt;I am creating a block of entities and place it at some point in drawing , but it gets placed at some distance from the given point.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2016 23:07:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6771835#M33339</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-12-27T23:07:05Z</dc:date>
    </item>
    <item>
      <title>Re : Add Entities in Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6772279#M33340</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;&lt;SPAN&gt;I see two possible reasons:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;&lt;BR /&gt;&lt;SPAN&gt;1. The current UCS is different from the GIS at the time of insertion (keep in mind that only Editor methods work about current UCS)&lt;/SPAN&gt;. This can be easily corrected by transforming the specified point:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;BlockReference br = new BlockReference(pr.Value&lt;STRONG&gt;.TransformBy(ed.CurrentUserCoordinateSystem)&lt;/STRONG&gt;, btr.ObjectId);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;2. The base point of the block is not correctly defined. The way you create the block definition (BlockTableRecord), the block base point is WCS origin.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Dec 2016 08:36:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6772279#M33340</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-12-28T08:36:46Z</dc:date>
    </item>
    <item>
      <title>Re : Add Entities in Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6772286#M33341</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;just a casual observation :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is this line accomplishing&amp;nbsp;your end ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Entity entity = (Entity)tr.GetObject(objectId, OpenMode.ForRead);
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;objectId variable doesn't seem to have a value in the code posted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;//-----------------&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I can't follow the code logic ... but it's my holiday and probably my fault.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;//-----------------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally I'd split the code &amp;nbsp;;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;one method to assert that the BlockReference exists ( or build it )&lt;/P&gt;
&lt;P&gt;one method to insert the block ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;easier to read&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and easier to test&lt;/P&gt;
&lt;P&gt;therefore less heartache for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Dec 2016 08:52:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6772286#M33341</guid>
      <dc:creator>kerry_w_brown</dc:creator>
      <dc:date>2016-12-28T08:52:02Z</dc:date>
    </item>
    <item>
      <title>Re : Add Entities in Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6775830#M33342</link>
      <description>&lt;P&gt;Thank You &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Dec 2016 10:42:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6775830#M33342</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-12-30T10:42:09Z</dc:date>
    </item>
    <item>
      <title>Re : Add Entities in Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6777878#M33343</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also having Problems adding blocks in layout. i have scaled the block and want add it in paper space.&lt;/P&gt;&lt;P&gt;the code below gives you the block reference of selected entities.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/////////////////////////////////////////////////////////////&lt;BR /&gt;public static BlockReference JoinEntitiesandScale(Point3d Point,double scale)&lt;BR /&gt;{&lt;BR /&gt;Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;Database db = doc.Database;&lt;BR /&gt;Editor ed = doc.Editor;&lt;BR /&gt;PromptSelectionOptions opts = new PromptSelectionOptions();&lt;BR /&gt;opts.MessageForAdding = "Select entities: ";&lt;/P&gt;&lt;P&gt;PromptSelectionResult res = ed.GetSelection(opts);&lt;BR /&gt;SelectionSet selSet = res.Value;&lt;BR /&gt;ObjectId[] idArray = selSet.GetObjectIds();&lt;BR /&gt;BlockReference insert;&lt;BR /&gt;//ObjectId blockId = ObjectId.Null;&lt;BR /&gt;Transaction tr = db.TransactionManager.StartTransaction();&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;using (DocumentLock locDoc = ed.Document.LockDocument())&lt;BR /&gt;{&lt;BR /&gt;using (tr)&lt;BR /&gt;{&lt;BR /&gt;BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);&lt;BR /&gt;//bt.UpgradeOpen();&lt;BR /&gt;BlockTableRecord record = new BlockTableRecord();&lt;BR /&gt;record.Name = "123";&lt;BR /&gt;bt.UpgradeOpen();&lt;BR /&gt;if (!bt.Has(record.Name))&lt;BR /&gt;{&lt;BR /&gt;ObjectId btrId = bt.Add(record);&lt;BR /&gt;tr.AddNewlyCreatedDBObject(record, true);&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;record = (BlockTableRecord)tr.GetObject(bt[record.Name], OpenMode.ForWrite);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;ObjectIdCollection collection = new ObjectIdCollection(idArray);&lt;BR /&gt;IdMapping mapping = new IdMapping();&lt;BR /&gt;db.DeepCloneObjects(collection, record.ObjectId, mapping, false);&lt;/P&gt;&lt;P&gt;Matrix3d newscaleMatrix = Matrix3d.Scaling(scale, Point);&lt;BR /&gt;//BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead);&lt;BR /&gt;foreach (ObjectId entId in record)&lt;BR /&gt;{&lt;BR /&gt;DBObject obj = tr.GetObject(entId, OpenMode.ForRead);&lt;BR /&gt;Entity ent = obj as Entity;&lt;BR /&gt;if (ent != null)&lt;BR /&gt;{&lt;BR /&gt;ent.UpgradeOpen();&lt;BR /&gt;ent.TransformBy(newscaleMatrix);&lt;BR /&gt;ent.DowngradeOpen();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;BlockTableRecord curSpace = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);&lt;BR /&gt;insert = new BlockReference(Point, record.ObjectId);&lt;BR /&gt;curSpace.AppendEntity(insert);&lt;BR /&gt;tr.AddNewlyCreatedDBObject(insert, true);&lt;BR /&gt;tr.Commit();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;if (insert != null)&lt;BR /&gt;{&lt;BR /&gt;return insert;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (System.Exception)&lt;BR /&gt;{&lt;BR /&gt;}&lt;BR /&gt;return null;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;/////////////////////////////////////////////////////////////////////&lt;/P&gt;&lt;P&gt;after creating a block reference i want to add it in layout. my Code is.&lt;/P&gt;&lt;P&gt;////////////////////////////////////////////////////////////////&lt;/P&gt;&lt;P&gt;public static void Layout()&lt;BR /&gt;{&lt;BR /&gt;var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;if (doc == null)&lt;BR /&gt;return;&lt;BR /&gt;var db = doc.Database;&lt;BR /&gt;var ed = doc.Editor;&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;Transaction tr = db.TransactionManager.StartTransaction();&lt;BR /&gt;using (tr)&lt;BR /&gt;{&lt;BR /&gt;PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");&lt;BR /&gt;if (pr.Status == PromptStatus.OK)&lt;BR /&gt;{&lt;BR /&gt;DCS.RxServices.CADUtility.JoinEntitiesandScale(new Point3d(pr.Value.X, pr.Value.Y, 0), 0.25);&lt;/P&gt;&lt;P&gt;ObjectId id = LayoutManager.Current.CreateAndMakeLayoutCurrent("Plot", true);&lt;BR /&gt;Layout lay = (Layout)tr.GetObject(id, OpenMode.ForWrite);&lt;BR /&gt;lay.SetPlotSettings(&lt;BR /&gt;//"ISO_full_bleed_2A0_(1189.00_x_1682.00_MM)", // Try this big boy!&lt;BR /&gt;//"ANSI_B_(11.00_x_17.00_Inches)",&lt;BR /&gt;"ISO A4 (210.00 x 297.00 MM)",&lt;BR /&gt;"monochrome.ctb",&lt;BR /&gt;"DWGF6 ePlot.pc3"&lt;BR /&gt;);&lt;BR /&gt;}&lt;BR /&gt;tr.commit();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;PromptSelectionOptions opts = new PromptSelectionOptions();&lt;BR /&gt;opts.MessageForAdding = "Select entities: ";&lt;BR /&gt;PromptSelectionResult res = ed.GetSelection(opts);&lt;BR /&gt;//1 block selected&lt;BR /&gt;if (res.Status == PromptStatus.OK)&lt;BR /&gt;{&lt;BR /&gt;SelectionSet selSet = res.Value;&lt;BR /&gt;ObjectId[] idArray = selSet.GetObjectIds();&lt;BR /&gt;Transaction tr1 = db.TransactionManager.StartTransaction();&lt;BR /&gt;using (tr1)&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt;BlockTableRecord space = tr1.GetObject(SymbolUtilityServices.GetBlockPaperSpaceId(db), OpenMode.ForWrite) as BlockTableRecord;&lt;/P&gt;&lt;P&gt;foreach (ObjectId entId in idArray)&lt;BR /&gt;{&lt;BR /&gt;Entity entity = tr1.GetObject(entId, OpenMode.ForWrite) as Entity;&lt;BR /&gt;Entity entClone = entity.Clone() as Entity;&lt;BR /&gt;space.AppendEntity(entity);&lt;BR /&gt;tr1.AddNewlyCreatedDBObject(entity, true);&lt;BR /&gt;ObjectIdCollection collection = new ObjectIdCollection(idArray);&lt;BR /&gt;IdMapping mapping = new IdMapping();&lt;BR /&gt;db.DeepCloneObjects(collection, space.ObjectId, mapping, false);&lt;/P&gt;&lt;P&gt;entity.Erase();&lt;BR /&gt;}&lt;BR /&gt;tr1.Commit();&lt;/P&gt;&lt;P&gt;}&lt;BR /&gt;ed.Regen();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (Autodesk.AutoCAD.Runtime.Exception e)&lt;BR /&gt;{&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;///////////////////////////////////////////////////////&lt;/P&gt;&lt;P&gt;i want to add atleast 4 block in a layout by scaling it down. i have tried this using view ports and have created 4 viewports in a layout but is there any other way i can add blocks without using viewports.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jan 2017 06:26:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6777878#M33343</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-01-02T06:26:39Z</dc:date>
    </item>
    <item>
      <title>Re : Add Entities in Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6779072#M33344</link>
      <description>&lt;P&gt;Hi @Anonymous,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following sample certainly doesn't exactly match your request (that I'm not sure to completely understand), but you should probably get some inspiration from it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

[assembly: CommandClass(typeof(MultiInsertInLayout.Commands))]

namespace MultiInsertInLayout
{
    public class Commands
    {
        [CommandMethod("Test")]
        public static void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            Editor ed = doc.Editor;

            var selRes = ed.GetSelection();
            if (selRes.Status != PromptStatus.OK)
                return;

            var ptRes = ed.GetPoint("\nSpecify the base point: ");
            if (ptRes.Status != PromptStatus.OK)
                return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var btrId = CreateBlock("123", ptRes.Value.TransformBy(ed.CurrentUserCoordinateSystem), selRes.Value, true);
                var layout = ClearOrCreateLayout(db, "Plot");
                int nbInserts = MultiInsertInLayout(btrId, layout);
                ed.WriteMessage($"\nThe block has been inserted {nbInserts} times.");
                tr.Commit();
            }
        }

        private static ObjectId CreateBlock(string blockName, Point3d basePoint, SelectionSet selSet, bool convertToBlock)
        {
            var db = selSet[0].ObjectId.Database;
            var tr = db.TransactionManager.TopTransaction;
            var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
            BlockTableRecord btr;
            ObjectId btrId;
            if (bt.Has("123"))
            {
                btrId = bt["123"];
                btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForWrite);
                foreach (ObjectId id in btr)
                {
                    var ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                    ent.Erase();
                }
            }
            else
            {
                btr = new BlockTableRecord();
                btr.Name = "123";
                bt.UpgradeOpen();
                btrId = bt.Add(btr);
                tr.AddNewlyCreatedDBObject(btr, true);
            }
            var ids = new ObjectIdCollection(selSet.GetObjectIds());
            var mapping = new IdMapping();
            db.DeepCloneObjects(ids, btrId, mapping, false);
            var xform = Matrix3d.Displacement(basePoint.GetAsVector().Negate());
            foreach (IdPair pair in mapping)
            {
                if (pair.IsPrimary &amp;amp;&amp;amp; pair.IsCloned)
                {
                    var ent = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
                    ent.TransformBy(xform);
                    if (convertToBlock)
                    {
                        var source = (Entity)tr.GetObject(pair.Key, OpenMode.ForWrite);
                        source.Erase();
                    }
                }
            }
            if (convertToBlock)
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                var br = new BlockReference(basePoint, btrId);
                curSpace.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
            }

            return btrId;
        }

        private static Layout ClearOrCreateLayout(Database db, string layoutName)
        {
            var tr = db.TransactionManager.TopTransaction;
            var layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
            Layout layout;
            if (layoutDict.Contains(layoutName))
            {
                layout = (Layout)tr.GetObject(layoutDict.GetAt(layoutName), OpenMode.ForWrite);
            }
            else
            {
                var layoutId = LayoutManager.Current.CreateLayout(layoutName);
                layout = (Layout)tr.GetObject(layoutId, OpenMode.ForWrite);
            }
            LayoutManager.Current.CurrentLayout = layoutName;
            var btr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead);
            foreach (ObjectId id in btr)
            {
                var ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                ent.Erase();
            }
            return layout;
        }

        private static int MultiInsertInLayout(ObjectId btrId, Layout layout)
        {
            var db = btrId.Database;
            var tr = db.TransactionManager.TopTransaction;

            // get the block definition extents and center
            var extents = new Extents3d();
            var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
            foreach (ObjectId id in btr)
            {
                var ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                var bounds = ent.Bounds;
                if (bounds.HasValue)
                    extents.AddExtents(bounds.Value);
            }
            var blkWidth = extents.MaxPoint.X - extents.MinPoint.X;
            var blkHeight = extents.MaxPoint.Y - extents.MinPoint.Y;
            var blkCenter = new Point3d(
                (extents.MaxPoint.X + extents.MinPoint.X) / 2.0,
                (extents.MaxPoint.Y + extents.MinPoint.Y) / 2.0,
                0.0);

            // get the plot area extents
            var plotMargins = layout.PlotPaperMargins;
            double plotWidth = 0.0, plotHeight = 0.0;
            switch (layout.PlotRotation)
            {
                case PlotRotation.Degrees000:
                case PlotRotation.Degrees180:
                    plotWidth = layout.PlotPaperSize.X - plotMargins.MinPoint.X - plotMargins.MaxPoint.X;
                    plotHeight = layout.PlotPaperSize.Y - plotMargins.MinPoint.Y - plotMargins.MaxPoint.Y;
                    break;
                case PlotRotation.Degrees090:
                case PlotRotation.Degrees270:
                    plotWidth = layout.PlotPaperSize.Y - plotMargins.MinPoint.Y - plotMargins.MaxPoint.Y;
                    plotHeight = layout.PlotPaperSize.X - plotMargins.MinPoint.X - plotMargins.MaxPoint.X;
                    break;
            }

            // compute the scale so that the block can be inserted at least 4 times
            double scale = 1.0;
            while (plotWidth &amp;lt; 2.0 * scale * blkWidth || plotHeight &amp;lt; 2.0 * scale * blkHeight)
                scale /= 2.0;
            blkWidth *= scale;
            blkHeight *= scale;
            int columns = (int)(plotWidth / blkWidth);
            int rows = (int)(plotHeight / blkHeight);
            double colWidth = plotWidth / columns;
            double rowHeight = plotHeight / rows;

            // compute the displacement vector from block center to 'cell' center 
            var disp =
                new Vector3d(colWidth, rowHeight, 0.0) / 2.0 - blkCenter.GetAsVector() * scale;
                //new Vector3d(extents.MinPoint.X + extents.MaxPoint.X, extents.MinPoint.Y + extents.MaxPoint.Y, 0.0) * scale / 2.0;

            // insert the blocks
            var ps = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);
            for (int i = 0; i &amp;lt; rows; i++)
            {
                for (int j = 0; j &amp;lt; columns; j++)
                {
                    Point3d insPt = new Point3d(layout.PlotOrigin.X + j * colWidth, layout.PlotOrigin.Y + i * rowHeight, 0.0);
                    var br = new BlockReference(insPt + disp, btr.ObjectId) { ScaleFactors = new Scale3d(scale) };
                    ps.AppendEntity(br);
                    tr.AddNewlyCreatedDBObject(br, true);
                }
            }
            return rows * columns;
        }
    }
}
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2017 09:18:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6779072#M33344</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-01-03T09:18:33Z</dc:date>
    </item>
    <item>
      <title>Re : Add Entities in Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6794375#M33345</link>
      <description>&lt;P&gt;thank u so much.....&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2017 06:46:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-block-of-entities-and-blocks-are-not-getting-added-into/m-p/6794375#M33345</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-01-10T06:46:29Z</dc:date>
    </item>
  </channel>
</rss>

