- 积分
- 8245
- 明经币
- 个
- 注册时间
- 2005-2-21
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2012-6-27 15:50:48
|
显示全部楼层
- /// <summary>
- /// 图层管理
- /// </summary>
- [CommandMethod("LayerManagement", "TT", CommandFlags.Modal)]
- public void LayerManagement()
- {
- string message = "\n图层管理:[关闭(C)/冻结(F)/锁定(S)/删除(D)/打开解冻解锁所有图层(A)]<默认C>";
- string key = this.Keywords(message, new string[] { "C", "F", "S", "D", "A" });
- if (!string.IsNullOrEmpty(key))
- {
- switch (key)
- {
- case "C"://关闭或打开图层
- this.CloseLayer();
- break;
- case "F"://冻结或解冻图层
- this.FrozenLayer();
- break;
- case "S"://锁定或解锁图层
- this.LockLayer();
- break;
- case "D"://删除与图层关联的对象
- this.DeleteLayer();
- break;
- case "A"://打开/解冻/解锁所有图层
- this.OpenUnFrozenUnlockLayer();
- break;
- default:
- break;
- }
- }
- }
- /// <summary>
- /// 图层命令调用
- /// </summary>
- /// <param name="message">提示字符串</param>
- /// <param name="layerstatus">图层状态</param>
- private void LayerCommandCalls(string message, LayerStatus layerstatus)
- {
- string key = this.Keywords(message, this.array);
- if (!string.IsNullOrEmpty(key))
- {
- switch (key)
- {
- case "S":
- LayerOperating(layerstatus, this.SelectEntityGetLayerName(), false, true);
- break;
- case "F":
- LayerOperating(layerstatus, this.SelectEntityGetLayerName(), true, true);
- break;
- case "R":
- LayerOperating(layerstatus, this.ImportLayerName(), false, true);
- break;
- case "T":
- LayerOperating(layerstatus, this.ImportLayerName(), false, false);
- Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("REGEN\r", true, false, false);
- break;
- case "A":
- foreach (string item in this.GetAllLayerName())
- {
- LayerOperating(layerstatus, item, false, false);
- }
- Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("REGEN\r", true, false, false);
- break;
- default:
- break;
- }
- }
- }
- /// <summary>
- /// 获取所有图层的名称
- /// </summary>
- /// <returns></returns>
- private List<string> GetAllLayerName()
- {
- List<string> LayerNames = new List<string>();
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
- foreach (ObjectId layerId in lt)
- {
- LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(layerId, OpenMode.ForRead);
- LayerNames.Add(ltr.Name);
- }
- trans.Commit();
- }
- return LayerNames;
- }
- /// <summary>
- /// 提示用户输入关键字
- /// </summary>
- /// <param name="message">提示字符串</param>
- /// <param name="keywords">关键字数组</param>
- /// <returns>用户输入的字符</returns>
- private string Keywords(string message, string[] keywords)
- {
- try
- {
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- PromptKeywordOptions pko = new PromptKeywordOptions(message);
- for (int i = 0; i < keywords.Length; i++)
- {
- pko.Keywords.Add(keywords, keywords, keywords, false, true);
- }
- pko.Keywords.Default = keywords[0];
- //pko.AllowNone = true;
- PromptResult k = ed.GetKeywords(pko);
- if (k.Status == PromptStatus.OK)
- {
- return k.StringResult;
- }
- else
- {
- return string.Empty;
- }
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 从图中选取实体对象,从中获得实体的图层名称
- /// </summary>
- /// <returns>图层名称</returns>
- private string SelectEntityGetLayerName()
- {
- string strLayerName = string.Empty;
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- try
- {
- using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
- {
- PromptSelectionOptions pso = new PromptSelectionOptions();
- pso.MessageForAdding = "\n请选择对象";
- PromptSelectionResult psr = ed.GetSelection(pso);
- if (psr.Status == PromptStatus.OK)
- {
- SelectionSet ss = psr.Value;
- ObjectId[] objectIds = ss.GetObjectIds();
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- // 遍历选择集.
- foreach (ObjectId objectId in objectIds)
- {
- Entity entity = (Entity)trans.GetObject(objectId, OpenMode.ForRead);
- strLayerName = entity.Layer;
- }
- trans.Commit();
- }
- }
- }
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- return strLayerName;
- }
- /// <summary>
- /// 获取输入图层名称关键或全称
- /// </summary>
- private string ImportLayerName()
- {
- string importLayerName = string.Empty;
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- try
- {
- using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
- {
- PromptStringOptions pso = new PromptStringOptions(this.strMessage);
- PromptResult res = ed.GetString(pso);
- if (res.Status == PromptStatus.OK || res.Status == PromptStatus.None)
- {
- importLayerName = res.StringResult;
- }
- }
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- return importLayerName;
- }
|
|