010集——关于图层( 新建图层、获取图层名)(CAD—C#二次开发入门)
新建图层,指定图层颜色、获取所有图层名的操作:
如图所示,通过代码已将所有图层放入一个字符串类型的list列表中。
代码如下:
using AcTools;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
//using System.Windows.Forms;
namespace Acdemo
{
public class Acdemo
{
public static void SendACommandToAutoCAD(string message)
{
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
// 绘制一个圆并缩放图形 Draws a circle and zooms to the extents or
// limits of the drawing
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(message);
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(message);
acDoc.SendStringToExecute("._circle 2,2,0 4 ", true, false, false);
//SendKeys.SendWait("^{A}");
// SendKeys.SendWait("{ESC}");
acDoc.SendStringToExecute("._zoom _e ", true, false, false);
//for (int i = 0; i < 1000; i++)
//{
// SendKeys.SendWait("{ESC}");
//}
}
public static List<string> Getlayername()
{
List<string> lname = new List<string>();
// 获得当前文档和数据库 Get the current document and database
Document Doc = Application.DocumentManager.MdiActiveDocument;
Database db = Doc.Database;
// 启动一个事务 Start a transaction
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// 以只读方式打开图层表 Open the Layer table for read
LayerTable lt = tr.GetObject(db.LayerTableId,
OpenMode.ForRead) as LayerTable;
// string sLayerNames = "";
foreach (ObjectId acObjId in lt)
{
LayerTableRecord ltr = tr.GetObject(acObjId,
OpenMode.ForRead) as LayerTableRecord;
lname.Add(ltr.Name);
// sLayerNames = sLayerNames + "\n" + ltr.Name;
}
return lname;
// Application.ShowAlertDialog("The layers in this drawing are: " + sLayerNames);
// Dispose of the transaction
}
}
[CommandMethod("xx")]
public void Getlayer()
{
Database db = HostApplicationServices.WorkingDatabase;
List<string> lname = db.GetLayername ();
}
}
}
封装函数如下:
public static List<string> GetLayername(this Database db)
{
List<string> list = new List<string>();
Document mdiActiveDocument = Application.DocumentManager.MdiActiveDocument;
using Transaction transaction = db.TransactionManager.StartTransaction();
LayerTable layerTable = transaction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (ObjectId item in layerTable)
{
LayerTableRecord layerTableRecord = transaction.GetObject(item, OpenMode.ForRead) as LayerTableRecord;
list.Add(layerTableRecord.Name);
}
return list;
}
新建图层代码如下:
using AcTools;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
//using System.Windows.Forms;
namespace Acdemo
{
public class Acdemo
{
public static string AddLayer( string layname, short colorindex)
{
if (colorindex < 0 || colorindex > 256)
{
colorindex = 0;
}
Database db = HostApplicationServices.WorkingDatabase;
Document mdiActiveDocument = Application.DocumentManager.MdiActiveDocument;
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
LayerTable layerTable = transaction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
if (!layerTable.Has(layname))
{
LayerTableRecord layerTableRecord = new LayerTableRecord();
layerTableRecord.Color = Color.FromColorIndex(ColorMethod.ByAci, colorindex);
layerTableRecord.Name = layname;
layerTable.UpgradeOpen();
layerTable.Add(layerTableRecord);
transaction.AddNewlyCreatedDBObject(layerTableRecord, add: true);
}
}
return layname;
}
[CommandMethod("xx")]
public void Getlayer()
{
Database db = HostApplicationServices.WorkingDatabase;
AddLayer("新图层1",202);
}
}
}
设置实体图层:
代码如下:
using AcTools;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
//using System.Windows.Forms;
namespace Acdemo
{
public class Acdemo
{
public static string SetLayer(Entity ent, string layname)
{
Database db = HostApplicationServices.WorkingDatabase;
Document mdiActiveDocument = Application.DocumentManager.MdiActiveDocument;
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
LayerTable layerTable = transaction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
if (!layerTable.Has(layname))
{
LayerTableRecord layerTableRecord = new LayerTableRecord();
layerTableRecord.Color = Color.FromColorIndex(ColorMethod.ByAci, 0);
layerTableRecord.Name = layname;
layerTable.UpgradeOpen();
layerTable.Add(layerTableRecord);
transaction.AddNewlyCreatedDBObject(layerTableRecord, add: true);
}
// 以只读方式打开块表 Open the Block table for read
BlockTable acBlkTbl = transaction.GetObject(db.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
BlockTableRecord acBlkTblRec = transaction.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 设置图层
ent.Layer = layname;
acBlkTblRec.AppendEntity(ent);
transaction.AddNewlyCreatedDBObject(ent, true);
// Save the changes and dispose of the transaction
transaction.Commit();
}
return layname;
}
[CommandMethod("xx")]
public void Getlayer()
{
Database db = HostApplicationServices.WorkingDatabase;
// AddLayer("新图层1",202);
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Line line = new Line(Point3d.Origin ,new Point3d (100,100,0));
SetLayer((Entity)line,"aa");
}
}
}
Application.DocumentManager .MdiActiveDocument.SendStringToExecute("._zoom _e ", true, false, false);
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n已完成!");