.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Attribute Alignment

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Anonymous
3191 Views, 4 Replies

Attribute Alignment

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 the alignment actually displays correctly even though no changes were made.

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.

My code is below. What do I need to do to force the attribute alignment to display correctly?

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);
    }
  }
}

 

Tags (1)
4 REPLIES 4
Message 2 of 5
chiefbraincloud
in reply to: Anonymous

At first glance, it appears that you have all the correct bits in your code to have it work right.  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.  That was all it took for me. (of course making sure to set the Working Database back to the previous Database)

 

I see all of that in your code, so I'm not sure.  I'll try and take a closer look.

Dave O.                                                                  Sig-Logos32.png
Message 3 of 5
chiefbraincloud
in reply to: Anonymous

I'm pretty sure I see the problem now.

 

You did not add an AttributeDefinition to the BlockTableRecord, you only added an AttributeReference to the BlockReference, therefore AdjustAlignment isn't really doing anything.

 

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.  I'll have something for you to try in a few minutes.

Dave O.                                                                  Sig-Logos32.png
Message 4 of 5
chiefbraincloud
in reply to: Anonymous

Try this.  I have commented the changes, and one caveat near the end about the placement of the AdjustAlignment call.  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.  In hindsight, I probably should have posted the code that way to start with.

 

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 );
    }
  }
}

 

Dave O.                                                                  Sig-Logos32.png
Message 5 of 5
Anonymous
in reply to: chiefbraincloud

THANKS chiefbraincloud,

 

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.

 

-Casey

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost