Author Topic: Edit AttributeReference alignment changing  (Read 1430 times)

0 Members and 1 Guest are viewing this topic.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Edit AttributeReference alignment changing
« on: January 06, 2012, 08:35:56 AM »
Synopsis:
Code: [Select]
db.ReadDwgFile(filename, FileShare.ReadWrite, false, null);
using (Transaction tr = db.TransactionManager.StartTransaction());
{
  ...
  foreach (ObjectId oid in bref.AttributeCollection)
  {
    AttributeReference aref = tr.GetObject(oid, OpenMode.ForWrite) as AttributeReference;
    aref.TextString = newText;

   aref.AdjustAlignment(db);  <--- Added this line from post found on The Swamp but still not working
  }
  tr.Commit();
}
db.SaveAs(filename, flase, DwgVersion.Newest, null);

Even after adding AdjustAlignment() all attributes that are not left aligned are showing up left aligned even though their alignment is middle/center. If I open the attribute up in the attribute editor it will snap back to middle/center.  There has got to be an easy fix to this.
Revit 2019, AMEP 2019 64bit Win 10

kaefer

  • Guest
Re: Edit AttributeReference alignment changing
« Reply #1 on: January 06, 2012, 09:05:50 AM »
Code: [Select]
   ...
   aref.AdjustAlignment(db);  <--- Added this line from post found on The Swamp but still not working
   ...
There has got to be an easy fix to this.

You hit a snag. A documented one, for a change. From arxmgd.chm, DBText.AdjustAlignment Method:
Quote
When this method is called, the database used must also be the current working database (as returned by HostApplicationServices()->workingDatabase()), the text entity must have non-NULL text string data and a valid text style objectId that resides in the database being used.

The most elegant solution I've seen so far involves a class taking the side database as constructor argument, saving the current working database to a private field; and which implements IDisposable to revert to the original current working database; see here.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Edit AttributeReference alignment changing
« Reply #2 on: January 06, 2012, 09:07:43 AM »
Ok, as usual after posting I come up with new search words and find an answer.

Seems that AdjustAlignment() only works if the database its in is the Working Database. So adding,
Code: [Select]
db.ReadDwgFile(filename, FileShare.ReadWrite, false, null);
using (Transaction tr = db.TransactionManager.StartTransaction());
{
  Database oldDB = HostApplicationServices.WorkingDatabase;
  HostApplicationServices.WorkingDatabase = db;
  ...
  foreach (ObjectId oid in bref.AttributeCollection)
  {
    AttributeReference aref = tr.GetObject(oid, OpenMode.ForWrite) as AttributeReference;
    aref.TextString = newText;

   aref.AdjustAlignment(db);  <--- Added this line from post found on The Swamp but still not working
  }
  tr.Commit();

  HostApplicationServices.WorkingDatabase = oldDB;
}
db.SaveAs(filename, flase, DwgVersion.Newest, null);


Fixes the problem.  I continue to be confused by this API. As I type this I see kaefer has responded. Thanks kaefer!
Revit 2019, AMEP 2019 64bit Win 10