.NET

Reply
Mentor
GVDB
Posts: 215
Registered: ‎01-16-2006
Message 1 of 11 (248 Views)

Plot dwg with centered attributes - problem

248 Views, 10 Replies
01-16-2012 06:46 AM

Hi,

 

My routine inserts in modelspace different blocks with attributes in it with justification "Center".

After the blocks are inserted and the attributes are filled, it all looks good in Autocad.

Then I want to plot the DWG.

The problem is that the centered attributes in the plot are then justified to "Left" ????

 

When I run my routine the second time in the same drawing then it goes well.

 

Why is that? Am I missing something?

It looks like the attributes or blockreference isn't updated well in the database after insert?

 

Thanks, 

Geert

 

Code:

      public static BlockReference InsertBlockReference(Database database, string filename, string blockname, Point3d

                                                                                         insertionpoint, Scale3d scalefactor)

         {

             using (DocumentLock documentLock = Active.Document.LockDocument())

             {

                 using (Transaction transaction = database.TransactionManager.StartTransaction())

                 {

                     ObjectId newBlockID = new ObjectId();

                     BlockTable blockTable = (BlockTable) database.BlockTableId.GetObject(OpenMode.ForRead);                     //Modelspace

                     using (BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject

                                                                                                 (database.CurrentSpaceId, OpenMode.ForWrite))

                     {

                        if (blockTable.Has(blockname))

                         {

                             newBlockID = blockTable[blockname];

                         }

                         else

                         {

                             newBlockID = GetBlockIDFromReadDwgFile(database, filename, blockname);

                         }

                        BlockReference newBlockReference = new BlockReference(insertionpoint, newBlockID);

                         newBlockReference.ScaleFactors = scalefactor;

                         blockTableRecord.AppendEntity(newBlockReference);

                         transaction.AddNewlyCreatedDBObject(newBlockReference, true);

                        //Attributes in Block

                         BlockTableRecord newBlockTableRecord = GetBlockTableRecord(newBlockID, blockname);                         if (newBlockTableRecord.HasAttributeDefinitions)

                         {

                             AddAttributesToNewBlockReference(newBlockTableRecord, newBlockReference);

                         }

                         transaction.Commit();

                         return newBlockReference;

                     }

                 }

             }

         }

 

       private static void AddAttributesToNewBlockReference(BlockTableRecord blocktablerecord, BlockReference

                                                                                                           blockreference)

         {

             using (Transaction transaction = blockreference.BlockId.Database.TransactionManager.StartTransaction())

             {

                 foreach (ObjectId objectId in blocktablerecord)

                 {

                     Entity entity = (Entity) transaction.GetObject(objectId,OpenMode.ForRead);

                     if (entity is AttributeDefinition)

                     {

                         AttributeDefinition newAttributeDefinition = (AttributeDefinition)entity;

                         AttributeReference newAttributeReference = new AttributeReference();

                         newAttributeReference.SetAttributeFromBlock(newAttributeDefinition, blockreference.BlockTransform);                         ObjectId attributeReferenceID = blockreference.AttributeCollection.AppendAttribute

                                                                                                                                                  (newAttributeReference);

                         transaction.AddNewlyCreatedDBObject(newAttributeReference, true);

                     }

                 }

                 transaction.Commit();

             }

         }

 

       public static void UpdateAttributeValues(BlockReference blockreference, Dictionary<string, string> dictionary)

         {

             using (Transaction transaction = blockreference.BlockId.Database.TransactionManager.StartTransaction())

             {

                 AttributeCollection attributeCollection = blockreference.AttributeCollection;

                 foreach (ObjectId attributeId in attributeCollection)

                 {

                     AttributeReference attributeReference = (AttributeReference)attributeId.GetObject(OpenMode.ForWrite);

                     if (dictionary.ContainsKey(attributeReference.Tag))

                     {

                         attributeReference.TextString = dictionary[attributeReference.Tag];

                     }

                 }

                 transaction.Commit();

             }

         }

 

*Expert Elite*
Alfred.NESWADBA
Posts: 8,893
Registered: ‎06-29-2007
Message 2 of 11 (233 Views)

Re: Plot dwg with centered attributes - problem

01-16-2012 01:04 PM in reply to: GVDB

Hi,

 

try the .AdjustAlignment option after adding an AttributeReference.

 

From the object browser:

Public Overridable Sub AdjustAlignment(alternateDatabaseToUse As Autodesk.AutoCAD.DatabaseServices.Database)

 

Good luck, - alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Mentor
GVDB
Posts: 215
Registered: ‎01-16-2006
Message 3 of 11 (222 Views)

Re: Plot dwg with centered attributes - problem

01-16-2012 11:58 PM in reply to: GVDB

Hi Alfred,

 

thanks for your reply.

I add -> newAttributeReference.AdjustAlignment(blockreference.BlockId.Database); to the code with no result.

 

When I check the newAttributeReference.Justify in my code by defining them its "BaseCenter" which it has to be, so that's good.

 

I discovered when I (in code)  insert the blocks with attributes and fill them and then I first save the drawing, with for example:

Active .WorkingDatabase.SaveAs(@"K:\" + articleNumber + ".dwg", DwgVersion.AC1021);

If I then plot the drawing it seems to be good.

But I don't want/needs to save the drawing. I just want to open the drawing, insert some blocks, fill the attributes, plot it and then close the drawing.

 

I must be something missing in saving / writing in a database of something, so the attributes doesn't have their right justification?????

 

regards,

 

Geert





Mentor
SENL1362
Posts: 150
Registered: ‎07-20-2011
Message 4 of 11 (214 Views)

Re: Plot dwg with centered attributes - problem

01-17-2012 02:43 AM in reply to: GVDB

The same happended to me last week for centered text entities if the textstyle is changed via program.

Both old and new text style used the same shx file and thus the text itself looks the same. But the location of the centered text changed. Redraw solved this issue for me and because it was a one time happening i didn't looked any further.

 

So probably any update function need to be called after changing the attributes.

Maybe any of the following may be helpful

 

    attRef.AdjustAlignment(db);

    blr.RecordGraphicsModified(true);


    doc.TransactionManager.QueueForGraphicsFlush();
    ed.Regen();
    Application.Update;
    Application.UpdateScreen();
    pvp.UpdateDisplay();
    db.UpdateExt(true);


 

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 5 of 11 (208 Views)

Re: Plot dwg with centered attributes - problem

01-17-2012 03:04 AM in reply to: GVDB

If attributes are MText attributes then use method AttributeReference.UpdateMTextAttribute

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Mentor
GVDB
Posts: 215
Registered: ‎01-16-2006
Message 6 of 11 (197 Views)

Re: Plot dwg with centered attributes - problem

01-17-2012 07:07 AM in reply to: SENL1362

Thanks for your reply!

 

All your suggestions tried with no result, except ->  pvp.UpdateDisplay();

I can't find a way to trigger this.

What's pvp? and how do I get this?

 

Geert

 

 

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 7 of 11 (195 Views)

Re: Plot dwg with centered attributes - problem

01-17-2012 07:22 AM in reply to: GVDB

As I remember for proper alignment you need temporary switch working database to database in which you add blockreference (and attributes):

Database wdb = HostApplicationServices.WorkingDatabase;
HostApplicationServices.WorkingDatabase = blockreference.BlockId.Database;
ar.AdjustAlignment(blockreference.BlockId.Database);
HostApplicationServices.WorkingDatabase = wdb;

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Mentor
SENL1362
Posts: 150
Registered: ‎07-20-2011
Message 8 of 11 (181 Views)

Re: Plot dwg with centered attributes - problem

01-17-2012 12:17 PM in reply to: GVDB

pvp is a Paperspace Viewport.

Viewport pvp;

    DBDictionary layoutDic = trx.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
        foreach (DBDictionaryEntry entry in layoutDic)
        {
                Layout lay = trx.GetObject(entry.Value, OpenMode.ForRead) as Layout;
        if (!lay.ModelType)...
                BlockTableRecord tbr = (BlockTableRecord)trx.GetObject(lay.BlockTableRecordId, OpenMode.ForRead);
        ObjectIdCollection vpIds = lay.GetViewports();

        Viewport pvp = tr.GetObject(vpIds[1], OpenMode.ForWrite) as Viewport;


Mentor
GVDB
Posts: 215
Registered: ‎01-16-2006
Message 9 of 11 (172 Views)

Re: Plot dwg with centered attributes - problem

01-17-2012 11:39 PM in reply to: SENL1362

Nope, this isn't the solution either.

Also Alexander thanks for your reply but that's not working either.

 

For now it leaves me nothing else then to SaveAs the drawing before I plot.

It's a pity because that takes time.

 

other suggestions?

 

Geert

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 10 of 11 (161 Views)

Re: Plot dwg with centered attributes - problem

01-18-2012 07:00 AM in reply to: GVDB

GVDB wrote:
For now it leaves me nothing else then to SaveAs the drawing before I plot.

Post resulting code after all modification.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community