<?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: Attribute Alignment in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182804#M58960</link>
    <description>&lt;P&gt;I'm pretty sure I see the problem now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You did not add an AttributeDefinition to the BlockTableRecord, you only added an AttributeReference to the BlockReference, therefore AdjustAlignment isn't really doing anything.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am almost done tweaking your code to do what needs done, but my Intellisense just went wonky on me, so I need to reboot.&amp;nbsp; I'll have something for you to try in a few minutes.&lt;/P&gt;</description>
    <pubDate>Thu, 06 Oct 2011 19:48:36 GMT</pubDate>
    <dc:creator>chiefbraincloud</dc:creator>
    <dc:date>2011-10-06T19:48:36Z</dc:date>
    <item>
      <title>Attribute Alignment</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182590#M58958</link>
      <description>&lt;P&gt;I want to add a block with a center aligned attribute to drawing file that is not open in the autocad editor; however, the attribute alignment doesn't display when i open the drawing. But when i open the attribute editor i can verify that the alignment is set properly and then after closing the attribute editor window﻿&amp;nbsp;the alignment actually displays correctly even though no changes were made.&lt;/P&gt;&lt;P&gt;I read a number of other posts with the same or similar issue, but i still have been unable to get mine to display right.﻿&lt;/P&gt;&lt;P&gt;My code is below. What do I need to do to force the attribute alignment to display correctly?&lt;/P&gt;&lt;PRE&gt;private void centerAttribute() {
  var path = @"c:\test.dwg";
  using (var database = new Database(false, true))
  {
    database.ReadDwgFile(path, FileShare.ReadWrite, false, password: "");
    Transaction transaction = null;
    try {
      transaction = database.TransactionManager.StartTransaction();
                    
      var previousDatabase = HostApplicationServices.WorkingDatabase;
      HostApplicationServices.WorkingDatabase = database;
                    
      // add block
      var block = new BlockTableRecord() { Name = "TestBlock3" };
      var blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForWrite) as BlockTable;
      var blockId = blockTable.Add(block);
      transaction.AddNewlyCreatedDBObject(block, true);

      // add block reference
      var blockReference = new BlockReference(Point3d.Origin, block.ObjectId);
      var modelSpace = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
      modelSpace.AppendEntity(blockReference);
      transaction.AddNewlyCreatedDBObject(blockReference, true);
                    
      // add attribute
      var center = new Point3d(1, 1, 0);
      var attribute = new AttributeReference(center, "Test", "TestAttribute", database.Textstyle);
      attribute.Justify = AttachmentPoint.BaseCenter;
      attribute.AlignmentPoint = center;
      attribute.AdjustAlignment(database);
      attribute.Position = center;

      blockReference.AttributeCollection.AppendAttribute(attribute);
      transaction.AddNewlyCreatedDBObject(attribute, true);

      database.SaveAs(path, DwgVersion.AC1021);
      transaction.Commit();

      HostApplicationServices.WorkingDatabase = previousDatabase;
    }
    catch (Exception exception) {
      transaction.Abort();
      System.Diagnostics.Debug.WriteLine(exception.Message);
    }
  }
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Oct 2011 17:16:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182590#M58958</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-10-06T17:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Alignment</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182688#M58959</link>
      <description>&lt;P&gt;At first glance, it appears that you have all the correct bits in your code to have it work right.&amp;nbsp; I have just recently created some code of my own that does something similar, and I also had to use AtRef.AdjustAlignment, and during that excercise I discovered that it would only work if you set the HostApplicationServices.WorkingDatabase to the Side Database.&amp;nbsp; That was all it took for me. (of course making sure to set the Working Database back to the previous Database)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I see all of that in your code, so I'm not sure.&amp;nbsp; I'll try and take a closer look.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Oct 2011 18:21:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182688#M58959</guid>
      <dc:creator>chiefbraincloud</dc:creator>
      <dc:date>2011-10-06T18:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Alignment</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182804#M58960</link>
      <description>&lt;P&gt;I'm pretty sure I see the problem now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You did not add an AttributeDefinition to the BlockTableRecord, you only added an AttributeReference to the BlockReference, therefore AdjustAlignment isn't really doing anything.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am almost done tweaking your code to do what needs done, but my Intellisense just went wonky on me, so I need to reboot.&amp;nbsp; I'll have something for you to try in a few minutes.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Oct 2011 19:48:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182804#M58960</guid>
      <dc:creator>chiefbraincloud</dc:creator>
      <dc:date>2011-10-06T19:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Alignment</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182842#M58961</link>
      <description>&lt;P&gt;Try this.&amp;nbsp; I have commented the changes, and one caveat near the end about the placement of the AdjustAlignment call.&amp;nbsp; The code I have that uses it on a side database like this is not creating the attributes, only modifying them, so I think your case may be different, and you may need to get the AttributeReference added to the BlockReference and the Transaction prior to calling AdjustAlignment.&amp;nbsp; In hindsight, I probably should have posted the code that way to start with.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;private void centerAttribute() {
  var path = @"c:\test.dwg";
  using (var database = new Database(false, true))
  {
    database.ReadDwgFile(path,  FileShare.ReadWrite, false, password: "");
    Transaction transaction = null;
    try {
      transaction = database.TransactionManager.StartTransaction();
                    
      var previousDatabase = HostApplicationServices.WorkingDatabase;
      HostApplicationServices.WorkingDatabase = database;
      var center = new Point3d(1, 1, 0);
      
      // add block
      var block = new BlockTableRecord() { Name = "TestBlock3" };
      var blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForWrite) as BlockTable;
      var blockId = blockTable.Add(block);
      transaction.AddNewlyCreatedDBObject(block, true);

      //add AttributeDefinition to BlockTableRecord
      var AtDef = new AttributeDefinition(center, "ThisIsTheDefaultValue", "TestAttribute", "Enter Value for TestAttribute:", database.Textstyle);
      block.AppendEntity(AtDef);
      transaction.AddNewlyCreatedDBObject(AtDef, true);

      // add block reference
      var blockReference = new BlockReference(Point3d.Origin, block.ObjectId);
      var modelSpace = transaction.GetObject(blockTable[BlockTableRecord.​ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
      modelSpace.AppendEntity(blockReference);
      transaction.AddNewlyCreatedDBObject(blockReference​, true);
                    
      // add attribute Reference to BlockReference 
      // this is slightly different from what you had
      var attribute = new AttributeReference();
      attribute.SetAttributeFromBlock(AtDef, blockReference.BlockTransform);

      attribute.Justify = AttachmentPoint.BaseCenter;
      attribute.AlignmentPoint = center;

        //I'm not sure, but you may need to move this AdjustAlignment call 
        //to after you have added the AttributeReference to the BlockReference and the Transaction
      attribute.AdjustAlignment(database);

        //if the attribute is not BaseLeft justified, you use the alignmentpoint property, not Position
        //and either way, I certainly would not set the position property after calling AdjustAlignment
      //attribute.Position = center;

      blockReference.AttributeCollection.AppendAttribute​(attribute);
      transaction.AddNewlyCreatedDBObject(attribute, true);
        //as noted, might need to put the adjust alignment call here
      database.SaveAs(path, DwgVersion.AC1021);
      transaction.Commit();

      HostApplicationServices.WorkingDatabase = previousDatabase;
    }
    catch (Exception ex) {
      transaction.Abort();
      System.Diagnostics.Debug.WriteLine(ex.Message );
    }
  }
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Oct 2011 20:23:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182842#M58961</guid>
      <dc:creator>chiefbraincloud</dc:creator>
      <dc:date>2011-10-06T20:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Alignment</title>
      <link>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182914#M58962</link>
      <description>&lt;P&gt;THANKS chiefbraincloud,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just tried the code how you had it and it appears that it's working. Thanks I've been trying to figure that out for a long time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Casey&lt;/P&gt;</description>
      <pubDate>Thu, 06 Oct 2011 21:17:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attribute-alignment/m-p/3182914#M58962</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-10-06T21:17:32Z</dc:date>
    </item>
  </channel>
</rss>

