[Solved] Attribute Definition vanishing after saving> closing> returning to block editor (C# .NET)

[Solved] Attribute Definition vanishing after saving> closing> returning to block editor (C# .NET)

caiodeodatomha
Participant Participant
685 Views
1 Reply
Message 1 of 2

[Solved] Attribute Definition vanishing after saving> closing> returning to block editor (C# .NET)

caiodeodatomha
Participant
Participant

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;
        }

 

 

 

0 Likes
Accepted solutions (1)
686 Views
1 Reply
Reply (1)
Message 2 of 2

caiodeodatomha
Participant
Participant
Accepted solution
I have figured out what is going on. The code is not wrong, but I think the problem is AutoCAD itself.

When running the program, the attribute definition will be created, but it is not shown in the block editor drawing because AutoCAD does not work with any event that can detect the creation of the new entity using some code, so this element is already in the Block Table Record "since the block editor was opened". So, to solve this problem, I must synchronize my new attribute definition and close the block editor discarding any modification (when I say "any", it is just any *user* modification, because the modification of your code will remain as long as it is synchronized). 

*PS: I assume that this problem occurs for any entity you try to insert into the block editor environment using code (I tried with circular entity and the same thing happens).

Any new solution is still valid.
0 Likes