1、属性块的定义
属性块是有构成的实体和附加信息(属性)组成的,属性块中块的定义与简单块中块的定义一样,而属性的定义主要是通过属性的AttributeDefinition类的有关属性和函数来实现的。具体实现有:
a 、AttributeDefinition类的实例并设置对象的属性值;
b、由于块的属性定义也可以看做是块中的实体,可以通过块表记录类的成员函数AppendEntity将属性定义附加到块中。
其中,属性定义的属性值主要有:
文字的插入点、高度、旋转角度、对齐方式和宽度;
属性的默认值;
属性的模式,如不可见方式Invisible、常量方式Constant、验证方式Verify、预置方式Preset;
属性标签名。
1 /// <summary> 2 /// 块添加属性 3 /// </summary> 4 /// <param name="blockId"></param> 5 /// <param name="atts"></param> 6 public static void AddAttsToBlocks(this ObjectId blockId, List<AttributeDefinition> atts) 7 { 8 Database db = blockId.Database;//获取数据库对象 9 10 BlockTableRecord btr = blockId.GetObject(OpenMode.ForWrite) as BlockTableRecord; 11 12 foreach (AttributeDefinition att in atts) 13 { 14 btr.AppendEntity(att); 15 16 db.TransactionManager.AddNewlyCreatedDBObject(att, true); 17 } 18 19 btr.DowngradeOpen(); 20 } 21 22 23 public static void AddAttsToBlocks(this ObjectId blockId, params AttributeDefinition[] atts) 24 { 25 blockId.AddAttsToBlocks(atts.ToList()); 26 }
2、插入属性块
块参照中的属性实体由DatabaseServices命名空间中的AttibuteReference类表示,它其实是一个单行文本对象,由DBText类派生。为块参照添加书体的步骤如下:
1)打开块参照所属的块表记录对象;
2)对块表记录中的实体进行循环遍历,如果实体是属性定义的对象,则根据它的标识为块参照属性对象设置属性值;
3)获取块参照对象的属性集合对象来为块参照添加新创建的属性参照对象。属性集合对象由BlockReference的AttributeCollection 属性标识,调用它的AppendAttribute函数就可以完成块参照的属性添加。
1 /// <summary> 2 /// 插入带属性的参照快 3 /// </summary> 4 /// <param name="spaceId">空间的ID</param> 5 /// <param name="layer">块要加入的图层名</param> 6 /// <param name="blockName">快参照所属的快名</param> 7 /// <param name="postion">插入点</param> 8 /// <param name="scale">缩放比例</param> 9 /// <param name="rotateAngle">旋转角度</param> 10 /// <param name="attNameValues">属性名称与取值</param> 11 /// <returns></returns> 12 public static ObjectId InsertBlockrefence(this ObjectId spaceId, string layer, string blockName, Point3d postion, Scale3d scale, double rotateAngle, Dictionary<string, string> attNameValues) 13 { 14 // 获取数据库对象 15 Database db = spaceId.Database; 16 //以读的方式打开块表 17 BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; 18 //如果没有blockName d的块,则程序返回 19 if (!bt.Has(blockName)) 20 21 return ObjectId.Null;//如果没有blockName的块,程序返回 22 //以写的模式打开空间 23 BlockTableRecord space = (BlockTableRecord)spaceId.GetObject(OpenMode.ForWrite); 24 //获取块表的记录ID 25 ObjectId btrId = bt[blockName]; 26 //打开块表记录 27 BlockTableRecord record = btrId.GetObject(OpenMode.ForRead) as BlockTableRecord; 28 //创建一个快参照并设置插入点 29 BlockReference br = new BlockReference(postion, bt[blockName]); 30 31 br.ScaleFactors = scale; 32 33 br.Layer = layer; 34 br.Rotation = rotateAngle; 35 36 space.AppendEntity(br); 37 //判断块表记录是否包含属性定义 38 if (record.HasAttributeDefinitions) 39 { 40 //若包含,则遍历属性定义 41 foreach (ObjectId id in record) 42 { 43 //检查是否是属性定义 44 AttributeDefinition attDef = id.GetObject(OpenMode.ForRead) as AttributeDefinition; 45 46 if (attDef != null) 47 { 48 49 //创建一个新的属性对象 50 AttributeReference attribute = new AttributeReference(); 51 //从属性定义获取属性对象的对象特性 52 attribute.SetAttributeFromBlock(attDef, br.BlockTransform); 53 attribute.Rotation = attDef.Rotation; 54 55 attribute.Position = attDef.Position.TransformBy(br.BlockTransform); 56 57 attribute.AdjustAlignment(db); 58 //判断是否包含指定的属性名称 59 if (attNameValues.ContainsKey(attDef.Tag.ToUpper())) 60 { 61 //设置属性值 62 attribute.TextString = attNameValues[attDef.Tag.ToUpper()].ToString(); 63 64 } 65 // 向块参照添加属性对象 66 br.AttributeCollection.AppendAttribute(attribute); 67 db.TransactionManager.AddNewlyCreatedDBObject(attribute, true); 68 69 } 70 } 71 } 72 db.TransactionManager.AddNewlyCreatedDBObject(br, true); 73 return br.ObjectId;//返回添加的快参照的ID 74 }
1 /// <summary> 2 /// 更新属性名称与取值 3 /// </summary> 4 /// <param name="blockRefId"></param> 5 /// <param name="attNameValues"></param> 6 public static void UpdateAttributesInBlock(this ObjectId blockRefId, Dictionary<string, string> attNameValues) 7 { 8 BlockReference blockRef = blockRefId.GetObject(OpenMode.ForRead) as BlockReference; 9 if (blockRef != null) 10 { 11 foreach (ObjectId id in blockRef.AttributeCollection) 12 { 13 AttributeReference attref = id.GetObject(OpenMode.ForRead) as AttributeReference; 14 if (attNameValues.ContainsKey(attref.Tag.ToUpper())) 15 { 16 attref.UpgradeOpen(); 17 //设置属性值 18 attref.TextString = attNameValues[attref.Tag.ToUpper()].ToString(); 19 20 attref.DowngradeOpen(); 21 } 22 } 23 24 } 25 26 }