<?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: 使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。 in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11969726#M8824</link>
    <description>&lt;BR /&gt;Hello, I have reviewed the post about updating attribute references but have not found a solution. Can you help me?</description>
    <pubDate>Wed, 17 May 2023 10:03:49 GMT</pubDate>
    <dc:creator>a932994040</dc:creator>
    <dc:date>2023-05-17T10:03:49Z</dc:date>
    <item>
      <title>使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。</title>
      <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11963756#M8818</link>
      <description>&lt;P&gt;想请教一下，我使用 ReadDwgFile 获取 Database ；&lt;/P&gt;&lt;P&gt;使用 Transaction 获取 Database 中的 DBText 与 AttributeReference；&lt;/P&gt;&lt;P&gt;对其属性 TextString 进行修改；&lt;/P&gt;&lt;P&gt;但只修改了文本值，对齐方式与宽度因子未随之改变；&lt;/P&gt;&lt;P&gt;尝试使用 AdjustAlignment，但并未解决问题。&lt;/P&gt;&lt;P&gt;using (var docLock = document.LockDocument())&lt;BR /&gt;{&lt;BR /&gt;using(var ndb = new Database(false, true))&lt;BR /&gt;{&lt;BR /&gt;ndb.ReadDwgFile("D:\\Projects\\Document3.dwg", FileShare.ReadWrite, true, "");&lt;/P&gt;&lt;P&gt;using (Transaction tr = ndb.TransactionManager.StartTransaction())&lt;BR /&gt;{&lt;BR /&gt;var bt = tr.GetObject(ndb.BlockTableId, OpenMode.ForRead) as BlockTable;&lt;BR /&gt;var btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;&lt;/P&gt;&lt;P&gt;foreach (ObjectId id in btr)&lt;BR /&gt;{&lt;BR /&gt;var dbo = tr.GetObject(id, OpenMode.ForWrite);&lt;/P&gt;&lt;P&gt;if (dbo.GetType() == typeof(DBText))&lt;BR /&gt;{&lt;BR /&gt;txt = dbo as DBText;&lt;BR /&gt;txt.TextString = "填充文本";&lt;BR /&gt;var originalDatabase = HostApplicationServices.WorkingDatabase;&lt;BR /&gt;HostApplicationServices.WorkingDatabase = ndb;&lt;BR /&gt;txt.AdjustAlignment(ndb);&lt;BR /&gt;HostApplicationServices.WorkingDatabase = originalDatabase;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;tr.Commit();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;ndb.SaveAs("D:\\Projects\\Document3.dwg", DwgVersion.Current);&lt;BR /&gt;ndb.Dispose();&lt;BR /&gt;}&lt;BR /&gt;}&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="微信图片_20230515154643.png" style="width: 609px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1214361iBC16F8A1B9BA4624/image-size/large?v=v2&amp;amp;px=999" role="button" title="微信图片_20230515154643.png" alt="微信图片_20230515154643.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="微信图片_20230515154726.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1214362iE86678BFB25BA6DF/image-size/large?v=v2&amp;amp;px=999" role="button" title="微信图片_20230515154726.png" alt="微信图片_20230515154726.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I borrowed from previous posts：&lt;/P&gt;&lt;P&gt;&lt;A href="https://forums.autodesk.com/t5/net/attrubute-text-alignment-problem-after-update/m-p/2624675#M17532" target="_blank" rel="noopener"&gt;https://forums.autodesk.com/t5/net/attrubute-text-alignment-problem-after-update/m-p/2624675#M17532&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://forums.autodesk.com/t5/net/wrong-justification-of-dbtext-in-block/m-p/9401872/highlight/false#M65199" target="_blank" rel="noopener"&gt;https://forums.autodesk.com/t5/net/wrong-justification-of-dbtext-in-block/m-p/9401872/highlight/false#M65199&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://forums.autodesk.com/t5/net/c-dbtext-alignment-error-when-openning-readdwgfile-method-have/m-p/6546086" target="_blank" rel="noopener"&gt;https://forums.autodesk.com/t5/net/c-dbtext-alignment-error-when-openning-readdwgfile-method-have/m-p/6546086&lt;/A&gt;&lt;/P&gt;&lt;P&gt;use code：&lt;/P&gt;&lt;P&gt;var originalDatabase = HostApplicationServices.WorkingDatabase;&lt;BR /&gt;HostApplicationServices.WorkingDatabase = ndb;&lt;BR /&gt;txt.AdjustAlignment(ndb);&lt;BR /&gt;HostApplicationServices.WorkingDatabase = originalDatabase;&lt;/P&gt;&lt;P&gt;no success&lt;/P&gt;&lt;P&gt;Must the DWG file be opened in a viewport to take effect?&lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2023 08:15:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11963756#M8818</guid>
      <dc:creator>a932994040</dc:creator>
      <dc:date>2023-05-15T08:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: 使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。</title>
      <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11964445#M8819</link>
      <description>&lt;P&gt;Well, I would try to set WorkingDatabase BEFORE starting the Transaction:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;using (ndb=new Database(false, true))
{
  ndb.ReadDwgFile(...);
  var originalDatabase=HostApplicationServices.WorkingDatabase;
  using (var tr = ndb.TransactionManager.StartTransaction()
  {
    ...
    tr.Commit();
  }
  HostApplicationServices.WorkingDatabase=originalDatabase
  ndb.SaveAs(...)
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2023 12:42:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11964445#M8819</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2023-05-15T12:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: 使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。</title>
      <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11966089#M8820</link>
      <description>&lt;P&gt;I followed your method to try to set WorkingDatabase BEFORE starting the Transaction&lt;/P&gt;&lt;P&gt;original document：&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="企业微信截图_16841977255099.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1214842iE82085A3EE9899DF/image-size/large?v=v2&amp;amp;px=999" role="button" title="企业微信截图_16841977255099.png" alt="企业微信截图_16841977255099.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;After program execution：&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="企业微信截图_1684197796122.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1214843i51875BEDBF1E296E/image-size/large?v=v2&amp;amp;px=999" role="button" title="企业微信截图_1684197796122.png" alt="企业微信截图_1684197796122.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Manual movement：&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="企业微信截图_1684197862947.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1214845i3718CAFE5812D7ED/image-size/large?v=v2&amp;amp;px=999" role="button" title="企业微信截图_1684197862947.png" alt="企业微信截图_1684197862947.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;using (var docLock = document.LockDocument())
{
	using(var ndb = new Database(false, true))
	{
		ndb.ReadDwgFile("D:\\Projects\\Document3.dwg", FileShare.ReadWrite, true, "");
		var originalDatabase = HostApplicationServices.WorkingDatabase;
		using (Transaction tr = ndb.TransactionManager.StartTransaction())
		{
			var bt = tr.GetObject(ndb.BlockTableId, OpenMode.ForRead) as BlockTable;
			var btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

			foreach (ObjectId id in btr)
			{
				var dbo = tr.GetObject(id, OpenMode.ForWrite);

				if (dbo.GetType() == typeof(DBText))
				{
					txt.SetDatabaseDefaults(ndb);
					txt = dbo as DBText;
					txt.TextString = "红旗H平台";
					
					HostApplicationServices.WorkingDatabase = ndb;
					txt.AdjustAlignment(ndb);
					
				}
			}
			tr.Commit();
		}
		HostApplicationServices.WorkingDatabase = originalDatabase;

		ndb.SaveAs("D:\\Projects\\Document3.dwg", DwgVersion.Current);
		ndb.Dispose();
	}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;div class="lia-vid-container video-embed-center"&gt;&lt;div id="lia-vid-6327558228112w864h540r483" class="lia-video-brightcove-player-container"&gt;&lt;video-js data-video-id="6327558228112" data-account="6057940548001" data-player="default" data-embed="default" class="vjs-fluid" controls="" data-application-id="" style="width: 100%; height: 100%;"&gt;&lt;/video-js&gt;&lt;/div&gt;&lt;script src="https://players.brightcove.net/6057940548001/default_default/index.min.js"&gt;&lt;/script&gt;&lt;script&gt;(function() {  var wrapper = document.getElementById('lia-vid-6327558228112w864h540r483');  var videoEl = wrapper ? wrapper.querySelector('video-js') : null;  if (videoEl) {     if (window.videojs) {       window.videojs(videoEl).ready(function() {         this.on('loadedmetadata', function() {           this.el().querySelectorAll('.vjs-load-progress div[data-start]').forEach(function(bar) {             bar.setAttribute('role', 'presentation');             bar.setAttribute('aria-hidden', 'true');           });         });       });     }  }})();&lt;/script&gt;&lt;a class="video-embed-link" href="https://forums.autodesk.com/t5/video/gallerypage/video-id/6327558228112"&gt;(view in My Videos)&lt;/a&gt;&lt;/div&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;</description>
      <pubDate>Tue, 16 May 2023 01:17:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11966089#M8820</guid>
      <dc:creator>a932994040</dc:creator>
      <dc:date>2023-05-16T01:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: 使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。</title>
      <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11966783#M8821</link>
      <description>&lt;P&gt;Hi, in my test, I found that if you set the command flag to&amp;nbsp;CommandFlags.Session, it works well. The sample code is as below:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[CommandMethod("test",CommandFlags.Session)]
        public void Test()
        {
            Database sourceDb = new Database(false, true);
            sourceDb.ReadDwgFile("Your dwg file path", System.IO.FileShare.ReadWrite, true, "");
            sourceDb.CloseInput(true);

            var originalDb = HostApplicationServices.WorkingDatabase;

            using (Transaction acTrans = sourceDb.TransactionManager.StartOpenCloseTransaction())
            {
                HostApplicationServices.WorkingDatabase = sourceDb;

                BlockTable acBlkTbl = acTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
                BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                
                foreach (ObjectId id in acBlkTblRec)
                {
                    DBObject obj = acTrans.GetObject(id, OpenMode.ForWrite);
                    if (obj.GetType() != typeof(DBText))
                        continue;
                    DBText txt = (DBText)obj;
                    txt.TextString = "Change text from dll";
                    txt.Height = 5000;
                    txt.WidthFactor = 5;
                    txt.Justify = AttachmentPoint.MiddleCenter;
                    txt.AlignmentPoint = txt.Position;
                }

                acTrans.Commit();
            }

            HostApplicationServices.WorkingDatabase = originalDb;

            sourceDb.SaveAs("Your dwg file path", DwgVersion.Current);
            sourceDb.Dispose();
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 16 May 2023 09:13:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11966783#M8821</guid>
      <dc:creator>Miralkong</dc:creator>
      <dc:date>2023-05-16T09:13:22Z</dc:date>
    </item>
    <item>
      <title>Re: 使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。</title>
      <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11968819#M8822</link>
      <description>Thanks! That was it.</description>
      <pubDate>Wed, 17 May 2023 00:35:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11968819#M8822</guid>
      <dc:creator>a932994040</dc:creator>
      <dc:date>2023-05-17T00:35:16Z</dc:date>
    </item>
    <item>
      <title>Re: 使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。</title>
      <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11968947#M8823</link>
      <description>&lt;P&gt;Hello, I want to replicate a BlockReference and modify the TextString attribute value of AttributeReference in the replicated BlockReference。I applied the same method to AttributeReference, but the position did not refresh。Could you please guide me again? Thank you very much！&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[CommandMethod("test", CommandFlags.Session)]
public void Test()
{
	Database sourceDb = new Database(false, true);
	sourceDb.ReadDwgFile("D:\\Projects\\Document2.dwg", System.IO.FileShare.ReadWrite, true, "");
	sourceDb.CloseInput(true);

	var originalDb = HostApplicationServices.WorkingDatabase;

	using (Transaction acTrans = sourceDb.TransactionManager.StartOpenCloseTransaction())
	{
		HostApplicationServices.WorkingDatabase = sourceDb;

		BlockReference block = null;

		BlockTable acBlkTbl = acTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
		BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

		foreach (ObjectId id in acBlkTblRec)
		{
			DBObject obj = acTrans.GetObject(id, OpenMode.ForWrite);
			if (obj.GetType() != typeof(BlockReference))
				continue;
			block = (BlockReference)obj;
		}

		var point = new Point3d(block.Position.X + 260, block.Position.Y, block.Position.Z);
		var mt = Matrix3d.Displacement(point - block.Position);
		var nbr = block.GetTransformedCopy(mt) as BlockReference;
		acBlkTblRec.AppendEntity(nbr);
		acTrans.AddNewlyCreatedDBObject(nbr, true);

		foreach (ObjectId id in nbr.AttributeCollection)
		{
			var nbr_ar = acTrans.GetObject(id, OpenMode.ForWrite) as AttributeReference;

			switch (nbr_ar.Tag)
			{
				case "厂项名称":
					nbr_ar.TextString = "红旗H平台";
					break;

				case "图纸名称":
					nbr_ar.TextString = "蜡烘干室";
					break;
				case "图纸编号":
					nbr_ar.TextString = "FH866";
					break;

				case "年":
					nbr_ar.TextString = "2023";
					break;

				case "月":
					nbr_ar.TextString = "5";
					break;
			}

			nbr_ar.AdjustAlignment(sourceDb);
		}

		acTrans.Commit();
	}

	HostApplicationServices.WorkingDatabase = originalDb;

	sourceDb.SaveAs("D:\\Projects\\Document2.dwg", DwgVersion.Current);
	sourceDb.Dispose();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&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="企业微信截图_1684289313190.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1215417i7CD936CF82B0DAA9/image-size/large?v=v2&amp;amp;px=999" role="button" title="企业微信截图_1684289313190.png" alt="企业微信截图_1684289313190.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;div class="lia-vid-container video-embed-center"&gt;&lt;div id="lia-vid-6327664905112w600h375r743" class="lia-video-brightcove-player-container"&gt;&lt;video-js data-video-id="6327664905112" data-account="6057940548001" data-player="default" data-embed="default" class="vjs-fluid" controls="" data-application-id="" style="width: 100%; height: 100%;"&gt;&lt;/video-js&gt;&lt;/div&gt;&lt;script src="https://players.brightcove.net/6057940548001/default_default/index.min.js"&gt;&lt;/script&gt;&lt;script&gt;(function() {  var wrapper = document.getElementById('lia-vid-6327664905112w600h375r743');  var videoEl = wrapper ? wrapper.querySelector('video-js') : null;  if (videoEl) {     if (window.videojs) {       window.videojs(videoEl).ready(function() {         this.on('loadedmetadata', function() {           this.el().querySelectorAll('.vjs-load-progress div[data-start]').forEach(function(bar) {             bar.setAttribute('role', 'presentation');             bar.setAttribute('aria-hidden', 'true');           });         });       });     }  }})();&lt;/script&gt;&lt;a class="video-embed-link" href="https://forums.autodesk.com/t5/video/gallerypage/video-id/6327664905112"&gt;(view in My Videos)&lt;/a&gt;&lt;/div&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 May 2023 08:42:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11968947#M8823</guid>
      <dc:creator>a932994040</dc:creator>
      <dc:date>2023-05-17T08:42:41Z</dc:date>
    </item>
    <item>
      <title>Re: 使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。</title>
      <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11969726#M8824</link>
      <description>&lt;BR /&gt;Hello, I have reviewed the post about updating attribute references but have not found a solution. Can you help me?</description>
      <pubDate>Wed, 17 May 2023 10:03:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11969726#M8824</guid>
      <dc:creator>a932994040</dc:creator>
      <dc:date>2023-05-17T10:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: 使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。</title>
      <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11971830#M8825</link>
      <description>I refer to other similar solutions, which are all copying the AttributeDefinition of the original BlockReference and recreating the BlockReference before adding a new AttributeDefinition;</description>
      <pubDate>Thu, 18 May 2023 03:34:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11971830#M8825</guid>
      <dc:creator>a932994040</dc:creator>
      <dc:date>2023-05-18T03:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: 使用 ReadDwgFile 获取 Database ，修改其中的DBText 与 AttributeReference 发生对齐及宽度因子不改变。</title>
      <link>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11971949#M8826</link>
      <description>&lt;P&gt;Hi. The reason why you did't get a correct result is that you should call "attsync" method after you change the contents of an attribute block. However, if you are working with a side database, you probably can't use Editor.Command("_attsync ", "_name ", "your block name"). Another way you could try is to create a new block reference other than copying one and editing its value. Another thing to say here, the method you used in your code to get the attribute block can only work on the circumstance that you only have one block in your drawing. You should use more general ways to do it.&amp;nbsp;Below is a sample that works on my computer.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[CommandMethod("test", CommandFlags.Session)]
        public void Test()
        {
            Database sourceDb = new Database(false, true);
            sourceDb.ReadDwgFile(@"C:\Users\MiralKong\Desktop\Document2.dwg", System.IO.FileShare.ReadWrite, true, "");
            sourceDb.CloseInput(true);

            using (Transaction acTrans = sourceDb.TransactionManager.StartOpenCloseTransaction())
            {
                string attBlkName = "汇总封面块";

                BlockTable acBlkTbl = acTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                
                Point3d insertPt = new Point3d();
                foreach (ObjectId id in acBlkTblRec)
                {
                    BlockReference origin = acTrans.GetObject(id, OpenMode.ForRead) as BlockReference;
                    if(origin!=null &amp;amp;&amp;amp; origin.Name == attBlkName)
                    {
                        insertPt = origin.Position;
                        break;
                    }
                }
                insertPt = new Point3d(insertPt.X + 260, insertPt.Y, insertPt.Z);                

                if (!acBlkTbl.Has(attBlkName))
                {
                    return;
                }
              
                BlockTableRecord attBlkTblRec = acTrans.GetObject(acBlkTbl[attBlkName], OpenMode.ForRead) as BlockTableRecord;

                using (BlockReference acBlkRef = new BlockReference(insertPt, attBlkTblRec.ObjectId))
                {
                    acBlkTblRec.AppendEntity(acBlkRef);
                    acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

                    foreach (ObjectId id in attBlkTblRec)
                    {                       
                        DBObject dBObject = acTrans.GetObject(id, OpenMode.ForWrite);
                        AttributeDefinition attDef = dBObject as AttributeDefinition;
                        
                        if (attDef != null &amp;amp;&amp;amp; !attDef.Constant)
                        {
                            using (AttributeReference attRef = new AttributeReference())
                            {
                                attRef.SetAttributeFromBlock(attDef, acBlkRef.BlockTransform);
                                switch (attDef.Tag)
                                {
                                    case "厂项名称":
                                        attDef.TextString = "红旗H平台";
                                        break;
                                    case "图纸名称":
                                        attDef.TextString = "蜡烘干室";
                                        break;
                                    case "图纸编号":
                                        attDef.TextString = "FH866";
                                        break;
                                    case "年":
                                        attDef.TextString = "2023";
                                        break;
                                    case "月":
                                        attDef.TextString = "5";
                                        break;
                                }
                                acBlkRef.AttributeCollection.AppendAttribute(attRef);
                                acTrans.AddNewlyCreatedDBObject(attRef, true);
                            }
                        }
                    }
                }
                acTrans.Commit();
            }

            sourceDb.SaveAs(@"C:\Users\MiralKong\Desktop\Document2.dwg", DwgVersion.Current);
            sourceDb.Dispose();
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 18 May 2023 05:42:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/shi-yong-readdwgfile-huo-qu-database-xiu-gai-qi-zhong-dedbtext-yu-attributereference/m-p/11971949#M8826</guid>
      <dc:creator>Miralkong</dc:creator>
      <dc:date>2023-05-18T05:42:05Z</dc:date>
    </item>
  </channel>
</rss>

