返回块参照的属性名和属性值

    /// <returns>返回块参照的属性名和属性值</returns>
        public static SortedDictionary<string, string> GetAttributesInBlock(this Database db, string blockName)
        {
            SortedDictionary<string, string> attributes = new SortedDictionary<string, string>();
            // 筛选指定名称的块参照
            TypedValue[] values = { new TypedValue((int)DxfCode.Start, "INSERT"),
                                    new TypedValue((int)DxfCode.BlockName, blockName),
                                    };
            var filter = new SelectionFilter(values);
            Editor ed = Application.DocumentManager.GetDocument(db).Editor;
            var entSelected = ed.SelectAll(filter);
            // 如果数据库不存在指定名称的块参照,则返回
            if (entSelected.Status != PromptStatus.OK) return null;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 遍历块参照
                foreach (var id in entSelected.Value.GetObjectIds())
                {
                    BlockReference bref = (BlockReference)trans.GetObject(id, OpenMode.ForRead);
                    // 遍历块参照中的属性
                    foreach (ObjectId attId in bref.AttributeCollection)
                    {
                        AttributeReference attRef = (AttributeReference)trans.GetObject(attId, OpenMode.ForRead);
                        // 将块参照的属性名和属性值添加到字典中
                        attributes.Add(attRef.Tag, attRef.TextString);
                    }
                }
                trans.Commit();
            }
            return attributes; // 返回指定名称的块参照的属性名和属性值

  

原文地址:https://www.cnblogs.com/wwssgg/p/15498660.html