[Transfer] CAD secondary development–attribute block Block and BlockReference

1. Definition of attribute block

Attribute blocks are composed of constituted entities and additional information (attributes). The definition of blocks in attribute blocks is the same as the definition of blocks in simple blocks, and the definition of attributes is mainly realized through the relevant attributes and functions of the attribute’s AttributeDefinition class. . Specific implementations include:

a. An instance of the AttributeDefinition class and set the attribute value of the object;

b. Since the attribute definition of a block can also be regarded as an entity in the block, the attribute definition can be attached to the block through the member function AppendEntity of the block table record class.

Among them, the attribute values defined by attributes mainly include:

The insertion point, height, rotation angle, alignment and width of the text;

The default value of the attribute;

Attribute mode, such as invisible mode, Invisible mode, constant mode, Constant mode, verification mode, Verify mode, and preset mode, Preset;

Attribute tag name.

/// <summary>
         /// Block add attributes
         /// </summary>
         /// <param name="blockId"></param>
         /// <param name="atts"></param>
         public static void AddAttsToBlocks(this ObjectId blockId, List<AttributeDefinition> atts)
         {
             Database db = blockId.Database;//Get the database object
 
             BlockTableRecord btr = blockId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
 
             foreach (AttributeDefinition att in atts)
             {
                 btr.AppendEntity(att);
 
                 db.TransactionManager.AddNewlyCreatedDBObject(att, true);
             }
 
             btr.DowngradeOpen();
         }
 
         public static void AddAttsToBlocks(this ObjectId blockId, params AttributeDefinition[] atts)
         {
             blockId.AddAttsToBlocks(atts.ToList());
         }

Property block definition

2. Insert attribute block

The attribute entity in the block reference is represented by the AttibuteReference class in the DatabaseServices namespace, which is actually a single-line text object derived from the DBText class. To add text to a block reference, follow these steps:

1) Open the block table record object to which the block reference belongs;

2) Loop through the entities in the block table record. If the entity is an object defined by an attribute, set the attribute value for the block reference attribute object according to its identification;

3) Get the attribute collection object of the block reference object to add the newly created attribute reference object to the block reference. The attribute collection object is identified by the AttributeCollection attribute of BlockReference, and calling its AppendAttribute function can complete the attribute addition of the block reference.

Insert block reference with attributes

/// <summary>
         /// Insert reference blocks with attributes
         /// </summary>
         /// <param name="spaceId">Space ID</param>
         /// <param name="layer">The name of the layer to be added to the block</param>
         /// <param name="blockName">The block name to which the fast reference belongs</param>
         /// <param name="postion">Insertion point</param>
         /// <param name="scale">Scale ratio</param>
         /// <param name="rotateAngle">Rotation angle</param>
         /// <param name="attNameValues">Attribute names and values</param>
         /// <returns></returns>
         public static ObjectId InsertBlockrefence(this ObjectId spaceId, string layer, string blockName, Point3d postion, Scale3d scale, double rotateAngle, Dictionary<string, string> attNameValues)
         {
             // Get the database object
             Database db = spaceId.Database;
             //Open the block table for reading
             BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
             //If there is no block with blockName d, the program returns
             if (!bt.Has(blockName))
 
                 return ObjectId.Null;//If there is no block of blockName, the program returns
             //Open space in write mode
             BlockTableRecord space = (BlockTableRecord)spaceId.GetObject(OpenMode.ForWrite);
             //Get the record ID of the block table
             ObjectId btrId = bt[blockName];
             //Open block table record
             BlockTableRecord record = btrId.GetObject(OpenMode.ForRead) as BlockTableRecord;
             //Create a quick reference and set the insertion point
             BlockReference br = new BlockReference(postion, bt[blockName]);
 
             br.ScaleFactors = scale;
 
             br.Layer = layer;
             br.Rotation = rotateAngle;
 
             space.AppendEntity(br);
             //Determine whether the block table record contains attribute definitions
             if (record.HasAttributeDefinitions)
             {
                 //If included, traverse the attribute definition
                 foreach (ObjectId id in record)
                 {
                     //Check if it is a property definition
                     AttributeDefinition attDef = id.GetObject(OpenMode.ForRead) as AttributeDefinition;
 
                     if (attDef != null)
                     {
 
                         //Create a new property object
                         AttributeReference attribute = new AttributeReference();
                         //Get the object properties of the attribute object from the attribute definition
                         attribute.SetAttributeFromBlock(attDef, br.BlockTransform);
                         attribute.Rotation = attDef.Rotation;
 
                         attribute.Position = attDef.Position.TransformBy(br.BlockTransform);
 
                         attribute.AdjustAlignment(db);
                         //Determine whether the specified attribute name is included
                         if (attNameValues.ContainsKey(attDef.Tag.ToUpper()))
                         {
                             //Set attribute value
                             attribute.TextString = attNameValues[attDef.Tag.ToUpper()].ToString();
 
                         }
                         //Add property object to block reference
                         br.AttributeCollection.AppendAttribute(attribute);
                         db.TransactionManager.AddNewlyCreatedDBObject(attribute, true);
 
                     }
                 }
             }
             db.TransactionManager.AddNewlyCreatedDBObject(br, true);
             return br.ObjectId;//Return the ID of the added quick reference
         }

Update block reference

/// <summary>
         /// Update attribute name and value
         /// </summary>
         /// <param name="blockRefId"></param>
         /// <param name="attNameValues"></param>
         public static void UpdateAttributesInBlock(this ObjectId blockRefId, Dictionary<string, string> attNameValues)
         {
             BlockReference blockRef = blockRefId.GetObject(OpenMode.ForRead) as BlockReference;
             if (blockRef != null)
             {
                 foreach (ObjectId id in blockRef.AttributeCollection)
                 {
                     AttributeReference attref = id.GetObject(OpenMode.ForRead) as AttributeReference;
                     if (attNameValues.ContainsKey(attref.Tag.ToUpper()))
                     {
                         attref.UpgradeOpen();
                         //Set attribute value
                         attref.TextString = attNameValues[attref.Tag.ToUpper()].ToString();
 
                         attref.DowngradeOpen();
                     }
                 }
 
             }
 
         }

Original address: https://www.bbsmax.com/A/Gkz1oZW25R/