Insert block with attribute

Insert block with attribute

bahman.jf.68
Advocate Advocate
524 Views
1 Reply
Message 1 of 2

Insert block with attribute

bahman.jf.68
Advocate
Advocate

hello everyone,

I wanted to insert a block which is exsiting on the drawing to a new point and updating attribitue defenetion.

 

BlockReference bref = new BlockReference(ptm, acBlkTbl["TestBlock"]);

                foreach (ObjectId attId in bref.AttributeCollection)
                {
                    AttributeReference ar =
                        acTrans.GetObject(attId, OpenMode.ForRead) as AttributeReference;

                    if (ar != null && ar.Tag.ToUpper() == "WTYPE")
                    {
                        ar.UpgradeOpen();
                        ar.TextString = "SR23";
                        ar.DowngradeOpen();
                    }
                }

                acBlkTblRec.AppendEntity(bref);
                acTrans.AddNewlyCreatedDBObject(bref, true);
                acTrans.Commit();

I didn't get any warning during the build, but does not appear any block at the point place.

0 Likes
525 Views
1 Reply
Reply (1)
Message 2 of 2

_gile
Consultant
Consultant

Hi,

 

When you create/insert a new block reference, you must explicitly Create the attribute references (AttributeReference) and add them to the block attribute collection (AttributeCollection). This is done by using the attribute definitions (AttributeDefinition) contained in the block definition (BlockTableRecord) as templates. See this topic of the docs.

// Add the block reference
BlockReference bref = new BlockReference(ptm, acBlkTbl["TestBlock"]);
acBlkTblRec.AppendEntity(bref);
acTrans.AddNewlyCreatedDBObject(bref, true);

// Add the attribute references
var blockDef = (BlockTableRecord)acTrans.GetObject(acBlkTbl["TestBlock"], OpenMode.ForRead);
foreach (ObjectId id in blockDef)
{
    if (id.ObjectClass == RXObject.GetClass(typeof(AttributeDefinition)))
    {
        var attDef = (AttributeDefinition)acTrans.GetObject(id, OpenMode.ForRead);
        if (!attDef.Constant)
        {
            var attRef = new AttributeReference();
            attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
            bref.AttributeCollection.AppendAttribute(attRef);
            acTrans.AddNewlyCreatedDBObject(attRef, true);

            if (attRef.Tag.ToUpper() == "WTYPE")
            {
                attRef.TextString = "SR23";
            }
        }
    }
}
acTrans.Commit();

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub