|
在AutoCAD2010程序界面画一条直线保存为Drawing.dwg,在C#中启动AutoCAD2010打开Drawing.dwg获取这条直线并给这条直线新增一个属性length。
执行下面代码不报异常,但在直线的特性中找不到Length属性。麻烦各位帮我看看是什么问题。
- class AutoClass
- {
- [CommandMethod("WriteData")]
- public void AddData()
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- PromptEntityResult ent = ed.GetEntity("\n选择要写数据的对象");
- if (ent.Status == PromptStatus.OK)
- {
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Entity entity = (Entity)tr.GetObject(ent.ObjectId, OpenMode.ForWrite, true);
- if (entity.GetType().Name.ToString() == "Line")
- {
- Line line = entity as Line;
- AttributeDefinition ad = AttributeDefinition("Length", "L", line.Length.ToString(), new Point3d(100, 100, 0));
- BlockTableRecord btr = new BlockTableRecord();
- btr.Name = "LineBlock2";
- btr.AppendEntity(entity);
- btr.AppendEntity(ad);
- ObjectId id;
- id = AddToBlockTable(btr);
- ToModelSpace(id, Point3d.Origin, db);
- tr.Commit();
- }
- }
- }
- }
- /// <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;
- }
- }
复制代码
|
|