<?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: Dynamic Block Becomes Static After Insertion in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9388948#M20062</link>
    <description>&lt;P&gt;It mazes me when it works.&lt;/P&gt;&lt;P&gt;What exactly happening in the code?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;blkRefOld.BlockTableRecord = blkRefNew.DynamicBlockTableRecord;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 20 Mar 2020 07:34:38 GMT</pubDate>
    <dc:creator>ilovejingle</dc:creator>
    <dc:date>2020-03-20T07:34:38Z</dc:date>
    <item>
      <title>Dynamic Block Becomes Static After Insertion</title>
      <link>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9386510#M20058</link>
      <description>&lt;P&gt;I am trying to write a program to replace the old block with the new block. both blocks are dynamic. It seems the new blockreference becomes static after insertion&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any simpler way to insert a dynamic block which already exists in the drawing?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;        [CommandMethod("RP")]
        public void cmdRP()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var optEnt1 = new PromptEntityOptions("Please Select Entity to be replaced : ");
            var optEnt2 = new PromptEntityOptions("Please Select Enttity to Replace the orginal Entity : ");

            optEnt1.SetRejectMessage("Only Support Block Reference ");
            optEnt2.SetRejectMessage("Only Support Block Reference ");

            optEnt1.AddAllowedClass(typeof(BlockReference), true);
            optEnt2.AddAllowedClass(typeof(BlockReference), true);

            var blkOld = ed.GetEntity(optEnt1);
            if (blkOld.Status != PromptStatus.OK) return;
            var blkNew = ed.GetEntity(optEnt2);
            if (blkNew.Status != PromptStatus.OK) return;

            using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                try
                {
                    var blkRefOld = trans.GetObject(blkOld.ObjectId, OpenMode.ForRead) as BlockReference;
                    var blkRefNew = trans.GetObject(blkNew.ObjectId, OpenMode.ForRead) as BlockReference;

                    var blkMat = blkRefOld.BlockTransform;
                    var newBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRefNew.BlockTableRecord);

                    newBlkRef.SetDatabaseDefaults();
                    newBlkRef.TransformBy(blkMat);

                    var bt = trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
                    var ms = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                    ms.AppendEntity(newBlkRef);
                    

                    trans.AddNewlyCreatedDBObject(newBlkRef, true);

                    RXClass rxclass = RXObject.GetClass(typeof(AttributeDefinition));
                    var btr = trans.GetObject(blkRefOld.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                    var collection = newBlkRef.AttributeCollection;

                    if (btr.HasAttributeDefinitions)
                    {
                        foreach (ObjectId objId in btr)
                        {
                            if (objId.ObjectClass.IsDerivedFrom(rxclass))
                            {
                                var attDef = trans.GetObject(objId, OpenMode.ForRead) as AttributeDefinition;
                                if (!attDef.Constant)
                                {
                                    var attRef = new AttributeReference();
                                    attRef.SetAttributeFromBlock(attDef,blkMat);
                                    collection.AppendAttribute(attRef);
                                    trans.AddNewlyCreatedDBObject(attRef, true);
                                }
                            }
                        }
                    }

                    bgFunction.SyncDynamicBlockProps(blkRefOld.ObjectId, newBlkRef.ObjectId, trans);
                    blkRefOld.UpgradeOpen();
                    blkRefOld.Erase();

                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    ed.WriteMessage("Error : MAIN Method.");
                }
                finally
                {
                    trans.Commit();
                }
            }
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 19 Mar 2020 07:54:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9386510#M20058</guid>
      <dc:creator>ilovejingle</dc:creator>
      <dc:date>2020-03-19T07:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Block Becomes Static After Insertion</title>
      <link>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9387032#M20059</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What about this method?&lt;/P&gt;
&lt;PRE class="lia-code-sample  language-markup"&gt;&lt;CODE&gt;bgFunction.SyncDynamicBlockProps&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 12:21:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9387032#M20059</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-03-19T12:21:15Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Block Becomes Static After Insertion</title>
      <link>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9388654#M20060</link>
      <description>&lt;P&gt;This method has no relevance to this topic, it just try to copy all attributes value from the old block to the new block. I should remove it from my code before posting, sorry for that.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2020 01:08:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9388654#M20060</guid>
      <dc:creator>ilovejingle</dc:creator>
      <dc:date>2020-03-20T01:08:21Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Block Becomes Static After Insertion</title>
      <link>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9388918#M20061</link>
      <description>&lt;P&gt;May be you can start from this (not deeply tested)&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("RP")]
        public static void CmdRp()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var opts = new PromptEntityOptions("\nPlease Select Entity to be replaced : ");
            opts.SetRejectMessage("Only Support Block Reference ");
            opts.AddAllowedClass(typeof(BlockReference), true);

            var result = ed.GetEntity(opts);
            if (result.Status != PromptStatus.OK) return;
            var blkOldId = result.ObjectId;

            opts.Message = "\nPlease Select Entity to Replace the orginal Entity : ";
            result = ed.GetEntity(opts);
            if (result.Status != PromptStatus.OK) return;
            var blkNewId = result.ObjectId;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var blkRefOld = (BlockReference)tr.GetObject(blkOldId, OpenMode.ForWrite);
                var blkRefNew = (BlockReference)tr.GetObject(blkNewId, OpenMode.ForRead);
                blkRefOld.BlockTableRecord = blkRefNew.DynamicBlockTableRecord;

                foreach (ObjectId id in blkRefOld.AttributeCollection)
                {
                    var att = (AttributeReference)tr.GetObject(id, OpenMode.ForWrite);
                    att.Erase();
                }

                RXClass rxclass = RXObject.GetClass(typeof(AttributeDefinition));
                var btr = tr.GetObject(blkRefNew.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;

                if (btr.HasAttributeDefinitions)
                {
                    foreach (ObjectId objId in btr)
                    {
                        if (objId.ObjectClass.IsDerivedFrom(rxclass))
                        {
                            var attDef = tr.GetObject(objId, OpenMode.ForRead) as AttributeDefinition;
                            if (!attDef.Constant)
                            {
                                var attRef = new AttributeReference();
                                attRef.SetAttributeFromBlock(attDef, blkRefOld.BlockTransform);
                                blkRefOld.AttributeCollection.AppendAttribute(attRef);
                                tr.AddNewlyCreatedDBObject(attRef, true);
                            }
                        }
                    }
                }

                tr.Commit();
            }
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 20 Mar 2020 07:06:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9388918#M20061</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-03-20T07:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Block Becomes Static After Insertion</title>
      <link>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9388948#M20062</link>
      <description>&lt;P&gt;It mazes me when it works.&lt;/P&gt;&lt;P&gt;What exactly happening in the code?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;blkRefOld.BlockTableRecord = blkRefNew.DynamicBlockTableRecord;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2020 07:34:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9388948#M20062</guid>
      <dc:creator>ilovejingle</dc:creator>
      <dc:date>2020-03-20T07:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Block Becomes Static After Insertion</title>
      <link>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9389035#M20063</link>
      <description>&lt;P&gt;A BlockReference is only an inserted reference to a BlockTableRecord.&lt;/P&gt;
&lt;P&gt;You can simply change the BlockTableRecord the BlockReference references to.&lt;/P&gt;
&lt;P&gt;For dynamic blocks, you have to reference to the DynamicBlockTableRecord (the original definition).&lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2020 08:49:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/dynamic-block-becomes-static-after-insertion/m-p/9389035#M20063</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-03-20T08:49:48Z</dc:date>
    </item>
  </channel>
</rss>

