- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to create a new attribute definition while the user is inside de block editor environment. My code can create de Attribute Definition, but have some problems:
1° Only appears on Attribute Manager (BATTMAN);
2° Your TAG don't display on informed position;
3° If the block editor was opened using a block refence and the block editor, after synchronizing and saving, the block reference will have the new attribute definition (without the value 'prompt'), but if the block editor be opened again, this attribute definition will vanish;
4° If the block editor was opened using BEDIT command, how like the 3° case, after synchronizing and saving, if i open again the block editor, the same thing will to occur.
Code:
private bool AddAttributeToBlock(string attributeName, string prompt)
{
using (Transaction transic = DocDataBase.TransactionManager.StartTransaction())
{
try
{
BlockTable blockTable = transic.GetObject(DocDataBase.BlockTableId, OpenMode.ForWrite) as BlockTable;
if (blockTable[Autodesk.AutoCAD.Internal.AcAeUtilities.GetBlockName()].GetObject(OpenMode.ForWrite) is BlockTableRecord bTR)
{
List<Point3d> points = new List<Point3d>();
double[] maxPs = new double[] { 0, 0, 0 };
bool abort = bTR.Cast<ObjectId>()
.Where(oId => oId.ObjectClass.Name == "AcDbAttributeDefinition")
.Any(oId =>
{
var aD = ((AttributeDefinition)oId.GetObject(OpenMode.ForRead));
if (maxPs[0] < aD.Position.X) maxPs[0] = aD.Position.X;
if (maxPs[1] < aD.Position.Y) maxPs[1] = aD.Position.Y;
if (maxPs[2] < aD.Position.Z) maxPs[2] = aD.Position.Z;
return aD.Tag == attributeName;
});
if (abort)
{
transic.Abort();
return false;
}
AttributeDefinition attDef = new AttributeDefinition(new Point3d(maxPs[0], maxPs[1] + 30, maxPs[2]), $"", attributeName, prompt, new ObjectId())
{
Height = 20,
Invisible = false,
LockPositionInBlock = true,
Annotative = AnnotativeStates.False,
};
bTR.AppendEntity(attDef);
transic.AddNewlyCreatedDBObject(attDef, true);
transic.Commit();
return true;
}
}
catch (System.Exception error)
{
MessageBox.Show(error.Message + "\n" + error.StackTrace);
transic.Abort();
return false;
}
}
return false;
}
Solved! Go to Solution.