图块中可以添加一些属性以记录或者表现图块的参数属性(例如图框里面的标题等信息)这样既方便检索也方便显示,下面介绍图块中是如何加入属性的。 - using System;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- [assembly: CommandClass(typeof(Sample.AddAttributeBlock))]
- namespace Sample
- {
- class AddAttributeBlock
- {
- [CommandMethod("AddAttributeBlock")]
- public void AddBlock()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- ObjectId id;
- BlockTableRecord btr = new BlockTableRecord();
- Line line = new Line(new Point3d(), new Point3d(30, 30, 0));
- AttributeDefinition ad = AttributeDefinition("Length", "L", line.Length.ToString(), new Point3d(30, 30, 0));
- btr.Name = "LineBlock";
- btr.AppendEntity(line);
- btr.AppendEntity(ad);
- id = AddToBlockTable(btr);
- ToModelSpace(id, Point3d.Origin,db);
- }
- /// <summary>
- /// 创建属性
- /// </summary>
- /// <param name="Label">标记名</param>
- /// <param name="Prompt">提示</param>
- /// <param name="Value">属性值</param>
- /// <param name="pt">属性插入点位置</param>
- /// <returns></returns>
- public static AttributeDefinition AttributeDefinition(string Label, string Prompt, string Value, Point3d pt)
- {
- AttributeDefinition ad = new AttributeDefinition();
- ad.Constant = false;
- ad.Tag = Label;
- ad.Prompt = Prompt;
- ad.TextString = Value;
- ad.Position = pt;
- return ad;
- }
- /// <summary>
- /// 将指定的块定义变成块参照添加到指定模型空间
- /// </summary>
- /// <param name="blkid">块定义Id</param>
- /// <param name="pt">插入点</param>
- /// <param name="db">数据库</param>
- /// <returns></returns>
- public static ObjectId ToModelSpace(ObjectId blkid, Point3d pt, Database db)
- {
- ObjectId blkrfid = new ObjectId();
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- BlockTableRecord modelspace = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
- BlockTableRecord block = trans.GetObject(blkid, OpenMode.ForRead) as BlockTableRecord;
- BlockReference br = new BlockReference(pt, blkid); // 通过块定义添加块参照
- blkrfid = modelspace.AppendEntity(br); //把块参照添加到块表记录
- trans.AddNewlyCreatedDBObject(br, true); // 通过事务添加块参照到数据库
- foreach (ObjectId id in block)
- {
- if (id.ObjectClass.Equals(RXClass.GetClass(typeof(AttributeDefinition))))
- {
- AttributeDefinition ad = trans.GetObject(id, OpenMode.ForRead) as AttributeDefinition;
- AttributeReference ar = new AttributeReference(ad.Position, ad.TextString, ad.Tag, new ObjectId());
- br.AttributeCollection.AppendAttribute(ar);
- }
- }
- trans.Commit();
- }
- return blkrfid;
- }
- /// <summary>
- /// 将块表记录加入到块表中
- /// </summary>
- /// <returns></returns>
- public static ObjectId AddToBlockTable(BlockTableRecord Record)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- ObjectId id = new ObjectId();
- using (Transaction transaction = db.TransactionManager.StartTransaction())
- {
- BlockTable table = transaction.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
- id = table.Add(Record);
- transaction.AddNewlyCreatedDBObject(Record, true);
- transaction.Commit();
- }
- return id;
- }
- }
- }
复制代码
在AutoCAD中加载运行” AddAttributeBlock”命令后得到如下结果:
|