Inserting blocks

Inserting blocks

Anonymous
Not applicable
839 Views
2 Replies
Message 1 of 3

Inserting blocks

Anonymous
Not applicable
So i have a block made up of a circle and 2 attributes. When i am in acad and use the insert command it inserts just fine, everything is there.
I have a program that searches the current drawing to see if this block exists in the blocktable, and if it does i insert it into the drawing but only the circle show up, not the attributes. I say that i "insert" this block from code but im not sure that's accurate. I append it to the blocktablerecord and then use the addnewlycreatedDBObject, true command.
When i do this with a block made of anything else, like lines, text, shapes, etc it inserts just fine. It is only the atttibutes that dont show up.
Do i need to define the attributes before "inserting" it or something? The attributes are already part of the block that is already in the drawing. I just cannot figure out how to get this block with its attributes, that already exists in the current drawing to insert.

Thanks for any assistance,
James
0 Likes
Accepted solutions (1)
840 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You have to explicitly add AttributeReference instances to the newly created BlockReference.

 

        public void InsertBlock(BlockTableRecord owner, Point3d position, BlockTableRecord btr, Transaction tr)
        {
            var br = new BlockReference(position, btr.ObjectId);
            owner.AppendEntity(br);
            tr.AddNewlyCreatedDBObject(br, true);
            if (btr.HasAttributeDefinitions)
            {
                foreach (ObjectId id in btr)
                {
                    if (id.ObjectClass.Name == "AcDbAttributeDefinition")
                    {
                        var attDef = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
                        if (!attDef.Constant)
                        {
                            var attRef = new AttributeReference();
                            attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
                            attRef.TextString = attDef.TextString; // <- set the default attribute value
                            br.AttributeCollection.AppendAttribute(attRef);
                            tr.AddNewlyCreatedDBObject(attRef, true);
                        }
                    }
                }
            }
        }

you can also see this topic:

http://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-2107599E-9405-4D8B-A6DD-83D603B41568



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Anonymous
Not applicable
Thank you yet again sir!
0 Likes