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已完成!");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值