Hi Vince,
Try this code
{code}
[CommandMethod("AppendAttributeToBlock","appatt", CommandFlags.Session | CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
public static void AppendAttributeTest()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
try
{
using (doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord currSp = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
PromptNestedEntityOptions pno =
new PromptNestedEntityOptions("\nSelect source attribute to append new attribute below this one >>");
PromptNestedEntityResult nres =
ed.GetNestedEntity(pno);
if (nres.Status != PromptStatus.OK)
return;
ObjectId id = nres.ObjectId;
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
Point3d pnt = nres.PickedPoint;
ObjectId owId = ent.OwnerId;
AttributeReference attref = null;
if (id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeReference))))
{
attref = tr.GetObject(id,OpenMode.ForWrite) as AttributeReference;
}
BlockTableRecord btr = null;
BlockReference bref = null;
if (owId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(BlockReference))))
{
bref = tr.GetObject(owId,OpenMode.ForWrite) as BlockReference;
if (bref.IsDynamicBlock)
{
btr = tr.GetObject(bref.DynamicBlockTableRecord,OpenMode.ForRead) as BlockTableRecord;
}
else
{
btr = tr.GetObject(bref.BlockTableRecord,OpenMode.ForRead) as BlockTableRecord;
}
}
Point3d insPt = attref.Position.TransformBy(bref.BlockTransform);
btr.UpgradeOpen();
ObjectIdCollection bids = new ObjectIdCollection();
AttributeDefinition def = null;
foreach (ObjectId defid in btr)
{
if (defid.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeDefinition))))
{
def = tr.GetObject(defid, OpenMode.ForRead) as AttributeDefinition;
if (def.Tag == attref.Tag)
{
def.UpgradeOpen();
bids.Add(defid);
break;
}
}
}
IdMapping map = new IdMapping();
db.DeepCloneObjects(bids, btr.ObjectId, map,true);
ObjectIdCollection coll = new ObjectIdCollection();
AttributeDefinition attDef = null;
foreach (IdPair pair in map)
{
if (pair.IsPrimary)
{
Entity oent = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
if (oent != null)
{
if (pair.Value.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeDefinition))))
{
attDef = oent as AttributeDefinition;
attDef.UpgradeOpen();
attDef.SetPropertiesFrom(def as Entity);
// add other properties from source attribute definition to suit here:
attDef.Justify = def.Justify;
attDef.Position = btr.Origin.Add(
new Vector3d(attDef.Position.X, attDef.Position.Y - attDef.Height * 1.25, attDef.Position.Z)).TransformBy(Matrix3d.Identity
);
attDef.Tag = "NEW_TAG";
attDef.TextString = "New Prompt";
attDef.TextString = "New Textstring";
coll.Add(oent.ObjectId);
}
}
}
}
btr.AssumeOwnershipOf(coll);
btr.DowngradeOpen();
attDef.Dispose();//optional
bref.RecordGraphicsModified(true);
tr.TransactionManager.QueueForGraphicsFlush();
doc.TransactionManager.FlushGraphics();//optional
ed.UpdateScreen();
tr.Commit();
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
}
finally
{
acadApp.ShowAlertDialog("Call command \"ATTSYNC\" manually");
}
}
{code}
_____________________________________
C6309D9E0751D165D0934D0621DFF27919