- 积分
- 2124
- 明经币
- 个
- 注册时间
- 2011-4-12
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
-
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- namespace Base
- {
- public class Class1
- {
- //图层控制
- #region layercontral
- //关闭所选择实体所在图层
- [CommandMethod("layeroff")]
- public static void layeroff()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
-
- PromptSelectionOptions options = new PromptSelectionOptions();
- options.MessageForAdding = "\n请选择要关闭的图层";
- PromptSelectionResult result = ed.GetSelection(options);
- if (result.Status == PromptStatus.OK)
- {
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
- foreach (SelectedObject obj in result.Value)
- {
- Entity ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead)as Entity;
- LayerTableRecord ltr = trans.GetObject(ent.LayerId, OpenMode.ForWrite) as LayerTableRecord;
- ltr.IsOff = true;
- }
- trans.Commit();
- }
- }
- }
- //仅显示所选择实体所在图层
- [CommandMethod("layeron")]
- public static void layeron()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
-
- PromptSelectionOptions options = new PromptSelectionOptions();
- options.MessageForAdding = "\n请选择要显示的图层";
- PromptSelectionResult result = ed.GetSelection(options);
- if (result.Status == PromptStatus.OK)
- {
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
- foreach (ObjectId id in lt)
- {
- LayerTableRecord ltr2 = trans.GetObject(id, OpenMode.ForRead) as LayerTableRecord;
- if (ltr2.IsOff == false)
- {
- ltr2.UpgradeOpen();
- ltr2.IsOff = true;
- }
- }
- foreach (SelectedObject obj in result.Value)
- {
- Entity ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead) as Entity;
- LayerTableRecord ltr = trans.GetObject(ent.LayerId, OpenMode.ForWrite) as LayerTableRecord;
- ltr.IsOff = false;
-
-
- }
- trans.Commit();
- }
- }
- }
- //显示所有图层
- [CommandMethod("layerall")]
- public static void layerall()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
- foreach (ObjectId id in lt)
- {
- LayerTableRecord ltr = id.GetObject(OpenMode.ForRead) as LayerTableRecord;
- if (ltr.IsOff == true)
- {
- ltr.UpgradeOpen();
- ltr.IsOff = false;
- }
- }
- trans.Commit();
- }
- }
- //若图层不存在则新建并置为当前,存在则置为当前
- [CommandMethod("newlayer")]
- public static void newlayer()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- string name;
- PromptResult result = ed.GetString("\n请输入图层名");
- if (result.Status == PromptStatus.OK)
- {
- name = result.StringResult;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
- LayerTableRecord ltr = new LayerTableRecord();
- if (lt.Has(name)==true)
- {
- db.Clayer=lt[name];
- }
- else
- {
- ltr.Name = name;
- lt.Add(ltr);
- trans.AddNewlyCreatedDBObject(ltr, true );
- db.Clayer = ltr.ObjectId;
- }
- trans.Commit();
- }
- }
-
- }
- #endregion
- }
- }
|
评分
-
查看全部评分
|