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();
}
}
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 -
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
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);
If attributes are MText attributes then use method AttributeReference.UpdateMTextAttribute
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
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
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;
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
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;
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
@Anonymous wrote:
For now it leaves me nothing else then to SaveAs the drawing before I plot.
Post resulting code after all modification.
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Sorry I respond so late, had another project coming up what must be done.
My sourch isn't updated, because all suggestions didn't work!
But maybe it comes because I start with an empty drawing where I clone a drawing with the WBlockCloneObject command
I've another thread for that piece of code -> see http://forums.autodesk.com/t5/NET/WblockCloneObjects-Layout-of-Inventor-DWG/m-p/3299125
Maybe they are related to each other???
Geert