Author Topic: attribute alignment problems after insert  (Read 2039 times)

0 Members and 1 Guest are viewing this topic.

TimBlanch

  • Guest
attribute alignment problems after insert
« on: June 06, 2012, 04:15:51 PM »
I am trying to insert new blocks in some drawings with new and improved attributes.
This is what it looks like before:

This is what it looks like after:


Here is the code that inserts the attributes:
Code: [Select]
if (BlkTblRec.HasAttributeDefinitions)
                        {
                            foreach (ObjectId objId in BlkTblRec)
                            {
                                AttributeDefinition AttDef = Trans.GetObject(objId, OpenMode.ForRead) as AttributeDefinition;
                                if (AttDef != null)
                                {
                                    AttributeReference AttRef = new AttributeReference();
                                    AttRef.SetAttributeFromBlock(AttDef, BlkRef.BlockTransform);
                                    BlkRef.AttributeCollection.AppendAttribute(AttRef);
                                    Trans.AddNewlyCreatedDBObject(AttRef, true);
                                }
                            }
                        }
                        Trans.Commit();

How can I tell it to center the attributes? They are middle bottom justified in the drawing when created.
The blocks are the same block, just renamed and a couple hidden attributes added.
I have a few other blocks doing the same thing.

Any help or comments would be appreciated, if you need more info just say so,

Thanks,
Tim

kaefer

  • Guest
Re: attribute alignment problems after insert
« Reply #1 on: June 06, 2012, 04:44:20 PM »
Code: [Select]
                                    AttributeReference AttRef = new AttributeReference();
                                    AttRef.SetAttributeFromBlock(AttDef, BlkRef.BlockTransform);
                                    BlkRef.AttributeCollection.AppendAttribute(AttRef);

You should throw in these two operations on the AttributeReference before assigning ownership to the AttributeCollection. The second will do.
Code: [Select]
                                    if (AttDef.IsMTextAttributeDefinition)
                                        AttRef.UpdateMTextAttribute();
                                    AttRef.AdjustAlignment(db);

TimBlanch

  • Guest
Re: attribute alignment problems after insert
« Reply #2 on: June 07, 2012, 12:18:41 PM »
Thanks for the reply, but I am still having no luck. I even added some other formatting that I found in other examples.
Here is the complete method I am using:

Code: [Select]
// Mostly from T.Willey @ the swamp
private static void InsertBlock(Database mainDb, string loName, string blkName, Point3d insPt, string layer,System.Collections.Generic.Dictionary<string, string> dicAttValues, double rotation = 0)
{

    using (Transaction Trans = mainDb.TransactionManager.StartTransaction())
    {
        DBDictionary LoDict = Trans.GetObject(mainDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
        foreach (DictionaryEntry de in LoDict)
        {
            if (string.Compare((string)de.Key, loName, true).Equals(0))
            {
                Layout Lo = Trans.GetObject((ObjectId)de.Value, OpenMode.ForWrite) as Layout;
                BlockTable BlkTbl = Trans.GetObject(mainDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord LoRec = Trans.GetObject(Lo.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord;
                ObjectId BlkTblRecId = GetNonErasedTableRecordId(BlkTbl.Id, blkName);
                if (BlkTblRecId.IsNull)
                {
                    string BlkPath = HostApplicationServices.Current.FindFile(blkName + ".dwg", mainDb, FindFileHint.Default);
                    if (string.IsNullOrEmpty(BlkPath))
                        return;
                    BlkTbl.UpgradeOpen();
                    using (Database tempDb = new Database(false, true))
                    {
                        tempDb.ReadDwgFile(BlkPath, FileShare.Read, true, null);
                        mainDb.Insert(blkName, tempDb, false);
                    }
                    BlkTblRecId = GetNonErasedTableRecordId(BlkTbl.Id, blkName);
                }

                LoRec.UpgradeOpen();
                BlockReference BlkRef = new BlockReference(insPt, BlkTblRecId);
                BlkRef.Rotation = rotation;
               
                LayerTable lt = (LayerTable)Trans.GetObject(mainDb.LayerTableId, OpenMode.ForRead);
                if (lt.Has(layer))
                {
                    ObjectId lid = lt[layer];
                    BlkRef.SetLayerId(lid, false);
                }

                LoRec.AppendEntity(BlkRef);
                Trans.AddNewlyCreatedDBObject(BlkRef, true);

                BlockTableRecord BlkTblRec = Trans.GetObject(BlkTblRecId, OpenMode.ForRead) as BlockTableRecord;

                if (BlkTblRec.HasAttributeDefinitions)
                {
                    foreach (ObjectId objId in BlkTblRec)
                    {
                        AttributeDefinition attDef = Trans.GetObject(objId, OpenMode.ForRead) as AttributeDefinition;
                        if (attDef != null)
                        {
                       
                            AttributeReference attRef = new AttributeReference();
                            attRef.SetPropertiesFrom(attDef);
                            attRef.Visible = attDef.Visible;
                            attRef.SetAttributeFromBlock(attDef, BlkRef.BlockTransform);
                            attRef.HorizontalMode = attDef.HorizontalMode;
                            attRef.VerticalMode = attDef.VerticalMode;
                            attRef.Rotation = attDef.Rotation;
                            attRef.TextStyleId = attDef.TextStyleId;
                            attRef.Position = attDef.Position + insPt.GetAsVector();
                            attRef.Tag = attDef.Tag;
                            attRef.FieldLength = attDef.FieldLength;
                            if (dicAttValues.ContainsKey(attDef.Tag))
                                attRef.TextString = dicAttValues[attDef.Tag].ToString();
                            else
                                attRef.TextString = attDef.TextString;
                            attRef.AdjustAlignment(mainDb);
                            BlkRef.AttributeCollection.AppendAttribute(attRef);
                            Trans.AddNewlyCreatedDBObject(attRef, true);
                        }
                    }
                }
                Trans.Commit();
            }
        }
    }
}

If I insert the block manually after running this it inserts just fine so I am pretty sure it's not the block.
This code came from here (The Swamp), I have tweeked it a little to suit my needs.
Can you see where I am going wrong?

Thanks,
Tim