1 ///
2 /// 插入带属性的参照快
3 ///
4 /// 空间的ID
5 /// 块要加入的图层名
6 /// 快参照所属的快名
7 /// 插入点
8 /// 缩放比例
9 /// 旋转角度
10 /// 属性名称与取值
11 ///
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 }