<?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 Hatch cannot be updated automatically after dynamic block attributes are modified in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/hatch-cannot-be-updated-automatically-after-dynamic-block/m-p/11806241#M9674</link>
    <description>&lt;P&gt;The version I am using is CAD2020. I encountered the same problem in CAD2018 and CAD2019.&lt;BR /&gt;Scenario: I use the code to copy the block file I need from my block storage drawing to my target drawing project, and then modify the dynamic length and width attributes after inserting. The problem is that the in-block filling cannot be updated automatically. You need to manually open the block file, and then click to save the changes when closing. All blocks will be updated automatically.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="zzx2_0-1678255647582.jpeg" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1185942i0C17F5C766E1AA86/image-size/medium?v=v2&amp;amp;px=400" role="button" title="zzx2_0-1678255647582.jpeg" alt="zzx2_0-1678255647582.jpeg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="zzx2_1-1678255653887.jpeg" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1185943i20FEC5A4D2DD6C77/image-size/medium?v=v2&amp;amp;px=400" role="button" title="zzx2_1-1678255653887.jpeg" alt="zzx2_1-1678255653887.jpeg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="zzx2_2-1678255666115.jpeg" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1185944iF5A7EACB182204CD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="zzx2_2-1678255666115.jpeg" alt="zzx2_2-1678255666115.jpeg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    public class TestCmd
    {
        [CommandMethod("TestCmd")]
        public void Test()
        {
            try
            {
                HatchInDynamicBlock();
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message + ex.StackTrace);
            }
        }

        public void HatchInDynamicBlock()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            ObjectId result = ObjectId.Null;
            Point3d originPoint = new Point3d(0, 0, 0);
            double resultWidth = 1.1;
            double resultHeight = 2.2;
            string blockName = "块状测试图案";
            string blockDWGPath = @"C:\Users\Acer\Desktop\块测试图纸路径\块图纸.dwg";
            ObjectId blockId = ObjectId.Null;
            #region 拿到块的id
            //开启事务处理
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    if (!bt.Has(blockName))
                    {
                        using (Database tempDB = new Database(false, false))
                        {
                            tempDB.ReadDwgFile(blockDWGPath, FileOpenMode.OpenForReadAndReadShare, true, null);//读取外部DWG到临时数据库
                            tempDB.CloseInput(true);
                            ObjectIdCollection ids = new ObjectIdCollection();//存放块OjbectId的集合
                            using (Transaction trans2 = tempDB.TransactionManager.StartTransaction())
                            {
                                BlockTable bt2 = trans2.GetObject(tempDB.BlockTableId, OpenMode.ForRead) as BlockTable;
                                ObjectId targetID = bt2[blockName];
                                ids.Add(targetID);
                            }
                            ////复制临时数据库中的块到当前数据库的块表中
                            tempDB.WblockCloneObjects(ids, db.BlockTableId, new IdMapping(), DuplicateRecordCloning.Ignore, false);
                        }
                    }
                    blockId = bt[blockName];
                }
                catch (System.Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
                trans.Commit();
            }
            #endregion

            #region 插入块
            using(Transaction createTrans = doc.TransactionManager.StartTransaction())
            {
                //插入图块
                BlockTable bt = createTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord curBtr = createTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                //Trace.WriteLine(blockId.ToString());
                db.TransactionManager.QueueForGraphicsFlush();
                using (BlockReference blockRef = new BlockReference(originPoint, blockId))
                {
                    db.Regenmode = true;
                    result = curBtr.AppendEntity(blockRef);
                    createTrans.AddNewlyCreatedDBObject(blockRef, true);
                }
                createTrans.Commit();
            }
            #endregion

            #region 修改动态快属性
            using (Transaction trans2 = doc.TransactionManager.StartTransaction())
            {
                try
                {
                    doc.TransactionManager.QueueForGraphicsFlush();
                    BlockReference blockRef = trans2.GetObject(result, OpenMode.ForWrite, true, true) as BlockReference;
                    BlockTableRecord btr = trans2.GetObject(blockRef.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
                    var blockDynamicProperty = blockRef.DynamicBlockReferencePropertyCollection;
                    List&amp;lt;DynamicBlockReferenceProperty&amp;gt; blockPropertyList = blockDynamicProperty.OfType&amp;lt;DynamicBlockReferenceProperty&amp;gt;().ToList();
                    blockPropertyList.FirstOrDefault(x =&amp;gt; x.PropertyName.Equals("长度")).Value = resultHeight;
                    blockPropertyList.FirstOrDefault(x =&amp;gt; x.PropertyName.Equals("宽度")).Value = resultWidth;
                    btr.UpdateAnonymousBlocks();
                }
                catch (System.Exception ex)
                {

                    Trace.WriteLine(ex.Message);
                }
                trans2.Commit();
            }
            #endregion

        }
    }&lt;/LI-CODE&gt;&lt;P&gt;Where Path is replaced by the downloaded block drawing file path。&lt;/P&gt;&lt;P&gt;I also provide the block drawing file I use to test, and I will occasionally encounter this kind of thing, especially when it comes to batch generation, I hope I can get your help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Mar 2023 06:10:43 GMT</pubDate>
    <dc:creator>zzx2</dc:creator>
    <dc:date>2023-03-08T06:10:43Z</dc:date>
    <item>
      <title>Hatch cannot be updated automatically after dynamic block attributes are modified</title>
      <link>https://forums.autodesk.com/t5/net-forum/hatch-cannot-be-updated-automatically-after-dynamic-block/m-p/11806241#M9674</link>
      <description>&lt;P&gt;The version I am using is CAD2020. I encountered the same problem in CAD2018 and CAD2019.&lt;BR /&gt;Scenario: I use the code to copy the block file I need from my block storage drawing to my target drawing project, and then modify the dynamic length and width attributes after inserting. The problem is that the in-block filling cannot be updated automatically. You need to manually open the block file, and then click to save the changes when closing. All blocks will be updated automatically.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="zzx2_0-1678255647582.jpeg" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1185942i0C17F5C766E1AA86/image-size/medium?v=v2&amp;amp;px=400" role="button" title="zzx2_0-1678255647582.jpeg" alt="zzx2_0-1678255647582.jpeg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="zzx2_1-1678255653887.jpeg" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1185943i20FEC5A4D2DD6C77/image-size/medium?v=v2&amp;amp;px=400" role="button" title="zzx2_1-1678255653887.jpeg" alt="zzx2_1-1678255653887.jpeg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="zzx2_2-1678255666115.jpeg" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1185944iF5A7EACB182204CD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="zzx2_2-1678255666115.jpeg" alt="zzx2_2-1678255666115.jpeg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    public class TestCmd
    {
        [CommandMethod("TestCmd")]
        public void Test()
        {
            try
            {
                HatchInDynamicBlock();
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message + ex.StackTrace);
            }
        }

        public void HatchInDynamicBlock()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            ObjectId result = ObjectId.Null;
            Point3d originPoint = new Point3d(0, 0, 0);
            double resultWidth = 1.1;
            double resultHeight = 2.2;
            string blockName = "块状测试图案";
            string blockDWGPath = @"C:\Users\Acer\Desktop\块测试图纸路径\块图纸.dwg";
            ObjectId blockId = ObjectId.Null;
            #region 拿到块的id
            //开启事务处理
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    if (!bt.Has(blockName))
                    {
                        using (Database tempDB = new Database(false, false))
                        {
                            tempDB.ReadDwgFile(blockDWGPath, FileOpenMode.OpenForReadAndReadShare, true, null);//读取外部DWG到临时数据库
                            tempDB.CloseInput(true);
                            ObjectIdCollection ids = new ObjectIdCollection();//存放块OjbectId的集合
                            using (Transaction trans2 = tempDB.TransactionManager.StartTransaction())
                            {
                                BlockTable bt2 = trans2.GetObject(tempDB.BlockTableId, OpenMode.ForRead) as BlockTable;
                                ObjectId targetID = bt2[blockName];
                                ids.Add(targetID);
                            }
                            ////复制临时数据库中的块到当前数据库的块表中
                            tempDB.WblockCloneObjects(ids, db.BlockTableId, new IdMapping(), DuplicateRecordCloning.Ignore, false);
                        }
                    }
                    blockId = bt[blockName];
                }
                catch (System.Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
                trans.Commit();
            }
            #endregion

            #region 插入块
            using(Transaction createTrans = doc.TransactionManager.StartTransaction())
            {
                //插入图块
                BlockTable bt = createTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord curBtr = createTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                //Trace.WriteLine(blockId.ToString());
                db.TransactionManager.QueueForGraphicsFlush();
                using (BlockReference blockRef = new BlockReference(originPoint, blockId))
                {
                    db.Regenmode = true;
                    result = curBtr.AppendEntity(blockRef);
                    createTrans.AddNewlyCreatedDBObject(blockRef, true);
                }
                createTrans.Commit();
            }
            #endregion

            #region 修改动态快属性
            using (Transaction trans2 = doc.TransactionManager.StartTransaction())
            {
                try
                {
                    doc.TransactionManager.QueueForGraphicsFlush();
                    BlockReference blockRef = trans2.GetObject(result, OpenMode.ForWrite, true, true) as BlockReference;
                    BlockTableRecord btr = trans2.GetObject(blockRef.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
                    var blockDynamicProperty = blockRef.DynamicBlockReferencePropertyCollection;
                    List&amp;lt;DynamicBlockReferenceProperty&amp;gt; blockPropertyList = blockDynamicProperty.OfType&amp;lt;DynamicBlockReferenceProperty&amp;gt;().ToList();
                    blockPropertyList.FirstOrDefault(x =&amp;gt; x.PropertyName.Equals("长度")).Value = resultHeight;
                    blockPropertyList.FirstOrDefault(x =&amp;gt; x.PropertyName.Equals("宽度")).Value = resultWidth;
                    btr.UpdateAnonymousBlocks();
                }
                catch (System.Exception ex)
                {

                    Trace.WriteLine(ex.Message);
                }
                trans2.Commit();
            }
            #endregion

        }
    }&lt;/LI-CODE&gt;&lt;P&gt;Where Path is replaced by the downloaded block drawing file path。&lt;/P&gt;&lt;P&gt;I also provide the block drawing file I use to test, and I will occasionally encounter this kind of thing, especially when it comes to batch generation, I hope I can get your help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Mar 2023 06:10:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/hatch-cannot-be-updated-automatically-after-dynamic-block/m-p/11806241#M9674</guid>
      <dc:creator>zzx2</dc:creator>
      <dc:date>2023-03-08T06:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: Hatch cannot be updated automatically after dynamic block attributes are modified</title>
      <link>https://forums.autodesk.com/t5/net-forum/hatch-cannot-be-updated-automatically-after-dynamic-block/m-p/11813190#M9675</link>
      <description>&lt;P&gt;Your code could use some cleaning up, but the main problem is that you need to update the properties of the block reference, not the block table record. See &lt;A href="https://forums.autodesk.com/t5/net/need-help-with-dynamic-block-reference-property/m-p/11801453#M76137" target="_blank" rel="noopener"&gt;this post&lt;/A&gt; for a sample. Note the section below. It uses a method called SetDynProps to set the properties of the BlockReference. Note that you don't need multiple transactions. You do it in the same transaction, where you already have a reference to the BlockReference.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt; using (var tr = db.TransactionManager.StartTransaction())
            {
                // insert the block reference and set its dynamic properties
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                var br = new BlockReference(Point3d.Origin, btrId);
                curSpace.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
                SetDynProps(br, dynPropValues);

                // create the solid 3d
                CreateSweptSolid(ed, startPoint, endPoint, tr, curSpace, br);
                tr.Commit();
            }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2023 16:58:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/hatch-cannot-be-updated-automatically-after-dynamic-block/m-p/11813190#M9675</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2023-03-10T16:58:12Z</dc:date>
    </item>
  </channel>
</rss>

