<?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: Inserting a dynamic block with c# in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/inserting-a-dynamic-block-with-c/m-p/9345299#M20212</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You did not show the JigBlockMove class and your code is not very robust.&lt;/P&gt;
&lt;P&gt;You could get some inspiration from this example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;        private static void InsertBlock(string blockPath, string blockName)
        {
            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);
                ObjectId btrId = bt.Has(blockName) ? 
                    bt[blockName] : 
                    ImportBlock(db, blockName, blockPath);
                if (btrId.IsNull)
                {
                    ed.WriteMessage($"\nBlock '{blockName}' not found.");
                    return;
                }
                using (var br = new BlockReference(Point3d.Origin, btrId))
                {
                    var jig = new InsertBlockJig(br);
                    var pr = ed.Drag(jig);
                    if (pr.Status == PromptStatus.OK)
                    {
                        var cSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        cSpace.AppendEntity(br);
                        tr.AddNewlyCreatedDBObject(br, true);
                    }
                }
                tr.Commit();
            }
        }

        private static ObjectId ImportBlock(Database destDb, string blockName, string sourceFileName)
        {
            if (System.IO.File.Exists(sourceFileName))
            {
                using (var sourceDb = new Database(false, true))
                {
                    try
                    {
                        // Read the DWG into a side database
                        sourceDb.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, true, "");

                        // Create a variable to store the block identifier
                        var id = ObjectId.Null;
                        using (var tr = new OpenCloseTransaction())
                        {
                            // Open the block table
                            var bt = (BlockTable)tr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);

                            // if the block table contains 'blockName', store it into the variable
                            if (bt.Has(blockName))
                                id = bt[blockName];
                        }
                        // if the variable is not null (i.e. the block was found)
                        if (!id.IsNull)
                        {
                            // Copy the block deinition from source to destination database
                            var blockIds = new ObjectIdCollection();
                            blockIds.Add(id);
                            var mapping = new IdMapping();
                            sourceDb.WblockCloneObjects(blockIds, destDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
                            // if the copy succeeded, return the ObjectId of the clone
                            if (mapping[id].IsCloned)
                                return mapping[id].Value;
                        }
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        var ed = AcApplication.DocumentManager.MdiActiveDocument.Editor;
                        ed.WriteMessage("\nError during copy: " + ex.Message + "\n" + ex.StackTrace);
                    }
                }
            }
            return ObjectId.Null;
        }

        class InsertBlockJig : EntityJig
        {
            BlockReference br;
            Point3d pt;

            public InsertBlockJig(BlockReference br) : base(br)
            {
                this.br = br;
            }

            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                var options = new JigPromptPointOptions("\nSpecify insertion point: ");
                options.UserInputControls = UserInputControls.Accept3dCoordinates;
                var result = prompts.AcquirePoint(options);
                if (result.Value.IsEqualTo(pt))
                    return SamplerStatus.NoChange;
                pt = result.Value;
                return SamplerStatus.OK;
            }

            protected override bool Update()
            {
                br.Position = pt;
                return true;
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Feb 2020 10:59:13 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2020-02-27T10:59:13Z</dc:date>
    <item>
      <title>Inserting a dynamic block with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-dynamic-block-with-c/m-p/9345135#M20211</link>
      <description>&lt;P&gt;Hi Guys!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This would be my first time writing in a forum.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I have been reading guides and copying codes to insert a block into current model space but unfortunately, none of it has worked. There is no error but the block was not inserted into the model space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you can direct me to any link that I can make as a reference or study, it would be a big help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are some of my codes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;private void InsertBlock(string blockPath, string blockName)&lt;BR /&gt;{&lt;BR /&gt;var dm = AcApplication.DocumentManager;&lt;BR /&gt;var doc = dm.MdiActiveDocument;&lt;BR /&gt;var ed = doc.Editor;&lt;BR /&gt;using (var lockDoc = doc.LockDocument())&lt;BR /&gt;{&lt;BR /&gt;Database db = AcApplication.DocumentManager.MdiActiveDocument.Database;&lt;BR /&gt;ImportBlock(blockPath, blockName);&lt;/P&gt;&lt;P&gt;using (Transaction trans = db.TransactionManager.StartTransaction())&lt;BR /&gt;{&lt;BR /&gt;BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);&lt;/P&gt;&lt;P&gt;var blockBtr = bt.Cast&amp;lt;ObjectId&amp;gt;().Select(s =&amp;gt; (BlockTableRecord)trans.GetObject(s, OpenMode.ForRead)).&lt;BR /&gt;Where(s =&amp;gt; s.IsDynamicBlock &amp;amp;&amp;amp; s.Name == blockName).FirstOrDefault();&lt;/P&gt;&lt;P&gt;BlockTableRecord blockDef = bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;&lt;BR /&gt;BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;&lt;BR /&gt;//ms = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;&lt;/P&gt;&lt;P&gt;var point = new Point3d(0, 0, 0);&lt;/P&gt;&lt;P&gt;BlockReference blockRef = new BlockReference(point, blockBtr.ObjectId);&lt;/P&gt;&lt;P&gt;JigBlockMove jig = new JigBlockMove(blockRef);&lt;BR /&gt;var pr = ed.Drag(jig);&lt;/P&gt;&lt;P&gt;var entity = jig.GetEntity();&lt;BR /&gt;ms.AppendEntity(entity);&lt;BR /&gt;trans.AddNewlyCreatedDBObject(entity, true);&lt;/P&gt;&lt;P&gt;trans.Commit();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void ImportBlock(string sourceFileName, string blockName)&lt;BR /&gt;{&lt;BR /&gt;if (!File.Exists(sourceFileName)) return;&lt;BR /&gt;DocumentCollection dm = AcApplication.DocumentManager;&lt;BR /&gt;var ed = dm.MdiActiveDocument.Editor;&lt;BR /&gt;Database destDb = dm.MdiActiveDocument.Database;&lt;BR /&gt;using (Database sourceDb = new Database(false, true))&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;// Read the DWG into a side database&lt;BR /&gt;sourceDb.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, true, "");&lt;/P&gt;&lt;P&gt;// Create a variable to store the list of block identifiers&lt;BR /&gt;ObjectIdCollection blockIds = new ObjectIdCollection();&lt;BR /&gt;Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = sourceDb.TransactionManager;&lt;/P&gt;&lt;P&gt;using (Transaction myT = tm.StartTransaction())&lt;BR /&gt;{&lt;BR /&gt;// Open the block table&lt;BR /&gt;BlockTable bt = (BlockTable)tm.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);&lt;/P&gt;&lt;P&gt;// if the block table contains 'blockName', add its ObjectId to the collection&lt;BR /&gt;if (bt.Has(blockName))&lt;BR /&gt;blockIds.Add(bt[blockName]);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Copy the block from source to destination database&lt;BR /&gt;if (blockIds.Count &amp;gt; 0)&lt;BR /&gt;{&lt;BR /&gt;IdMapping mapping = new IdMapping();&lt;BR /&gt;sourceDb.WblockCloneObjects(blockIds, destDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;//ed.WriteMessage("\nCopied 1 block definitions from "+ sourceFileName+ " to the current drawing.");&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;catch (Autodesk.AutoCAD.Runtime.Exception ex)&lt;BR /&gt;{&lt;BR /&gt;ed.WriteMessage("\nError during copy: " + ex.Message + "\n" + ex.StackTrace);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 09:52:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-dynamic-block-with-c/m-p/9345135#M20211</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-02-27T09:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a dynamic block with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-dynamic-block-with-c/m-p/9345299#M20212</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You did not show the JigBlockMove class and your code is not very robust.&lt;/P&gt;
&lt;P&gt;You could get some inspiration from this example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;        private static void InsertBlock(string blockPath, string blockName)
        {
            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);
                ObjectId btrId = bt.Has(blockName) ? 
                    bt[blockName] : 
                    ImportBlock(db, blockName, blockPath);
                if (btrId.IsNull)
                {
                    ed.WriteMessage($"\nBlock '{blockName}' not found.");
                    return;
                }
                using (var br = new BlockReference(Point3d.Origin, btrId))
                {
                    var jig = new InsertBlockJig(br);
                    var pr = ed.Drag(jig);
                    if (pr.Status == PromptStatus.OK)
                    {
                        var cSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        cSpace.AppendEntity(br);
                        tr.AddNewlyCreatedDBObject(br, true);
                    }
                }
                tr.Commit();
            }
        }

        private static ObjectId ImportBlock(Database destDb, string blockName, string sourceFileName)
        {
            if (System.IO.File.Exists(sourceFileName))
            {
                using (var sourceDb = new Database(false, true))
                {
                    try
                    {
                        // Read the DWG into a side database
                        sourceDb.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, true, "");

                        // Create a variable to store the block identifier
                        var id = ObjectId.Null;
                        using (var tr = new OpenCloseTransaction())
                        {
                            // Open the block table
                            var bt = (BlockTable)tr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);

                            // if the block table contains 'blockName', store it into the variable
                            if (bt.Has(blockName))
                                id = bt[blockName];
                        }
                        // if the variable is not null (i.e. the block was found)
                        if (!id.IsNull)
                        {
                            // Copy the block deinition from source to destination database
                            var blockIds = new ObjectIdCollection();
                            blockIds.Add(id);
                            var mapping = new IdMapping();
                            sourceDb.WblockCloneObjects(blockIds, destDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
                            // if the copy succeeded, return the ObjectId of the clone
                            if (mapping[id].IsCloned)
                                return mapping[id].Value;
                        }
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        var ed = AcApplication.DocumentManager.MdiActiveDocument.Editor;
                        ed.WriteMessage("\nError during copy: " + ex.Message + "\n" + ex.StackTrace);
                    }
                }
            }
            return ObjectId.Null;
        }

        class InsertBlockJig : EntityJig
        {
            BlockReference br;
            Point3d pt;

            public InsertBlockJig(BlockReference br) : base(br)
            {
                this.br = br;
            }

            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                var options = new JigPromptPointOptions("\nSpecify insertion point: ");
                options.UserInputControls = UserInputControls.Accept3dCoordinates;
                var result = prompts.AcquirePoint(options);
                if (result.Value.IsEqualTo(pt))
                    return SamplerStatus.NoChange;
                pt = result.Value;
                return SamplerStatus.OK;
            }

            protected override bool Update()
            {
                br.Position = pt;
                return true;
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 10:59:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-dynamic-block-with-c/m-p/9345299#M20212</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-02-27T10:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a dynamic block with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-dynamic-block-with-c/m-p/9347280#M20213</link>
      <description>&lt;P&gt;Hi Gilles!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your quick response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;JibBlockMove class was just basically from here&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.keanw.com/2015/05/jigging-an-autocad-block-with-attributes-using-net-redux.html" target="_blank"&gt;https://www.keanw.com/2015/05/jigging-an-autocad-block-with-attributes-using-net-redux.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I tested your code and I am having an 'eLockViolation' error so have to wrap your code with doc.LockDcument().&amp;nbsp;&lt;/P&gt;&lt;P&gt;Still, the block doesn't show on the model space. Am I missing something? I'm testing it in AutoCAD2019 by the way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;private void InsertBlock(string blockPath, string blockName)&lt;BR /&gt;{&lt;BR /&gt;var doc = AcApplication.DocumentManager.MdiActiveDocument;&lt;BR /&gt;var db = doc.Database;&lt;BR /&gt;var ed = doc.Editor;&lt;BR /&gt;using (var docLock = doc.LockDocument())&lt;BR /&gt;{&lt;BR /&gt;using (var tr = db.TransactionManager.StartTransaction())&lt;BR /&gt;{&lt;BR /&gt;var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);&lt;BR /&gt;ObjectId btrId = bt.Has(blockName) ? bt[blockName] : ImportBlock(db, blockName, blockPath);&lt;BR /&gt;if (btrId.IsNull)&lt;BR /&gt;{&lt;BR /&gt;ed.WriteMessage($"\nBlock '{blockName}' not found.");&lt;BR /&gt;return;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;using (var br = new BlockReference(Point3d.Origin, btrId))&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt;var jig = new JigBlockMove(br);&lt;BR /&gt;var pr = ed.Drag(jig);&lt;BR /&gt;if (pr.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)&lt;BR /&gt;{&lt;BR /&gt;var cSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);&lt;BR /&gt;cSpace.AppendEntity(br);&lt;BR /&gt;tr.AddNewlyCreatedDBObject(br, true);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;tr.Commit();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2020 01:37:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-dynamic-block-with-c/m-p/9347280#M20213</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-02-28T01:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a dynamic block with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-dynamic-block-with-c/m-p/9347612#M20214</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The 'eLockViolation' error is probably due to the way you call the method: from a modeless dialog or from a command decores with the CommandFlags.Session (do not use CommandFlags.Session if it's not absolutely required by your code).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The fact you do not see the block reference during the insertion (jig) may be due to ablock definition which only contains attribute definitions.&lt;/P&gt;
&lt;P&gt;When inserting attributed block references, you have to explicitely add the attribute references to the Attribute Collection of the block reference and if you use a Jig for insertion, you also have to manage the attribute references jigging.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

using System.Collections.Generic;

using AcApplication = Autodesk.AutoCAD.ApplicationServices.Core.Application;

[assembly: CommandClass(typeof(InsertAttributedBlockSample.Commands))]

namespace InsertAttributedBlockSample
{
    public class Commands
    {
        [CommandMethod("TEST")]
        public static void Test()
        {
            InsertBlock(@"B:\Desktop\Test.dwg", "bloc-att");
        }

        private static void InsertBlock(string blockPath, string blockName)
        {
            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);
                ObjectId btrId = bt.Has(blockName) ?
                    bt[blockName] :
                    ImportBlock(db, blockName, blockPath);
                if (btrId.IsNull)
                {
                    ed.WriteMessage($"\nBlock '{blockName}' not found.");
                    return;
                }

                var cSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                var br = new BlockReference(Point3d.Origin, btrId);
                cSpace.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);

                // add attribute references to the block reference
                var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                var attInfos = new Dictionary&amp;lt;string, TextInfo&amp;gt;();
                if (btr.HasAttributeDefinitions)
                {
                    foreach (ObjectId id in btr)
                    {
                        if (id.ObjectClass.DxfName == "ATTDEF")
                        {
                            var attDef = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
                            attInfos[attDef.Tag] = new TextInfo(
                                attDef.Position,
                                attDef.AlignmentPoint,
                                attDef.Justify != AttachmentPoint.BaseLeft,
                                attDef.Rotation);
                            var attRef = new AttributeReference();
                            attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
                            attRef.TextString = attDef.TextString;
                            br.AttributeCollection.AppendAttribute(attRef);
                            tr.AddNewlyCreatedDBObject(attRef, true);
                        }
                    }
                }
                var jig = new InsertBlockJig(br, attInfos);
                var pr = ed.Drag(jig);
                if (pr.Status != PromptStatus.OK)
                {
                    br.Erase();
                }
                tr.Commit();
            }
        }

        private static ObjectId ImportBlock(Database destDb, string blockName, string sourceFileName)
        {
            if (System.IO.File.Exists(sourceFileName))
            {
                using (var sourceDb = new Database(false, true))
                {
                    try
                    {
                        // Read the DWG into a side database
                        sourceDb.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, true, "");

                        // Create a variable to store the block identifier
                        var id = ObjectId.Null;
                        using (var tr = new OpenCloseTransaction())
                        {
                            // Open the block table
                            var bt = (BlockTable)tr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);

                            // if the block table contains 'blockName', store it into the variable
                            if (bt.Has(blockName))
                                id = bt[blockName];
                        }
                        // if the variable is not null (i.e. the block was found)
                        if (!id.IsNull)
                        {
                            // Copy the block deinition from source to destination database
                            var blockIds = new ObjectIdCollection();
                            blockIds.Add(id);
                            var mapping = new IdMapping();
                            sourceDb.WblockCloneObjects(blockIds, destDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
                            // if the copy succeeded, return the ObjectId of the clone
                            if (mapping[id].IsCloned)
                                return mapping[id].Value;
                        }
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        var ed = AcApplication.DocumentManager.MdiActiveDocument.Editor;
                        ed.WriteMessage("\nError during copy: " + ex.Message + "\n" + ex.StackTrace);
                    }
                }
            }
            return ObjectId.Null;
        }

        struct TextInfo
        {
            public Point3d Position { get; private set; }
            public Point3d Alignment { get; private set; }
            public bool IsAligned { get; private set; }
            public double Rotation { get; private set; }
            public TextInfo(Point3d position, Point3d alignment, bool aligned, double rotation)
            {
                Position = position;
                Alignment = alignment;
                IsAligned = aligned;
                Rotation = rotation;
            }
        }

        class InsertBlockJig : EntityJig
        {
            BlockReference br;
            Point3d pt;
            Dictionary&amp;lt;string, TextInfo&amp;gt; attInfos;

            public InsertBlockJig(BlockReference br, Dictionary&amp;lt;string, TextInfo&amp;gt; attInfos) : base(br)
            {
                this.br = br;
                this.attInfos = attInfos;
            }

            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                var options = new JigPromptPointOptions("\nSpecify insertion point: ");
                options.UserInputControls = UserInputControls.Accept3dCoordinates;
                var result = prompts.AcquirePoint(options);
                if (result.Value.IsEqualTo(pt))
                    return SamplerStatus.NoChange;
                pt = result.Value;
                return SamplerStatus.OK;
            }

            protected override bool Update()
            {
                br.Position = pt;
                if (br.AttributeCollection.Count &amp;gt; 0)
                {
                    var tr = br.Database.TransactionManager.TopTransaction;
                    foreach (ObjectId id in br.AttributeCollection)
                    {
                        var attRef = (AttributeReference)tr.GetObject(id, OpenMode.ForRead);
                        string tag = attRef.Tag.ToUpper();
                        if (attInfos.ContainsKey(tag))
                        {
                            TextInfo ti = attInfos[tag];
                            attRef.Position = ti.Position.TransformBy(br.BlockTransform);
                            if (ti.IsAligned)
                            {
                                attRef.AlignmentPoint =
                                    ti.Alignment.TransformBy(br.BlockTransform);
                                attRef.AdjustAlignment(br.Database);
                            }
                            if (attRef.IsMTextAttribute)
                            {
                                attRef.UpdateMTextAttribute();
                            }
                        }
                    }
                }
                return true;
            }
        }
    }
}
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2020 06:48:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-dynamic-block-with-c/m-p/9347612#M20214</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-02-28T06:48:12Z</dc:date>
    </item>
  </channel>
</rss>

