|
当几个对象重叠的时候,图形的显示有一定的先后顺序,顺序在前面的显示在上层,本节介绍如何改变对象的显示次序。类似AutoCAD界面里面的"绘图次序"中的“前置”功能。- using System;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- [assembly: CommandClass(typeof(Sample.MoveTop))]
- namespace Sample
- {
- class MoveTop
- {
- [CommandMethod("Order")]
- public void ToTop()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Entity ent = SelectEntity("\n选择要前置的对象");
- MoveTop(ent, db);
- tr.Commit();
- }
- }
- /// <summary>
- /// 提示用户选择对象
- /// </summary>
- /// <param name="word"></param>
- /// <returns></returns>
- public Entity SelectEntity(string word)
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- Entity entity = null;
- PromptEntityResult ent = ed.GetEntity(word);
- if (ent.Status == PromptStatus.OK)
- {
- using (Transaction transaction = db.TransactionManager.StartTransaction())
- {
- entity = (Entity)transaction.GetObject(ent.ObjectId, OpenMode.ForWrite, true);
- transaction.Commit();
- }
- }
- return entity;
- }
- /// <summary>
- /// 改变对象的绘图次序到顶层
- /// </summary>
- /// <param name="ent"></param>
- /// <param name="db"></param>
- public static void MoveTop(Entity ent, Database db)
- {
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- ObjectIdCollection idc = new ObjectIdCollection();
- idc.Add(ent.ObjectId);
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
- DrawOrderTable orderTable = tr.GetObject(btr.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
- orderTable.MoveToTop(idc);
- tr.Commit();
- }
- }
- }
- }
复制代码
先画两个重叠的对象:
运行命令” Order”后选择要前置的对象得到如下效果:
|
|