CAD.NET API一日一练(3)图层控制
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
//关闭所选择实体所在图层
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();
}
}
}
//仅显示所选择实体所在图层
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();
}
}
}
//显示所有图层
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();
}
}
//若图层不存在则新建并置为当前,存在则置为当前
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;
}
else
{
ltr.Name = name;
lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true );
db.Clayer = ltr.ObjectId;
}
trans.Commit();
}
}
}
#endregion
}
}
来学习了,多谢分享代码 感谢分享代码 感谢分享代码 好贴,一定支持。 感谢分享代码。学习了。对初学者十分有用。
页:
[1]